-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
410 lines (354 loc) · 12.8 KB
/
dataset.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import os
import re
from typing import List, Union
import numpy as np
import pandas as pd
import torch
from rdkit import RDLogger
from rdkit.Chem import AddHs, MolFromSmiles, MolToSmiles
from rdkit.Chem.Descriptors import descList
from rdkit.rdBase import DisableLog
from torch_geometric.data import Data, Dataset
from utils import atom_features, bond_features
for level in RDLogger._levels:
DisableLog(level)
class OneMol(Dataset):
def __init__(self, smiles, maxlen):
super(OneMol, self).__init__()
self.i2t, self.t2i = tokenizer()
self.max_len = maxlen
self.smiles = smiles
self.mol = MolFromSmiles(smiles)
if self.mol is None:
raise ValueError(f"Invalid SMILES: {smiles}")
def __getitem__(self, idx):
smils = self.smiles
num_nodes, atom_feats, bond_feats, edge_index = attentive_fp_features(self.mol)
smils_pad = np.full(self.max_len + 2, self.t2i[" "], dtype="uint8")
smils_pad[: len(smils) + 2] = [self.t2i["^"]] + [self.t2i[c] for c in smils] + [self.t2i["$"]]
return Data(
atoms=torch.FloatTensor(atom_feats),
bonds=torch.FloatTensor(bond_feats),
edge_index=torch.LongTensor(edge_index),
trg_smi=torch.LongTensor(smils_pad.reshape(1, -1)),
num_nodes=num_nodes,
)
def __len__(self):
return 1
def len(self) -> int:
return self.__len__()
def get(self, idx: int) -> Data:
return self.__getitem__(idx)
class AttFPDataset(Dataset):
"""Dataset class for SMILES and on-the-fly calculated properties"""
def __init__(
self,
filename,
delimiter="\t",
smls_col="SMILES",
props=None,
scaled_props=True,
random=False,
embed=False,
steps=128000,
):
super(AttFPDataset, self).__init__()
# tokenizer
self.i2t, self.t2i = tokenizer()
self.random = random
self.embed = embed
self.smls_col = smls_col
self.scaled_props = scaled_props
self.scaler = PropertyScaler(props, do_scale=scaled_props)
self.n_props = len(self.scaler.descriptors)
# Load smiles dataset
self.data = load_from_fname(filename, smls_col, delimiter)
self.max_len = self.data[smls_col].apply(lambda x: len(x)).max()
self.min_len = self.data[smls_col].apply(lambda x: len(x)).max()
if isinstance(filename, str):
print(f"Loaded {len(self.data)} SMILES")
print("Max Length: ", self.max_len)
print("Min Length: ", self.min_len)
# if random, set loops
if random:
self.loop = list(range(0, steps))
def __getitem__(self, idx):
mol = None
if self.random: # randomly sample any molecule
idx = np.random.randint(len(self.data))
while mol is None: # escape invalid molecules
mol = MolFromSmiles(self.data.iloc[idx][self.smls_col])
idx = np.random.randint(len(self.data))
num_nodes, atom_feats, bond_feats, edge_index = attentive_fp_features(mol)
if self.embed: # if used for embedding, no need to calculate properties and random SMILES
return Data(
atoms=torch.FloatTensor(atom_feats),
bonds=torch.FloatTensor(bond_feats),
edge_index=torch.LongTensor(edge_index),
num_nodes=num_nodes,
)
props = self.scaler.transform(mol) # get scaled properties between 0 and 1
mask = np.isfinite(props).astype(float) # to exclude potential nan / inf values
props = np.nan_to_num(props, nan=0.0, posinf=1.0, neginf=0.0)
try:
smils = MolToSmiles(mol, doRandom=True)
except Exception:
smils = self.data.iloc[idx][self.smls_col]
if len(smils) > self.max_len:
smils = self.data.iloc[idx][[self.smls_col]]
smils_pad = np.full(self.max_len + 2, self.t2i[" "], dtype="uint8")
smils_pad[: len(smils) + 2] = (
[self.t2i["^"]] + [self.t2i[c] if c in self.t2i else self.t2i["*"] for c in smils] + [self.t2i["$"]]
)
return Data(
atoms=torch.FloatTensor(atom_feats),
bonds=torch.FloatTensor(bond_feats),
edge_index=torch.LongTensor(edge_index),
trg_smi=torch.LongTensor(smils_pad.reshape(1, -1)),
props=torch.FloatTensor(props),
prop_mask=torch.FloatTensor(mask),
num_nodes=num_nodes,
)
def __len__(self):
if self.random:
return len(self.loop)
return len(self.data)
def len(self) -> int:
return self.__len__()
def get(self, idx: int) -> Data:
return self.__getitem__(idx)
class AttFPTableDataset(Dataset):
"""Dataset class for SMILES and properties in a table format"""
def __init__(
self,
filename,
delimiter="\t",
smls_col="SMILES",
props=None,
random=False,
scaled_props=False,
steps=128000,
):
super(AttFPTableDataset, self).__init__()
# tokenizer
self.i2t, self.t2i = tokenizer()
self.random = random
self.smls_col = smls_col
_ = scaled_props
# Load tabular dataset
self.data = load_from_fname(filename, smls_col, delimiter)
self.max_len = self.data[smls_col].apply(lambda x: len(x)).max()
self.props = props if props else [c for c in self.data.columns if c != smls_col]
self.data[self.props] = self.data[self.props].astype(float)
self.n_props = len(self.props)
if isinstance(filename, str):
print(f"Loaded {len(self.data)} SMILES with {self.n_props} properties")
print("Max Length: ", self.max_len)
# if random, set loops
if random:
self.loop = list(range(0, steps))
else:
self.loop = list(range(0, len(self.data)))
def __getitem__(self, idx):
mol = None
if self.random: # randomly sample any molecule
idx = np.random.randint(len(self.data))
while mol is None: # escape invalid molecules
mol = MolFromSmiles(self.data.iloc[idx][self.smls_col])
idx = np.random.randint(len(self.data))
num_nodes, atom_feats, bond_feats, edge_index = attentive_fp_features(mol)
props = np.array(self.data.iloc[idx][self.props].values, dtype=float)
mask = np.isfinite(props).astype(float) # to exclude potential nan / inf values
props = np.nan_to_num(props, nan=0.0, posinf=1.0, neginf=0.0)
try:
smils = MolToSmiles(mol, doRandom=True)
except Exception:
smils = self.data.iloc[idx][self.smls_col]
if len(smils) > self.max_len:
smils = self.data.iloc[idx][self.smls_col]
smils_pad = np.full(self.max_len + 2, self.t2i[" "], dtype="uint8")
smils_pad[: len(smils) + 2] = (
[self.t2i["^"]] + [self.t2i[c] if c in self.t2i else self.t2i["*"] for c in smils] + [self.t2i["$"]]
)
return Data(
atoms=torch.FloatTensor(atom_feats),
bonds=torch.FloatTensor(bond_feats),
edge_index=torch.LongTensor(edge_index),
trg_smi=torch.LongTensor(smils_pad.reshape(1, -1)),
props=torch.FloatTensor(props),
prop_mask=torch.FloatTensor(mask),
num_nodes=num_nodes,
)
def __len__(self):
if self.random:
return len(self.loop)
return len(self.data)
def len(self) -> int:
return self.__len__()
def get(self, idx: int) -> Data:
return self.__getitem__(idx)
def load_from_fname(filename, smls_col, delimiter):
# Load smiles dataset
if isinstance(filename, str):
if filename.endswith(".gz"):
data = pd.read_csv(filename, delimiter=delimiter, compression="gzip")
else:
data = pd.read_csv(filename, delimiter=delimiter)
data.dropna(how="all", axis=1, inplace=True)
if smls_col not in data.columns and len(data.columns) == 1:
data = pd.concat(
(
pd.DataFrame({smls_col: data.columns.tolist()}),
data.rename(columns={data.columns[0]: smls_col}),
)
)
elif isinstance(filename, list) or isinstance(filename, np.ndarray):
data = pd.DataFrame({smls_col: filename})
elif isinstance(filename, pd.Series):
data = filename.to_frame()
data.columns = [smls_col]
elif isinstance(filename, pd.DataFrame):
data = filename.copy()
else:
raise NotImplementedError(
f"Can only understand str, list/array, DataFrame or Series as filename! {type(filename)} provided"
)
return data
def attentive_fp_features(mol):
mol = AddHs(mol)
# node and edge features
atom_feats = np.array([atom_features(a) for a in mol.GetAtoms()])
bond_feats = np.array([bond_features(a) for a in mol.GetBonds()] * 2)
# edge indices (in the form that pyg Data edge_index needs it)
edge_indices = []
for bond in mol.GetBonds():
edge_indices += [[bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()]]
edge_indices += [[bond.GetEndAtomIdx(), bond.GetBeginAtomIdx()]]
return mol.GetNumAtoms(), atom_feats, bond_feats, np.array(edge_indices).T
class PropertyScaler(object):
def __init__(self, descriptors: Union[List, str, None] = None, do_scale: bool = True):
if isinstance(descriptors, str):
self.descriptors = {}
desc_regex = re.compile(descriptors)
for descriptor, func in descList:
if desc_regex.match(descriptor):
self.descriptors[descriptor] = func
elif isinstance(descriptors, list):
self.descriptors = {descriptor: func for descriptor, func in descList if descriptor in descriptors}
else:
self.descriptors = {descriptor: func for descriptor, func in descList}
self.load_min_max_values()
self.do_scale = do_scale
def load_min_max_values(self):
dirname = os.path.dirname(os.path.abspath(__file__))
d = json.loads(open(os.path.join(dirname, "data/property_scales.json")).read())
d = {k: v for k, v in d.items() if k in self.descriptors.keys()}
self.min_val = {k: v[0] for k, v in d.items()}
self.max_val = {k: v[1] for k, v in d.items()}
def _calc(self, mol, missing_val=0):
rslt = {}
if mol is not None:
for descriptor, func in self.descriptors.items():
rslt[descriptor] = list()
try:
val = func(mol)
except Exception:
val = missing_val
rslt[descriptor] = val
else:
rslt = {descriptor: missing_val for descriptor in self.descriptors}
return rslt
def scale(self, x, n):
try:
return (min(x, self.max_val[n]) - min(x, self.min_val[n])) / (self.max_val[n] - self.min_val[n])
except KeyError:
raise KeyError(x, n, self.min_val, self.max_val, self.descriptors)
def transform(self, mol):
props = self._calc(mol)
if self.do_scale:
return [self.scale(x, n) for n, x in props.items()]
else:
return [v for v in props.values()]
def tokenizer():
"""Function to generate all possibly relevant SMILES token and put them into two translation dictionaries"""
indices_token = {
0: " ",
1: "#",
2: "%",
3: "(",
4: ")",
5: "*",
6: "+",
7: "-",
8: ".",
9: "/",
10: "0",
11: "1",
12: "2",
13: "3",
14: "4",
15: "5",
16: "6",
17: "7",
18: "8",
19: "9",
20: ":",
21: "=",
22: "@",
23: "A",
24: "B",
25: "C",
26: "D",
27: "E",
28: "F",
29: "G",
30: "H",
31: "I",
32: "K",
33: "L",
34: "M",
35: "N",
36: "O",
37: "P",
38: "R",
39: "S",
40: "T",
41: "U",
42: "V",
43: "W",
44: "X",
45: "Y",
46: "Z",
47: "[",
48: "\\",
49: "]",
50: "a",
51: "b",
52: "c",
53: "d",
54: "e",
55: "f",
56: "g",
57: "h",
58: "i",
59: "k",
60: "l",
61: "m",
62: "n",
63: "o",
64: "p",
65: "r",
66: "s",
67: "t",
68: "u",
69: "y",
70: "{",
71: "}",
72: "^",
73: "$",
}
token_indices = {v: k for k, v in indices_token.items()}
return indices_token, token_indices