-
Notifications
You must be signed in to change notification settings - Fork 26
/
molecule3d.py
439 lines (363 loc) · 18 KB
/
molecule3d.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import os, json, ast, glob, ssl
import torch
import os.path as osp
import numpy as np
import pandas as pd
from tqdm import tqdm
from rdkit import Chem
from itertools import repeat
from six.moves import urllib
from torch_geometric.data import Data, InMemoryDataset, download_url
from .utils import mol2graph
class Molecule3D(InMemoryDataset):
"""
A `Pytorch Geometric <https://pytorch-geometric.readthedocs.io/en/latest/index.html>`_ data interface for
datasets used in molecule generation.
.. note::
Some datasets may not come with any node labels, like :obj:`moses`.
Since they don't have any properties in the original data file. The process of the
dataset can only save the current input property and will load the same property
label when the processed dataset is used. You can change the augment :obj:`processed_filename`
to re-process the dataset with intended property.
Args:
root (string, optional): Root directory where the dataset should be saved. (default: :obj:`./`)
split (string, optional): If :obj:`"train"`, loads the training dataset.
If :obj:`"val"`, loads the validation dataset.
If :obj:`"test"`, loads the test dataset. (default: :obj:`"train"`)
split_mode (string, optional): Mode of split chosen from :obj:`"random"` and :obj:`"scaffold"`.
(default: :obj:`penalized_logp`)
transform (callable, optional): A function/transform that takes in an
:obj:`torch_geometric.data.Data` object and returns a transformed
version. The data object will be transformed before every access.
(default: :obj:`None`)
pre_transform (callable, optional): A function/transform that takes in
an :obj:`torch_geometric.data.Data` object and returns a
transformed version. The data object will be transformed before
being saved to disk. (default: :obj:`None`)
pre_filter (callable, optional): A function that takes in an
:obj:`torch_geometric.data.Data` object and returns a boolean
value, indicating whether the data object should be included in the
final dataset. (default: :obj:`None`)
"""
def __init__(self,
root,
split='train',
split_mode='random',
transform=None,
pre_transform=None,
pre_filter=None,
):
assert split in ['train', 'val', 'test']
assert split_mode in ['random', 'scaffold']
self.split_mode = split_mode
self.root = root
self.name = 'data'
self.target_df = pd.read_csv(osp.join(self.raw_dir, 'properties.csv'))
# if not osp.exists(self.raw_paths[0]):
# self.download()
super(Molecule3D, self).__init__(root, transform, pre_transform, pre_filter)
self.data, self.slices = torch.load(
osp.join(self.processed_dir, '{}_{}.pt'.format(split_mode, split)))
@property
def num_node_labels(self):
if self.data.x is None:
return 0
for i in range(self.data.x.size(1)):
x = self.data.x[:, i:]
if ((x == 0) | (x == 1)).all() and (x.sum(dim=1) == 1).all():
return self.data.x.size(1) - i
return 0
@property
def num_node_attributes(self):
if self.data.x is None:
return 0
return self.data.x.size(1) - self.num_node_labels
@property
def num_edge_labels(self):
if self.data.edge_attr is None:
return 0
for i in range(self.data.edge_attr.size(1)):
if self.data.edge_attr[:, i:].sum() == self.data.edge_attr.size(0):
return self.data.edge_attr.size(1) - i
return 0
@property
def raw_dir(self):
return osp.join(self.root, self.name, 'raw')
@property
def processed_dir(self):
return osp.join(self.root, self.name, 'processed')
@property
def raw_file_names(self):
name = self.name + '.csv'
return name
@property
def processed_file_names(self):
return ['random_train.pt', 'random_val.pt', 'random_test.pt',
'scaffold_train.pt', 'scaffold_val.pt', 'scaffold_test.pt']
def download(self):
# print('making raw files:', self.raw_dir)
# if not osp.exists(self.raw_dir):
# os.makedirs(self.raw_dir)
# url = self.url
# path = download_url(url, self.raw_dir)
pass
def pre_process(self):
data_list = []
sdf_paths = [osp.join(self.raw_dir, 'combined_mols_0_to_1000000.sdf'),
osp.join(self.raw_dir, 'combined_mols_1000000_to_2000000.sdf'),
osp.join(self.raw_dir, 'combined_mols_2000000_to_3000000.sdf'),
osp.join(self.raw_dir, 'combined_mols_3000000_to_3899647.sdf')]
suppl_list = [Chem.SDMolSupplier(p, removeHs=False, sanitize=True) for p in sdf_paths]
abs_idx = -1
for i, suppl in enumerate(suppl_list):
for j in tqdm(range(len(suppl)), desc=f'{i+1}/{len(sdf_paths)}'):
abs_idx += 1
mol = suppl[j]
smiles = Chem.MolToSmiles(mol)
coords = mol.GetConformer().GetPositions()
z = [atom.GetAtomicNum() for atom in mol.GetAtoms()]
graph = mol2graph(mol)
data = Data()
data.__num_nodes__ = int(graph['num_nodes'])
data.smiles = smiles
data.edge_index = torch.from_numpy(graph['edge_index']).to(torch.int64)
data.edge_attr = torch.from_numpy(graph['edge_feat']).to(torch.int64)
data.x = torch.from_numpy(graph['node_feat']).to(torch.int64)
data.xyz = torch.tensor(coords, dtype=torch.float32)
data_list.append(data)
return data_list
def process(self):
r"""Processes the dataset from raw data file to the :obj:`self.processed_dir` folder.
If one-hot format is required, the processed data type will include an extra dimension
of virtual node and edge feature.
"""
full_list = self.pre_process()
print('making processed files:', self.processed_dir)
if not osp.exists(self.processed_dir):
os.makedirs(self.processed_dir)
for m, split_mode in enumerate(['random', 'scaffold']):
ind_path = osp.join(self.raw_dir, '{}_split_inds.json').format(split_mode)
with open(ind_path, 'r') as f:
inds = json.load(f)
for s, split in enumerate(['train', 'valid', 'test']):
data_list = [self.get_data_prop(full_list, idx, split) for idx in inds[split]]
if self.pre_filter is not None:
data_list = [data for data in data_list if self.pre_filter(data)]
if self.pre_transform is not None:
data_list = [self.pre_transform(data) for data in data_list]
torch.save(self.collate(data_list), self.processed_paths[s+3*m])
def get_data_prop(self, full_list, abs_idx, split):
data = full_list[abs_idx]
if split == 'test':
data.props = torch.FloatTensor(self.target_df.iloc[abs_idx,1:].values)
return data
def __repr__(self):
return '{}({})'.format(self.name, len(self))
def get(self, idx):
r"""Gets the data object at index :idx:.
Args:
idx: The index of the data that you want to reach.
:rtype: A data object corresponding to the input index :obj:`idx` .
"""
data = self.data.__class__()
if hasattr(self.data, '__num_nodes__'):
data.num_nodes = self.data.__num_nodes__[idx]
for key in self.data.keys:
item, slices = self.data[key], self.slices[key]
if torch.is_tensor(item):
s = list(repeat(slice(None), item.dim()))
s[self.data.__cat_dim__(key, item)] = slice(slices[idx], slices[idx + 1])
else:
s = slice(slices[idx], slices[idx + 1])
data[key] = item[s]
return data
class Molecule3DProps(InMemoryDataset):
"""
A `Pytorch Geometric <https://pytorch-geometric.readthedocs.io/en/latest/index.html>`_ data interface for
datasets used in molecule generation.
.. note::
Some datasets may not come with any node labels, like :obj:`moses`.
Since they don't have any properties in the original data file. The process of the
dataset can only save the current input property and will load the same property
label when the processed dataset is used. You can change the augment :obj:`processed_filename`
to re-process the dataset with intended property.
Args:
root (string, optional): Root directory where the dataset should be saved. (default: :obj:`./`)
split (string, optional): If :obj:`"train"`, loads the training dataset.
If :obj:`"val"`, loads the validation dataset.
If :obj:`"test"`, loads the test dataset. (default: :obj:`"train"`)
split_mode (string, optional): Mode of split chosen from :obj:`"random"` and :obj:`"scaffold"`.
(default: :obj:`penalized_logp`)
transform (callable, optional): A function/transform that takes in an
:obj:`torch_geometric.data.Data` object and returns a transformed
version. The data object will be transformed before every access.
(default: :obj:`None`)
pre_transform (callable, optional): A function/transform that takes in
an :obj:`torch_geometric.data.Data` object and returns a
transformed version. The data object will be transformed before
being saved to disk. (default: :obj:`None`)
pre_filter (callable, optional): A function that takes in an
:obj:`torch_geometric.data.Data` object and returns a boolean
value, indicating whether the data object should be included in the
final dataset. (default: :obj:`None`)
process_dir_base (string, optional): target directory to store your processed data. Should use
different dir when using different :obj:`pre_transform' functions.
test_pt_dir (string, optional): If you already called :obj:`Molecule3D' and have raw data
pre-processed, set :obj:`test_pt_dir` to the folder name where test data file is stored.
Usually stored in "processed".
"""
def __init__(self,
root,
split='train',
split_mode='random',
transform=None,
pre_transform=None,
pre_filter=None,
process_dir_base='processed_downstream',
test_pt_dir=None
):
assert split in ['train', 'val', 'test']
assert split_mode in ['random', 'scaffold']
self.split_mode = split_mode
self.root = root
self.name = 'data'
self.process_dir_base = process_dir_base
self.test_pt_dir = test_pt_dir
# if not osp.exists(self.raw_paths[0]):
# self.download()
super(Molecule3DProps, self).__init__(root, transform, pre_transform, pre_filter)
if split == 'train':
self.data, self.slices = torch.load(self.processed_paths[0])
elif split == 'val':
self.data, self.slices = torch.load(self.processed_paths[1])
elif split == 'test':
self.data, self.slices = torch.load(self.processed_paths[2])
@property
def num_node_labels(self):
if self.data.x is None:
return 0
for i in range(self.data.x.size(1)):
x = self.data.x[:, i:]
if ((x == 0) | (x == 1)).all() and (x.sum(dim=1) == 1).all():
return self.data.x.size(1) - i
return 0
@property
def num_node_attributes(self):
if self.data.x is None:
return 0
return self.data.x.size(1) - self.num_node_labels
@property
def num_edge_labels(self):
if self.data.edge_attr is None:
return 0
for i in range(self.data.edge_attr.size(1)):
if self.data.edge_attr[:, i:].sum() == self.data.edge_attr.size(0):
return self.data.edge_attr.size(1) - i
return 0
@property
def raw_dir(self):
return osp.join(self.root, self.name, 'raw')
@property
def processed_dir(self):
return osp.join(self.root, self.name,
'{}_{}'.format(self.process_dir_base, self.split_mode))
@property
def raw_file_names(self):
name = self.name + '.csv'
return name
@property
def processed_file_names(self):
return ['train.pt', 'val.pt', 'test.pt']
def download(self):
# print('making raw files:', self.raw_dir)
# if not osp.exists(self.raw_dir):
# os.makedirs(self.raw_dir)
# url = self.url
# path = download_url(url, self.raw_dir)
pass
def pre_process(self):
data_list = []
sdf_paths = [osp.join(self.raw_dir, 'combined_mols_0_to_1000000.sdf'),
osp.join(self.raw_dir, 'combined_mols_1000000_to_2000000.sdf'),
osp.join(self.raw_dir, 'combined_mols_2000000_to_3000000.sdf'),
osp.join(self.raw_dir, 'combined_mols_3000000_to_3899647.sdf')]
suppl_list = [Chem.SDMolSupplier(p, removeHs=False, sanitize=True) for p in sdf_paths]
ind_path = osp.join(self.raw_dir, '{}_split_inds.json').format(self.split_mode)
with open(ind_path, 'r') as f:
inds = json.load(f)
test_dict = dict.fromkeys(inds['test'])
target_path = osp.join(self.raw_dir, 'properties.csv')
target_df = pd.read_csv(target_path)
abs_idx = -1
for i, suppl in enumerate(suppl_list):
for j in tqdm(range(len(suppl)), desc=f'{i+1}/{len(sdf_paths)}'):
abs_idx += 1
try:
test_dict[abs_idx]
except:
continue
mol = suppl[j]
smiles = Chem.MolToSmiles(mol)
coords = mol.GetConformer().GetPositions()
z = [atom.GetAtomicNum() for atom in mol.GetAtoms()]
graph = mol2graph(mol)
data = Data()
data.__num_nodes__ = int(graph['num_nodes'])
# Required by GNNs
data.edge_index = torch.from_numpy(graph['edge_index']).to(torch.int64)
data.edge_attr = torch.from_numpy(graph['edge_feat']).to(torch.int64)
data.x = torch.from_numpy(graph['node_feat']).to(torch.int64)
data.props = torch.FloatTensor(target_df.iloc[abs_idx,1:].values)
data.smiles = smiles
# Required by Schnet
data.xyz = torch.tensor(coords, dtype=torch.float32)
data.z = torch.tensor(z, dtype=torch.int64)
data_list.append(data)
return self.collate(data_list)
def process(self):
r"""Processes the dataset from raw data file to the :obj:`self.processed_dir` folder.
If one-hot format is required, the processed data type will include an extra dimension
of virtual node and edge feature.
"""
if self.test_pt_dir is not None:
test_path = osp.join(self.root, self.name, self.test_pt_dir,
'{}_test.pt'.format(self.split_mode))
print('Loading pre-processed data from: {}...'.format(test_path))
self.data, self.slices = torch.load(test_path)
else:
self.data, self.slices = self.pre_process()
ind_path = osp.join(self.raw_dir, '{}_test_split_inds.json').format(self.split_mode)
with open(ind_path, 'r') as f:
inds = json.load(f)
print('making processed files:', self.processed_dir)
if not osp.exists(self.processed_dir):
os.makedirs(self.processed_dir)
for s, split in enumerate(['train', 'valid', 'test']):
data_list = [self.get(idx) for idx in inds[split]]
if self.pre_filter is not None:
data_list = [data for data in data_list if self.pre_filter(data)]
if self.pre_transform is not None:
data_list = [self.pre_transform(data) for data in
tqdm(data_list, desc="Pre-transform {}".format(split))]
data_list = list(filter(None, data_list))
torch.save(self.collate(data_list), self.processed_paths[s])
def __repr__(self):
return '{}({})'.format(self.name, len(self))
def get(self, idx):
r"""Gets the data object at index :idx:.
Args:
idx: The index of the data that you want to reach.
:rtype: A data object corresponding to the input index :obj:`idx` .
"""
data = self.data.__class__()
if hasattr(self.data, '__num_nodes__'):
data.num_nodes = self.data.__num_nodes__[idx]
for key in self.data.keys:
item, slices = self.data[key], self.slices[key]
if torch.is_tensor(item):
s = list(repeat(slice(None), item.dim()))
s[self.data.__cat_dim__(key, item)] = slice(slices[idx], slices[idx + 1])
else:
s = slice(slices[idx], slices[idx + 1])
data[key] = item[s]
return data