Repository 'vgx_converter'
hg clone https://toolshed.g2.bx.psu.edu/repos/computationaltranscriptomics/vgx_converter

Changeset 8:1ae351222569 (2016-05-10)
Previous changeset 7:006d30548237 (2016-05-10) Next changeset 9:26655854fac5 (2016-05-13)
Commit message:
Uploaded
added:
vgx_converter.py
b
diff -r 006d30548237 -r 1ae351222569 vgx_converter.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/vgx_converter.py Tue May 10 19:21:45 2016 -0400
[
@@ -0,0 +1,76 @@
+#!/usr/bin/python
+
+import sys # 
+import csv
+import json
+import shlex
+
+
+def __main__():
+
+    arg_names = ['command', "network", "delimiter", "attributes", "output"]
+    args = dict(zip(arg_names, sys.argv))
+    print args
+
+    idc = 0 # counter for the ids
+    ids = {} # dictionary with id and name correlation
+    keys = []
+
+    nodes = []
+    links = []
+
+    delim = {}
+    delim['tab'] = '\t'
+    delim['space'] = ' '
+
+    #################################################
+    data = {}
+    with open(sys.argv[1], 'r') as f:
+        reader = csv.reader(f, delimiter=delim[sys.argv[2]])
+        for row in reader: # iterate through each line
+            p1 = shlex.split(row[0])[0]
+            p2 = shlex.split(row[2])[0]
+
+            if p1 not in ids:
+                ids[p1] = idc
+                idc = idc + 1
+                nodes.append({'id': ids[p1], 'name': p1 })
+            if p2 not in ids:
+                ids[p2] = idc
+                idc = idc + 1
+                nodes.append({'id': ids[p2], 'name': p2 })
+            
+            links.append({'source': ids[p1], 'target': ids[p2]})
+    f.close()
+
+    data = { 'graph': {'nodes': nodes, 'edges': links}}
+    
+    if args['attributes'] != 'None': # None for Galaxy
+        data['some'] = 'not in here'
+
+        # process attributes list 
+        with open(sys.argv[3]) as f:
+            reader = csv.reader(f, delimiter='\t')
+            ids_keys = ids.keys()
+            for row in reader:
+                propscnt = 0
+                if row[0] in ids_keys:
+                    pos = ids[row[0]]
+                    #for props in range(len(row)-1):
+                    #    nodes[pos]['property'+str(propscnt)] = row[props+1]
+                    #    propscnt = propscnt +1
+                    metadata = {}
+                    for props in range(len(row)-1):
+                        metadata['property'+str(propscnt)] = row[props+1]
+                        propscnt = propscnt +1
+                    print metadata
+                    nodes[pos]['metadata'] = metadata
+        f.close()
+    
+    # write json data back to spec ified output file
+    with open(sys.argv[4], 'w') as out:
+        json.dump(data, out)
+    out.close()
+    
+if __name__ == "__main__":
+    __main__()
\ No newline at end of file