Back to index

python-biopython  1.60
test_PopGen_GenePop.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.Controller import GenePopController
00011 
00012 #Tests genepop related code. Note: this case 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 class AppTest(unittest.TestCase):
00029     """Tests genepop execution via biopython.
00030     """
00031 
00032     def test_allele_genotype_frequencies(self):
00033         """Test genepop execution on basic allele and genotype frequencies.
00034         """
00035         ctrl = GenePopController()
00036         pop_iter, locus_iter = ctrl.calc_allele_genotype_freqs("PopGen" + os.sep + "big.gen")
00037         #print pop, loci
00038         #for popc in pop_iter:
00039         #    pop_name, loci_content = popc
00040         #    print pop_name
00041         #    for locus in loci_content.keys():
00042         #        geno_list, hets, freq_fis = loci_content[locus]
00043         #        print locus
00044         #        print hets
00045         #        print freq_fis
00046         #        print geno_list
00047         #    print
00048 
00049     def test_calc_diversities_fis_with_identity(self):
00050         """Test calculations of diversities ...
00051         """
00052         ctrl = GenePopController()
00053         iter, avg_fis, avg_Qintra = ctrl.calc_diversities_fis_with_identity(
00054             "PopGen" + os.sep + "big.gen")
00055         liter = list(iter)
00056         assert len(liter) == 37
00057         assert liter[0][0] == "Locus1"
00058         assert len(avg_fis)==10
00059         assert len(avg_Qintra)==10
00060 
00061     def test_estimate_nm(self):
00062         """Test Nm estimation.
00063         """
00064         ctrl = GenePopController()
00065         mean_sample_size, mean_priv_alleles, mig10, mig25, mig50, mig_corrected =\
00066             ctrl.estimate_nm("PopGen" + os.sep + "big.gen")
00067         assert (mean_sample_size, mean_priv_alleles, mig10, mig25, mig50, mig_corrected) == \
00068                (28.0, 0.016129, 52.5578, 15.3006, 8.94583, 13.6612)
00069 
00070 
00071     def test_fst_all(self):
00072         """Test genepop execution on all fst.
00073         """
00074         ctrl = GenePopController()
00075         (allFis, allFst, allFit), itr = ctrl.calc_fst_all("PopGen" + os.sep + "c2line.gen")
00076         results = list(itr)
00077         assert (len(results) == 3)
00078         assert (results[0][0] == "136255903")
00079         assert (results[1][3] - 0.33 < 0.01)
00080 
00081     def test_haploidy(self):
00082         """Test haploidy.
00083         """
00084         ctrl = GenePopController()
00085         (allFis, allFst, allFit), itr = ctrl.calc_fst_all("PopGen" + os.sep + "haplo.gen")
00086         litr = list(itr)
00087         assert not type(allFst) == int
00088         assert len(litr) == 37
00089         assert litr[36][0] == "Locus37" 
00090 
00091 if __name__ == "__main__":
00092     runner = unittest.TextTestRunner(verbosity = 2)
00093     unittest.main(testRunner=runner)