Coverage for SynRBL/SynMCSImputer/merge.py: 89%
79 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
1from .rules import MergeRule, ExpandRule, CompoundRule
2from .structure import Boundary, Compound, CompoundSet
5class NoExpandRule(Exception):
6 def __str__(self):
7 return "No expand rule found."
10def expand_boundary(boundary: Boundary) -> Compound:
11 compound = None
12 for rule in ExpandRule.get_all():
13 if rule.can_apply(boundary):
14 compound = rule.apply()
15 break
16 if compound is None:
17 raise NoExpandRule()
18 return compound
21def merge_boundaries(boundary1: Boundary, boundary2: Boundary) -> Compound | None:
22 for rule in MergeRule.get_all():
23 if not rule.can_apply(boundary1, boundary2):
24 continue
25 return rule.apply(boundary1, boundary2)
26 return None
29def update_compound(compound: Compound):
30 for rule in CompoundRule.get_all():
31 if rule.can_apply(compound):
32 rule.apply(compound)
33 break
36def _merge_one_compound(compound: Compound) -> Compound:
37 merged_compound = compound
38 while len(merged_compound.boundaries) > 0:
39 boundary1 = merged_compound.boundaries[0]
40 try:
41 compound2 = expand_boundary(boundary1)
42 if len(compound2.boundaries) != 1:
43 raise NotImplementedError(
44 "Compound expansion and merge is only supported for "
45 + "compounds with a single boundary atom."
46 )
47 merged_compound = merge_boundaries(boundary1, compound2.boundaries[0])
48 except NoExpandRule:
49 # If no compound rule is found, leave the compound as is
50 merged_compound.update(merged_compound.mol, boundary1)
51 pass
52 if merged_compound is None:
53 raise ValueError("No merge rule found.")
54 return merged_compound
57def _merge_two_compounds(compound1: Compound, compound2: Compound) -> Compound:
58 boundaries1 = compound1.boundaries
59 boundaries2 = compound2.boundaries
60 merged_compound = None
61 if len(boundaries1) != 1:
62 raise NotImplementedError(
63 ("Can only merge compounds with single boundary atom. ({})").format(
64 len(boundaries1)
65 )
66 )
67 if len(boundaries1) != len(boundaries2):
68 if compound1.num_compounds == 1 and compound2.num_compounds == 1:
69 # If boundaries don't match, try to expand-merge them.
70 # This is only safe if the smiles contains only one compound,
71 # otherwise MCS was probably wrong.
72 compound1 = _merge_one_compound(compound1)
73 compound2 = _merge_one_compound(compound2)
74 compound1.concat(compound2)
75 merged_compound = compound1
76 else:
77 raise ValueError(
78 (
79 "Can not merge compounds with unequal "
80 + "number of boundaries. ({} != {})."
81 ).format(len(boundaries1), len(boundaries2))
82 )
83 else:
84 merged_compound = merge_boundaries(boundaries1[0], boundaries2[0])
85 if merged_compound is None:
86 raise ValueError("No merge rule found.")
87 return merged_compound
90def merge(compound_set: CompoundSet) -> Compound:
91 merged_compound = None
93 comps_with_boundaries, comps_without_boundaries = [], []
94 removed_rules = []
95 for c in compound_set.compounds:
96 update_compound(c)
97 if not c.active:
98 removed_rules.extend(c.rules)
99 continue
100 if len(c.boundaries) == 0:
101 comps_without_boundaries.append(c)
102 else:
103 comps_with_boundaries.append(c)
105 if len(comps_with_boundaries) == 1:
106 merged_compound = _merge_one_compound(comps_with_boundaries[0])
107 elif len(comps_with_boundaries) == 2:
108 merged_compound = _merge_two_compounds(
109 comps_with_boundaries[0], comps_with_boundaries[1]
110 )
112 if merged_compound is None:
113 raise NotImplementedError(
114 "Merging {} compounds is not supported.".format(len(comps_with_boundaries))
115 )
117 merged_compound.rules = removed_rules + merged_compound.rules
118 for c in comps_without_boundaries:
119 merged_compound.concat(c)
120 return merged_compound