Back to index

python-biopython  1.60
test_PAML_tools.py
Go to the documentation of this file.
00001 # Copyright (C) 2011 by Brandon Invergo (b.invergo@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 import unittest
00007 import os
00008 import sys
00009 from Bio.Phylo.PAML import codeml, baseml, yn00
00010 from Bio import MissingExternalDependencyError
00011 
00012 def is_exe(filepath):
00013     """Test if a file is an executable."""
00014     return os.path.exists(filepath) and os.access(filepath, os.X_OK)
00015 
00016 def which(program):
00017     """Find the path to an executable."""
00018     filepath, filename = os.path.split(program)
00019     os_path = os.environ["PATH"].split(os.pathsep)
00020     if sys.platform == "win32":
00021         try:
00022             #This can vary depending on the Windows language.
00023             prog_files = os.environ["PROGRAMFILES"]
00024         except KeyError:
00025             prog_files = r"C:\Program Files"
00026         #For Windows, the user is instructed to move the programs to a folder
00027         #and then to add the folder to the system path. Just in case they didn't
00028         #do that, we can check for it in Program Files.
00029         likely_dirs = ["", #Current dir
00030                        prog_files,
00031                        os.path.join(prog_files, "paml41"),
00032                        os.path.join(prog_files, "paml43"),
00033                        os.path.join(prog_files, "paml44"),
00034                        os.path.join(prog_files, "paml45")] + sys.path
00035         os_path.extend(likely_dirs)
00036     for path in os.environ["PATH"].split(os.pathsep):
00037         exe_file = os.path.join(path, program)
00038         if is_exe(exe_file):
00039             return exe_file
00040     return None
00041 
00042 #Find the PAML binaries
00043 if sys.platform == "win32":
00044     binaries = ["codeml.exe", "baseml.exe", "yn00.exe"]
00045 else:
00046     binaries = ["codeml", "baseml", "yn00"]
00047 for binary in binaries:
00048     if which(binary) is None:
00049         raise MissingExternalDependencyError(\
00050             "Install PAML if you want to use the Bio.Phylo.PAML wrapper.")
00051 
00052 
00053 class Common(unittest.TestCase):
00054     """Base class for PAML unit tests."""
00055 
00056     del_files = []
00057 
00058     def __del__(self):
00059         """Just in case tool creates some junk files, do a clean-up."""
00060         del_files = self.del_files
00061         for filename in del_files:
00062             if os.path.exists(filename):
00063                 os.remove(filename)
00064 
00065 
00066 class CodemlTest(Common):
00067     "Tests for PAML tool codeml."""
00068 
00069     def setUp(self):
00070         self.cml = codeml.Codeml()
00071 
00072     def testCodemlBinary(self):
00073         """Test that the codeml binary runs and generates correct output
00074         and is the correct version.
00075         """
00076         ctl_file = os.path.join("PAML", "Control_files", "codeml", "codeml.ctl")
00077         self.cml.read_ctl_file(ctl_file)
00078         self.cml.alignment = os.path.join("PAML", "Alignments", "alignment.phylip")
00079         self.cml.tree = os.path.join("PAML", "Trees", "species.tree")
00080         self.cml.out_file = os.path.join("PAML", "temp.out")
00081         self.cml.working_dir = os.path.join("PAML", "codeml_test")
00082         results = self.cml.run()
00083         self.assertTrue(results["version"] > "4.0")
00084         self.assertTrue("NSsites" in results)
00085         self.assertEqual(len(results["NSsites"]), 1)
00086         self.assertEqual(len(results["NSsites"][0]), 5)
00087 
00088 
00089 class BasemlTest(Common):
00090     """Tests for PAML tool baseml."""
00091 
00092     def setUp(self):
00093         self.bml = baseml.Baseml()
00094 
00095     def testBasemlBinary(self):
00096         """Test that the baseml binary runs and generates correct output
00097         and is the correct version.
00098         """
00099         ctl_file = os.path.join("PAML", "Control_files", "baseml", "baseml.ctl")
00100         self.bml.read_ctl_file(ctl_file)
00101         self.bml.alignment = os.path.join("PAML", "Alignments", "alignment.phylip")
00102         self.bml.tree = os.path.join("PAML", "Trees", "species.tree")
00103         self.bml.out_file = os.path.join("PAML", "temp.out")
00104         self.bml.working_dir = os.path.join("PAML", "baseml_test")
00105         results = self.bml.run()
00106         self.assertTrue(results["version"] > "4.0")
00107         self.assertTrue("parameters" in results)
00108         self.assertEqual(len(results["parameters"]), 5)
00109 
00110 
00111 class Yn00Test(Common):
00112     """Tests for PAML tool yn00."""
00113 
00114     def setUp(self):
00115         self.yn = yn00.Yn00()
00116 
00117     def testYn00Binary(self):
00118         """Test that the yn00 binary runs and generates correct output.
00119         yn00 output does not specify the version number.
00120         """
00121         ctl_file = os.path.join("PAML", "Control_files", "yn00", "yn00.ctl")
00122         self.yn.read_ctl_file(ctl_file)
00123         self.yn.alignment = os.path.join("PAML", "Alignments", "alignment.phylip")
00124         self.yn.out_file = os.path.join("PAML", "temp.out")
00125         self.yn.working_dir = os.path.join("PAML", "yn00_test")
00126         results = self.yn.run()
00127         self.assertEqual(len(results), 5)
00128 
00129 
00130 if __name__ == "__main__":
00131     runner = unittest.TextTestRunner(verbosity = 2)
00132     unittest.main(testRunner=runner)
00133     clean_up()