-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathANP_model.R
240 lines (161 loc) · 8.01 KB
/
ANP_model.R
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
########################################################################
################## Virus Dynamics Model for Passaging ##################
########################################################################
###### Required libraries
library(deSolve)
########################################################################
###### Definition of functions ######
########################################################################
########################################################################
#### Case 1: beta, delta, c are the same for both viruses ####
########################################################################
### Virus dynamics model
model2 <- function(t, cinit, parms){
# definition of the ODE virus dynamics model
### input:
# t time
# cinit vector of initial conditions, eg. cinit<- c(U = 4e5, IE = 0, IK = 0, VE = 0.8*400, VK = 0.2*400 )
# parms vector of parmeter values, eg. parms <- c(beta = 2.7e-6, delta = 4, pE = 3, pK = 4, c = 3)
### output:
# list with differentials of U, IE, IK, VE, VK
with(as.list(c(cinit, parms)), {
dU = - beta * U * (VE + VK)
dIE = beta * U * VE - delta * IE
dIK = beta * U * VK - delta * IK
dVE = pE * IE - c * VE
dVK = pK * IK - c * VK
return(list(c(dU = dU, dIE = dIE, dIK = dIK, dVE = dVE, dVK = dVK)))
})
}
### Solving the ODE
solve_ode_model2 <- function(cinit, parms, t){
# solve ODE for a given set of parameters
### input:
# cinit vector of initial conditions, eg. cinit<- c(U = 4e5, IE = 0, IK = 0, VE = 0.8*400, VK = 0.2*400 )
# parms vector of parmeter values, eg. parms <- c(beta = 2.7e-6, delta = 4, pE = 3, pK = 4, c = 3)
# t vector with time steps, eg. t <- seq(0,5,0.1)
### output:
# matrix with time steps and the concentrations of U, IE, IK, VE, VK
out <- ode(y = cinit, times = t, parms = parms, func = model2, method="ode23")
return(out)
}
### Predicting virus abundance in passaging experiments
predicting_passage_model2 <- function(cinit, parms, t, Npass, Tpass){
# function to predict percentPB2.627K (mammalian-adapted) virus percentage
### input:
# cinit vector of initial conditions, eg. cinit<- c(U = 4e5, IE = 0, IK = 0, VE = 0.8*400, VK = 0.2*400 )
# parms vector of parmeter values, eg. parms <- c(beta = 2.7e-6, delta = 4, pE = 3, pK = 4, c = 3)
# t vector with time steps, eg. t <- seq(0,5,0.1)
# Npass number of passages, eg. Npas <- 5
# Tpass time (in steps) in simulation that corresponds to a passaging round, eg. Tpass<-6
### output:
# matrix of dimensions (Npass +1)x2 with passage number and percentPB2.627K
# extract initial conditions for cells (Uj) and infected cells (Iij)
cinit_or <- cinit[ 1:3]
# output matrix: passage number and %VK
out<- matrix(NA, nrow = Npass+1, ncol= 2, dimnames=list(rep("", Npass+1), c("passage", "percentPB2.627K")))
# generate the first passage
first <- solve_ode_model2(cinit, parms, t)
# store the values in output matrix
out[1,1] <- 0
out[1,2] <- first[1,"VK"]/(first[1,"VK"]+first[1,"VE"])
# determination for initial conditions in next round
cinit_next <- c(cinit_or, first[1+Tpass,"VE"], first[1+Tpass,"VK"])
# loop over the further passages
for( i in 1:Npass){
zw <- solve_ode_model2(cinit_next, parms, t)
out[1+i,1] <- i
out[1+i,2] <- zw[1,"VK"]/(zw[1,"VK"]+zw[1,"VE"])
# determination of new initial conditions
cinit_next <- c(cinit_or, zw[1+Tpass,"VE"], zw[1+Tpass,"VK"])
}
out[,2] <- 100*out[,2]
out
}
########################################################################
#### Case 2: beta, delta, c are different for each virus ####
########################################################################
### Virus dynamics model
model2_diffratesforvir <- function(t, cinit, parms){
# definition of the ODE virus dynamics model for different beta, delta, c for the different viral strains
### input:
# t time
# cinit vector of initial conditions, eg. cinit<- c(U = 4e5, IE = 0, IK = 0, VE = 0.8*400, VK = 0.2*400 )
# parms vector of parmeter values, eg. parms <- c(beta_E = 2.7e-6, beta_K = 2.7e-6, delta_E = 4, delta_K =4 , c_E = 3, c_K = 3, pE = 12, pK = 1)
### output:
# list with differentials of U, IE, IK, VE, VK
with(as.list(c(cinit, parms)), {
dU = - beta_E * U * VE - beta_K * U * VK
dIE = beta_E * U * VE - delta_E * IE
dIK = beta_K * U * VK - delta_K * IK
dVE = pE * IE - c_E * VE
dVK = pK * IK - c_K * VK
return(list(c(dU = dU, dIE = dIE, dIK = dIK, dVE = dVE, dVK = dVK)))
})
}
### Solving the ODE
solve_ode_model2_diffratesforvir <- function(cinit, parms, t){
# solve ODE for a given set of parameters for different beta, delta, c for the different viral strains
### input:
# cinit vector of initial conditions, eg. cinit<- c(U = 4e5, IE = 0, IK = 0, VE = 0.8*400, VK = 0.2*400 )
# parms vector of parmeter values, eg. parms <- c(beta_E = 2.7e-6, beta_K = 2.7e-6, delta_E = 4, delta_K =4 , c_E = 3, c_K = 3, pE = 12, pK = 1)
# t vector with time steps, eg. t <- seq(0,5,0.1)
### output:
# matrix with time steps and the concentrations of U, IE, IK, VE, VK
out <- ode(y = cinit, times = t, parms = parms, func = model2_diffratesforvir, method="ode23")
return(out)
}
### Predicting virus abundance in passaging experiments
predicting_passage_model2_diffratesforvir <- function(cinit = c(U = 4e5, IE=0, IK=0, VE=0.8*400, VK=0.2*400 ), parms, t, Npass, Tpass){
# function to predict percentPB2.627K (mammalian-adapted) virus percentage for different beta, delta, c for the different viral strains
### input:
# cinit vector of initial conditions, eg. cinit<- c(U = 4e5, IE = 0, IK = 0, VE = 0.8*400, VK = 0.2*400 )
# parms vector of parmeter values, eg. parms <- c(beta_E = 2.7e-6, beta_K = 2.7e-6, delta_E = 4, delta_K =4 , c_E = 3, c_K = 3, pE = 12, pK = 1)
# t vector with time steps, eg. t <- seq(0,5,0.1)
# Npass number of passages, eg. Npas <- 5
# Tpass time (in steps) in simulation that corresponds to a passaging round, eg. Tpass<-6
### output:
# matrix of dimensions (Npass +1)x2 with passage number and percentPB2.627K
# extract initial conditions for cells (Uj) and infected cells (Iij)
cinit_or <- cinit[ 1:3]
# output matrix: passage number and %VK
out<- matrix(NA, nrow = Npass+1, ncol= 2, dimnames=list(rep("", Npass+1), c("passage", "percentPB2.627K")))
# generate the first passage
first <- solve_ode_model2_diffratesforvir(cinit, parms, t)
# store the values in output matrix
out[1,1] <- 0
out[1,2] <- first[1,"VK"]/(first[1,"VK"]+first[1,"VE"])
# determination for initial conditions in next round
cinit_next <- c(cinit_or, first[1+Tpass,"VE"], first[1+Tpass,"VK"])
# loop over the further passages
for( i in 1:Npass){
zw <- solve_ode_model2_diffratesforvir(cinit_next, parms, t)
out[1+i,1] <- i
out[1+i,2] <- zw[1,"VK"]/(zw[1,"VK"]+zw[1,"VE"])
# determination of new initial conditions
cinit_next <- c(cinit_or, zw[1+Tpass,"VE"], zw[1+Tpass,"VK"])
}
out[,2] <- 100*out[,2]
out
}
########################################################################
########## Definition of the normalised risk score ###########
########################################################################
normalised_risk_score <- function(predictions){
# function that calculates the risk score of evolving a K mutation with range: [-1,1]
### input:
# predictions: matrix with columns passage, percentPB2.627K as generated with function predicting_passage_model2()
### output:
# normalised risk score
startK <- predictions[which(predictions[,"passage"]==0) , "percentPB2.627K"]
pred <- predictions[, "percentPB2.627K"] - startK
for(i in 1:(length(pred))){
if(pred[i] < 0){
pred[i] <- pred[i]/startK
}
else{
pred[i] <- pred[i]/(100-startK)
}
}
sum(pred)/ (dim(predictions)[1] -1)
}