Back to index

python-biopython  1.60
Public Member Functions | Public Attributes
test_Phylo.MixinTests Class Reference

List of all members.

Public Member Functions

def setUp
def test_find_elements
def test_find_clades
def test_find_terminal
def test_get_path
def test_trace
def test_common_ancestor
def test_depths
def test_distance
def test_is_bifurcating
def test_is_monophyletic
def test_total_branch_length
def test_collapse
def test_collapse_all
def test_ladderize
def test_prune
def test_split

Public Attributes

 phylogenies

Detailed Description

Tests for TreeMixin methods.

Definition at line 154 of file test_Phylo.py.


Member Function Documentation

Definition at line 156 of file test_Phylo.py.

00156 
00157     def setUp(self):
00158         self.phylogenies = list(Phylo.parse(EX_PHYLO, 'phyloxml'))

TreeMixin: collapse() method.

Definition at line 303 of file test_Phylo.py.

00303 
00304     def test_collapse(self):
00305         """TreeMixin: collapse() method."""
00306         tree = self.phylogenies[1]
00307         parent = tree.collapse(tree.clade[0])
00308         self.assertEqual(len(parent), 3)
00309         for clade, name, blength in zip(parent,
00310                 ('C', 'A', 'B'),
00311                 (0.4, 0.162, 0.29)):
00312             self.assertEqual(clade.name, name)
00313             self.assertAlmostEqual(clade.branch_length, blength)

TreeMixin: collapse_all() method.

Definition at line 314 of file test_Phylo.py.

00314 
00315     def test_collapse_all(self):
00316         """TreeMixin: collapse_all() method."""
00317         tree = Phylo.read(EX_APAF, 'phyloxml')
00318         d1 = tree.depths()
00319         tree.collapse_all()
00320         d2 = tree.depths()
00321         # Total branch lengths should not change
00322         for clade in d2:
00323             self.assertAlmostEqual(d1[clade], d2[clade])
00324         # No internal nodes should remain except the root
00325         self.assertEqual(len(tree.get_terminals()), len(tree.clade))
00326         self.assertEqual(len(list(tree.find_clades(terminal=False))), 1)
00327         # Again, with a target specification
00328         tree = Phylo.read(EX_APAF, 'phyloxml')
00329         d1 = tree.depths()
00330         internal_node_ct = len(tree.get_nonterminals())
00331         tree.collapse_all(lambda c: c.branch_length < 0.1)
00332         d2 = tree.depths()
00333         # Should have collapsed 7 internal nodes
00334         self.assertEqual(len(tree.get_nonterminals()), internal_node_ct - 7)
00335         for clade in d2:
00336             self.assertAlmostEqual(d1[clade], d2[clade])

TreeMixin: common_ancestor() method.

Definition at line 245 of file test_Phylo.py.

00245 
00246     def test_common_ancestor(self):
00247         """TreeMixin: common_ancestor() method."""
00248         tree = self.phylogenies[1]
00249         lca = tree.common_ancestor('A', 'B')
00250         self.assertEqual(lca, tree.clade[0])
00251         lca = tree.common_ancestor('A', 'C')
00252         self.assertEqual(lca, tree.clade)
00253         tree = self.phylogenies[10]
00254         lca = tree.common_ancestor('A', 'B', 'C')
00255         self.assertEqual(lca, tree.clade[0])

TreeMixin: depths() method.

Definition at line 256 of file test_Phylo.py.

00256 
00257     def test_depths(self):
00258         """TreeMixin: depths() method."""
00259         tree = self.phylogenies[1]
00260         depths = tree.depths()
00261         self.assertEqual(len(depths), 5)
00262         for found, expect in zip(sorted(depths.values()),
00263                                  [0, 0.060, 0.162, 0.290, 0.400]):
00264             self.assertAlmostEqual(found, expect)

TreeMixin: distance() method.

Definition at line 265 of file test_Phylo.py.

00265 
00266     def test_distance(self):
00267         """TreeMixin: distance() method."""
00268         t = self.phylogenies[1]
00269         self.assertAlmostEqual(t.distance('A'), 0.162)
00270         self.assertAlmostEqual(t.distance('B'), 0.29)
00271         self.assertAlmostEqual(t.distance('C'), 0.4)
00272         self.assertAlmostEqual(t.distance('A', 'B'), 0.332)
00273         self.assertAlmostEqual(t.distance('A', 'C'), 0.562)
00274         self.assertAlmostEqual(t.distance('B', 'C'), 0.69)

TreeMixin: find_clades() method.

Definition at line 195 of file test_Phylo.py.

00195 
00196     def test_find_clades(self):
00197         """TreeMixin: find_clades() method."""
00198         # boolean filter
00199         for clade, name in zip(self.phylogenies[10].find_clades(name=True),
00200                                 list('ABCD')):
00201             self.assertTrue(isinstance(clade, PhyloXML.Clade))
00202             self.assertEqual(clade.name, name)
00203         # finding deeper attributes
00204         octo = list(self.phylogenies[5].find_clades(code='OCTVU'))
00205         self.assertEqual(len(octo), 1)
00206         self.assertTrue(isinstance(octo[0], PhyloXML.Clade))
00207         self.assertEqual(octo[0].taxonomies[0].code, 'OCTVU')
00208         # string filter
00209         dee = self.phylogenies[10].find_clades('D').next()
00210         self.assertEqual(dee.name, 'D')
00211 

TreeMixin: find_elements() method.

Definition at line 161 of file test_Phylo.py.

00161 
00162     def test_find_elements(self):
00163         """TreeMixin: find_elements() method."""
00164         # From the docstring example
00165         tree = self.phylogenies[5]
00166         matches = list(tree.find_elements(PhyloXML.Taxonomy, code='OCTVU'))
00167         self.assertEqual(len(matches), 1)
00168         self.assertTrue(isinstance(matches[0], PhyloXML.Taxonomy))
00169         self.assertEqual(matches[0].code, 'OCTVU')
00170         self.assertEqual(matches[0].scientific_name, 'Octopus vulgaris')
00171         # Iteration and regexps
00172         tree = self.phylogenies[10]
00173         for point, alt in zip(tree.find_elements(geodetic_datum=r'WGS\d{2}'),
00174                                (472, 10, 452)):
00175             self.assertTrue(isinstance(point, PhyloXML.Point))
00176             self.assertEqual(point.geodetic_datum, 'WGS84')
00177             self.assertAlmostEqual(point.alt, alt)
00178         # class filter
00179         tree = self.phylogenies[4]
00180         events = list(tree.find_elements(PhyloXML.Events))
00181         self.assertEqual(len(events), 2)
00182         self.assertEqual(events[0].speciations, 1)
00183         self.assertEqual(events[1].duplications, 1)
00184         # string filter & find_any
00185         tree = self.phylogenies[3]
00186         taxonomy = tree.find_any("B. subtilis")
00187         self.assertEqual(taxonomy.scientific_name, "B. subtilis")
00188         # integer filter
00189         tree = Phylo.read(EX_APAF, 'phyloxml')
00190         domains = list(tree.find_elements(start=5))
00191         self.assertEqual(len(domains), 8)
00192         for dom in domains:
00193             self.assertEqual(dom.start, 5)
00194             self.assertEqual(dom.value, 'CARD')

TreeMixin: find_elements() with terminal argument.

Definition at line 212 of file test_Phylo.py.

00212 
00213     def test_find_terminal(self):
00214         """TreeMixin: find_elements() with terminal argument."""
00215         for tree, total, extern, intern in zip(
00216                 self.phylogenies,
00217                 (6, 6, 7, 18, 21, 27, 7, 9, 9, 19, 15, 9, 6),
00218                 (3, 3, 3, 3,  3,  3,  3, 3, 3, 3,  4,  3, 3),
00219                 (3, 3, 3, 3,  3,  3,  3, 3, 3, 3,  3,  3, 3),
00220                 ):
00221             self.assertEqual(len(list(tree.find_elements())), total)
00222             self.assertEqual(len(list(tree.find_elements(terminal=True))),
00223                              extern)
00224             self.assertEqual(len(list(tree.find_elements(terminal=False))),
00225                              intern)

TreeMixin: get_path() method.

Definition at line 226 of file test_Phylo.py.

00226 
00227     def test_get_path(self):
00228         """TreeMixin: get_path() method."""
00229         path = self.phylogenies[1].get_path('B')
00230         self.assertEqual(len(path), 2)
00231         self.assertAlmostEqual(path[0].branch_length, 0.06)
00232         self.assertAlmostEqual(path[1].branch_length, 0.23)
00233         self.assertEqual(path[1].name, 'B')

TreeMixin: is_bifurcating() method.

Definition at line 275 of file test_Phylo.py.

00275 
00276     def test_is_bifurcating(self):
00277         """TreeMixin: is_bifurcating() method."""
00278         for tree, is_b in zip(self.phylogenies,
00279                 (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1)):
00280             self.assertEqual(tree.is_bifurcating(), is_b)

TreeMixin: is_monophyletic() method.

Definition at line 281 of file test_Phylo.py.

00281 
00282     def test_is_monophyletic(self):
00283         """TreeMixin: is_monophyletic() method."""
00284         tree = self.phylogenies[10]
00285         abcd = tree.get_terminals()
00286         abc = tree.clade[0].get_terminals()
00287         ab = abc[:2]
00288         d = tree.clade[1].get_terminals()
00289         self.assertEqual(tree.is_monophyletic(abcd), tree.root)
00290         self.assertEqual(tree.is_monophyletic(abc), tree.clade[0])
00291         self.assertEqual(tree.is_monophyletic(ab), False)
00292         self.assertEqual(tree.is_monophyletic(d), tree.clade[1])
00293         # Alternate argument form
00294         self.assertEqual(tree.is_monophyletic(*abcd), tree.root)

TreeMixin: ladderize() method.

Definition at line 337 of file test_Phylo.py.

00337 
00338     def test_ladderize(self):
00339         """TreeMixin: ladderize() method."""
00340         def ordered_names(tree):
00341             return [n.name for n in tree.get_terminals()]
00342         tree = self.phylogenies[10]
00343         self.assertEqual(ordered_names(tree), list('ABCD'))
00344         tree.ladderize()
00345         self.assertEqual(ordered_names(tree), list('DABC'))
00346         tree.ladderize(reverse=True)
00347         self.assertEqual(ordered_names(tree), list('ABCD'))

TreeMixin: prune() method.

Definition at line 348 of file test_Phylo.py.

00348 
00349     def test_prune(self):
00350         """TreeMixin: prune() method."""
00351         tree = self.phylogenies[10]
00352         # Taxon in a trifurcation -- no collapse afterward
00353         parent = tree.prune(name='B')
00354         self.assertEqual(len(parent.clades), 2)
00355         self.assertEqual(parent.clades[0].name, 'A')
00356         self.assertEqual(parent.clades[1].name, 'C')
00357         self.assertEqual(len(tree.get_terminals()), 3)
00358         self.assertEqual(len(tree.get_nonterminals()), 2)
00359         # Taxon in a bifurcation -- collapse
00360         tree = self.phylogenies[0]
00361         parent = tree.prune(name='A')
00362         self.assertEqual(len(parent.clades), 2)
00363         for clade, name, blen in zip(parent, 'BC', (.29, .4)):
00364             self.assertTrue(clade.is_terminal())
00365             self.assertEqual(clade.name, name)
00366             self.assertAlmostEqual(clade.branch_length, blen)
00367         self.assertEqual(len(tree.get_terminals()), 2)
00368         self.assertEqual(len(tree.get_nonterminals()), 1)
00369         # Taxon just below the root -- don't screw up
00370         tree = self.phylogenies[1]
00371         parent = tree.prune(name='C')
00372         self.assertEqual(parent, tree.root)
00373         self.assertEqual(len(parent.clades), 2)
00374         for clade, name, blen in zip(parent, 'AB', (.102, .23)):
00375             self.assertTrue(clade.is_terminal())
00376             self.assertEqual(clade.name, name)
00377             self.assertAlmostEqual(clade.branch_length, blen)
00378         self.assertEqual(len(tree.get_terminals()), 2)
00379         self.assertEqual(len(tree.get_nonterminals()), 1)

TreeMixin: split() method.

Definition at line 380 of file test_Phylo.py.

00380 
00381     def test_split(self):
00382         """TreeMixin: split() method."""
00383         tree = self.phylogenies[0]
00384         C = tree.clade[1]
00385         C.split()
00386         self.assertEqual(len(C), 2)
00387         self.assertEqual(len(tree.get_terminals()), 4)
00388         self.assertEqual(len(tree.get_nonterminals()), 3)
00389         C[0].split(3, .5)
00390         self.assertEqual(len(tree.get_terminals()), 6)
00391         self.assertEqual(len(tree.get_nonterminals()), 4)
00392         for clade, name, blen in zip(C[0],
00393                 ('C00', 'C01', 'C02'),
00394                 (0.5, 0.5, 0.5)):
00395             self.assertTrue(clade.is_terminal())
00396             self.assertEqual(clade.name, name)
00397             self.assertEqual(clade.branch_length, blen)
00398 
00399 
00400 # ---------------------------------------------------------

TreeMixin: total_branch_length() method.

Definition at line 295 of file test_Phylo.py.

00295 
00296     def test_total_branch_length(self):
00297         """TreeMixin: total_branch_length() method."""
00298         tree = self.phylogenies[1]
00299         self.assertAlmostEqual(tree.total_branch_length(), 0.792)
00300         self.assertAlmostEqual(tree.clade[0].total_branch_length(), 0.392)

TreeMixin: trace() method.

Definition at line 234 of file test_Phylo.py.

00234 
00235     def test_trace(self):
00236         """TreeMixin: trace() method."""
00237         tree = self.phylogenies[1]
00238         path = tree.trace('A', 'C')
00239         self.assertEqual(len(path), 3)
00240         self.assertAlmostEqual(path[0].branch_length, 0.06)
00241         self.assertAlmostEqual(path[2].branch_length, 0.4)
00242         self.assertEqual(path[2].name, 'C')


Member Data Documentation

Definition at line 157 of file test_Phylo.py.


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