Back to index

python-biopython  1.60
test_PopGen_DFDist.py
Go to the documentation of this file.
00001 # Copyright 2010 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 DFDist related code. Note: this case requires Dfdist (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 ['Dfdist', 'Ddatacal', 'pv2', '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 Dfdist, Ddatacal, pv2 and cplot2 if you want to use DFDist with Bio.PopGen.FDist.")
00031 del wanted
00032 
00033 
00034 class AppTest(unittest.TestCase):
00035     """Tests the Dfdist 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_dfst_outfile', 'data_fst_outfile')
00046         self._copyfile('dfdist1', 'infile')
00047         self._copyfile('dout.dat', 'out.dat')
00048         self._copyfile('dout.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_ddatacal(self):
00059         """Test Ddatacal execution.
00060         """
00061         fst, samp_size, loci, pops, F, obs = \
00062             self.ctrl.run_datacal(data_dir = self.dirname, version=2)
00063         self.assertTrue(fst - 0.23 < 0.02)
00064         self.assertEqual(samp_size, 32)
00065         self.assertEqual(loci, 300)
00066         self.assertEqual(pops, 2)
00067         self.assertTrue(F - 0.11 < 0.01)
00068         self.assertEqual(obs, 300)
00069 
00070     def test_dfdist(self):
00071         """Test Dfdist execution.
00072         """
00073         #The number of simulations in real life should be at least 10000,
00074         #see the fdist2 documentation.
00075         fst = self.ctrl.run_fdist(npops = 15, nsamples = 10, fst = 0.1,
00076                 sample_size = 20, mut = 0, num_sims = 100,
00077                 data_dir = self.dirname, is_dominant = True)
00078         self.assertTrue(abs(fst - 0.1) < 0.025,
00079                         "Stochastic result, expected %f close to 0.1" % fst)
00080 
00081     def atest_dfdist_force_fst(self):
00082         """Test dfdist execution approximating Fst.
00083            THIS IS TOO SLOW
00084         """
00085         #The number of simulations in real life should be at least 10000,
00086         #see the fdist2 documentation.
00087         fst = self.ctrl.run_fdist_force_fst(npops = 15, nsamples = 10,
00088                 fst = 0.1,
00089                 sample_size = 20, mut = 0, num_sims = 100,
00090                 data_dir = self.dirname, is_dominant=True)
00091         self.assertTrue(abs(fst - 0.09) < 0.05,
00092                         "Stochastic result, expected %f close to 0.09" % fst)
00093 
00094     def test_cplot2(self):
00095         """Test cplot2 execution.
00096         """
00097         cpl_interval =self.ctrl.run_cplot(data_dir = self.dirname, version=2)
00098         self.assertEqual(len(cpl_interval), 300)
00099 
00100     def test_pv2(self):
00101         """Test pv2 execution.
00102         """
00103         pv_data = self.ctrl.run_pv(data_dir = self.dirname, version=2)
00104         self.assertEqual(len(pv_data), 300)
00105 
00106 
00107 if __name__ == "__main__":
00108     print "Running fdist tests, which might take some time, please wait"
00109     runner = unittest.TextTestRunner(verbosity = 2)
00110     unittest.main(testRunner=runner)