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