-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
232 lines (164 loc) · 4.97 KB
/
preprocess.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
from rdkit.Chem import Fragments
from rdkit import Chem, DataStructs
from rdkit.Chem import Descriptors, rdMolDescriptors
from scipy.spatial import distance
#####################
# FUNCTIONAL GROUPS #
#####################
def get_mol(smiles):
return Chem.MolFromSmiles(smiles)
def fp(mol):
return list(rdMolDescriptors.GetMorganFingerprintAsBitVect(mol, 2, nBits=512))
def logp(mol):
return round(Descriptors.MolLogP(mol), 4)
def molwt(mol):
return Descriptors.ExactMolWt(mol)
def NH2(mol):
return Fragments.fr_NH2(mol)
def NR2(mol):
patt1 = Chem.MolFromSmarts('c(N(C)(C))')
patt2 = Chem.MolFromSmarts('C(N(C)(C))')
total = 0
for p in [patt1, patt2]:
total += len(mol.GetSubstructMatches(p))
return total
def OH(mol):
total = Fragments.fr_Ar_OH(mol)
total += Fragments.fr_Al_OH(mol)
return total
def OR(mol):
patt1 = Chem.MolFromSmarts('c(OC)')
patt2 = Chem.MolFromSmarts('C(OC)')
total = 0
for p in [patt1, patt2]:
total += len(mol.GetSubstructMatches(p))
return total
def SH(mol):
return Fragments.fr_SH(mol)
def SR(mol):
return Fragments.fr_sulfide(mol)
def NO2(mol):
return Fragments.fr_nitro(mol)
def CN(mol):
patt = Chem.MolFromSmarts('C#N')
return len(mol.GetSubstructMatches(patt))
def SO3H(mol):
patt = Chem.MolFromSmarts('S(=O)(=O)(O)')
return len(mol.GetSubstructMatches(patt))
def CF3(mol):
patt = Chem.MolFromSmarts('FC(F)(F)')
return len(mol.GetSubstructMatches(patt))
def COOH(mol):
return Fragments.fr_Ar_COO(mol)
def F(mol):
patt1 = Chem.MolFromSmarts('c(F)')
patt2 = Chem.MolFromSmarts('C(F)')
total = 0
for p in [patt1, patt2]:
total += len(mol.GetSubstructMatches(p))
cf3 = Chem.MolFromSmarts('FC(F)(F)')
total -= 3 * len(mol.GetSubstructMatches(cf3))
return total
def Br(mol):
patt = Chem.MolFromSmarts('c(Br)')
total = len(mol.GetSubstructMatches(patt))
return total
####################
# REACTION CLASSES #
####################
def suzuki(mol):
grps = ['[#6;H0;D3:1]B([OH])[OH]', '[#6;H0;D3:2][Br,I,Cl]']
total = 0
for grp in grps:
patt = Chem.MolFromSmarts(grp)
total += len(mol.GetSubstructMatches(patt))
return total
def buchwald_hartwig(mol):
grps = [
'[Cl,Br,I][c;$(c1:[c,n]:[c,n]:[c,n]:[c,n]:[c,n]:1):1]',
'[N;$(NC)&!$(N=*)&!$([N-])&!$(N#*)&!$([ND3])&!$([ND4])&!$'
'(N[c,O])&!$(N[C,S]=[S,O,N]),H2&$(Nc1:[c,n]:[c,n]:[c,n]:[c,n]'
':[c,n]:1):2]'
]
total = 0
for grp in grps:
patt = Chem.MolFromSmarts(grp)
total += len(mol.GetSubstructMatches(patt))
return total
def schotten_baumann_amide(mol):
grps = [
'[C;$(C=O):1][OH1]',
'[N;$(N[#6]);!$(N=*);!$([N-]);!$(N#*);!$([ND3]);!$([ND4]);!$(N[O,N]);!$(N[C,S]=[S,O,N]):2]'
]
total = 0
for grp in grps:
patt = Chem.MolFromSmarts(grp)
total += len(mol.GetSubstructMatches(patt))
return total
def reductive_amination(mol):
grps = [
'[#6:4]-[C;H1,$([CH0](-[#6])[#6]):1]=[OD1]',
'[N;H2,$([NH1;D2](C)C);!$(N-[#6]=[*]):3]-[C:5]'
]
total = 0
for grp in grps:
patt = Chem.MolFromSmarts(grp)
total += len(mol.GetSubstructMatches(patt))
return total
def mida_deprotection(mol):
grps = ['[#6:1]B12OC(=O)C[N+](C)1CC(=O)O2']
total = 0
for grp in grps:
patt = Chem.MolFromSmarts(grp)
total += len(mol.GetSubstructMatches(patt))
return total
####################
# SIMILARITY CALCS #
####################
def calculate_pairwise_similarities(df):
def calc_tanimoto_similarity(fp1, fp2):
return DataStructs.TanimotoSimilarity(fp1, fp2)
n_molecules = len(df.smiles)
similarities = []
for i in range(n_molecules):
for j in range(n_molecules - i):
similarity = 1 - distance.jaccard(df.fp.iloc[i], df.fp.iloc[j])
similarities.append(similarity)
return similarities
#################
# PREPROCESSING #
#################
def preprocess(df_from_upload):
df = df_from_upload
df.columns = ['smiles']
preprocess_functions_fgroups = {
'fp': fp,
'logp': logp,
'molwt': molwt,
'NH2': NH2,
'NR2': NR2,
'OH': OH,
'OR': OR,
'SH': SH,
'SR': SR,
'NO2': NO2,
'CN': CN,
'SO3H': SO3H,
'CF3': CF3,
'COOH': COOH,
'F': F,
'Br': Br,
}
preprocess_functions_rxns = {
'suzuki-miyaura': suzuki,
'buchwald-hartwig': buchwald_hartwig,
'schotten-baumann-amide': schotten_baumann_amide,
'reductive-amination': reductive_amination,
'mida-deprotection': mida_deprotection,
}
df['mol'] = df.smiles.apply(get_mol)
for name, function in preprocess_functions_fgroups.items():
df[name] = df.mol.apply(function)
for name, function in preprocess_functions_rxns.items():
df[name] = df.mol.apply(function)
return df