-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils_eval.py
615 lines (550 loc) · 19.1 KB
/
utils_eval.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
import numpy as np
from numpy import nan
import pickle
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import Descriptors
from rdkit import DataStructs
from rdkit.Chem import rdFMCS
from rdkit.Chem import rdMolDescriptors
def read_smiles(filename):
"""
read a txt file of line separated smiles into a set of smiles
Args:
filename: txt file directory
Output:
set of smiles
"""
lines = open(filename).read().split('\n')
smiles = set()
for smi in lines:
smiles.add(smi)
return smiles
def writeout_smiles(filename, smiles_set):
"""
writes a set of smiles into a txt file
Args:
filename: output txt file directory
smiles_set: set of smiles
"""
outfile = open(filename,'w')
for smiles in smiles_set:
outfile.write(smiles + '\n')
outfile.close()
return
def load_obj(filename):
"""
load a pkl object
"""
with open(filename, 'rb') as f:
return pickle.load(f)
def check_smiles(smi):
"""
check smiles for validity
"""
mol = Chem.MolFromSmiles(smi)
if mol is None:
return False
else:
return True
def normalize_smiles(smi, canonical, isomeric):
"""
for canonical smiles: canonical =True and isomeric=False
"""
normalized = Chem.MolToSmiles(Chem.MolFromSmiles(smi), canonical=canonical, isomericSmiles=isomeric)
return normalized
def get_valid(pred_list):
valid = set()
for smi in pred_list:
try:
check = check_smiles(smi)
if check:
valid.add(smi)
except:
removed = removed + 1
return valid
def get_species(smiles):
"""
Return the atom species that are present in a chemical molecule
Args: smiles representation
Output: set of atom species
"""
species = set()
mol = Chem.MolFromSmiles(smiles)
if mol:
for at in mol.GetAtoms():
species.add(at.GetSymbol())
return species
def get_tanimoto(smiles1, smiles2, radius, bits):
"""
tanimoto fingerprint similarity between 2 smiles
Args:
smiles1, smiles2 to be compared
radius and bits for the fingerprint representation
Output:
tanimoto similarity
"""
m1 = Chem.MolFromSmiles(smiles1)
m2 = Chem.MolFromSmiles(smiles2)
if m1 and m2:
fp1 = AllChem.GetMorganFingerprintAsBitVect(m1,radius,nBits=bits)
fp2 = AllChem.GetMorganFingerprintAsBitVect(m2,radius,nBits=bits)
return DataStructs.FingerprintSimilarity(fp1, fp2)
else:
return 0
def get_cosine(smiles1, smiles2, radius, bits):
"""
cosine fingerprint similarity between 2 smiles
Args:
smiles1, smiles2 to be compared
radius and bits for the fingerprint representation
Output:
cosine similarity
"""
m1 = Chem.MolFromSmiles(smiles1)
m2 = Chem.MolFromSmiles(smiles2)
if m1 and m2:
fp1 = AllChem.GetMorganFingerprintAsBitVect(m1,radius,nBits=bits)
fp2 = AllChem.GetMorganFingerprintAsBitVect(m2,radius,nBits=bits)
return DataStructs.CosineSimilarity(fp1, fp2)
else:
return 0
def get_RDFsim(smiles1, smiles2):
"""
Fingerprint similarity based on RDKFingerprint
"""
original_fgp = Chem.RDKFingerprint(Chem.MolFromSmiles(smiles1))
predicted_fgp = Chem.RDKFingerprint(Chem.MolFromSmiles(smiles2))
sim = DataStructs.FingerprintSimilarity(original_fgp,predicted_fgp)
return sim
def get_max_tanimoto(pred_list, original, radius, bits):
"""
find the predicted smiles with the maximum tanimoto similarity with respect to the original smiles
Args:
pred_list: list of predicted smiles
original: reference smiles
radius and bits for the fingerprint representation
Output:
closst_smiles: smiles with the max tanimoto
max_tan: maximum tanimoto
"""
max_tan = 0
closest_smiles = ''
for smi in pred_list:
tan = get_tanimoto(original, smi, radius, bits)
if tan > max_tan:
max_tan = tan
closest_smiles = smi
return closest_smiles, max_tan
def get_max_cosine(pred_list, original, radius, bits):
"""
find the predicted smiles with the maximum cosine similarity
Args:
pred_list: list of predicted smiles
original: reference smiles
radius and bits for the fingerprint representation
Output:
closest_smiles: smiles with the max cosine similarity
max_cos: maximum cosine
"""
max_cos = 0
closest_smiles = ''
for smi in pred_list:
cos = get_cosine(original, smi, radius, bits)
if cos > max_cos:
max_cos = cos
closest_smiles = smi
return closest_smiles, max_cos
def get_avg_cosine(pred_list, original, radius, bits):
"""
average cosine fingerprint similarity among all predictions
Args:
pred_list: list of predicted smiles
original: reference smiles
radius and bits for the fingerprint representation
Output:
average cosine similarity
"""
coss = []
for smi in pred_list:
cos = get_cosine(original, smi, radius, bits)
if cos>0:
coss.append(cos)
return np.mean(coss)
def get_max_mcs(pred_list,original):
"""
MCS-based metrics of the prediction with the max number of atoms in the maximum common substructure
Args:
pred_list: list of predicted smiles
original: reference smiles
Output:
closest_smiles: smiles with the maximum number of atoms in the MCS
mcs ratio for the smiles with the max number of atoms in the MCS
mcs tanimoto >>
mcs coefficient >>
"""
removed = 0
original_mol = Chem.MolFromSmiles(original)
original_atoms = original_mol.GetNumAtoms()
max_mcs = 0
mcs_atoms = 0
closest_smiles = ''
for smi in pred_list:
try:
mols = [Chem.MolFromSmiles(original), Chem.MolFromSmiles(smi)]
mcs = rdFMCS.FindMCS(mols, ringMatchesRingOnly=True, atomCompare=Chem.rdFMCS.AtomCompare.CompareElements, bondCompare=Chem.rdFMCS.BondCompare.CompareOrder, timeout=60)
mcs_atoms = mcs.numAtoms
if mcs_atoms>max_mcs:
max_mcs = mcs_atoms
closest_smiles = smi
except:
removed = removed + 1
closest_mol = Chem.MolFromSmiles(closest_smiles)
closest_atoms = closest_mol.GetNumAtoms()
if mcs_atoms == 0:
mcs_ratio = 0
mcs_tan = 0
mcs_coef = 0
else:
mcs_ratio = max_mcs/original_atoms
mcs_tan = max_mcs/(original_atoms+closest_atoms-max_mcs)
mcs_coef = max_mcs/min(original_atoms, closest_atoms)
return closest_smiles, mcs_ratio, mcs_tan, mcs_coef
def get_avg_mcs(pred_list,original):
"""
Average values of the MCS-based metrics between the predicted smiles and the reference smiles
Args:
pred_list: list of predicted smiles
original: reference smiles
Output:
average mcs ratio
average mcs tanimoto
average mcs coefficient
"""
removed = 0
original_mol = Chem.MolFromSmiles(original)
original_atoms = original_mol.GetNumAtoms()
mcs_rs = []
mcs_tns = []
mcs_cfs = []
for smi in pred_list:
try:
pred_mol = Chem.MolFromSmiles(smi)
pred_atoms = pred_mol.GetNumAtoms()
mols = [original_mol,pred_mol]
mcs = rdFMCS.FindMCS(mols, ringMatchesRingOnly=True, atomCompare=Chem.rdFMCS.AtomCompare.CompareElements, bondCompare=Chem.rdFMCS.BondCompare.CompareOrder, timeout=60)
mcs_atoms = mcs.numAtoms
if mcs_atoms == 0:
mcs_ratio = 0
mcs_tan = 0
mcs_coef = 0
else:
mcs_ratio = mcs_atoms/original_atoms
mcs_tan = mcs_atoms/(original_atoms+pred_atoms-mcs_atoms)
mcs_coef = mcs_atoms/min(original_atoms, pred_atoms)
mcs_rs.append(mcs_ratio)
mcs_tns.append(mcs_tan)
mcs_cfs.append(mcs_coef)
except:
removed = removed + 1
return np.mean(mcs_rs), np.mean(mcs_tns), np.mean(mcs_cfs)
def compare_formulas(pred_list,original):
"""
check whether any of the predicted smiles has the correct molecular formula (with respect to the reference molecule)
Args:
pred_list: list of predicted smiles
original: reference smiles
Output:
True/False
"""
removed = 0
original_mol = Chem.MolFromSmiles(original)
formula_orig = rdMolDescriptors.CalcMolFormula(original_mol)
pred_formulas = set()
for smi in pred_list:
try:
predicted_mol = Chem.MolFromSmiles(smi)
formula = rdMolDescriptors.CalcMolFormula(predicted_mol)
pred_formulas.add(formula)
except:
removed = removed + 1
if formula_orig in pred_formulas:
return True
else:
return False
def get_formulas(formulas_file):
formulas = set()
lines = open(formulas_file).read().split('\n')
for line in lines[1:]:
if not line == '':
line = line.split('\t')
formulas.add(line[1])
return formulas
def get_structures(structures_file, topn):
structures = set()
lines = open(structures_file).read().split('\n')
for line in lines[1:]:
if len(structures) == topn:
break
if not line == '':
line = line.split('\t')
structures.add(normalize_smiles(line[8], True, False))
return structures
def get_formulas_min_distance(pred_list, original, hydrogens=False):
"""
Get minimum distance between predicted molecular formulas and the reference molecular formula.
Distance is defined as the number of atoms that differ between two molecules
Args:
List of predicted smiles
original: reference smiles
hydrogens: if False then hydrogens are not taken into account
Output:
minimum distance over all distances from each predicted smiles
"""
min_distance = 100
removed = 0
for smi in pred_list:
try:
dist = compare_species_counts(smi,original,hydrogens)
if dist<min_distance:
min_distance = dist
except:
removed = removed + 1
return min_distance
def get_formulas_avg_distance(pred_list, original, hydrogens=False):
"""
Get the average over all distances between the molecular formulas of the predicted smiles and the molecular formula of the reference smiles
Distance is defined as the number of atoms that differ between the two molecules
Args:
pred_list: list of predicted smiles
original: reference smiles
hydrogens: if False then hydrogens are not taken into account
Output:
average distance over all distances from each predicted smiles
"""
removed = 0
dists = []
for smi in pred_list:
try:
dist = compare_species_counts(smi, original, hydrogens)
dists.append(dist)
except:
removed = removed + 1
return np.mean(dists)
def compare_species(smi_ref, smi_pred):
"""
compares the atom species between a predicted smiles and a reference smiles
without accounting for discrepancies for the number of atoms for each species
Args:
smi_ref: reference smiles
smi_pred: predicted smiles
Output:
found: set of atom species that have been found in the predicted smiles and the reference smiles
not_found: set of atom species that are found in the reference smiles but not i the predicted smiles
"""
found = set()
not_found = set()
species_ref = get_species(smi_ref)
species_pred = get_species(smi_pred)
for spe in species_ref:
if spe in species_pred:
found.add(spe)
else:
not_found.add(spe)
return found, not_found
def species_dif(found,not_found, atom_counts_found, atom_counts_notfound):
"""
It aggregates the found/not found atom species in the entire dataset
Args:
found/not_found: set of atom species found/not found for a single molecule
atom_counts_found/atom_counts_notfound: dictionaries that keep track of the number of molecules
for which a specific tom species from the reference smiles has been / has not been found in the
predicted smiles
"""
for spe in found:
atom_counts_found[spe] = atom_counts_found[spe] + 1
for spe in not_found:
atom_counts_notfound[spe] = atom_counts_notfound[spe] + 1
return atom_counts_found, atom_counts_notfound
def count_hydrogens(smiles):
"""
counts the number of hydrogens in a given smiles
"""
hydrogens = 0
mol = Chem.MolFromSmiles(smiles)
mol = Chem.RemoveHs(mol)
heavy_atoms = mol.GetNumAtoms()
molH = Chem.AddHs(mol)
all_atoms = molH.GetNumAtoms()
if all_atoms - heavy_atoms > 0:
hydrogens = all_atoms - heavy_atoms
return hydrogens
def get_species_counts(smiles, hydrogens=False):
"""
creates a vocabulary with the number of atoms for each atom species
"""
species_counts = {}
mol = Chem.MolFromSmiles(smiles)
for atom in mol.GetAtoms():
species = atom.GetSymbol()
if species in species_counts.keys():
counts = species_counts[species]
else:
counts = 0
counts = counts + 1
species_counts[species] = counts
if hydrogens:
species_counts['H'] = count_hydrogens(smiles)
return species_counts
def compare_species_counts(smiles_1, smiles_2, hydrogens=False):
"""
computes the molecular formula distance:
"""
species_counts_1 = get_species_counts(smiles_1)
species_counts_2 = get_species_counts(smiles_2)
errors = 0
for spe in species_counts_1.keys():
counts1 = species_counts_1[spe]
if spe in species_counts_2.keys():
counts2 = species_counts_2[spe]
else:
counts2 = 0
errors = errors + abs(counts1-counts2)
for spe in species_counts_2.keys():
if not spe in species_counts_1.keys():
errors = errors + species_counts_2[spe]
return errors
def get_MW_dif_min(pred_list, original):
"""
finds the minimum deviation from the reference molecular weight among all predicted smiles
"""
original_mol = Chem.MolFromSmiles(original)
original_mw = Chem.Descriptors.ExactMolWt(original_mol)
min_diff = 100
rem = 0
for pred in pred_list:
try:
predicted_mol = Chem.MolFromSmiles(pred)
predicted_mw = Chem.Descriptors.ExactMolWt(predicted_mol)
diff = abs(original_mw-predicted_mw)
if diff < min_diff:
min_diff = diff
except:
rem = rem + 1
return min_diff
def species_confusion(smi_ref, smi_pred, true_pos, true_neg, false_pos, false_neg):
"""
finds true positives, true negatives, false positives and false negatives in the detection
of the atom species in the predicted smiles without taking into account the atom counts per species
Args:
smi_ref: reference smiles
smi_pred: predicted smiles
true_pos: dictionary that keeps track of the number of true positives (at a molecule level) per species
true_neg: dictionary...
false_pos: dictionary...
false_neg: ...
Output
Updated dictionaries
"""
species_ref = get_species(smi_ref)
species_pred = get_species(smi_pred)
if not len(species_pred)==0:
for spe in species_ref:
if spe in true_pos.keys():
if spe in species_pred:
true_pos[spe] = true_pos[spe] + 1
for spe in species_ref:
if spe in true_pos.keys():
if not spe in species_pred:
false_neg[spe] = false_neg[spe] + 1
for spe in species_pred:
if spe in true_pos.keys():
if not spe in species_ref:
false_pos[spe] = false_pos[spe] + 1
for spe in true_pos.keys():
if not spe in species_ref and not spe in species_pred:
true_neg[spe] = true_neg[spe] + 1
return true_pos, true_neg, false_pos, false_neg
def get_MW_dif_avg(pred_list, original):
"""
finds the average deviation from the reference molecular weight among all predicted smiles
"""
original_mol = Chem.MolFromSmiles(original)
original_mw = Chem.Descriptors.ExactMolWt(original_mol)
dmws = []
rem = 0
for pred in pred_list:
try:
predicted_mol = Chem.MolFromSmiles(pred)
predicted_mw = Chem.Descriptors.ExactMolWt(predicted_mol)
diff = abs(original_mw-predicted_mw)
dmws.append(diff)
except:
rem = rem + 1
return np.mean(dmws)
def get_pubchem_mols(infile, topn):
pubmols = torch.load(infile)
original_topn = {}
for smi in pubmols:
smiles_dist = {}
topnset = set()
dist_smiles = pubmols[smi]
for ii in range(0,len(dist_smiles)):
dist = dist_smiles[ii][0]
smiles = dist_smiles[ii][1]
smiles_dist[smiles] = dist
sorted_smiles = sorted(smiles_dist.items(), key=operator.itemgetter(1))
ii = 0
while len(topnset)<topn and ii<len(sorted_smiles):
topnset.add(sorted_smiles[ii][0])
ii = ii + 1
original_topn[normalize_smiles(smi, True, False)] = topnset
return original_topn
def select_smiles_MW(pred_list, original_mw, top_n):
'''
ranks predictions based on the deviation from the original MW
and returns the topn
Args:
pred_list: list of predicted smiles
original_mw: MW of the molecule
top_n: number of selected molecules
'''
smiles_MWdiff = {}
rem = 0
selected = set()
order_smiles = {}
for smi in pred_list:
try:
mol = Chem.MolFromSmiles(smi)
mw = Chem.Descriptors.ExactMolWt(mol)
diff = abs(original_mw-mw)
smiles_MWdiff[smi] = diff
except:
rem = rem + 1
sorted_smiles = sorted(smiles_MWdiff.items(), key=operator.itemgetter(1))
ii = 0
while len(selected)<top_n and ii<len(sorted_smiles):
selected.add(sorted_smiles[ii][0])
order_smiles[ii] = sorted_smiles[ii][0]
ii = ii + 1
return selected, order_smiles
def visualize_predictions(order_smiles, original, figures_dir, idx):
'''
Visualizes predictions and the reference molecule
Args:
order_smiles: dictionary with the top-n ordered smiles (dictionary maps the orderring indices to the smiles)
original: reference molecule in smiles
figures_dir: directory to save figures
idx: id number of the molecule
'''
work_dir = figures_dir + 'mol_' + str(idx) + '/'
if not os.path.exists(work_dir):
os.mkdir(work_dir)
filename = work_dir + 'original.png'
mol = Chem.MolFromSmiles(original)
Draw.MolToFile(mol,filename)
for ii in order_smiles.keys():
filename = work_dir + str(ii) + '.png'
mol = Chem.MolFromSmiles(order_smiles[ii])
Draw.MolToFile(mol,filename)
return 1