0
|
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' ):
|
1
|
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 """
|
0
|
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():
|
1
|
29 """
|
|
30 Returns all descriptor functions under the Chem.Descriptors Module as tuple of (name, function)
|
|
31 """
|
0
|
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 )
|
1
|
75 molecule_id = mol.GetProp("_Name")
|
|
76 args.outfile.write( "%s\n" % '\t'.join( [molecule_id]+ [str(res) for name, res in descs] ) )
|
0
|
77
|