p2g.py :  » Network » NetworkX » networkx-1.1 » networkx » readwrite » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » NetworkX 
NetworkX » networkx 1.1 » networkx » readwrite » p2g.py
"""
This module provides the following: read and write of p2g format 
used in metabolic pathway studies.

See 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.

"""
__author__ = """Willem Ligtenberg (w.p.a.ligtenberg@tue.nl)\n Aric Hagberg (hagberg@lanl.gov)"""
__date__ = """2008-05-27"""
#    Copyright (C) 2008 by 
#    Aric Hagberg <hagberg@lanl.gov>
#    Dan Schult <dschult@colgate.edu>
#    Pieter Swart <swart@lanl.gov>
#    All rights reserved.
#    BSD license.

import networkx
from networkx.utils import is_string_like,_get_fh

def write_p2g(G, path):
    """Write NetworkX graph in p2g format.

    This format is meant to be used with directed graphs with
    possible self loops.
    """
    
    fh=_get_fh(path,mode='w')

    fh.write("%s\n"%G.name)
    fh.write("%s %s\n"%(G.order(),G.size()))

    nodes = G.nodes()

    # make dictionary mapping nodes to integers
    nodenumber=dict(zip(nodes,range(len(nodes)))) 

    for n in nodes:
        fh.write("%s\n"%n)
        for nbr in G.neighbors(n):
            fh.write("%s "%nodenumber[nbr])
        fh.write("\n")
    fh.close()

def read_p2g(path):
    """Read graph in p2g format from path. Returns an XDiGraph.

    If you want a DiGraph (with no self loops allowed and no edge data)
    use D=networkx.DiGraph(read_p2g(path))

    """
    fh=_get_fh(path,mode='r')        
    G=parse_p2g(fh)
    return G

def parse_p2g(lines):
    """Parse p2g format graph from string or iterable. Returns an XDiGraph."""
    if is_string_like(lines): lines=iter(lines.split('\n'))
    lines = iter([line.rstrip('\n') for line in lines])

    description = lines.next()
    # are multiedges (parallel edges) allowed?
    G=networkx.XDiGraph(name=description,selfloops=True)
    nnodes,nedges=map(int,lines.next().split())

    nodelabel={}
    nbrs={}
    # loop over the nodes keeping track of node labels and out neighbors
    # defer adding edges until all node labels are known
    for i in range(nnodes):
        n=lines.next()
        nodelabel[i]=n
        G.add_node(n)
        nbrs[n]=map(int,lines.next().split())

    # now we know all of the node labels so we can add the edges
    # with the correct labels        
    for n in G:
        for nbr in nbrs[n]:
            G.add_edge(n,nodelabel[nbr])
    return G
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.