-
Notifications
You must be signed in to change notification settings - Fork 4
/
a2_parse.py
executable file
·58 lines (50 loc) · 1.82 KB
/
a2_parse.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
import sys
import os
cwd = os.getcwd()
nodes = dict()
edges = dict()
infile = sys.argv[1]
outdir = os.path.dirname(os.path.abspath(infile))
with open(infile) as fh:
next(fh)
for line in fh:
line = line.rstrip("\n|\r")
cols = line.split(",")
src = cols[0]
target = cols[1]
# print(src, target)
if src not in nodes:
nodes[src] = dict()
nodes[src]['desc'] = cols[10].replace("'", "")
if 'count' not in nodes[src]:
nodes[src]['count'] = 0
nodes[src]['count'] += 1
if target not in nodes:
nodes[target] = dict()
nodes[target]['desc'] = cols[11].replace("'", "")
if 'count' not in nodes[target]:
nodes[target]['count'] = 0
nodes[target]['count'] += 1
edgeid = src + "_" + target
if edgeid not in edges:
edges[edgeid] = dict()
edges[edgeid]['source'] = src
edges[edgeid]['target'] = target
edges[edgeid]['coef'] = cols[2]
edges[edgeid]['exp_coef'] = "{0:.4f}".format(float(cols[3]))
edges[edgeid]['se_coef'] = cols[4]
edges[edgeid]['z'] = cols[5]
edges[edgeid]['Pr'] = "{0:.4f}".format(float(cols[6]))
edges[edgeid]['N'] = cols[7]
edges[edgeid]['HRtest_p'] = "{0:.5f}".format(float(cols[8]))
edges[edgeid]['Pr_adjusted'] = "{0:.5f}".format(float(cols[9]))
datalist = []
for id in nodes:
datalist.append("{{data:{{id:'{}',name:'{}',count:{}}}}}".format(id, nodes[id]['desc'], nodes[id]['count']))
for id in edges:
datalist.append(" {{data:{{id:'{}',source:'{}',target:'{}',valueN:{},valuePrAdjusted:{},valueexp_coef:{},pHRtest:{},rawPvalue:{}}}}}".format(id, edges[id]['source'], edges[id]['target'], edges[id]['N'],edges[id]['Pr_adjusted'],edges[id]['exp_coef'],edges[id]['HRtest_p'],edges[id]['Pr']))
# print(edges[id]['Pr'],edges[id]['Pr_adjusted'])
with open(outdir+'/all.edges.csv.js', 'w') as out:
out.write("var jsonobj = [")
out.write(",".join(datalist))
out.write("]")