Back to index

python-biopython  1.60
Public Member Functions | Private Attributes
Bio.Pathway.Rep.Graph.Graph Class Reference

List of all members.

Public Member Functions

def __init__
def __eq__
def __ne__
def __repr__
def __str__
def add_node
def add_edge
def child_edges
def children
def edges
def labels
def nodes
def parent_edges
def parents
def remove_node
def remove_edge

Private Attributes

 _adjacency_list
 _label_map
 _edge_map

Detailed Description

A directed graph abstraction with labeled edges.

Definition at line 8 of file Graph.py.


Constructor & Destructor Documentation

def Bio.Pathway.Rep.Graph.Graph.__init__ (   self,
  nodes = [] 
)
Initializes a new Graph object.

Definition at line 11 of file Graph.py.

00011 
00012     def __init__(self, nodes = []):
00013         """Initializes a new Graph object."""
00014         self._adjacency_list = {}    # maps parent -> set of child objects
00015         for n in nodes:
00016             self._adjacency_list[n] = set()
00017         self._label_map = {}         # maps label -> set of (parent, child) pairs
00018         self._edge_map = {}          # maps (parent, child) pair -> label


Member Function Documentation

def Bio.Pathway.Rep.Graph.Graph.__eq__ (   self,
  g 
)
Returns true if g is equal to this graph.

Definition at line 19 of file Graph.py.

00019 
00020     def __eq__(self, g):
00021         """Returns true if g is equal to this graph."""
00022         return isinstance(g, Graph) and \
00023                (self._adjacency_list == g._adjacency_list) and \
00024                (self._label_map == g._label_map) and \
00025                (self._edge_map == g._edge_map)

Here is the caller graph for this function:

def Bio.Pathway.Rep.Graph.Graph.__ne__ (   self,
  g 
)
Returns true if g is not equal to this graph.

Definition at line 26 of file Graph.py.

00026 
00027     def __ne__(self, g):
00028         """Returns true if g is not equal to this graph."""
00029         return not self.__eq__(g)

Here is the call graph for this function:

Returns an unique string representation of this graph.

Definition at line 30 of file Graph.py.

00030 
00031     def __repr__(self):
00032         """Returns an unique string representation of this graph."""
00033         s = "<Graph: "
00034         keys = self._adjacency_list.keys()
00035         keys.sort()
00036         for key in keys:
00037             values = [(x,self._edge_map[(key,x)]) \
00038                       for x in self._adjacency_list[key].list()]
00039             values.sort()
00040             s = s + "(" + repr(key) + ": " + ",".join(map(repr, values)) + ")" 
00041         return s + ">"

Returns a concise string description of this graph.

Definition at line 42 of file Graph.py.

00042 
00043     def __str__(self):
00044         """Returns a concise string description of this graph."""
00045         nodenum = len(self._adjacency_list.keys())
00046         edgenum = reduce(lambda x,y: x+y,
00047                          map(len, self._adjacency_list.values()))
00048         labelnum = len(self._label_map.keys())
00049         return "<Graph: " + \
00050                str(nodenum) + " node(s), " + \
00051                str(edgenum) + " edge(s), " + \
00052                str(labelnum) + " unique label(s)>"

def Bio.Pathway.Rep.Graph.Graph.add_edge (   self,
  source,
  to,
  label = None 
)
Adds an edge to this graph.

Definition at line 58 of file Graph.py.

00058 
00059     def add_edge(self, source, to, label = None):
00060         """Adds an edge to this graph."""
00061         if source not in self._adjacency_list:
00062             raise ValueError("Unknown <from> node: " + str(source))
00063         if to not in self._adjacency_list:
00064             raise ValueError("Unknown <to> node: " + str(to))
00065         if (source,to) in self._edge_map:
00066             raise ValueError(str(source) + " -> " + str(to) + " exists")
00067         self._adjacency_list[source].add(to)
00068         if label not in self._label_map:
00069             self._label_map[label] = set()
00070         self._label_map[label].add((source,to))
00071         self._edge_map[(source,to)] = label

def Bio.Pathway.Rep.Graph.Graph.add_node (   self,
  node 
)
Adds a node to this graph.

Definition at line 53 of file Graph.py.

00053 
00054     def add_node(self, node):
00055         """Adds a node to this graph."""
00056         if node not in self._adjacency_list:
00057             self._adjacency_list[node] = set()

def Bio.Pathway.Rep.Graph.Graph.child_edges (   self,
  parent 
)
Returns a list of (child, label) pairs for parent.

Definition at line 72 of file Graph.py.

00072 
00073     def child_edges(self, parent):
00074         """Returns a list of (child, label) pairs for parent."""
00075         if parent not in self._adjacency_list:
00076             raise ValueError("Unknown <parent> node: " + str(parent))
00077         return [(x, self._edge_map[(parent,x)]) \
00078                 for x in sorted(self._adjacency_list[parent])]

Here is the caller graph for this function:

def Bio.Pathway.Rep.Graph.Graph.children (   self,
  parent 
)
Returns a list of unique children for parent.

Definition at line 79 of file Graph.py.

00079 
00080     def children(self, parent):
00081         """Returns a list of unique children for parent."""
00082         return sorted(self._adjacency_list[parent])

def Bio.Pathway.Rep.Graph.Graph.edges (   self,
  label 
)
Returns a list of all the edges with this label.

Definition at line 83 of file Graph.py.

00083 
00084     def edges(self, label):
00085         """Returns a list of all the edges with this label."""
00086         if label not in self._label_map:
00087             raise ValueError("Unknown label: " + str(label))
00088         return self._label_map[label].list()

Returns a list of all the edge labels in this graph.

Definition at line 89 of file Graph.py.

00089 
00090     def labels(self):
00091         """Returns a list of all the edge labels in this graph."""
00092         return self._label_map.keys()

Returns a list of the nodes in this graph.

Definition at line 93 of file Graph.py.

00093 
00094     def nodes(self):
00095         """Returns a list of the nodes in this graph."""
00096         return self._adjacency_list.keys()

def Bio.Pathway.Rep.Graph.Graph.parent_edges (   self,
  child 
)
Returns a list of (parent, label) pairs for child.

Definition at line 97 of file Graph.py.

00097 
00098     def parent_edges(self, child):
00099         """Returns a list of (parent, label) pairs for child."""
00100         if child not in self._adjacency_list:
00101             raise ValueError("Unknown <child> node: " + str(child))
00102         parents = []
00103         for parent, children in self._adjacency_list.iteritems():
00104             for x in children:
00105                 if x is child:
00106                     parents.append((parent, self._edge_map[(parent, child)]))
00107         return sorted(parents)

Here is the caller graph for this function:

def Bio.Pathway.Rep.Graph.Graph.parents (   self,
  child 
)
Returns a list of unique parents for child.

Definition at line 108 of file Graph.py.

00108 
00109     def parents(self, child):
00110         """Returns a list of unique parents for child."""
00111         return sorted(set([x[0] for x in self.parent_edges(child)]))

Here is the call graph for this function:

def Bio.Pathway.Rep.Graph.Graph.remove_edge (   self,
  parent,
  child,
  label 
)
Removes edge. -- NOT IMPLEMENTED

Definition at line 136 of file Graph.py.

00136 
00137     def remove_edge(self, parent, child, label):
00138         """Removes edge. -- NOT IMPLEMENTED"""
00139         # hm , this is a multigraph - how should this be implemented?
00140         raise NotImplementedError("remove_edge is not yet implemented")
00141 
00142 
00143 
def Bio.Pathway.Rep.Graph.Graph.remove_node (   self,
  node 
)
Removes node and all edges connected to it.

Definition at line 112 of file Graph.py.

00112 
00113     def remove_node(self, node):
00114         """Removes node and all edges connected to it."""
00115         if node not in self._adjacency_list:
00116             raise ValueError("Unknown node: " + str(node))
00117         # remove node (and all out-edges) from adjacency list
00118         del self._adjacency_list[node]
00119         # remove all in-edges from adjacency list
00120         for n in self._adjacency_list.keys():
00121             self._adjacency_list[n] = set(x for x in self._adjacency_list[n] \
00122                                           if x is not node)
00123         # remove all refering pairs in label map
00124         for label in self._label_map.keys():
00125             lm = set(x for x in self._label_map[label] \
00126                      if (x[0] is not node) and (x[1] is not node))
00127             # remove the entry completely if the label is now unused
00128             if lm:
00129                 self._label_map[label] = lm
00130             else:
00131                 del self._label_map[label]
00132         # remove all refering entries in edge map
00133         for edge in self._edge_map.keys():
00134             if edge[0] is node or edge[1] is node:
00135                 del self._edge_map[edge]
        

Member Data Documentation

Definition at line 13 of file Graph.py.

Definition at line 17 of file Graph.py.

Definition at line 16 of file Graph.py.


The documentation for this class was generated from the following file: