-
Notifications
You must be signed in to change notification settings - Fork 36
/
sim_search.py
351 lines (257 loc) · 12.4 KB
/
sim_search.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
import time
import selfies
import rdkit
import numpy as np
from rdkit import Chem
from selfies import encoder, decoder
from rdkit.Chem import MolFromSmiles as smi2mol
from rdkit.Chem import AllChem
from rdkit.DataStructs.cDataStructs import TanimotoSimilarity
from rdkit.Chem import Mol
from rdkit.Chem.AtomPairs.Sheridan import GetBPFingerprint, GetBTFingerprint
from rdkit.Chem.Pharm2D import Generate, Gobbi_Pharm2D
from rdkit.Chem import MolToSmiles as mol2smi
from rdkit import RDLogger
RDLogger.DisableLog('rdApp.*')
def randomize_smiles(mol):
'''Returns a random (dearomatized) SMILES given an rdkit mol object of a molecule.
Parameters:
mol (rdkit.Chem.rdchem.Mol) : RdKit mol object (None if invalid smile string smi)
Returns:
mol (rdkit.Chem.rdchem.Mol) : RdKit mol object (None if invalid smile string smi)
'''
if not mol:
return None
Chem.Kekulize(mol)
return rdkit.Chem.MolToSmiles(mol, canonical=False, doRandom=True, isomericSmiles=False, kekuleSmiles=True)
def sanitize_smiles(smi):
'''Return a canonical smile representation of smi
Parameters:
smi (string) : smile string to be canonicalized
Returns:
mol (rdkit.Chem.rdchem.Mol) : RdKit mol object (None if invalid smile string smi)
smi_canon (string) : Canonicalized smile representation of smi (None if invalid smile string smi)
conversion_successful (bool): True/False to indicate if conversion was successful
'''
try:
mol = smi2mol(smi, sanitize=True)
smi_canon = mol2smi(mol, isomericSmiles=False, canonical=True)
return (mol, smi_canon, True)
except:
return (None, None, False)
def get_selfie_chars(selfie):
'''Obtain a list of all selfie characters in string selfie
Parameters:
selfie (string) : A selfie string - representing a molecule
Example:
>>> get_selfie_chars('[C][=C][C][=C][C][=C][Ring1][Branch1_1]')
['[C]', '[=C]', '[C]', '[=C]', '[C]', '[=C]', '[Ring1]', '[Branch1_1]']
Returns:
chars_selfie: list of selfie characters present in molecule selfie
'''
chars_selfie = [] # A list of all SELFIE sybols from string selfie
while selfie != '':
chars_selfie.append(selfie[selfie.find('['): selfie.find(']')+1])
selfie = selfie[selfie.find(']')+1:]
return chars_selfie
class _FingerprintCalculator:
''' Calculate the fingerprint for a molecule, given the fingerprint type
Parameters:
mol (rdkit.Chem.rdchem.Mol) : RdKit mol object (None if invalid smile string smi)
fp_type (string) :Fingerprint type (choices: AP/PHCO/BPF,BTF,PAT,ECFP4,ECFP6,FCFP4,FCFP6)
Returns:
RDKit fingerprint object
'''
def get_fingerprint(self, mol: Mol, fp_type: str):
method_name = 'get_' + fp_type
method = getattr(self, method_name)
if method is None:
raise Exception(f'{fp_type} is not a supported fingerprint type.')
return method(mol)
def get_AP(self, mol: Mol):
return AllChem.GetAtomPairFingerprint(mol, maxLength=10)
def get_PHCO(self, mol: Mol):
return Generate.Gen2DFingerprint(mol, Gobbi_Pharm2D.factory)
def get_BPF(self, mol: Mol):
return GetBPFingerprint(mol)
def get_BTF(self, mol: Mol):
return GetBTFingerprint(mol)
def get_PATH(self, mol: Mol):
return AllChem.RDKFingerprint(mol)
def get_ECFP4(self, mol: Mol):
return AllChem.GetMorganFingerprint(mol, 2)
def get_ECFP6(self, mol: Mol):
return AllChem.GetMorganFingerprint(mol, 3)
def get_FCFP4(self, mol: Mol):
return AllChem.GetMorganFingerprint(mol, 2, useFeatures=True)
def get_FCFP6(self, mol: Mol):
return AllChem.GetMorganFingerprint(mol, 3, useFeatures=True)
def get_fingerprint(mol: Mol, fp_type: str):
''' Fingerprint getter method. Fingerprint is returned after using object of
class '_FingerprintCalculator'
Parameters:
mol (rdkit.Chem.rdchem.Mol) : RdKit mol object (None if invalid smile string smi)
fp_type (string) :Fingerprint type (choices: AP/PHCO/BPF,BTF,PAT,ECFP4,ECFP6,FCFP4,FCFP6)
Returns:
RDKit fingerprint object
'''
return _FingerprintCalculator().get_fingerprint(mol=mol, fp_type=fp_type)
def mutate_selfie(selfie, max_molecules_len, write_fail_cases=False):
'''Return a mutated selfie string (only one mutation on slefie is performed)
Mutations are done until a valid molecule is obtained
Rules of mutation: With a 33.3% propbabily, either:
1. Add a random SELFIE character in the string
2. Replace a random SELFIE character with another
3. Delete a random character
Parameters:
selfie (string) : SELFIE string to be mutated
max_molecules_len (int) : Mutations of SELFIE string are allowed up to this length
write_fail_cases (bool) : If true, failed mutations are recorded in "selfie_failure_cases.txt"
Returns:
selfie_mutated (string) : Mutated SELFIE string
smiles_canon (string) : canonical smile of mutated SELFIE string
'''
valid=False
fail_counter = 0
chars_selfie = get_selfie_chars(selfie)
while not valid:
fail_counter += 1
alphabet = list(selfies.get_semantic_robust_alphabet()) # 34 SELFIE characters
choice_ls = [1, 2, 3] # 1=Insert; 2=Replace; 3=Delete
random_choice = np.random.choice(choice_ls, 1)[0]
# Insert a character in a Random Location
if random_choice == 1:
random_index = np.random.randint(len(chars_selfie)+1)
random_character = np.random.choice(alphabet, size=1)[0]
selfie_mutated_chars = chars_selfie[:random_index] + [random_character] + chars_selfie[random_index:]
# Replace a random character
elif random_choice == 2:
random_index = np.random.randint(len(chars_selfie))
random_character = np.random.choice(alphabet, size=1)[0]
if random_index == 0:
selfie_mutated_chars = [random_character] + chars_selfie[random_index+1:]
else:
selfie_mutated_chars = chars_selfie[:random_index] + [random_character] + chars_selfie[random_index+1:]
# Delete a random character
elif random_choice == 3:
random_index = np.random.randint(len(chars_selfie))
if random_index == 0:
selfie_mutated_chars = chars_selfie[random_index+1:]
else:
selfie_mutated_chars = chars_selfie[:random_index] + chars_selfie[random_index+1:]
else:
raise Exception('Invalid Operation trying to be performed')
selfie_mutated = "".join(x for x in selfie_mutated_chars)
sf = "".join(x for x in chars_selfie)
try:
smiles = decoder(selfie_mutated)
mol, smiles_canon, done = sanitize_smiles(smiles)
if len(selfie_mutated_chars) > max_molecules_len or smiles_canon=="":
done = False
if done:
valid = True
else:
valid = False
except:
valid=False
if fail_counter > 1 and write_fail_cases == True:
f = open("selfie_failure_cases.txt", "a+")
f.write('Tried to mutate SELFIE: '+str(sf)+' To Obtain: '+str(selfie_mutated) + '\n')
f.close()
return (selfie_mutated, smiles_canon)
def get_mutated_SELFIES(selfies_ls, num_mutations):
''' Mutate all the SELFIES in 'selfies_ls' 'num_mutations' number of times.
Parameters:
selfies_ls (list) : A list of SELFIES
num_mutations (int) : number of mutations to perform on each SELFIES within 'selfies_ls'
Returns:
selfies_ls (list) : A list of mutated SELFIES
'''
for _ in range(num_mutations):
selfie_ls_mut_ls = []
for str_ in selfies_ls:
str_chars = get_selfie_chars(str_)
max_molecules_len = len(str_chars) + num_mutations
selfie_mutated, _ = mutate_selfie(str_, max_molecules_len)
selfie_ls_mut_ls.append(selfie_mutated)
selfies_ls = selfie_ls_mut_ls.copy()
return selfies_ls
def get_fp_scores(smiles_back, target_smi, fp_type):
'''Calculate the Tanimoto fingerprint (using fp_type fingerint) similarity between a list
of SMILES and a known target structure (target_smi).
Parameters:
smiles_back (list) : A list of valid SMILES strings
target_smi (string) : A valid SMILES string. Each smile in 'smiles_back' will be compared to this stucture
fp_type (string) : Type of fingerprint (choices: AP/PHCO/BPF,BTF,PAT,ECFP4,ECFP6,FCFP4,FCFP6)
Returns:
smiles_back_scores (list of floats) : List of fingerprint similarities
'''
smiles_back_scores = []
target = Chem.MolFromSmiles(target_smi)
fp_target = get_fingerprint(target, fp_type)
for item in smiles_back:
mol = Chem.MolFromSmiles(item)
# fp_mol = get_ECFP4(mol)
fp_mol = get_fingerprint(mol, fp_type)
score = TanimotoSimilarity(fp_mol, fp_target)
smiles_back_scores.append(score)
return smiles_back_scores
if __name__ == "__main__":
# Executable code for EXPERIMENT B:
# Aripirazole starting structure:
smi = 'C1CC(=O)NC2=C1C=CC(=C2)OCCCCN3CCN(CC3)C4=C(C(=CC=C4)Cl)Cl' # Aripiprazole
fp_type = 'ECFP4'
# Albuterol starting structure:
# smi = 'CC(C)(C)NCC(C1=CC(=C(C=C1)O)CO)O' # Albuterol
# fp_type = 'FCFP4'
# Mestranol starting structure:
# smi = 'CC12CCC3C(C1CCC2(C#C)O)CCC4=C3C=CC(=C4)OC' # Mestranol
# fp_type = 'AP'
total_time = time.time()
# num_random_samples = 50000 # TODO
num_random_samples = 1000
num_mutation_ls = [1, 2, 3, 4, 5]
mol = Chem.MolFromSmiles(smi)
if mol == None:
raise Exception('Invalid starting structure encountered')
start_time = time.time()
randomized_smile_orderings = [randomize_smiles(mol) for _ in range(num_random_samples)]
# Convert all the molecules to SELFIES
selfies_ls = [encoder(x) for x in randomized_smile_orderings]
print('Randomized molecules (in SELFIES) time: ', time.time()-start_time)
all_smiles_collect = []
all_smiles_collect_broken = []
start_time = time.time()
for num_mutations in num_mutation_ls:
# Mutate the SELFIES:
selfies_mut = get_mutated_SELFIES(selfies_ls.copy(), num_mutations=num_mutations)
# Convert back to SMILES:
smiles_back = [decoder(x) for x in selfies_mut]
all_smiles_collect = all_smiles_collect + smiles_back
all_smiles_collect_broken.append(smiles_back)
print('Mutation obtainment time (back to smiles): ', time.time()-start_time)
# Work on: all_smiles_collect
start_time = time.time()
canon_smi_ls = []
for item in all_smiles_collect:
mol, smi_canon, did_convert = sanitize_smiles(item)
if mol == None or smi_canon == '' or did_convert == False:
raise Exception('Invalid smile string found')
canon_smi_ls.append(smi_canon)
canon_smi_ls = list(set(canon_smi_ls))
print('Unique mutated structure obtainment time: ', time.time()-start_time)
start_time = time.time()
canon_smi_ls_scores = get_fp_scores(canon_smi_ls, target_smi=smi, fp_type=fp_type)
print('Fingerprint calculation time: ', time.time()-start_time)
print('Total time: ', time.time()-total_time)
print()
A, B, C = [x for x in canon_smi_ls_scores if x > 0.75], [x for x in canon_smi_ls_scores if x > 0.6], [x for x in canon_smi_ls_scores if x > 0.4]
print('Number of molecules: ')
print(' # Delta > 0.75: ', len(A) )
print(' # Delta > 0.6: ', len(B))
print(' # Delta > 0.4: ', len(C))
print()
print('Percentage of total molecules: ')
print(' % Delta > 0.75: ', (len(A)/len(canon_smi_ls_scores))*100 )
print(' % Delta > 0.6: ', (len(B)/len(canon_smi_ls_scores))*100)
print(' % Delta > 0.4: ', (len(C)/len(canon_smi_ls_scores))*100)