-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
424 lines (349 loc) · 12.8 KB
/
utils.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
import scipy
import os
import json
import rdkit
from rdkit import Chem
from rdkit.Chem.Draw import MolsToGridImage
import pandas as pd
import numpy as np
from rdkit.Chem import AllChem
from sklearn.decomposition import PCA
import math
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
from rdkit import Chem, DataStructs
from rdkit.Chem import AllChem, Descriptors
import numpy as np
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from rdkit import DataStructs
from rdkit.ML.Cluster import Butina
from rdkit.DataStructs.cDataStructs import TanimotoSimilarity
from rdkit.Chem.Descriptors import qed
from rdkit.Chem import Descriptors, Mol, rdMolDescriptors
from rdkit import Chem
from rdkit.Chem import rdMolDescriptors
import pickle
import math
from collections import defaultdict
import os.path as op
_fscores = None
def readFragmentScores(name="fpscores"):
import gzip
global _fscores
# generate the full path filename:
if name == "fpscores":
name = op.join(os.getcwd(), name)
data = pickle.load(gzip.open("%s.pkl.gz" % name))
outDict = {}
for i in data:
for j in range(1, len(i)):
outDict[i[j]] = float(i[0])
_fscores = outDict
def numBridgeheadsAndSpiro(mol, ri=None):
nSpiro = rdMolDescriptors.CalcNumSpiroAtoms(mol)
nBridgehead = rdMolDescriptors.CalcNumBridgeheadAtoms(mol)
return nBridgehead, nSpiro
def calculateScore(m):
if _fscores is None:
readFragmentScores()
# fragment score
fp = rdMolDescriptors.GetMorganFingerprint(
m, 2
) # <- 2 is the *radius* of the circular fingerprint
fps = fp.GetNonzeroElements()
score1 = 0.0
nf = 0
for bitId, v in fps.items():
nf += v
sfp = bitId
score1 += _fscores.get(sfp, -4) * v
score1 /= nf
# features score
nAtoms = m.GetNumAtoms()
nChiralCenters = len(Chem.FindMolChiralCenters(m, includeUnassigned=True))
ri = m.GetRingInfo()
nBridgeheads, nSpiro = numBridgeheadsAndSpiro(m, ri)
nMacrocycles = 0
for x in ri.AtomRings():
if len(x) > 8:
nMacrocycles += 1
sizePenalty = nAtoms**1.005 - nAtoms
stereoPenalty = math.log10(nChiralCenters + 1)
spiroPenalty = math.log10(nSpiro + 1)
bridgePenalty = math.log10(nBridgeheads + 1)
macrocyclePenalty = 0.0
# ---------------------------------------
# This differs from the paper, which defines:
# macrocyclePenalty = math.log10(nMacrocycles+1)
# This form generates better results when 2 or more macrocycles are present
if nMacrocycles > 0:
macrocyclePenalty = math.log10(2)
score2 = (
0.0
- sizePenalty
- stereoPenalty
- spiroPenalty
- bridgePenalty
- macrocyclePenalty
)
# correction for the fingerprint density
# not in the original publication, added in version 1.1
# to make highly symmetrical molecules easier to synthetise
score3 = 0.0
if nAtoms > len(fps):
score3 = math.log(float(nAtoms) / len(fps)) * 0.5
sascore = score1 + score2 + score3
# need to transform "raw" value into scale between 1 and 10
min = -4.0
max = 2.5
sascore = 11.0 - (sascore - min + 1) / (max - min) * 9.0
# smooth the 10-end
if sascore > 8.0:
sascore = 8.0 + math.log(sascore + 1.0 - 9.0)
if sascore > 10.0:
sascore = 10.0
elif sascore < 1.0:
sascore = 1.0
return sascore
def average_tanimoto_similarity(fps_1, fps_2):
sim = 0
count = 0
for fp in fps_1:
for fp_other in fps_2:
sim += DataStructs.FingerprintSimilarity(fp, fp_other)
count += 1
return sim / count
def max_tanimoto_similarity(fps_1, fps_2):
sim = 0
count = 0
for fp in fps_1:
for fp_other in fps_2:
sim = max(sim, DataStructs.FingerprintSimilarity(fp, fp_other))
return sim
def indexes_identical_fps(fps_1, fps_2):
indexes = []
indexes_other = []
for i, fp in enumerate(fps_1):
for j, fp_other in enumerate(fps_2):
if DataStructs.FingerprintSimilarity(fp, fp_other) > 0.99:
indexes.append(i)
indexes_other.append(j)
return indexes, indexes_other
def tanimoto_similarities(fps_1, fps_2):
sim = []
for fp in fps_1:
for fp_other in fps_2:
sim.append(DataStructs.FingerprintSimilarity(fp, fp_other))
return sim
def ClusterFps(fps, cutoff=0.2):
# Source :
# first generate the distance matrix:
dists = []
nfps = len(fps)
for i in range(1, nfps):
sims = DataStructs.BulkTanimotoSimilarity(fps[i], fps[:i])
dists.extend([1 - x for x in sims])
# now cluster the data:
cs = Butina.ClusterData(dists, nfps, cutoff, isDistData=True)
return cs
def find_cluster(fp, centroids_fp):
index = 0
max_sim = 0
for i, fp_cent in enumerate(centroids_fp):
sim = DataStructs.FingerprintSimilarity(fp, fp_cent)
if sim > max_sim:
max_sim = sim
index = i
return index
def get_n_clusters(fps, cutoff=0.7):
cs = ClusterFps(fps, cutoff=cutoff)
return len(cs)
def return_distribution_cycle_size(smiles):
max_size = []
for s in smiles:
m = Chem.MolFromSmiles(s)
if m:
ri = m.GetRingInfo()
n_rings = len(ri.AtomRings())
max_ring_size = len(max(ri.AtomRings(), key=len, default=()))
max_size.append(max_ring_size)
return max_size
def return_distribution_mw(smiles):
molecular_weights = []
for s in smiles:
m = Chem.MolFromSmiles(s)
if m:
mw = rdMolDescriptors._CalcMolWt(m)
molecular_weights.append(mw)
return molecular_weights
def return_distribution_radicals(smiles):
radicals = []
for s in smiles:
m = Chem.MolFromSmiles(s)
if m:
r = Descriptors.NumRadicalElectrons(m)
radicals.append(r)
return radicals
def return_distribution_sulphur(smiles):
sulphur = []
for s in smiles:
m = Chem.MolFromSmiles(s)
if m:
substructure = Chem.MolFromSmarts("S")
sulphur.append(len(m.GetSubstructMatches(substructure)))
return sulphur
def return_distribution_halogen(smiles):
halogen = []
for s in smiles:
m = Chem.MolFromSmiles(s)
if m:
substructure = Chem.MolFromSmarts("[F,Cl,Br,I]")
halogen.append(len(m.GetSubstructMatches(substructure)))
return halogen
def return_distribution_heteroatoms(smiles):
heteroatoms = []
for s in smiles:
m = Chem.MolFromSmiles(s)
if m:
substructure = Chem.MolFromSmarts("[!C;!H;!c]~[!C;!H;!c]")
heteroatoms.append(len(m.GetSubstructMatches(substructure)))
return heteroatoms
def qualitative_analysis(smiles, smiles_test):
values = []
distributions = []
properties = []
max_size = return_distribution_cycle_size(smiles)
values.extend(max_size)
distributions.extend(["Generated" for _ in range(len(smiles))])
properties.extend(["Cycle Size" for _ in range(len(smiles))])
max_size_ref = return_distribution_cycle_size(smiles_test)
values.extend(max_size_ref)
distributions.extend(["Dataset" for _ in range(len(smiles_test))])
properties.extend(["Cycle Size" for _ in range(len(smiles_test))])
molecular_weights = return_distribution_mw(smiles)
values.extend(molecular_weights)
distributions.extend(["Generated" for _ in range(len(smiles))])
properties.extend(["Molecular Weights" for _ in range(len(smiles))])
molecular_weights_ref = return_distribution_mw(smiles_test)
values.extend(molecular_weights_ref)
distributions.extend(["Dataset" for _ in range(len(smiles_test))])
properties.extend(["Molecular Weights" for _ in range(len(smiles_test))])
sulphur = return_distribution_sulphur(smiles)
values.extend(sulphur)
distributions.extend(["Generated" for _ in range(len(smiles))])
properties.extend(["Number of Sulphurs" for _ in range(len(smiles))])
sulphur_ref = return_distribution_sulphur(smiles_test)
values.extend(sulphur_ref)
distributions.extend(["Dataset" for _ in range(len(smiles_test))])
properties.extend(["Number of Sulphurs" for _ in range(len(smiles_test))])
halogen = return_distribution_halogen(smiles)
values.extend(halogen)
distributions.extend(["Generated" for _ in range(len(smiles))])
properties.extend(["Number of Halogens" for _ in range(len(smiles))])
halogen_ref = return_distribution_halogen(smiles_test)
values.extend(halogen_ref)
distributions.extend(["Dataset" for _ in range(len(smiles_test))])
properties.extend(["Number of Halogens" for _ in range(len(smiles_test))])
heteroatoms = return_distribution_heteroatoms(smiles)
values.extend(heteroatoms)
distributions.extend(["Generated" for _ in range(len(smiles))])
properties.extend(["Number of heteroatoms" for _ in range(len(smiles))])
heteroatoms_ref = return_distribution_heteroatoms(smiles_test)
values.extend(heteroatoms_ref)
distributions.extend(["Dataset" for _ in range(len(smiles_test))])
properties.extend(["Number of heteroatoms" for _ in range(len(smiles_test))])
radicals = return_distribution_radicals(smiles)
values.extend(radicals)
distributions.extend(["Generated" for _ in range(len(smiles))])
properties.extend(["Number of radicals" for _ in range(len(smiles))])
radicals_ref = return_distribution_radicals(smiles_test)
values.extend(radicals_ref)
distributions.extend(["Dataset" for _ in range(len(smiles_test))])
properties.extend(["Number of radicals" for _ in range(len(smiles_test))])
return values, distributions, properties
def quantitative_analysis(
smiles, smiles_test, lower_percentile=0, higher_percentile=100
):
max_size = np.array(return_distribution_cycle_size(smiles))
max_size_ref = np.array(return_distribution_cycle_size(smiles_test))
cycle_size_ok = np.mean(
np.array(max_size >= np.percentile(max_size_ref, lower_percentile))
& np.array(max_size <= np.percentile(max_size_ref, higher_percentile))
)
molecular_weights = return_distribution_mw(smiles)
molecular_weights_ref = return_distribution_mw(smiles_test)
molecular_weights_ok = np.mean(
np.array(
molecular_weights >= np.percentile(molecular_weights_ref, lower_percentile)
)
& np.array(
molecular_weights <= np.percentile(molecular_weights_ref, higher_percentile)
)
)
heteroatoms = return_distribution_heteroatoms(smiles)
heteroatoms_ref = return_distribution_heteroatoms(smiles_test)
heteroatoms_ok = np.mean(
np.array(heteroatoms >= np.percentile(heteroatoms_ref, lower_percentile))
& np.array(heteroatoms <= np.percentile(heteroatoms_ref, higher_percentile))
)
radicals = return_distribution_radicals(smiles)
radicals_ref = return_distribution_radicals(smiles_test)
radicals_ok = np.mean(
np.array(radicals >= np.percentile(radicals_ref, lower_percentile))
& np.array(radicals <= np.percentile(radicals_ref, higher_percentile))
)
return (
100 * cycle_size_ok,
100 * molecular_weights_ok,
100 * heteroatoms_ok,
100 * radicals_ok,
)
def one_ecfp(smile, radius=2):
"Calculate ECFP fingerprint. If smiles is invalid return none"
try:
m = Chem.MolFromSmiles(smile)
fp = np.array(AllChem.GetMorganFingerprintAsBitVect(m, radius, nBits=1024))
return fp
except:
return None
def ecfp4(smiles):
"""Input: list of SMILES
Output: list of descriptors.
Compute ECFP4 featurization."""
X = [one_ecfp(s, radius=2) for s in smiles]
return X
def data_split(dataset):
"""
Args:
chid: which assay to use:
external_file:
Returns:
clfs: Dictionary of fitted classifiers
aucs: Dictionary of AUCs
balance: Two numbers showing the number of actives in split 1 / split 2
df1: data in split 1
df2: data in split 2
"""
# read data and calculate ecfp fingerprints
assay_file = f"datasets/{dataset}.csv"
print(f"Reading data from: {assay_file}")
df = pd.read_csv(assay_file)
df["ecfp"] = ecfp4(df.smiles)
df_train, df_test = train_test_split(
df, test_size=0.25, stratify=df["label"], random_state=0
)
X1 = np.array(list(df_train["ecfp"]))
X2 = np.array(list(df_test["ecfp"]))
y1 = np.array(list(df_train["label"]))
y2 = np.array(list(df_test["label"]))
# train classifiers and store them in dictionary
clf = RandomForestClassifier(n_estimators=100, n_jobs=1, random_state=0)
clf.fit(X1, y1)
return (
clf.predict_proba(X2[np.where(y2 == 1)[0], :])[:, 1],
list(df_test.smiles),
list(df_test.label),
clf,
)