NetworkX阅读细胞景观json错误?

nx7onnlm  于 2023-01-14  发布在  其他
关注(0)|答案(2)|浏览(181)

我正在尝试将Cytoscape Json(* cyjs)读入NetworkX图表。到目前为止没有成功,我也尝试了其他格式。下面是一个示例输入:

{
  "format_version" : "1.0",
  "generated_by" : "cytoscape-3.8.2",
  "target_cytoscapejs_version" : "~2.1",
  "data" : {
    "shared_name" : "Network",
    "name" : "Network",
    "SUID" : 172,
    "__Annotations" : [ ],
    "selected" : true
  },
  "elements" : {
    "nodes" : [ {
      "data" : {
        "id" : "187",
        "shared_name" : "Node 3",
        "name" : "Node 3",
        "SUID" : 187,
        "selected" : false
      },
      "position" : {
        "x" : -387.81580333030377,
        "y" : 6.552640965689666
      },
      "selected" : false
    }, {
      "data" : {
        "id" : "185",
        "shared_name" : "Node 2",
        "name" : "Node 2",
        "SUID" : 185,
        "selected" : false
      },
      "position" : {
        "x" : -236.0,
        "y" : -66.0
      },
      "selected" : false
    }, {
      "data" : {
        "id" : "183",
        "shared_name" : "Node 1",
        "name" : "Node 1",
        "SUID" : 183,
        "selected" : false
      },
      "position" : {
        "x" : -392.0,
        "y" : -89.0
      },
      "selected" : false
    } ],
    "edges" : [ {
      "data" : {
        "id" : "189",
        "source" : "187",
        "target" : "185",
        "shared_name" : "Node 3 (interacts with) Node 2",
        "shared_interaction" : "interacts with",
        "name" : "Node 3 (interacts with) Node 2",
        "interaction" : "interacts with",
        "SUID" : 189,
        "selected" : false
      },
      "selected" : false
    }, {
      "data" : {
        "id" : "191",
        "source" : "183",
        "target" : "185",
        "shared_name" : "Node 1 (interacts with) Node 2",
        "shared_interaction" : "interacts with",
        "name" : "Node 1 (interacts with) Node 2",
        "interaction" : "interacts with",
        "SUID" : 191,
        "selected" : false
      },
      "selected" : false
    } ]
  }
}

这是一个简单的有向图:

当我试图用Netorkx读取它时

import json
from networkx.readwrite.json_graph import cytoscape_data, cytoscape_graph
cyjs = json.load(open("Network.cyjs"))
graph = cytoscape_graph(cyjs)

我得到了以下结果:

...
    graph = cytoscape_graph(cyjs)
  File "/$HOMEDIR/.local/lib/python3.8/site-packages/networkx/readwrite/json_graph/cytoscape.py", line 89, in cytoscape_graph
    node = d["data"]["value"]
KeyError: 'value'

有人有从Cytoscape成功输入到NetworkX的工作示例吗?
任何帮助或见解将不胜感激。

oprakyz7

oprakyz71#

使用JSON编写自己的解析器,提取边列表,从边列表中构建图形。沿着(确保这里的名称是唯一的)

import networkx as nx
import json

def cyjs2graph(cyjs_file_name):

    cyjson = json.load(open(cyjs_file_name))

    name_from_id = {}
    for node in cyjson["elements"]["nodes"]:
        name_from_id[node['data']['id']] = node['data']['name']

    edge_list = []
    for edge in cyjson["elements"]["edges"]:
        src_id = edge['data']['source']
        src_name = name_from_id[src_id]
        tgt_id   = edge['data']['target']
        tgt_name = name_from_id[tgt_id]
        edge_list.append([src_name, tgt_name])
    graph = nx.from_edgelist(edge_list, create_using=nx.DiGraph)
    return graph
gjmwrych

gjmwrych2#

根据前面的答案,我测试了下面的代码,添加“stringdb_score”作为权重,添加“display_name”作为节点名称。

import networkx as nx
import json

def cyjs2graph(cyjs_file_name):
    #Read json
    cyjson = json.load(open(cyjs_file_name))
    #Read nodes and make a dictionary of names
    name_from_id,code_from_id = {},{}
    for node in cyjson["elements"]["nodes"]:
        code_from_id[node['data']['id']] = node['data']['name']
        name_from_id[node['data']['id']] = node['data']['display_name']#Name of the nodes are assumed to be in the "display_name" key

    #Create network
    g = nx.Graph()
    #The assumption next is that nodes has just one name, otherwise the following exception rises
    try:
        assert len(set(code_from_id))==len(set(name_from_id))
    except:
        raise Exception('Names are redundant')

    #Add nodes
    for n in set(name_from_id.values()):
        g.add_node(n)

    #Add edges
    for edge in cyjson["elements"]["edges"]:
        src_id = edge['data']['source']
        src_name = name_from_id[src_id]
        tgt_id   = edge['data']['target']
        tgt_name = name_from_id[tgt_id]
        #Add "stringdb_score" as edge weight
        weight = edge['data']["stringdb_score"]
        if weight>0:
            g.add_weighted_edges_from([(src_name,tgt_name,weight)])
        #
    return(g)

相关问题