Mercurial > repos > bgruening > openbabel_addh
comparison ob_spectrophore_search.py @ 0:ada6daa717d2 draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 01da22e4184a5a6f6a3dd4631a7b9c31d1b6d502
author | bgruening |
---|---|
date | Sat, 20 May 2017 08:37:26 -0400 |
parents | |
children | a3c767e30424 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ada6daa717d2 |
---|---|
1 #!/usr/bin/env python | |
2 """ | |
3 Input: tabular format file with one column storing the unique id for the compounds and any other with the Spectrophores(TM) descriptors. | |
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. | |
5 Copyright 2012, Bjoern Gruening and Xavier Lucas | |
6 """ | |
7 import sys, os | |
8 import argparse | |
9 import openbabel | |
10 openbabel.obErrorLog.StopLogging() | |
11 import pybel | |
12 import math | |
13 import numpy as np | |
14 | |
15 #TODO get rid of eval() | |
16 | |
17 global spectrophore | |
18 spectrophore = pybel.ob.OBSpectrophore() | |
19 | |
20 def parse_command_line(): | |
21 parser = argparse.ArgumentParser() | |
22 parser.add_argument('--target', required=True, help='target file name in sdf format with Spectrophores(TM) descriptors stored as meta-data') | |
23 parser.add_argument('--library', required=True, help='library of compounds with pre-computed physico-chemical properties, including Spectrophores(TM) in tabular format') | |
24 parser.add_argument('-c', '--column', required=True, type=int, help='#column containing the Spectrophores(TM) descriptors in the library file') | |
25 parser.add_argument('-o', '--output', required=True, help='output file name') | |
26 parser.add_argument('-n', '--normalization', default="ZeroMeanAndUnitStd", choices=['No', 'ZeroMean', 'UnitStd', 'ZeroMeanAndUnitStd'], help='Normalization method') | |
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') | |
28 parser.add_argument('-s', '--stereo', default="No", choices=['No', 'Unique', 'Mirror', 'All'], help='Stereospecificity of the cage') | |
29 parser.add_argument('-r', '--resolution', type=float, default="3.0", help='Resolution') | |
30 return parser.parse_args() | |
31 | |
32 def set_parameters(args): | |
33 if args.normalization == 'No': | |
34 spectrophore.SetNormalization( spectrophore.NoNormalization ) | |
35 else: | |
36 spectrophore.SetNormalization( eval('spectrophore.NormalizationTowards' + args.normalization) ) | |
37 spectrophore.SetAccuracy( eval('spectrophore.AngStepSize' + args.accuracy) ) | |
38 spectrophore.SetStereo( eval('spectrophore.' + args.stereo + 'StereoSpecificProbes') ) | |
39 spectrophore.SetResolution( args.resolution ) | |
40 return True | |
41 | |
42 def Compute_Spectrophores_distance(target_spectrophore, args): | |
43 outfile = open(args.output, 'w') | |
44 for mol in open(args.library, 'r'): | |
45 try: | |
46 distance = ( ( np.asarray( target_spectrophore, dtype=float ) - np.asarray( mol.split('\t')[ args.column - 1 ].strip().split(', '), dtype=float) )**2).sum() | |
47 except ValueError: | |
48 distance = 0 | |
49 outfile.write( '%s\t%f\n' % (mol.strip(), distance ) ) | |
50 outfile.close() | |
51 | |
52 def __main__(): | |
53 """ | |
54 Computation of Spectrophores(TM) distances to a target molecule. | |
55 """ | |
56 args = parse_command_line() | |
57 # This sets up the parameters for the Spectrophore generation. Parameters are set to fit those of our standard parsing tool | |
58 set_parameters(args) | |
59 | |
60 mol = pybel.readfile('sdf', args.target).next() | |
61 target_spectrophore = mol.data["Spectrophores(TM)"].strip().split(', ') | |
62 # Compute the paired-distance between every molecule in the library and the target | |
63 distances = Compute_Spectrophores_distance(target_spectrophore, args) | |
64 | |
65 if __name__ == "__main__" : | |
66 __main__() |