annotate mol2gspan.py @ 0:99091a5d5c84 draft

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