Package Bio :: Package PopGen :: Package GenePop :: Module EasyController
[hide private]
[frames] | no frames]

Source Code for Module Bio.PopGen.GenePop.EasyController

  1  # Copyright 2009 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  """ 
  7  This module allows to control GenePop through an easier interface. 
  8   
  9  This interface is less efficient than the standard GenePopControler 
 10   
 11  """ 
 12   
 13  from .Controller import GenePopController 
 14  from Bio.PopGen import GenePop 
 15   
 16   
17 -class EasyController(object):
18 - def __init__(self, fname, genepop_dir=None):
19 """Initializes the controller. 20 21 genepop_dir is the directory where GenePop is. 22 23 The binary should be called Genepop (capital G) 24 """ 25 self._fname = fname 26 self._controller = GenePopController(genepop_dir) 27 self.__fst_pair_locus = {} # More caches like this needed! 28 self.__allele_frequency = {} # More caches like this needed!
29
30 - def get_basic_info(self):
31 with open(self._fname) as f: 32 rec = GenePop.read(f) 33 return rec.pop_list, rec.loci_list
34
35 - def test_hw_pop(self, pop_pos, test_type="probability"):
36 if test_type == "deficiency": 37 hw_res = self._controller.test_pop_hz_deficiency(self._fname) 38 elif test_type == "excess": 39 hw_res = self._controller.test_pop_hz_excess(self._fname) 40 else: 41 loci_res, hw_res, fisher_full = self._controller.test_pop_hz_prob(self._fname, ".P") 42 for i in range(pop_pos - 1): 43 next(hw_res) 44 return next(hw_res)
45
46 - def test_hw_global(self, test_type="deficiency", enum_test=True, 47 dememorization=10000, batches=20, iterations=5000):
48 if test_type == "deficiency": 49 pop_res, loc_res, all = self._controller.test_global_hz_deficiency(self._fname, 50 enum_test, dememorization, batches, iterations) 51 else: 52 pop_res, loc_res, all = self._controller.test_global_hz_excess(self._fname, 53 enum_test, dememorization, batches, iterations) 54 return list(pop_res), list(loc_res), all
55
56 - def test_ld_all_pair(self, locus1, locus2, dememorization=10000, 57 batches=20, iterations=5000):
58 all_ld = self._controller.test_ld(self._fname, dememorization, batches, iterations)[1] 59 for ld_case in all_ld: 60 (l1, l2), result = ld_case 61 if (l1 == locus1 and l2 == locus2) or (l1 == locus2 and l2 == locus1): 62 return result
63
64 - def estimate_nm(self):
65 """ Estimate Nm. Just a simple bridge. 66 """ 67 return self._controller.estimate_nm(self._fname)
68
69 - def get_heterozygosity_info(self, pop_pos, locus_name):
70 """Returns the heterozygosity info for a certain locus on a population. 71 72 Returns (Expected homozygotes, observed homozygotes, 73 Expected heterozygotes, observed heterozygotes) 74 """ 75 geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname) 76 pop_iter, loc_iter = geno_freqs 77 pops = list(pop_iter) 78 return pops[pop_pos][1][locus_name][1]
79
80 - def get_genotype_count(self, pop_pos, locus_name):
81 """Returns the genotype counts for a certain population and locus 82 83 """ 84 geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname) 85 pop_iter, loc_iter = geno_freqs 86 pop_iter = list(pop_iter) 87 return pop_iter[pop_pos][1][locus_name][0]
88
89 - def get_fis(self, pop_pos, locus_name):
90 """Returns the Fis for a certain population and locus 91 92 Below CW means Cockerham and Weir and RH means Robertson and Hill. 93 94 Returns a pair: 95 96 - dictionary [allele] = (repetition count, frequency, Fis CW ) 97 with information for each allele 98 - a triple with total number of alleles, Fis CW, Fis RH 99 100 101 """ 102 geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname) 103 pop_iter, loc_iter = geno_freqs 104 pops = list(pop_iter) 105 return pops[pop_pos][1][locus_name][2:]
106
107 - def get_alleles(self, pop_pos, locus_name):
108 """Returns the alleles for a certain population and locus. 109 110 """ 111 geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname) 112 pop_iter, loc_iter = geno_freqs 113 pop_iter = list(pop_iter) 114 return list(pop_iter[pop_pos][1][locus_name][2].keys())
115
116 - def get_alleles_all_pops(self, locus_name):
117 """Returns the alleles for a certain population and locus. 118 119 """ 120 geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname) 121 pop_iter, loc_iter = geno_freqs 122 for locus_info in loc_iter: 123 if locus_info[0] == locus_name: 124 return locus_info[1]
125
126 - def get_allele_frequency(self, pop_pos, locus_name):
127 if len(self.__allele_frequency) == 0: 128 geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname) 129 pop_iter, loc_iter = geno_freqs 130 for locus_info in loc_iter: 131 if locus_info[0] is None: 132 self.__allele_frequency[locus_info[0]] = None, None 133 else: 134 self.__allele_frequency[locus_info[0]] = locus_info[1:] 135 info = self.__allele_frequency[locus_name] 136 pop_name, freqs, total = info[1][pop_pos] 137 allele_freq = {} 138 alleles = info[0] 139 for i in range(len(alleles)): 140 allele_freq[alleles[i]] = freqs[i] 141 return total, allele_freq
142
143 - def get_multilocus_f_stats(self):
144 """ Returns the multilocus F stats 145 146 Explain averaging. 147 Returns Fis(CW), Fst, Fit 148 """ 149 return self._controller.calc_fst_all(self._fname)[0]
150
151 - def get_f_stats(self, locus_name):
152 """ Returns F stats for a locus 153 154 Returns Fis(CW), Fst, Fit, Qintra, Qinter 155 """ 156 loci_iter = self._controller.calc_fst_all(self._fname)[1] 157 for name, fis, fst, fit, qintra, qinter in loci_iter: 158 if name == locus_name: 159 return fis, fst, fit, qintra, qinter
160
161 - def get_avg_fis(self):
162 return self._controller.calc_diversities_fis_with_identity(self._fname)[1]
163
164 - def get_avg_fst_pair(self):
165 return self._controller.calc_fst_pair(self._fname)[1]
166
167 - def get_avg_fst_pair_locus(self, locus):
168 if len(self.__fst_pair_locus) == 0: 169 iter = self._controller.calc_fst_pair(self._fname)[0] 170 for locus_info in iter: 171 self.__fst_pair_locus[locus_info[0]] = locus_info[1] 172 return self.__fst_pair_locus[locus]
173
174 - def calc_ibd(self, is_diplo=True, stat="a", scale="Log", min_dist=0.00001):
175 if is_diplo: 176 return self._controller.calc_ibd_diplo(self._fname, stat, scale, min_dist) 177 else: 178 return self._controller.calc_ibd_haplo(self._fname, stat, scale, min_dist)
179