将布局从networkx转换到cytoscape

18 投票
3 回答
11868 浏览
提问于 2025-04-16 16:38

我想用networkx来生成一个图的布局。请问能不能把这个布局转移到cytoscape上,然后在那里绘制出来?我试着简单地把图写成

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')

但是这些格式在cytoscape里都无法读取。我不太确定怎么把图转成可以包含位置的格式。

3 个回答

0

这里补充一点内容:

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

这个参数的位置不对……

4

现在,networkx 有一些功能,可以把图(也就是网络)写入或从 Cytoscape 的 JSON 格式中读取。你可以在这里找到相关的文档:https://networkx.github.io/documentation/stable/_modules/networkx/readwrite/json_graph/cytoscape.html

18

你的 g.xml GraphML 文件看起来没问题,可以在 Cytoscape 中正常加载(我用的是 Mac)。你有没有安装 graphmlreader 插件?

如果还没有,下载这个插件,把它放到你的插件文件夹里,然后重启 Cytoscape,再试着加载 g.xml 网络。

更新 这里有一些代码,可以为 networkx 图形添加外观和位置。这段代码有点长,你可以根据自己的需要省略一些属性:

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')

结果:

在这里输入图片描述

撰写回答