class AppTest(unittest.TestCase):
    """Tests genepop execution via biopython using EasyController.
    """

    def setUp(self):
        # Genepop likes to be on the directory where the file is.
        os.chdir("PopGen")
        self.ctrl = EasyController("big.gen")

    def tearDown(self):
        os.chdir(cur_dir)

    def test_basic_info(self):
        """Test basic info.
        """
        pops, loci = self.ctrl.get_basic_info()
        self.assertEqual(len(pops), 10)
        self.assertEqual(len(loci), 37)

    def test_get_heterozygosity_info(self):
        """Test heterozygosity info.
        """
        hz_info = self.ctrl.get_heterozygosity_info(0, "Locus2")
        self.assertEqual(hz_info[1], 24)
        self.assertEqual(hz_info[3], 7)

    def test_get_alleles(self):
        """Test get alleles.
        """
        # Returns keys of a dict, so order is Python implementation dependent
        self.assertEqual(set(self.ctrl.get_alleles(0, "Locus3")), set([3, 20]))

    def test_get_alleles_all_pops(self):
        """Test get alleles for all populations.
        """
        self.assertEqual(self.ctrl.get_alleles_all_pops("Locus4"), [1, 3])

    def test_get_fis(self):
        """Test get Fis.
        """
        alleles, overall = self.ctrl.get_fis(0, "Locus2")
        self.assertEqual(alleles[3][0], 55)
        self.assertEqual(overall[0], 62)

    def test_get_allele_frequency(self):
        """Test allele frequency.
        """
        tot_genes, alleles = self.ctrl.get_allele_frequency(0, "Locus2")
        self.assertEqual(tot_genes, 62)
        self.assertTrue(abs(alleles[20] - 0.113) < 0.05)

    def test_get_genotype_count(self):
        """Test genotype count.
        """
        self.assertEqual(len(self.ctrl.get_genotype_count(0, "Locus2")), 3)

    def test_estimate_nm(self):
        """Test Nm estimation.
        """
        nms = self.ctrl.estimate_nm()
        self.assertEqual(nms[0], 28.0)

    def test_hwe_excess(self):
        """Test Hardy-Weinberg Equilibrium.
        """
        hwe_excess = self.ctrl.test_hw_pop(0, "excess")
        self.assertEqual(hwe_excess["Locus1"], (0.4955, None, -0.16, -0.1623, 5))

# These tests are frequently failing, possibly due to a Genepop problem.
#    def test_get_avg_fst_pair_locus(self):
#        """Test get average Fst for pairwise pops on a locus.
#        """
#        self.assertEqual(len(self.ctrl.get_avg_fst_pair_locus("Locus4")), 45)
#
#    def test_get_avg_fst_pair(self):
#        """Test get pairwise Fst.
#        """
#        pop_fis =  self.ctrl.get_avg_fst_pair()
#        self.assertEqual(len(pop_fis), 45)

    def test_get_avg_fis(self):
        """Test average Fis.
        """
        self.ctrl.get_avg_fis()

    def test_get_multilocus_f_stats(self):
        """Test multilocus F stats.
        """
        mf = self.ctrl.get_multilocus_f_stats()
        self.assertEqual(len(mf), 3)
        self.assertTrue(mf[0] < 0.1)

    def test_get_f_stats(self):
        """Test F stats.
        """
        fs = self.ctrl.get_f_stats("Locus2")
        self.assertEqual(len(fs), 5)
        self.assertTrue(fs[0] < 0)
 def setUp(self):
     # Genepop likes to be on the directory where the file is.
     os.chdir("PopGen")
     self.ctrl = EasyController("big.gen")
示例#3
0
 def setUp(self):
     """Change working directory."""
     # Genepop likes to be on the directory where the file is.
     os.chdir("PopGen")
     self.ctrl = EasyController("big.gen")
示例#4
0
class AppTest(unittest.TestCase):
    """Tests genepop execution via biopython using EasyController."""
    def setUp(self):
        """Change working directory."""
        # Genepop likes to be on the directory where the file is.
        os.chdir("PopGen")
        self.ctrl = EasyController("big.gen")

    def tearDown(self):
        """Restore working directory."""
        os.chdir(cur_dir)

    def test_basic_info(self):
        """Test basic info."""
        pops, loci = self.ctrl.get_basic_info()
        self.assertEqual(len(pops), 10)
        self.assertEqual(len(loci), 37)

    def test_get_heterozygosity_info(self):
        """Test heterozygosity info."""
        hz_info = self.ctrl.get_heterozygosity_info(0, "Locus2")
        self.assertEqual(hz_info[1], 24)
        self.assertEqual(hz_info[3], 7)

    def test_get_alleles(self):
        """Test get alleles."""
        # Returns keys of a dict, so order is Python implementation dependent
        self.assertCountEqual(self.ctrl.get_alleles(0, "Locus3"), [3, 20])

    def test_get_alleles_all_pops(self):
        """Test get alleles for all populations."""
        self.assertEqual(self.ctrl.get_alleles_all_pops("Locus4"), [1, 3])

    def test_get_fis(self):
        """Test get Fis."""
        alleles, overall = self.ctrl.get_fis(0, "Locus2")
        self.assertEqual(alleles[3][0], 55)
        self.assertEqual(overall[0], 62)

    def test_get_allele_frequency(self):
        """Test allele frequency."""
        tot_genes, alleles = self.ctrl.get_allele_frequency(0, "Locus2")
        self.assertEqual(tot_genes, 62)
        self.assertLess(abs(alleles[20] - 0.113), 0.05)

    def test_get_genotype_count(self):
        """Test genotype count."""
        self.assertEqual(len(self.ctrl.get_genotype_count(0, "Locus2")), 3)

    def test_estimate_nm(self):
        """Test Nm estimation."""
        nms = self.ctrl.estimate_nm()
        self.assertEqual(nms[0], 28.0)

    def test_hwe_excess(self):
        """Test Hardy-Weinberg Equilibrium."""
        hwe_excess = self.ctrl.test_hw_pop(0, "excess")
        self.assertEqual(hwe_excess["Locus1"],
                         (0.4955, None, -0.16, -0.1623, 5))

    # These tests are frequently failing, possibly due to a Genepop problem.
    #    def test_get_avg_fst_pair_locus(self):
    #        """Test get average Fst for pairwise pops on a locus."""
    #        self.assertEqual(len(self.ctrl.get_avg_fst_pair_locus("Locus4")), 45)
    #
    #    def test_get_avg_fst_pair(self):
    #        """Test get pairwise Fst."""
    #        pop_fis =  self.ctrl.get_avg_fst_pair()
    #        self.assertEqual(len(pop_fis), 45)

    def test_get_avg_fis(self):
        """Test average Fis."""
        self.ctrl.get_avg_fis()

    def test_get_multilocus_f_stats(self):
        """Test multilocus F stats."""
        mf = self.ctrl.get_multilocus_f_stats()
        self.assertEqual(len(mf), 3)
        self.assertLess(mf[0], 0.1)

    def test_get_f_stats(self):
        """Test F stats."""
        fs = self.ctrl.get_f_stats("Locus2")
        self.assertEqual(len(fs), 5)
        self.assertLess(fs[0], 0)
示例#5
0
def filehandler(infile):
    """Doc here"""
    filehandle = EasyController(infile)

    return filehandle
示例#6
0
if len(sys.argv) != 3:
    print("python %s startGen endGen" % (sys.argv[0],))
    sys.exit(-1)

startGen = int(sys.argv[1])
endGen = int(sys.argv[2])

fName = "/tmp/hz%s.txt" % (str(os.getpid()))

f = open(fName, "w")
l = sys.stdin.readline()
while l != "":
    f.write(l)
    l = sys.stdin.readline()
f.close()

ctrl = EasyController(fName)
pop_list, loci_list = ctrl.get_basic_info()
numPops = len(pop_list)
for pop in range(numPops):
    for locus in loci_list:
        # expho, obsho, exphe, obshe = ctrl.get_heterozygosity_info(pop, locus)
        # print float(exphe)/(exphe+expho),
        obs, freqs = ctrl.get_allele_frequency(pop, locus)
        ho = 0.0
        for freq in list(freqs.values()):
            ho += freq**2
        print(1-ho, end=' ')
    print()
os.remove(fName)
class AppTest(unittest.TestCase):
    """Tests genepop execution via biopython using EasyController.
    """

    def setUp(self):
        #Genepop likes to be on the directory where the file is.
        os.chdir("PopGen")
        self.ctrl = EasyController("big.gen")

    def tearDown(self):
        os.chdir("..")

    def test_basic_info(self):
        """Test basic info.
        """
        pops, loci = self.ctrl.get_basic_info()
        assert len(pops) == 10
        assert len(loci) == 37

    def test_get_heterozygosity_info(self):
        """Test heterozygosity info.
        """
        hz_info = self.ctrl.get_heterozygosity_info(0, "Locus2")
        assert hz_info[1] == 24
        assert hz_info[3] == 7

    def test_get_alleles(self):
        """Test get alleles.
        """
        assert self.ctrl.get_alleles(0,"Locus3") == [3, 20]

    def test_get_alleles_all_pops(self):
        """Test get alleles for all populations.
        """
        assert self.ctrl.get_alleles_all_pops("Locus4") == [1, 3]

    def test_get_fis(self):
        """Test get Fis.
        """
        alleles, overall = self.ctrl.get_fis(0,"Locus2")
        assert alleles[3][0] == 55
        assert overall[0] == 62

    def test_get_allele_frequency(self):
        """Test allele frequency.
        """
        tot_genes, alleles = self.ctrl.get_allele_frequency(0,"Locus2")
        assert tot_genes == 62
        assert abs(alleles[20] - 0.113) < 0.05

    def test_get_genotype_count(self):
        """Test genotype count.
        """
        assert len(self.ctrl.get_genotype_count(0,"Locus2")) == 3

    def test_estimate_nm(self):
        """Test Nm estimation.
        """
        nms = self.ctrl.estimate_nm()
        assert nms[0] == 28.0

    def test_get_avg_fst_pair_locus(self):
        """Test get average Fst for pairwise pops on a locus.
        """
        assert len(self.ctrl.get_avg_fst_pair_locus("Locus4")) == 45

    def test_get_avg_fst_pair(self):
        """Test get pairwise Fst.
        """
        pop_fis =  self.ctrl.get_avg_fst_pair()
        assert len(pop_fis) == 45

    def test_get_avg_fis(self):
        """Test average Fis.
        """
        self.ctrl.get_avg_fis()

    def test_get_multilocus_f_stats(self):
        """Test multilocus F stats.
        """
        mf = self.ctrl.get_multilocus_f_stats()
        assert len(mf) == 3
        assert mf[0]<0.1

    def test_get_f_stats(self):
        """Test F stats.
        """
        fs = self.ctrl.get_f_stats("Locus2")
        assert len(fs)==5
        assert fs[0]<0