Back to index

python-biopython  1.60
test_PopGen_SimCoal.py
Go to the documentation of this file.
00001 # Copyright 2007 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 os
00007 import unittest
00008 from Bio.PopGen import SimCoal
00009 from Bio.PopGen.SimCoal.Controller import SimCoalController
00010 from Bio import MissingExternalDependencyError
00011 
00012 #Tests simcoal related code. Note: this case requires simcoal
00013 #test_PopGen_SimCoal_nodepend tests code that does not require simcoal
00014 
00015 found = False
00016 for path in os.environ['PATH'].split(os.pathsep):
00017     try:
00018         for filename in os.listdir(path):
00019             if filename == "simcoal2" \
00020             or (filename.lower() == "simcoal2.exe"):
00021                 found = True
00022                 simcoal_dir = path
00023     except os.error:
00024         pass #Path doesn't exist - correct to pass
00025 if not found:
00026     raise MissingExternalDependencyError(\
00027         "Install SIMCOAL2 if you want to use Bio.PopGen.SimCoal.")
00028 
00029 
00030 class AppTest(unittest.TestCase):
00031     """Tests simcoal execution via biopython.
00032     """
00033     def setUp(self):
00034         self.tidy()
00035 
00036     def tearDown(self):
00037         self.tidy()
00038 
00039     def tidy(self):
00040         if not os.path.isdir(os.path.join('PopGen', 'simple')):
00041             #Unit test must have failed to invoke simcaol,
00042             #and thus it never created the directory.
00043             return
00044         for file in os.listdir(os.path.join('PopGen', 'simple')):
00045             os.remove(os.sep.join(['PopGen', 'simple', file]))
00046         os.rmdir(os.path.join('PopGen', 'simple'))
00047 
00048     def test_simcoal(self):
00049         """Test simcoal execution.
00050         """
00051         ctrl = SimCoalController(simcoal_dir)
00052         ctrl.run_simcoal('simple.par', 50, par_dir = 'PopGen')
00053         assert os.path.isdir(os.path.join('PopGen', 'simple')), \
00054                "Output directory not created!"
00055         assert( len(os.listdir(os.path.join('PopGen', 'simple'))) == 52)
00056 
00057 if __name__ == "__main__":
00058     runner = unittest.TextTestRunner(verbosity = 2)
00059     unittest.main(testRunner=runner)