Attachment 'Network.py'

## Copyright 2007 by Jake Feala jfeala@gmail.com

from networkx import XGraph,XDiGraph
from networkx.operators import union

def create_network(directed=False):
    """Generates Network object derived from a directed or undirected NetworkX Graph class """
    if not directed:
        GraphClass = XGraph
    else:
        GraphClass = XDiGraph
        
    class Network(GraphClass):
        """Biological network based on NetworkX XGraph class.  This wrapper
        bundles biological annotations (from InteractionRecord) with the
        graph representation and offers compatibility with Biopython, SBML,
        and Cytoscape
        """
        def __init__(self):
            """Initializes a new Network object."""
            super(Network,self).__init__(selfloops=True)
            self.__interaction_recs = {}

        def __str__(self):
            """Returns a string representation of this network."""
            return "Network of " + str(len(self.nodes())) + " species and " + \
                   str(len(self.interactions())) + " interactions."        

        def add_interaction(self, interaction):
            """Adds an InteractionRecord to this network."""
            self.add_edge(interaction.source, interaction.target, interaction.id)
            self.__interaction_recs[interaction.id] = interaction

        def load(self,rec_iterator):
            for rec in rec_iterator:
                self.add_interaction(rec)
            
        def interactions(self,edge_list=None):
            """Returns list of InteractionRecords in this network. If no edge list is specified,
            then all interactions are returned.
            """
            if edge_list is None:
                edges = self.edges()
            interaction_ids = [edge[2] for edge in edges]
            return [self.__interaction_recs[id] for id in interaction_ids]

    return Network()


def merge(G,H):
    """Merge two biological networks, checking aliases when merging nodes"""
    pass
    ## needs work...
##    all_interactions = G.interactions() + H.interactions()
##    new_interactions = []
##    new_graph = union(G,H)
##    for edge in new_graph.edges():
##        interaction_id = edge[2]
##        new_interactions.append(all_interactions[interaction_id])
##    new_network = Network()
##    new_network.load(new_interactions)
##    return new_network
        

You are not allowed to view this page.