-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsimulation.py
191 lines (145 loc) · 5.64 KB
/
simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import numpy as np
from scipy.stats import poisson
from scipy.linalg import block_diag
def mutation_free_population(strains, G):
p = np.array([ [0] * G for _ in range(strains) ], dtype=np.float64)
p[0,0] = 1
assert np.allclose(p.sum(), 1)
return p
def smooth_fitness(s, H, strains, G):
w = np.array([ [ (1 - s ) ** (k + i) for k in range(G)] for i in range(strains) ])
return w
def rugged_fitness(s, H, strains, G):
w = smooth_fitness(s, H, strains, G)
w[-1,:] *= (1 + s * H)/((1 - s) ** 2) # fix fitness of the double mutant strain
return w
def mean_fitness(p, w):
return (p*w).sum()
def mutation_rates_matrix(U, pi, tau, w):
mutation_rates = np.ones(w.shape) * U
mutation_rates[w < pi] *= tau
return mutation_rates
def big_mutation_matrix(mutation_rates, repeats, small_mutation_matrix_function):
assert mutation_rates.shape[0] == 3 or mutation_rates.shape[1] == 3
M = np.zeros((0,0))
for i in range(repeats):
m = small_mutation_matrix_function(mutation_rates[i,:])
M = block_diag(M, m)
assert np.allclose(M.sum(axis=0),1)
return M
def small_background_mutation_matrix(mutation_rates):
assert mutation_rates.shape[0] == len(mutation_rates)
mutation_rvs = poisson(mutation_rates)
m = np.diag(mutation_rvs.pmf(0))
for k in range(1,mutation_rates.shape[0]):
m += np.diag(mutation_rvs.pmf(k)[:-k],-k)
# absorb further mutations in the last class
for j in range(mutation_rates.shape[0]):
m[-1,j] = 1 - mutation_rvs.cdf(mutation_rates.shape[0] - 2 - j)[j]
return m
def small_strain_mutation_matrix(mutation_rates):
assert mutation_rates.shape[0] == len(mutation_rates)
mu = mutation_rates
u = np.array([ [ (1 - mu[0]) ** 2, 0, 0 ], [2 * mu[0] * (1 - mu[0]) , 1 - mu[1], 0], [ mu[0] ** 2, mu[1], 1 ] ])
return u
def run(pop_size, s, H, U, beta, G, pi, tau, tick_interval=1000000):
# init population
w = smooth_fitness(s, H, 3, G)
mutation_rates = mutation_rates_matrix(U, pi, tau, w)
Mm = big_mutation_matrix(mutation_rates, 3, small_background_mutation_matrix)
mutation_rates2 = mutation_rates.copy()
Mu = big_mutation_matrix((mutation_rates2 * beta).transpose(), G, small_strain_mutation_matrix)
p = mutation_free_population(3, G)
ps = [p]
shape = p.shape
W = mean_fitness(p,w)
Ws = [W]
tick = 0
print("Starting simulation")
## MSB
while tick < 5000:
# selection
p = w * p
# strain mutations
p = Mu.dot( p.flatten(order="F") )
p = p.reshape(shape, order="F")
# background mutations
p = Mm.dot( p.flatten(order="C") )
p = p.reshape(shape, order="C")
p /= p.sum()
# drift
if pop_size > 0:
p = np.random.multinomial(pop_size, p.flatten()) / np.float64(pop_size)
p = p.reshape(shape)
# mean fitness
W = mean_fitness(p,w)
# monitoring and logging
if tick_interval != 0 and tick % tick_interval == 0:
print("Tick %d" % tick)
tick += 1
ps.append(p)
Ws.append(W)
print("MSB reached at tick %d with mean fitness %.4g" % (tick, W))
w = rugged_fitness(s, H, 3, G)
mutation_rates = mutation_rates_matrix(U, pi, tau, w)
Mm = big_mutation_matrix(mutation_rates, 3, small_background_mutation_matrix)
mutation_rates2 = mutation_rates.copy()
Mu = big_mutation_matrix((mutation_rates2 * beta).transpose(), G, small_strain_mutation_matrix)
## Double mutant appearance
while p[2,:].sum() == 0:
# selection
p = w * p
# strain mutations
p = Mu.dot( p.flatten(order="F") )
p = p.reshape(shape, order="F")
# background mutations
p = Mm.dot( p.flatten(order="C") )
p = p.reshape(shape, order="C")
p /= p.sum()
# drift
if pop_size > 0:
p = np.random.multinomial(pop_size, p.flatten()) / np.float64(pop_size)
p = p.reshape(shape)
# mean fitness
W = mean_fitness(p,w)
# monitoring and logging
if tick_interval != 0 and tick % tick_interval == 0:
print("Tick %d" % tick)
tick += 1
ps.append(p)
Ws.append(W)
print("Double mutant appeared at tick %d with mean fitness %.4g" % (tick, W))
AB0,AB1,AB2,AB3 = p[2,0],p[2,1],p[2,2],p[2,3]
print("AB/0 %.4g, AB/1 %.4g, AB/2 %.4g, AB/3 %.4g" % (AB0, AB1, AB2, AB3))
## Double mutant fixation
while p[2,:].sum() > 0 and p[2,:].sum() < 1:
# selection
p = w * p
# NO strain mutations
# p = Mu.dot( p.flatten(order="F") )
# p = p.reshape(shape, order="F")
# background mutations
p = Mm.dot( p.flatten(order="C") )
p = p.reshape(shape, order="C")
p /= p.sum()
# drift
if pop_size > 0:
p = np.random.multinomial(pop_size, p.flatten()) / np.float64(pop_size)
p = p.reshape(shape)
# mean fitness
W = mean_fitness(p,w)
# monitoring and logging
if tick_interval != 0 and tick % tick_interval == 0:
print("Tick %d" % tick)
tick += 1
ps.append(p)
Ws.append(W)
if bool(p[2,:].sum() > 0):
print("Fixation at tick %d with mean fitness %.4g and AB frequency %.4g" % (tick, W, p[2,:].sum()))
AB0,AB1,AB2,AB3 = p[2,0],p[2,1],p[2,2],p[2,3]
print("AB/0 %.4g, AB/1 %.4g, AB/2 %.4g, AB/3 %.4g" % (AB0, AB1, AB2, AB3))
else:
print("Extinction at tick %d with mean fitness %.4g" % (tick, W))
# wrap up
print("Simulation finished, %d ticks" % tick)
return np.array(ps), np.array(Ws)