0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 import os, sys
|
|
4 import argparse
|
|
5
|
|
6 def main(args ):
|
|
7
|
|
8 begin = True
|
|
9 iid = 0
|
|
10 graph_counter = 1
|
|
11
|
|
12 for line in args.infile:
|
|
13 if line.rstrip():
|
|
14 if line.strip().endswith('END'):
|
|
15 begin = False
|
|
16 elif line.strip() == '$$$$':
|
|
17 graph_counter += 1
|
|
18 iid = 0
|
|
19 else:
|
|
20 # found header line, like: 21 21 0 0 0 0 0 0 0 0999 V2000
|
|
21 if len(line.split()) >= 5 and line.split()[-1] == 'V2000':
|
|
22 args.outfile.write('t # id %s\n' % graph_counter)
|
|
23 begin=True
|
|
24 continue
|
|
25 # connection or coordinate/atom table
|
|
26 if len(line.split()) >= 4 and begin:
|
|
27 # coordinate/atom table
|
|
28 if line.split()[3].isalpha():
|
|
29 args.outfile.write( 'v %s %s \n' % (iid, line.split()[3]) )
|
|
30 iid += 1
|
|
31 else:
|
|
32 #connection table
|
|
33 id, node, edge, trash = line.split(None, 3)
|
|
34 args.outfile.write( 'e %s %s %s\n' % ( int(id) - 1 , int(node) -1, edge ) )
|
|
35
|
|
36 if __name__ == "__main__":
|
|
37 parser = argparse.ArgumentParser()
|
|
38 parser.add_argument('--infile', nargs='?', type=argparse.FileType('r'),
|
|
39 default=sys.stdin, help="Specify one or more input files")
|
|
40 parser.add_argument('--outfile', type=argparse.FileType('w'),
|
|
41 default=sys.stdout, help="Specify one output file")
|
|
42 args = parser.parse_args()
|
|
43 main( args )
|