File: test_PopGen_GenePop.py

package info (click to toggle)
python-biopython 1.68%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 46,860 kB
  • ctags: 13,237
  • sloc: python: 160,306; xml: 93,216; ansic: 9,118; sql: 1,208; makefile: 155; sh: 63
file content (94 lines) | stat: -rw-r--r-- 3,369 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright 2009 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

from __future__ import print_function

import os
import unittest

from Bio import MissingExternalDependencyError
from Bio.PopGen.GenePop.Controller import GenePopController

# Tests genepop related code. Note: this case requires genepop
# test_PopGen_GenePop_nodepend tests code that does not require genepop

found = False
for path in os.environ['PATH'].split(os.pathsep):
    try:
        for filename in os.listdir(path):
            if filename.startswith('Genepop'):
                found = True
    except os.error:
        pass  # Path doesn't exist - correct to pass
if not found:
    raise MissingExternalDependencyError(
        "Install GenePop if you want to use Bio.PopGen.GenePop.")


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

    def test_allele_genotype_frequencies(self):
        """Test genepop execution on basic allele and genotype frequencies.
        """
        ctrl = GenePopController()
        pop_iter, locus_iter = ctrl.calc_allele_genotype_freqs("PopGen" + os.sep + "big.gen")
        # print("%s %s" % (pop, loci))
        # for popc in pop_iter:
        #    pop_name, loci_content = popc
        #    print(pop_name)
        #    for locus in loci_content:
        #        geno_list, hets, freq_fis = loci_content[locus]
        #        print(locus)
        #        print(hets)
        #        print(freq_fis)
        #        print(geno_list)
        #    print("")

    def test_calc_diversities_fis_with_identity(self):
        """Test calculations of diversities ...
        """
        ctrl = GenePopController()
        iter, avg_fis, avg_Qintra = ctrl.calc_diversities_fis_with_identity(
            "PopGen" + os.sep + "big.gen")
        liter = list(iter)
        assert len(liter) == 37
        assert liter[0][0] == "Locus1"
        assert len(avg_fis) == 10
        assert len(avg_Qintra) == 10

    def test_estimate_nm(self):
        """Test Nm estimation.
        """
        ctrl = GenePopController()
        mean_sample_size, mean_priv_alleles, mig10, mig25, mig50, mig_corrected =\
            ctrl.estimate_nm("PopGen" + os.sep + "big.gen")
        assert (mean_sample_size, mean_priv_alleles, mig10, mig25, mig50, mig_corrected) == \
               (28.0, 0.016129, 52.5578, 15.3006, 8.94583, 13.6612)

    def test_fst_all(self):
        """Test genepop execution on all fst.
        """
        ctrl = GenePopController()
        (allFis, allFst, allFit), itr = ctrl.calc_fst_all("PopGen" + os.sep + "c2line.gen")
        results = list(itr)
        assert (len(results) == 3)
        assert (results[0][0] == "136255903")
        assert (results[1][3] - 0.33 < 0.01)

    def test_haploidy(self):
        """Test haploidy.
        """
        ctrl = GenePopController()
        (allFis, allFst, allFit), itr = ctrl.calc_fst_all("PopGen" + os.sep + "haplo.gen")
        litr = list(itr)
        assert not isinstance(allFst, int)
        assert len(litr) == 37
        assert litr[36][0] == "Locus37"

if __name__ == "__main__":
    runner = unittest.TextTestRunner(verbosity=2)
    unittest.main(testRunner=runner)