1
2
3
4
5
6
7 import os
8 from Bio.PopGen import GenePop
9 import Bio.PopGen.FDist
10
11
12
13
14
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
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 = []
46 for allele in alleles:
47 allele_array.append(allele_counts.get(allele, 0))
48 pop_data.append(allele_array)
49
50
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