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