diff rpviz/network2json.py @ 16:fe78fd6b315a draft

planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
author pablocarb
date Tue, 11 Jun 2019 11:42:40 -0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rpviz/network2json.py	Tue Jun 11 11:42:40 2019 -0400
@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Thu May 30 16:23:33 2019
+
+@author: anael
+"""
+
+import networkx as nx
+import json
+
+
+
+def network2(LR,Lreact,Lprod,name,smile,image,spname,splinks):
+    ###Create the network with networkx
+    G=nx.DiGraph()
+    G.add_nodes_from(LR) #add reactions nodes
+    
+    
+    for i in range(len(LR)):
+        for j in range(len(Lreact[i])):
+            G.add_edge(Lreact[i][j],LR[i]) #add reactants nodes
+        for k in range(len(Lprod[i])):
+            G.add_edge(LR[i],Lprod[i][k]) #add products nodes
+    
+    #Attribute category
+    
+    dic_types={}
+    for i in range(len(LR)):
+        dic_types[(list(G.nodes))[i]]='reactions'
+        for node in range(len(list(G.nodes))):
+            if list(G.nodes)[node] in Lprod[i]:
+                dic_types[list(G.nodes)[node]]='product'
+            if list(G.nodes)[node] not in dic_types:
+                dic_types[list(G.nodes)[node]]='reactant'
+    nx.set_node_attributes(G,name='category',values=dic_types)   
+    nx.draw(G)
+    
+    #Attribute smile
+    nx.set_node_attributes(G, name='smiles', values=smile)
+    
+    #Attribute image
+    nx.set_node_attributes(G,name='image', values=image)
+
+    #Attribute name
+    nx.set_node_attributes(G,name='name', values=spname)
+    
+    #Attribute link
+    nx.set_node_attributes(G,name="link",values=splinks)
+    
+    js = nx.readwrite.json_graph.cytoscape_data(G)
+    json_elements=json.dumps(js)
+    
+    return(json_elements)
+    #file = os.path.join('file_json',name+'.json')
+    #json.dump(js,open(file,'w')) #doesn't work on Windows
+
+    #from py2html import html
+    #html(file, name)
+    
+    #from py2html2 import html2
+    #html2(file,name)
\ No newline at end of file