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.

Shog9
  • 156,901
  • 35
  • 231
  • 235
highBandWidth
  • 16,751
  • 20
  • 84
  • 131

3 Answers3

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

samplebias
  • 37,113
  • 6
  • 107
  • 103
  • 1
    +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? – highBandWidth Apr 29 '11 at 16:36
  • 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. – samplebias Apr 29 '11 at 18:13
  • 1
    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. – Jasper Feb 02 '13 at 10:21
  • 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. – Roman2452809 Nov 02 '16 at 16:31
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

Schobster
  • 101
  • 2
  • 2
  • 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 cytoscape – seeiespi Dec 06 '21 at 23:09
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...

user1265067
  • 867
  • 1
  • 10
  • 26