8
|
1 #!/usr/bin/python
|
|
2
|
|
3 import sys #
|
|
4 import csv
|
|
5 import json
|
|
6 import shlex
|
|
7
|
|
8
|
|
9 def __main__():
|
|
10
|
|
11 arg_names = ['command', "network", "delimiter", "attributes", "output"]
|
|
12 args = dict(zip(arg_names, sys.argv))
|
|
13 print args
|
|
14
|
|
15 idc = 0 # counter for the ids
|
|
16 ids = {} # dictionary with id and name correlation
|
|
17 keys = []
|
|
18
|
|
19 nodes = []
|
|
20 links = []
|
|
21
|
|
22 delim = {}
|
|
23 delim['tab'] = '\t'
|
|
24 delim['space'] = ' '
|
|
25
|
|
26 #################################################
|
|
27 data = {}
|
|
28 with open(sys.argv[1], 'r') as f:
|
|
29 reader = csv.reader(f, delimiter=delim[sys.argv[2]])
|
|
30 for row in reader: # iterate through each line
|
|
31 p1 = shlex.split(row[0])[0]
|
|
32 p2 = shlex.split(row[2])[0]
|
|
33
|
|
34 if p1 not in ids:
|
|
35 ids[p1] = idc
|
|
36 idc = idc + 1
|
9
|
37 nodes.append({'id': str(ids[p1]), 'label': p1 })
|
8
|
38 if p2 not in ids:
|
|
39 ids[p2] = idc
|
|
40 idc = idc + 1
|
9
|
41 nodes.append({'id': str(ids[p2]), 'label': p2 })
|
8
|
42
|
|
43 links.append({'source': ids[p1], 'target': ids[p2]})
|
|
44 f.close()
|
|
45
|
|
46 data = { 'graph': {'nodes': nodes, 'edges': links}}
|
|
47
|
|
48 if args['attributes'] != 'None': # None for Galaxy
|
|
49 data['some'] = 'not in here'
|
|
50
|
|
51 # process attributes list
|
|
52 with open(sys.argv[3]) as f:
|
|
53 reader = csv.reader(f, delimiter='\t')
|
|
54 ids_keys = ids.keys()
|
|
55 for row in reader:
|
|
56 propscnt = 0
|
|
57 if row[0] in ids_keys:
|
|
58 pos = ids[row[0]]
|
|
59 #for props in range(len(row)-1):
|
|
60 # nodes[pos]['property'+str(propscnt)] = row[props+1]
|
|
61 # propscnt = propscnt +1
|
|
62 metadata = {}
|
|
63 for props in range(len(row)-1):
|
|
64 metadata['property'+str(propscnt)] = row[props+1]
|
|
65 propscnt = propscnt +1
|
|
66 print metadata
|
|
67 nodes[pos]['metadata'] = metadata
|
|
68 f.close()
|
|
69
|
|
70 # write json data back to spec ified output file
|
|
71 with open(sys.argv[4], 'w') as out:
|
|
72 json.dump(data, out)
|
|
73 out.close()
|
|
74
|
|
75 if __name__ == "__main__":
|
|
76 __main__() |