-
Notifications
You must be signed in to change notification settings - Fork 0
/
mol_methods.py
385 lines (293 loc) · 10.2 KB
/
mol_methods.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"""
MOL METHODS
====================
Compendium of methods for SMILES parsing and molecular metrics handling.
This module is mainly a reorganization of Gabriel Guimaraes and Benjamin
Sanchez-Lengeling's original implementation (http://github.com/gablg1/ORGAN).
Carlos Outeiral has cleaned up and documented the code, as well
as added a few functions and modified the I/O part. Benjamin Sanchez-Lengeling
has added new mathematical utilities.
"""
from __future__ import absolute_import, division, print_function
import os
import csv
import numpy as np
from rdkit.Chem import AllChem as Chem
from rdkit.Chem import MolFromSmiles, MolToSmiles
""" DATA I/O """
def read_smi(filename):
"""Reads SMILES from a .smi file.
Arguments
-----------
- filename. String pointing to the .smi file.
"""
with open(filename) as file:
smiles = file.readlines()
smiles = [i.strip() for i in smiles]
return smiles
def read_smiles_csv(filename):
"""Reads SMILES from a .csv file
Arguments
-----------
- filename. String pointing to the .csv file.
Note
-----------
This function will assume that the SMILES are
in column 0.
"""
with open(filename) as file:
reader = csv.reader(file)
smiles_idx = next(reader).index("smiles")
data = [row[smiles_idx] for row in reader]
return data
def load_train_data(filename):
"""Loads training data from a .csv or .smi file
Arguments
-----------
- filename. String pointing to the .csv or .smi file.
"""
ext = filename.split(".")[-1]
if ext == 'csv':
return read_smiles_csv(filename)
if ext == 'smi':
return read_smi(filename)
else:
raise ValueError('data is not smi or csv!')
return
def save_smi(name, smiles):
"""Saves SMILES data as a .smi file.
Arguments
-----------
- filename. String pointing to the .smi file.
- smiles. List of SMILES strings to be saved.
"""
if not os.path.exists('epoch_data'):
os.makedirs('epoch_data')
smi_file = os.path.join('epoch_data', "{}.smi".format(name))
with open(smi_file, 'w') as afile:
afile.write('\n'.join(smiles))
return
""" MATHEMATICAL UTILITIES """
def checkarray(x):
if type(x) == np.ndarray or type(x) == list:
if x.size == 1:
return False
else:
return True
else:
return False
def gauss_remap(x, x_mean, x_std):
"""Remaps a given value to a gaussian distribution.
Arguments
-----------
- x. Value to be remapped.
- x_mean. Mean of the distribution.
- x_std. Standard deviation of the distribution.
"""
return np.exp(-(x - x_mean)**2 / (x_std**2))
def remap(x, x_min, x_max):
"""Remaps a given value to [0, 1].
Arguments
-----------
- x. Value to be remapped.
- x_min. Minimum value (will correspond to 0).
- x_max. Maximum value (will correspond to 1).
Note
-----------
If x > x_max or x < x_min, the value will be outside
of the [0, 1] interval.
"""
if x_max != 0 and x_min != 0:
return 0
elif x_max - x_min == 0:
return x
else:
return (x - x_min) / (x_max - x_min)
def constant_range(x, x_low, x_high):
if checkarray(x):
return np.array([constant_range_func(xi, x_low, x_high) for xi in x])
else:
return constant_range_func(x, x_low, x_high)
def constant_range_func(x, x_low, x_high):
"""Returns 1 if x is in [x_low, x_high] and 0 if not."""
if x <= x_low or x >= x_high:
return 0
else:
return 1
def constant_bump_func(x, x_low, x_high, decay=0.025):
if x <= x_low:
return np.exp(-(x - x_low)**2 / decay)
elif x >= x_high:
return np.exp(-(x - x_high)**2 / decay)
else:
return 1
def constant_bump(x, x_low, x_high, decay=0.025):
if checkarray(x):
return np.array([constant_bump_func(xi, x_low, x_high, decay) for xi in x])
else:
return constant_bump_func(x, x_low, x_high, decay)
def smooth_plateau(x, x_point, decay=0.025, increase=True):
if checkarray(x):
return np.array([smooth_plateau_func(xi, x_point, decay, increase) for xi in x])
else:
return smooth_plateau_func(x, x_point, decay, increase)
def smooth_plateau_func(x, x_point, decay=0.025, increase=True):
if increase:
if x <= x_point:
return np.exp(-(x - x_point)**2 / decay)
else:
return 1
else:
if x >= x_point:
return np.exp(-(x - x_point)**2 / decay)
else:
return 1
def pct(a, b):
if len(b) == 0:
return 0
return float(len(a)) / len(b)
def rectification(x, x_low, x_high, reverse=False):
if checkarray(x):
return np.array([rec_fun(xi, x_low, x_high, reverse) for xi in x])
else:
return rec_fun(x, x_low, x_high, reverse)
def rec_fun(x, x_low, x_high, reverse=False):
if reverse == True:
if x_low <= x <= x_high:
return 0
else:
return x
else:
if x_low <= x <= x_high:
return x
else:
return 0
def asym_rectification(x, y, reverse=False):
if checkarray(x):
return np.array([asymrec_fun(xi, y, reverse=reverse) for xi in x])
else:
return asymrec_fun(x, y, reverse=reverse)
def asymrec_fun(x, y, reverse=False):
if reverse == True:
if x < y:
return x
else:
return 0
else:
if x < y:
return 0
else:
return x
"""Encoding/decoding utilities"""
def canon_smile(smile):
"""Transforms to canonic SMILES"""
return MolToSmiles(MolFromSmiles(smile))
def verified_and_below(smile, max_len):
"""Returns True if the SMILES string is valid and
its length is less than max_len."""
return len(smile) < max_len and verify_sequence(smile)
def verify_sequence(smile):
"""Returns True if the SMILES string is valid and
its length is less than max_len."""
mol = Chem.MolFromSmiles(smile)
return smile != '' and mol is not None and mol.GetNumAtoms() > 1
def apply_to_valid(smile, fun, **kwargs):
"""Returns fun(smile, **kwargs) if smiles is a valid
SMILES string, and 0.0 otherwise."""
mol = Chem.MolFromSmiles(smile)
return fun(mol, **kwargs) if smile != '' and mol is not None and mol.GetNumAtoms() > 1 else 0.0
def filter_smiles(smiles):
"""Filters out valid SMILES string from a list."""
return [smile for smile in smiles if verify_sequence(smile)]
def build_vocab(smiles, pad_char='_', start_char='^'):
"""Builds the vocabulary dictionaries.
Arguments
-----------
- smiles. List of SMILES.
- pad_char. Char used for padding. '_' by default.
- start_char. First char of every generated string.
'^' by default.
Returns
-----------
- char_dict. Dictionary which maps a given character
to a number
- ord_dict. Dictionary which maps a given number to a
character.
"""
i = 1
char_dict, ord_dict = {start_char: 0}, {0: start_char}
for smile in smiles:
for c in smile:
if c not in char_dict:
char_dict[c] = i
ord_dict[i] = c
i += 1
char_dict[pad_char], ord_dict[i] = i, pad_char
return char_dict, ord_dict
def pad(smile, n, pad_char='_'):
"""Adds the padding char (by default '_') to a string
until it is of n length"""
if n < len(smile):
return smile
return smile + pad_char * (n - len(smile))
def unpad(smile, pad_char='_'):
"""Removes the padding of a string"""
return smile.rstrip(pad_char)
def encode(smile, max_len, char_dict):
"""Encodes a SMILES string using the previously built
vocabulary."""
return [char_dict[c] for c in pad(smile, max_len)]
def decode(ords, ord_dict):
"""Decodes a SMILES string using the previously built
vocabulary."""
return unpad(''.join([ord_dict[o] for o in ords]))
def compute_results(model_samples, train_data, ord_dict, results={}, verbose=True):
samples = [decode(s, ord_dict) for s in model_samples]
results['mean_length'] = np.mean([len(sample) for sample in samples])
results['n_samples'] = len(samples)
results['uniq_samples'] = len(set(samples))
verified_samples = []
unverified_samples = []
for sample in samples:
if verify_sequence(sample):
verified_samples.append(sample)
else:
unverified_samples.append(sample)
results['good_samples'] = len(verified_samples)
results['bad_samples'] = len(unverified_samples)
# save smiles
if 'Batch' in results.keys():
smi_name = '{}_{}'.format(results['exp_name'], results['Batch'])
save_smi(smi_name, samples)
results['model_samples'] = smi_name
if verbose:
print_results(verified_samples, unverified_samples, results)
return
def print_results(verified_samples, unverified_samples, results={}):
print('Summary of the epoch')
print('~~~~~~~~~~~~~~~~~~~~~~~~\n')
print('{:15s} : {:6d}'.format("Total samples", results['n_samples']))
percent = results['uniq_samples'] / float(results['n_samples']) * 100
print('{:15s} : {:6d} ({:2.2f}%)'.format(
'Unique', results['uniq_samples'], percent))
percent = results['bad_samples'] / float(results['n_samples']) * 100
print('{:15s} : {:6d} ({:2.2f}%)'.format('Unverified',
results['bad_samples'], percent))
percent = results['good_samples'] / float(results['n_samples']) * 100
print('{:15s} : {:6d} ({:2.2f}%)'.format(
'Verified', results['good_samples'], percent))
print('\nSome good samples:')
print('~~~~~~~~~~~~~~~~~~~~~~~~\n')
if len(verified_samples) > 10:
for s in verified_samples[0:10]:
print('' + s)
else:
print('No good samples were found :(...')
print('\nSome bad samples:')
print('~~~~~~~~~~~~~~~~~~~~~~~~\n')
if len(unverified_samples) > 10:
for s in unverified_samples[0:10]:
print('' + s)
else:
print('No bad samples were found :D!')
return