-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsv_to_json_fd.py
86 lines (64 loc) · 1.85 KB
/
tsv_to_json_fd.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
import csv
import json
import time
start = time.time()
start_process = time.clock()
nodes = []
drug_list = []
pair_dict = {}
effect_list = []
links = []
group_number = 1
# umls_dict = {}
# num_lines = 0
# with open("offsides_first1500.tsv") as tsv:
# next(tsv)
# for line in csv.reader(tsv, dialect="excel-tab"):
# umls_id = line[2]
# if umls_id not in umls_dict:
# umls_dict[umls_id] = group_number
# group_number += 1
with open("offsides.tsv") as tsv:
next(tsv)
for line in csv.reader(tsv, dialect="excel-tab"):
drug = line[1]
umls_id = line[2]
effect = line[3]
value = line[7]
if drug not in drug_list:
drug_list.append(drug)
# drug_dict = {"name": drug, "group": umls_dict[umls_id]}
# nodes.append(drug_dict)
if effect not in effect_list:
effect_list.append(effect)
# effect_dict = {"name": effect, "group": umls_dict[umls_id]}
# nodes.append(effect_dict)
for drug in drug_list:
drug_dict = {"id": drug, "group": group_number}
group_number += 1
nodes.append(drug_dict)
for effect in effect_list:
effect_dict = {"id": effect, "group": group_number}
group_number += 1
nodes.append(effect_dict)
with open("offsides.tsv") as tsv:
next(tsv)
for line in csv.reader(tsv, dialect="excel-tab"):
drug = line[1]
effect = line[3]
value = line[7]
try:
drug_index = drug_list.index(drug)
effect_index = effect_list.index(effect)
except ValueError:
continue
links_dict = {"source": drug, "target": effect, "value": float(value)}
if links_dict not in links:
links.append(links_dict)
data = {"nodes": nodes, "links": links}
with open('templates/offsides_every20.json', 'w') as outfile:
json.dump(data, outfile, indent=4, separators=(',', ': '))
end = time.time()
end_process = time.clock()
print "time: " + str(end - start)
print "process time: " + str(end_process - start_process)