Back to index

python-biopython  1.60
EasyController.py
Go to the documentation of this file.
00001 # Copyright 2009 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 
00007 
00008 """
00009 This module allows to control GenePop through an easier interface.
00010 
00011 This interface is less efficient than the standard GenePopControler
00012 
00013 """
00014 
00015 from Controller import GenePopController
00016 from Bio.PopGen import GenePop
00017 
00018 
00019 class EasyController(object):
00020     def __init__(self, fname, genepop_dir = None):
00021         """Initializes the controller.
00022         
00023         genepop_dir is the directory where GenePop is.
00024 
00025         The binary should be called Genepop (capital G)
00026         
00027         """
00028         self._fname = fname
00029         self._controller = GenePopController(genepop_dir)
00030         self.__fst_pair_locus = {} #More caches like this needed!
00031         self.__allele_frequency = {} #More caches like this needed!
00032 
00033     def get_basic_info(self):
00034         f=open(self._fname)
00035         rec = GenePop.read(f)
00036         f.close()
00037         return rec.pop_list, rec.loci_list
00038 
00039     def test_hw_pop(self, pop_pos, test_type = "probability"):
00040         if test_type=="deficiency":
00041             hw_res = self._controller.test_pop_hz_deficiency(self._fname)
00042         elif test_type=="excess":
00043             hw_res = self._controller.test_pop_hz_excess(self._fname)
00044         else:
00045             loci_res, hw_res, fisher_full = self._controller.test_pop_hz_prob(self._fname, ".P")
00046         for i in range(pop_pos-1):
00047             hw_res.next()
00048         return hw_res.next()
00049 
00050     def test_hw_global(self, test_type = "deficiency", enum_test = True,
00051         dememorization = 10000, batches = 20, iterations = 5000):
00052         if test_type=="deficiency":
00053             pop_res, loc_res, all = self._controller.test_global_hz_deficiency(self._fname,
00054                 enum_test, dememorization, batches, iterations)
00055         else:
00056             pop_res, loc_res, all = self._controller.test_global_hz_excess(self._fname,
00057                 enum_test, dememorization, batches, iterations)
00058         return list(pop_res), list(loc_res), all
00059 
00060     def test_ld_all_pair(self, locus1, locus2,
00061         dememorization = 10000, batches = 20, iterations = 5000):
00062         all_ld = self._controller.test_ld(self._fname, dememorization, batches, iterations)[1]
00063         for ld_case in all_ld:
00064             (l1, l2), result = ld_case
00065             if (l1==locus1 and l2==locus2) or (l1==locus2 and l2==locus1):
00066                 return result
00067 
00068     def estimate_nm(self):
00069         """ Estimate Nm. Just a simple bridge.
00070         """
00071         return self._controller.estimate_nm(self._fname)
00072 
00073     def get_heterozygosity_info(self, pop_pos, locus_name):
00074         """Returns the heterozygosity info for a certain locus on a population.
00075 
00076            Returns (Expected homozygotes, observed homozygotes,
00077                     Expected heterozygotes, observed heterozygotes)
00078         """
00079         geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname)
00080         pop_iter, loc_iter = geno_freqs
00081         pops = list(pop_iter)
00082         return pops[pop_pos][1][locus_name][1]
00083 
00084     def get_genotype_count(self, pop_pos, locus_name):
00085         """Returns the genotype counts for a certain population and locus
00086 
00087         """
00088         geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname)
00089         pop_iter, loc_iter = geno_freqs
00090         pop_iter = list(pop_iter)
00091         return pop_iter[pop_pos][1][locus_name][0]
00092 
00093     def get_fis(self, pop_pos, locus_name):
00094         """Returns the Fis for a certain population and locus
00095 
00096            Below CW means Cockerham and Weir and RH means Robertson and Hill. 
00097 
00098            Returns a pair:
00099            dictionary [allele] = (repetition count, frequency, Fis CW )
00100                with information for each allele
00101            a triple with total number of alleles, Fis CW, Fis RH
00102 
00103 
00104         """
00105         geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname)
00106         pop_iter, loc_iter = geno_freqs
00107         pops = list(pop_iter)
00108         return pops[pop_pos][1][locus_name][2:]
00109 
00110     def get_alleles(self, pop_pos, locus_name):
00111         """Returns the alleles for a certain population and locus.
00112 
00113         """
00114         geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname)
00115         pop_iter, loc_iter = geno_freqs
00116         pop_iter = list(pop_iter)
00117         return pop_iter[pop_pos][1][locus_name][2].keys()
00118 
00119     def get_alleles_all_pops(self, locus_name):
00120         """Returns the alleles for a certain population and locus.
00121 
00122         """
00123         geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname)
00124         pop_iter, loc_iter = geno_freqs
00125         for locus_info in loc_iter:
00126             if locus_info[0] == locus_name:
00127                 return locus_info[1]
00128 
00129     def get_allele_frequency(self, pop_pos, locus_name):
00130         if len(self.__allele_frequency) == 0:
00131             geno_freqs = self._controller.calc_allele_genotype_freqs(self._fname)
00132             pop_iter, loc_iter = geno_freqs
00133             for locus_info in loc_iter:
00134                 if locus_info[0] == None:
00135                     self.__allele_frequency[locus_info[0]] = None, None
00136                 else:
00137                     self.__allele_frequency[locus_info[0]] = locus_info[1:]
00138         info = self.__allele_frequency[locus_name]
00139         pop_name, freqs, total = info[1][pop_pos]
00140         allele_freq = {}
00141         alleles = info[0]
00142         for i in range(len(alleles)):
00143             allele_freq[alleles[i]] = freqs[i]
00144         return total, allele_freq
00145 
00146 
00147     def get_multilocus_f_stats(self):
00148         """ Returns the multilocus F stats
00149 
00150             Explain averaging.
00151             Returns Fis(CW), Fst, Fit
00152         """
00153         return self._controller.calc_fst_all(self._fname)[0]
00154 
00155     def get_f_stats(self, locus_name):
00156         """ Returns F stats for a locus
00157 
00158             Returns Fis(CW), Fst, Fit, Qintra, Qinter
00159         """
00160         loci_iter =  self._controller.calc_fst_all(self._fname)[1]
00161         for name, fis, fst, fit, qintra, qinter in loci_iter:
00162             if name == locus_name:
00163                 return fis, fst, fit, qintra, qinter
00164 
00165     def get_avg_fis(self):
00166         return self._controller.calc_diversities_fis_with_identity(self._fname)[1]
00167 
00168     def get_avg_fst_pair(self):
00169         return self._controller.calc_fst_pair(self._fname)[1]
00170 
00171     def get_avg_fst_pair_locus(self, locus):
00172         if len(self.__fst_pair_locus) == 0:
00173             iter = self._controller.calc_fst_pair(self._fname)[0]
00174             for locus_info in iter:
00175                 self.__fst_pair_locus[locus_info[0]] = locus_info[1]
00176         return self.__fst_pair_locus[locus]
00177 
00178     def calc_ibd(self, is_diplo = True, stat="a", scale="Log", min_dist=0.00001):
00179         if is_diplo:
00180             return self._controller.calc_ibd_diplo(self._fname, stat, scale, min_dist)
00181         else:
00182             return self._controller.calc_ibd_haplo(self._fname, stat, scale, min_dist)