-
Notifications
You must be signed in to change notification settings - Fork 15
/
transformData.py
374 lines (322 loc) · 12.9 KB
/
transformData.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
import numpy as np
import pickle as pkl
import csv
import random
def readFromCSV(data_path):
adj = []
with open(data_path,"r") as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='\"')
n =0;
for line in spamreader:
if n==0:
codes = [int(str(a).lstrip("CID")) for a in line[1:]]
n = n+1
else:
code = line[0]
sims = [float(a) for a in line[1:]]
adj.append(sims)
n = n +1
return codes, np.array(adj)
def readFeatureFromCSV(data_path):
featureDict = dict()
with open(data_path, "r") as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='\"')
n = 0;
for line in spamreader:
if n == 0:
featurecodes = line[1:]
n = n + 1
else:
medcode = int(line[0])
sims = [float(a) for a in line[1:]]
featureDict[medcode] = sims
n = n + 1
return featurecodes, featureDict
def readFeature(data_path):
featureDict = dict()
with open(data_path, "r") as featurefile:
n = 0;
for line in featurefile:
if n == 0:
n = n + 1
continue
else:
tabs = line.split("\t")
medcode = int(tabs[0])
features = [int(a) for a in tabs[2:]]
featureDict[medcode] = features
n = n + 1
return featureDict
def list2dict(codes):
dic = dict()
for i in range(len(codes)):
dic[codes[i]] = i
return dic
def tranformFormat(codes, adj, featureDict= None):
codeDic = list2dict(codes)
numCode = len(codes)
graph = dict()
if featureDict is None:
x = np.zeros((numCode, numCode))
for code in codes:
index = codeDic[code]
x[index,index] =1.0
graph[index] = [gi for gi in range(len(codes)) if adj[index, gi]>0 and gi!=index]
return x, graph
else:
x = list()
n = 0
for code in codes:
feature = featureDict[code]
x.append(feature)
index = len(x) - 1
graph[index] = [gi for gi in range(len(codes)) if adj[index, gi] > 0 and gi != index]
return np.array(x), graph
def splitData_node(y, test_ratio):
#test_ratio: ratio on all data including val
numNode = len(y)
Indexs = random.sample(range(numNode), int((test_ratio + 0.1) * numNode))
valIndex = random.sample(Indexs, int(0.1 * numNode))
testIndex = [i for i in Indexs if i not in valIndex]
trainIndex = [i for i in range(numNode) if i not in Indexs]
trainMask = np.zeros((numNode, numNode))
# test_ratio: ratio on data other than val.
# numNode = len(y)
# valIndex = random.sample(range(numNode), int(0.1 * numNode))
# Indexs = [i for i in range(numNode) if i not in valIndex]
# numIndex = len(Indexs)
#
# testIndex = random.sample(Indexs,int((test_ratio) * numIndex))
# trainIndex = [i for i in Indexs if i not in testIndex]
# trainMask = np.zeros((numNode,numNode))
for i in trainIndex:
for j in trainIndex:
if j<i:
continue
elif y[i,j]==1:
trainMask[i,j] = 1
for i in trainIndex:
for j in trainIndex:
if j<i:
continue
elif y[i,j]==0:
sig = random.random()
if sig>0.6:
trainMask[i, j] = 1
trainMask = trainMask+trainMask.T
valMask = np.zeros((numNode, numNode))
for i in valIndex:
for j in valIndex:
if j < i:
continue
else:
valMask[i, j] = 1
valMask[j,i] = 1
for i in valIndex:
for j in trainIndex:
valMask[i, j] = 1
valMask[j,i] = 1
testMask = np.zeros((numNode,numNode))
for i in testIndex:
for j in testIndex:
if j<i:
continue
else:
testMask[i,j] = 1
testMask[j,i] = 1
for i in testIndex:
for j in trainIndex:
testMask[i, j] = 1
testMask[j, i] = 1
return trainMask, valMask, testMask
def splitData_edge(y, test_ratio):
numNode = len(y)
population = [(i,j) for i in range(numNode) for j in range(numNode) if i>j]
numPopulation = len(population)
choices = random.sample(population, int(numPopulation*(test_ratio+0.1)))
valchoices = random.sample(choices, int(numPopulation*0.1))
testMask = np.zeros((numNode,numNode))
valMask = np.zeros((numNode, numNode))
for (i,j) in choices:
if (i,j) not in valchoices:
testMask[i,j] = 1
testMask[j,i] = 1
else:
valMask[i,j] = 1
valMask[j, i] = 1
trainMask = np.ones((numNode,numNode)) - testMask - valMask
np.fill_diagonal(trainMask,0)
# train_ratio = 0.9 - test_ratio
# numNode = len(y)
# population = [(i, j) for i in range(numNode) for j in range(numNode) if i > j and y[i,j]==1]
# numPopulation = len(population)
# numNode = len(y)
# choices = random.sample(population, int(numPopulation * (train_ratio + 0.1)))
# valchoices = random.sample(choices, int(numPopulation * 0.1))
# testchoices = [(i,j) for (i,j) in population if (i,j) not in choices]
# trainMask = np.zeros((numNode,numNode))
# valMask = np.zeros((numNode, numNode))
# testMask = np.zeros((numNode, numNode))
# for (i,j) in choices:
# if (i,j) not in valchoices:
# trainMask[i,j] = 1
# trainMask[j,i] = 1
# else:
# valMask[i,j] = 1
# valMask[j,i] = 1
# for (i,j) in testchoices:
# testMask[i,j] = 1
# testMask[j,i] = 1
#
# nolink_population = [(i, j) for i in range(numNode) for j in range(numNode) if i > j and y[i, j] == 0]
# numNolink = len(nolink_population)
# choices = random.sample(nolink_population, int(numPopulation * (train_ratio + 0.1)))
# valchoices = random.sample(choices, int(numPopulation * 0.1))
# testchoices = [(i, j) for (i, j) in nolink_population if (i, j) not in choices]
# testchoices = random.sample(testchoices, int(numPopulation * test_ratio))
# for (i, j) in choices:
# if (i, j) not in valchoices:
# trainMask[i, j] = 1
# trainMask[j, i] = 1
# else:
# valMask[i, j] = 1
# valMask[j, i] = 1
# for (i, j) in testchoices:
# testMask[i, j] = 1
# testMask[j, i] = 1
return trainMask, testMask, valMask
def writeSimFile(datapath):
circDict = readFeature(datapath)
sortedCirc = sorted(circDict.items(), key=lambda x: x[0])
circFeatureList = []
circCodeList = []
for code, feature in sortedCirc:
circFeatureList.append(feature)
circCodeList.append(code)
strCodeList = ["CID" + ("0000000" + str(code))[-9:] for code in circCodeList]
circSIM = np.zeros((len(circCodeList), len(circCodeList)))
for i in range(len(circFeatureList)):
for j in range(i):
coNum = len([t for t in range(len(circFeatureList[i])) if
circFeatureList[i][t] == 1 and circFeatureList[j][t] == 1])
circSIM[i, j] = float(coNum) / float(sum(circFeatureList[i]) + sum(circFeatureList[j]) - coNum)
circSIM[j, i] = circSIM[i, j]
with open("../Dataset/circular_sim.csv", "wb") as csvfile:
csvfile.write("," + ",".join(strCodeList) + "\n")
for i in range(len(circCodeList)):
strSim = [str(sim) for sim in circSIM[i, :]]
csvfile.write(strCodeList[i] + "," + ",".join(strSim) + "\n")
def run_569():
data_path1 = "../Dataset/chem_sim.csv"
data_path2 = "../Dataset/circular_sim.csv"
data_path3 = "../Dataset/label_sim.csv"
data_path4 = "../Dataset/offlabel_sim.csv"
label_path = "../Dataset/ddi_569.csv"
feature_path = "../Dataset/newsample-pubchem.txt"
circular_path = "../Dataset/newsample-circular.txt"
codes, adj_1 = readFromCSV(data_path1)
_, adj_2 = readFromCSV(data_path2)
_, adj_3 = readFromCSV(data_path3)
_, adj_4 = readFromCSV(data_path4)
printCIDs = [str(a) for a in codes]
wf = open("../Dataset/cid.txt","wb")
wf.write(", ".join(printCIDs))
wf.close()
featureDict = readFeature(circular_path)
x_featureless, graph_1 = tranformFormat(codes, adj_1)
_, graph_2 = tranformFormat(codes, adj_2)
_, graph_3 = tranformFormat(codes, adj_3, featureDict)
x, graph_4 = tranformFormat(codes, adj_4)
codes, y = readFromCSV(label_path)
#trainMask, testMask, valMask = splitData_edge(y, 0.5)
trainMask, testMask, valMask = splitData_node(y, 0.75)
pkl.dump(x,open("DDIdata/ind.ddi.allx","wb"))
pkl.dump(y, open("DDIdata/ind.ddi.ally","wb"))
pkl.dump(x_featureless, open("DDIdata/ind.ddi.x_featureless", "wb"))
pkl.dump(trainMask, open("DDIdata/ind.ddi.trainMask", "wb"))
pkl.dump(testMask, open("DDIdata/ind.ddi.testMask", "wb"))
pkl.dump(valMask, open("DDIdata/ind.ddi.valMask", "wb"))
pkl.dump([adj_1, adj_2, adj_3, adj_4], open("DDIdata/ind.ddi.adjmat","wb"))
pkl.dump([graph_1, graph_2, graph_3, graph_4], open("DDIdata/ind.ddi.graph","wb"))
# print(len(codes))
def run_526():
data_path1 = "../Dataset/pubchem_sim.csv"
data_path2 = "../Dataset/indication_sim.csv"
data_path3 = "../Dataset/TTDS_sim.csv"
data_path4 = "../Dataset/cpi_sim.csv"
label_path = "../Dataset/ddi_C0917801.csv"
feature_path = "../Dataset/pubchem.csv"
codes, adj_1 = readFromCSV(data_path1)
_, adj_2 = readFromCSV(data_path2)
_, adj_3 = readFromCSV(data_path3)
_, adj_4 = readFromCSV(data_path4)
# printCIDs = [str(a) for a in codes]
# wf = open("../Dataset/cid.txt","wb")
# wf.write(", ".join(printCIDs))
# wf.close()
_,featureDict = readFeatureFromCSV(feature_path)
x_featureless, graph_1 = tranformFormat(codes, adj_1)
_, graph_2 = tranformFormat(codes, adj_2)
x, graph_3 = tranformFormat(codes, adj_3, featureDict)
_, graph_4 = tranformFormat(codes, adj_4)
codes, y = readFromCSV(label_path)
#trainMask, testMask, valMask = splitData_edge(y, 0.5)
trainMask, testMask, valMask = splitData_node(y, 0.25)
pkl.dump(x,open("DDIdata/ind.ddi.allx","wb"))
pkl.dump(y, open("DDIdata/ind.ddi.ally","wb"))
pkl.dump(x_featureless, open("DDIdata/ind.ddi.x_featureless", "wb"))
pkl.dump(trainMask, open("DDIdata/ind.ddi.trainMask", "wb"))
pkl.dump(testMask, open("DDIdata/ind.ddi.testMask", "wb"))
pkl.dump(valMask, open("DDIdata/ind.ddi.valMask", "wb"))
pkl.dump([adj_1, adj_2, adj_3, adj_4], open("DDIdata/ind.ddi.adjmat","wb"))
pkl.dump([graph_1, graph_2, graph_3, graph_4], open("DDIdata/ind.ddi.graph","wb"))
# print(len(codes))
def run_for_MLK():
data_path1 = "../Dataset/pubchem_sim.csv"
data_path2 = "../Dataset/indication_sim.csv"
data_path3 = "../Dataset/TTDS_sim.csv"
data_path4 = "../Dataset/cpi_sim.csv"
label_path = "../Dataset/ddi.csv"
feature_path1 = "../Dataset/pubchem.csv"
feature_path2 = "../Dataset/indication.csv"
feature_path3 = "../Dataset/TTDS.csv"
feature_path4 = "../Dataset/cpi.csv"
codes, adj_1 = readFromCSV(data_path1)
_, adj_2 = readFromCSV(data_path2)
_, adj_3 = readFromCSV(data_path3)
_, adj_4 = readFromCSV(data_path4)
# printCIDs = [str(a) for a in codes]
# wf = open("../Dataset/cid.txt","wb")
# wf.write(", ".join(printCIDs))
# wf.close()
_, featureDict1 = readFeatureFromCSV(feature_path1)
_, featureDict2 = readFeatureFromCSV(feature_path2)
_, featureDict3 = readFeatureFromCSV(feature_path3)
_, featureDict4 = readFeatureFromCSV(feature_path4)
featureDict = dict()
for code in featureDict1.keys():
x1 = featureDict1[code]
x2 = featureDict2[code]
x3 = featureDict3[code]
x4 = featureDict4[code]
featureDict[code] = x1+x2+x3+x4
x_featureless, graph_1 = tranformFormat(codes, adj_1)
_, graph_2 = tranformFormat(codes, adj_2)
x, graph_3 = tranformFormat(codes, adj_3, featureDict)
_, graph_4 = tranformFormat(codes, adj_4)
codes, y = readFromCSV(label_path)
# trainMask, testMask, valMask = splitData_edge(y, 0.5)
trainMask, testMask, valMask = splitData_node(y, 0.5)
pkl.dump(x, open("DDIdata/ind.ddi.allx", "wb"))
pkl.dump(y, open("DDIdata/ind.ddi.ally", "wb"))
pkl.dump(x_featureless, open("DDIdata/ind.ddi.x_featureless", "wb"))
pkl.dump(trainMask, open("DDIdata/ind.ddi.trainMask", "wb"))
pkl.dump(testMask, open("DDIdata/ind.ddi.testMask", "wb"))
pkl.dump(valMask, open("DDIdata/ind.ddi.valMask", "wb"))
pkl.dump([adj_1, adj_2, adj_3, adj_4], open("DDIdata/ind.ddi.adjmat", "wb"))
pkl.dump([graph_1, graph_2, graph_3, graph_4], open("DDIdata/ind.ddi.graph", "wb"))
# print(len(codes))
if __name__ == '__main__':
run_569()
# run_526()
# run_for_MLK()