-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossover.py
307 lines (249 loc) · 9.04 KB
/
crossover.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
# %%
"""Written by Jan H.
Jensen 2018
"""
import random
import numpy as np
from rdkit import Chem, rdBase
from rdkit.Chem import AllChem
from rdkit.Chem.rdMolDescriptors import CalcNumRotatableBonds
rdBase.DisableLog("rdApp.error")
def cut(mol):
if not mol.HasSubstructMatch(Chem.MolFromSmarts("[*]-;!@[*]")):
return None
bis = random.choice(
mol.GetSubstructMatches(Chem.MolFromSmarts("[*]-;!@[*]"))
) # single bond not in ring
# print bis,bis[0],bis[1]
bs = [mol.GetBondBetweenAtoms(bis[0], bis[1]).GetIdx()]
fragments_mol = Chem.FragmentOnBonds(mol, bs, addDummies=True, dummyLabels=[(1, 1)])
try:
fragments = Chem.GetMolFrags(fragments_mol, asMols=True)
return fragments
except:
return None
def cut_ring(mol):
for i in range(10):
if random.random() < 0.5:
if not mol.HasSubstructMatch(Chem.MolFromSmarts("[R]@[R]@[R]@[R]")):
return None
bis = random.choice(
mol.GetSubstructMatches(Chem.MolFromSmarts("[R]@[R]@[R]@[R]"))
)
bis = (
(bis[0], bis[1]),
(bis[2], bis[3]),
)
else:
if not mol.HasSubstructMatch(Chem.MolFromSmarts("[R]@[R;!D2]@[R]")):
return None
bis = random.choice(
mol.GetSubstructMatches(Chem.MolFromSmarts("[R]@[R;!D2]@[R]"))
)
bis = (
(bis[0], bis[1]),
(bis[1], bis[2]),
)
# print bis
bs = [mol.GetBondBetweenAtoms(x, y).GetIdx() for x, y in bis]
fragments_mol = Chem.FragmentOnBonds(
mol, bs, addDummies=True, dummyLabels=[(1, 1), (1, 1)]
)
try:
fragments = Chem.GetMolFrags(fragments_mol, asMols=True)
except:
return None
if len(fragments) == 2:
return fragments
return None
def ring_OK(mol):
if not mol.HasSubstructMatch(Chem.MolFromSmarts("[R]")):
return True
ring_allene = mol.HasSubstructMatch(Chem.MolFromSmarts("[R]=[R]=[R]"))
cycle_list = mol.GetRingInfo().AtomRings()
max_cycle_length = max([len(j) for j in cycle_list])
macro_cycle = max_cycle_length > 6
double_bond_in_small_ring = mol.HasSubstructMatch(
Chem.MolFromSmarts("[r3,r4]=[r3,r4]")
)
return not ring_allene and not macro_cycle and not double_bond_in_small_ring
def tert_amine_OK(mol):
"""Checks if there is at least on tertiary amine."""
if mol.HasSubstructMatch(Chem.MolFromSmarts("[NX3;H0;D3;!$(NC=O);!$(*n);!$(*N)]")):
return True
else:
return False
def primary_secondary_amine_OK(mol):
"""Checks if there is any secondary or primary amines."""
if mol.HasSubstructMatch(Chem.MolFromSmarts("[NX3;H2,H1;!$(NC=O);!$(*n);!$(*N)]")):
return True
else:
return False
def is_parameterized(mol, forcefield="UFF"):
if forcefield is "UFF":
return AllChem.UFFHasAllMoleculeParams(mol)
if forcefield is "MMFF":
return AllChem.MMFFHasAllMoleculeParams(mol)
def mol_issane(mol: Chem.Mol, filter) -> bool:
"""Checks that a RDKit molecule matches some filter If a match is found
between the molecule and the filter the molecule is NOT suitable for
further use.
:param mol: SMILES string of molecule
:param filter: a frame with a filter
"""
# always return True (molecule OK) if a filter is not supplied
if filter is None:
return True
for pattern in filter:
if mol.HasSubstructMatch(pattern):
# print(smarts, row['rule_set_name']) #debug
print("matches:", Chem.MolToSmarts(pattern))
return False
return True
def mol_OK_old(mol, molecule_filter):
if not size_stdev or not average_size:
print("size parameters are not defined")
try:
Chem.SanitizeMol(mol)
test_mol = Chem.MolFromSmiles(Chem.MolToSmiles(mol))
if test_mol == None:
return None
if not mol_is_sane(mol, molecule_filter):
return False
target_size = (
size_stdev * np.random.randn() + average_size
) # parameters set in GA_mol
target_nrb = 2 * np.random.randn() + 5
if target_nrb < 5:
target_nrb = 5
print(CalcNumRotatableBonds(mol))
if mol.GetNumAtoms() > 5 and mol.GetNumAtoms() < target_size:
return True
else:
return False
except:
return False
def mol_OK(mol, filter):
"""Returns of molecule on input is OK according to various criteria
Criteria currently tested are:
* check if RDKit can understand the smiles string
* check if the size is OK
* check if the molecule is sane
:param mol string: SMILES string
:param filter string: the name of the filter to use
"""
try:
# check RDKit understands a molecule
Chem.SanitizeMol(mol)
test_mol = Chem.MolFromSmiles(Chem.MolToSmiles(mol))
if test_mol is None:
return False
# check molecule is sane
if not mol_issane(mol, filter):
return False
# check molecule size
target_size = (
size_stdev * np.random.randn() + average_size
) # parameters set in GA_mol
if mol.GetNumAtoms() > 5 and mol.GetNumAtoms() < target_size:
return True
else:
return False
except:
return False
def crossover_ring(parent_A, parent_B, filter):
ring_smarts = Chem.MolFromSmarts("[R]")
if not parent_A.HasSubstructMatch(ring_smarts) and not parent_B.HasSubstructMatch(
ring_smarts
):
return None
rxn_smarts1 = [
"[*:1]~[1*].[1*]~[*:2]>>[*:1]-[*:2]",
"[*:1]~[1*].[1*]~[*:2]>>[*:1]=[*:2]",
]
rxn_smarts2 = [
"([*:1]~[1*].[1*]~[*:2])>>[*:1]-[*:2]",
"([*:1]~[1*].[1*]~[*:2])>>[*:1]=[*:2]",
]
for i in range(10):
fragments_A = cut_ring(parent_A)
fragments_B = cut_ring(parent_B)
# print [Chem.MolToSmiles(x) for x in list(fragments_A)+list(fragments_B)]
if fragments_A == None or fragments_B == None:
return None
new_mol_trial = []
for rs in rxn_smarts1:
rxn1 = AllChem.ReactionFromSmarts(rs)
new_mol_trial = []
for fa in fragments_A:
for fb in fragments_B:
new_mol_trial.append(rxn1.RunReactants((fa, fb))[0])
new_mols = []
for rs in rxn_smarts2:
rxn2 = AllChem.ReactionFromSmarts(rs)
for m in new_mol_trial:
m = m[0]
if mol_OK(m, filter):
new_mols += list(rxn2.RunReactants((m,)))
new_mols2 = []
for m in new_mols:
m = m[0]
if (
mol_OK(m, filter)
and ring_OK(m)
and (primary_secondary_amine_OK(m) or tert_amine_OK(m))
):
new_mols2.append(m)
if len(new_mols2) > 0:
return random.choice(new_mols2)
return None
def crossover_non_ring(parent_A, parent_B, filter):
for i in range(10):
fragments_A = cut(parent_A)
fragments_B = cut(parent_B)
if fragments_A == None or fragments_B == None:
return None
rxn = AllChem.ReactionFromSmarts("[*:1]-[1*].[1*]-[*:2]>>[*:1]-[*:2]")
new_mol_trial = []
for fa in fragments_A:
for fb in fragments_B:
new_mol_trial.append(rxn.RunReactants((fa, fb))[0])
new_mols = []
for mol in new_mol_trial:
mol = mol[0]
if mol_OK(mol, filter) and (
primary_secondary_amine_OK(mol) or tert_amine_OK(mol)
):
new_mols.append(mol)
if len(new_mols) > 0:
return random.choice(new_mols)
return None
def crossover(parent_A, parent_B, filter):
parent_smiles = [Chem.MolToSmiles(parent_A), Chem.MolToSmiles(parent_B)]
try:
Chem.Kekulize(parent_A, clearAromaticFlags=True)
Chem.Kekulize(parent_B, clearAromaticFlags=True)
except:
pass
for i in range(10):
if random.random() <= 0.5:
# print 'non-ring crossover'
new_mol = crossover_non_ring(parent_A, parent_B, filter)
if new_mol != None:
new_smiles = Chem.MolToSmiles(new_mol)
if new_mol != None and new_smiles not in parent_smiles:
return new_mol
else:
# print 'ring crossover'
new_mol = crossover_ring(parent_A, parent_B, filter)
if new_mol != None:
new_smiles = Chem.MolToSmiles(new_mol)
if new_mol != None and new_smiles not in parent_smiles:
return new_mol
return new_mol
if __name__ == "__main__":
smiles1 = "Cc1ccc(S(=O)(=O)N2C(N)=C(C#N)C(c3ccc(Cl)cc3)C2C(=O)c2ccccc2)cc1"
smiles2 = "C[C@@H]1CC(Nc2cncc(-c3nncn3C)c2)C[C@@H](C)C1"
mol1 = Chem.MolFromSmiles(smiles1)
mol2 = Chem.MolFromSmiles(smiles2)
child = crossover(mol1, mol2, None)