Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / readwrite / json_graph / cytoscape.py @ 5cef0f13

History | View | Annotate | Download (3.02 KB)

1
#    BSD license.
2

    
3
import networkx as nx
4

    
5
__all__ = ['cytoscape_data', 'cytoscape_graph']
6

    
7
_attrs = dict(name='name', ident='id')
8

    
9

    
10
def cytoscape_data(G, attrs=None):
11
    """Returns data in Cytoscape JSON format (cyjs).
12

13
    Parameters
14
    ----------
15
    G : NetworkX Graph
16

17

18
    Returns
19
    -------
20
    data: dict
21
        A dictionary with cyjs formatted data.
22
    Raises
23
    ------
24
    NetworkXError
25
        If values in attrs are not unique.
26
    """
27
    if not attrs:
28
        attrs = _attrs
29
    else:
30
        attrs.update({k: v for (k, v) in _attrs.items() if k not in attrs})
31

    
32
    name = attrs["name"]
33
    ident = attrs["ident"]
34

    
35
    if len(set([name, ident])) < 2:
36
        raise nx.NetworkXError('Attribute names are not unique.')
37

    
38
    jsondata = {"data": list(G.graph.items())}
39
    jsondata['directed'] = G.is_directed()
40
    jsondata['multigraph'] = G.is_multigraph()
41
    jsondata["elements"] = {"nodes": [], "edges": []}
42
    nodes = jsondata["elements"]["nodes"]
43
    edges = jsondata["elements"]["edges"]
44

    
45
    for i, j in G.nodes.items():
46
        n = {"data": j.copy()}
47
        n["data"]["id"] = j.get(ident) or str(i)
48
        n["data"]["value"] = i
49
        n["data"]["name"] = j.get(name) or str(i)
50
        nodes.append(n)
51

    
52
    if G.is_multigraph():
53
        for e in G.edges(keys=True):
54
            n = {"data": G.adj[e[0]][e[1]][e[2]].copy()}
55
            n["data"]["source"] = e[0]
56
            n["data"]["target"] = e[1]
57
            n["data"]["key"] = e[2]
58
            edges.append(n)
59
    else:
60
        for e in G.edges():
61
            n = {"data": G.adj[e[0]][e[1]].copy()}
62
            n["data"]["source"] = e[0]
63
            n["data"]["target"] = e[1]
64
            edges.append(n)
65
    return jsondata
66

    
67

    
68
def cytoscape_graph(data, attrs=None):
69
    if not attrs:
70
        attrs = _attrs
71
    else:
72
        attrs.update({k: v for (k, v) in _attrs.items() if k not in attrs})
73

    
74
    name = attrs["name"]
75
    ident = attrs["ident"]
76

    
77
    if len(set([ident, name])) < 2:
78
        raise nx.NetworkXError('Attribute names are not unique.')
79

    
80
    multigraph = data.get('multigraph')
81
    directed = data.get('directed')
82
    if multigraph:
83
        graph = nx.MultiGraph()
84
    else:
85
        graph = nx.Graph()
86
    if directed:
87
        graph = graph.to_directed()
88
    graph.graph = dict(data.get('data'))
89
    for d in data["elements"]["nodes"]:
90
        node_data = d["data"].copy()
91
        node = d["data"]["value"]
92

    
93
        if d["data"].get(name):
94
            node_data[name] = d["data"].get(name)
95
        if d["data"].get(ident):
96
            node_data[ident] = d["data"].get(ident)
97

    
98
        graph.add_node(node)
99
        graph.nodes[node].update(node_data)
100

    
101
    for d in data["elements"]["edges"]:
102
        edge_data = d["data"].copy()
103
        sour = d["data"].pop("source")
104
        targ = d["data"].pop("target")
105
        if multigraph:
106
            key = d["data"].get("key", 0)
107
            graph.add_edge(sour, targ, key=key)
108
            graph.edges[sour, targ, key].update(edge_data)
109
        else:
110
            graph.add_edge(sour, targ)
111
            graph.edges[sour, targ].update(edge_data)
112
    return graph