comparison env/lib/python3.7/site-packages/networkx/readwrite/p2g.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
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 # Copyright (C) 2008-2012 by
35 # Aric Hagberg <hagberg@lanl.gov>
36 # Dan Schult <dschult@colgate.edu>
37 # Pieter Swart <swart@lanl.gov>
38 # All rights reserved.
39 # BSD license.
40 import networkx
41 from networkx.utils import is_string_like, open_file
42 __author__ = '\n'.join(['Willem Ligtenberg (w.p.a.ligtenberg@tue.nl)',
43 'Aric Hagberg (aric.hagberg@gmail.com)'])
44
45
46 @open_file(1, mode='w')
47 def write_p2g(G, path, encoding='utf-8'):
48 """Write NetworkX graph in p2g format.
49
50 Notes
51 -----
52 This format is meant to be used with directed graphs with
53 possible self loops.
54 """
55 path.write(("%s\n" % G.name).encode(encoding))
56 path.write(("%s %s\n" % (G.order(), G.size())).encode(encoding))
57 nodes = list(G)
58 # make dictionary mapping nodes to integers
59 nodenumber = dict(zip(nodes, range(len(nodes))))
60 for n in nodes:
61 path.write(("%s\n" % n).encode(encoding))
62 for nbr in G.neighbors(n):
63 path.write(("%s " % nodenumber[nbr]).encode(encoding))
64 path.write("\n".encode(encoding))
65
66
67 @open_file(0, mode='r')
68 def read_p2g(path, encoding='utf-8'):
69 """Read graph in p2g format from path.
70
71 Returns
72 -------
73 MultiDiGraph
74
75 Notes
76 -----
77 If you want a DiGraph (with no self loops allowed and no edge data)
78 use D=networkx.DiGraph(read_p2g(path))
79 """
80 lines = (line.decode(encoding) for line in path)
81 G = parse_p2g(lines)
82 return G
83
84
85 def parse_p2g(lines):
86 """Parse p2g format graph from string or iterable.
87
88 Returns
89 -------
90 MultiDiGraph
91 """
92 description = next(lines).strip()
93 # are multiedges (parallel edges) allowed?
94 G = networkx.MultiDiGraph(name=description, selfloops=True)
95 nnodes, nedges = map(int, next(lines).split())
96 nodelabel = {}
97 nbrs = {}
98 # loop over the nodes keeping track of node labels and out neighbors
99 # defer adding edges until all node labels are known
100 for i in range(nnodes):
101 n = next(lines).strip()
102 nodelabel[i] = n
103 G.add_node(n)
104 nbrs[n] = map(int, next(lines).split())
105 # now we know all of the node labels so we can add the edges
106 # with the correct labels
107 for n in G:
108 for nbr in nbrs[n]:
109 G.add_edge(n, nodelabel[nbr])
110 return G