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