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:
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
32 - def get_basic_info(self):
33 f=open(self._fname) 34 rec = GenePop.read(f) 35 f.close() 36 return rec.pop_list, rec.loci_list
37
38 - def test_hw_pop(self, pop_pos, test_type = "probability"):
39 if test_type=="deficiency": 40 hw_res = self._controller.test_pop_hz_deficiency(self._fname) 41 elif test_type=="excess": 42 hw_res = self._controller.test_pop_hz_excess(self._fname) 43 else: 44 loci_res, hw_res, fisher_full = self._controller.test_pop_hz_prob(self._fname, ".P") 45 for i in range(pop_pos-1): 46 hw_res.next() 47 return hw_res.next()
48
49 - def test_hw_global(self, test_type = "deficiency", enum_test = True, 50 dememorization = 10000, batches = 20, iterations = 5000):
51 if test_type=="deficiency": 52 pop_res, loc_res, all = self._controller.test_global_hz_deficiency(self._fname, 53 enum_test, dememorization, batches, iterations) 54 else: 55 pop_res, loc_res, all = self._controller.test_global_hz_excess(self._fname, 56 enum_test, dememorization, batches, iterations) 57 return list(pop_res), list(loc_res), all
58
59 - def test_ld_all_pair(self, locus1, locus2, 60 dememorization = 10000, batches = 20, iterations = 5000):
61 all_ld = self._controller.test_ld(self._fname, dememorization, batches, iterations)[1] 62 for ld_case in all_ld: 63 (l1, l2), result = ld_case 64 if (l1==locus1 and l2==locus2) or (l1==locus2 and l2==locus1): 65 return result
66
67 - def estimate_nm(self):
68 """ Estimate Nm. Just a simple bridge. 69 """ 70 return self._controller.estimate_nm(self._fname)
71
72 - def get_heterozygosity_info(self, pop_pos, locus_name):
73 """Returns the heterozygosity info for a certain locus on a population. 74 75 Returns (Expected homozygotes, observed homozygotes, 76 Expected heterozygotes, observed heterozygotes) 77 """ 78 geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname) 79 pop_iter, loc_iter = geno_freqs 80 pops = list(pop_iter) 81 return pops[pop_pos][1][locus_name][1]
82
83 - def get_genotype_count(self, pop_pos, locus_name):
84 """Returns the genotype counts for a certain population and locus 85 86 """ 87 geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname) 88 pop_iter, loc_iter = geno_freqs 89 pop_iter = list(pop_iter) 90 return pop_iter[pop_pos][1][locus_name][0]
91
92 - def get_fis(self, pop_pos, locus_name):
93 """Returns the Fis for a certain population and locus 94 95 Below CW means Cockerham and Weir and RH means Robertson and Hill. 96 97 Returns a pair: 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 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 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] == locus_name: 133 alleles = locus_info[1] 134 pop_name, freqs, total = locus_info[2][pop_pos] 135 allele_freq = {} 136 for i in range(len(alleles)): 137 allele_freq[alleles[i]] = freqs[i] 138 return total, allele_freq
139
140 - def get_multilocus_f_stats(self):
141 """ Returns the multilocus F stats 142 143 Explain averaging. 144 Returns Fis(CW), Fst, Fit 145 """ 146 return self._controller.calc_fst_all(self._fname)[0]
147
148 - def get_f_stats(self, locus_name):
149 """ Returns F stats for a locus 150 151 Returns Fis(CW), Fst, Fit, Qintra, Qinter 152 """ 153 loci_iter = self._controller.calc_fst_all(self._fname)[1] 154 for name, fis, fst, fit, qintra, qinter in loci_iter: 155 if name == locus_name: 156 return fis, fst, fit, qintra, qinter
157
158 - def get_avg_fis(self):
159 return self._controller.calc_diversities_fis_with_identity(self._fname)[1]
160
161 - def get_avg_fst_pair(self):
162 return self._controller.calc_fst_pair(self._fname)[1]
163
164 - def get_avg_fst_pair_locus(self, locus):
165 if len(self.__fst_pair_locus) == 0: 166 iter = self._controller.calc_fst_pair(self._fname)[0] 167 for locus_info in iter: 168 self.__fst_pair_locus[locus_info[0]] = locus_info[1] 169 return self.__fst_pair_locus[locus]
170
171 - def calc_ibd(self, is_diplo = True, stat="a", scale="Log", min_dist=0.00001):
172 if is_diplo: 173 return self._controller.calc_ibd_diplo(self._fname, stat, scale, min_dist) 174 else: 175 return self._controller.calc_ibd_haplo(self._fname, stat, scale, min_dist)
176