comparison ob_spectrophore_search.py @ 13:9ce9f996b941 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit 1fe240ef0064a1a4a66d9be1ccace53824280b75"
author bgruening
date Mon, 19 Oct 2020 14:41:00 +0000
parents afd14e10a318
children f5d7ffbb2d33
comparison
equal deleted inserted replaced
12:afd14e10a318 13:9ce9f996b941
2 """ 2 """
3 Input: tabular format file with one column storing the unique id for the compounds and any other with the Spectrophores(TM) descriptors. 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. 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 5 Copyright 2012, Bjoern Gruening and Xavier Lucas
6 """ 6 """
7 import sys, os
8 import argparse 7 import argparse
9 import math 8
10 import numpy as np 9 import numpy as np
11
12 from openbabel import openbabel, pybel 10 from openbabel import openbabel, pybel
13 openbabel.obErrorLog.StopLogging() 11 openbabel.obErrorLog.StopLogging()
14 #TODO get rid of eval() 12 # TODO get rid of eval()
15 13
16 global spectrophore 14 global spectrophore
17 spectrophore = pybel.ob.OBSpectrophore() 15 spectrophore = pybel.ob.OBSpectrophore()
16
18 17
19 def parse_command_line(): 18 def parse_command_line():
20 parser = argparse.ArgumentParser() 19 parser = argparse.ArgumentParser()
21 parser.add_argument('--target', required=True, help='target file name in sdf format with Spectrophores(TM) descriptors stored as meta-data') 20 parser.add_argument('--target', required=True, help='target file name in sdf format with Spectrophores(TM) descriptors stored as meta-data')
22 parser.add_argument('--library', required=True, help='library of compounds with pre-computed physico-chemical properties, including Spectrophores(TM) in tabular format') 21 parser.add_argument('--library', required=True, help='library of compounds with pre-computed physico-chemical properties, including Spectrophores(TM) in tabular format')
26 parser.add_argument('-a', '--accuracy', default="20", choices=['1', '2', '5', '10', '15', '20', '30', '36', '45', '60'], help='Accuracy expressed as angular stepsize') 25 parser.add_argument('-a', '--accuracy', default="20", choices=['1', '2', '5', '10', '15', '20', '30', '36', '45', '60'], help='Accuracy expressed as angular stepsize')
27 parser.add_argument('-s', '--stereo', default="No", choices=['No', 'Unique', 'Mirror', 'All'], help='Stereospecificity of the cage') 26 parser.add_argument('-s', '--stereo', default="No", choices=['No', 'Unique', 'Mirror', 'All'], help='Stereospecificity of the cage')
28 parser.add_argument('-r', '--resolution', type=float, default="3.0", help='Resolution') 27 parser.add_argument('-r', '--resolution', type=float, default="3.0", help='Resolution')
29 return parser.parse_args() 28 return parser.parse_args()
30 29
30
31 def set_parameters(args): 31 def set_parameters(args):
32 if args.normalization == 'No': 32 if args.normalization == 'No':
33 spectrophore.SetNormalization( spectrophore.NoNormalization ) 33 spectrophore.SetNormalization(spectrophore.NoNormalization)
34 else: 34 else:
35 spectrophore.SetNormalization( eval('spectrophore.NormalizationTowards' + args.normalization) ) 35 spectrophore.SetNormalization(eval('spectrophore.NormalizationTowards' + args.normalization))
36 spectrophore.SetAccuracy( eval('spectrophore.AngStepSize' + args.accuracy) ) 36 spectrophore.SetAccuracy(eval('spectrophore.AngStepSize' + args.accuracy))
37 spectrophore.SetStereo( eval('spectrophore.' + args.stereo + 'StereoSpecificProbes') ) 37 spectrophore.SetStereo(eval('spectrophore.' + args.stereo + 'StereoSpecificProbes'))
38 spectrophore.SetResolution( args.resolution ) 38 spectrophore.SetResolution(args.resolution)
39 return True 39 return True
40
40 41
41 def Compute_Spectrophores_distance(target_spectrophore, args): 42 def Compute_Spectrophores_distance(target_spectrophore, args):
42 outfile = open(args.output, 'w') 43 outfile = open(args.output, 'w')
43 for mol in open(args.library, 'r'): 44 for mol in open(args.library, 'r'):
44 try: 45 try:
45 distance = ( ( np.asarray( target_spectrophore, dtype=float ) - np.asarray( mol.split('\t')[ args.column - 1 ].strip().split(', '), dtype=float) )**2).sum() 46 distance = ((np.asarray(target_spectrophore, dtype=float) - np.asarray(mol.split('\t')[args.column - 1].strip().split(', '), dtype=float))**2).sum()
46 except ValueError: 47 except ValueError:
47 distance = 0 48 distance = 0
48 outfile.write( '%s\t%f\n' % (mol.strip(), distance ) ) 49 outfile.write('%s\t%f\n' % (mol.strip(), distance))
49 outfile.close() 50 outfile.close()
51
50 52
51 def __main__(): 53 def __main__():
52 """ 54 """
53 Computation of Spectrophores(TM) distances to a target molecule. 55 Computation of Spectrophores(TM) distances to a target molecule.
54 """ 56 """
57 set_parameters(args) 59 set_parameters(args)
58 60
59 mol = next(pybel.readfile('sdf', args.target)) 61 mol = next(pybel.readfile('sdf', args.target))
60 target_spectrophore = mol.data["Spectrophores(TM)"].strip().split(', ') 62 target_spectrophore = mol.data["Spectrophores(TM)"].strip().split(', ')
61 # Compute the paired-distance between every molecule in the library and the target 63 # Compute the paired-distance between every molecule in the library and the target
62 distances = Compute_Spectrophores_distance(target_spectrophore, args) 64 Compute_Spectrophores_distance(target_spectrophore, args)
63 65
64 if __name__ == "__main__" : 66
67 if __name__ == "__main__":
65 __main__() 68 __main__()