-
Notifications
You must be signed in to change notification settings - Fork 2
/
visu_network.py
442 lines (366 loc) · 16.6 KB
/
visu_network.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# -*- coding: utf-8 -*-
"""
Description:
Allows to visualize a metabolic network on a compounds perspectives
::
usage:
padmet visu_network -i=FILE -o=FILE [--html=FILE] [--level=STR] [--hide-currency]
options:
-h --help Show help.
-i=FILE pathname to the input file (either PADMet or SBML).
-o=FILE pathname to the output file (picture of metabolic network).
--html=FILE pathname to the output file (interactive hmtl of metabolic network).
--level=STR level of precision for the visualization (compound, reaction or pathway). By default visualization uses "compound".
--hide-currency hide currency metabolites.
"""
import docopt
import logging
import sys
try:
import igraph
except ImportError:
raise ImportError('Requires igraph with cairocffi, try:\npip install python-igraph cairocffi')
from cobra.io.sbml import read_sbml_model
from padmet.classes import PadmetRef, PadmetSpec
from padmet.utils.sbmlPlugin import convert_from_coded_id
logging.getLogger("cobra.io.sbml").setLevel(logging.CRITICAL)
def command_help():
"""
Show help for analysis command.
"""
print(docopt.docopt(__doc__))
def visu_network_cli(command_args):
args = docopt.docopt(__doc__, argv=command_args)
metabolic_network_file = args["-i"]
output_file = args["-o"]
html_output_file = args['--html']
visualization_level = args['--level']
hide_currency_metabolites = args["--hide-currency"]
if visualization_level is None:
visualization_level = "compound"
create_graph(metabolic_network_file, output_file, visualization_level, hide_currency_metabolites)
if html_output_file:
create_html_graph(metabolic_network_file, html_output_file, visualization_level, hide_currency_metabolites)
def parse_compounds_padmet(padmet_file, hide_metabolites):
""" Parse padmets files to extract compounds to create edges and nodes for igraph.
Parameters
----------
padmet_file: str
pathname of the padmet file
hide_metabolites: list
list of metabolites to hide
Returns
-------
edges: list
edges between two compounds (symbolizing the reaction)
edges_label: list
for each edge the name of the reaction
weights: list
the weight associated to each edge
nodes: list
a compound
nodes_label: list
for each node the name of the compound
"""
padmetSpec = PadmetSpec(padmet_file)
all_rxns = [node for node in padmetSpec.dicOfNode.values() if node.type == "reaction"]
edges = []
edges_label = []
weights = []
nodes = {}
nodes_label = []
for rxn in all_rxns:
ins = []
outs = []
for rlt in padmetSpec.dicOfRelationIn[rxn.id]:
if rlt.type == "consumes":
ins.append(rlt.id_out)
if rlt.type == "produces":
outs.append(rlt.id_out)
for compound_in in ins:
if compound_in not in hide_metabolites:
for compound_out in outs:
if compound_out not in hide_metabolites:
if compound_in not in nodes:
new_cpd_id = len(nodes_label)
nodes_label.append(compound_in)
nodes[compound_in] = new_cpd_id
if compound_out not in nodes:
new_cpd_id = len(nodes_label)
nodes_label.append(compound_out)
nodes[compound_out] = new_cpd_id
edges.append((nodes[compound_in], nodes[compound_out]))
weights.append(1)
edges_label.append(rxn.id)
if 'REVERSIBLE' in rxn.misc['DIRECTION']:
edges.append((nodes[compound_out], nodes[compound_in]))
weights.append(1)
edges_label.append(rxn.id)
return edges, edges_label, weights, nodes, nodes_label
def parse_compounds_sbml(sbml_file, hide_metabolites):
""" Parse sbml files to extract compounds to create edges and nodes for igraph.
Parameters
----------
sbml_file: str
pathname of the sbml file
hide_metabolites: list
list of metabolites to hide
Returns
-------
edges: list
edges between two compounds (symbolizing the reaction)
edges_label: list
for each edge the name of the reaction
weights: list
the weight associated to each edge
nodes: list
a compound
nodes_label: list
for each node the name of the compound
"""
sbml_model = read_sbml_model(sbml_file)
edges = []
edges_label = []
weights = []
nodes = {}
nodes_label = []
for reaction in sbml_model.reactions:
for reactant in reaction.reactants:
reactant = convert_from_coded_id(reactant.id)[0]
if reactant not in hide_metabolites:
for product in reaction.products:
product = convert_from_coded_id(product.id)[0]
if product not in hide_metabolites:
if reactant not in nodes:
new_cpd_id = len(nodes_label)
nodes_label.append(reactant)
nodes[reactant] = new_cpd_id
if product not in nodes:
new_cpd_id = len(nodes_label)
nodes_label.append(product)
nodes[product] = new_cpd_id
edges.append((nodes[reactant], nodes[product]))
weights.append(1)
edges_label.append(reaction.id)
if reaction.reversibility is True:
edges.append((nodes[product], nodes[reactant]))
weights.append(1)
edges_label.append(reaction.id)
return edges, edges_label, weights, nodes, nodes_label
def parse_reactions_padmet(padmet_file):
""" Parse padmets files to extract reactions to create edges and nodes for igraph.
Parameters
----------
padmet_file: str
pathname of the padmet file
Returns
-------
edges: list
edges between two reactions
edges_label: list
for each edge the name of the reaction
weights: list
the weight associated to each edge
nodes: list
a compound
nodes_label: list
for each node the name of the compound
"""
padmetSpec = PadmetSpec(padmet_file)
edges = []
edges_label = []
weights = []
nodes = {}
nodes_label = []
all_rxns = [node for node in padmetSpec.dicOfNode.values() if node.type == "reaction"]
for rxn in all_rxns:
for rlt in padmetSpec.dicOfRelationIn[rxn.id]:
if rlt.type == "produces":
for sec_rlt in padmetSpec.dicOfRelationOut[rlt.id_out]:
if sec_rlt.type == "consumes":
sec_rxn_id = sec_rlt.id_in
if sec_rxn_id not in nodes:
new_cpd_id = len(nodes_label)
nodes_label.append(sec_rxn_id)
nodes[sec_rxn_id] = new_cpd_id
if rxn.id not in nodes:
new_cpd_id = len(nodes_label)
nodes_label.append(rxn.id)
nodes[rxn.id] = new_cpd_id
if (nodes[rxn.id], nodes[sec_rxn_id]) not in edges:
edges.append((nodes[rxn.id], nodes[sec_rxn_id]))
weights.append(1)
edges_label.append(rxn.id)
return edges, edges_label, weights, nodes, nodes_label
def parse_pathways_padmet(padmet_file):
""" Parse padmets files to extract pathway inputs and ouputs to create edges and nodes for igraph.
Parameters
----------
padmet_file: str
pathname of the padmet file
Returns
-------
edges: list
edges between two compounds (symbolizing the pathway)
edges_label: list
for each edge the name of the pathway
weights: list
the weight associated to each edge
nodes: list
a compound
nodes_label: list
for each node the name of the compound
"""
padmetSpec = PadmetSpec(padmet_file)
# Check if the padmets and padmetref contain the INPUT-COMPOUNDS and OUTPUT-COMPOUNDS in pathway node.misc needed for this analysis.
padmet_input_compounds_in_pwys = [1 for node_pathway in padmetSpec.dicOfNode
if padmetSpec.dicOfNode[node_pathway].type == 'pathway' and 'INPUT-COMPOUNDS' in padmetSpec.dicOfNode[node_pathway].misc]
padmet_output_compounds_in_pwys = [1 for node_pathway in padmetSpec.dicOfNode
if padmetSpec.dicOfNode[node_pathway].type == 'pathway' and 'OUTPUT-COMPOUNDS' in padmetSpec.dicOfNode[node_pathway].misc]
if sum(padmet_input_compounds_in_pwys) == 0 or sum(padmet_output_compounds_in_pwys) == 0:
sys.exit("The padmet " + padmet_file + " does not contain INPUT-COMPOUNDS and OUTPUT-COMPOUNDS in the pathway node, padmet can't produce the pathway visualization without them.")
edges = []
edges_label = []
weights = []
nodes = {}
nodes_label = []
all_pwys = [node for node in padmetSpec.dicOfNode if padmetSpec.dicOfNode[node].type == "pathway"]
for pwy in all_pwys:
node_pwy = padmetSpec.dicOfNode[pwy]
if 'INPUT-COMPOUNDS' in node_pwy.misc and 'OUTPUT-COMPOUNDS' in node_pwy.misc:
reactants = node_pwy.misc['INPUT-COMPOUNDS'][0].split(',')
products = node_pwy.misc['OUTPUT-COMPOUNDS'][0].split(',')
for reactant in reactants:
if reactant not in nodes:
new_cpd_id = len(nodes_label)
nodes_label.append(reactant)
nodes[reactant] = new_cpd_id
for product in products:
if product not in nodes:
new_cpd_id = len(nodes_label)
nodes_label.append(product)
nodes[product] = new_cpd_id
reactant_product_tuples = [(reactant, product) for reactant in reactants for product in products]
for reactant, product in reactant_product_tuples:
edges.append((nodes[reactant], nodes[product]))
weights.append(1)
edges_label.append(node_pwy.id)
return edges, edges_label, weights, nodes, nodes_label
def create_graph(metabolic_network_file, output_file, visualization_level, hide_currency_metabolites):
""" Using output of parse_compounds_padmet or parse_compounds_sbml create a network picture using igraph.
Parameters
----------
metabolic_network_file: str
pathname of the metabolic network file
output_file: str
pathname of the output picture of the metabolic network
visualization_level: str
level of visualization either compound, reaction or pathway
hide_currency_metabolites: bool
hide currency metabolites
"""
if hide_currency_metabolites:
hide_metabolites = ["PROTON", "WATER", "OXYGEN-MOLECULE", "NADP", "NADPH", "ATP",
"PPI", "CARBON-DIOXIDE", "Pi", "ADP", "CO-A", "UDP", "NAD",
"NADH", "AMP", "AMMONIA", "HYDROGEN-PEROXIDE", "Acceptor",
"Donor-H2", "3-5-ADP", "GDP", "CARBON-MONOXIDE", "GTP", "FAD"]
else:
hide_metabolites = []
if visualization_level == "compound":
if metabolic_network_file.endswith('.padmet'):
edges, edges_label, weights, nodes, nodes_label = parse_compounds_padmet(metabolic_network_file, hide_metabolites)
elif metabolic_network_file.endswith('.sbml'):
edges, edges_label, weights, nodes, nodes_label = parse_compounds_sbml(metabolic_network_file, hide_metabolites)
else:
sys.exit('No correct extension file as input. Input must be a .padmet or a .sbml file.')
elif visualization_level == "reaction":
if metabolic_network_file.endswith('.padmet'):
edges, edges_label, weights, nodes, nodes_label = parse_reactions_padmet(metabolic_network_file)
else:
sys.exit('No correct extension file as input for reaction level. Input must be a .padmet.')
elif visualization_level == "pathway":
if metabolic_network_file.endswith('.padmet'):
edges, edges_label, weights, nodes, nodes_label = parse_pathways_padmet(metabolic_network_file)
else:
sys.exit('No correct extension file as input for pathway level. Input must be a .padmet.')
n_vertices = len(nodes)
# igraph network implementation is from https://gist.github.com/Vini2/d13b12b37b01b4001cabf38b1f850d8a#file-visualise_graph_demo-ipynb
# Thanks to Vini2.
# Create graph
compounds_graph = igraph.Graph(directed=True)
# Add vertices
compounds_graph.add_vertices(n_vertices)
# Add edges to the graph
compounds_graph.add_edges(edges)
# Nodes label
compounds_graph.vs["label"] = nodes_label
# Add weights to edges in the graph
compounds_graph.es['weight'] = weights
visual_style = {}
# Define colors used for outdegree visualization
colours = ['#fecc5c', '#a31a1c']
# Set bbox and margin
visual_style["bbox"] = (5000,5000)
visual_style["margin"] = 17
# Set vertex colours
visual_style["vertex_color"] = 'grey'
# Set vertex size
visual_style["vertex_size"] = 20
# Set vertex lable size
visual_style["vertex_label_size"] = 8
# Don't curve the edges
visual_style["edge_curved"] = True
visual_style["edge_width"] = 2
visual_style["edge_arrow_size"] = 0.2
# Set the layout
my_layout = compounds_graph.layout_fruchterman_reingold()
visual_style["layout"] = my_layout
# Plot the graph
igraph.plot(compounds_graph, output_file, **visual_style)
def create_html_graph(metabolic_network_file, output_file, visualization_level, hide_currency_metabolites):
""" Using output of parse_compounds_padmet or parse_compounds_sbml create an interactive graph in html.
Parameters
----------
metabolic_network_file: str
pathname of the metabolic network file
output_file: str
pathname of the output picture of the metabolic network
visualization_level: str
level of visualization either compound, reaction or pathway
hide_currency_metabolites: bool
hide currency metabolites
"""
try:
import pyvis
except ImportError:
raise ImportError('Requires pyvis, try:\npip install pyvis')
if hide_currency_metabolites:
hide_metabolites = ["PROTON", "WATER", "OXYGEN-MOLECULE", "NADP", "NADPH", "ATP",
"PPI", "CARBON-DIOXIDE", "Pi", "ADP", "CO-A", "UDP", "NAD",
"NADH", "AMP", "AMMONIA", "HYDROGEN-PEROXIDE", "Acceptor",
"Donor-H2", "3-5-ADP", "GDP", "CARBON-MONOXIDE", "GTP", "FAD"]
else:
hide_metabolites = []
if visualization_level == "compound":
if metabolic_network_file.endswith('.padmet'):
edges, edges_label, weights, nodes, nodes_label = parse_compounds_padmet(metabolic_network_file, hide_metabolites)
elif metabolic_network_file.endswith('.sbml'):
edges, edges_label, weights, nodes, nodes_label = parse_compounds_sbml(metabolic_network_file, hide_metabolites)
else:
sys.exit('No correct extension file as input. Input must be a .padmet or a .sbml file.')
elif visualization_level == "reaction":
if metabolic_network_file.endswith('.padmet'):
edges, edges_label, weights, nodes, nodes_label = parse_reactions_padmet(metabolic_network_file)
else:
sys.exit('No correct extension file as input for reaciton level. Input must be a .padmet.')
elif visualization_level == "pathway":
if metabolic_network_file.endswith('.padmet'):
edges, edges_label, weights, nodes, nodes_label = parse_pathways_padmet(metabolic_network_file)
else:
sys.exit('No correct extension file as input for pathway level. Input must be a .padmet.')
compounds_graph = pyvis.network.Network(height=1900, width=1900, directed=True)
compounds_graph.barnes_hut()
compounds_graph.add_nodes([node for node in nodes.values()], label=[nodes_label[node] for node in nodes.values()])
for edge in edges:
compounds_graph.add_edge(edge[0], edge[1])
compounds_graph.show(output_file)