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