annotate modify/ob_genProp.py @ 1:b52872a1755a draft

update cheminfolib dependency
author bgruening
date Wed, 21 Aug 2013 03:06:02 -0400
parents 527ecd2fc500
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
1 #!/usr/bin/env python
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
2 """
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
3 Input: Molecular input file.
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
4 Output: Physico-chemical properties are computed and stored as metadata in the sdf output file.
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
5 Copyright 2012, Bjoern Gruening and Xavier Lucas
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
6 """
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
7 import sys, os
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
8 import argparse
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
9 import openbabel
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
10 openbabel.obErrorLog.StopLogging()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
11 import pybel
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
12 import cheminfolib
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
13
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
14
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
15 def parse_command_line(argv):
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
16 parser = argparse.ArgumentParser()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
17 parser.add_argument('--iformat', default='sdf' , help='input file format')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
18 parser.add_argument('-i', '--input', required=True, help='input file name')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
19 parser.add_argument('--oformat', default='sdf', choices = ['sdf', 'table'] , help='output file format')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
20 parser.add_argument('--header', type=bool, help='Include the header as the first line of the output table')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
21 parser.add_argument('-o', '--output', required=True, help='output file name')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
22 return parser.parse_args()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
23
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
24 def compute_properties(args):
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
25 if args.oformat == 'sdf':
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
26 outfile = pybel.Outputfile(args.oformat, args.output, overwrite=True)
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
27 else:
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
28 outfile = open(args.output, 'w')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
29 if args.header:
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
30 mol = pybel.readfile(args.iformat, args.input).next()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
31 metadata = cheminfolib.get_properties_ext(mol)
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
32 outfile.write( '%s\n' % '\t'.join( [ cheminfolib.ColumnNames[key] for key in metadata ] ) )
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
33
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
34 for mol in pybel.readfile(args.iformat, args.input):
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
35 if mol.OBMol.NumHvyAtoms() > 5:
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
36 metadata = cheminfolib.get_properties_ext(mol)
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
37 if args.oformat == 'sdf':
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
38 [ mol.data.update( { cheminfolib.ColumnNames[key] : metadata[key] } ) for key in metadata ]
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
39 outfile.write(mol)
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
40 else:
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
41 outfile.write( '%s\n' % ('\t'.join( [ str(metadata[key]) for key in metadata ] ) ) )
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
42 outfile.close()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
43
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
44 def __main__():
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
45 """
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
46 Physico-chemical properties are computed and stored as metadata in the sdf output file
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
47 """
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
48 args = parse_command_line(sys.argv)
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
49 compute_properties(args)
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
50
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
51 if __name__ == "__main__" :
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
52 __main__()