forked from dkaschek/publication_dMod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsourcecode.R
257 lines (206 loc) · 8.51 KB
/
sourcecode.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
##
## Replication script to reproduce the results shown in the manuscript
##
library(dMod)
library(deSolve)
library(trust)
setwd("/tmp")
# Section 4.1. Simulation and prediction ------------------------------
# Add reactions
reactions <- NULL
reactions <- addReaction(reactions, "TCA_buffer", "TCA_cell",
rate = "import*TCA_buffer",
description = "Uptake")
reactions <- addReaction(reactions, "TCA_cell", "TCA_buffer",
rate = "export_sinus*TCA_cell",
description = "Sinusoidal export")
reactions <- addReaction(reactions, "TCA_cell", "TCA_cana",
rate = "export_cana*TCA_cell",
description = "Canalicular export")
reactions <- addReaction(reactions, "TCA_cana", "TCA_buffer",
rate = "reflux*TCA_cana",
description = "Reflux into the buffer")
# Translate into ODE model
mymodel <- odemodel(reactions, modelname = "bamodel")
# Generate prediction function from ODE model
x <- Xs(mymodel, condition = NULL)
times <- seq(0, 50, .1)
pars <- c(TCA_buffer = 1,
TCA_cell = 0,
TCA_cana = 0,
import = 0.2,
export_sinus = 0.2,
export_cana = 0.04,
reflux = 0.1)
# Generate the prediction
out <- x(times, pars)
plot(out)
# Get sensitivities for the rate parameters only, pars[4:7]
out <- getDerivs(x(times, pars[4:7], fixed = pars[1:3]))
plot(out)
# Section 4.2. Observation function and simulated data ---------------
# Generate observation function
observables <- eqnvec(
buffer = "s*TCA_buffer",
cellular = "s*(TCA_cana + TCA_cell)"
)
g <- Y(observables, f = reactions, condition = NULL,
compile = TRUE, modelname = "obsfn")
# Reset parameter values
pars["TCA_cell"] <- 0.3846154
pars["TCA_cana"] <- 0.1538462
pars["TCA_buffer"] <- 0
pars["s"] <- 1e3
out <- (g*x)(times, pars, conditions = "standard")
# Simuate data
set.seed(1)
timesD <- c(0.1, 1, 3, 7, 11, 15, 20, 41)
datasheet <- subset(wide2long(out),
time %in% timesD & name %in% names(observables))
datasheet$sigma <- sqrt(datasheet$value + 1)
datasheet$value <- rnorm(nrow(datasheet), datasheet$value, datasheet$sigma)
data <- as.datalist(datasheet)
plot(out, data)
# Section 4.3. Parameter transformation ------------------------------
p <- P(
trafo = eqnvec(
TCA_buffer = "0",
TCA_cell = "exp(TCA_cell)",
TCA_cana = "exp(TCA_cana)",
import = "exp(import)",
export_sinus = "exp(export_sinus)",
export_cana = "exp(export_cana)",
reflux = "exp(reflux)",
s = "exp(s)"
),
condition = "standard"
)
outerpars <- getParameters(p)
pouter <- structure(rep(-1, length(outerpars)), names = outerpars)
plot((g*x*p)(times, pouter), data)
# 4.4. Objective function and model fitting --------------------------
obj <- normL2(data, g*x*p) + constraintL2(pouter, sigma = 10)
myfit <- trust(obj, pouter, rinit = 1, rmax = 10)
plot((g*x*p)(times, myfit$argument), data)
out_mstrust <- mstrust(obj, pouter, rinit = 1, rmax = 10, iterlim = 500,
sd = 4,
cores = 4, fits = 50)
myframe <- as.parframe(out_mstrust)
plotValues(myframe, value < 100)
plotPars(myframe, value < 100)
# 4.5. Working with several conditions -------------------------------
pars["reflux"] <- 1e3
out <- (g*x)(times, pars, conditions = "open")
datasheet <- subset(wide2long(out),
time %in% timesD & name %in% names(observables))
datasheet$sigma <- sqrt(datasheet$value + 1)
datasheet$value <- rnorm(nrow(datasheet), datasheet$value, datasheet$sigma)
data <- data + as.datalist(datasheet)
trafo <- summary(p)$standard$equations
trafo["reflux"] <- "exp(reflux_open)"
p <- p + P(trafo, condition = "open")
outerpars <- getParameters(p)
pouter <- structure(rep(-1, length(outerpars)), names = outerpars)
obj <- normL2(data, g*x*p) + constraintL2(pouter, sigma = 10)
out_mstrust <- mstrust(obj, pouter, rinit = 1, rmax = 10, iterlim = 500,
sd = 4, cores = 4, fits = 50)
myframe <- as.parframe(out_mstrust)
plotValues(myframe, value < 100)
plotPars(myframe, value < 100)
bestfit <- as.parvec(myframe)
plot((g*x*p)(times, bestfit), data)
# 4.6. Parameter uncertainty and identifiability ---------------------
profiles <- profile(obj, bestfit, names(bestfit), limits = c(-5, 5), cores = 4)
plotProfile(profiles)
plotPaths(profiles, whichPar = "s")
# 4.7. Steady-state constraints and implicit transformations ---------
pSS <- NULL
trafos <- summary(p)
conditions <- names(trafos)
for (n in conditions) {
equations <- trafos[[n]]$equations
equations["TCA_cana"] <- "exp(export_cana)*exp(TCA_cell)/exp(reflux)"
pSS <- pSS + P(equations, condition = n)
}
outerpars <- getParameters(pSS)
pouter <- structure(rep(-1, length(outerpars)), names = outerpars)
obj <- normL2(data, g*x*pSS) + constraintL2(pouter, sigma = 10)
bestfit <- trust(obj, pouter, rinit = 1, rmax = 10)$argument
profiles_SS_analytic <- profile(obj, bestfit, names(bestfit), limits = c(-5, 5), cores = 4)
plotProfile(list(noSS = profiles, SS_explicit = profiles_SS_analytic))
# Add reactions
reactions <- NULL
reactions <- addReaction(reactions, "TCA_buffer", "TCA_cell",
rate = "import*TCA_buffer",
description = "Uptake")
reactions <- addReaction(reactions, "TCA_cell", "TCA_buffer",
rate = "export_sinus*TCA_cell",
description = "Sinusoidal export")
reactions <- addReaction(reactions, "TCA_cell", "TCA_cana",
rate = "export_cana*TCA_cell",
description = "Canalicular export")
reactions <- addReaction(reactions, "TCA_cana", "TCA_buffer",
rate = "(reflux*(1-switch) + reflux_open*switch)*TCA_cana",
description = "Reflux into the buffer")
reactions <- addReaction(reactions, "0", "switch",
rate = "0",
description = "Create a switch")
# Translate into ODE model
mymodel <- odemodel(reactions, modelname = "bamodel")
# Set up implicit parameter transformation
f <- as.eqnvec(reactions)[c("TCA_buffer", "TCA_cana", "TCA_cell")]
f["TCA_cell"] <- "TCA_buffer + TCA_cana + TCA_cell - TCA_tot"
pSS <- P(f, "TCA_tot", method = "implicit",
compile = TRUE, modelname = "pfn")
# Set up explicit parameter transformation
innerpars <- unique(c(getParameters(mymodel),
getSymbols(observables),
getSymbols(f)))
trafo <- as.eqnvec(innerpars, names = innerpars)
trafo[reactions$states] <- "0"
trafo <- replaceSymbols(innerpars,
paste0("exp(", innerpars, ")"),
trafo)
p <- P(trafo)
# Set up prediction function with events
event.buffer <- data.frame(var = "TCA_buffer",
time = 0,
value = 0,
method = "replace")
event.open <- data.frame(var = "switch",
time = 0,
value = 1,
method = "replace")
x <- Xs(mymodel,
events = event.buffer,
condition = "standard") +
Xs(mymodel,
events = rbind(event.buffer, event.open),
condition = "open")
# Generate observation function with modified states/parameters
g <- Y(observables,
states = reactions$states,
parameters = setdiff(innerpars, reactions$states),
compile = TRUE, modelname = "obsfn")
# Generate objective function
outerpars <- getParameters(p)
pouter <- structure(rep(-1, length(outerpars)), names = outerpars)
obj <- normL2(data, g*x*pSS*p) + constraintL2(pouter, sigma = 10)
bestfit <- trust(obj, pouter, rinit = 1, rmax = 10)$argument
profiles_SS_implicit <- profile(obj, bestfit, names(bestfit), limits = c(-5, 5), cores = 4)
plotProfile(list(noSS = profiles,
SS_explicit = profiles_SS_analytic,
SS_implicit = profiles_SS_implicit))
# 4.8. Prediction uncertainty and validation profiles ----------------
obj.validation <- datapointL2(name = "TCA_cell",
time = 41,
value = "d1",
sigma = .002,
condition = "standard")
myfit <- trust(obj + obj.validation,
parinit = c(d1 = 1, bestfit[-7]),
fixed = c(TCA_tot = log(1)),
rinit = 1, rmax = 10)
profile_prediction <- profile(obj + obj.validation,
myfit$argument, "d1", limits = c(-5, 5),
fixed = c(TCA_tot = log(1)))