comparison rpviz/network2json.py @ 16:fe78fd6b315a draft

planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
author pablocarb
date Tue, 11 Jun 2019 11:42:40 -0400
parents
children
comparison
equal deleted inserted replaced
15:785b6253af1f 16:fe78fd6b315a
1 # -*- coding: utf-8 -*-
2 """
3 Created on Thu May 30 16:23:33 2019
4
5 @author: anael
6 """
7
8 import networkx as nx
9 import json
10
11
12
13 def network2(LR,Lreact,Lprod,name,smile,image,spname,splinks):
14 ###Create the network with networkx
15 G=nx.DiGraph()
16 G.add_nodes_from(LR) #add reactions nodes
17
18
19 for i in range(len(LR)):
20 for j in range(len(Lreact[i])):
21 G.add_edge(Lreact[i][j],LR[i]) #add reactants nodes
22 for k in range(len(Lprod[i])):
23 G.add_edge(LR[i],Lprod[i][k]) #add products nodes
24
25 #Attribute category
26
27 dic_types={}
28 for i in range(len(LR)):
29 dic_types[(list(G.nodes))[i]]='reactions'
30 for node in range(len(list(G.nodes))):
31 if list(G.nodes)[node] in Lprod[i]:
32 dic_types[list(G.nodes)[node]]='product'
33 if list(G.nodes)[node] not in dic_types:
34 dic_types[list(G.nodes)[node]]='reactant'
35 nx.set_node_attributes(G,name='category',values=dic_types)
36 nx.draw(G)
37
38 #Attribute smile
39 nx.set_node_attributes(G, name='smiles', values=smile)
40
41 #Attribute image
42 nx.set_node_attributes(G,name='image', values=image)
43
44 #Attribute name
45 nx.set_node_attributes(G,name='name', values=spname)
46
47 #Attribute link
48 nx.set_node_attributes(G,name="link",values=splinks)
49
50 js = nx.readwrite.json_graph.cytoscape_data(G)
51 json_elements=json.dumps(js)
52
53 return(json_elements)
54 #file = os.path.join('file_json',name+'.json')
55 #json.dump(js,open(file,'w')) #doesn't work on Windows
56
57 #from py2html import html
58 #html(file, name)
59
60 #from py2html2 import html2
61 #html2(file,name)