highBandWidth
highBandWidth

Reputation: 17321

Transfer layout from networkx to cytoscape

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.

Upvotes: 18

Views: 12427

Answers (3)

Schobster
Schobster

Reputation: 101

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

Upvotes: 4

user1265067
user1265067

Reputation: 897

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...

Upvotes: 0

samplebias
samplebias

Reputation: 37899

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

Upvotes: 18

Related Questions