-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_search_global_greedy.py
477 lines (425 loc) · 14.2 KB
/
tree_search_global_greedy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import csv
import json
import heapq
from rdkit import Chem
from predict import predict
from rdkit.Chem import AllChem
from mhnreact.inspector import *
from price import calculate_cost
from reaction_cond import pred_temperature, pred_solvent_score
class Node:
def __init__(self, smiles, cost_usd_per_g, depth):
self.smiles = smiles
self.cost_usd_per_g = cost_usd_per_g
self.depth = depth
self.subtrees = []
class Edge:
def __init__(self, reaction_smiles, temperature, score, enzyme, rule, label):
self.reaction_smiles = reaction_smiles
self.temperature = temperature
self.enzyme = enzyme
self.score = score
self.rule = rule
self.label = label
def find_pathways(
input_smiles, n_enz=3, n_syn=3, max_depth=3, json_pathway="tree.json", device="cpu"
):
"""
Discover pathways for a target molecule using hybrid enzymatic/synthetic approaches.
Implements a depth-first search strategy to explore possible routes,
combining enzymatic and synthetic reaction rules.
Parameters
----------
input_smiles : str
SMILES string of target molecule to retro-synthesize
n_enz : int, optional
Number of enzymatic rules to consider per node (default: 3)
n_syn : int, optional
Number of synthetic rules to consider per node (default: 3)
max_depth : int, optional
Maximum steps to explore (default: 3)
json_pathway : str, optional
Output file path for JSON pathway visualization (default: 'tree.json')
device : str, optional
Computation device ('cpu' or 'cuda') (default: 'cpu')
Returns
-------
None
Writes results to specified JSON file
"""
# Load the models
clf_enz = load_clf("enz_final.pt", model_type="mhn", device=device)
clf_syn1 = load_clf("syn1_final.pt", model_type="mhn", device=device)
clf_syn2 = load_clf("syn2_final.pt", model_type="mhn", device=device)
clf_syn3 = load_clf("syn3_final.pt", model_type="mhn", device=device)
clf_syn4 = load_clf("syn4_final.pt", model_type="mhn", device=device)
clf_syn5 = load_clf("syn5_final.pt", model_type="mhn", device=device)
print("getting price")
price = get_price(input_smiles)
print("got price: ", price)
if price is None:
price = 50000
start_node = Node(input_smiles, price, 0)
global_greedy_search(
start_node,
n_enz,
n_syn,
max_depth,
clf_enz,
clf_syn1,
clf_syn2,
clf_syn3,
clf_syn4,
clf_syn5,
start_node,
json_pathway,
)
print_tree_to_json(start_node, json_pathway)
def global_greedy_search(
node,
n_enz,
n_syn,
max_depth,
clf_enz,
clf_syn1,
clf_syn2,
clf_syn3,
clf_syn4,
clf_syn5,
start_node,
json_pathway,
):
"""
Global best-first search for optimal pathways using priority queue optimization.
Explores chemical space by always expanding the most promising node first,
evaluating node viability through cost, environmental, and reaction metrics.
Parameters
----------
node : Node
Current molecule node being expanded
n_enz : int
Number of enzymatic rules to consider at this depth
n_syn : int
Number of synthetic rules to consider at this depth
max_depth : int
Maximum allowed synthesis steps from starting molecule
clf_enz : MHN
Enzymatic template prioritizer
clf_syn1-clf_syn5 : MHN
Synthetic reaction prioritizers (5-fold ensemble)
start_node : Node
Original target molecule node (for cycle detection)
json_pathway : str
Path for incremental JSON output of search tree
Returns
-------
None
Modifies node subtrees in-place and writes to JSON file
Notes
-----
Advantages over DFS:
- Finds optimal paths faster in shallow search spaces
- Less prone to local minima in scoring landscape
- Better for finding best-scoring rather than minimum-step paths
"""
pq = []
heapq.heappush(
pq, (-float("inf"), start_node)
) # Start node with arbitrarily high score
while pq:
print_tree_to_json(start_node, json_pathway)
_, node = heapq.heappop(pq)
if node.cost_usd_per_g <= 100 or node.depth >= max_depth:
return
enz_rules, syn_rules, enz_labels = find_applicable_rules(
node.smiles,
n_enz,
n_syn,
clf_enz,
clf_syn1,
clf_syn2,
clf_syn3,
clf_syn4,
clf_syn5,
)
for i in range(len(enz_rules)):
rule = enz_rules[i]
label = enz_labels[i]
# label = 0
reaction_smiles = apply_rule(rule, node.smiles)
if reaction_smiles == 0:
continue
try:
temperature = pred_temperature(reaction_smiles)
except:
temperature = 300
try:
solvent_score = pred_solvent_score(reaction_smiles)
except:
solvent_score = 0
new_smiles = get_reactant_smiles(reaction_smiles)
if start_node.smiles in new_smiles:
continue
new_edge = Edge(reaction_smiles, temperature, -1000, 1, rule, label, 0)
for reactant in new_smiles:
cost_usd_per_g = get_price(reactant)
if cost_usd_per_g is None:
cost_usd_per_g = 50000
score = -(temperature / 300) - (cost_usd_per_g / 500) + solvent_score
new_edge.score = max(score, new_edge.score)
new_node = Node(reactant, cost_usd_per_g, node.depth + 1)
node.subtrees.append((new_edge, new_node))
heapq.heappush(pq, (-score, new_node))
for i in range(len(syn_rules)):
rule = syn_rules[i]
reaction_smiles = apply_rule(rule, node.smiles)
if reaction_smiles == 0:
continue
try:
temperature = pred_temperature(reaction_smiles)
except:
temperature = 300
try:
solvent_score = pred_solvent_score(reaction_smiles)
except:
solvent_score = 0
new_smiles = get_reactant_smiles(reaction_smiles)
if start_node.smiles in new_smiles:
continue
new_edge = Edge(reaction_smiles, temperature, -1000, 0, rule, 0, 0)
for reactant in new_smiles:
cost_usd_per_g = get_price(reactant)
if cost_usd_per_g is None:
cost_usd_per_g = 50000
score = -(temperature / 300) - (cost_usd_per_g / 500) + solvent_score
new_edge.score = max(score, new_edge.score)
new_node = Node(reactant, cost_usd_per_g, node.depth + 1)
node.subtrees.append((new_edge, new_node))
heapq.heappush(pq, (-score, new_node))
def find_applicable_rules(
smiles, n_enz, n_syn, clf_enz, clf_syn1, clf_syn2, clf_syn3, clf_syn4, clf_syn5
):
"""
Retrieve applicable enzymatic/synthetic reaction rules for a molecule.
Parameters
----------
smiles : str
Product SMILES to find precursor reactions for
n_enz : int
Number of enzymatic rules to return
n_syn : int
Number of synthetic rules to return
clf_enz/syn1-syn5 : MHN
Pretrained template prioritizer models from load_clf()
Returns
-------
tuple: (list, list, list)
- enz_rules: Top enzymatic reaction SMARTS
- syn_rules: Top synthetic reaction SMARTS
- enz_labels: Corresponding enzymatic rule labels
"""
enz_rules, syn_rules, enz_labels = predict(
[smiles],
n_enz,
n_syn,
clf_enz,
clf_syn1,
clf_syn2,
clf_syn3,
clf_syn4,
clf_syn5,
)
print(
smiles
+ " : NUMBER of rules : "
+ str(len(enz_rules))
+ "----"
+ str(len(syn_rules))
)
return enz_rules, syn_rules, enz_labels
def apply_rule(rule, product):
"""
Apply reaction rule in reverse to generate precursors from product.
Parameters
----------
rule : str
Reaction SMARTS string (product side last)
product : str
Product SMILES to attempt retrosynthesis
Returns
-------
str or int
Reaction SMILES string "reactants>>product" if successful
0 if application fails (invalid rule/product)
"""
rule = "(" + rule.replace(">>", ")>>")
prod = Chem.MolFromSmiles(product)
rxn = AllChem.ReactionFromSmarts(rule)
try:
reactant = ""
res = rxn.RunReactants([prod])
for i in range(len(res[0])):
if reactant != "":
reactant += "."
reactant += Chem.MolToSmiles(res[0][i])
return reactant + ">>" + product
except Exception as e:
return 0
def get_reactant_smiles(reaction_smiles):
"""
Parse reactant components from reaction SMILES string.
Parameters
----------
reaction_smiles : str
Complete reaction in "reactants>>product" format
Returns
-------
list of str
Individual reactant SMILES strings (dot-separated)
"""
smiles_parts = reaction_smiles.split(">>")
reactant_part = smiles_parts[0].strip()
reactant_parts = reactant_part.split(".")
return reactant_parts
def get_price(smiles):
"""
Retrieve chemical price from database or calculate if unknown.
Parameters
----------
smiles : str
Query chemical structure
Returns
-------
float or None
USD/gram price, with priority:
1. Canonical SMILES match in buyables.csv
2. Literal SMILES match in buyables.csv
3. Minimum price from calculate_cost() API query
"""
try:
csv_file = "data/buyables.csv"
compound_smiles = Chem.MolToSmiles(Chem.MolFromSmiles(smiles))
with open(csv_file, "r") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
row_smiles = Chem.MolToSmiles(Chem.MolFromSmiles(row["smiles"]))
if row_smiles == compound_smiles:
return float(row["ppg"])
except:
pass
csv_file = "data/buyables.csv"
with open(csv_file, "r") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row["smiles"] == smiles:
return float(row["ppg"])
c = calculate_cost([smiles])
return min(c)
def print_tree_to_json(node, filename="tree.json", level=0):
"""
Recursively serialize reaction tree to hierarchical JSON format.
Parameters
----------
node : Node
Root node of synthesis tree
filename : str
Output file path (only used at root level)
level : int
Current recursion depth (internal use)
Returns
-------
dict or None
Returns tree dict when recursing, writes to file at root level"
"""
tree_dict = {
"smiles": node.smiles,
"cost_usd_per_g": node.cost_usd_per_g,
"depth": node.depth,
"subtrees": [],
}
for edge, subtree in node.subtrees:
edge_dict = {
"reaction_smiles": edge.reaction_smiles,
"temperature": edge.temperature,
"enzyme": edge.enzyme,
"score": edge.score,
"rule": edge.rule,
"label": edge.label,
}
subtree_dict = print_tree_to_json(subtree, filename, level + 1)
edge_dict["subtree"] = subtree_dict
tree_dict["subtrees"].append(edge_dict)
if level == 0:
with open(filename, "w") as file:
json.dump(tree_dict, file, indent=2)
else:
return tree_dict
# Example usage
# find_pathways('Oc(ccc(CC1NCCc(cc2O)c1cc2O)c1)c1O', n_enz=5, n_syn=5, max_depth=5, json_pathway='tree.json', device='cpu')
# -------------------------
# Command-Line Interface
# -------------------------
#
# This script can also be executed from the command line.
#
# Usage example:
#
# python tree_search_global_greedy.py -product "Oc(ccc(CC1NCCc(cc2O)c1cc2O)c1)c1O" -n_enz 5 -n_syn 5 -max_depth 5 -json_pathway "tree.json" -device "cpu"
#
# Parameters:
# -product : SMILES string of the target product. (Required)
# -n_enz : Number of enzyme reaction rules to consider. (Optional, default: 3)
# -n_syn : Number of synthetic reaction rules to consider. (Optional, default: 3)
# -max_depth : Maximum depth for the tree search. (Optional, default: 3)
# -json_pathway : Filename to which the resulting pathway tree will be saved in JSON format. (Optional, default: "tree.json")
# -device : Device to run the model on; either "cpu" or "cuda". (Optional, default: "cpu")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description="Tree Search for Pathways: Generate pathways by applying enzyme and synthetic reaction rules."
)
parser.add_argument(
"-product",
type=str,
required=True,
help="SMILES string of the target product (e.g., 'Oc(ccc(CC1NCCc(cc2O)c1cc2O)c1)c1O').",
)
parser.add_argument(
"-n_enz",
type=int,
default=3,
help="Number of enzyme reaction rules to consider (default: 3).",
)
parser.add_argument(
"-n_syn",
type=int,
default=3,
help="Number of synthetic reaction rules to consider (default: 3).",
)
parser.add_argument(
"-max_depth",
type=int,
default=3,
help="Maximum search depth for pathway exploration (default: 3).",
)
parser.add_argument(
"-json_pathway",
type=str,
default="tree.json",
help="Filename for the output JSON file (default: 'tree.json').",
)
parser.add_argument(
"-device",
type=str,
default="cpu",
help="Device to run the model on: 'cpu' or 'cuda' (default: 'cpu').",
)
args = parser.parse_args()
find_pathways(
args.product,
n_enz=args.n_enz,
n_syn=args.n_syn,
max_depth=args.max_depth,
json_pathway=args.json_pathway,
device=args.device,
)