Back to index

python-biopython  1.60
test_Phylo_depend.py
Go to the documentation of this file.
00001 # Copyright (C) 2009 by Eric Talevich (eric.talevich@gmail.com)
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 """Unit tests for Bio.Phylo functions with external dependencies."""
00007 
00008 # Check for any missing dependencies at the top level so we can skip
00009 from Bio import MissingExternalDependencyError
00010 
00011 try:
00012     import matplotlib
00013 except ImportError:
00014     raise MissingExternalDependencyError(
00015             "Install matplotlib if you want to use Bio.Phylo._utils.")
00016 else:
00017     # Don't use the Wx backend for matplotlib, use the simpler postscript
00018     # backend -- we're not going to display or save the plot anyway, so it
00019     # doesn't matter much, as long as it's not Wx.  See:
00020     # http://lists.open-bio.org/pipermail/biopython-dev/2012-April/009559.html
00021     matplotlib.use('ps')
00022     from matplotlib import pyplot
00023 
00024 try:
00025     import networkx
00026 except ImportError:
00027     raise MissingExternalDependencyError(
00028             "Install NetworkX if you want to use Bio.Phylo._utils.")
00029 
00030 try:
00031     import pygraphviz
00032 except ImportError:
00033     try:
00034         import pydot
00035     except ImportError:
00036         raise MissingExternalDependencyError(
00037                 "Install PyGraphviz or Pydot if you want to use "
00038                 "Bio.Phylo._utils.")
00039 
00040 # OK, we can go ahead
00041 import unittest
00042 from cStringIO import StringIO
00043 
00044 from Bio import Phylo
00045 
00046 # Example PhyloXML file
00047 EX_DOLLO = 'PhyloXML/o_tol_332_d_dollo.xml'
00048 EX_APAF = 'PhyloXML/apaf.xml'
00049 
00050 class UtilTests(unittest.TestCase):
00051     """Tests for various utility functions."""
00052 
00053     def test_draw(self):
00054         """Run the tree layout algorithm, but don't display it."""
00055         pyplot.ioff()   # Turn off interactive display
00056         dollo = Phylo.read(EX_DOLLO, 'phyloxml')
00057         apaf = Phylo.read(EX_APAF, 'phyloxml')
00058         Phylo.draw(dollo, do_show=False)
00059         Phylo.draw(apaf, do_show=False)
00060         # Fancier options
00061         Phylo.draw(apaf, do_show=False, branch_labels={apaf.root: 'Root'})
00062         Phylo.draw(apaf, do_show=False, branch_labels=lambda c: c.branch_length)
00063 
00064     def test_draw_ascii(self):
00065         """Tree to Graph conversion, if networkx is available."""
00066         handle = StringIO()
00067         tree = Phylo.read(EX_APAF, 'phyloxml')
00068         Phylo.draw_ascii(tree, file=handle)
00069         Phylo.draw_ascii(tree, file=handle, column_width=120)
00070         handle.close()
00071 
00072     def test_to_networkx(self):
00073         """Tree to Graph conversion, if networkx is available."""
00074         tree = Phylo.read(EX_DOLLO, 'phyloxml')
00075         G = Phylo.to_networkx(tree)
00076         self.assertEqual(len(G.nodes()), 659)
00077 
00078 
00079 # ---------------------------------------------------------
00080 
00081 if __name__ == '__main__':
00082     runner = unittest.TextTestRunner(verbosity=2)
00083     unittest.main(testRunner=runner)