comparison ob_spectrophore_search.py @ 7:fac2c28b4c55 draft default tip

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/openbabel commit d9c51279c061a1da948a2582d5b502ca7573adbf
author bgruening
date Thu, 15 Aug 2024 11:04:16 +0000
parents a5f4b80e6769
children
comparison
equal deleted inserted replaced
6:5486f7a2b0cb 7:fac2c28b4c55
6 """ 6 """
7 import argparse 7 import argparse
8 8
9 import numpy as np 9 import numpy as np
10 from openbabel import openbabel, pybel 10 from openbabel import openbabel, pybel
11
11 openbabel.obErrorLog.StopLogging() 12 openbabel.obErrorLog.StopLogging()
12 # TODO get rid of eval() 13 # TODO get rid of eval()
13 14
14 global spectrophore 15 global spectrophore
15 spectrophore = pybel.ob.OBSpectrophore() 16 spectrophore = pybel.ob.OBSpectrophore()
16 17
17 18
18 def parse_command_line(): 19 def parse_command_line():
19 parser = argparse.ArgumentParser() 20 parser = argparse.ArgumentParser()
20 parser.add_argument('--target', required=True, help='target file name in sdf format with Spectrophores(TM) descriptors stored as meta-data') 21 parser.add_argument(
21 parser.add_argument('--library', required=True, help='library of compounds with pre-computed physico-chemical properties, including Spectrophores(TM) in tabular format') 22 "--target",
22 parser.add_argument('-c', '--column', required=True, type=int, help='#column containing the Spectrophores(TM) descriptors in the library file') 23 required=True,
23 parser.add_argument('-o', '--output', required=True, help='output file name') 24 help="target file name in sdf format with Spectrophores(TM) descriptors stored as meta-data",
24 parser.add_argument('-n', '--normalization', default="ZeroMeanAndUnitStd", choices=['No', 'ZeroMean', 'UnitStd', 'ZeroMeanAndUnitStd'], help='Normalization method') 25 )
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') 26 parser.add_argument(
26 parser.add_argument('-s', '--stereo', default="No", choices=['No', 'Unique', 'Mirror', 'All'], help='Stereospecificity of the cage') 27 "--library",
27 parser.add_argument('-r', '--resolution', type=float, default="3.0", help='Resolution') 28 required=True,
29 help="library of compounds with pre-computed physico-chemical properties, including Spectrophores(TM) in tabular format",
30 )
31 parser.add_argument(
32 "-c",
33 "--column",
34 required=True,
35 type=int,
36 help="#column containing the Spectrophores(TM) descriptors in the library file",
37 )
38 parser.add_argument("-o", "--output", required=True, help="output file name")
39 parser.add_argument(
40 "-n",
41 "--normalization",
42 default="ZeroMeanAndUnitStd",
43 choices=["No", "ZeroMean", "UnitStd", "ZeroMeanAndUnitStd"],
44 help="Normalization method",
45 )
46 parser.add_argument(
47 "-a",
48 "--accuracy",
49 default="20",
50 choices=["1", "2", "5", "10", "15", "20", "30", "36", "45", "60"],
51 help="Accuracy expressed as angular stepsize",
52 )
53 parser.add_argument(
54 "-s",
55 "--stereo",
56 default="No",
57 choices=["No", "Unique", "Mirror", "All"],
58 help="Stereospecificity of the cage",
59 )
60 parser.add_argument(
61 "-r", "--resolution", type=float, default="3.0", help="Resolution"
62 )
28 return parser.parse_args() 63 return parser.parse_args()
29 64
30 65
31 def set_parameters(args): 66 def set_parameters(args):
32 if args.normalization == 'No': 67 if args.normalization == "No":
33 spectrophore.SetNormalization(spectrophore.NoNormalization) 68 spectrophore.SetNormalization(spectrophore.NoNormalization)
34 else: 69 else:
35 spectrophore.SetNormalization(eval('spectrophore.NormalizationTowards' + args.normalization)) 70 spectrophore.SetNormalization(
36 spectrophore.SetAccuracy(eval('spectrophore.AngStepSize' + args.accuracy)) 71 eval("spectrophore.NormalizationTowards" + args.normalization)
37 spectrophore.SetStereo(eval('spectrophore.' + args.stereo + 'StereoSpecificProbes')) 72 )
73 spectrophore.SetAccuracy(eval("spectrophore.AngStepSize" + args.accuracy))
74 spectrophore.SetStereo(eval("spectrophore." + args.stereo + "StereoSpecificProbes"))
38 spectrophore.SetResolution(args.resolution) 75 spectrophore.SetResolution(args.resolution)
39 return True 76 return True
40 77
41 78
42 def Compute_Spectrophores_distance(target_spectrophore, args): 79 def Compute_Spectrophores_distance(target_spectrophore, args):
43 outfile = open(args.output, 'w') 80 outfile = open(args.output, "w")
44 for mol in open(args.library, 'r'): 81 for mol in open(args.library, "r"):
45 try: 82 try:
46 distance = ((np.asarray(target_spectrophore, dtype=float) - np.asarray(mol.split('\t')[args.column - 1].strip().split(', '), dtype=float))**2).sum() 83 distance = (
84 (
85 np.asarray(target_spectrophore, dtype=float)
86 - np.asarray(
87 mol.split("\t")[args.column - 1].strip().split(", "),
88 dtype=float,
89 )
90 )
91 ** 2
92 ).sum()
47 except ValueError: 93 except ValueError:
48 distance = 0 94 distance = 0
49 outfile.write('%s\t%f\n' % (mol.strip(), distance)) 95 outfile.write("%s\t%f\n" % (mol.strip(), distance))
50 outfile.close() 96 outfile.close()
51 97
52 98
53 def __main__(): 99 def __main__():
54 """ 100 """
55 Computation of Spectrophores(TM) distances to a target molecule. 101 Computation of Spectrophores(TM) distances to a target molecule.
56 """ 102 """
57 args = parse_command_line() 103 args = parse_command_line()
58 # This sets up the parameters for the Spectrophore generation. Parameters are set to fit those of our standard parsing tool 104 # This sets up the parameters for the Spectrophore generation. Parameters are set to fit those of our standard parsing tool
59 set_parameters(args) 105 set_parameters(args)
60 106
61 mol = next(pybel.readfile('sdf', args.target)) 107 mol = next(pybel.readfile("sdf", args.target))
62 target_spectrophore = mol.data["Spectrophores(TM)"].strip().split(', ') 108 target_spectrophore = mol.data["Spectrophores(TM)"].strip().split(", ")
63 # Compute the paired-distance between every molecule in the library and the target 109 # Compute the paired-distance between every molecule in the library and the target
64 Compute_Spectrophores_distance(target_spectrophore, args) 110 Compute_Spectrophores_distance(target_spectrophore, args)
65 111
66 112
67 if __name__ == "__main__": 113 if __name__ == "__main__":