-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_pop_delta.py
More file actions
423 lines (345 loc) · 13.4 KB
/
struct_pop_delta.py
File metadata and controls
423 lines (345 loc) · 13.4 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# -*- coding: utf-8 -*-
'''
Code for simulating the serial dilution model.
We consider a population with D demes and N_types types of individuals. The population composition is represented as an array e.g. [[1, 99], [0,100]] has 2 demes ; the first one has 1 mutant and 99 wild-types, the second has 100 wild-types.
The graph structure is implemented through the migration matrix.
'''
import numpy as np
from numba import njit
from numba import set_num_threads
set_num_threads(1)
#%%______________________________________Migration matrices________________________________________________
def define_clique(D,m):
'''
Function to define the clique migration matrix.
Parameters
----------
D : int
Number of demes.
m : float
Per-capita migration rate.
Returns
-------
migration_matrix : array
Migration matrix for the clique.
'''
migration_matrix=np.zeros((D,D))
for i in range(D):
for j in range(D):
if not i==j :
migration_matrix[i,j]=m
migration_matrix[i,i]=1-(D-1)*m
return migration_matrix
def define_cycle(D,mA,mC):
'''
Function to define the cycle migration matrix.
Parameters
----------
D : int
Number of demes.
mA : float
Migration rate, anticlockwise direction.
mC : float
Migration rate, clockwise direction.
Returns
-------
migration_matrix : array
Migration matrix for the cycle.
'''
migration_matrix=np.zeros((D,D))
for i in range(D-1):
migration_matrix[i,i+1]=mA
migration_matrix[i+1,i]=mC
migration_matrix[D-1,0]=mA
migration_matrix[0,D-1]=mC
for j in range(D):
migration_matrix[j,j]=1-mA-mC
return migration_matrix
def define_star(D,mI,mO,equal_contribution=True):
'''
Function to define the star migration matrix.
Parameters
----------
D : int
Number of demes.
mI : float
Inward migration rate.
mO : float
Outward migration rate.
equal_contribution : bool, optional
If True: sum on lines = 1 (all demes send the same number of individuals on average).
If False: sum on columns = 1 (all demes receive the same number on average).
The default is True.
Returns
-------
migration_matrix : array
Migration matrix for the star.
'''
migration_matrix=np.zeros((D,D))
for i in range(1,D):
migration_matrix[i,0]=mI
migration_matrix[0,i]=mO
if equal_contribution:
migration_matrix[i,i]=1-mI
else:
migration_matrix[i,i]=1-mO
if equal_contribution:
migration_matrix[0,0]=1-(D-1)*mO
else:
migration_matrix[0,0]=1-(D-1)*mI
return migration_matrix
def random_graph(adj_matrix,convention,alpha_values=None):
'''
Generates a random migration matrix from a Dirichlet distribution, with alpha values given as parameters.
Convention is either 'eqsize' (demes have size K on average) or 'eqcon' (demes contribute by K on average).
Parameters:
----------
adj_matrix : array
Adjacency matrix representing the graph structure.
convention : str
Either 'eqsize' or 'eqcon', specifying the type of equilibrium to consider.
alpha_values : array, optional
Alpha values for sampling the Dirichlet distribution. If None, uniform values are used.
Returns
-------
M : array
Random migration matrix.
'''
D=np.shape(adj_matrix)[0]
M=np.zeros((D,D))
if alpha_values is None:
alphas=np.ones((D,D))
else:
alphas=alpha_values
neighbour_out=np.array([np.argwhere(adj_matrix[i,:]==1).flatten() for i in range(D)],dtype=object)
neighbour_in=np.array([np.argwhere(adj_matrix[:,i]==1).flatten() for i in range(D)],dtype=object)
if convention=='eqsize':
for i in range(D):
alphas_in=np.array([alphas[j,i] for j in neighbour_in[i]])
sample_rates=np.random.dirichlet(alphas_in)
M[neighbour_in[i].astype(np.int64),i]=sample_rates
elif convention=='eqcon':
for i in range(D):
alphas_out=np.array([alphas[i,j] for j in neighbour_out[i]])
sample_rates=np.random.dirichlet(alphas_out)
M[i,neighbour_out[i]]=sample_rates
return M
def define_line(D,mR,mL, equal_contribution = True):
'''
Function to define the line migration matrix.
Parameters
----------
D : int
Number of demes.
mR : float
Rightward migration rate.
mL : float
Leftward migration rate.
equal_contribution : bool, optional
If True: sum on lines = 1 (all demes send the same number of individuals on average).
If False: sum on columns = 1 (all demes receive the same number on average).
The default is True.
Returns
-------
migration_matrix : array
Migration matrix for the line.
'''
migration_matrix=np.zeros((D,D))
for i in range(D-1):
migration_matrix[i,i+1]=mR
migration_matrix[i+1,i]=mL
for j in range(1, D-1):
migration_matrix[j,j]=1-mR-mL
if equal_contribution:
migration_matrix[0,0] = 1-mR
migration_matrix[D-1,D-1] = 1-mL
else:
migration_matrix[0,0] = 1-mL
migration_matrix[D-1,D-1] = 1-mR
return migration_matrix
#%%_________________________________________Growth and dilution/migration_____________________________________________
@njit
def growth_event(in_numbers,fitnesses,t):
'''
Describe the deterministic growth event for time t.
Parameters
----------
in_numbers : List or array
Initial demes' compositions before the growth phase.
fitnesses : list or array
list with the mutant and wild-type fitnesses, i.e., [1, 1+s]. It can also be a list of lists (in gradient case).
t : int
Time duration of the growth event.
Returns
-------
Array
Returns the number of individuals (same shape as in_numbers) after the growth time t.
'''
return (in_numbers * np.exp(fitnesses * t)).astype(np.float64)
@njit
def dilution_migration_event(in_numbers,migration_matrix,K):
'''
Dilution and migration event.
Parameters
----------
in_numbers : array or list
Composition of the demes at the end of the growth phase.
migration_matrix : array
Migration matrix, one of those defined above with parameters specific for my case.
K : int
Bottleneck number.
Returns
-------
new_numbers : array
Composition of the demes after migration and dilution steps.
'''
D, N_types= np.shape(in_numbers)
new_numbers=np.zeros((D, N_types), dtype=np.int64)
migrants_ij=np.empty(2, dtype=np.int64)
for i in range(D):
Ni=np.sum(in_numbers[i,:], dtype = np.int64)
if Ni<1:
print('extinct deme', i)
p=in_numbers[i,0]/Ni
for j in np.arange(D):
mij=migration_matrix[i,j]
p0=np.float64(max(min(K*p*mij/Ni,1),0))
p1=np.float64(max(min(K*(1-p)*mij/Ni,1),0))
migrants_ij[0]=np.random.binomial(Ni, p0,1)[0]
migrants_ij[1]=np.random.binomial(Ni, p1,1)[0]
new_numbers[j, 0]+=migrants_ij[0]
new_numbers[j, 1]+=migrants_ij[1]
return new_numbers
#%%_________________________________________Extinction or fixation____________________________________________________________
@njit
def extinct_mutant(numbers):
''' Check if the mutant population is extinct. '''
D = numbers.shape[0]
for i in range(D):
if numbers[i,0]>0:
return False
return True
@njit
def extinct_wild(numbers):
''' Check if the wild-type population is extinct. '''
D, N_types= numbers.shape
for i in range(D):
for j in range(N_types -1):
if numbers[i,j+1]>0:
return False
return True
#%%____________________________________Complete simulation for one trajectory_____________________________________________
@njit
def cycle(in_numbers, migration_matrix, fitnesses, nb_cycles, growth_factor, K, start_follow_numbers, size_follow_numbers, start_cycle, print_frequency):
''' Simulate one cycle of serial dilutions.
Parameters
----------
in_numbers : array
Conditions of the system before beginning cycle.
migration_matrix : array
Matrix containing the migration rates for the spatial structure.
fitnesses : array
Fitnesses of mutants and wild-types in the demes.
nb_cycles : int
A priori number of how many cycles we do before observing fixation or extinction.
growth_factor : int
t, the deterministic growth duration.
K : inte
Bottleneck size.
start_follow_numbers : array
The array of initial start numbers to follow. Can be also 'None'.
size_follow_numbers : int
Size of the output array we want to save.
start_cycle : int
Number of the cycle at which we want to start saving the dynamics.
print_frequency : int
The frequency at which we print the state.
save_dynamics : bool, optional
If True: saves the trajectories
If False: does not save the trajectories.
The default is False.
Returns
-------
follow_numbers : array
The array containing the numbers tracked over the cycles.
end_cycle : int
The cycle at which the simulation ended.
fixation : bool
Whether fixation occurred (True) or not (False).
'''
D, N_types= np.shape(in_numbers)
if start_follow_numbers is None:
follow_numbers=np.zeros((size_follow_numbers,D,N_types), dtype=np.int64)
else:
follow_numbers=start_follow_numbers.copy()
fixation=True
numbers=in_numbers.copy()
#Booleans that checks if mutants or wild-types are extinct
keep_going=True
for i in range(nb_cycles):
end_cycle = nb_cycles
numbers1=growth_event(numbers,fitnesses,growth_factor)
numbers=dilution_migration_event(numbers1,migration_matrix,K)
if (start_cycle+i)%print_frequency==0 and ((i+start_cycle)/print_frequency)<size_follow_numbers:
follow_numbers[int(i+start_cycle), :, :]=numbers
if extinct_mutant(numbers):
keep_going=False
fixation=False
end_cycle=i+start_cycle
follow_numbers[int((i+start_cycle))+1:]=numbers
break
if extinct_wild(numbers):
keep_going=False
fixation=True
end_cycle=i+start_cycle
follow_numbers[int((i+start_cycle))+1:]=numbers
break
#If mutants are not extinct or fixed at the end of nb_cycles cycles, we keep going
if keep_going:
follow_numbers, end_cycle, fixation= cycle(numbers, migration_matrix, fitnesses, nb_cycles, growth_factor, K, follow_numbers, size_follow_numbers, start_cycle+end_cycle, print_frequency)
return follow_numbers, end_cycle, fixation
#%%________________________________________Fixation probability computed on several simulations_______________________________________________
@njit
def fixation_probability(in_numbers, migration_matrix, fitnesses, nb_sim, nb_cycles, growth_factor, K, size_follow_numbers=10000, print_frequency=1, save_dynamics=False):
''' Computes the fixation probability over several simulations, using the 'cycle' function as of above, starting from in_numbers, on nb_sim simulations.
Parameters (see above).
Returns:
- average_extinction_cycle : float
The average time at which extinction occurred.
- ci95_ec : float
The 95% confidence interval for the average extinction time.
- average_fixation_cycle : float
The average time at which fixation occurred.
- ci95_fc : float
The 95% confidence interval for the average fixation time.
- proba : float
The fixation probabilitys.
'''
fix_count=0
fix_cycle=np.zeros(nb_sim)
ex_cycle=np.zeros(nb_sim)
for i in range(nb_sim):
start_cycle=0
start_follow_numbers=None
follow_numbers, end_cycle, fixation = cycle(in_numbers, migration_matrix, fitnesses, nb_cycles, growth_factor, K, start_follow_numbers, size_follow_numbers, start_cycle, print_frequency)
if fixation :
fix_count+=1
fix_cycle[i] = end_cycle
else:
ex_cycle[i] = end_cycle
#Number of extinctions
ex_count=nb_sim-fix_count
if fix_count>0:
average_fixation_cycle = np.sum(fix_cycle)/fix_count
variance_fixation_cycle = np.var(fix_cycle)
ci95_fc = 1.96 * np.sqrt(variance_fixation_cycle) / np.sqrt(fix_count)
else :
average_fixation_cycle = 0
if ex_count>0:
average_extinction_cycle = np.sum(ex_cycle)/ex_count
variance_extinction_cycle = np.var(ex_cycle)
ci95_ec = 1.96 * np.sqrt(variance_extinction_cycle) / np.sqrt(ex_count)
else:
average_extinction_cycle=0
proba = fix_count/nb_sim
return average_extinction_cycle, ci95_ec, average_fixation_cycle, ci95_fc, proba