Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/readwrite/json_graph/cytoscape.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 import networkx as nx | |
2 | |
3 __all__ = ["cytoscape_data", "cytoscape_graph"] | |
4 | |
5 _attrs = dict(name="name", ident="id") | |
6 | |
7 | |
8 def cytoscape_data(G, attrs=None): | |
9 """Returns data in Cytoscape JSON format (cyjs). | |
10 | |
11 Parameters | |
12 ---------- | |
13 G : NetworkX Graph | |
14 | |
15 | |
16 Returns | |
17 ------- | |
18 data: dict | |
19 A dictionary with cyjs formatted data. | |
20 Raises | |
21 ------ | |
22 NetworkXError | |
23 If values in attrs are not unique. | |
24 """ | |
25 if not attrs: | |
26 attrs = _attrs | |
27 else: | |
28 attrs.update({k: v for (k, v) in _attrs.items() if k not in attrs}) | |
29 | |
30 name = attrs["name"] | |
31 ident = attrs["ident"] | |
32 | |
33 if len({name, ident}) < 2: | |
34 raise nx.NetworkXError("Attribute names are not unique.") | |
35 | |
36 jsondata = {"data": list(G.graph.items())} | |
37 jsondata["directed"] = G.is_directed() | |
38 jsondata["multigraph"] = G.is_multigraph() | |
39 jsondata["elements"] = {"nodes": [], "edges": []} | |
40 nodes = jsondata["elements"]["nodes"] | |
41 edges = jsondata["elements"]["edges"] | |
42 | |
43 for i, j in G.nodes.items(): | |
44 n = {"data": j.copy()} | |
45 n["data"]["id"] = j.get(ident) or str(i) | |
46 n["data"]["value"] = i | |
47 n["data"]["name"] = j.get(name) or str(i) | |
48 nodes.append(n) | |
49 | |
50 if G.is_multigraph(): | |
51 for e in G.edges(keys=True): | |
52 n = {"data": G.adj[e[0]][e[1]][e[2]].copy()} | |
53 n["data"]["source"] = e[0] | |
54 n["data"]["target"] = e[1] | |
55 n["data"]["key"] = e[2] | |
56 edges.append(n) | |
57 else: | |
58 for e in G.edges(): | |
59 n = {"data": G.adj[e[0]][e[1]].copy()} | |
60 n["data"]["source"] = e[0] | |
61 n["data"]["target"] = e[1] | |
62 edges.append(n) | |
63 return jsondata | |
64 | |
65 | |
66 def cytoscape_graph(data, attrs=None): | |
67 if not attrs: | |
68 attrs = _attrs | |
69 else: | |
70 attrs.update({k: v for (k, v) in _attrs.items() if k not in attrs}) | |
71 | |
72 name = attrs["name"] | |
73 ident = attrs["ident"] | |
74 | |
75 if len({ident, name}) < 2: | |
76 raise nx.NetworkXError("Attribute names are not unique.") | |
77 | |
78 multigraph = data.get("multigraph") | |
79 directed = data.get("directed") | |
80 if multigraph: | |
81 graph = nx.MultiGraph() | |
82 else: | |
83 graph = nx.Graph() | |
84 if directed: | |
85 graph = graph.to_directed() | |
86 graph.graph = dict(data.get("data")) | |
87 for d in data["elements"]["nodes"]: | |
88 node_data = d["data"].copy() | |
89 node = d["data"]["value"] | |
90 | |
91 if d["data"].get(name): | |
92 node_data[name] = d["data"].get(name) | |
93 if d["data"].get(ident): | |
94 node_data[ident] = d["data"].get(ident) | |
95 | |
96 graph.add_node(node) | |
97 graph.nodes[node].update(node_data) | |
98 | |
99 for d in data["elements"]["edges"]: | |
100 edge_data = d["data"].copy() | |
101 sour = d["data"].pop("source") | |
102 targ = d["data"].pop("target") | |
103 if multigraph: | |
104 key = d["data"].get("key", 0) | |
105 graph.add_edge(sour, targ, key=key) | |
106 graph.edges[sour, targ, key].update(edge_data) | |
107 else: | |
108 graph.add_edge(sour, targ) | |
109 graph.edges[sour, targ].update(edge_data) | |
110 return graph |