Skip to content

Commit a9aad69

Browse files
committed
Merge branch 'main' of github.com:Wang-Bioinformatics-Lab/ModiFinder_base
2 parents d94bbf0 + a70f392 commit a9aad69

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

modifinder/runner.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"""
44

55
from modifinder import ModiFinder, Compound, BasicEvaluationEngine
6+
from modifinder.utilities import visualizer as mf_vis
67
from modifinder.utilities.general_utils import entropy
8+
import matplotlib.pyplot as plt
79
import pandas as pd
810
import pickle
911
import concurrent.futures
@@ -27,7 +29,7 @@ def load_Compound_from_cache(data, cache = None, **kwargs):
2729
return Compound(data, **kwargs)
2830

2931
def run_single(match_index, network = None, networkUnknowns = None, unknown_compound = None, known_compounds = None,
30-
helpers = None, cached_compounds = None, match_meta = None, **kwargs):
32+
helpers = None, cached_compounds = None, match_meta = None, output_dir = None, images_name = None, **kwargs):
3133
"""
3234
Run the modifinder algorithm on the given input data and return the result as a dictionary.
3335
"""
@@ -44,7 +46,7 @@ def run_single(match_index, network = None, networkUnknowns = None, unknown_comp
4446
if network is not None:
4547
mf = ModiFinder(network = network, networkUnknowns = networkUnknowns, **kwargs)
4648
else:
47-
mf = ModiFinder()
49+
mf = ModiFinder(**kwargs)
4850
unknown_compound = load_Compound_from_cache(unknown_compound, cached_compounds, **kwargs)
4951
if unknown_compound is None:
5052
raise ValueError("Unknown compound is not found.")
@@ -94,24 +96,40 @@ def run_single(match_index, network = None, networkUnknowns = None, unknown_comp
9496

9597
for known_index, node in enumerate(knowns):
9698
try:
97-
probs = mf.generate_probabilities(unknown_id=unknown_id, known_id=node, **kwargs)
98-
data = mf.get_meta_data(unknown_id=unknown_id, known_id=node)
9999
result = {
100100
"match_index": match_index,
101101
"unknown_id": unknown_id,
102102
"known_id": node,
103103
}
104+
known_compound = mf.network.nodes[node]["compound"]
105+
if abs(known_compound.spectrum.precursor_mz - unknown_compound.spectrum.precursor_mz) < 0.1:
106+
# exact match
107+
probs = [0] * len(known_compounds)
108+
result["entropy"] = None
109+
else:
110+
probs = mf.generate_probabilities(unknown_id=unknown_id, known_id=node, **kwargs)
111+
result["entropy"] = entropy(probs)
112+
data = mf.get_meta_data(unknown_id=unknown_id, known_id=node)
113+
try:
114+
if images_name is not None:
115+
if isinstance(images_name, str):
116+
data['image_path'] = os.path.join(output_dir, images_name + str(match_index) + ".png")
117+
else:
118+
data['image_path'] = os.path.join(output_dir, images_name[known_index])
119+
png = mf_vis.draw_molecule_heatmap(known_compound.structure, probs, show_labels=True, shrink_labels=True, show_legend=False)
120+
plt.imsave(data['image_path'], png)
121+
except Exception as err:
122+
data['image_path'] = str(err)
123+
pass
104124
result.update(data)
105-
106125
if unknown_compound.structure:
107126
for method in ["is_max", "average_distance"]:
108-
known_compound = mf.network.nodes[node]["compound"]
109127
evaluation_result = evaluation_engine.evaluate_single(known_compound.structure, unknown_compound.structure,
110128
probs, evaluation_method=method, **kwargs)
111129
result[method] = evaluation_result
112130

113131
# add entropy of the probabilities
114-
result["entropy"] = entropy(probs)
132+
115133
result["error"] = "No Issues"
116134
final_result[known_index] = result
117135

@@ -137,7 +155,7 @@ def run_batch(matches, output_dir, file_name, save_pickle = True, save_csv = Tru
137155
result = []
138156
for match in matches:
139157
try:
140-
result.extend(run_single(**match, cached_compounds = cached_data))
158+
result.extend(run_single(**match, cached_compounds = cached_data, output_dir = output_dir))
141159
except Exception as err:
142160
result.append({"error": "Error in run_single: " + str(err), "match_index": match["match_index"]})
143161

0 commit comments

Comments
 (0)