Back to index

python-biopython  1.60
test_PopGen_GenePop_EasyController.py
Go to the documentation of this file.
00001 # Copyright 2009 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 
00009 from Bio import MissingExternalDependencyError
00010 from Bio.PopGen.GenePop.EasyController import EasyController
00011 
00012 #Tests genepop related code for easy contorller. Note: this requires genepop
00013 #test_PopGen_GenePop_nodepend tests code that does not require genepop
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.startswith('Genepop'):
00020                 found = True
00021     except os.error:
00022         pass #Path doesn't exist - correct to pass
00023 if not found:
00024     raise MissingExternalDependencyError(\
00025         "Install GenePop if you want to use Bio.PopGen.GenePop.")
00026 
00027 
00028 cur_dir = os.path.abspath(".") #Tests directory
00029 
00030 class AppTest(unittest.TestCase):
00031     """Tests genepop execution via biopython using EasyController.
00032     """
00033 
00034     def setUp(self):
00035         #Genepop likes to be on the directory where the file is.
00036         os.chdir("PopGen")
00037         self.ctrl = EasyController("big.gen")
00038 
00039     def tearDown(self):
00040         os.chdir(cur_dir)
00041 
00042     def test_basic_info(self):
00043         """Test basic info.
00044         """
00045         pops, loci = self.ctrl.get_basic_info()
00046         self.assertEqual(len(pops), 10)
00047         self.assertEqual(len(loci), 37)
00048 
00049     def test_get_heterozygosity_info(self):
00050         """Test heterozygosity info.
00051         """
00052         hz_info = self.ctrl.get_heterozygosity_info(0, "Locus2")
00053         self.assertEqual(hz_info[1], 24)
00054         self.assertEqual(hz_info[3], 7)
00055 
00056     def test_get_alleles(self):
00057         """Test get alleles.
00058         """
00059         #Returns keys of a dict, so order is Python implementation dependent
00060         self.assertEqual(set(self.ctrl.get_alleles(0,"Locus3")), set([3, 20]))
00061 
00062     def test_get_alleles_all_pops(self):
00063         """Test get alleles for all populations.
00064         """
00065         self.assertEqual(self.ctrl.get_alleles_all_pops("Locus4"), [1, 3])
00066 
00067     def test_get_fis(self):
00068         """Test get Fis.
00069         """
00070         alleles, overall = self.ctrl.get_fis(0,"Locus2")
00071         self.assertEqual(alleles[3][0], 55)
00072         self.assertEqual(overall[0], 62)
00073 
00074     def test_get_allele_frequency(self):
00075         """Test allele frequency.
00076         """
00077         tot_genes, alleles = self.ctrl.get_allele_frequency(0,"Locus2")
00078         self.assertEqual(tot_genes, 62)
00079         self.assertTrue(abs(alleles[20] - 0.113) < 0.05)
00080 
00081     def test_get_genotype_count(self):
00082         """Test genotype count.
00083         """
00084         self.assertEqual(len(self.ctrl.get_genotype_count(0,"Locus2")), 3)
00085 
00086     def test_estimate_nm(self):
00087         """Test Nm estimation.
00088         """
00089         nms = self.ctrl.estimate_nm()
00090         self.assertEqual(nms[0], 28.0)
00091 
00092 #These tests are frequently failing, possibly due to a Genepop problem.
00093 #    def test_get_avg_fst_pair_locus(self):
00094 #        """Test get average Fst for pairwise pops on a locus.
00095 #        """
00096 #        self.assertEqual(len(self.ctrl.get_avg_fst_pair_locus("Locus4")), 45)
00097 #
00098 #    def test_get_avg_fst_pair(self):
00099 #        """Test get pairwise Fst.
00100 #        """
00101 #        pop_fis =  self.ctrl.get_avg_fst_pair()
00102 #        self.assertEqual(len(pop_fis), 45)
00103 
00104     def test_get_avg_fis(self):
00105         """Test average Fis.
00106         """
00107         self.ctrl.get_avg_fis()
00108 
00109     def test_get_multilocus_f_stats(self):
00110         """Test multilocus F stats.
00111         """
00112         mf = self.ctrl.get_multilocus_f_stats()
00113         self.assertEqual(len(mf), 3)
00114         self.assertTrue(mf[0]<0.1)
00115 
00116     def test_get_f_stats(self):
00117         """Test F stats.
00118         """
00119         fs = self.ctrl.get_f_stats("Locus2")
00120         self.assertEqual(len(fs), 5)
00121         self.assertTrue(fs[0]<0)
00122 
00123 if __name__ == "__main__":
00124     runner = unittest.TextTestRunner(verbosity = 2)
00125     unittest.main(testRunner=runner)