Package Bio :: Package PopGen :: Package FDist :: Module Utils
[hide private]
[frames] | no frames]

Source Code for Module Bio.PopGen.FDist.Utils

 1  # Copyright 2007 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  import os 
 8  from Bio.PopGen import GenePop 
 9  import Bio.PopGen.FDist 
10   
11  # Quite a few utility functions could be done (like remove pop, 
12  # add locus, etc...). The recommended strategy is convert back 
13  # and forth from/to GenePop and use GenePop Utils 
14   
15 -def convert_genepop_to_fdist(gp_rec):
16 """Converts a GenePop record to a FDist one. 17 18 Parameters: 19 gp_rec - Genepop Record 20 21 Returns: 22 FDist record. 23 """ 24 fd_rec = Bio.PopGen.FDist.Record() 25 26 fd_rec.data_org = 0 27 fd_rec.num_loci = len(gp_rec.loci_list) 28 fd_rec.num_pops = len(gp_rec.populations) 29 for lc_i in range(len(gp_rec.loci_list)): 30 alleles = [] 31 pop_data = [] 32 for pop_i in range(len(gp_rec.populations)): 33 for indiv in gp_rec.populations[pop_i]: 34 for al in indiv[1][lc_i]: 35 if al is not None and al not in alleles: 36 alleles.append(al) 37 #here we go again (necessary...) 38 for pop_i in range(len(gp_rec.populations)): 39 allele_counts = {} 40 for indiv in gp_rec.populations[pop_i]: 41 for al in indiv[1][lc_i]: 42 if al is not None: 43 count = allele_counts.get(al, 0) 44 allele_counts[al] = count + 1 45 allele_array = [] #We need the same order as in alleles 46 for allele in alleles: 47 allele_array.append(allele_counts.get(allele, 0)) 48 pop_data.append(allele_array) 49 #if lc_i==3: 50 # print alleles, allele_counts#, pop_data 51 fd_rec.loci_data.append((len(alleles), pop_data)) 52 return fd_rec
53
54 -def approximate_fst(desired_fst, simulated_fst, parameter_fst, 55 max_run_fst = 1, min_run_fst = 0, limit = 0.005):
56 """Calculates the next Fst attempt in order to approximate a 57 desired Fst. 58 59 """ 60 if abs(simulated_fst - desired_fst) < limit: 61 return parameter_fst, max_run_fst, min_run_fst 62 if simulated_fst > desired_fst: 63 max_run_fst = parameter_fst 64 next_parameter_fst = (min_run_fst + parameter_fst)/2 65 else: 66 min_run_fst = parameter_fst 67 next_parameter_fst = (max_run_fst + parameter_fst)/2 68 return next_parameter_fst, max_run_fst, min_run_fst
69