diff --git a/hamazaki1990/individual.py b/hamazaki1990/individual.py index c46a3d7..b1ee698 100644 --- a/hamazaki1990/individual.py +++ b/hamazaki1990/individual.py @@ -1,3 +1,6 @@ +from decimal import Decimal + + class Individual: def __init__(self, n, f=1.0): self._id = n @@ -11,6 +14,7 @@ def get_fitness(self): def __repr__(self): return str(self._id) + return Decimal(self._fitness) def main(): @@ -18,10 +22,6 @@ def main(): print(ind.get_id()) print(ind.get_fitness()) - ind2 = Individual(30, 0.8) - print(ind2.get_id()) - print(ind2.get_fitness()) - if __name__ == '__main__': main() diff --git a/hamazaki1990/population.py b/hamazaki1990/population.py index da6a4ed..f46fed1 100644 --- a/hamazaki1990/population.py +++ b/hamazaki1990/population.py @@ -16,12 +16,17 @@ def __init__(self, n, mutantrate=0, s=0): wild_inds = [Individual(x) for x in range(num_mutant, n)] self._inds = mutant_inds + wild_inds - def print_ids(self): - print(self._inds) + def get_ids(self): + return self._inds - def print_fitness(self): + def get_fitnesses(self): fitness = [x.get_fitness() for x in self._inds] - print(fitness) + return fitness + + def get_mutantfreq(self): + fitness = [x.get_fitness() for x in self._inds] + mutantfreq = 1 - fitness.count(1.0)/len(fitness) + return mutantfreq def next_genwf(self): fitness = [x.get_fitness() for x in self._inds] @@ -46,45 +51,60 @@ def is_not_fixed(self): else: return False + def mutation_is_not_fixed(self): + fitness = [x.get_fitness() for x in self._inds] + for x in range(1, len(fitness)): + if fitness[0] != fitness[x]: + return True + else: + return False + def main(): p1_1 = Population(10) - p1_1.print_ids() - p1_1.print_fitness() + print(p1_1.get_ids()) + print(p1_1.get_fitnesses()) print(p1_1.is_not_fixed()) + print(p1_1.mutation_is_not_fixed()) for x in range(20): p1_1.next_genwf() - p1_1.print_ids() - p1_1.print_fitness() + print(p1_1.get_ids()) + print(p1_1.get_fitnesses()) print(p1_1.is_not_fixed()) + print(p1_1.mutation_is_not_fixed()) p1_2 = Population(10, 0.3, 0.2) - p1_2.print_ids() - p1_2.print_fitness() + print(p1_2.get_ids()) + print(p1_2.get_fitnesses()) + print(p1_2.is_not_fixed()) + print(p1_2.mutation_is_not_fixed()) + print(p1_2.get_mutantfreq()) for x in range(20): p1_2.next_genwf() - p1_2.print_ids() - p1_2.print_fitness() + print(p1_2.get_ids()) + print(p1_2.get_fitnesses()) + print(p1_2.is_not_fixed()) + print(p1_2.mutation_is_not_fixed()) p2_1 = Population(10) - p2_1.print_ids() - p2_1.print_fitness() + print(p2_1.get_ids()) + print(p2_1.get_fitnesses()) for x in range(20): p2_1.next_genmo() - p2_1.print_ids() - p2_1.print_fitness() + print(p2_1.get_ids()) + print(p2_1.get_fitnesses()) p2_2 = Population(10, 0.3, 0.2) - p2_2.print_ids() - p2_2.print_fitness() + print(p2_2.get_ids()) + print(p2_2.get_fitnesses()) for x in range(20): p2_2.next_genmo() - p2_2.print_ids() - p2_2.print_fitness() + print(p2_2.get_ids()) + print(p2_2.get_fitnesses()) if __name__ == '__main__': diff --git a/hamazaki1990/visualize.py b/hamazaki1990/visualize.py new file mode 100644 index 0000000..d4f43d4 --- /dev/null +++ b/hamazaki1990/visualize.py @@ -0,0 +1,38 @@ +import csv +from population import Population + + +def change_allelewf(population): + t = 0 + fixprocess = [[t, population.get_mutantfreq()]] + while population.mutation_is_not_fixed(): + t += 1 + population.next_genwf() + fixprocess.append([t, population.get_mutantfreq()]) + return fixprocess + + +def change_allelemo(population): + t = 0 + fixprocess = [[t, population.get_mutantfreq()]] + while population.mutation_is_not_fixed(): + t += 1 + population.next_genmo() + fixprocess.append([t, population.get_mutantfreq()]) + return fixprocess + + +def output_allelechange(filename, population, function): + with open(filename, "w", encoding="utf-8") as outfile: + writer = csv.writer(outfile) + writer.writerow(["generation", "derived_allele_frequency"]) + fixprocess = function(population) + for x in range(len(fixprocess)): + writer.writerow(fixprocess[x]) + + +p1 = Population(10, 0.1, 0.2) +output_allelechange("allelefreqwf.csv", p1, change_allelewf) + +p2 = Population(10, 0.1, 0.5) +output_allelechange("allelefreqmo.csv", p2, change_allelemo) diff --git a/hamazaki1990/visualize.r b/hamazaki1990/visualize.r new file mode 100644 index 0000000..9e100f6 --- /dev/null +++ b/hamazaki1990/visualize.r @@ -0,0 +1,13 @@ +library("tidyverse") +setwd("~/github/practice-py/hamazaki1990/") +freq_wf <- read_csv("allelefreqwf.csv") %>% + dplyr::mutate(model = "wf") +freq_wf +freq_mo <- read_csv("allelefreqmo.csv") %>% + dplyr::mutate(model = "moran") +freq_mo +freq <- dplyr::bind_rows(freq_wf, freq_mo) +print(freq) +freqplot <- ggplot(freq, aes(x=generation, y=derived_allele_frequency, group=model, color=model)) +freqplot <-freqplot + geom_line() +print(freqplot)