-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel1Module.R
More file actions
522 lines (476 loc) · 20.4 KB
/
Copy pathmodel1Module.R
File metadata and controls
522 lines (476 loc) · 20.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
library(shinycssloaders)
model1UI <- function() {
tabPanel(
title = "Carvacrol at sub-MIC concentrations",
value = "Carvacrol_SubMIC",
sidebarLayout(
sidebarPanel(
fluidRow(
column(
10,
radioButtons("model1StrainID", "Preset data for:",
c("Escherichia coli" = "ecoli", "
Bacillus cereus" = "bcereus"),
inline = TRUE
),
bsTooltip("strainData",
"Predefined data and parameters for E.coli and B.cereus.",
"bottom",
options = list(container = "body")
)
),
column(
5,
numericInput(
inputId = "kS",
HTML("<span class='nobr'>k<span class='supsub'><br/>S</span> </span><span>(h<sup>-1</sup>)</span>"),
value = NULL,
step = 0.00001,
width = "100%"
),
bsTooltip("kS",
"Positive constant characterising the rate of the state transition.",
"right",
options = list(container = "body")
),
numericInput(
inputId = "kG",
HTML("<span class='nobr'>k<span class='supsub'><br/>g</span> </span><span>(h<sup>-1</sup>)</span>"),
value = NULL,
step = 0.0000001,
width = "100%"
),
bsTooltip("kG",
"Rate of growth as antimicrobial concentration increases",
"right",
options = list(container = "body")
),
numericInput(
inputId = "kD",
HTML("<span class='nobr'>k<span class='supsub'><br/>d</span> </span><span>(h<sup>-1</sup>)</span>"),
value = NULL,
step = 0.00000001,
width = "100%"
),
bsTooltip("kD",
"Rate of decay as antimicrobial concentration increases",
"right",
options = list(container = "body")
),
),
column(
5, numericInput(
inputId = "mug0",
HTML("<span class='nobr'>μ<span class='supsub'>0<br/>g</span> </span><span>(h<sup>-1</sup>)</span>"),
value = NULL,
step = 0.0001,
width = "100%"
),
bsTooltip("mug0",
"Maximum growth rate in absence of antimicrobial",
"right",
options = list(container = "body")
)
),
column(
5, numericInput(
inputId = "mud0",
HTML("<span class='nobr'>μ<span class='supsub'>0<br/>m</span> </span><span>(h<sup>-1</sup>)</span>"),
value = NULL,
step = 0.000001,
width = "100%"
),
bsTooltip("mud0",
"Maximum death rate in absence of antimicrobial",
"right",
options = list(container = "body")
)
),
),
sliderInput("model1DiscTimes",
label = "Discretization points",
step = 10,
value = 100,
min = 10,
max = 200
),
bsTooltip("model1DiscTimes",
"Number of intermediate points for modelling",
"bottom",
options = list(container = "body")
)
),
mainPanel(
tabsetPanel(
tabPanel(
"Plot", withSpinner(plotlyOutput("model1PlotSingle", height = "400px"), image = "https://github.com/apedreira/microracle/blob/main/var/img/customLoading.gif?raw=true")
),
tabPanel(
"Reference", HTML("<br> <p>
Pedreira, A., Vázquez, J. A., & García, M. R. (2022). Modelling the antimicrobial effect of food preservatives in bacteria: Application to <i>Escherichia coli</i> and <i>Bacillus cereus</i> inhibition with carvacrol.
<i>Journal of Food Engineering</i>, 361, 111734. doi: <a href='https://doi.org/10.1016/j.jfoodeng.2023.111734' target='_blank'>doi.org/10.1016/j.jfoodeng.2023.111734</a></p>
")
)
)
)
),
fluidRow(
style = "padding:16px",
tabsetPanel(
id = "model1_tabsetPanel",
type = "tabs",
tabPanel(
"Single experiment",
column(12,
align = "center", style = "padding:16px",
actionButton("runSingle", "Run", class = "btn-success"),
actionButton("resetSingle", "Reset", class = "btn-warning"),
),
sidebarPanel(
width = 4,
rHandsontableOutput("table", height = "400px")
)
),
tabPanel(
"Multiple experiment",
column(12,
align = "center", style = "padding:16px",
actionButton("runMultiple", "Run", style = "padding:8px 16px; margin-right: 16px;font-size:120%", class = "btn-success"),
actionButton("resetMultiple", "Reset", style = "padding:8px 16px; font-size:120%", class = "btn-success", class = "btn-warning")
),
sidebarPanel(
width = 12,
fluidRow(
column(
width = 12,
fluidRow(
column(
width = 2,
textInput("colHeader1", "Exp#1 name:", value = "Control", width = "40%")
),
column(
width = 2,
textInput("colHeader2", "Exp#2 name:", value = "Exp2", width = "40%")
),
column(
width = 2,
textInput("colHeader3", "Exp#3 name:", value = "Exp3", width = "40%")
),
column(
width = 2,
textInput("colHeader4", "Exp#4 name:", value = "Exp4", width = "40%")
),
column(
width = 2,
textInput("colHeader5", "Exp#5 name:", value = "Exp5", width = "40%")
)
),
fluidRow(
column(
width = 12,
rHandsontableOutput("table2", height = "400px")
)
)
)
)
)
)
),
),
)
}
model1Server <- function(input, output, session) {
observe({
simpleDataCar <- simpleDataCar(input$model1StrainID)
# Use updateXInput to avoid triggering automatic rendering when updating parameters
updateNumericInput(session, "kS", value = simpleDataCar[4])
updateNumericInput(session, "kG", value = simpleDataCar[5])
updateNumericInput(session, "kD", value = simpleDataCar[6])
updateNumericInput(session, "mug0", value = simpleDataCar[7])
updateNumericInput(session, "mud0", value = simpleDataCar[8])
updateSliderInput(session, "model1DiscTimes", value = 100)
# Call external function simpleDataCar to create a data frame
# and set predefined experimental/modelling values for the initial plot
exp_df <- data.frame(
Time = unlist(simpleDataCar[1]),
y = simpleDataCar[[2]][[2]],
drug = unlist(simpleDataCar[[3]][[2]])
)
texto <- input$model1StrainID
# Initialize reactive values for the data frame
datavalues <- reactiveValues(data = exp_df)
# Create a handsontable for the data and validate that content is numerical
output$table <- renderRHandsontable({
rhandsontable(datavalues$data, maxRows = 100, colHeaders = c("Time (h)", "CFU/mL", "Carvacrol concentration (mg/L)")) %>%
hot_validate_numeric(col = c(1, 2, 3), min = 0)
})
alreadyOpened <- FALSE
# Observe the selected tab (Single vs. Multiple experiments)
observeEvent(input$model1_tabsetPanel, {
### ----------------------------##
### CODE FOR SINGLE EXPERIMENT ##
### ----------------------------##
# Flag to check if the single experiment was already executed. Prevents
# loss of previous plot when switching tabs by blocking empty plot rendering.
alreadyExecuted <- FALSE
if (input$model1_tabsetPanel == "Single experiment") {
alreadyExecuted <- TRUE
if (TRUE) {
# Render empty plot frame
output$model1PlotSingle <- renderPlotly({
plot_ly(
x = c(0), y = c(0), type = "scatter",
mode = "markers", color = "white"
) %>%
layout(
yaxis = list(showexponent = "all", exponentformat = "E", title = "Log<sub>10</sub> CFU/mL"),
xaxis = list(title = "Time (h)")
)
})
}
# Update data frame values and plot only when clicking "Run". Prevents automatic rendering.
observeEvent(
input$runSingle,
#
{
datavalues$data <- hot_to_r(input$table)
validation_msg <- dataValidatorSingle(datavalues$data)
# Ensure Time, Drug, and CFU/mL columns have matching lengths (excluding NA values)
if (validation_msg == "OK") {
# Update data frame by extracting values from the handsontable
datavalues$data <- hot_to_r(input$table)
# Pack equation parameters into a vector
p1 <- input$mug0
p2 <- input$mud0
p3 <- input$kS
p4 <- input$kG
p5 <- input$kD
params <- c(p1, p2, p3, p4, p5)
# Retrieve discretization points
discTimes <- input$model1DiscTimes
# Extract experimental data from data frame
y_exp <- unlist(datavalues$data$y)
c_exp <- unlist(datavalues$data$drug)
time_exp <- unlist(datavalues$data$Time)
# Execute modelling function and store output
submic_car_func <- submiC3(time_exp, c_exp, params, y_exp, discTimes)
time_mod <- submic_car_func[[1]]
y_mod <- submic_car_func[[2]]
# Plot results (markers for experimental data, lines for model output)
output$model1PlotSingle <- renderPlotly({
plot_ly(datavalues$data,
x = ~Time, y = log10(datavalues$data$y), name = "Observed", type = "scatter",
mode = "markers", color = "white"
) %>%
add_lines(name = "Expected", x = time_mod, y = log10(y_mod), mode = "line") %>%
layout(
yaxis = list(showexponent = "all", exponentformat = "E", title = "Log<sub>10</sub> CFU/mL"),
xaxis = list(title = "Time (h)")
)
})
}
}
)
# Reset data frame, handsontable, and plot to default state
observeEvent(
input$resetSingle,
{
datavalues$data <- exp_df
output$table <- renderRHandsontable({
rhandsontable(datavalues$data, maxRows = 100, colHeaders = c("Time (h)", "CFU/mL", "Carvacrol concentration (mg/L)")) %>%
hot_validate_numeric(col = c(1, 2, 3), min = 0)
})
simpleDataCar <- simpleDataCar(input$model1StrainID)
updateNumericInput(session, "kS", value = simpleDataCar[4])
updateNumericInput(session, "kG", value = simpleDataCar[5])
updateNumericInput(session, "kD", value = simpleDataCar[6])
updateNumericInput(session, "mug0", value = simpleDataCar[7])
updateNumericInput(session, "mud0", value = simpleDataCar[8])
updateSliderInput(session, "model1DiscTimes", value = 100)
# Render empty plot frame
output$model1PlotSingle <- renderPlotly({
plot_ly(
x = c(0), y = c(0), type = "scatter",
mode = "markers", color = "white"
) %>%
layout(
yaxis = list(showexponent = "all", exponentformat = "E", title = "Log<sub>10</sub> CFU/mL"),
xaxis = list(title = "Time (h)")
)
})
}
)
}
### -------------------------------##
### CODE FOR MULTIPLE EXPERIMENT ##
### -------------------------------##
else if (input$model1_tabsetPanel == "Multiple experiment") {
colHeaders <- c(
"Time (h)",
"Control CFU/mL",
"Control Carvacrol (mg/L)",
"Exp#2 CFU/mL",
"Exp#2 Carvacrol (mg/L)",
"Exp#3 CFU/mL",
"Exp#3 Carvacrol (mg/L)",
"Exp#4 CFU/mL",
"Exp#4 Carvacrol (mg/L)",
"Exp#5 CFU/mL",
"Exp#5 Carvacrol (mg/L)",
"Exp#6 CFU/mL",
"Exp#6 Carvacrol (mg/L)"
)
# Create data frame and set predefined values for initial plot
preset_df <- data.frame(
exp1_t = unlist(simpleDataCar[1]),
exp1_y = simpleDataCar[[2]][[1]],
exp1_d = unlist(simpleDataCar[[3]][[1]]),
exp2_y = simpleDataCar[[2]][[2]],
exp2_d = unlist(simpleDataCar[[3]][[2]]),
exp3_y = simpleDataCar[[2]][[3]],
exp3_d = unlist(simpleDataCar[[3]][[3]]),
exp4_y = simpleDataCar[[2]][[4]],
exp4_d = unlist(simpleDataCar[[3]][[4]]),
exp5_y = simpleDataCar[[2]][[5]],
exp5_d = unlist(simpleDataCar[[3]][[5]])
)
# Render empty plot frame
output$model1PlotSingle <- renderPlotly({
plot_ly(
x = c(0), y = c(0), name = "Observed", type = "scatter",
mode = "markers", color = "white",
) %>%
layout(
yaxis = list(showexponent = "all", exponentformat = "E", title = "Log<sub>10</sub> CFU/mL"),
xaxis = list(title = "Time (h)")
)
})
# Make data frame reactive to changes
outputValues <- reactiveValues(data = preset_df)
colHeaders <- function() {
input_count <- 1
headers <- character(12)
headers[1] <- "Time (h)"
for (i in seq(from = 2, to = 10, by = 2)) {
headers[i] <- paste0(input[[paste0("colHeader", input_count)]], " CFU/mL")
headers[i + 1] <- paste0(input[[paste0("colHeader", input_count)]], " Carvacrol (mg/L)")
input_count <- input_count + 1
}
return(headers)
}
# outputValues$data=hot_to_r(input$table2)
# Create handsontable using dynamic input values for column headers
output$table2 <- renderRHandsontable({
rhandsontable(outputValues$data, maxRows = 100, colHeaders = colHeaders()) %>%
hot_validate_numeric(col = c(1:11), min = 0)
})
# Loop to observe changes in input values and update data frame/handsontable column names
for (i in 1:5) {
observeEvent(input[[paste0("colHeader", i)]], {
colnames(preset_df)[i] <- input[[paste0("colHeader", i)]]
})
}
# Apply handsontable changes to data frame when clicking "Run"
observeEvent(input$runMultiple, {
outputValues$data <- hot_to_r(input$table2)
colHeaders <- colHeaders()
validation <- dataValidatorMultiple(outputValues$data)
validation_msg <- validation[[1]]
validated_subsetDF <- validation[[2]]
if (validation_msg == "OK") {
# Pack parameters into a vector
p1 <- input$mug0
p2 <- input$mud0
p3 <- input$kS
p4 <- input$kG
p5 <- input$kD
params <- c(p1, p2, p3, p4, p5)
# Retrieve discretization points
discTimes <- input$model1DiscTimes
# Plot results
output$model1PlotSingle <- renderPlotly({
# Initialize a scatter plot
p <- plot_ly(type = "scatter", mode = "markers")
# Initialize loop counters
l <- 2
exp_name <- 2
# Create a palette with 10 colors for traces and lines
plotColors <- c("#404040", "#619cff", "#e69f00", "#cc79a7", "#78ac44", "#d55e00", "#0072b2", "#ffce00", "#751056", "#01665e")
# Unlist Time column from data frame
time_exp <- unlist(validated_subsetDF[1])
# Iterate over data frame to dynamically add traces and lines to plot
while (l < (length(validated_subsetDF) + 1)) {
y_exp <- unlist(validated_subsetDF[l])
drug_conc <- unlist(validated_subsetDF[l + 1])
# Execute model function and store results
submic_car_func <- submiC3(time_exp, drug_conc, params, y_exp, discTimes)
time_mod <- submic_car_func[[1]]
y_mod <- log10(submic_car_func[[2]])
# Retrieve column name for legend, removing the "CFU/mL" suffix
currentColName <- colHeaders[exp_name]
legendName <- substr(currentColName, 1, nchar(currentColName) - 6)
# Trace plot: Markers represent experimental data, lines represent model predictions
p <- add_trace(p, x = unlist(validated_subsetDF[1]), y = log10(y_exp), mode = "markers", name = paste0(legendName, " (observed)"), marker = list(color = plotColors[l - 1]))
p <- add_lines(p, x = time_mod, y = y_mod, mode = "line", name = paste0(legendName, " (expected)"), line = list(color = plotColors[l - 1]))
l <- l + 2
exp_name <- exp_name + 2
}
p %>%
layout(
yaxis = list(showexponent = "all", exponentformat = "E", title = "Log<sub>10</sub> CFU/mL"),
xaxis = list(title = "Time (h)")
)
})
}
})
# Reset data frame, handsontable, and plot to default state when clicking "Reset"
observeEvent(
input$resetMultiple,
{
outputValues$data <- preset_df
output$table2 <- renderRHandsontable({
rhandsontable(outputValues$data, maxRows = 100) %>%
hot_validate_numeric(col = c(1, 2, 3), min = 0)
})
output$table2 <- renderRHandsontable({
rhandsontable(outputValues$data, maxRows = 100, colHeaders = c(
"Time (h)",
"Control CFU/mL",
"Control Carvacrol (mg/L)",
"Exp#2 CFU/mL",
"Exp#2 Carvacrol (mg/L)",
"Exp#3 CFU/mL",
"Exp#3 Carvacrol (mg/L)",
"Exp#4 CFU/mL",
"Exp#4 Carvacrol (mg/L)",
"Exp#5 CFU/mL",
"Exp#5 Carvacrol (mg/L)",
"Exp#6 CFU/mL",
"Exp#6 Carvacrol (mg/L)"
)) %>%
hot_validate_numeric(col = c(1, 2, 3), min = 0)
})
simpleDataCar <- simpleDataCar(input$model1StrainID)
updateNumericInput(session, "kS", value = simpleDataCar[4])
updateNumericInput(session, "kG", value = simpleDataCar[5])
updateNumericInput(session, "kD", value = simpleDataCar[6])
updateNumericInput(session, "mug0", value = simpleDataCar[7])
updateNumericInput(session, "mud0", value = simpleDataCar[8])
updateSliderInput(session, "model1DiscTimes", value = 100)
# Render empty plot frame
output$model1PlotSingle <- renderPlotly({
plot_ly(
x = c(0), y = c(0), type = "scatter",
mode = "markers", color = "white"
) %>%
layout(
yaxis = list(showexponent = "all", exponentformat = "E", title = "Log<sub>10</sub> CFU/mL"),
xaxis = list(title = "Time (h)")
)
})
}
)
}
})
})
}