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