Back to index

python-biopython  1.60
test_PAML_yn00.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 os.path
00009 from Bio.Phylo.PAML import yn00
00010 from Bio.Phylo.PAML._paml import PamlError
00011 
00012 class ModTest(unittest.TestCase):
00013     
00014     align_dir = os.path.join("PAML", "Alignments")
00015     tree_dir = os.path.join("PAML", "Trees")
00016     ctl_dir = os.path.join("PAML", "Control_files")
00017     results_dir = os.path.join("PAML", "Results")
00018     working_dir = os.path.join("PAML", "yn00_test")
00019 
00020     align_file = os.path.join(align_dir, "alignment.phylip")
00021     out_file = os.path.join(results_dir, "test.out")
00022     results_file = os.path.join(results_dir, "bad_results.out")
00023     bad_ctl_file1 = os.path.join(ctl_dir, "bad1.ctl")
00024     bad_ctl_file2 = os.path.join(ctl_dir, "bad2.ctl")
00025     ctl_file = os.path.join(ctl_dir, "yn00", "yn00.ctl")
00026     
00027     def tearDown(self):
00028         """Just in case yn00 creates some junk files, do a clean-up."""
00029         del_files = [self.out_file, "2YN.dN", "2YN.dS", "2YN.t", "rst",
00030             "rst1", "rub"]
00031         for filename in del_files:
00032             if os.path.exists(filename):
00033                 os.remove(filename)
00034         if os.path.exists(self.working_dir):
00035             for filename in os.listdir(self.working_dir):
00036                 filepath = os.path.join(self.working_dir, filename)
00037                 os.remove(filepath)
00038             os.rmdir(self.working_dir)
00039     
00040     def setUp(self):
00041         self.yn00 = yn00.Yn00()
00042         
00043     def testAlignmentFileIsValid(self):
00044         self.assertRaises((AttributeError, TypeError),
00045             yn00.Yn00, alignment = 1)
00046         self.yn00.alignment = 1
00047         self.yn00.out_file = self.out_file
00048         self.assertRaises((AttributeError, TypeError),
00049             self.yn00.run)
00050         
00051     def testAlignmentExists(self):
00052         self.assertRaises((EnvironmentError, IOError), yn00.Yn00, 
00053             alignment = "nonexistent")
00054         self.yn00.alignment = "nonexistent"
00055         self.yn00.out_file = self.out_file
00056         self.assertRaises(IOError, self.yn00.run)
00057 
00058     def testWorkingDirValid(self):
00059         self.yn00.alignment = self.align_file
00060         self.yn00.out_file = self.out_file
00061         self.yn00.working_dir = 1
00062         self.assertRaises((AttributeError, TypeError),
00063             self.yn00.run)
00064     
00065     def testOutputFileValid(self):
00066         self.yn00.alignment = self.align_file
00067         self.yn00.out_file = 1
00068         self.assertRaises((AttributeError, TypeError),
00069             self.yn00.run)
00070     
00071     def testOptionExists(self):
00072         self.assertRaises((AttributeError, KeyError),
00073                           self.yn00.set_options, xxxx=1)
00074         self.assertRaises((AttributeError, KeyError),
00075             self.yn00.get_option, "xxxx")
00076     
00077     def testAlignmentSpecified(self):
00078         self.yn00.out_file = self.out_file
00079         self.assertRaises((AttributeError, ValueError),
00080             self.yn00.run)
00081         
00082     def testOutputFileSpecified(self):
00083         self.yn00.alignment = self.align_file
00084         self.assertRaises((AttributeError, ValueError),
00085             self.yn00.run)
00086         
00087     #def testPamlErrorsCaught(self):
00088         #self.yn00.alignment = self.align_file
00089         #self.yn00.out_file = self.out_file
00090         #self.assertRaises((EnvironmentError, PamlError),
00091             #self.yn00.run)
00092         
00093     def testCtlFileValidOnRun(self):
00094         self.yn00.alignment = self.align_file
00095         self.yn00.out_file = self.out_file
00096         self.assertRaises((AttributeError, TypeError),
00097             self.yn00.run, ctl_file = 1)
00098         
00099     def testCtlFileExistsOnRun(self):
00100         self.yn00.alignment = self.align_file
00101         self.yn00.out_file = self.out_file
00102         self.assertRaises(IOError,
00103             self.yn00.run, ctl_file = "nonexistent")
00104             
00105     def testCtlFileValidOnRead(self):
00106         self.assertRaises((AttributeError, TypeError),
00107             self.yn00.read_ctl_file, 1)
00108         self.assertRaises((AttributeError, KeyError), 
00109             self.yn00.read_ctl_file, self.bad_ctl_file1)
00110         self.assertRaises(AttributeError, 
00111             self.yn00.read_ctl_file, self.bad_ctl_file2)
00112         target_options = {"verbose": 1,
00113                         "icode": 0,
00114                         "weighting": 0,
00115                         "commonf3x4": 0,
00116                         "ndata": 1}
00117         self.yn00.read_ctl_file(self.ctl_file)
00118         self.assertEqual(self.yn00._options, target_options)
00119         
00120     def testCtlFileExistsOnRead(self):
00121         self.assertRaises(IOError,
00122             self.yn00.read_ctl_file, ctl_file = "nonexistent")
00123         
00124     def testResultsValid(self):
00125         self.assertRaises((AttributeError, TypeError),
00126             yn00.read, 1)
00127     
00128     def testResultsExist(self):
00129         self.assertRaises((EnvironmentError, IOError),
00130             yn00.read, "nonexistent")
00131         
00132     def testResultsParsable(self):
00133         self.assertRaises(ValueError, yn00.read, self.results_file)
00134         
00135     def testParseAllVersions(self):
00136         folder = os.path.join(self.results_dir, "yn00")
00137         for results_file in os.listdir(folder):
00138             file_path = os.path.join(folder, results_file)
00139             results = yn00.read(file_path)
00140             self.assertEqual(len(results), 5)
00141             self.assertEqual(len(results["Homo_sapie"]), 4)
00142             self.assertEqual(len(results["Homo_sapie"]["Pan_troglo"]),
00143                 5)
00144         
00145 if __name__ == "__main__":
00146     runner = unittest.TextTestRunner(verbosity = 2)
00147     unittest.main(testRunner=runner)