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