Skip to content

Commit

Permalink
output heavywatal#22
Browse files Browse the repository at this point in the history
  • Loading branch information
hamazaki1990 committed May 24, 2017
1 parent 85c5188 commit 2b2c136
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 56 deletions.
2 changes: 1 addition & 1 deletion hamazaki1990/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class Individual:
mutationrate = 0.01
mutationrate = 0.001

def __init__(self, n, f=1.0):
self._id = n
Expand Down
33 changes: 26 additions & 7 deletions hamazaki1990/population.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import copy
from individual import Individual


Expand Down Expand Up @@ -33,7 +34,8 @@ def next_genwf(self):
cumsum_fitness = [sum(fitness[:i]) for i in range(1, size + 1)]
next_generation = []
for x in range(size):
next_inds = roulettechoice(self._inds, cumsum_fitness)
current_inds = copy.deepcopy(self._inds)
next_inds = roulettechoice(current_inds, cumsum_fitness)
next_inds.acquire_mutation()
next_generation.append(next_inds)
self._inds = next_generation
Expand All @@ -43,7 +45,8 @@ def next_genmo(self):
size = len(self._inds)
cumsum_fitness = [sum(fitness[:i]) for i in range(1, size + 1)]
i_dying = random.randrange(size)
next_inds = roulettechoice(self._inds, cumsum_fitness)
current_inds = copy.deepcopy(self._inds)
next_inds = roulettechoice(current_inds, cumsum_fitness)
next_inds.acquire_mutation()
self._inds[i_dying] = next_inds

Expand All @@ -52,15 +55,30 @@ def list_mutation(self):
m_sites = []
for x in genotypes:
m_sites.extend(x)
m_sites = sorted(m_sites)
m_sites = sorted(list(set(m_sites)))
m_list = ([[0 for x in range(len(m_sites))]
for y in range(len(self._inds))])
for i in range(len(self._inds)):
for j in range(len(genotypes[i])):
k = m_sites.index(genotypes[i][j])
if k != "ValueError":
m_list[i][k] += 1
print(m_list)
m_list[i][k] += 1
return m_list

def calculate_mutantfreq_per_site(self):
genotypes = [x.get_genotype() for x in self._inds]
m_sites = []
for x in genotypes:
m_sites.extend(x)
m_sites = sorted(list(set(m_sites)))
m_count = [0 for x in range(len(m_sites))]
for i in range(len(self._inds)):
for j in range(len(genotypes[i])):
k = m_sites.index(genotypes[i][j])
m_count[k] += 1
mutantfreq = [x/len(self._inds) for x in m_count]
mutantfreq_per_site = ({m_sites[i]: mutantfreq[i]
for i in range(len(m_sites))})
return mutantfreq_per_site

def is_not_fixed(self):
for x in range(1, len(self._inds)):
Expand All @@ -80,7 +98,8 @@ def main():
p1_1.next_genwf()
print(p1_1.get_ids())
print(p1_1.get_genotypes())
p1_1.list_mutation()
print(p1_1.list_mutation())
print(p1_1.calculate_mutantfreq_per_site())

print(p1_1.is_not_fixed())
p1_1.list_mutation()
Expand Down
98 changes: 50 additions & 48 deletions hamazaki1990/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,62 @@
from population import Population


def calculate_mutantfreq(population):
m_list = population.list_mutation()
m_count = [0 for x in range(len(m_list[0]))]
for i in m_list:
for j in m_list[i]:
if i[j] != 0:
m_count[j] += 1
mutantfreq = [x/len(m_list) for x in m_count]
return mutantfreq


def change_allelewf(population, generation):
t = 0
fixprocess = [[t, population.calculate_mutantfreq()]]
change_allele = [population.calculate_mutantfreq_per_site()]
for x in range(generation):
t += 1
population.next_genwf()
fixprocess.append([t, population.calculate_mutantfreq()])
return fixprocess


def change_allelemo(population):
t = 0
fixprocess = [[t, population.calculate_mutantfreq()]]
for x in range(20):
t += 1
change_allele.append(population.calculate_mutantfreq_per_site())
all_mutations = []
for x in change_allele:
all_mutations.extend(x.keys())
all_mutationsites = sorted(list(set(all_mutations)))
derived_allelefreq = []
for x in range(generation+1):
t = [x]
t.extend([change_allele[x].get(y, 0.0) for y in all_mutationsites])
derived_allelefreq.append(t)
return derived_allelefreq


def change_allelemo(population, generation):
change_allele = [population.calculate_mutantfreq_per_site()]
for x in range(generation):
population.next_genmo()
fixprocess.append([t, population.calculate_mutantfreq()])
return fixprocess


def output_allelechange(filename, population, function):
change_allele.append(population.calculate_mutantfreq_per_site())
all_mutations = []
for x in change_allele:
all_mutations.extend(x.keys())
all_mutationsites = sorted(list(set(all_mutations)))
derived_allelefreq = []
for x in range(generation+1):
t = [x]
t.extend([change_allele[x].get(y, 0.0) for y in all_mutationsites])
derived_allelefreq.append(t)
return derived_allelefreq


def output_allelechange(filename, function, population, generation):
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])
m_freq = function(population, generation)
num = ["allele" + str(i) + "frequency" for i in range(len(m_freq[0]))]
col_name = ["generation"]
col_name.extend(num)
writer.writerow(col_name)
for x in range(len(m_freq)):
writer.writerow(m_freq[x])


p1_1 = Population(10)
print(p1_1.get_ids())
print(p1_1.get_fitnesses())
print(p1_1.is_not_fixed())

for x in range(10):
p1_1.next_genwf()
print(p1_1.get_ids())
p1_1.get_genotypes()
p1_1.list_mutation()
print(p1_1.calculate_mutantfreq())

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)
fixprocess1 = change_allelewf(p1_1, 10)
print(fixprocess1)

p1_2 = Population(10)
fixprocess2 = change_allelemo(p1_2, 20)
print(fixprocess2)

p1 = Population(100)
output_allelechange("allelefreqwf.csv", change_allelewf, p1, 100)

p2 = Population(100)
output_allelechange("allelefreqmo.csv", change_allelemo, p2, 100)

0 comments on commit 2b2c136

Please sign in to comment.