-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
132 lines (110 loc) · 4.17 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
import torch
from torch.utils.data import Dataset
from transformers import BertTokenizer
import networkx as nx
from rdkit import Chem
# Assume SmilesTokenizer is implemented or imported as mentioned in previous responses
class MoleculeDataset(Dataset):
def __init__(self, data):
self.descriptions, self.selfies, self.smiles = zip(*data)
def __len__(self):
return len(self.descriptions)
def __getitem__(self, index):
return {
"description": self.descriptions[index],
"selfies": self.selfies[index],
"smiles": self.smiles[index]
}
class DrugTargetDataset(Dataset):
def __init__(self, data):
self.seq, self.selfies, self.smiles = zip(*data)
def __len__(self):
return len(self.seq)
def __getitem__(self, index):
text = self.seq[index]
smiles = self.smiles[index]
selfies = self.selfies[index]
return {
"description": text,
"smiles": smiles,
"selfies": selfies
}
import h5py
class PocketMolDataset(Dataset):
def __init__(self, file):
hdf = h5py.File(file, 'r')
self.array_group = hdf['arrays']
self.string_group = hdf['strings']
def __len__(self):
return len(self.array_group)
def __getitem__(self, index):
return {
"smiles": self.string_group[f'strings_{index}'][...][0],
"selfies": self.string_group[f'strings_{index}'][...][1],
"pocket_seq": self.string_group[f'strings_{index}'][...][2],
"pocket_coord": self.array_group[f'array_{index}'][...],
}
import numpy as np
class PocketMolDataLoader():
def __init__(self, dataset, batch_tokens=1000, shuffle=True, collate_fn=lambda x:x, drop_last=False):
self.dataset = dataset
self.size = len(dataset)
self.lengths = [len(dataset.string_group[f'strings_{i}'][...][2]) for i in range(self.size)]
self.batch_tokens = batch_tokens
sorted_ix = np.argsort(self.lengths)
clusters, batch = [], []
batch_max = 0
for ix in sorted_ix:
size = self.lengths[ix]
if size * (len(batch) + 1) <= self.batch_tokens:
batch.append(ix)
batch_max = size
else:
clusters.append(batch)
batch, batch_max = [], 0
if len(batch) > 0:
clusters.append(batch)
self.clusters = clusters
if drop_last == True:
if len(self.clusters[-1]) == 1:
del self.clusters[-1]
def __len(self):
return len(self.clusters)
def __iter__(self):
np.random.shuffle(self.clusters)
for b_idx in self.clusters:
batch = [self.dataset[i] for i in b_idx]
yield batch
class MoleculeGraphDataset(Dataset):
def __init__(self, data):
# Data is a list of tuples containing descriptions and SMILES strings
self.descriptions, self.smiles = zip(*data)
self.molecule_graph = []
for smile in self.smiles:
mol = Chem.MolFromSmiles(smile)
# Initialize an empty graph
G = nx.Graph()
# Add atoms to graph
for atom in mol.GetAtoms():
G.add_node(atom.GetIdx(),
atomic_num=atom.GetAtomicNum(),
formal_charge=atom.GetFormalCharge(),
chiral_tag=atom.GetChiralTag(),
hybridization=atom.GetHybridization(),
num_explicit_hs=atom.GetNumExplicitHs(),
is_aromatic=atom.GetIsAromatic())
# Add bonds to graph
for bond in mol.GetBonds():
G.add_edge(bond.GetBeginAtomIdx(),
bond.GetEndAtomIdx(),
bond_type=bond.GetBondType())
self.molecule_graph.append(G)
def __len__(self):
return len(self.descriptions)
def __getitem__(self, index):
text = self.descriptions[index]
molecule = self.molecule_graph[index]
return {
"description": text,
"molecule": molecule
}