-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrugRes_lattice.py
320 lines (244 loc) · 10.2 KB
/
DrugRes_lattice.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
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
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 14 14:01:41 2023
@author: cfruet
Use numba to accelerate the code.
The program aims at buiding the spatial structure of the lattice,
adding the antimicrobial at an user-defined time.
"""
#%%
from itertools import permutations
import numpy as np
from numpy import reshape
import math
import random
import sys
from numba import njit
#%%
@njit
def is_close_to_final_time(t, t_final, precision=1e-2):
"""
Check if t is close to t_final within the specified precision.
Parameters:
- t: The value to be checked.
- t_final: The target value.
- precision: The allowed absolute difference.
Returns:
- True if |t - t_final| < precision, False otherwise.
"""
return abs(t - t_final) < precision
#%%
def get_possible_migrations(node):
'''
Get the numbers indicating the places in the lattice where you can migrate, starting from node i.
Boundary conditions: periodic.
Parameters
----------
node : int
number of the node under consideration.
Returns
-------
possible_migrations : list
number of the nodes to which you can migrate.
'''
i, j = divmod(node, 4) # Convert node number to row and column indices
possible_migrations = []
# Add possible migrations for interior nodes
if i > 0:
possible_migrations.append(node - 4) # Migrate to node above
if i < 3:
possible_migrations.append(node + 4) # Migrate to node below
if j > 0:
possible_migrations.append(node - 1) # Migrate to node on the left
if j < 3:
possible_migrations.append(node + 1) # Migrate to node on the right
# Handle nodes on the edges
if j == 0:
possible_migrations.append(node + 3) # Migrate to node on the right edge of the same row
elif j == 3:
possible_migrations.append(node - 3) # Migrate to node on the left edge of the same row
if i == 0:
possible_migrations.append(node + 12) # Migrate to node on the bottom row
elif i == 3:
possible_migrations.append(node - 12) # Migrate to node on the top row
return possible_migrations
# Example usage
migrations = {}
for node in range(16):
migrations[node] = get_possible_migrations(node)
#%%
#@njit
def build_Vmatrix_lattice(D):
'''
Build the reaction matrix for a lattice of demes
Parameters
----------
D : int
number of demes in the system.
Returns
-------
V : matrix
matrix of systems' reactions.
'''
# reactions in deme: D demes, 5 reactions,2 species
rd = 5
reacts_indeme = D * rd
md = D-1
migrations = 4 * D
tot_reacts = reacts_indeme + migrations * 2
V = np.zeros((2*D, tot_reacts))
# This is inside the single demes (5 reactions for each deme)
l=0
for i in range(D):
V[2*i][0 + l*rd] = 1
V[2*i][1 + l*rd] = -1
V[2*i+1][2 + l*rd] = 1
V[2*i+1][3 + l*rd] = 1
V[2*i+1][4 + l*rd] = -1
l+=1
# Set the -1 for all reactions
n = 0
for m in range(D):
for k in range(4):
V[2*m][reacts_indeme + k + n*4] = -1
V[2*m+1][reacts_indeme + migrations + k + n*4] = -1
n+=1
for k in range(16):
tomig = get_possible_migrations(k)
for i in range(4):
V[2*tomig[i]][reacts_indeme + k*4 + i] = 1
for k in range(16):
tomig = get_possible_migrations(k)
for i in range(4):
V[2*tomig[i]+1][reacts_indeme + migrations + k*4 + i] = 1
return V
@njit
def simulate(V, realisations, Tadd, fs, gs, fsp, gsp, EP, K, mi1, fr, gr, gamma, js0, jr0, t_final, n):
extinctions_R = []
fixations_R = []
state_beforeAM = []
final_state = []
ks =[]
count_fix_beforeAM = 0
V = np.asarray(V, dtype=np.int64)
X = np.zeros((2*n,1), dtype=np.float64)
a = np.zeros((208,1), dtype=np.float64)
for k in range(realisations):
print('k=' + str(k))
antimicrobial_added = False
if k % 50 == 0:
print('k=' + str(k))
for i in range(16):
X[2*i] = js0
X[2*i + 1] = jr0
t = 0
fse = fs
gse = gs
## ++++++++++++++++++ Second loop (over time) ++++++++++++++++++ ##
while(t <= t_final):
for y in range(16):
a[0+ y*5] = fse * (1 - mi1) * (1 - (X[0 + y*2] + X[1 + y*2] )/ K ) * X[0 + y*2]
a[1+ y*5] = gse * X[0+ y*2]
a[2+ y*5] = fse * mi1 * (1 - (X[0+ y*2] + X[1+ y*2] )/ K ) * X[0+ y*2]
a[3+ y*5] = fr * (1 - (X[0+ y*2] + X[1+ y*2] )/ K ) * X[1+ y*2]
a[4+ y*5] = gr * X[1+ y*2]
for g in range(16):
for h in range(4):
a[80 + 4*g + h] = gamma * X[2*g]
for g in range(16):
for h in range(4):
a[144 + 4*g + h] = gamma * X[2*g+1]
for j in range(16):
if (X[0 + j*2] + X[1 + j*2] >= K):
#print('N exceeds K in deme ' + str(j))
a[0+ j*5] = 0
a[3+ j*5] = 0
asum = np.sum(a)
cumulative = np.cumsum(a) / asum
# ------ Random numbers extraction ------ #
xi = np.random.rand(2)
# --------- update time and check if time is higher than the antibiotic switching time --------- #
tau = (np.log(1 / xi[1]) / asum)
# --------- A) update time and check which reaction happens --------- #
if (t + tau ) < Tadd or ((t + tau ) >= Tadd and antimicrobial_added):
t = t + tau # update time normally :)
j = []
for i in range(len(cumulative)):
if xi[0] < cumulative[i] and cumulative[i] != 0:
j.append(i)
j_int = int(min(j)) # as the index for V must be integer
v_sel = V[:, j_int]
v_sel_contig = np.ascontiguousarray(v_sel)
new_shape = (32, 1)
v_sel_contig = v_sel_contig.reshape(new_shape)
X = X +v_sel_contig
if X[0] == 0 and X[1] == 0 and X[2] == 0 and X[3] == 0 and X[4] == 0 and X[5] == 0 and X[6] == 0 and X[7] == 0 and X[8] == 0 and X[9] == 0 and X[10] == 0 and X[11] == 0 and X[12] == 0 and X[13] == 0 and X[14] == 0 and X[15] == 0 and X[16] == 0 and X[17] == 0 and X[18] == 0 and X[19] == 0 and X[20] ==0 and X[21] == 0 and X[22] == 0 and X[23] ==0 and X[24] == 0 and X[25] == 0 and X[26] ==0 and X[27] ==0 and X[28] == 0 and X[29] ==0 and X[30] == 0 and X[31] == 0:
print('population extict')
#print(X)
ks.append(k)
final_state.append(X.copy())
extinctions_R.append(t)
if len(final_state) != len(state_beforeAM):
state_beforeAM.append(X.copy())
break
if X[0] == 0 and X[1] >= 0.9* EP and X[2] == 0 and X[3] >= 0.9* EP and X[4] == 0 and X[5] >= 0.9* EP and X[6] == 0 and X[7] >= 0.9* EP and X[8] == 0 and X[9] >= 0.9* EP and X[10] == 0 and X[11] >= 0.9* EP and X[12] == 0 and X[13] >= 0.9* EP and X[14] == 0 and X[15] >= 0.9* EP and X[16] == 0 and X[17] >= 0.9* EP and X[18] == 0 and X[19] >= 0.9* EP and X[20] == 0 and X[21] >= 0.9* EP and X[22] == 0 and X[23] >= 0.9 * EP and X[24] == 0 and X[25] >= 0.9 *EP and X[26] == 0 and X[27] >= 0.9 *EP and X[28] == 0 and X[29] >= 0.9 * EP and X[30] == 0 and X[31] >= 0.9*EP:
print('population survived')
ks.append(k)
final_state.append(X.copy())
fixations_R.append(t)
if len(final_state) != len(state_beforeAM):
print('pop fix before AM')
state_beforeAM.append(X.copy())
count_fix_beforeAM += 1
break
if is_close_to_final_time(t, t_final, 1e-3):
print("t is close to t_final within the specified precision.")
print('tfinal')
ks.append(k)
final_state.append(X.copy())
break
# --------- B) set switch and change rates --------- #
elif (t + tau ) >= Tadd and not antimicrobial_added:
print("tadd reached")
t = Tadd
state_beforeAM.append(X.copy())
fse = fsp
gse = gsp
print('Add antimicrobial')
antimicrobial_added = True
return ks, count_fix_beforeAM, extinctions_R, fixations_R, state_beforeAM, final_state
#%% Parameters
V = build_Vmatrix_lattice(16)
fs = 1
gs = 0.1
fsp = 0
gsp = 0.1
fr = 1
gr = 0.1
gamma= ( 15/4 ) * 1e-7
mi1 = 1e-5
K = 100
js0 = 10
jr0 = 0
n = 16 # number of demes, spatial structure
Tadd = 500000
realisations = 100 # Number of simulations (=runs)
t = 0
t_final = 1000000000
EP = (1 - gs / fs) * K
#%%
ks, count_fix_beforeAM, extinctions_R, fixations_R, state_beforeAM, final_state_end = simulate(V, realisations, Tadd, fs, gs, fsp, gsp, EP, K, mi1, fr, gr, gamma, js0, jr0, t_final, n)
#%%
print(sys.argv)
i = int(sys.argv[1])
print(i)
output_surv = f'survival_LATTICE_T{Tadd}_{i}.npy'
output_ext = f'extinction_LATTICE_T{Tadd}_{i}.npy'
output_state = f'state_LATTICE_beforeAM_T{Tadd}_{i}.npy'
final_state = f'state_LATTICE_final_T{Tadd}_{i}.npy'
count_fix_bAM = f'count_fix_bAM_LATTICE_T{Tadd}_{i}.npy'
np.save(output_state, state_beforeAM)
np.save(output_surv, fixations_R)
np.save(output_ext, extinctions_R)
np.save(final_state, final_state_end)
np.save(count_fix_bAM, count_fix_beforeAM)