
    wg                         d Z ddlZddlmZ  edd      dd       Z edd       ej                  dd	
      dd              Z ej                  dd	
      d        Zy)a  
This module provides the following: read and write of p2g format
used in metabolic pathway studies.

See https://web.archive.org/web/20080626113807/http://www.cs.purdue.edu/homes/koyuturk/pathway/ for a description.

The summary is included here:

A file that describes a uniquely labeled graph (with extension ".gr")
format looks like the following:


name
3 4
a
1 2
b

c
0 2

"name" is simply a description of what the graph corresponds to. The
second line displays the number of nodes and number of edges,
respectively. This sample graph contains three nodes labeled "a", "b",
and "c". The rest of the graph contains two lines for each node. The
first line for a node contains the node label. After the declaration
of the node label, the out-edges of that node in the graph are
provided. For instance, "a" is linked to nodes 1 and 2, which are
labeled "b" and "c", while the node labeled "b" has no outgoing
edges. Observe that node labeled "c" has an outgoing edge to
itself. Indeed, self-loops are allowed. Node index starts from 0.

    N)	open_file   w)modec           
      R   |j                  | j                   dj                  |             |j                  | j                          d| j	                          dj                  |             t        |       }t        t        |t        t        |                        }|D ]  }|j                  | dj                  |             | j                  |      D ](  }|j                  ||    dj                  |             * |j                  dj                  |              y)zWrite NetworkX graph in p2g format.

    Notes
    -----
    This format is meant to be used with directed graphs with
    possible self loops.
    
 N)writenameencodeordersizelistdictziprangelen	neighbors)Gpathencodingnodes
nodenumbernnbrs          [/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/readwrite/p2g.py	write_p2gr   '   s     	JJ166("%%h/0JJ1779+Qqvvxj+33H=>GEc%s5z!234J *

qcH$$X./;;q> 	ACJJ:c?+1-55h?@	A

4;;x()	*    rT)graphsreturns_graphc                 4    fd| D        }t        |      }|S )zRead graph in p2g format from path.

    Returns
    -------
    MultiDiGraph

    Notes
    -----
    If you want a DiGraph (with no self loops allowed and no edge data)
    use D=nx.DiGraph(read_p2g(path))
    c              3   @   K   | ]  }|j                          y w)N)decode).0liner   s     r   	<genexpr>zread_p2g.<locals>.<genexpr>J   s     4tT[["4s   )	parse_p2g)r   r   linesr   s    `  r   read_p2gr*   <   s     5t4E%AHr   c                    t        |       j                         }t        j                  |d      }t	        t
        t        |       j                               \  }}i }i }t        |      D ][  }t        |       j                         }|||<   |j                  |       t	        t
        t        |       j                               ||<   ] |D ]!  }||   D ]  }	|j                  |||	           # |S )z^Parse p2g format graph from string or iterable.

    Returns
    -------
    MultiDiGraph
    T)r   	selfloops)
nextstripnxMultiDiGraphmapintsplitr   add_nodeadd_edge)
r)   descriptionr   nnodesnedges	nodelabelnbrsir   r   s
             r   r(   r(   O   s     u+##%K
[D9Ad5k//12NFFID 6] 0K	!	

1c4;,,./Q	0  *7 	*CJJq)C.)	** Hr   )zutf-8)	__doc__networkxr/   networkx.utilsr   r   _dispatchabler*   r(    r   r   <module>rA      s    D  $ 13* *( 13T2 3 " T2 3r   