annotate rdkit_descriptors.py @ 0:764340994e71

Uploaded
author bgruening
date Sat, 27 Apr 2013 09:01:41 -0400
parents
children 45b822b9d522
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
764340994e71 Uploaded
bgruening
parents:
diff changeset
1 #!/usr/bin/env python
764340994e71 Uploaded
bgruening
parents:
diff changeset
2
764340994e71 Uploaded
bgruening
parents:
diff changeset
3 from rdkit.Chem import Descriptors
764340994e71 Uploaded
bgruening
parents:
diff changeset
4 from rdkit import Chem
764340994e71 Uploaded
bgruening
parents:
diff changeset
5 import sys, os, re
764340994e71 Uploaded
bgruening
parents:
diff changeset
6 import argparse
764340994e71 Uploaded
bgruening
parents:
diff changeset
7 import inspect
764340994e71 Uploaded
bgruening
parents:
diff changeset
8
764340994e71 Uploaded
bgruening
parents:
diff changeset
9 def get_supplier( infile, format = 'smiles' ):
764340994e71 Uploaded
bgruening
parents:
diff changeset
10 with open(infile) as handle:
764340994e71 Uploaded
bgruening
parents:
diff changeset
11 for line in handle:
764340994e71 Uploaded
bgruening
parents:
diff changeset
12 line = line.strip()
764340994e71 Uploaded
bgruening
parents:
diff changeset
13 if format == 'smiles':
764340994e71 Uploaded
bgruening
parents:
diff changeset
14 mol = Chem.MolFromSmiles( line, sanitize=True )
764340994e71 Uploaded
bgruening
parents:
diff changeset
15 elif format == 'inchi':
764340994e71 Uploaded
bgruening
parents:
diff changeset
16 mol = Chem.inchi.MolFromInchi( line, sanitize=True, removeHs=True, logLevel=None, treatWarningAsError=False )
764340994e71 Uploaded
bgruening
parents:
diff changeset
17 if mol is None:
764340994e71 Uploaded
bgruening
parents:
diff changeset
18 yield False
764340994e71 Uploaded
bgruening
parents:
diff changeset
19 else:
764340994e71 Uploaded
bgruening
parents:
diff changeset
20 mol.SetProp( '_Name', line.split('\t')[0] )
764340994e71 Uploaded
bgruening
parents:
diff changeset
21 yield mol
764340994e71 Uploaded
bgruening
parents:
diff changeset
22
764340994e71 Uploaded
bgruening
parents:
diff changeset
23
764340994e71 Uploaded
bgruening
parents:
diff changeset
24 def get_rdkit_descriptor_functions():
764340994e71 Uploaded
bgruening
parents:
diff changeset
25 ret = [ (name, f) for name, f in inspect.getmembers( Descriptors ) if inspect.isfunction( f ) and not name.startswith( '_' ) ]
764340994e71 Uploaded
bgruening
parents:
diff changeset
26 ret.sort()
764340994e71 Uploaded
bgruening
parents:
diff changeset
27 return ret
764340994e71 Uploaded
bgruening
parents:
diff changeset
28
764340994e71 Uploaded
bgruening
parents:
diff changeset
29
764340994e71 Uploaded
bgruening
parents:
diff changeset
30 def descriptors( mol, functions ):
764340994e71 Uploaded
bgruening
parents:
diff changeset
31 """
764340994e71 Uploaded
bgruening
parents:
diff changeset
32 Calculates the descriptors of a given molecule.
764340994e71 Uploaded
bgruening
parents:
diff changeset
33 """
764340994e71 Uploaded
bgruening
parents:
diff changeset
34
764340994e71 Uploaded
bgruening
parents:
diff changeset
35 for name, function in functions:
764340994e71 Uploaded
bgruening
parents:
diff changeset
36 yield (name, function( mol ))
764340994e71 Uploaded
bgruening
parents:
diff changeset
37
764340994e71 Uploaded
bgruening
parents:
diff changeset
38
764340994e71 Uploaded
bgruening
parents:
diff changeset
39 if __name__ == "__main__":
764340994e71 Uploaded
bgruening
parents:
diff changeset
40 parser = argparse.ArgumentParser()
764340994e71 Uploaded
bgruening
parents:
diff changeset
41 parser.add_argument('-i', '--infile', required=True, help='Path to the input file.')
764340994e71 Uploaded
bgruening
parents:
diff changeset
42 parser.add_argument("--iformat", help="Specify the input file format.")
764340994e71 Uploaded
bgruening
parents:
diff changeset
43
764340994e71 Uploaded
bgruening
parents:
diff changeset
44 parser.add_argument('-o', '--outfile', type=argparse.FileType('w+'),
764340994e71 Uploaded
bgruening
parents:
diff changeset
45 default=sys.stdout, help="path to the result file, default it sdtout")
764340994e71 Uploaded
bgruening
parents:
diff changeset
46
764340994e71 Uploaded
bgruening
parents:
diff changeset
47 parser.add_argument("--header", dest="header", action="store_true",
764340994e71 Uploaded
bgruening
parents:
diff changeset
48 default=False,
764340994e71 Uploaded
bgruening
parents:
diff changeset
49 help="Write header line.")
764340994e71 Uploaded
bgruening
parents:
diff changeset
50
764340994e71 Uploaded
bgruening
parents:
diff changeset
51 args = parser.parse_args()
764340994e71 Uploaded
bgruening
parents:
diff changeset
52
764340994e71 Uploaded
bgruening
parents:
diff changeset
53 if args.iformat == 'sdf':
764340994e71 Uploaded
bgruening
parents:
diff changeset
54 supplier = Chem.SDMolSupplier( args.infile )
764340994e71 Uploaded
bgruening
parents:
diff changeset
55 elif args.iformat =='smi':
764340994e71 Uploaded
bgruening
parents:
diff changeset
56 supplier = get_supplier( args.infile, format = 'smiles' )
764340994e71 Uploaded
bgruening
parents:
diff changeset
57 elif args.iformat == 'inchi':
764340994e71 Uploaded
bgruening
parents:
diff changeset
58 supplier = get_supplier( args.infile, format = 'inchi' )
764340994e71 Uploaded
bgruening
parents:
diff changeset
59
764340994e71 Uploaded
bgruening
parents:
diff changeset
60 functions = get_rdkit_descriptor_functions()
764340994e71 Uploaded
bgruening
parents:
diff changeset
61
764340994e71 Uploaded
bgruening
parents:
diff changeset
62 if args.header:
764340994e71 Uploaded
bgruening
parents:
diff changeset
63 args.outfile.write( '%s\n' % '\t'.join( [name for name, f in functions] ) )
764340994e71 Uploaded
bgruening
parents:
diff changeset
64
764340994e71 Uploaded
bgruening
parents:
diff changeset
65 for mol in supplier:
764340994e71 Uploaded
bgruening
parents:
diff changeset
66 if not mol:
764340994e71 Uploaded
bgruening
parents:
diff changeset
67 continue
764340994e71 Uploaded
bgruening
parents:
diff changeset
68 descs = descriptors( mol, functions )
764340994e71 Uploaded
bgruening
parents:
diff changeset
69 name = mol.GetProp("_Name")
764340994e71 Uploaded
bgruening
parents:
diff changeset
70 args.outfile.write( "%s\n" % '\t'.join( [name]+ [str(res) for name, res in descs] ) )
764340994e71 Uploaded
bgruening
parents:
diff changeset
71