Mercurial > repos > pablocarb > synbiodesign
comparison rpviz/nxvisualizer.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 10:21:48 2019 | |
4 | |
5 @author: anael | |
6 """ | |
7 #Usefull | |
8 import inspect | |
9 #inspect.getargspec(function) | |
10 | |
11 import networkx as nx | |
12 from IPython.display import Image | |
13 import requests | |
14 import json | |
15 import pandas as pd | |
16 | |
17 # Library for util | |
18 from py2cytoscape.data.cynetwork import CyNetwork | |
19 from py2cytoscape.data.cyrest_client import CyRestClient | |
20 from py2cytoscape.data.style import StyleUtil | |
21 | |
22 | |
23 def network(LR,Lreact,Lprod,name,smile,image): | |
24 ###Create the network with networkx | |
25 G=nx.DiGraph() | |
26 G.add_nodes_from(LR) #add reactions nodes | |
27 | |
28 | |
29 for i in range(len(LR)): | |
30 for j in range(len(Lreact[i])): | |
31 G.add_edge(Lreact[i][j],LR[i]) #add reactants nodes | |
32 for k in range(len(Lprod[i])): | |
33 G.add_edge(LR[i],Lprod[i][k]) #add products nodes | |
34 | |
35 #Attribute category | |
36 | |
37 dic_types={} | |
38 for i in range(len(LR)): | |
39 dic_types[(list(G.nodes))[i]]='reactions' | |
40 for node in range(len(list(G.nodes))): | |
41 if list(G.nodes)[node] in Lprod[i]: | |
42 dic_types[list(G.nodes)[node]]='product' | |
43 if list(G.nodes)[node] not in dic_types: | |
44 dic_types[list(G.nodes)[node]]='reactant' | |
45 print(dic_types) | |
46 nx.set_node_attributes(G,name='category',values=dic_types) | |
47 | |
48 #Attribute smile | |
49 nx.set_node_attributes(G, name='smiles', values=smile) | |
50 | |
51 #Attribute image | |
52 nx.set_node_attributes(G,name='image', values=image) | |
53 | |
54 nx.write_gml(G,name+'.gml') | |
55 | |
56 #Connect with cytoscape | |
57 cy = CyRestClient() | |
58 | |
59 # Reset | |
60 #cy.session.delete() | |
61 | |
62 #To create the network in cytoscape | |
63 network = cy.network.create_from_networkx(G, name=name, collection='My network collection') | |
64 print('New network created with py2cytoscape. Its SUID is ' + str(network.get_id())) | |
65 | |
66 #To get the SUID of all the components of the network | |
67 all_suid = cy.network.get_all() | |
68 net1 = cy.network.create(all_suid[0]) | |
69 print(net1.get_first_view()) | |
70 | |
71 #Styles | |
72 cy.layout.apply(name='hierarchical', network=network) | |
73 | |
74 # Get all existing Visual Styles | |
75 styles = cy.style.get_all() | |
76 print(json.dumps(styles, indent=4)) | |
77 | |
78 | |
79 # Get a reference to the existing style | |
80 default_style = cy.style.create('default') | |
81 print(default_style.get_name()) | |
82 | |
83 # Get all available Visual Properties | |
84 print(len(cy.style.vps.get_all())) | |
85 node_vps=cy.style.vps.get_node_visual_props() | |
86 edge_vps = cy.style.vps.get_edge_visual_props() | |
87 network_vps=cy.style.vps.get_network_visual_props() | |
88 print(pd.Series(edge_vps).head()) | |
89 print(pd.Series(node_vps).head()) | |
90 | |
91 | |
92 # Create a new style | |
93 style1 = cy.style.create("My_style") | |
94 print(style1.get_name()) | |
95 | |
96 | |
97 new_defaults = { | |
98 # Node defaults | |
99 'NODE_FILL_COLOR': '#00ddee', | |
100 'NODE_SIZE': 20, | |
101 'NODE_BORDER_WIDTH': 0, | |
102 'NODE_LABEL_COLOR': 'black', | |
103 | |
104 # Edge defaults | |
105 'EDGE_WIDTH': 3, | |
106 'EDGE_STROKE_UNSELECTED_PAINT': '#aaaaaa', | |
107 'EDGE_LINE_TYPE': 'LONG_DASH', | |
108 'EDGE_TARGET_ARROW_SHAPE' : 'DELTA', | |
109 | |
110 # Network defaults | |
111 'NETWORK_BACKGROUND_PAINT': 'white', | |
112 } | |
113 | |
114 style1.update_defaults(new_defaults) | |
115 #Apply style | |
116 cy.style.apply(style1, network) | |
117 | |
118 # Passthrough mapping to get node labels | |
119 style1.create_passthrough_mapping(column='name', col_type='String', vp='NODE_LABEL') | |
120 | |
121 # Discrete mapping for node colours: | |
122 cat = { | |
123 'reactant': '#4D833C', | |
124 'product': '#C04E5D' | |
125 } | |
126 style1.create_discrete_mapping(column='category', | |
127 col_type='String', | |
128 vp='NODE_FILL_COLOR', | |
129 mappings=cat) | |
130 | |
131 # Discrete mapping for node shape: | |
132 reac = { | |
133 'reactions': 'RECTANGLE' | |
134 } | |
135 style1.create_discrete_mapping(column='category', | |
136 col_type='String', | |
137 vp='NODE_SHAPE', | |
138 mappings=reac) | |
139 |