Back to index

python-biopython  1.60
test_PopGen_FDist.py
Go to the documentation of this file.
00001 # Copyright 2006 by Tiago Antao <tiagoantao@gmail.com>.  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 import commands
00007 import os
00008 import shutil
00009 import tempfile
00010 import unittest
00011 from Bio.PopGen import FDist
00012 from Bio.PopGen.FDist import Controller
00013 from Bio import MissingExternalDependencyError
00014 
00015 #Tests FDist2 related code. Note: this case requires fdist2 (four binaries)
00016 #test_PopGen_FDist_nodepend tests code that does not require fdist2 or Dfdist
00017 
00018 wanted = dict()
00019 for path in os.environ['PATH'].split(os.pathsep):
00020     try:
00021         list = os.listdir(path)
00022         for file in os.listdir(path):
00023             for f in ['fdist2', 'datacal', 'pv', 'cplot2']:
00024                 if file == f or file.lower() == f.lower()+".exe":
00025                     wanted[f] = file
00026     except os.error:
00027         pass #Path doesn't exist - correct to pass
00028 if len(wanted) != 4:
00029     raise MissingExternalDependencyError(\
00030         "Install fdist2, datacal, pv and cplot if you want to use FDist2 with Bio.PopGen.FDist.")
00031 del wanted
00032 
00033 
00034 class AppTest(unittest.TestCase):
00035     """Tests the fdist suite of applications.
00036     """
00037     def _copyfile(self, inname, outname):
00038         shutil.copyfile(
00039             'PopGen' + os.sep + inname,
00040             self.dirname + os.sep + outname)
00041 
00042     def setUp(self):
00043         self.ctrl = Controller.FDistController()
00044         self.dirname = tempfile.mkdtemp()
00045         self._copyfile('data_fst_outfile', 'data_fst_outfile')
00046         self._copyfile('fdist1', 'infile')
00047         self._copyfile('out.dat', 'out.dat')
00048         self._copyfile('out.cpl', 'out.cpl')
00049 
00050     def tearDown(self):
00051         #Not sure how exactly, but its possible the temp directory
00052         #may not (still) exist.
00053         if os.path.isdir(self.dirname):
00054             for file in os.listdir(self.dirname):
00055                 os.remove(self.dirname + os.sep + file)
00056             os.rmdir(self.dirname)
00057 
00058     def test_datacal(self):
00059         """Test datacal execution.
00060         """
00061         fst, samp_size = self.ctrl.run_datacal(data_dir = self.dirname)
00062         self.assertTrue(fst - 0.44 < 0.01)
00063         self.assertEqual(samp_size, 11)
00064 
00065     def test_fdist(self):
00066         """Test fdist execution.
00067         """
00068         #The number of simulations in real life should be at least 10000,
00069         #see the fdist2 documentation.
00070         fst = self.ctrl.run_fdist(npops = 15, nsamples = 10, fst = 0.1,
00071                 sample_size = 20, mut = 0, num_sims = 100,
00072                 data_dir = self.dirname)
00073         self.assertTrue(abs(fst - 0.1) < 0.025,
00074                         "Stochastic result, expected %f close to 0.1" % fst)
00075 
00076     def test_fdist_force_fst(self):
00077         """Test fdist execution approximating Fst.
00078         """
00079         #The number of simulations in real life should be at least 10000,
00080         #see the fdist2 documentation.
00081         fst = self.ctrl.run_fdist_force_fst(npops = 15, nsamples = 10,
00082                 fst = 0.1,
00083                 sample_size = 20, mut = 0, num_sims = 100,
00084                 data_dir = self.dirname)
00085         self.assertTrue(abs(fst - 0.09) < 0.05,
00086                         "Stochastic result, expected %f close to 0.09" % fst)
00087 
00088     def test_cplot(self):
00089         """Test cplot execution.
00090         """
00091         cpl_interval =self.ctrl.run_cplot(data_dir = self.dirname)
00092         self.assertEqual(len(cpl_interval), 8)
00093 
00094     def test_pv(self):
00095         """Test pv execution.
00096         """
00097         pv_data = self.ctrl.run_pv(data_dir = self.dirname)
00098         self.assertEqual(len(pv_data), 4)
00099 
00100 
00101 if __name__ == "__main__":
00102     print "Running fdist tests, which might take some time, please wait"
00103     runner = unittest.TextTestRunner(verbosity = 2)
00104     unittest.main(testRunner=runner)