Back to index

python-biopython  1.60
test_Pathway.py
Go to the documentation of this file.
00001 # Copyright 2001 by Tarjei Mikkelsen.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 # python unittest framework
00007 import unittest
00008 import sys
00009 
00010 # modules to be tested
00011 from Bio.Pathway import Reaction
00012 from Bio.Pathway.Rep.Graph import Graph
00013 from Bio.Pathway.Rep.MultiGraph import MultiGraph
00014 
00015 
00016 class GraphTestCase(unittest.TestCase):
00017 
00018     def testEquals(self):
00019         a = Graph(['a','b','c'])
00020         a.add_edge('a','b','label1')
00021         a.add_edge('b','c','label1')
00022         a.add_edge('b','a','label2')
00023         b = Graph(['a','b','c'])
00024         self.assertNotEqual(a, b, "equal to similar nodes, no edges")
00025         b.add_edge('a','b','label1')
00026         self.assertNotEqual(a, b, "equal to similar nodes, edge subset")
00027         b.add_edge('b','c','label1')
00028         b.add_edge('b','a','label2')
00029         self.assertEqual(a, b, "not equal to similar")
00030         c = Graph(['a','b','c'])
00031         c.add_edge('a','b','label2')
00032         c.add_edge('b','c','label2')
00033         c.add_edge('b','a','label1')
00034         self.assertNotEqual(a, c, "equal to similar with different labels")
00035         self.assertNotEqual(c, Graph(), "equal to empty graph")
00036         self.assertEqual(Graph(), Graph(), "empty graph not equal to self")
00037 
00038     def testNodes(self):
00039         a = Graph()
00040         self.assertEqual(a.nodes(), [], "default graph not empty")
00041         a.add_node('a')
00042         self.assertEqual(a.nodes(), ['a'], "one node not added")
00043         a.add_node('a')
00044         self.assertEqual(a.nodes(), ['a'], "duplicate node added")
00045         a.add_node('b')
00046         l = a.nodes()
00047         l.sort()
00048         self.assertEqual(l, ['a', 'b'], "second node not added")
00049 
00050     def testEdges(self):
00051         a = Graph(['a','b','c','d'])
00052         a.add_edge('a','b','label1')
00053         self.assertEqual(a.child_edges('a'), [('b','label1')]) #, "incorrect child edges")
00054         a.add_edge('b','a','label2')
00055         self.assertEqual(a.parent_edges('a'), [('b','label2')]) #, "incorrect parent edges")
00056         a.add_edge('b','c','label3')
00057         self.assertEqual(a.parent_edges('c'), [('b','label3')]) #, "incorrect parent edges")
00058         l = a.children('b')
00059         l.sort()
00060         self.assertEqual(l, ['a', 'c'], "incorrect children")
00061         self.assertEqual(a.children('d'), [], "incorrect children for singleton")
00062         self.assertEqual(a.parents('a'), ['b'], "incorrect parents")
00063 
00064     def testRemoveNode(self):
00065         a = Graph(['a','b','c','d','e'])
00066         a.add_edge('a','e','label1')
00067         a.add_edge('b','e','label1')
00068         a.add_edge('c','e','label2')
00069         a.add_edge('d','e','label3')
00070         a.add_edge('e','d','label4')
00071         a.add_edge('a','b','label5')
00072         a.remove_node('e')
00073         b = Graph(['a','b','c','d'])
00074         b.add_edge('a','b','label5')
00075         self.assertEqual(a, b)#, "incorrect node removal")
00076 
00077 
00078 class MultiGraphTestCase(unittest.TestCase):
00079 
00080     def testEquals(self):
00081         a = MultiGraph(['a','b','c'])
00082         a.add_edge('a','b','label1')
00083         a.add_edge('b','c','label1')
00084         a.add_edge('b','a','label2')
00085         b = MultiGraph(['a','b','c'])
00086         self.assertNotEqual(a, b, "equal to similar nodes, no edges")
00087         b.add_edge('a','b','label1')
00088         self.assertNotEqual(a, b, "equal to similar nodes, edge subset")
00089         b.add_edge('b','c','label1')
00090         b.add_edge('b','a','label2')
00091         self.assertEqual(a, b, "not equal to similar")
00092         c = MultiGraph(['a','b','c'])
00093         c.add_edge('a','b','label2')
00094         c.add_edge('b','c','label2')
00095         c.add_edge('b','a','label1')
00096         self.assertNotEqual(a, c, "equal to similar with different labels")
00097         self.assertNotEqual(c, MultiGraph(), "equal to empty graph")
00098         self.assertEqual(MultiGraph(), MultiGraph(), "empty graph not equal to self")
00099 
00100     def testNodes(self):
00101         a = MultiGraph()
00102         self.assertEqual(a.nodes(), [], "default graph not empty")
00103         a.add_node('a')
00104         self.assertEqual(a.nodes(), ['a'], "one node not added")
00105         a.add_node('a')
00106         self.assertEqual(a.nodes(), ['a'], "duplicate node added")
00107         a.add_node('b')
00108         l = a.nodes()
00109         l.sort()
00110         self.assertEqual(l, ['a', 'b'], "second node not added")
00111 
00112     def testEdges(self):
00113         a = MultiGraph(['a','b','c','d'])
00114         a.add_edge('a','b','label1')
00115         self.assertEqual(a.child_edges('a'), [('b','label1')]) #, "incorrect child edges")
00116         a.add_edge('a','b','label2')
00117         l = a.child_edges('a')
00118         l.sort()
00119         self.assertEqual(l, [('b','label1'),('b','label2')]) #, "incorrect child edges")
00120         a.add_edge('b','a','label2')
00121         self.assertEqual(a.parent_edges('a'), [('b','label2')]) #, "incorrect parent edges")
00122         a.add_edge('b','c','label3')
00123         self.assertEqual(a.parent_edges('c'), [('b','label3')]) #, "incorrect parent edges")
00124         l = a.children('b')
00125         l.sort()
00126         self.assertEqual(l, ['a', 'c'], "incorrect children")
00127         self.assertEqual(a.children('d'), [], "incorrect children for singleton")
00128         self.assertEqual(a.parents('a'), ['b'], "incorrect parents")
00129 
00130     def testRemoveNode(self):
00131         a = MultiGraph(['a','b','c','d','e'])
00132         a.add_edge('a','e','label1')
00133         self.assertEqual(repr(a), "<MultiGraph: ('a': ('e', 'label1'))('b': )('c': )('d': )('e': )>")
00134         a.add_edge('b','e','label1')
00135         a.add_edge('c','e','label2')
00136         a.add_edge('d','e','label3')
00137         a.add_edge('e','d','label4')
00138         a.add_edge('a','b','label5')
00139         self.assertEqual(repr(a), "<MultiGraph: ('a': ('b', 'label5'),('e', 'label1'))('b': ('e', 'label1'))('c': ('e', 'label2'))('d': ('e', 'label3'))('e': ('d', 'label4'))>")
00140         a.remove_node('e')
00141         self.assertEqual(repr(a), "<MultiGraph: ('a': ('b', 'label5'))('b': )('c': )('d': )>")
00142         b = MultiGraph(['a','b','c','d'])
00143         b.add_edge('a','b','label5')
00144         self.assertEqual(repr(b), "<MultiGraph: ('a': ('b', 'label5'))('b': )('c': )('d': )>")
00145         self.assertEqual(repr(a), repr(b))
00146         self.assertEqual(a, b)#, "incorrect node removal")
00147 
00148         
00149 class ReactionTestCase(unittest.TestCase):
00150 
00151     def setUp(self):
00152         self.r_empty = Reaction()
00153         self.r_prod = Reaction({"a":1})
00154         self.r_dest = Reaction({"a":-1})
00155         self.r_1 = Reaction({"a":-1, "b":1})
00156         self.r_1i = Reaction({"a":-1, "b":1, "c":0})
00157         self.r_2 = Reaction({"b":-1, "c":1})
00158         self.r_3 = Reaction({"a":-1, "d":2})
00159         self.r_4 = Reaction({"c":-1, "d":-1, "a":1, "e":2})
00160 
00161     def testEq(self):
00162         self.assertEqual(self.r_1, self.r_1i) #, "not equal to similar")
00163         self.assertNotEqual(self.r_3, self.r_4) #, "equal to different")
00164         
00165     def testRev(self):
00166         self.assertEqual(self.r_empty.reverse(), self.r_empty, "empty reversed not empty")
00167         self.assertEqual(self.r_prod.reverse(), self.r_dest,
00168                           "reversed reaction not equal to similar")
00169         self.assertEqual(self.r_4.reverse(), Reaction({"c":1, "d":1, "a":-1, "e":-2}),
00170                          "reversed reaction not equal to similar")
00171         self.assertEqual(self.r_3.reverse().reverse(), self.r_3,
00172                           "double reversal not identity")
00173     
00174 
00175 if __name__ == "__main__":
00176     runner = unittest.TextTestRunner(verbosity = 2)
00177     unittest.main(testRunner=runner)