Coverage for SynRBL/mcs.py: 27%
30 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-11 11:17 +0200
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-11 11:17 +0200
1import logging
3from SynRBL.SynMCSImputer.SubStructure.mcs_process import ensemble_mcs
4from SynRBL.SynMCSImputer.SubStructure.extract_common_mcs import ExtractMCS
5from SynRBL.SynMCSImputer.MissingGraph.find_graph_dict import find_graph_dict
7logger = logging.getLogger(__name__)
10class MCS:
11 def __init__(self, id_col, solved_col="solved", mcs_data_col="mcs", n_jobs=-1):
12 self.id_col = id_col
13 self.solved_col = solved_col
14 self.mcs_data_col = mcs_data_col
15 self.n_jobs = n_jobs
17 self.conditions = [
18 {
19 "RingMatchesRingOnly": True,
20 "CompleteRingsOnly": True,
21 "method": "MCIS",
22 "sort": "MCIS",
23 "ignore_bond_order": True,
24 },
25 # {
26 # "RingMatchesRingOnly": True,
27 # "CompleteRingsOnly": True,
28 # "method": "MCIS",
29 # "sort": "MCIS",
30 # "ignore_bond_order": False,
31 # },
32 {
33 "RingMatchesRingOnly": False,
34 "CompleteRingsOnly": False,
35 "method": "MCIS",
36 "sort": "MCIS",
37 "ignore_bond_order": True,
38 },
39 # {
40 # "RingMatchesRingOnly": False,
41 # "CompleteRingsOnly": False,
42 # "method": "MCIS",
43 # "sort": "MCIS",
44 # "ignore_bond_order": False,
45 # },
46 {"method": "MCES", "sort": "MCES"},
47 ]
49 def find(self, reactions):
50 mcs_reactions = [
51 reactions[key]
52 for key, value in enumerate(reactions)
53 if value["carbon_balance_check"] != "balanced"
54 or (
55 value["carbon_balance_check"] == "balanced"
56 and not value[self.solved_col]
57 )
58 ]
59 if len(mcs_reactions) == 0:
60 return reactions
61 logger.info(
62 "Find maximum-common-substructure for {} reactions.".format(
63 len(mcs_reactions)
64 )
65 )
67 condition_results = ensemble_mcs(
68 mcs_reactions, self.conditions, n_jobs=self.n_jobs, Timeout=60
69 )
71 analysis = ExtractMCS()
72 mcs_dict, _ = analysis.extract_matching_conditions(
73 0,
74 100,
75 *condition_results,
76 extraction_method="largest_mcs",
77 using_threshold=True,
78 )
79 if len(mcs_dict) == 0:
80 return reactions
82 missing_results_largest = find_graph_dict(mcs_dict)
84 assert len(mcs_dict) == len(missing_results_largest)
85 for i, r in enumerate(missing_results_largest):
86 _id = int(mcs_reactions[i][self.id_col])
87 r["sorted_reactants"] = mcs_dict[i]["sorted_reactants"]
88 r["mcs_results"] = mcs_dict[i]["mcs_results"]
89 reactions[_id][self.mcs_data_col] = r
91 return reactions