sapiopycommons 2025.8.22a715__py3-none-any.whl → 2025.8.22a716__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of sapiopycommons might be problematic. Click here for more details.
- sapiopycommons/ai/tool_of_tools.py +917 -0
- sapiopycommons/callbacks/callback_util.py +26 -16
- sapiopycommons/chem/IndigoMolecules.py +12 -10
- sapiopycommons/chem/ps_commons.py +773 -0
- sapiopycommons/files/assay_plate_reader.py +93 -0
- sapiopycommons/files/file_text_converter.py +207 -0
- sapiopycommons/flowcyto/flow_cyto.py +2 -24
- sapiopycommons/general/accession_service.py +2 -28
- sapiopycommons/multimodal/multimodal.py +2 -24
- sapiopycommons/webhook/webservice_handlers.py +1 -1
- {sapiopycommons-2025.8.22a715.dist-info → sapiopycommons-2025.8.22a716.dist-info}/METADATA +2 -2
- {sapiopycommons-2025.8.22a715.dist-info → sapiopycommons-2025.8.22a716.dist-info}/RECORD +14 -45
- sapiopycommons/ai/converter_service_base.py +0 -131
- sapiopycommons/ai/protoapi/fielddefinitions/fields_pb2.py +0 -43
- sapiopycommons/ai/protoapi/fielddefinitions/fields_pb2.pyi +0 -31
- sapiopycommons/ai/protoapi/fielddefinitions/fields_pb2_grpc.py +0 -24
- sapiopycommons/ai/protoapi/fielddefinitions/velox_field_def_pb2.py +0 -123
- sapiopycommons/ai/protoapi/fielddefinitions/velox_field_def_pb2.pyi +0 -598
- sapiopycommons/ai/protoapi/fielddefinitions/velox_field_def_pb2_grpc.py +0 -24
- sapiopycommons/ai/protoapi/plan/converter/converter_pb2.py +0 -51
- sapiopycommons/ai/protoapi/plan/converter/converter_pb2.pyi +0 -63
- sapiopycommons/ai/protoapi/plan/converter/converter_pb2_grpc.py +0 -149
- sapiopycommons/ai/protoapi/plan/item/item_container_pb2.py +0 -55
- sapiopycommons/ai/protoapi/plan/item/item_container_pb2.pyi +0 -88
- sapiopycommons/ai/protoapi/plan/item/item_container_pb2_grpc.py +0 -24
- sapiopycommons/ai/protoapi/plan/script/script_pb2.py +0 -59
- sapiopycommons/ai/protoapi/plan/script/script_pb2.pyi +0 -102
- sapiopycommons/ai/protoapi/plan/script/script_pb2_grpc.py +0 -153
- sapiopycommons/ai/protoapi/plan/step_output_pb2.py +0 -45
- sapiopycommons/ai/protoapi/plan/step_output_pb2.pyi +0 -42
- sapiopycommons/ai/protoapi/plan/step_output_pb2_grpc.py +0 -24
- sapiopycommons/ai/protoapi/plan/step_pb2.py +0 -43
- sapiopycommons/ai/protoapi/plan/step_pb2.pyi +0 -43
- sapiopycommons/ai/protoapi/plan/step_pb2_grpc.py +0 -24
- sapiopycommons/ai/protoapi/plan/tool/entry_pb2.py +0 -41
- sapiopycommons/ai/protoapi/plan/tool/entry_pb2.pyi +0 -35
- sapiopycommons/ai/protoapi/plan/tool/entry_pb2_grpc.py +0 -24
- sapiopycommons/ai/protoapi/plan/tool/tool_pb2.py +0 -75
- sapiopycommons/ai/protoapi/plan/tool/tool_pb2.pyi +0 -237
- sapiopycommons/ai/protoapi/plan/tool/tool_pb2_grpc.py +0 -154
- sapiopycommons/ai/protoapi/session/sapio_conn_info_pb2.py +0 -39
- sapiopycommons/ai/protoapi/session/sapio_conn_info_pb2.pyi +0 -32
- sapiopycommons/ai/protoapi/session/sapio_conn_info_pb2_grpc.py +0 -24
- sapiopycommons/ai/protobuf_utils.py +0 -504
- sapiopycommons/ai/server.py +0 -104
- sapiopycommons/ai/test_client.py +0 -356
- sapiopycommons/ai/tool_service_base.py +0 -922
- {sapiopycommons-2025.8.22a715.dist-info → sapiopycommons-2025.8.22a716.dist-info}/WHEEL +0 -0
- {sapiopycommons-2025.8.22a715.dist-info → sapiopycommons-2025.8.22a716.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,773 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Parallel Synthesis Commons
|
|
3
|
+
Author: Yechen Qiao
|
|
4
|
+
"""
|
|
5
|
+
import dataclasses
|
|
6
|
+
import itertools
|
|
7
|
+
import json
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from indigo import IndigoObject, IndigoException
|
|
12
|
+
from sapiopycommons.chem.IndigoMolecules import indigo, get_aromatic_dearomatic_forms, renderer
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class SerializableQueryMolecule:
|
|
16
|
+
mol_block: str
|
|
17
|
+
smarts: str
|
|
18
|
+
render_svg: str
|
|
19
|
+
|
|
20
|
+
@staticmethod
|
|
21
|
+
def create(query_molecule: IndigoObject):
|
|
22
|
+
aromatic, dearomatic = get_aromatic_dearomatic_forms(query_molecule)
|
|
23
|
+
ret: SerializableQueryMolecule = SerializableQueryMolecule()
|
|
24
|
+
ret.mol_block = aromatic.molfile()
|
|
25
|
+
ret.smarts = aromatic.smarts()
|
|
26
|
+
ret.render_svg = renderer.renderToString(dearomatic)
|
|
27
|
+
return ret
|
|
28
|
+
|
|
29
|
+
def to_json(self) -> dict[str, Any]:
|
|
30
|
+
"""
|
|
31
|
+
Save the SerializableQueryMolecule to a JSON string.
|
|
32
|
+
:return: A JSON string representation of the query molecule.
|
|
33
|
+
"""
|
|
34
|
+
return {
|
|
35
|
+
"mol_block": self.mol_block,
|
|
36
|
+
"smarts": self.smarts,
|
|
37
|
+
"render_svg": self.render_svg
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class SerializableMoleculeMatch:
|
|
42
|
+
"""
|
|
43
|
+
A serializable match that stores and loads a match that can be serialized to JSON.
|
|
44
|
+
"""
|
|
45
|
+
_query_atom_to_atom: dict[int, int]
|
|
46
|
+
_query_bond_to_bond: dict[int, int]
|
|
47
|
+
_query_molecule_file: str
|
|
48
|
+
_matching_molecule_file: str
|
|
49
|
+
_query_molecule: IndigoObject
|
|
50
|
+
_matching_molecule: IndigoObject
|
|
51
|
+
_record_id: int # Only when received from Sapio.
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def record_id(self) -> int:
|
|
55
|
+
"""
|
|
56
|
+
Get the record ID of the match.
|
|
57
|
+
:return: The record ID.
|
|
58
|
+
"""
|
|
59
|
+
return self._record_id
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def query_atom_indexes(self) -> set[int]:
|
|
63
|
+
return set(self._query_atom_to_atom.keys())
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def matching_atom_indexes(self) -> set[int]:
|
|
67
|
+
return set(self._query_atom_to_atom.values())
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def matching_molecule_copy(self) -> IndigoObject:
|
|
71
|
+
return self._matching_molecule.clone()
|
|
72
|
+
|
|
73
|
+
def __str__(self):
|
|
74
|
+
return json.dumps(self.to_json())
|
|
75
|
+
|
|
76
|
+
def __hash__(self):
|
|
77
|
+
return hash(self._query_molecule.smarts())
|
|
78
|
+
|
|
79
|
+
def __eq__(self, other):
|
|
80
|
+
if not isinstance(other, SerializableMoleculeMatch):
|
|
81
|
+
return False
|
|
82
|
+
if self._query_atom_to_atom == other._query_atom_to_atom and \
|
|
83
|
+
self._query_bond_to_bond == other._query_bond_to_bond and \
|
|
84
|
+
self._query_molecule_file == other._query_molecule_file and \
|
|
85
|
+
self._matching_molecule_file == other._matching_molecule_file and \
|
|
86
|
+
self._record_id == other._record_id:
|
|
87
|
+
return True
|
|
88
|
+
if self._query_molecule.smarts() != other._query_molecule.smarts():
|
|
89
|
+
return False
|
|
90
|
+
return are_symmetrical_subs(self, other)
|
|
91
|
+
|
|
92
|
+
def mapAtom(self, atom: IndigoObject) -> IndigoObject | None:
|
|
93
|
+
if not self._query_atom_to_atom or atom.index() not in self._query_atom_to_atom:
|
|
94
|
+
return None
|
|
95
|
+
index = self._query_atom_to_atom[atom.index()]
|
|
96
|
+
return self._matching_molecule.getAtom(index)
|
|
97
|
+
|
|
98
|
+
def mapBond(self, bond: IndigoObject) -> IndigoObject | None:
|
|
99
|
+
if not self._query_bond_to_bond or bond.index() not in self._query_bond_to_bond:
|
|
100
|
+
return None
|
|
101
|
+
index = self._query_bond_to_bond[bond.index()]
|
|
102
|
+
return self._matching_molecule.getBond(index)
|
|
103
|
+
|
|
104
|
+
def to_json(self) -> dict[str, Any]:
|
|
105
|
+
"""
|
|
106
|
+
Save the SerializableMoleculeMatch to a JSON string.
|
|
107
|
+
:return: A JSON string representation of the match.
|
|
108
|
+
"""
|
|
109
|
+
return {
|
|
110
|
+
"query_molecule_file": self._query_molecule_file,
|
|
111
|
+
"matching_molecule_file": self._matching_molecule_file,
|
|
112
|
+
"query_atom_to_atom": self._query_atom_to_atom,
|
|
113
|
+
"query_bond_to_bond": self._query_bond_to_bond,
|
|
114
|
+
"record_id": self._record_id
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
@staticmethod
|
|
118
|
+
def from_json(json_dct: dict[str, Any]) -> 'SerializableMoleculeMatch':
|
|
119
|
+
"""
|
|
120
|
+
Load a SerializableMoleculeMatch from a JSON string.
|
|
121
|
+
:param json_dct: A JSON string representation of the match.
|
|
122
|
+
:return: A new SerializableMoleculeMatch instance.
|
|
123
|
+
"""
|
|
124
|
+
smm = SerializableMoleculeMatch()
|
|
125
|
+
smm._query_atom_to_atom = {}
|
|
126
|
+
for key, value in json_dct.get("query_atom_to_atom", {}).items():
|
|
127
|
+
smm._query_atom_to_atom[int(key)] = int(value)
|
|
128
|
+
smm._query_bond_to_bond = {}
|
|
129
|
+
for key, value in json_dct.get("query_bond_to_bond", {}).items():
|
|
130
|
+
smm._query_bond_to_bond[int(key)] = int(value)
|
|
131
|
+
smm._query_molecule_file = json_dct.get("query_molecule_file")
|
|
132
|
+
smm._matching_molecule_file = json_dct.get("matching_molecule_file")
|
|
133
|
+
smm._query_molecule = indigo.loadQueryMolecule(smm._query_molecule_file)
|
|
134
|
+
smm._matching_molecule = indigo.loadMolecule(smm._matching_molecule_file)
|
|
135
|
+
smm._record_id = json_dct.get("record_id", 0) # Default to 0 if not present
|
|
136
|
+
return smm
|
|
137
|
+
|
|
138
|
+
@staticmethod
|
|
139
|
+
def create(query_molecule: IndigoObject, matching_molecule: IndigoObject,
|
|
140
|
+
match: IndigoObject, query_mol_atom_index_filter: set[int] | None = None) -> 'SerializableMoleculeMatch':
|
|
141
|
+
"""
|
|
142
|
+
Create a SerializableMoleculeMatch from a query molecule, matching molecule, and match.
|
|
143
|
+
:param query_molecule: The query molecule.
|
|
144
|
+
:param matching_molecule: The matching molecule.
|
|
145
|
+
:param match: The match object containing atom mappings.
|
|
146
|
+
:param query_mol_atom_index_filter: Optional list of atom indexes to filter the query molecule atoms.
|
|
147
|
+
:return: A new SerializableMoleculeMatch instance.
|
|
148
|
+
"""
|
|
149
|
+
smm = SerializableMoleculeMatch()
|
|
150
|
+
smm._query_atom_to_atom = {}
|
|
151
|
+
smm._query_bond_to_bond = {}
|
|
152
|
+
smm._query_molecule = query_molecule.clone()
|
|
153
|
+
smm._matching_molecule = matching_molecule.clone()
|
|
154
|
+
smm._query_molecule_file = query_molecule.molfile()
|
|
155
|
+
smm._matching_molecule_file = matching_molecule.molfile()
|
|
156
|
+
smm._record_id = 0
|
|
157
|
+
|
|
158
|
+
for qatom in query_molecule.iterateAtoms():
|
|
159
|
+
if query_mol_atom_index_filter and qatom.index() not in query_mol_atom_index_filter:
|
|
160
|
+
continue
|
|
161
|
+
concrete_atom = match.mapAtom(qatom)
|
|
162
|
+
if concrete_atom is None:
|
|
163
|
+
continue
|
|
164
|
+
smm._query_atom_to_atom[qatom.index()] = concrete_atom.index()
|
|
165
|
+
|
|
166
|
+
qbond: IndigoObject
|
|
167
|
+
for qbond in query_molecule.iterateBonds():
|
|
168
|
+
if query_mol_atom_index_filter:
|
|
169
|
+
if (qbond.source().index() not in query_mol_atom_index_filter or
|
|
170
|
+
qbond.destination().index() not in query_mol_atom_index_filter):
|
|
171
|
+
continue
|
|
172
|
+
concrete_bond = match.mapBond(qbond)
|
|
173
|
+
if concrete_bond is None:
|
|
174
|
+
continue
|
|
175
|
+
smm._query_bond_to_bond[qbond.index()] = concrete_bond.index()
|
|
176
|
+
return smm
|
|
177
|
+
|
|
178
|
+
def get_matched_molecule_copy(self):
|
|
179
|
+
return self._matching_molecule.clone()
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def is_reaction_atom_map_completed(q_reaction: IndigoObject) -> bool:
|
|
183
|
+
"""
|
|
184
|
+
Tests each atom in product of query reaction.
|
|
185
|
+
:param q_reaction: The query reaction to test.
|
|
186
|
+
:return: True if and only if for every atom that is not an R-Site, it has a mapping number.
|
|
187
|
+
"""
|
|
188
|
+
for product in q_reaction.iterateProducts():
|
|
189
|
+
for atom in product.iterateAtoms():
|
|
190
|
+
if atom.isRSite():
|
|
191
|
+
continue
|
|
192
|
+
map_num = q_reaction.atomMappingNumber(atom)
|
|
193
|
+
if map_num == 0:
|
|
194
|
+
return False
|
|
195
|
+
return True
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
@dataclass
|
|
199
|
+
class ReplacementReaction:
|
|
200
|
+
"""
|
|
201
|
+
A replacement reaction stores reactio template with 1 reactant replaced by specific user match.
|
|
202
|
+
"""
|
|
203
|
+
reaction: IndigoObject
|
|
204
|
+
reaction_reactant: IndigoObject
|
|
205
|
+
replacement_reactant: IndigoObject
|
|
206
|
+
replacement_query_reaction_match: SerializableMoleculeMatch
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
# noinspection PyProtectedMember
|
|
210
|
+
def highlight_mol_substructure_serial_match(molecule: IndigoObject, serializable_match: SerializableMoleculeMatch):
|
|
211
|
+
"""
|
|
212
|
+
Highlight the substructure in the molecule based on the SerializableMoleculeMatch.
|
|
213
|
+
:param molecule: The molecule to highlight.
|
|
214
|
+
:param serializable_match: The SerializableMoleculeMatch containing atom mappings.
|
|
215
|
+
"""
|
|
216
|
+
for qatom in serializable_match._query_molecule.iterateAtoms():
|
|
217
|
+
atom = serializable_match.mapAtom(qatom)
|
|
218
|
+
if atom is None:
|
|
219
|
+
continue
|
|
220
|
+
atom.highlight()
|
|
221
|
+
|
|
222
|
+
for nei in atom.iterateNeighbors():
|
|
223
|
+
if not nei.isPseudoatom() and not nei.isRSite() and nei.atomicNumber() == 1:
|
|
224
|
+
nei.highlight()
|
|
225
|
+
nei.bond().highlight()
|
|
226
|
+
|
|
227
|
+
for bond in serializable_match._query_molecule.iterateBonds():
|
|
228
|
+
bond = serializable_match.mapBond(bond)
|
|
229
|
+
if bond is None:
|
|
230
|
+
continue
|
|
231
|
+
bond.highlight()
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def clear_highlights(molecule: IndigoObject):
|
|
235
|
+
"""
|
|
236
|
+
Clear all highlights in the molecule.
|
|
237
|
+
:param molecule: The molecule to clear highlights from.
|
|
238
|
+
"""
|
|
239
|
+
for atom in molecule.iterateAtoms():
|
|
240
|
+
atom.unhighlight()
|
|
241
|
+
for bond in molecule.iterateBonds():
|
|
242
|
+
bond.unhighlight()
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def clear_reaction_highlights(reaction: IndigoObject):
|
|
246
|
+
"""
|
|
247
|
+
Clear all highlights in the reaction.
|
|
248
|
+
:param reaction: The reaction to clear highlights from.
|
|
249
|
+
"""
|
|
250
|
+
for reactant in reaction.iterateReactants():
|
|
251
|
+
clear_highlights(reactant)
|
|
252
|
+
for product in reaction.iterateProducts():
|
|
253
|
+
clear_highlights(product)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def reserve_atom_mapping_number_of_search_result(q_reaction: IndigoObject, q_reactant: IndigoObject,
|
|
257
|
+
new_reaction_reactant: IndigoObject, new_reaction: IndigoObject,
|
|
258
|
+
sub_match: SerializableMoleculeMatch) -> None:
|
|
259
|
+
"""
|
|
260
|
+
Set the atom mapping number on the query molecule based on the atom mapping number of the sub_match molecule, if it exists.
|
|
261
|
+
:param new_reaction: The new reaction where the new reaction's reactant is found. This will be the target reaciton to write AAM to.
|
|
262
|
+
:param new_reaction_reactant: The new reaction's reactant where the AAM will be written to.
|
|
263
|
+
:param q_reactant: The query reactant from the query reaction that is being matched.
|
|
264
|
+
:param q_reaction: The query reaction that contains the query reactant for the sub_match.
|
|
265
|
+
:param sub_match: The substructure search match obtained from indigo.substructureMatcher(mol).match(query).
|
|
266
|
+
"""
|
|
267
|
+
for query_atom in q_reactant.iterateAtoms():
|
|
268
|
+
concrete_atom = sub_match.mapAtom(query_atom)
|
|
269
|
+
if concrete_atom is None:
|
|
270
|
+
continue
|
|
271
|
+
reaction_atom = q_reactant.getAtom(query_atom.index())
|
|
272
|
+
map_num = q_reaction.atomMappingNumber(reaction_atom)
|
|
273
|
+
if map_num:
|
|
274
|
+
concrete_atom = new_reaction_reactant.getAtom(concrete_atom.index())
|
|
275
|
+
new_reaction.setAtomMappingNumber(concrete_atom, map_num)
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def clean_product_aam(reaction: IndigoObject):
|
|
279
|
+
"""
|
|
280
|
+
Remove atom mappings from product that are not present in the reactants.
|
|
281
|
+
"""
|
|
282
|
+
existing_mapping_numbers = set()
|
|
283
|
+
for reactant in reaction.iterateReactants():
|
|
284
|
+
for atom in reactant.iterateAtoms():
|
|
285
|
+
map_num = reaction.atomMappingNumber(atom)
|
|
286
|
+
if map_num:
|
|
287
|
+
existing_mapping_numbers.add(map_num)
|
|
288
|
+
|
|
289
|
+
for product in reaction.iterateProducts():
|
|
290
|
+
for atom in product.iterateAtoms():
|
|
291
|
+
map_num = reaction.atomMappingNumber(atom)
|
|
292
|
+
if map_num and map_num not in existing_mapping_numbers:
|
|
293
|
+
reaction.setAtomMappingNumber(atom, 0) # YQ: atom number 0 means no mapping number in Indigo
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def make_concrete_reaction(reactants: list[IndigoObject], products: list[IndigoObject], replacement: IndigoObject,
|
|
297
|
+
replacement_index: int) -> tuple[IndigoObject, IndigoObject]:
|
|
298
|
+
"""
|
|
299
|
+
Create a concrete reaction from the given reactants and products, replacing the specified reactant with the replacement molecule.
|
|
300
|
+
:param reactants: List of reactant molecules.
|
|
301
|
+
:param products: List of product molecules.
|
|
302
|
+
:param replacement: The molecule to replace in the reactants.
|
|
303
|
+
:param replacement_index: The index of the reactant to replace.
|
|
304
|
+
:return: A new IndigoObject representing the concrete reaction.
|
|
305
|
+
"""
|
|
306
|
+
concrete_reaction = indigo.createQueryReaction()
|
|
307
|
+
for i, reactant in enumerate(reactants):
|
|
308
|
+
if i == replacement_index:
|
|
309
|
+
concrete_reaction.addReactant(indigo.loadQueryMolecule(replacement.molfile()))
|
|
310
|
+
else:
|
|
311
|
+
concrete_reaction.addReactant(reactant.clone())
|
|
312
|
+
for product in products:
|
|
313
|
+
concrete_reaction.addProduct(product.clone())
|
|
314
|
+
return concrete_reaction, concrete_reaction.getMolecule(replacement_index)
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def is_ambiguous_atom(atom: IndigoObject) -> bool:
|
|
318
|
+
"""
|
|
319
|
+
Test whether the symbol is an adjacent matching wildcard.
|
|
320
|
+
"""
|
|
321
|
+
if atom.isPseudoatom() or atom.isRSite():
|
|
322
|
+
return True
|
|
323
|
+
symbol = atom.symbol()
|
|
324
|
+
if symbol in {'A', 'Q', 'X', 'M', 'AH', 'QH', 'XH', 'MH', 'NOT', 'R', '*'}:
|
|
325
|
+
return True
|
|
326
|
+
return "[" in symbol and "]" in symbol
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def get_react_site_highlights(product, ignored_atom_indexes):
|
|
330
|
+
"""
|
|
331
|
+
Get the highlights for the reaction site in the product, ignoring the atoms that are not part of the reaction site.
|
|
332
|
+
:param product: The product molecule.
|
|
333
|
+
:param ignored_atom_indexes: A set of atom indexes to ignore.
|
|
334
|
+
:return: An IndigoObject with highlighted atoms and bonds that are part of the reaction site.
|
|
335
|
+
"""
|
|
336
|
+
highlight = product.clone()
|
|
337
|
+
for atom in highlight.iterateAtoms():
|
|
338
|
+
if atom.index() not in ignored_atom_indexes:
|
|
339
|
+
atom.highlight()
|
|
340
|
+
for nei in atom.iterateNeighbors():
|
|
341
|
+
if nei.index() not in ignored_atom_indexes:
|
|
342
|
+
nei.highlight()
|
|
343
|
+
nei.bond().highlight()
|
|
344
|
+
return highlight
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def inherit_auto_map_by_match(target_reaction: IndigoObject, source_reaction: IndigoObject,
|
|
348
|
+
reaction_match: IndigoObject):
|
|
349
|
+
"""
|
|
350
|
+
Inherit the auto-mapping from the source reaction to the target reaction based on the reaction match.
|
|
351
|
+
:param target_reaction: The target reaction to inherit auto-mapping to.
|
|
352
|
+
:param source_reaction: The source reaction to inherit auto-mapping from.
|
|
353
|
+
:param reaction_match: The match object that maps atoms and bonds between the source and target reactions.
|
|
354
|
+
"""
|
|
355
|
+
source_molecules = []
|
|
356
|
+
for q_reactant in source_reaction.iterateReactants():
|
|
357
|
+
source_molecules.append(q_reactant)
|
|
358
|
+
for q_product in source_reaction.iterateProducts():
|
|
359
|
+
source_molecules.append(q_product)
|
|
360
|
+
for source_molecule in source_molecules:
|
|
361
|
+
for source_atom in source_molecule.iterateAtoms():
|
|
362
|
+
source_atom_map_number = source_reaction.atomMappingNumber(source_atom)
|
|
363
|
+
if source_atom_map_number == 0:
|
|
364
|
+
continue
|
|
365
|
+
target_atom = reaction_match.mapAtom(source_atom)
|
|
366
|
+
if target_atom:
|
|
367
|
+
target_reaction.setAtomMappingNumber(target_atom, source_atom_map_number)
|
|
368
|
+
target_reaction.automap("keep")
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def are_symmetrical_subs(match1: SerializableMoleculeMatch, match2: SerializableMoleculeMatch) -> bool:
|
|
372
|
+
"""
|
|
373
|
+
Check if two SerializableMoleculeMatch objects are symmetrical.
|
|
374
|
+
That is, if we only get the atoms and bonds in the mapping, the two molecules are identical.
|
|
375
|
+
:param match1: The first SerializableMoleculeMatch object.
|
|
376
|
+
:param match2: The second SerializableMoleculeMatch object.
|
|
377
|
+
:return: True if the matches are symmetrical, False otherwise.
|
|
378
|
+
"""
|
|
379
|
+
match1_test = match1.get_matched_molecule_copy()
|
|
380
|
+
match1_atom_indexes = set(match1._query_atom_to_atom.values())
|
|
381
|
+
match1_bond_indexes = set(match1._query_bond_to_bond.values())
|
|
382
|
+
atom_delete_list: list[int] = []
|
|
383
|
+
atom_mirror_list: list[int] = []
|
|
384
|
+
bond_delete_list: list[int] = []
|
|
385
|
+
bond_mirror_list: list[int] = []
|
|
386
|
+
for atom in match1_test.iterateAtoms():
|
|
387
|
+
if atom.index() not in match1_atom_indexes:
|
|
388
|
+
atom_delete_list.append(atom.index())
|
|
389
|
+
else:
|
|
390
|
+
atom_mirror_list.append(atom.index())
|
|
391
|
+
for bond in match1_test.iterateBonds():
|
|
392
|
+
if bond.index() not in match1_bond_indexes:
|
|
393
|
+
bond_delete_list.append(bond.index())
|
|
394
|
+
else:
|
|
395
|
+
bond_mirror_list.append(bond.index())
|
|
396
|
+
match1_test.removeBonds(bond_delete_list)
|
|
397
|
+
match1_test.removeAtoms(atom_delete_list)
|
|
398
|
+
match1_mirror_test = match1.get_matched_molecule_copy()
|
|
399
|
+
match1_mirror_test.removeBonds(bond_mirror_list)
|
|
400
|
+
match1_mirror_test.removeAtoms(atom_mirror_list)
|
|
401
|
+
|
|
402
|
+
match2_test = match2.get_matched_molecule_copy()
|
|
403
|
+
match2_atom_indexes = set(match2._query_atom_to_atom.values())
|
|
404
|
+
match2_bond_indexes = set(match2._query_bond_to_bond.values())
|
|
405
|
+
atom_delete_list = []
|
|
406
|
+
bond_delete_list = []
|
|
407
|
+
atom_mirror_list = []
|
|
408
|
+
bond_mirror_list = []
|
|
409
|
+
for atom in match2_test.iterateAtoms():
|
|
410
|
+
if atom.index() not in match2_atom_indexes:
|
|
411
|
+
atom_delete_list.append(atom.index())
|
|
412
|
+
else:
|
|
413
|
+
atom_mirror_list.append(atom.index())
|
|
414
|
+
for bond in match2_test.iterateBonds():
|
|
415
|
+
if bond.index() not in match2_bond_indexes:
|
|
416
|
+
bond_delete_list.append(bond.index())
|
|
417
|
+
else:
|
|
418
|
+
bond_mirror_list.append(bond.index())
|
|
419
|
+
match2_test.removeBonds(bond_delete_list)
|
|
420
|
+
match2_test.removeAtoms(atom_delete_list)
|
|
421
|
+
match2_mirror_test = match2.get_matched_molecule_copy()
|
|
422
|
+
match2_mirror_test.removeBonds(bond_mirror_list)
|
|
423
|
+
match2_mirror_test.removeAtoms(atom_mirror_list)
|
|
424
|
+
|
|
425
|
+
return match1_test.canonicalSmiles() == match2_test.canonicalSmiles() and \
|
|
426
|
+
match1_mirror_test.canonicalSmiles() == match2_mirror_test.canonicalSmiles()
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
def replace_r_site_with_wildcards(mol: IndigoObject) -> IndigoObject:
|
|
430
|
+
"""
|
|
431
|
+
This will be used to replace molecule's R sites with wildcard *.
|
|
432
|
+
The substructure matcher at molecular level will not touch R sites. Therefore if we are to preserve mapping with bonds we need to replace R sites with wildcards.
|
|
433
|
+
:param mol: The molecule to process.
|
|
434
|
+
:return: A cloned molecule with R sites replaced by wildcards.
|
|
435
|
+
"""
|
|
436
|
+
ret = mol.clone()
|
|
437
|
+
for atom in ret.iterateAtoms():
|
|
438
|
+
if atom.isRSite():
|
|
439
|
+
atom.resetAtom("*")
|
|
440
|
+
return ret
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
def get_r_substructure(query_mol: IndigoObject, mol: IndigoObject,
|
|
444
|
+
initial_atom: IndigoObject, match: IndigoObject | SerializableMoleculeMatch,
|
|
445
|
+
r_site: str) -> IndigoObject:
|
|
446
|
+
"""
|
|
447
|
+
Return a connected R substructure sourced from the symbol, that is not within the original query match.
|
|
448
|
+
:param query_mol: The query molecule that contains the R site.
|
|
449
|
+
:param mol: The molecule that contains the R site.
|
|
450
|
+
:param initial_atom: The initial atom that is the R site.
|
|
451
|
+
:param match: The match object that maps atoms and bonds between the query and the molecule.
|
|
452
|
+
Note the within-R site molecules will not be part of the match.
|
|
453
|
+
But the starting position of R site is replaced with psuedoatom "*" and thus matches.
|
|
454
|
+
:param r_site: The R site symbol to match against.
|
|
455
|
+
"""
|
|
456
|
+
keeping_atom_index_set = set()
|
|
457
|
+
visiting: set[int] = set()
|
|
458
|
+
visiting.add(initial_atom.index())
|
|
459
|
+
visited: set[int] = set()
|
|
460
|
+
|
|
461
|
+
exclusion_indexes = set()
|
|
462
|
+
for q_atom in query_mol.iterateAtoms():
|
|
463
|
+
mapped_atom = match.mapAtom(q_atom)
|
|
464
|
+
if mapped_atom:
|
|
465
|
+
to_exclude: bool
|
|
466
|
+
if q_atom.isRSite():
|
|
467
|
+
to_exclude = q_atom.symbol() != r_site
|
|
468
|
+
else:
|
|
469
|
+
to_exclude = True
|
|
470
|
+
if to_exclude:
|
|
471
|
+
exclusion_indexes.add(mapped_atom.index())
|
|
472
|
+
while visiting:
|
|
473
|
+
visiting_atom: IndigoObject = mol.getAtom(visiting.pop())
|
|
474
|
+
keeping_atom_index_set.add(visiting_atom.index())
|
|
475
|
+
visited.add(visiting_atom.index())
|
|
476
|
+
for nei in visiting_atom.iterateNeighbors():
|
|
477
|
+
nei_index = nei.index()
|
|
478
|
+
if nei_index in visited or nei_index in visiting:
|
|
479
|
+
continue
|
|
480
|
+
if nei_index in exclusion_indexes and nei_index != initial_atom.index():
|
|
481
|
+
continue
|
|
482
|
+
visiting.add(nei_index)
|
|
483
|
+
removing_index_set: list[int] = list()
|
|
484
|
+
for atom in mol.iterateAtoms():
|
|
485
|
+
if atom.index() not in keeping_atom_index_set:
|
|
486
|
+
removing_index_set.append(atom.index())
|
|
487
|
+
r_substructure = mol.clone()
|
|
488
|
+
r_substructure.removeAtoms(removing_index_set)
|
|
489
|
+
return r_substructure
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
def get_rr_substructure_by_symbol(query_reactant, replacement_reaction) -> dict[str, IndigoObject]:
|
|
493
|
+
rr_substructure_by_symbol: dict[str, IndigoObject] = {}
|
|
494
|
+
for q_atom in query_reactant.iterateAtoms():
|
|
495
|
+
if not q_atom.isRSite():
|
|
496
|
+
continue
|
|
497
|
+
r_site_symbol = q_atom.symbol()
|
|
498
|
+
mapped_atom = replacement_reaction.replacement_query_reaction_match.mapAtom(q_atom)
|
|
499
|
+
if mapped_atom is None:
|
|
500
|
+
raise ValueError(
|
|
501
|
+
"The replacement reactant " + replacement_reaction.replacement_reactant.smiles() + " do not have R Site: " + r_site_symbol + ". This should not happen.")
|
|
502
|
+
r_substructure = get_r_substructure(query_reactant, replacement_reaction.replacement_reactant, mapped_atom,
|
|
503
|
+
replacement_reaction.replacement_query_reaction_match, r_site_symbol)
|
|
504
|
+
rr_substructure_by_symbol[r_site_symbol] = r_substructure
|
|
505
|
+
return rr_substructure_by_symbol
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
@dataclasses.dataclass
|
|
509
|
+
class FinalReactionMatchResult:
|
|
510
|
+
"""
|
|
511
|
+
Indicates a single final reaction match.
|
|
512
|
+
Note that a single reaction can output multiple such matches, if there are multiple combinations of reactant, products that can produce the same reaction.
|
|
513
|
+
"""
|
|
514
|
+
highlighted_reaction: IndigoObject
|
|
515
|
+
replacement_reaction_list: list[ReplacementReaction]
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
def __test_reactant_match(replacement_reaction: ReplacementReaction,
|
|
519
|
+
testing_reactant: IndigoObject,
|
|
520
|
+
query_reactant: IndigoObject) -> SerializableMoleculeMatch | None:
|
|
521
|
+
""" YQ: Finally piecing together both sides...
|
|
522
|
+
Test whether the reactant in the replacement reaction matches the reactant in the testing reaction.
|
|
523
|
+
We will be matching against the highlighted portion on each section to ensure the highlighted atom and bonds match.
|
|
524
|
+
:param replacement_reaction: The replacement reaction containing the reactant to test.
|
|
525
|
+
:param testing_reactant: The reactant in the testing reaction to match against.
|
|
526
|
+
:param query_reactant: The reactant in the query reaction to match against.
|
|
527
|
+
"""
|
|
528
|
+
orig_query_reactant = query_reactant
|
|
529
|
+
query_reactant = replace_r_site_with_wildcards(query_reactant)
|
|
530
|
+
if not indigo.exactMatch(replacement_reaction.replacement_reactant, testing_reactant):
|
|
531
|
+
return None
|
|
532
|
+
outer_matcher: IndigoObject = indigo.substructureMatcher(testing_reactant)
|
|
533
|
+
used_query_atom_indexes = replacement_reaction.replacement_query_reaction_match.query_atom_indexes
|
|
534
|
+
used_rr_atom_indexes = replacement_reaction.replacement_query_reaction_match.matching_atom_indexes
|
|
535
|
+
rr_substructure_by_symbol: dict[str, IndigoObject] = get_rr_substructure_by_symbol(orig_query_reactant,
|
|
536
|
+
replacement_reaction)
|
|
537
|
+
|
|
538
|
+
for outer_match in outer_matcher.iterateMatches(query_reactant):
|
|
539
|
+
ret: SerializableMoleculeMatch = SerializableMoleculeMatch.create(
|
|
540
|
+
orig_query_reactant, testing_reactant, outer_match, used_query_atom_indexes)
|
|
541
|
+
used_testing_reactant_atoms = []
|
|
542
|
+
for q_atom in query_reactant.iterateAtoms():
|
|
543
|
+
if q_atom.index() not in used_query_atom_indexes:
|
|
544
|
+
continue
|
|
545
|
+
mapped_atom = outer_match.mapAtom(q_atom)
|
|
546
|
+
if mapped_atom is None:
|
|
547
|
+
continue
|
|
548
|
+
used_testing_reactant_atoms.append(mapped_atom.index())
|
|
549
|
+
used_replacement_mol = replacement_reaction.replacement_reactant.clone()
|
|
550
|
+
used_replacement_mol_delete_indexes = []
|
|
551
|
+
for atom in used_replacement_mol.iterateAtoms():
|
|
552
|
+
if atom.index() not in used_rr_atom_indexes:
|
|
553
|
+
used_replacement_mol_delete_indexes.append(atom.index())
|
|
554
|
+
used_replacement_mol.removeAtoms(used_replacement_mol_delete_indexes)
|
|
555
|
+
used_testing_mol = testing_reactant.clone()
|
|
556
|
+
used_testing_mol_delete_indexes = []
|
|
557
|
+
for atom in used_testing_mol.iterateAtoms():
|
|
558
|
+
if atom.index() not in used_testing_reactant_atoms:
|
|
559
|
+
used_testing_mol_delete_indexes.append(atom.index())
|
|
560
|
+
used_testing_mol.removeAtoms(used_testing_mol_delete_indexes)
|
|
561
|
+
try:
|
|
562
|
+
exact_match = indigo.exactMatch(used_replacement_mol, used_testing_mol)
|
|
563
|
+
if not exact_match:
|
|
564
|
+
continue
|
|
565
|
+
except IndigoException:
|
|
566
|
+
continue
|
|
567
|
+
# Now check each R site substructure and it should be an exact match.
|
|
568
|
+
outer_match_r_substructure_by_symbol: dict[str, IndigoObject] = {}
|
|
569
|
+
missing_r_site = False
|
|
570
|
+
for q_atom in query_reactant.iterateAtoms():
|
|
571
|
+
orig_q_atom = orig_query_reactant.getAtom(q_atom.index())
|
|
572
|
+
if not orig_q_atom.isRSite():
|
|
573
|
+
continue
|
|
574
|
+
r_site_symbol = orig_q_atom.symbol()
|
|
575
|
+
mapped_atom = outer_match.mapAtom(q_atom)
|
|
576
|
+
if mapped_atom is None:
|
|
577
|
+
missing_r_site = True
|
|
578
|
+
continue
|
|
579
|
+
r_substructure = get_r_substructure(orig_query_reactant, testing_reactant, mapped_atom, outer_match,
|
|
580
|
+
r_site_symbol)
|
|
581
|
+
outer_match_r_substructure_by_symbol[r_site_symbol] = r_substructure
|
|
582
|
+
if missing_r_site:
|
|
583
|
+
# If we are missing an R site, we cannot match.
|
|
584
|
+
continue
|
|
585
|
+
r_site_mismatch = False
|
|
586
|
+
for r_site_symbol in rr_substructure_by_symbol.keys():
|
|
587
|
+
rr_substructure = rr_substructure_by_symbol[r_site_symbol]
|
|
588
|
+
outer_match_r_substructure = outer_match_r_substructure_by_symbol[r_site_symbol]
|
|
589
|
+
if not indigo.exactMatch(rr_substructure, outer_match_r_substructure):
|
|
590
|
+
r_site_mismatch = True
|
|
591
|
+
break
|
|
592
|
+
if r_site_mismatch:
|
|
593
|
+
# If we have a mismatch in R site substructure, we cannot match.
|
|
594
|
+
continue
|
|
595
|
+
|
|
596
|
+
# We are done matching. Return the match mapping.
|
|
597
|
+
return ret
|
|
598
|
+
return None
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
def __test_product_match(testing_reaction: IndigoObject, q_reaction: IndigoObject,
|
|
602
|
+
cur_rr_list: list[ReplacementReaction],
|
|
603
|
+
testing_reactants_match_list: list[SerializableMoleculeMatch]) -> list[
|
|
604
|
+
SerializableMoleculeMatch] | None:
|
|
605
|
+
""" YQ: My fifth try OOF
|
|
606
|
+
For each product, we are testing against two criteria:
|
|
607
|
+
1. That every R site from a product would exact match to the R site defined within reactant.
|
|
608
|
+
2. That atomic mapping numbers for query matches are within the matches of intersection of cur_rr_list and testing_reactants_match_list.
|
|
609
|
+
And the matching result for each atom via their reaction atom mapping numbers should follow atomic conservation law:
|
|
610
|
+
2.1 For each atom number in the reactant part, there should be no more than one atom in the product part with the same mapping number.
|
|
611
|
+
2.2 For each atom number in the reactant part, the mapped atom in the product part has the nucleus.
|
|
612
|
+
:param testing_reaction:
|
|
613
|
+
:param q_reaction:
|
|
614
|
+
:param cur_rr_list:
|
|
615
|
+
:param testing_reactants_match_list:
|
|
616
|
+
:return:
|
|
617
|
+
"""
|
|
618
|
+
# ********* PREPARE DATA *********
|
|
619
|
+
ret: list[SerializableMoleculeMatch] = []
|
|
620
|
+
testing_reactants = []
|
|
621
|
+
for testing_reactant in testing_reaction.iterateReactants():
|
|
622
|
+
testing_reactants.append(testing_reactant)
|
|
623
|
+
testing_products = []
|
|
624
|
+
for testing_product in testing_reaction.iterateProducts():
|
|
625
|
+
testing_products.append(testing_product)
|
|
626
|
+
query_reactants = []
|
|
627
|
+
for q_reactant in q_reaction.iterateReactants():
|
|
628
|
+
query_reactants.append(q_reactant)
|
|
629
|
+
query_products = []
|
|
630
|
+
for q_product in q_reaction.iterateProducts():
|
|
631
|
+
query_products.append(q_product)
|
|
632
|
+
replacement_reactants = []
|
|
633
|
+
replacement_reactant_match_list = []
|
|
634
|
+
q_atom_mapping_number_to_rr_reactant_atom: dict[int, IndigoObject] = {}
|
|
635
|
+
for reactant_index, replacement_reaction in enumerate(cur_rr_list):
|
|
636
|
+
replacement_reactants.append(replacement_reaction.replacement_reactant)
|
|
637
|
+
replacement_reactant_match_list.append(replacement_reaction.replacement_query_reaction_match)
|
|
638
|
+
query_reactant = query_reactants[reactant_index]
|
|
639
|
+
for q_atom in query_reactant.iterateAtoms():
|
|
640
|
+
mapped_atom = replacement_reaction.replacement_query_reaction_match.mapAtom(q_atom)
|
|
641
|
+
q_atom_mapping_number = q_reaction.atomMappingNumber(q_atom)
|
|
642
|
+
if q_atom_mapping_number == 0:
|
|
643
|
+
continue
|
|
644
|
+
if mapped_atom is None:
|
|
645
|
+
continue
|
|
646
|
+
q_atom_mapping_number_to_rr_reactant_atom[q_atom_mapping_number] = mapped_atom
|
|
647
|
+
rr_substructure_by_symbol: dict[str, IndigoObject] = {}
|
|
648
|
+
for reactant_index, replacement_reaction in enumerate(cur_rr_list):
|
|
649
|
+
query_reactant = query_reactants[reactant_index]
|
|
650
|
+
cur_dict = get_rr_substructure_by_symbol(query_reactant, replacement_reaction)
|
|
651
|
+
rr_substructure_by_symbol.update(cur_dict)
|
|
652
|
+
|
|
653
|
+
# ********* TESTING PRODUCTS *********
|
|
654
|
+
accepted_used_atom_mapping_numbers: set[int] = set()
|
|
655
|
+
for product_index, testing_product in enumerate(testing_products):
|
|
656
|
+
q_product = query_products[product_index]
|
|
657
|
+
orig_q_product = q_product
|
|
658
|
+
q_product = replace_r_site_with_wildcards(q_product)
|
|
659
|
+
outer_matcher: IndigoObject = indigo.substructureMatcher(testing_product)
|
|
660
|
+
found_match: SerializableMoleculeMatch | None = None
|
|
661
|
+
for outer_match in outer_matcher.iterateMatches(q_product):
|
|
662
|
+
ss_match: SerializableMoleculeMatch = SerializableMoleculeMatch.create(orig_q_product, testing_product,
|
|
663
|
+
outer_match)
|
|
664
|
+
valid_q_product_aam = True
|
|
665
|
+
valid_r_group = True
|
|
666
|
+
used_atom_mapping_numbers: set[int] = set()
|
|
667
|
+
for q_atom in q_product.iterateAtoms():
|
|
668
|
+
mapped_product_atom = outer_match.mapAtom(q_atom)
|
|
669
|
+
orig_q_atom = orig_q_product.getAtom(q_atom.index())
|
|
670
|
+
q_atom_mapping_number = q_reaction.atomMappingNumber(orig_q_atom)
|
|
671
|
+
if q_atom_mapping_number > 0 and not orig_q_atom.isRSite():
|
|
672
|
+
if q_atom_mapping_number in used_atom_mapping_numbers or q_atom_mapping_number in accepted_used_atom_mapping_numbers:
|
|
673
|
+
raise ValueError(
|
|
674
|
+
"Multiple atoms in the product with the same query atom mapping number: " + str(
|
|
675
|
+
q_atom_mapping_number))
|
|
676
|
+
rr_atom = q_atom_mapping_number_to_rr_reactant_atom.get(q_atom_mapping_number)
|
|
677
|
+
if rr_atom is None:
|
|
678
|
+
valid_q_product_aam = False
|
|
679
|
+
break
|
|
680
|
+
if not rr_atom.symbol() == mapped_product_atom.symbol():
|
|
681
|
+
valid_q_product_aam = False
|
|
682
|
+
break
|
|
683
|
+
used_atom_mapping_numbers.add(q_atom_mapping_number)
|
|
684
|
+
elif orig_q_atom.isRSite():
|
|
685
|
+
r_site_symbol = orig_q_atom.symbol()
|
|
686
|
+
r_substructure = get_r_substructure(orig_q_product, testing_product, mapped_product_atom,
|
|
687
|
+
outer_match,
|
|
688
|
+
r_site_symbol)
|
|
689
|
+
rr_substructure = rr_substructure_by_symbol.get(r_site_symbol)
|
|
690
|
+
if rr_substructure is None:
|
|
691
|
+
# This only happens if we didn't replace wildcard properly in original highlight or a misalignment between reactant and actual reaction template in Sapio.
|
|
692
|
+
raise ValueError("Missing RR substructure for R site: " + r_site_symbol + ".")
|
|
693
|
+
if not indigo.exactMatch(rr_substructure, r_substructure):
|
|
694
|
+
valid_r_group = False
|
|
695
|
+
break
|
|
696
|
+
if valid_q_product_aam and valid_r_group:
|
|
697
|
+
found_match = ss_match
|
|
698
|
+
accepted_used_atom_mapping_numbers.update(used_atom_mapping_numbers)
|
|
699
|
+
break
|
|
700
|
+
if not found_match:
|
|
701
|
+
return None
|
|
702
|
+
ret.append(found_match)
|
|
703
|
+
return ret
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
def __get_final_highlighted_reaction(cur_rr_list: list[ReplacementReaction],
|
|
707
|
+
product_matches: list[SerializableMoleculeMatch]) -> FinalReactionMatchResult:
|
|
708
|
+
"""
|
|
709
|
+
Translates the final match into a highlighted reaction.
|
|
710
|
+
:param cur_rr_list: The selected reactants that together forms an acceptable reaction.
|
|
711
|
+
:param product_matches: The generated products coming from the selected reactants.
|
|
712
|
+
:return: An IndigoObject representing the final reaction with highlights.
|
|
713
|
+
"""
|
|
714
|
+
ret: IndigoObject = indigo.createReaction()
|
|
715
|
+
for replacement_reaction in cur_rr_list:
|
|
716
|
+
reactant_mol = replacement_reaction.replacement_reactant.clone()
|
|
717
|
+
highlighting_atom_indexes = replacement_reaction.replacement_query_reaction_match.matching_atom_indexes
|
|
718
|
+
for atom in reactant_mol.iterateAtoms():
|
|
719
|
+
if atom.index() in highlighting_atom_indexes:
|
|
720
|
+
atom.highlight()
|
|
721
|
+
for nei in atom.iterateNeighbors():
|
|
722
|
+
if nei.index() in highlighting_atom_indexes:
|
|
723
|
+
nei.bond().highlight()
|
|
724
|
+
ret.addReactant(reactant_mol)
|
|
725
|
+
for product_match in product_matches:
|
|
726
|
+
product_mol = product_match.get_matched_molecule_copy()
|
|
727
|
+
highlighting_atom_indexes = product_match.matching_atom_indexes
|
|
728
|
+
for atom in product_mol.iterateAtoms():
|
|
729
|
+
if atom.index() in highlighting_atom_indexes:
|
|
730
|
+
atom.highlight()
|
|
731
|
+
for nei in atom.iterateNeighbors():
|
|
732
|
+
if nei.index() in highlighting_atom_indexes:
|
|
733
|
+
nei.bond().highlight()
|
|
734
|
+
ret.addProduct(product_mol)
|
|
735
|
+
_, ret = get_aromatic_dearomatic_forms(ret)
|
|
736
|
+
return FinalReactionMatchResult(ret, cur_rr_list)
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
def ps_match(testing_reaction: IndigoObject, q_reaction: IndigoObject,
|
|
740
|
+
kept_replacement_reaction_list_list: list[list[ReplacementReaction]]) -> FinalReactionMatchResult | None:
|
|
741
|
+
testing_reactants = []
|
|
742
|
+
for testing_reactant in testing_reaction.iterateReactants():
|
|
743
|
+
testing_reactants.append(testing_reactant)
|
|
744
|
+
query_reactants = []
|
|
745
|
+
for q_reactant in q_reaction.iterateReactants():
|
|
746
|
+
query_reactants.append(q_reactant)
|
|
747
|
+
|
|
748
|
+
reactant_ranges = []
|
|
749
|
+
for replacement_reaction_list in kept_replacement_reaction_list_list:
|
|
750
|
+
reactant_ranges.append(range(len(replacement_reaction_list)))
|
|
751
|
+
reactant_cartesian_products = itertools.product(*reactant_ranges)
|
|
752
|
+
for reactant_combination in reactant_cartesian_products:
|
|
753
|
+
cur_rr_list: list[ReplacementReaction] = []
|
|
754
|
+
for reactant_index, replacement_reaction_index in enumerate(reactant_combination):
|
|
755
|
+
replacement_reaction: ReplacementReaction = kept_replacement_reaction_list_list[reactant_index][
|
|
756
|
+
replacement_reaction_index]
|
|
757
|
+
cur_rr_list.append(replacement_reaction)
|
|
758
|
+
is_valid_reactants = True
|
|
759
|
+
testing_reactants_match_list = []
|
|
760
|
+
for reactant_index, replacement_reaction in enumerate(cur_rr_list):
|
|
761
|
+
match = __test_reactant_match(
|
|
762
|
+
replacement_reaction, testing_reactants[reactant_index], query_reactants[reactant_index])
|
|
763
|
+
if not match:
|
|
764
|
+
is_valid_reactants = False
|
|
765
|
+
break
|
|
766
|
+
testing_reactants_match_list.append(match)
|
|
767
|
+
if not is_valid_reactants:
|
|
768
|
+
continue
|
|
769
|
+
product_matches = __test_product_match(testing_reaction, q_reaction, cur_rr_list, testing_reactants_match_list)
|
|
770
|
+
if product_matches:
|
|
771
|
+
final_match: FinalReactionMatchResult = __get_final_highlighted_reaction(cur_rr_list, product_matches)
|
|
772
|
+
return final_match
|
|
773
|
+
return None
|