diff ob_spectrophore_search.py @ 15:c5de6c19eb06 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:00:46 +0000
parents 12aca74f07d7
children
line wrap: on
line diff
--- a/ob_spectrophore_search.py	Tue Nov 10 20:30:47 2020 +0000
+++ b/ob_spectrophore_search.py	Thu Aug 15 11:00:46 2024 +0000
@@ -8,6 +8,7 @@
 
 import numpy as np
 from openbabel import openbabel, pybel
+
 openbabel.obErrorLog.StopLogging()
 # TODO get rid of eval()
 
@@ -17,49 +18,94 @@
 
 def parse_command_line():
     parser = argparse.ArgumentParser()
-    parser.add_argument('--target', required=True, help='target file name in sdf format with Spectrophores(TM) descriptors stored as meta-data')
-    parser.add_argument('--library', required=True, help='library of compounds with pre-computed physico-chemical properties, including Spectrophores(TM) in tabular format')
-    parser.add_argument('-c', '--column', required=True, type=int, help='#column containing the Spectrophores(TM) descriptors in the library file')
-    parser.add_argument('-o', '--output', required=True, help='output file name')
-    parser.add_argument('-n', '--normalization', default="ZeroMeanAndUnitStd", choices=['No', 'ZeroMean', 'UnitStd', 'ZeroMeanAndUnitStd'], help='Normalization method')
-    parser.add_argument('-a', '--accuracy', default="20", choices=['1', '2', '5', '10', '15', '20', '30', '36', '45', '60'], help='Accuracy expressed as angular stepsize')
-    parser.add_argument('-s', '--stereo', default="No", choices=['No', 'Unique', 'Mirror', 'All'], help='Stereospecificity of the cage')
-    parser.add_argument('-r', '--resolution', type=float, default="3.0", help='Resolution')
+    parser.add_argument(
+        "--target",
+        required=True,
+        help="target file name in sdf format with Spectrophores(TM) descriptors stored as meta-data",
+    )
+    parser.add_argument(
+        "--library",
+        required=True,
+        help="library of compounds with pre-computed physico-chemical properties, including Spectrophores(TM) in tabular format",
+    )
+    parser.add_argument(
+        "-c",
+        "--column",
+        required=True,
+        type=int,
+        help="#column containing the Spectrophores(TM) descriptors in the library file",
+    )
+    parser.add_argument("-o", "--output", required=True, help="output file name")
+    parser.add_argument(
+        "-n",
+        "--normalization",
+        default="ZeroMeanAndUnitStd",
+        choices=["No", "ZeroMean", "UnitStd", "ZeroMeanAndUnitStd"],
+        help="Normalization method",
+    )
+    parser.add_argument(
+        "-a",
+        "--accuracy",
+        default="20",
+        choices=["1", "2", "5", "10", "15", "20", "30", "36", "45", "60"],
+        help="Accuracy expressed as angular stepsize",
+    )
+    parser.add_argument(
+        "-s",
+        "--stereo",
+        default="No",
+        choices=["No", "Unique", "Mirror", "All"],
+        help="Stereospecificity of the cage",
+    )
+    parser.add_argument(
+        "-r", "--resolution", type=float, default="3.0", help="Resolution"
+    )
     return parser.parse_args()
 
 
 def set_parameters(args):
-    if args.normalization == 'No':
+    if args.normalization == "No":
         spectrophore.SetNormalization(spectrophore.NoNormalization)
     else:
-        spectrophore.SetNormalization(eval('spectrophore.NormalizationTowards' + args.normalization))
-    spectrophore.SetAccuracy(eval('spectrophore.AngStepSize' + args.accuracy))
-    spectrophore.SetStereo(eval('spectrophore.' + args.stereo + 'StereoSpecificProbes'))
+        spectrophore.SetNormalization(
+            eval("spectrophore.NormalizationTowards" + args.normalization)
+        )
+    spectrophore.SetAccuracy(eval("spectrophore.AngStepSize" + args.accuracy))
+    spectrophore.SetStereo(eval("spectrophore." + args.stereo + "StereoSpecificProbes"))
     spectrophore.SetResolution(args.resolution)
     return True
 
 
 def Compute_Spectrophores_distance(target_spectrophore, args):
-    outfile = open(args.output, 'w')
-    for mol in open(args.library, 'r'):
+    outfile = open(args.output, "w")
+    for mol in open(args.library, "r"):
         try:
-            distance = ((np.asarray(target_spectrophore, dtype=float) - np.asarray(mol.split('\t')[args.column - 1].strip().split(', '), dtype=float))**2).sum()
+            distance = (
+                (
+                    np.asarray(target_spectrophore, dtype=float)
+                    - np.asarray(
+                        mol.split("\t")[args.column - 1].strip().split(", "),
+                        dtype=float,
+                    )
+                )
+                ** 2
+            ).sum()
         except ValueError:
             distance = 0
-        outfile.write('%s\t%f\n' % (mol.strip(), distance))
+        outfile.write("%s\t%f\n" % (mol.strip(), distance))
     outfile.close()
 
 
 def __main__():
     """
-        Computation of Spectrophores(TM) distances to a target molecule.
+    Computation of Spectrophores(TM) distances to a target molecule.
     """
     args = parse_command_line()
     # This sets up the parameters for the Spectrophore generation. Parameters are set to fit those of our standard parsing tool
     set_parameters(args)
 
-    mol = next(pybel.readfile('sdf', args.target))
-    target_spectrophore = mol.data["Spectrophores(TM)"].strip().split(', ')
+    mol = next(pybel.readfile("sdf", args.target))
+    target_spectrophore = mol.data["Spectrophores(TM)"].strip().split(", ")
     # Compute the paired-distance between every molecule in the library and the target
     Compute_Spectrophores_distance(target_spectrophore, args)