annotate rdkit_descriptors.py @ 1:2f212f34b80c draft default tip

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