annotate ob_spectrophore_search.py @ 0:631cd009009b draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
author bgruening
date Tue, 31 May 2016 11:46:16 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
1 #!/usr/bin/env python
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
2 """
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
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.
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
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.
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
5 Copyright 2012, Bjoern Gruening and Xavier Lucas
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
6 """
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
7 import sys, os
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
8 import argparse
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
9 import openbabel
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
10 openbabel.obErrorLog.StopLogging()
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
11 import pybel
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
12 import math
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
13 import numpy as np
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
14
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
15 #TODO get rid of eval()
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
16
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
17 global spectrophore
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
18 spectrophore = pybel.ob.OBSpectrophore()
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
19
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
20 def parse_command_line():
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
21 parser = argparse.ArgumentParser()
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
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')
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
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')
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
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')
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
25 parser.add_argument('-o', '--output', required=True, help='output file name')
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
26 parser.add_argument('-n', '--normalization', default="ZeroMeanAndUnitStd", choices=['No', 'ZeroMean', 'UnitStd', 'ZeroMeanAndUnitStd'], help='Normalization method')
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
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')
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
28 parser.add_argument('-s', '--stereo', default="No", choices=['No', 'Unique', 'Mirror', 'All'], help='Stereospecificity of the cage')
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
29 parser.add_argument('-r', '--resolution', type=float, default="3.0", help='Resolution')
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
30 return parser.parse_args()
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
31
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
32 def set_parameters(args):
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
33 if args.normalization == 'No':
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
34 spectrophore.SetNormalization( spectrophore.NoNormalization )
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
35 else:
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
36 spectrophore.SetNormalization( eval('spectrophore.NormalizationTowards' + args.normalization) )
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
37 spectrophore.SetAccuracy( eval('spectrophore.AngStepSize' + args.accuracy) )
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
38 spectrophore.SetStereo( eval('spectrophore.' + args.stereo + 'StereoSpecificProbes') )
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
39 spectrophore.SetResolution( args.resolution )
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
40 return True
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
41
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
42 def Compute_Spectrophores_distance(target_spectrophore, args):
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
43 outfile = open(args.output, 'w')
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
44 for mol in open(args.library, 'r'):
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
45 try:
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
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()
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
47 except ValueError:
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
48 distance = 0
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
49 outfile.write( '%s\t%f\n' % (mol.strip(), distance ) )
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
50 outfile.close()
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
51
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
52 def __main__():
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
53 """
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
54 Computation of Spectrophores(TM) distances to a target molecule.
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
55 """
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
56 args = parse_command_line()
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
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
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
58 set_parameters(args)
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
59
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
60 mol = pybel.readfile('sdf', args.target).next()
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
61 target_spectrophore = mol.data["Spectrophores(TM)"].strip().split(', ')
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
62 # Compute the paired-distance between every molecule in the library and the target
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
63 distances = Compute_Spectrophores_distance(target_spectrophore, args)
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
64
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
65 if __name__ == "__main__" :
631cd009009b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 4a60df09ddf9dfcedad80f7b72a927198da2f755
bgruening
parents:
diff changeset
66 __main__()