changeset 2:58d18838e244 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/chemicaltoolbox/sucos commit 6f1ee2812cca091561a2b2e464498dae2f913b8d"
author bgruening
date Thu, 19 Mar 2020 07:25:25 -0400
parents 334ad24525db
children bd12f4b4c3a8
files sucos.py sucos_max.py
diffstat 2 files changed, 46 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/sucos.py	Fri Oct 11 18:26:08 2019 -0400
+++ b/sucos.py	Thu Mar 19 07:25:25 2020 -0400
@@ -109,17 +109,21 @@
     fm_score = get_FeatureMapScore(ref_features, query_features, tani, score_mode)
     fm_score = np.clip(fm_score, 0, 1)
 
-    if tani:
-        tani_sim = 1 - float(rdShapeHelpers.ShapeTanimotoDist(ref_mol, query_mol))
-        tani_sim = np.clip(tani_sim, 0, 1)
-        SuCOS_score = 0.5*fm_score + 0.5*tani_sim
-        return SuCOS_score, fm_score, tani_sim
-    else:
-        protrude_dist = rdShapeHelpers.ShapeProtrudeDist(ref_mol, query_mol, allowReordering=False)
-        protrude_dist = np.clip(protrude_dist, 0, 1)
-        protrude_val = 1.0 - protrude_dist
-        SuCOS_score = 0.5 * fm_score + 0.5 * protrude_val
-        return SuCOS_score, fm_score, protrude_val
+    try :
+        if tani:
+            tani_sim = 1 - float(rdShapeHelpers.ShapeTanimotoDist(ref_mol, query_mol))
+            tani_sim = np.clip(tani_sim, 0, 1)
+            SuCOS_score = 0.5*fm_score + 0.5*tani_sim
+            return SuCOS_score, fm_score, tani_sim
+        else:
+            protrude_dist = rdShapeHelpers.ShapeProtrudeDist(ref_mol, query_mol, allowReordering=False)
+            protrude_dist = np.clip(protrude_dist, 0, 1)
+            protrude_val = 1.0 - protrude_dist
+            SuCOS_score = 0.5 * fm_score + 0.5 * protrude_val
+            return SuCOS_score, fm_score, protrude_val
+    except:
+        utils.log("Failed to calculate SuCOS scores. Returning 0,0,0")
+        return 0, 0, 0
 
 def process(refmol_filename, inputs_filename, outputs_filename, refmol_index=None,
             refmol_format=None, tani=False, score_mode=FeatMaps.FeatMapScoreMode.All):
--- a/sucos_max.py	Fri Oct 11 18:26:08 2019 -0400
+++ b/sucos_max.py	Thu Mar 19 07:25:25 2020 -0400
@@ -39,8 +39,7 @@
 from rdkit import Chem
 
 
-def process(inputfilename, clusterfilenames, outputfilename, mode):
-
+def process(inputfilename, clusterfilenames, outputfilename):
     all_clusters = {}
     for filename in clusterfilenames:
         cluster = []
@@ -79,7 +78,8 @@
         except:
             utils.log("WARNING: failed to generate features for molecule", mol_num, "in input")
             continue
-        scores = [0, 0, 0]
+        scores_max = [0, 0, 0]
+        scores_cum = [0, 0, 0]
         for clusterfilename in all_clusters:
             cluster = all_clusters[clusterfilename]
             index = 0
@@ -89,42 +89,35 @@
                 index += 1
                 comparisons += 1
                 sucos_score, fm_score, vol_score = sucos.get_SucosScore(hit, mol,
-                    tani=False, ref_features=ref_features, query_features=query_features)
-                if mode == 'max':
-                    if sucos_score > scores[0]:
-                        scores[0] = sucos_score
-                        scores[1] = fm_score
-                        scores[2] = vol_score
-                        cluster_name = clusterfilename
-                        cluster_index = index
-                elif mode == 'cum':
-                    scores[0] += sucos_score
-                    scores[1] += fm_score
-                    scores[2] += vol_score
-                else:
-                    raise ValueError("Invalid mode: " + mode)
+                                                                        tani=False, ref_features=ref_features,
+                                                                        query_features=query_features)
+
+                if sucos_score > scores_max[0]:
+                    scores_max[0] = sucos_score
+                    scores_max[1] = fm_score
+                    scores_max[2] = vol_score
+                    cluster_name = clusterfilename
+                    cluster_index = index
+
+                scores_cum[0] += sucos_score
+                scores_cum[1] += fm_score
+                scores_cum[2] += vol_score
 
-        if scores[0] > 0:
-            if mode == 'max':
-                cluster_file_name_only = cluster_name.split(os.sep)[-1]
-                #utils.log("Max SuCOS:", scores[0], "FM:", scores[1], "P:", scores[2],"File:", cluster_file_name_only, "Index:", cluster_index)
-                mol.SetDoubleProp("Max_SuCOS_Score", scores[0])
-                mol.SetDoubleProp("Max_SuCOS_FeatureMap_Score", scores[1])
-                mol.SetDoubleProp("Max_SuCOS_Protrude_Score", scores[2])
-                mol.SetProp("Max_SuCOS_Cluster", cluster_file_name_only)
-                mol.SetIntProp("Max_SuCOS_Index", cluster_index)
+        if scores_max[0] > 0:
+            cluster_file_name_only = cluster_name.split(os.sep)[-1]
+            # utils.log("Max SuCOS:", scores[0], "FM:", scores[1], "P:", scores[2],"File:", cluster_file_name_only, "Index:", cluster_index)
+            mol.SetDoubleProp("Max_SuCOS_Score", scores_max[0])
+            mol.SetDoubleProp("Max_SuCOS_FeatureMap_Score", scores_max[1])
+            mol.SetDoubleProp("Max_SuCOS_Protrude_Score", scores_max[2])
+            mol.SetProp("Max_SuCOS_Cluster", cluster_file_name_only)
+            mol.SetIntProp("Max_SuCOS_Index", cluster_index)
 
-            else:
-                #utils.log("Cum SuCOS:", scores[0], "FM:", scores[1], "P:", scores[2])
-                mol.SetDoubleProp("Cum_SuCOS_Score", scores[0])
-                mol.SetDoubleProp("Cum_SuCOS_FeatureMap_Score", scores[1])
-                mol.SetDoubleProp("Cum_SuCOS_Protrude_Score", scores[2])
+            # utils.log("Cum SuCOS:", scores[0], "FM:", scores[1], "P:", scores[2])
+            mol.SetDoubleProp("Cum_SuCOS_Score", scores_cum[0])
+            mol.SetDoubleProp("Cum_SuCOS_FeatureMap_Score", scores_cum[1])
+            mol.SetDoubleProp("Cum_SuCOS_Protrude_Score", scores_cum[2])
 
-            writer.write(mol)
-
-        else:
-            utils.log("Molecule", mol_num, "did not overlay. Omitting from results")
-
+        writer.write(mol)
 
     input_file.close()
     writer.flush()
@@ -140,15 +133,13 @@
     parser = argparse.ArgumentParser(description='Max SuCOS scores with RDKit')
     parser.add_argument('-i', '--input', help='Input file to score in SDF format. Can be gzipped (*.gz).')
     parser.add_argument('-o', '--output', help='Output file in SDF format. Can be gzipped (*.gz).')
-    parser.add_argument('-m', '--mode', choices=['max', 'cum'],
-                        default='max', help='Score mode: max = best score, cum = sum of all scores')
     parser.add_argument('clusters', nargs='*', help="One or more SDF files with the clustered hits")
 
     args = parser.parse_args()
     utils.log("Max SuCOS Args: ", args)
 
-    process(args.input, args.clusters, args.output, args.mode)
+    process(args.input, args.clusters, args.output)
 
 
 if __name__ == "__main__":
-    main()
\ No newline at end of file
+    main()