Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 111 lines (96 sloc) 4.099 kb
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
1 # Copyright 2010 by Tiago Antao <tiagoantao@gmail.com>. All rights reserved.
2 # This code is part of the Biopython distribution and governed by its
3 # license. Please see the LICENSE file that should have been included
4 # as part of this package.
5
6 import commands
7 import os
8 import shutil
9 import tempfile
10 import unittest
11 from Bio.PopGen import FDist
12 from Bio.PopGen.FDist import Controller
13 from Bio import MissingExternalDependencyError
14
f1fa709 Peter Cock Check for all four FDist related binaries
peterjc authored
15 #Tests DFDist related code. Note: this case requires Dfdist (four binaries)
16 #test_PopGen_FDist_nodepend tests code that does not require fdist2 or Dfdist
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
17
f1fa709 Peter Cock Check for all four FDist related binaries
peterjc authored
18 wanted = dict()
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
19 for path in os.environ['PATH'].split(os.pathsep):
20 try:
21 list = os.listdir(path)
22 for file in os.listdir(path):
f1fa709 Peter Cock Check for all four FDist related binaries
peterjc authored
23 for f in ['Dfdist', 'Ddatacal', 'pv2', 'cplot2']:
01d1db1 Peter Cock Tweak FDist / DFDist detection on Windows
peterjc authored
24 if file == f or file.lower() == f.lower()+".exe":
f1fa709 Peter Cock Check for all four FDist related binaries
peterjc authored
25 wanted[f] = file
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
26 except os.error:
27 pass #Path doesn't exist - correct to pass
f1fa709 Peter Cock Check for all four FDist related binaries
peterjc authored
28 if len(wanted) != 4:
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
29 raise MissingExternalDependencyError(\
f1fa709 Peter Cock Check for all four FDist related binaries
peterjc authored
30 "Install Dfdist, Ddatacal, pv2 and cplot2 if you want to use DFDist with Bio.PopGen.FDist.")
31 del wanted
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
32
33
34 class AppTest(unittest.TestCase):
bc16831 Peter Cock Minor correction of fdist -> Dfdist in test
peterjc authored
35 """Tests the Dfdist suite of applications.
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
36 """
37 def _copyfile(self, inname, outname):
38 shutil.copyfile(
39 'PopGen' + os.sep + inname,
40 self.dirname + os.sep + outname)
41
42 def setUp(self):
43 self.ctrl = Controller.FDistController()
44 self.dirname = tempfile.mkdtemp()
45 self._copyfile('data_dfst_outfile', 'data_fst_outfile')
46 self._copyfile('dfdist1', 'infile')
47 self._copyfile('dout.dat', 'out.dat')
48 self._copyfile('dout.cpl', 'out.cpl')
49
50 def tearDown(self):
51 #Not sure how exactly, but its possible the temp directory
52 #may not (still) exist.
53 if os.path.isdir(self.dirname):
54 for file in os.listdir(self.dirname):
55 os.remove(self.dirname + os.sep + file)
56 os.rmdir(self.dirname)
57
8d17727 Tiago Antao dfdist support
tiagoantao authored
58 def test_ddatacal(self):
59 """Test Ddatacal execution.
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
60 """
61 fst, samp_size, loci, pops, F, obs = \
62 self.ctrl.run_datacal(data_dir = self.dirname, version=2)
079067f Peter Cock Use unittest assert methods
peterjc authored
63 self.assertTrue(fst - 0.23 < 0.02)
64 self.assertEqual(samp_size, 32)
65 self.assertEqual(loci, 300)
66 self.assertEqual(pops, 2)
67 self.assertTrue(F - 0.11 < 0.01)
68 self.assertEqual(obs, 300)
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
69
70 def test_dfdist(self):
bc16831 Peter Cock Minor correction of fdist -> Dfdist in test
peterjc authored
71 """Test Dfdist execution.
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
72 """
73 #The number of simulations in real life should be at least 10000,
74 #see the fdist2 documentation.
75 fst = self.ctrl.run_fdist(npops = 15, nsamples = 10, fst = 0.1,
76 sample_size = 20, mut = 0, num_sims = 100,
77 data_dir = self.dirname, is_dominant = True)
f2adc7e Peter Cock Relax stochastic DFDist test (was failing too often)
peterjc authored
78 self.assertTrue(abs(fst - 0.1) < 0.025,
79 "Stochastic result, expected %f close to 0.1" % fst)
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
80
8d17727 Tiago Antao dfdist support
tiagoantao authored
81 def atest_dfdist_force_fst(self):
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
82 """Test dfdist execution approximating Fst.
8d17727 Tiago Antao dfdist support
tiagoantao authored
83 THIS IS TOO SLOW
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
84 """
85 #The number of simulations in real life should be at least 10000,
86 #see the fdist2 documentation.
87 fst = self.ctrl.run_fdist_force_fst(npops = 15, nsamples = 10,
88 fst = 0.1,
89 sample_size = 20, mut = 0, num_sims = 100,
90 data_dir = self.dirname, is_dominant=True)
f2adc7e Peter Cock Relax stochastic DFDist test (was failing too often)
peterjc authored
91 self.assertTrue(abs(fst - 0.09) < 0.05,
92 "Stochastic result, expected %f close to 0.09" % fst)
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
93
8d17727 Tiago Antao dfdist support
tiagoantao authored
94 def test_cplot2(self):
95 """Test cplot2 execution.
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
96 """
97 cpl_interval =self.ctrl.run_cplot(data_dir = self.dirname, version=2)
079067f Peter Cock Use unittest assert methods
peterjc authored
98 self.assertEqual(len(cpl_interval), 300)
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
99
8d17727 Tiago Antao dfdist support
tiagoantao authored
100 def test_pv2(self):
101 """Test pv2 execution.
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
102 """
103 pv_data = self.ctrl.run_pv(data_dir = self.dirname, version=2)
079067f Peter Cock Use unittest assert methods
peterjc authored
104 self.assertEqual(len(pv_data), 300)
6eb8b98 Tiago Antao dfdist support
tiagoantao authored
105
106
107 if __name__ == "__main__":
108 print "Running fdist tests, which might take some time, please wait"
109 runner = unittest.TextTestRunner(verbosity = 2)
110 unittest.main(testRunner=runner)
Something went wrong with that request. Please try again.