-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpreprocess.py
268 lines (239 loc) · 10.4 KB
/
preprocess.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
from collections import defaultdict
import numpy as np
from rdkit import Chem
import torch
import pickle
atom_dict = defaultdict(lambda: len(atom_dict))
bond_dict = defaultdict(lambda: len(bond_dict))
fingerprint_dict = defaultdict(lambda: len(fingerprint_dict))
edge_dict = defaultdict(lambda: len(edge_dict))
radius=1
def dump_dictionary(dictionary, filename):
with open(filename, 'wb') as f:
pickle.dump(dict(dictionary), f)
if torch.cuda.is_available():
device = torch.device('cuda')
print('The code uses a GPU!')
else:
device = torch.device('cpu')
print('The code uses a CPU...')
def create_atoms(mol, atom_dict):
"""Transform the atom types in a molecule (e.g., H, C, and O)
into the indices (e.g., H=0, C=1, and O=2).
Note that each atom index considers the aromaticity.
"""
atoms = [a.GetSymbol() for a in mol.GetAtoms()]
for a in mol.GetAromaticAtoms():
i = a.GetIdx()
atoms[i] = (atoms[i], 'aromatic')
atoms = [atom_dict[a] for a in atoms]
return np.array(atoms)
def create_ijbonddict(mol, bond_dict):
"""Create a dictionary, in which each key is a node ID
and each value is the tuples of its neighboring node
and chemical bond (e.g., single and double) IDs.
"""
i_jbond_dict = defaultdict(lambda: [])
for b in mol.GetBonds():
i, j = b.GetBeginAtomIdx(), b.GetEndAtomIdx()
bond = bond_dict[str(b.GetBondType())]
i_jbond_dict[i].append((j, bond))
i_jbond_dict[j].append((i, bond))
return i_jbond_dict
def extract_fingerprints(radius, atoms, i_jbond_dict,
fingerprint_dict, edge_dict):
"""Extract the fingerprints from a molecular graph
based on Weisfeiler-Lehman algorithm.
"""
if (len(atoms) == 1) or (radius == 0):
nodes = [fingerprint_dict[a] for a in atoms]
else:
nodes = atoms
i_jedge_dict = i_jbond_dict
for _ in range(radius):
"""Update each node ID considering its neighboring nodes and edges.
The updated node IDs are the fingerprint IDs.
"""
nodes_ = []
for i, j_edge in i_jedge_dict.items():
neighbors = [(nodes[j], edge) for j, edge in j_edge]
fingerprint = (nodes[i], tuple(sorted(neighbors)))
nodes_.append(fingerprint_dict[fingerprint])
"""Also update each edge ID considering
its two nodes on both sides.
"""
i_jedge_dict_ = defaultdict(lambda: [])
for i, j_edge in i_jedge_dict.items():
for j, edge in j_edge:
both_side = tuple(sorted((nodes[i], nodes[j])))
edge = edge_dict[(both_side, edge)]
i_jedge_dict_[i].append((j, edge))
nodes = nodes_
i_jedge_dict = i_jedge_dict_
return np.array(nodes)
def create_dataset(filename,path,dataname):
dir_dataset = path
print(filename)
"""Load a dataset."""
with open(dir_dataset + filename, 'r') as f:
smiles_property = f.readline().strip().split()
data_original = f.read().strip().split('\n')
"""Exclude the data contains '.' in its smiles."""
data_original = [data for data in data_original
if '.' not in data.split()[0]]
dataset = []
for data in data_original:
smiles, property = data.strip().split()
"""Create each data with the above defined functions."""
mol = Chem.AddHs(Chem.MolFromSmiles(smiles))
atoms = create_atoms(mol, atom_dict)
molecular_size = len(atoms)
i_jbond_dict = create_ijbonddict(mol, bond_dict)
fingerprints = extract_fingerprints(radius, atoms, i_jbond_dict,
fingerprint_dict, edge_dict)
adjacency = np.float32((Chem.GetAdjacencyMatrix(mol)))
#Transform the above each data of numpy to pytorch tensor on a device (i.e., CPU or GPU).
fingerprints = torch.LongTensor(fingerprints).to(device)
adjacency = torch.FloatTensor(adjacency).to(device)
property = torch.FloatTensor([[float(property)]]).to(device)
dataset.append((smiles,fingerprints, adjacency, molecular_size, property))
dir_dataset=path
dump_dictionary(fingerprint_dict, dir_dataset +dataname+ '-fingerprint_dict.pickle')
dump_dictionary(atom_dict, dir_dataset +dataname+ '-atom_dict.pickle')
dump_dictionary(bond_dict, dir_dataset +dataname+ '-bond_dict.pickle')
dump_dictionary(edge_dict, dir_dataset +dataname+ '-edge_dict.pickle')
return dataset
def create_dataset_randomsplit(x,y,path,dataname):
dir_input = path + 'SMRT-'
with open(dir_input + 'atom_dict.pickle', 'rb') as f:
c=pickle.load(f)
for k in c.keys():
atom_dict.get(k)
atom_dict[k]=c[k]
with open(dir_input+ 'bond_dict.pickle', 'rb') as f:
c=pickle.load(f)
for k in c.keys():
bond_dict.get(k)
bond_dict[k]=c[k]
with open(dir_input + 'edge_dict.pickle', 'rb') as f:
c=pickle.load(f)
for k in c.keys():
edge_dict.get(k)
edge_dict[k]=c[k]
with open(dir_input + 'fingerprint_dict.pickle', 'rb') as f:
c=pickle.load(f)
for k in c.keys():
fingerprint_dict.get(k)
fingerprint_dict[k]=c[k]
dataset = []
for i in range(len(x)):
smiles=x[i]
property=y[i]
"""Create each data with the above defined functions."""
mol = Chem.MolFromInchi(smiles)
mol = Chem.AddHs(Chem.MolFromInchi(smiles))
atoms = create_atoms(mol, atom_dict)
molecular_size = len(atoms)
i_jbond_dict = create_ijbonddict(mol, bond_dict)
fingerprints = extract_fingerprints(radius, atoms, i_jbond_dict,
fingerprint_dict, edge_dict)
adjacency = np.float32((Chem.GetAdjacencyMatrix(mol)))
#Transform the above each data of numpy to pytorch tensor on a device (i.e., CPU or GPU).
fingerprints = torch.LongTensor(fingerprints).to(device)
adjacency = torch.FloatTensor(adjacency).to(device)
property = torch.FloatTensor([[float(property)]]).to(device)
dataset.append((smiles,fingerprints, adjacency, molecular_size, property))
dir_dataset=path
dump_dictionary(fingerprint_dict, dir_dataset +dataname+ '-fingerprint_dict.pickle')
dump_dictionary(atom_dict, dir_dataset +dataname+ '-atom_dict.pickle')
dump_dictionary(bond_dict, dir_dataset +dataname+ '-bond_dict.pickle')
dump_dictionary(edge_dict, dir_dataset +dataname+ '-edge_dict.pickle')
return dataset
def create_dataset_kfold(x,y,path,dataname):
dir_input =path+'SMRT-'
with open(dir_input + 'atom_dict.pickle', 'rb') as f:
c=pickle.load(f)
for k in c.keys():
atom_dict.get(k)
atom_dict[k]=c[k]
with open(dir_input+ 'bond_dict.pickle', 'rb') as f:
c=pickle.load(f)
for k in c.keys():
bond_dict.get(k)
bond_dict[k]=c[k]
with open(dir_input + 'edge_dict.pickle', 'rb') as f:
c=pickle.load(f)
for k in c.keys():
edge_dict.get(k)
edge_dict[k]=c[k]
with open(dir_input + 'fingerprint_dict.pickle', 'rb') as f:
c=pickle.load(f)
for k in c.keys():
fingerprint_dict.get(k)
fingerprint_dict[k]=c[k]
dataset = []
for i in range(len(x)):
smiles=x[i]
property=y[i]
"""Create each data with the above defined functions."""
mol = Chem.AddHs(Chem.MolFromSmiles(smiles))
atoms = create_atoms(mol, atom_dict)
molecular_size = len(atoms)
i_jbond_dict = create_ijbonddict(mol, bond_dict)
fingerprints = extract_fingerprints(radius, atoms, i_jbond_dict,
fingerprint_dict, edge_dict)
adjacency = np.float32((Chem.GetAdjacencyMatrix(mol)))
#Transform the above each data of numpy to pytorch tensor on a device (i.e., CPU or GPU).
fingerprints = torch.LongTensor(fingerprints).to(device)
adjacency = torch.FloatTensor(adjacency).to(device)
property = torch.FloatTensor([[float(property)]]).to(device)
dataset.append((smiles,fingerprints, adjacency, molecular_size, property))
dir_dataset=path
dump_dictionary(fingerprint_dict, dir_dataset +dataname+ '-fingerprint_dict.pickle')
dump_dictionary(atom_dict, dir_dataset +dataname+ '-atom_dict.pickle')
dump_dictionary(bond_dict, dir_dataset +dataname+ '-bond_dict.pickle')
dump_dictionary(edge_dict, dir_dataset +dataname+ '-edge_dict.pickle')
return dataset
def transferlearning_dataset_predict(x,path):
dir_input = path+'SMRT-'
with open(dir_input + 'atom_dict.pickle', 'rb') as f:
c=pickle.load(f)
for k in c.keys():
atom_dict.get(k)
atom_dict[k]=c[k]
with open(dir_input+ 'bond_dict.pickle', 'rb') as f:
c=pickle.load(f)
for k in c.keys():
bond_dict.get(k)
bond_dict[k]=c[k]
with open(dir_input + 'edge_dict.pickle', 'rb') as f:
c=pickle.load(f)
for k in c.keys():
edge_dict.get(k)
edge_dict[k]=c[k]
with open(dir_input + 'fingerprint_dict.pickle', 'rb') as f:
c=pickle.load(f)
for k in c.keys():
fingerprint_dict.get(k)
fingerprint_dict[k]=c[k]
dataset = []
for i in range(len(x)):
smiles=x[i]
"""Create each data with the above defined functions."""
mol = Chem.MolFromSmiles(smiles)
if mol is None:
continue
else:
smi = Chem.MolToSmiles(mol)
mol = Chem.AddHs(Chem.MolFromSmiles(smiles))
atoms = create_atoms(mol, atom_dict)
molecular_size = len(atoms)
i_jbond_dict = create_ijbonddict(mol, bond_dict)
fingerprints = extract_fingerprints(radius, atoms, i_jbond_dict,
fingerprint_dict, edge_dict)
adjacency = np.float32((Chem.GetAdjacencyMatrix(mol)))
#Transform the above each data of numpy to pytorch tensor on a device (i.e., CPU or GPU).
fingerprints = torch.LongTensor(fingerprints).to(device)
adjacency = torch.FloatTensor(adjacency).to(device)
dataset.append((smiles,fingerprints, adjacency, molecular_size))
return dataset