annotate rdkit_descriptors.py @ 5:dcb775f2d56a

Uploaded
author bgruening
date Sat, 11 May 2013 17:15:05 -0400
parents 45b822b9d522
children
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' ):
1
45b822b9d522 Uploaded
bgruening
parents: 0
diff changeset
10 """
45b822b9d522 Uploaded
bgruening
parents: 0
diff changeset
11 Returns a generator over a SMILES or InChI file. Every element is of RDKit
45b822b9d522 Uploaded
bgruening
parents: 0
diff changeset
12 molecule and has its original string as _Name property.
45b822b9d522 Uploaded
bgruening
parents: 0
diff changeset
13 """
0
764340994e71 Uploaded
bgruening
parents:
diff changeset
14 with open(infile) as handle:
764340994e71 Uploaded
bgruening
parents:
diff changeset
15 for line in handle:
764340994e71 Uploaded
bgruening
parents:
diff changeset
16 line = line.strip()
764340994e71 Uploaded
bgruening
parents:
diff changeset
17 if format == 'smiles':
764340994e71 Uploaded
bgruening
parents:
diff changeset
18 mol = Chem.MolFromSmiles( line, sanitize=True )
764340994e71 Uploaded
bgruening
parents:
diff changeset
19 elif format == 'inchi':
764340994e71 Uploaded
bgruening
parents:
diff changeset
20 mol = Chem.inchi.MolFromInchi( line, sanitize=True, removeHs=True, logLevel=None, treatWarningAsError=False )
764340994e71 Uploaded
bgruening
parents:
diff changeset
21 if mol is None:
764340994e71 Uploaded
bgruening
parents:
diff changeset
22 yield False
764340994e71 Uploaded
bgruening
parents:
diff changeset
23 else:
764340994e71 Uploaded
bgruening
parents:
diff changeset
24 mol.SetProp( '_Name', line.split('\t')[0] )
764340994e71 Uploaded
bgruening
parents:
diff changeset
25 yield mol
764340994e71 Uploaded
bgruening
parents:
diff changeset
26
764340994e71 Uploaded
bgruening
parents:
diff changeset
27
764340994e71 Uploaded
bgruening
parents:
diff changeset
28 def get_rdkit_descriptor_functions():
1
45b822b9d522 Uploaded
bgruening
parents: 0
diff changeset
29 """
45b822b9d522 Uploaded
bgruening
parents: 0
diff changeset
30 Returns all descriptor functions under the Chem.Descriptors Module as tuple of (name, function)
45b822b9d522 Uploaded
bgruening
parents: 0
diff changeset
31 """
0
764340994e71 Uploaded
bgruening
parents:
diff changeset
32 ret = [ (name, f) for name, f in inspect.getmembers( Descriptors ) if inspect.isfunction( f ) and not name.startswith( '_' ) ]
764340994e71 Uploaded
bgruening
parents:
diff changeset
33 ret.sort()
764340994e71 Uploaded
bgruening
parents:
diff changeset
34 return ret
764340994e71 Uploaded
bgruening
parents:
diff changeset
35
764340994e71 Uploaded
bgruening
parents:
diff changeset
36
764340994e71 Uploaded
bgruening
parents:
diff changeset
37 def descriptors( mol, functions ):
764340994e71 Uploaded
bgruening
parents:
diff changeset
38 """
764340994e71 Uploaded
bgruening
parents:
diff changeset
39 Calculates the descriptors of a given molecule.
764340994e71 Uploaded
bgruening
parents:
diff changeset
40 """
764340994e71 Uploaded
bgruening
parents:
diff changeset
41 for name, function in functions:
764340994e71 Uploaded
bgruening
parents:
diff changeset
42 yield (name, function( mol ))
764340994e71 Uploaded
bgruening
parents:
diff changeset
43
764340994e71 Uploaded
bgruening
parents:
diff changeset
44
764340994e71 Uploaded
bgruening
parents:
diff changeset
45 if __name__ == "__main__":
764340994e71 Uploaded
bgruening
parents:
diff changeset
46 parser = argparse.ArgumentParser()
764340994e71 Uploaded
bgruening
parents:
diff changeset
47 parser.add_argument('-i', '--infile', required=True, help='Path to the input file.')
764340994e71 Uploaded
bgruening
parents:
diff changeset
48 parser.add_argument("--iformat", help="Specify the input file format.")
764340994e71 Uploaded
bgruening
parents:
diff changeset
49
764340994e71 Uploaded
bgruening
parents:
diff changeset
50 parser.add_argument('-o', '--outfile', type=argparse.FileType('w+'),
764340994e71 Uploaded
bgruening
parents:
diff changeset
51 default=sys.stdout, help="path to the result file, default it sdtout")
764340994e71 Uploaded
bgruening
parents:
diff changeset
52
764340994e71 Uploaded
bgruening
parents:
diff changeset
53 parser.add_argument("--header", dest="header", action="store_true",
764340994e71 Uploaded
bgruening
parents:
diff changeset
54 default=False,
764340994e71 Uploaded
bgruening
parents:
diff changeset
55 help="Write header line.")
764340994e71 Uploaded
bgruening
parents:
diff changeset
56
764340994e71 Uploaded
bgruening
parents:
diff changeset
57 args = parser.parse_args()
764340994e71 Uploaded
bgruening
parents:
diff changeset
58
764340994e71 Uploaded
bgruening
parents:
diff changeset
59 if args.iformat == 'sdf':
764340994e71 Uploaded
bgruening
parents:
diff changeset
60 supplier = Chem.SDMolSupplier( args.infile )
764340994e71 Uploaded
bgruening
parents:
diff changeset
61 elif args.iformat =='smi':
764340994e71 Uploaded
bgruening
parents:
diff changeset
62 supplier = get_supplier( args.infile, format = 'smiles' )
764340994e71 Uploaded
bgruening
parents:
diff changeset
63 elif args.iformat == 'inchi':
764340994e71 Uploaded
bgruening
parents:
diff changeset
64 supplier = get_supplier( args.infile, format = 'inchi' )
764340994e71 Uploaded
bgruening
parents:
diff changeset
65
764340994e71 Uploaded
bgruening
parents:
diff changeset
66 functions = get_rdkit_descriptor_functions()
764340994e71 Uploaded
bgruening
parents:
diff changeset
67
764340994e71 Uploaded
bgruening
parents:
diff changeset
68 if args.header:
764340994e71 Uploaded
bgruening
parents:
diff changeset
69 args.outfile.write( '%s\n' % '\t'.join( [name for name, f in functions] ) )
764340994e71 Uploaded
bgruening
parents:
diff changeset
70
764340994e71 Uploaded
bgruening
parents:
diff changeset
71 for mol in supplier:
764340994e71 Uploaded
bgruening
parents:
diff changeset
72 if not mol:
764340994e71 Uploaded
bgruening
parents:
diff changeset
73 continue
764340994e71 Uploaded
bgruening
parents:
diff changeset
74 descs = descriptors( mol, functions )
1
45b822b9d522 Uploaded
bgruening
parents: 0
diff changeset
75 molecule_id = mol.GetProp("_Name")
45b822b9d522 Uploaded
bgruening
parents: 0
diff changeset
76 args.outfile.write( "%s\n" % '\t'.join( [molecule_id]+ [str(res) for name, res in descs] ) )
0
764340994e71 Uploaded
bgruening
parents:
diff changeset
77