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 dictionary [allele] = (repetition count, frequency, Fis CW ) 96 with information for each allele 97 a triple with total number of alleles, Fis CW, Fis RH 98 99 100 """ 101 geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname) 102 pop_iter, loc_iter = geno_freqs 103 pops = list(pop_iter) 104 return pops[pop_pos][1][locus_name][2:]
105
106 - def get_alleles(self, pop_pos, locus_name):
107 """Returns the alleles for a certain population and locus. 108 109 """ 110 geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname) 111 pop_iter, loc_iter = geno_freqs 112 pop_iter = list(pop_iter) 113 return list(pop_iter[pop_pos][1][locus_name][2].keys())
114
115 - def get_alleles_all_pops(self, locus_name):
116 """Returns the alleles for a certain population and locus. 117 118 """ 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] == locus_name: 123 return locus_info[1]
124
125 - def get_allele_frequency(self, pop_pos, locus_name):
126 if len(self.__allele_frequency) == 0: 127 geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname) 128 pop_iter, loc_iter = geno_freqs 129 for locus_info in loc_iter: 130 if locus_info[0] is None: 131 self.__allele_frequency[locus_info[0]] = None, None 132 else: 133 self.__allele_frequency[locus_info[0]] = locus_info[1:] 134 info = self.__allele_frequency[locus_name] 135 pop_name, freqs, total = info[1][pop_pos] 136 allele_freq = {} 137 alleles = info[0] 138 for i in range(len(alleles)): 139 allele_freq[alleles[i]] = freqs[i] 140 return total, allele_freq
141
142 - def get_multilocus_f_stats(self):
143 """ Returns the multilocus F stats 144 145 Explain averaging. 146 Returns Fis(CW), Fst, Fit 147 """ 148 return self._controller.calc_fst_all(self._fname)[0]
149
150 - def get_f_stats(self, locus_name):
151 """ Returns F stats for a locus 152 153 Returns Fis(CW), Fst, Fit, Qintra, Qinter 154 """ 155 loci_iter = self._controller.calc_fst_all(self._fname)[1] 156 for name, fis, fst, fit, qintra, qinter in loci_iter: 157 if name == locus_name: 158 return fis, fst, fit, qintra, qinter
159
160 - def get_avg_fis(self):
161 return self._controller.calc_diversities_fis_with_identity(self._fname)[1]
162
163 - def get_avg_fst_pair(self):
164 return self._controller.calc_fst_pair(self._fname)[1]
165
166 - def get_avg_fst_pair_locus(self, locus):
167 if len(self.__fst_pair_locus) == 0: 168 iter = self._controller.calc_fst_pair(self._fname)[0] 169 for locus_info in iter: 170 self.__fst_pair_locus[locus_info[0]] = locus_info[1] 171 return self.__fst_pair_locus[locus]
172
173 - def calc_ibd(self, is_diplo = True, stat="a", scale="Log", min_dist=0.00001):
174 if is_diplo: 175 return self._controller.calc_ibd_diplo(self._fname, stat, scale, min_dist) 176 else: 177 return self._controller.calc_ibd_haplo(self._fname, stat, scale, min_dist)
178