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