Mercurial > repos > computationaltranscriptomics > vgx_converter
comparison vgx_converter.py @ 0:fb26ae916886 draft
Uploaded
author | computationaltranscriptomics |
---|---|
date | Tue, 23 Feb 2016 09:02:41 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:fb26ae916886 |
---|---|
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 ################################################# | |
28 data = {} | |
29 with open(sys.argv[1], 'r') as f: | |
30 reader = csv.reader(f, delimiter=delim[sys.argv[2]]) | |
31 for row in reader: # iterate through each line | |
32 p1 = shlex.split(row[0])[0] | |
33 p2 = shlex.split(row[2])[0] | |
34 | |
35 if p1 not in ids: | |
36 ids[p1] = idc | |
37 idc = idc + 1 | |
38 nodes.append({'id': ids[p1], 'name': p1 }) | |
39 if p2 not in ids: | |
40 ids[p2] = idc | |
41 idc = idc + 1 | |
42 nodes.append({'id': ids[p2], 'name': p2 }) | |
43 | |
44 links.append({'source': ids[p1], 'target': ids[p2]}) | |
45 f.close() | |
46 | |
47 data = {'nodes': nodes, 'links': links} | |
48 | |
49 if args['attributes'] != 'None': # None for Galaxy | |
50 data['some'] = 'not in here' | |
51 | |
52 # process attributes list | |
53 with open(sys.argv[3]) as f: | |
54 reader = csv.reader(f, delimiter='\t') | |
55 ids_keys = ids.keys() | |
56 for row in reader: | |
57 propscnt = 0 | |
58 if row[0] in ids_keys: | |
59 pos = ids[row[0]] | |
60 for props in range(len(row)-1): | |
61 nodes[pos]['property'+str(propscnt)] = row[props+1] | |
62 propscnt = propscnt +1 | |
63 f.close() | |
64 | |
65 # write json data back to spec ified output file | |
66 with open(sys.argv[4], 'w') as out: | |
67 json.dump(data, out) | |
68 out.close() | |
69 | |
70 if __name__ == "__main__": | |
71 __main__() |