-
Notifications
You must be signed in to change notification settings - Fork 8
/
prediction_extraction.py
635 lines (447 loc) · 18.6 KB
/
prediction_extraction.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
import re
def llama_forward_synthesis(output_text):
pattern = "(\[|]|\[[^\]]+]|Br?|Cl?|H|N|O|S|P|F|I|b|c|n|o|s|p|\(|\)|\.|;|=|#|-|\+|\\\\|\/|:|~|@|\?|>|\*|\$|\%[0-9]{2}|[0-9])+"
output_text = output_text.strip()
if re.match(pattern + '$', output_text):
return output_text
pos = output_text.rfind('Predicted product SMILES:')
if pos != -1:
output_text = output_text[pos + 1:].strip()
return output_text
pos = output_text.rfind(':')
if pos != -1:
m = re.match(pattern, output_text[pos + 1:].strip())
if m is not None:
output_text = output_text[pos + 1:].strip()
return output_text[:m.span()[1]]
match_begin = re.match(pattern, output_text)
if match_begin is not None:
return output_text[:(match_begin.span())[1]]
return ''
def llama_retrosynthesis(output_text):
pattern = "(\[|]|\[[^\]]+]|Br?|Cl?|H|N|O|S|P|F|I|b|c|n|o|s|p|\(|\)|\.|;|=|#|-|\+|\\\\|\/|:|~|@|\?|>|\*|\$|\%[0-9]{2}|[0-9])+"
output_text = output_text.strip()
m = re.match(pattern, output_text)
if m is not None:
span = m.span()
if span[1] == len(output_text):
return output_text.strip()
elif span[1] > 10:
return output_text[:span[1]]
titles = ('Reactant SMILES:', 'the predicted reactant molecules based on the product SMILES:', 'Here are the predicted reactant molecules in SMILES format:', 'Reactants SMILES:', 'Reactants:', 'the predicted reactants for the given product SMILES:', 'Here are the predicted reactants for the given product molecule:', 'following reactants:', 'Here are the predicted reactants for the given product molecule using the SMILES representation:', 'The predicted reactants for the given product molecule are:', 'the predicted reactants based on the product SMILES:', 'the predicted reactants for the given product:', 'the predicted reactants for the given reaction:')
found = False
for title in titles:
pos = output_text.lower().rfind(title.lower())
if pos != -1:
output_text = output_text[pos + len(title):].strip()
found = True
break
if not found:
for title in ('predicted reactants for each product SMILES:', 'the predicted reactants for each product:', 'the predicted reactants for each product molecule:', 'Here are the predicted reactants for the given product molecules:'):
pos = output_text.lower().rfind(title.lower())
if pos != -1:
output_text = output_text[pos + len(title):].strip()
pos = output_text.lower().find('product 2:')
if pos != -1:
output_text = output_text[pos + len('product 2:'):].strip().strip('*').strip()
found = True
if found:
match_begin = re.match(pattern, output_text)
if match_begin is not None:
return output_text[:(match_begin.span())[1]]
return ''
def llama_molecule_generation(output_text):
pattern = "(\[|]|\[[^\]]+]|Br?|Cl?|H|N|O|S|P|F|I|b|c|n|o|s|p|\(|\)|\.|;|=|#|-|\+|\\\\|\/|:|~|@|\?|>|\*|\$|\%[0-9]{2}|[0-9])+"
output_text = output_text.strip()
pos = output_text.find('\n')
if pos != -1:
first_line = output_text[:pos].strip()
else:
first_line = output_text
m = re.match(pattern + '$', first_line)
if m is not None:
return first_line
# span = m.span()
# if span[1] == len(output_text):
# return output_text.strip()
# elif span[1] > 7:
# return output_text[:span[1]]
found = False
titles = ('SMILES for the second molecule: ', 'the second input:', ' is:', 'molecules:', 'description:', 'you described:', 'SMILES:', 'described in the input:', 'Here is the SMILES representation of the molecule:', 'you provided:', 'the molecule is:')
for title in titles:
pos = output_text.lower().find(title.lower())
if pos != -1:
output_text = output_text[pos + len(title):].strip()
found = True
break
if not found:
titles= (':',)
for title in titles:
pos = output_text.lower().rfind(title.lower())
if pos != -1:
output_text = output_text[pos + len(title):].strip()
found = True
break
# if not found:
# for title in ('predicted reactants for each product SMILES:', 'the predicted reactants for each product:', 'the predicted reactants for each product molecule:', 'Here are the predicted reactants for the given product molecules:'):
# pos = output_text.lower().rfind(title.lower())
# if pos != -1:
# output_text = output_text[pos + len(title):].strip()
# pos = output_text.lower().find('product 2:')
# if pos != -1:
# output_text = output_text[pos + len('product 2:'):].strip().strip('*').strip()
# found = True
if found:
match_begin = re.match(pattern, output_text)
if match_begin is None:
pos = output_text.find(':')
output_text = output_text[pos + 1:].strip()
match_begin = re.match(pattern, output_text)
if match_begin is not None:
return output_text[:(match_begin.span())[1]]
return ''
def llama_molecule_captioning(output_text):
return output_text.strip()
def llama_name_conversion_i2f(output_text):
output_text = output_text.strip()
pos = output_text.rfind(':')
if pos == -1:
return output_text
output_text = output_text[pos + 1:].strip()
pos = output_text.find('\n')
if pos != -1:
output_text = output_text[:pos].strip()
if len(output_text) > 0 and output_text[-1] == '.':
output_text = output_text[:-1]
return output_text
def llama_name_conversion_i2s(output_text):
return llama_name_conversion_i2f(output_text)
def llama_name_conversion_s2f(output_text):
return llama_name_conversion_i2s(output_text)
def llama_name_conversion_s2i(output_text):
output_text = output_text.strip()
for title in ('IUPAC:', 'IUPAC name:', ':', ' is '):
pos = output_text.lower().rfind(title.lower())
if pos != -1:
output_text = output_text[pos + len(title):].strip()
break
pos = output_text.find('\n')
if pos != -1:
output_text = output_text[:pos].strip()
output_text = output_text.strip().strip('.')
if output_text == 'not provided':
output_text = ''
return output_text
def llama_property_prediction_esol(output_text):
return llama_name_conversion_i2s(output_text)
def llama_property_prediction_lipo(output_text):
output_text = output_text.strip()
pos = output_text.rfind(':')
if pos == -1:
pos = output_text.rfind('=')
if pos == -1:
return output_text
output_text = output_text[pos + 1:].strip()
pos = output_text.find('\n')
if pos != -1:
output_text = output_text[:pos].strip()
return output_text
def llama_property_prediction_bbbp(output_text):
return output_text.strip()
def llama_property_prediction_clintox(output_text):
return output_text.strip()
def llama_property_prediction_hiv(output_text):
return output_text.strip()
def llama_property_prediction_sider(output_text):
return output_text.strip()
def codellama_forward_synthesis(output_text):
output_text = output_text.strip().strip('.')
pos = output_text.rfind(':')
if pos == -1:
return output_text
output_text = output_text[pos + 1:].strip().strip('.')
pos = output_text.find('\n')
if pos != -1:
output_text = output_text[:pos].strip().strip('.')
return output_text
def codellama_retrosynthesis(output_text):
return codellama_forward_synthesis(output_text)
def codellama_molecule_captioning(output_text):
return output_text.strip()
def codellama_molecule_generation(output_text):
return codellama_forward_synthesis(output_text)
def codellama_name_conversion_i2f(output_text):
return llama_name_conversion_i2s(output_text)
def codellama_name_conversion_i2s(output_text):
return codellama_forward_synthesis(output_text)
def codellama_name_conversion_s2f(output_text):
return llama_name_conversion_i2s(output_text)
def codellama_name_conversion_s2i(output_text):
return llama_name_conversion_s2i(output_text)
def codellama_property_prediction_esol(output_text):
return output_text.strip()
def codellama_property_prediction_lipo(output_text):
return output_text.strip()
def codellama_property_prediction_bbbp(output_text):
return output_text.strip()
def codellama_property_prediction_clintox(output_text):
return output_text.strip()
def codellama_property_prediction_hiv(output_text):
return output_text.strip()
def codellama_property_prediction_sider(output_text):
return output_text.strip()
def mistral_forward_synthesis(output_text):
output_text = output_text.strip()
pos = output_text.find('\n')
if pos == -1:
return output_text
else:
return output_text[:pos].strip()
def mistral_retrosynthesis(output_text):
return mistral_forward_synthesis(output_text)
def mistral_molecule_captioning(output_text):
return output_text.strip()
def mistral_molecule_generation(output_text):
return llama_name_conversion_i2s(output_text)
def mistral_name_conversion_i2f(output_text):
output_text = output_text.strip()
output_text = output_text.split('\n')[0].strip()
return output_text
def mistral_name_conversion_i2s(output_text):
output_text = output_text.strip()
pos = output_text.rfind(':')
if pos != -1:
output_text = output_text[pos + 1:].strip()
pos = output_text.find('\n')
if pos != -1:
output_text = output_text[:pos].strip()
return output_text
def mistral_name_conversion_s2f(output_text):
output_text = output_text.strip()
output_text = output_text.split('\n')[0].strip()
return output_text
def mistral_name_conversion_s2i(output_text):
return llama_name_conversion_s2i(output_text)
def mistral_property_prediction_esol(output_text):
return mistral_name_conversion_i2s(output_text)
def mistral_property_prediction_lipo(output_text):
output_text = output_text.strip()
pos = output_text.find('\n')
if pos != -1:
first_line = output_text[:pos].strip()
try:
_ = float(first_line)
except:
pass
else:
return first_line
pos = output_text.rfind(':')
if pos != -1:
output_text = output_text[pos + 1:].strip().strip('.')
else:
pos = output_text.rfind('is ')
if pos != -1:
output_text = output_text[pos + 3:].strip().strip('.')
pos = output_text.find('\n')
if pos != -1:
output_text = output_text[:pos].strip()
return output_text
def mistral_property_prediction_bbbp(output_text):
return output_text.strip().strip('.')
def mistral_property_prediction_clintox(output_text):
return output_text.strip().strip('.')
def mistral_property_prediction_hiv(output_text):
return output_text.strip().strip('.')
def mistral_property_prediction_sider(output_text):
return output_text.strip().strip('.')
def mol_forward_synthesis(output_text):
output_text = output_text.replace('<unk>', '').replace('</s>', '').strip()
return output_text
def mol_retrosynthesis(output_text):
return mol_forward_synthesis(output_text)
def mol_molecule_captioning(output_text):
return mol_forward_synthesis(output_text)
def mol_molecule_generation(output_text):
return mol_forward_synthesis(output_text)
def mol_name_conversion_i2f(output_text):
return mol_forward_synthesis(output_text)
def mol_name_conversion_i2s(output_text):
return mol_forward_synthesis(output_text)
def mol_name_conversion_s2f(output_text):
return mol_forward_synthesis(output_text)
def mol_name_conversion_s2i(output_text):
return mol_forward_synthesis(output_text)
def mol_property_prediction_esol(output_text):
output_text = output_text.replace('</s>', '').replace('<unk>', '').strip()
output_text = output_text.strip('.').strip()
return output_text
def mol_property_prediction_lipo(output_text):
output_text = output_text.replace('</s>', '').replace('<unk>', '').strip()
output_text = output_text.strip('.').strip()
return output_text
def mol_property_prediction_bbbp(output_text):
output_text = output_text.strip().lower()
if output_text[:3] == 'yes':
return 'Yes'
elif output_text[:2] == 'no':
return 'No'
else:
return ''
def mol_property_prediction_clintox(output_text):
return mol_property_prediction_bbbp(output_text)
def mol_property_prediction_hiv(output_text):
return mol_property_prediction_bbbp(output_text)
def mol_property_prediction_sider(output_text):
return mol_property_prediction_bbbp(output_text)
def gal_forward_synthesis(output_text):
title = 'Answer:'
pos = output_text.find(title)
assert pos != -1
output_text = output_text[pos + len(title):]
title = '[START_I_SMILES]'
pos = output_text.find(title)
assert pos != -1
output_text = output_text[pos + len(title):]
pos = output_text.find('[END_I_SMILES]')
output_text = output_text[:pos]
output_text = output_text.strip()
return output_text
def gal_retrosynthesis(output_text):
return gal_forward_synthesis(output_text)
def gal_molecule_captioning(output_text):
title = 'Description:'
pos = output_text.find(title)
assert pos != -1
output_text = output_text[pos + len(title):].strip()
title = '\n'
pos = output_text.find(title)
if pos == -1:
return output_text
output_text = output_text[:pos]
output_text = output_text.strip()
return output_text
def gal_molecule_generation(output_text):
title = 'The SMILES formula of this molecule is [START_I_SMILES]'
pos = output_text.find(title)
assert pos != -1
output_text = output_text[pos + len(title):].strip()
pos = output_text.find('[END_I_SMILES]')
if pos == -1:
return output_text
output_text = output_text[:pos]
output_text = output_text.strip()
return output_text
def gal_name_conversion_i2f(output_text):
title = 'Molecular Formula'
pos = output_text.find(title)
assert pos != -1
output_text = output_text[pos + len(title):].strip().strip('*').strip()
title = '\n'
pos = output_text.find(title)
if pos == -1:
return output_text
output_text = output_text[:pos]
output_text = output_text.strip()
return output_text
def gal_name_conversion_i2s(output_text):
title = 'Canonical SMILES\n\n[START_SMILES]'
pos = output_text.find(title)
assert pos != -1
output_text = output_text[pos + len(title):]
pos = output_text.find('[END_SMILES]')
if pos == -1:
return output_text
output_text = output_text[:pos]
output_text = output_text.strip()
return output_text
def gal_name_conversion_s2f(output_text):
return gal_name_conversion_i2f(output_text)
def gal_name_conversion_s2i(output_text):
title = 'The following are chemical properties for '
pos = output_text.find(title)
assert pos != -1
output_text = output_text[pos + len(title):]
title = '\n'
pos = output_text.find(title)
if pos == -1:
return output_text
output_text = output_text[:pos].strip().strip('.').strip()
return output_text
def gal_property_prediction_esol(output_text):
title = 'Answer:'
pos = output_text.find(title)
assert pos != -1
output_text = output_text[pos + len(title):]
title = '\n'
pos = output_text.find(title)
if pos != -1:
output_text = output_text[:pos]
output_text = output_text.strip().replace('</s>', '').strip()
return output_text
def gal_property_prediction_lipo(output_text):
return gal_property_prediction_esol(output_text)
def gal_property_prediction_bbbp(output_text):
return gal_property_prediction_esol(output_text)
def gal_property_prediction_clintox(output_text):
return gal_property_prediction_esol(output_text)
def gal_property_prediction_hiv(output_text):
return gal_property_prediction_esol(output_text)
def gal_property_prediction_sider(output_text):
return gal_property_prediction_esol(output_text)
def chemllm_forward_synthesis(output_text):
output_text = output_text.replace("</s>", "").strip()
if output_text[-1] == '.':
output_text = output_text[:-1]
return output_text
def chemllm_retrosynthesis(output_text):
return chemllm_forward_synthesis(output_text)
def chemllm_molecule_captioning(output_text):
output_text = output_text.replace("</s>", "").strip()
return output_text
def chemllm_molecule_generation(output_text):
return chemllm_forward_synthesis(output_text)
def chemllm_name_conversion_i2f(output_text):
output_text = chemllm_forward_synthesis(output_text)
output_text = output_text.replace(" ", "").replace("_", "")
return output_text
def chemllm_name_conversion_i2s(output_text):
return chemllm_forward_synthesis(output_text)
def chemllm_name_conversion_s2f(output_text):
return chemllm_name_conversion_i2f(output_text)
def chemllm_name_conversion_s2i(output_text):
return chemllm_forward_synthesis(output_text)
def chemllm_property_prediction_esol(output_text):
return chemllm_forward_synthesis(output_text)
def chemllm_property_prediction_lipo(output_text):
return chemllm_forward_synthesis(output_text)
def chemllm_property_prediction_bbbp(output_text):
output_text = chemllm_forward_synthesis(output_text)
lower = output_text.lower()
if lower.startswith('yes') or lower.endswith('yes'):
return 'Yes'
elif lower.startswith('no') or lower.endswith('no'):
return 'No'
else:
return ''
def chemllm_property_prediction_clintox(output_text):
return chemllm_property_prediction_bbbp(output_text)
def chemllm_property_prediction_hiv(output_text):
return chemllm_property_prediction_bbbp(output_text)
def chemllm_property_prediction_sider(output_text):
return chemllm_property_prediction_bbbp(output_text)
def extract_pred(sample, model_name, task):
if model_name == 'mol_trained':
func_model_name = 'mol'
else:
func_model_name = model_name
func = eval('%s_%s' % (func_model_name, task.lower().replace('-', '_')))
preds = []
outputs = sample['output']
for output in outputs:
if model_name == 'mol':
pos = output.find('Response:')
assert pos != -1
output = output[pos + len('Response:'):].strip()
r = func(output)
preds.append(r)
return preds