Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 515
highBandWidth Avatar asked Apr 29 '11 04:04

highBandWidth


2 Answers

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

like image 50
samplebias Avatar answered Nov 11 '22 16:11

samplebias


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

like image 20
Schobster Avatar answered Nov 11 '22 16:11

Schobster