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