annotate search/ob_spectrophore_search.py @ 2:125da3a296ca draft default tip

Uploaded
author bgruening
date Wed, 15 Jul 2015 12:13:08 -0400
parents 527ecd2fc500
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
1 #!/usr/bin/env python
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
2 """
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
3 Input: tabular format file with one column storing the unique id for the compounds and any other with the Spectrophores(TM) descriptors.
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
4 Output: parse the target file using the same protocol used to generate the databases in our servers. Physico-chemical properties are computed and stored as metadata in the sdf output file.
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
5 Copyright 2012, Bjoern Gruening and Xavier Lucas
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
6 """
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
7 import sys, os
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
8 import argparse
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
9 import openbabel
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
10 openbabel.obErrorLog.StopLogging()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
11 import pybel
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
12 import math
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
13 import numpy as np
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
14
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
15 #TODO get rid of eval()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
16
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
17 global spectrophore
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
18 spectrophore = pybel.ob.OBSpectrophore()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
19
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
20 def parse_command_line():
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
21 parser = argparse.ArgumentParser()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
22 parser.add_argument('--target', required=True, help='target file name in sdf format with Spectrophores(TM) descriptors stored as meta-data')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
23 parser.add_argument('--library', required=True, help='library of compounds with pre-computed physico-chemical properties, including Spectrophores(TM) in tabular format')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
24 parser.add_argument('-c', '--column', required=True, type=int, help='#column containing the Spectrophores(TM) descriptors in the library file')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
25 parser.add_argument('-o', '--output', required=True, help='output file name')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
26 parser.add_argument('-n', '--normalization', default="ZeroMeanAndUnitStd", choices=['No', 'ZeroMean', 'UnitStd', 'ZeroMeanAndUnitStd'], help='Normalization method')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
27 parser.add_argument('-a', '--accuracy', default="20", choices=['1', '2', '5', '10', '15', '20', '30', '36', '45', '60'], help='Accuracy expressed as angular stepsize')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
28 parser.add_argument('-s', '--stereo', default="No", choices=['No', 'Unique', 'Mirror', 'All'], help='Stereospecificity of the cage')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
29 parser.add_argument('-r', '--resolution', type=float, default="3.0", help='Resolution')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
30 return parser.parse_args()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
31
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
32 def set_parameters(args):
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
33 if args.normalization == 'No':
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
34 spectrophore.SetNormalization( spectrophore.NoNormalization )
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
35 else:
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
36 spectrophore.SetNormalization( eval('spectrophore.NormalizationTowards' + args.normalization) )
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
37 spectrophore.SetAccuracy( eval('spectrophore.AngStepSize' + args.accuracy) )
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
38 spectrophore.SetStereo( eval('spectrophore.' + args.stereo + 'StereoSpecificProbes') )
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
39 spectrophore.SetResolution( args.resolution )
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
40 return True
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
41
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
42 def Compute_Spectrophores_distance(target_spectrophore, args):
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
43 outfile = open(args.output, 'w')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
44 for mol in open(args.library, 'r'):
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
45 try:
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
46 distance = ( ( np.asarray( target_spectrophore, dtype=float ) - np.asarray( mol.split('\t')[ args.column - 1 ].strip().split(', '), dtype=float) )**2).sum()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
47 except ValueError:
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
48 distance = 0
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
49 outfile.write( '%s\t%f\n' % (mol.strip(), distance ) )
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
50 outfile.close()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
51
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
52 def __main__():
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
53 """
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
54 Computation of Spectrophores(TM) distances to a target molecule.
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
55 """
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
56 args = parse_command_line()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
57 # This sets up the parameters for the Spectrophore generation. Parameters are set to fit those of our standard parsing tool
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
58 set_parameters(args)
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
59
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
60 mol = pybel.readfile('sdf', args.target).next()
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
61 target_spectrophore = mol.data["Spectrophores(TM)"].strip().split(', ')
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
62 # Compute the paired-distance between every molecule in the library and the target
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
63 distances = Compute_Spectrophores_distance(target_spectrophore, args)
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
64
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
65 if __name__ == "__main__" :
527ecd2fc500 Uploaded
bgruening
parents:
diff changeset
66 __main__()