-
Notifications
You must be signed in to change notification settings - Fork 4
/
featurizers.py
168 lines (144 loc) · 5.15 KB
/
featurizers.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
import uuid
from functools import partial
from multiprocessing import Pool
from time import gmtime, strftime
import numpy as np
from guacamol.scoring_function import BatchScoringFunction
from rdkit import Chem
from rdkit.Chem import AllChem
from sklearn.metrics import roc_auc_score
from rdkit.Chem import DataStructs, QED
from rdkit.Chem import rdMolDescriptors, FindMolChiralCenters
from rdkit.Chem.Scaffolds.MurckoScaffold import GetScaffoldForMol
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 one_maccs(smile):
"Calculate ECFP fingerprint. If smiles is invalid return none"
try:
m = Chem.MolFromSmiles(smile)
fp = np.array(rdkit.Chem.rdMolDescriptors.GetAtomFeatures(m))
return fp
except:
return None
def one_ap(smile):
"Calculate ECFP fingerprint. If smiles is invalid return none"
try:
m = Chem.MolFromSmiles(smile)
fp = np.array(rdMolDescriptors.GetHashedAtomPairFingerprintAsBitVect(m))
return fp
except:
return None
def one_ecfp_counts(smile, radius=2):
"Calculate ECFP fingerprint. If smiles is invalid return none"
try:
m = Chem.MolFromSmiles(smile)
fp = AllChem.GetHashedMorganFingerprint(m, 2, nBits=1024)
array = np.zeros((0,), dtype=np.int8)
DataStructs.ConvertToNumpyArray(fp, array)
return array
except:
print("Not Working")
return None
def one_fcfp(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, useFeatures=True))
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 ecfp6(smiles):
"""Input: list of SMILES
Output: list of descriptors.
Compute ECFP4 featurization."""
X = [one_ecfp(s, radius=3) for s in smiles]
return X
def fcfp4(smiles):
"""Input: list of SMILES
Output: list of descriptors.
Compute ECFP4 featurization."""
X = [one_fcfp(s, radius=2) for s in smiles]
return X
def fcfp6(smiles):
"""Input: list of SMILES
Output: list of descriptors.
Compute ECFP4 featurization."""
X = [one_fcfp(s, radius=3) for s in smiles]
return X
def maccs(smiles):
"""Input: list of SMILES
Output: list of descriptors.
Compute ECFP4 featurization."""
X = [one_maccs(s) for s in smiles]
return X
def ecfp4_counts(smiles):
"""Input: list of SMILES
Output: list of descriptors.
Compute ECFP4 featurization."""
X = [one_ecfp_counts(s, radius=2) for s in smiles]
return X
def ap(smiles):
"""Input: list of SMILES
Output: list of descriptors.
Compute ECFP4 featurization."""
X = [one_ap(s) for s in smiles]
return X
def one_physchem(smile):
try:
m = Chem.MolFromSmiles(smile)
if m is not None:
hba = rdMolDescriptors.CalcNumHBA(m)
hbd = rdMolDescriptors.CalcNumHBD(m)
nrings = rdMolDescriptors.CalcNumRings(m)
rtb = rdMolDescriptors.CalcNumRotatableBonds(m)
psa = rdMolDescriptors.CalcTPSA(m)
logp, mr = rdMolDescriptors.CalcCrippenDescriptors(m)
mw = rdMolDescriptors._CalcMolWt(m)
csp3 = rdMolDescriptors.CalcFractionCSP3(m)
hac = m.GetNumHeavyAtoms()
charges = []
for at in m.GetAtoms():
charges.append(at.GetFormalCharge())
if hac == 0:
fmf = 0
else:
fmf = GetScaffoldForMol(m).GetNumHeavyAtoms() / hac
ri = m.GetRingInfo()
n_rings = len(ri.AtomRings())
max_ring_size = len(max(ri.AtomRings(), key=len, default=()))
min_ring_size = len(min(ri.AtomRings(), key=len, default=()))
total_charges = sum(charges)
min_charge = min(charges)
max_charge = max(charges)
n_chiral_centers = len(FindMolChiralCenters(m, includeUnassigned=True))
return np.array([hba, hbd, hba + hbd, nrings, rtb, psa, logp, mr, mw,
csp3, fmf, hac,
max_ring_size, min_ring_size, total_charges, min_charge, max_charge, n_chiral_centers])
except:
return None
def physchem(smiles):
X = [one_physchem(s) for s in smiles]
return X
def physchem_and_ecfp4(smiles):
X = [np.concatenate((one_physchem(s), one_ecfp(s, radius=2))) for s in smiles]
return X
def physchem_and_ecfp4_counts(smiles):
X = [np.concatenate((one_physchem(s), one_ecfp_counts(s, radius=2))) for s in smiles]
return X
def qed(smiles):
mols = [Chem.MolFromSmiles(s) for s in smiles]
return [[QED.qed(mol)] if mol else 0 for mol in mols]