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