comparison env/lib/python3.9/site-packages/networkx/readwrite/p2g.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 """
2 This module provides the following: read and write of p2g format
3 used in metabolic pathway studies.
4
5 See https://web.archive.org/web/20080626113807/http://www.cs.purdue.edu/homes/koyuturk/pathway/ for a description.
6
7 The summary is included here:
8
9 A file that describes a uniquely labeled graph (with extension ".gr")
10 format looks like the following:
11
12
13 name
14 3 4
15 a
16 1 2
17 b
18
19 c
20 0 2
21
22 "name" is simply a description of what the graph corresponds to. The
23 second line displays the number of nodes and number of edges,
24 respectively. This sample graph contains three nodes labeled "a", "b",
25 and "c". The rest of the graph contains two lines for each node. The
26 first line for a node contains the node label. After the declaration
27 of the node label, the out-edges of that node in the graph are
28 provided. For instance, "a" is linked to nodes 1 and 2, which are
29 labeled "b" and "c", while the node labeled "b" has no outgoing
30 edges. Observe that node labeled "c" has an outgoing edge to
31 itself. Indeed, self-loops are allowed. Node index starts from 0.
32
33 """
34 import networkx
35 from networkx.utils import open_file
36
37
38 @open_file(1, mode="w")
39 def write_p2g(G, path, encoding="utf-8"):
40 """Write NetworkX graph in p2g format.
41
42 Notes
43 -----
44 This format is meant to be used with directed graphs with
45 possible self loops.
46 """
47 path.write((f"{G.name}\n").encode(encoding))
48 path.write((f"{G.order()} {G.size()}\n").encode(encoding))
49 nodes = list(G)
50 # make dictionary mapping nodes to integers
51 nodenumber = dict(zip(nodes, range(len(nodes))))
52 for n in nodes:
53 path.write((f"{n}\n").encode(encoding))
54 for nbr in G.neighbors(n):
55 path.write((f"{nodenumber[nbr]} ").encode(encoding))
56 path.write("\n".encode(encoding))
57
58
59 @open_file(0, mode="r")
60 def read_p2g(path, encoding="utf-8"):
61 """Read graph in p2g format from path.
62
63 Returns
64 -------
65 MultiDiGraph
66
67 Notes
68 -----
69 If you want a DiGraph (with no self loops allowed and no edge data)
70 use D=networkx.DiGraph(read_p2g(path))
71 """
72 lines = (line.decode(encoding) for line in path)
73 G = parse_p2g(lines)
74 return G
75
76
77 def parse_p2g(lines):
78 """Parse p2g format graph from string or iterable.
79
80 Returns
81 -------
82 MultiDiGraph
83 """
84 description = next(lines).strip()
85 # are multiedges (parallel edges) allowed?
86 G = networkx.MultiDiGraph(name=description, selfloops=True)
87 nnodes, nedges = map(int, next(lines).split())
88 nodelabel = {}
89 nbrs = {}
90 # loop over the nodes keeping track of node labels and out neighbors
91 # defer adding edges until all node labels are known
92 for i in range(nnodes):
93 n = next(lines).strip()
94 nodelabel[i] = n
95 G.add_node(n)
96 nbrs[n] = map(int, next(lines).split())
97 # now we know all of the node labels so we can add the edges
98 # with the correct labels
99 for n in G:
100 for nbr in nbrs[n]:
101 G.add_edge(n, nodelabel[nbr])
102 return G