forked from hauldhut/GraphDRP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
550 lines (445 loc) · 16.7 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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import os
import csv
from pubchempy import *
import numpy as np
import numbers
import h5py
import math
import pandas as pd
import json,pickle
from collections import OrderedDict
from rdkit import Chem
from rdkit.Chem import MolFromSmiles
import networkx as nx
from utils import *
import random
import pickle
import sys
import matplotlib.pyplot as plt
import argparse
def is_not_float(string_list):
try:
for string in string_list:
float(string)
return False
except:
return True
"""
The following 4 function is used to preprocess the drug data. We download the drug list manually, and download the SMILES format using pubchempy. Since this part is time consuming, I write the cids and SMILES into a csv file.
"""
folder = "data/"
#folder = ""
def load_drug_list():
filename = folder + "Druglist.csv"
csvfile = open(filename, "rb")
reader = csv.reader(csvfile)
next(reader, None)
drugs = []
for line in reader:
drugs.append(line[0])
drugs = list(set(drugs))
return drugs
def write_drug_cid():
drugs = load_drug_list()
drug_id = []
datas = []
outputfile = open(folder + 'pychem_cid.csv', 'wb')
wr = csv.writer(outputfile)
unknow_drug = []
for drug in drugs:
c = get_compounds(drug, 'name')
if drug.isdigit():
cid = int(drug)
elif len(c) == 0:
unknow_drug.append(drug)
continue
else:
cid = c[0].cid
print(drug, cid)
drug_id.append(cid)
row = [drug, str(cid)]
wr.writerow(row)
outputfile.close()
outputfile = open(folder + "unknow_drug_by_pychem.csv", 'wb')
wr = csv.writer(outputfile)
wr.writerow(unknow_drug)
def cid_from_other_source():
"""
some drug can not be found in pychem, so I try to find some cid manually.
the small_molecule.csv is downloaded from http://lincs.hms.harvard.edu/db/sm/
"""
f = open(folder + "small_molecule.csv", 'r')
reader = csv.reader(f)
reader.next()
cid_dict = {}
for item in reader:
name = item[1]
cid = item[4]
if not name in cid_dict:
cid_dict[name] = str(cid)
unknow_drug = open(folder + "unknow_drug_by_pychem.csv").readline().split(",")
drug_cid_dict = {k:v for k,v in cid_dict.iteritems() if k in unknow_drug and not is_not_float([v])}
return drug_cid_dict
def load_cid_dict():
reader = csv.reader(open(folder + "pychem_cid.csv"))
pychem_dict = {}
for item in reader:
pychem_dict[item[0]] = item[1]
pychem_dict.update(cid_from_other_source())
return pychem_dict
def download_smiles():
cids_dict = load_cid_dict()
cids = [v for k,v in cids_dict.iteritems()]
inv_cids_dict = {v:k for k,v in cids_dict.iteritems()}
download('CSV', folder + 'drug_smiles.csv', cids, operation='property/CanonicalSMILES,IsomericSMILES', overwrite=True)
f = open(folder + 'drug_smiles.csv')
reader = csv.reader(f)
header = ['name'] + reader.next()
content = []
for line in reader:
content.append([inv_cids_dict[line[0]]] + line)
f.close()
f = open(folder + "drug_smiles.csv", "w")
writer = csv.writer(f)
writer.writerow(header)
for item in content:
writer.writerow(item)
f.close()
"""
The following code will convert the SMILES format into onehot format
"""
def atom_features(atom):
return np.array(one_of_k_encoding_unk(atom.GetSymbol(),['C', 'N', 'O', 'S', 'F', 'Si', 'P', 'Cl', 'Br', 'Mg', 'Na','Ca', 'Fe', 'As', 'Al', 'I', 'B', 'V', 'K', 'Tl', 'Yb','Sb', 'Sn', 'Ag', 'Pd', 'Co', 'Se', 'Ti', 'Zn', 'H','Li', 'Ge', 'Cu', 'Au', 'Ni', 'Cd', 'In', 'Mn', 'Zr','Cr', 'Pt', 'Hg', 'Pb', 'Unknown']) +
one_of_k_encoding(atom.GetDegree(), [0, 1, 2, 3, 4, 5, 6,7,8,9,10]) +
one_of_k_encoding_unk(atom.GetTotalNumHs(), [0, 1, 2, 3, 4, 5, 6,7,8,9,10]) +
one_of_k_encoding_unk(atom.GetImplicitValence(), [0, 1, 2, 3, 4, 5, 6,7,8,9,10]) +
[atom.GetIsAromatic()])
def one_of_k_encoding(x, allowable_set):
if x not in allowable_set:
raise Exception("input {0} not in allowable set{1}:".format(x, allowable_set))
return list(map(lambda s: x == s, allowable_set))
def one_of_k_encoding_unk(x, allowable_set):
"""Maps inputs not in the allowable set to the last element."""
if x not in allowable_set:
x = allowable_set[-1]
return list(map(lambda s: x == s, allowable_set))
def smile_to_graph(smile):
mol = Chem.MolFromSmiles(smile)
c_size = mol.GetNumAtoms()
features = []
for atom in mol.GetAtoms():
feature = atom_features(atom)
features.append( feature / sum(feature) )
edges = []
for bond in mol.GetBonds():
edges.append([bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()])
g = nx.Graph(edges).to_directed()
edge_index = []
for e1, e2 in g.edges:
edge_index.append([e1, e2])
return c_size, features, edge_index
def load_drug_smile():
reader = csv.reader(open(folder + "drug_smiles.csv"))
next(reader, None)
drug_dict = {}
drug_smile = []
for item in reader:
name = item[0]
smile = item[2]
if name in drug_dict:
pos = drug_dict[name]
else:
pos = len(drug_dict)
drug_dict[name] = pos
drug_smile.append(smile)
smile_graph = {}
for smile in drug_smile:
g = smile_to_graph(smile)
smile_graph[smile] = g
return drug_dict, drug_smile, smile_graph
def save_cell_mut_matrix():
f = open(folder + "PANCANCER_Genetic_feature.csv")
reader = csv.reader(f)
next(reader)
features = {}
cell_dict = {}
mut_dict = {}
matrix_list = []
for item in reader:
cell_id = item[1]
mut = item[5]
is_mutated = int(item[6])
if mut in mut_dict:
col = mut_dict[mut]
else:
col = len(mut_dict)
mut_dict[mut] = col
if cell_id in cell_dict:
row = cell_dict[cell_id]
else:
row = len(cell_dict)
cell_dict[cell_id] = row
if is_mutated == 1:
matrix_list.append((row, col))
cell_feature = np.zeros((len(cell_dict), len(mut_dict)))
for item in matrix_list:
cell_feature[item[0], item[1]] = 1
with open('mut_dict', 'wb') as fp:
pickle.dump(mut_dict, fp)
return cell_dict, cell_feature
"""
This part is used to extract the drug - cell interaction strength. it contains IC50, AUC, Max conc, RMSE, Z_score
"""
def save_mix_drug_cell_matrix():
f = open(folder + "PANCANCER_IC.csv")
reader = csv.reader(f)
next(reader)
cell_dict, cell_feature = save_cell_mut_matrix()
drug_dict, drug_smile, smile_graph = load_drug_smile()
temp_data = []
bExist = np.zeros((len(drug_dict), len(cell_dict)))
for item in reader:
drug = item[0]
cell = item[3]
ic50 = item[8]
ic50 = 1 / (1 + pow(math.exp(float(ic50)), -0.1))
temp_data.append((drug, cell, ic50))
xd = []
xc = []
y = []
lst_drug = []
lst_cell = []
random.shuffle(temp_data)
for data in temp_data:
drug, cell, ic50 = data
if drug in drug_dict and cell in cell_dict:
xd.append(drug_smile[drug_dict[drug]])
xc.append(cell_feature[cell_dict[cell]])
y.append(ic50)
bExist[drug_dict[drug], cell_dict[cell]] = 1
lst_drug.append(drug)
lst_cell.append(cell)
with open('drug_dict', 'wb') as fp:
pickle.dump(drug_dict, fp)
xd, xc, y = np.asarray(xd), np.asarray(xc), np.asarray(y)
size = int(xd.shape[0] * 0.8)
size1 = int(xd.shape[0] * 0.9)
with open('list_drug_mix_test', 'wb') as fp:
pickle.dump(lst_drug[size1:], fp)
with open('list_cell_mix_test', 'wb') as fp:
pickle.dump(lst_cell[size1:], fp)
xd_train = xd[:size]
xd_val = xd[size:size1]
xd_test = xd[size1:]
xc_train = xc[:size]
xc_val = xc[size:size1]
xc_test = xc[size1:]
y_train = y[:size]
y_val = y[size:size1]
y_test = y[size1:]
dataset = 'GDSC'
print('preparing ', dataset + '_train.pt in pytorch format!')
train_data = TestbedDataset(root='data', dataset=dataset+'_train_mix', xd=xd_train, xt=xc_train, y=y_train, smile_graph=smile_graph)
val_data = TestbedDataset(root='data', dataset=dataset+'_val_mix', xd=xd_val, xt=xc_val, y=y_val, smile_graph=smile_graph)
test_data = TestbedDataset(root='data', dataset=dataset+'_test_mix', xd=xd_test, xt=xc_test, y=y_test, smile_graph=smile_graph)
def save_blind_drug_matrix():
f = open(folder + "PANCANCER_IC.csv")
reader = csv.reader(f)
next(reader)
cell_dict, cell_feature = save_cell_mut_matrix()
drug_dict, drug_smile, smile_graph = load_drug_smile()
matrix_list = []
temp_data = []
xd_train = []
xc_train = []
y_train = []
xd_val = []
xc_val = []
y_val = []
xd_test = []
xc_test = []
y_test = []
xd_unknown = []
xc_unknown = []
y_unknown = []
dict_drug_cell = {}
bExist = np.zeros((len(drug_dict), len(cell_dict)))
for item in reader:
drug = item[0]
cell = item[3]
ic50 = item[8]
ic50 = 1 / (1 + pow(math.exp(float(ic50)), -0.1))
temp_data.append((drug, cell, ic50))
random.shuffle(temp_data)
for data in temp_data:
drug, cell, ic50 = data
if drug in drug_dict and cell in cell_dict:
if drug in dict_drug_cell:
dict_drug_cell[drug].append((cell, ic50))
else:
dict_drug_cell[drug] = [(cell, ic50)]
bExist[drug_dict[drug], cell_dict[cell]] = 1
lstDrugTest = []
size = int(len(dict_drug_cell) * 0.8)
size1 = int(len(dict_drug_cell) * 0.9)
pos = 0
for drug,values in dict_drug_cell.items():
pos += 1
for v in values:
cell, ic50 = v
if pos < size:
xd_train.append(drug_smile[drug_dict[drug]])
xc_train.append(cell_feature[cell_dict[cell]])
y_train.append(ic50)
elif pos < size1:
xd_val.append(drug_smile[drug_dict[drug]])
xc_val.append(cell_feature[cell_dict[cell]])
y_val.append(ic50)
else:
xd_test.append(drug_smile[drug_dict[drug]])
xc_test.append(cell_feature[cell_dict[cell]])
y_test.append(ic50)
lstDrugTest.append(drug)
with open('drug_bind_test', 'wb') as fp:
pickle.dump(lstDrugTest, fp)
print(len(y_train), len(y_val), len(y_test))
xd_train, xc_train, y_train = np.asarray(xd_train), np.asarray(xc_train), np.asarray(y_train)
xd_val, xc_val, y_val = np.asarray(xd_val), np.asarray(xc_val), np.asarray(y_val)
xd_test, xc_test, y_test = np.asarray(xd_test), np.asarray(xc_test), np.asarray(y_test)
dataset = 'GDSC'
print('preparing ', dataset + '_train.pt in pytorch format!')
train_data = TestbedDataset(root='data', dataset=dataset+'_train_blind', xd=xd_train, xt=xc_train, y=y_train, smile_graph=smile_graph)
val_data = TestbedDataset(root='data', dataset=dataset+'_val_blind', xd=xd_val, xt=xc_val, y=y_val, smile_graph=smile_graph)
test_data = TestbedDataset(root='data', dataset=dataset+'_test_blind', xd=xd_test, xt=xc_test, y=y_test, smile_graph=smile_graph)
def save_blind_cell_matrix():
f = open(folder + "PANCANCER_IC.csv")
reader = csv.reader(f)
next(reader)
cell_dict, cell_feature = save_cell_mut_matrix()
drug_dict, drug_smile, smile_graph = load_drug_smile()
matrix_list = []
temp_data = []
xd_train = []
xc_train = []
y_train = []
xd_val = []
xc_val = []
y_val = []
xd_test = []
xc_test = []
y_test = []
xd_unknown = []
xc_unknown = []
y_unknown = []
dict_drug_cell = {}
bExist = np.zeros((len(drug_dict), len(cell_dict)))
for item in reader:
drug = item[0]
cell = item[3]
ic50 = item[8]
ic50 = 1 / (1 + pow(math.exp(float(ic50)), -0.1))
temp_data.append((drug, cell, ic50))
random.shuffle(temp_data)
for data in temp_data:
drug, cell, ic50 = data
if drug in drug_dict and cell in cell_dict:
if cell in dict_drug_cell:
dict_drug_cell[cell].append((drug, ic50))
else:
dict_drug_cell[cell] = [(drug, ic50)]
bExist[drug_dict[drug], cell_dict[cell]] = 1
lstCellTest = []
size = int(len(dict_drug_cell) * 0.8)
size1 = int(len(dict_drug_cell) * 0.9)
pos = 0
for cell,values in dict_drug_cell.items():
pos += 1
for v in values:
drug, ic50 = v
if pos < size:
xd_train.append(drug_smile[drug_dict[drug]])
xc_train.append(cell_feature[cell_dict[cell]])
y_train.append(ic50)
elif pos < size1:
xd_val.append(drug_smile[drug_dict[drug]])
xc_val.append(cell_feature[cell_dict[cell]])
y_val.append(ic50)
else:
xd_test.append(drug_smile[drug_dict[drug]])
xc_test.append(cell_feature[cell_dict[cell]])
y_test.append(ic50)
lstCellTest.append(cell)
with open('cell_bind_test', 'wb') as fp:
pickle.dump(lstCellTest, fp)
print(len(y_train), len(y_val), len(y_test))
xd_train, xc_train, y_train = np.asarray(xd_train), np.asarray(xc_train), np.asarray(y_train)
xd_val, xc_val, y_val = np.asarray(xd_val), np.asarray(xc_val), np.asarray(y_val)
xd_test, xc_test, y_test = np.asarray(xd_test), np.asarray(xc_test), np.asarray(y_test)
dataset = 'GDSC'
print('preparing ', dataset + '_train.pt in pytorch format!')
train_data = TestbedDataset(root='data', dataset=dataset+'_train_cell_blind', xd=xd_train, xt=xc_train, y=y_train, smile_graph=smile_graph)
val_data = TestbedDataset(root='data', dataset=dataset+'_val_cell_blind', xd=xd_val, xt=xc_val, y=y_val, smile_graph=smile_graph)
test_data = TestbedDataset(root='data', dataset=dataset+'_test_cell_blind', xd=xd_test, xt=xc_test, y=y_test, smile_graph=smile_graph)
def save_best_individual_drug_cell_matrix():
f = open(folder + "PANCANCER_IC.csv")
reader = csv.reader(f)
next(reader)
cell_dict, cell_feature = save_cell_mut_matrix()
drug_dict, drug_smile, smile_graph = load_drug_smile()
matrix_list = []
temp_data = []
xd_train = []
xc_train = []
y_train = []
dict_drug_cell = {}
bExist = np.zeros((len(drug_dict), len(cell_dict)))
i=0
for item in reader:
drug = item[0]
cell = item[3]
ic50 = item[8]
ic50 = 1 / (1 + pow(math.exp(float(ic50)), -0.1))
if drug == "Bortezomib":
temp_data.append((drug, cell, ic50))
random.shuffle(temp_data)
for data in temp_data:
drug, cell, ic50 = data
if drug in drug_dict and cell in cell_dict:
if drug in dict_drug_cell:
dict_drug_cell[drug].append((cell, ic50))
else:
dict_drug_cell[drug] = [(cell, ic50)]
bExist[drug_dict[drug], cell_dict[cell]] = 1
cells = []
for drug,values in dict_drug_cell.items():
for v in values:
cell, ic50 = v
xd_train.append(drug_smile[drug_dict[drug]])
xc_train.append(cell_feature[cell_dict[cell]])
y_train.append(ic50)
cells.append(cell)
xd_train, xc_train, y_train = np.asarray(xd_train), np.asarray(xc_train), np.asarray(y_train)
with open('cell_blind_sal', 'wb') as fp:
pickle.dump(cells, fp)
dataset = 'GDSC'
print('preparing ', dataset + '_train.pt in pytorch format!')
train_data = TestbedDataset(root='data', dataset=dataset+'_bortezomib', xd=xd_train, xt=xc_train, y=y_train, smile_graph=smile_graph, saliency_map=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='prepare dataset to train model')
parser.add_argument('--choice', type=int, required=False, default=0, help='0.mix test, 1.saliency value, 2.drug blind, 3.cell blind')
args = parser.parse_args()
choice = args.choice
if choice == 0:
# save mix test dataset
save_mix_drug_cell_matrix()
elif choice == 1:
# save saliency map dataset
save_best_individual_drug_cell_matrix()
elif choice == 2:
# save blind drug dataset
save_blind_drug_matrix()
elif choice == 3:
# save blind cell dataset
save_blind_cell_matrix()
else:
print("Invalide option, choose 0 -> 4")