Transfer layout from networkx to cytoscape
Asked Answered
P

3

18

I want to use networkx to generate a layout for a graph. Is it possible to transfer this layout to cytoscape and draw it there? I tried to simply write a graph as

import networkx as nx
G = nx.Graph()
G.add_edge(0,1,weight=.1)
G.add_edge(2,1,weight=.2)
nx.write_gml(G,'g.gml')
nx.write_graphml(G,'g.xml')

But neither of these is read in cytoscape. I am not sure how to transfer the graph in a format that can include positions.

Pompey answered 29/4, 2011 at 4:51 Comment(0)
D
18

Your g.xml GraphML file looks good, and loads into Cytoscape for me (I'm on a Mac). Have you installed the graphmlreader plugin?

If not, download it and drop it into your plugins folder, then restart Cytoscape and try loading the g.xml network again.

Update Here is some code to add the graphics look-and-feel and positioning to a networkx graph. It is a bit verbose, and you may be able to omit some of the attributes depending on your needs:

import networkx as nx

G = nx.Graph()
G.add_edge(0, 1, weight=0.1, label='edge', graphics={
    'width': 1.0, 'fill': '"#0000ff"', 'type': '"line"', 'Line': [],
    'source_arrow': 0, 'target_arrow': 0})
nx.set_node_attributes(G, 'graphics', {
    0: {'x': -85.0, 'y': -97.0, 'w': 20.0, 'h': 20.0,
        'type': '"ellipse"', 'fill': '"#889999"', 'outline': '"#666666"',
        'outline_width': 1.0},
    1: {'x': -16.0, 'y': -1.0, 'w': 40.0, 'h': 40.0,
        'type': '"ellipse"', 'fill': '"#ff9999"', 'outline': '"#666666"',
        'outline_width': 1.0}
    })
nx.set_node_attributes(G, 'label', {0: "0", 1: "1"})
nx.write_gml(G, 'network.gml')

Result:

enter image description here

Debit answered 29/4, 2011 at 5:28 Comment(4)
+1 for the tip. So I was able to read in the graphml file in cytoscape. How do I now encode the node positions in the graphml file?Pompey
Cool -- I'll give you an example of adding the node 'graphics' attributes in GML since it is a more compact format, and you may be able to translate this to GraphML.Debit
Just wanted to say THANKS! This piece of code explained just that little bit extra to get my graphs into cytoscape! Now I am able to generate and visualize with interactive probabilities complete metabolic maps.Rosemaria
Could you, please, say which version of python and networkx you are using? I have problems with writing to gml, because networkx doesn't allow to write a dictionary as a parameter (I mean 'graphics'). And I found this to be a common problem for many people.Lathe
M
4

networkx now has functions to write/read graphs to/from cytoscape JSON format: https://networkx.github.io/documentation/stable/_modules/networkx/readwrite/json_graph/cytoscape.html

Monkish answered 18/9, 2020 at 21:7 Comment(1)
This function does the same as @samplebias's answer above and outputs it in the required JSON format, except it does not allow for additional attributes. Only name and Id fields required by cytoscapeEda
T
0

Just an addition for

nx.__version__
'2.3'
nx.set_node_attributes(G,  {n: {'x': p[0], 'y': p[1]}}, 'graphics')

The parameter position is wrong...

Traylor answered 16/12, 2019 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.