forked from aspuru-guzik-group/JANUS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutate_parr.py
executable file
·288 lines (214 loc) · 9.84 KB
/
mutate_parr.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 31 12:15:57 2021
@author: akshat
"""
from __future__ import print_function
import rdkit
import random
import multiprocessing
from rdkit import Chem
from rdkit.Chem import MolFromSmiles as smi2mol
from rdkit.Chem import MolToSmiles as mol2smi
from selfies import encoder, decoder
manager = multiprocessing.Manager()
lock = multiprocessing.Lock()
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 strings) :
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
def mutate_sf(sf_chars, alphabet):
'''
Given a list of SELFIES alphabets, make random changes to the molecule using
alphabet. Opertations to molecules are character replacements, additions and deletions.
Parameters
----------
sf_chars : (list of string alphabets)
List of string alphabets for a SELFIE string.
alphabet : (list of SELFIE strings)
Replacements and addition operations are performed using this list of SELFIE strings.
Returns
-------
Muatted SELFIE string.
'''
random_char_idx = random.choice(range(len(sf_chars)))
choices_ls = [1, 2, 3] # 1 = replacement; 2 = addition; 3=delete
mutn_choice = choices_ls[random.choice(range(len(choices_ls)))] # Which mutation to do:
if alphabet != []:
alphabet = random.sample(alphabet, 200) + ['[=N]', '[C]', '[S]','[Branch3_1]','[Expl=Ring3]','[Branch1_1]','[Branch2_2]','[Ring1]', '[#P]','[O]', '[Branch2_1]', '[N]','[=O]','[P]','[Expl=Ring1]','[Branch3_2]','[I]', '[Expl=Ring2]', '[=P]','[Branch1_3]','[#C]','[Cl]', '[=C]','[=S]','[Branch1_2]','[#N]','[Branch2_3]','[Br]','[Branch3_3]','[Ring3]','[Ring2]','[F]']
else:
alphabet = ['[=N]', '[C]', '[S]','[Branch3_1]','[Expl=Ring3]','[Branch1_1]','[Branch2_2]','[Ring1]', '[#P]','[O]', '[Branch2_1]', '[N]','[=O]','[P]','[Expl=Ring1]','[Branch3_2]','[I]', '[Expl=Ring2]', '[=P]','[Branch1_3]','[#C]','[Cl]', '[=C]','[=S]','[Branch1_2]','[#N]','[Branch2_3]','[Br]','[Branch3_3]','[Ring3]','[Ring2]','[F]']
# Mutate character:
if mutn_choice == 1:
random_char = alphabet[random.choice(range(len(alphabet)))]
change_sf = sf_chars[0:random_char_idx] + [random_char] + sf_chars[random_char_idx+1: ]
# add character:
elif mutn_choice == 2:
random_char = alphabet[random.choice(range(len(alphabet)))]
change_sf = sf_chars[0:random_char_idx] + [random_char] + sf_chars[random_char_idx: ]
# delete character:
elif mutn_choice == 3:
if len(sf_chars) != 1:
change_sf = sf_chars[0:random_char_idx] + sf_chars[random_char_idx+1: ]
else:
change_sf = sf_chars
return ''.join(x for x in change_sf)
def get_prop_material(smile, alphabet, num_random_samples, num_mutations):
'''
Given an input smile, perform mutations to the strucutre using provided SELFIE
alphabet list. 'num_random_samples' number of different SMILES orientations are
considered & total 'num_mutations' are performed.
Parameters
----------
smile : (str)
Valid SMILES string.
alphabet : (list of str)
list of SELFIE strings.
num_random_samples : (int)
Number of different SMILES orientations to be formed for the input smile.
num_mutations : TYPE
Number of mutations to perform on each of different orientations SMILES.
Returns
-------
mutated_smiles_canon : (list of strings)
List of unique molecules produced from mutations.
'''
mol = Chem.MolFromSmiles(smile)
Chem.Kekulize(mol)
# Obtain randomized orderings of the SMILES:
randomized_smile_orderings = []
for _ in range(num_random_samples):
randomized_smile_orderings.append(rdkit.Chem.MolToSmiles(mol, canonical=False, doRandom=True, isomericSmiles=False, kekuleSmiles=True))
# Convert all the molecules to SELFIES
selfies_ls = [encoder(x) for x in randomized_smile_orderings]
selfies_ls_chars = [get_selfie_chars(selfie) for selfie in selfies_ls]
# Obtain the mutated selfies
mutated_sf = []
for sf_chars in selfies_ls_chars:
for i in range(num_mutations):
if i == 0: mutated_sf.append(mutate_sf(sf_chars, alphabet))
else: mutated_sf.append(mutate_sf ( get_selfie_chars(mutated_sf[-1]), alphabet ))
mutated_smiles = [decoder(x) for x in mutated_sf]
mutated_smiles_canon = []
for item in mutated_smiles:
try:
smi_canon = Chem.MolToSmiles(Chem.MolFromSmiles(item, sanitize=True), isomericSmiles=False, canonical=True)
if len(smi_canon) <= 81: # Size restriction!
mutated_smiles_canon.append(smi_canon)
except:
continue
mutated_smiles_canon = list(set(mutated_smiles_canon))
return mutated_smiles_canon
def sanitize_smiles(smi):
'''
Return a canonical smile representation of smi
Parameters
----------
smi : str
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_chunks(arr, num_processors, ratio):
'''
Split list of SMILES int sublists, each of which will be operated on seperate cpus.
Parameters
----------
arr : (list of sts)
A list of SMILES.
num_processors : (int)
Number of cpus available for conducting operation.
ratio : (int)
number of operations that will be performed on each cpu.
Returns
-------
chunks: (list of lists)
Each sublist is used by a different cpu to perform operations.
'''
chunks = [] # Collect arrays that will be sent to different processorr
counter = int(ratio)
for i in range(num_processors):
if i == 0:
chunks.append(arr[0:counter])
if i != 0 and i<num_processors-1:
chunks.append(arr[counter-int(ratio): counter])
if i == num_processors-1:
chunks.append(arr[counter-int(ratio): ])
counter += int(ratio)
return chunks
def calc_parr_prop(unseen_smile_ls, property_name, props_collect, num_random_samples, num_mutations, alphabet):
'''Calculate logP for each molecule in unseen_smile_ls, and record results
in locked dictionary props_collect
'''
for smile in unseen_smile_ls:
props_collect[property_name][smile] = get_prop_material(smile, alphabet=alphabet, num_random_samples=num_random_samples, num_mutations=num_mutations) # TODO: TESTING
def create_parr_process(chunks, alphabet, property_name, num_random_samples, num_mutations):
'''
Create parallel processes for creating mutating molecules for molecule in sublist chunks.
Parameters
----------
chunks : (list of list)
list of lists containing SMILES.
property_name : (syr)
optional name paramtere to enter.
Returns
-------
combined_dict : (dict)
input smiles -> [List of mutated smiles].
'''
process_collector = []
collect_dictionaries = []
for item in chunks:
props_collect = manager.dict(lock=True)
smiles_map_ = manager.dict(lock=True)
props_collect[property_name] = smiles_map_
collect_dictionaries.append(props_collect)
if property_name == 'logP':
process_collector.append(multiprocessing.Process(target=calc_parr_prop, args=(item, property_name, props_collect, num_random_samples, num_mutations, alphabet, )))
for item in process_collector:
item.start()
for item in process_collector: # wait for all parallel processes to finish
item.join()
combined_dict = {} # collect results from multiple processess
for i,item in enumerate(collect_dictionaries):
combined_dict.update(item[property_name])
return combined_dict
def get_mutated_smiles(smiles, alphabet, space='Explore'):
num_processors = multiprocessing.cpu_count()
molecules_here_unique = list(set(smiles))
ratio = len(molecules_here_unique) / num_processors
chunks = get_chunks(molecules_here_unique, num_processors, ratio)
if space == 'Explore':
mut_smiles = create_parr_process(chunks, alphabet, 'logP', num_random_samples=5, num_mutations=5)
else:
mut_smiles = create_parr_process(chunks, alphabet, 'logP', num_random_samples=400, num_mutations=400)
return mut_smiles
if __name__ == '__main__':
molecules_here = ['CCC', 'CCCC', 'CCCCC', 'CCCCCCCC', 'CS', 'CSSS', 'CSSSSS', 'CF', 'CI', 'CBr', 'CSSSSSSSSSSSS', 'CSSSSSSSSSC', 'CSSSSCCSSSC', 'CSSSSSSSSSF', 'SSSSSC']
A = get_mutated_smiles(molecules_here, alphabet=['[C]']*500, space='Explore')