-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
500 lines (496 loc) · 18.7 KB
/
app.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
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
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
options(shiny.maxRequestSize = 12 * 1024 ^ 2)
library(dplyr)
library(DT)
library(openxlsx)
library(plotrix)
library(shiny)
library(shinyBS)
library(RMySQL)
library(tidyr)
dataColumnNames <- readRDS('data/dataColumnNames.rds')
# SERVER Part ====
server <- function(input, output, session) {
rangesView <- reactiveValues(x = NULL, y = NULL)
rangesProcessing <- reactiveValues(x = NULL, y = NULL)
observeEvent(input$regSend, {
source('dbRegister.R', local = TRUE, echo = FALSE)
updateActionButton(
session, 'regSend',
label = "Successfully Sent!"
)
},
ignoreNULL = TRUE
)
observeEvent(input$dataViewPlot_dblClick, {
brush <- input$dataViewPlot_brush
if (!is.null(brush)) {
rangesView$x <- c(brush$xmin, brush$xmax)
rangesView$y <- c(brush$ymin, brush$ymax)
output$downloadSize <- renderText({
paste0("Data time range reduced to: ", floor(rangesView$x[1]), " to ", ceiling(rangesView$x[2]), " h")
})
} else {
rangesView$x <- NULL
rangesView$y <- NULL
output$downloadSize <- renderText({
paste0("")
})
}
})
observeEvent(input$dataProcessingPlot_dblClick, {
brush <- input$dataProcessingPlot_brush
if (!is.null(brush)) {
rangesProcessing$x <- c(brush$xmin, brush$xmax)
rangesProcessing$y <- c(brush$ymin, brush$ymax)
} else {
rangesProcessing$x <- NULL
rangesProcessing$y <- NULL
}
})
# File processing ====
# Upload
dataInput <- reactive({
if (is.null(input$dataFile) || (input$dataFile$type !=
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')) {
return(NULL)
}
withProgress(
message = "Processing uploaded data...",
value = 0.4,
{
untidyData <- readWorkbook(input$dataFile$datapath, sheet = 'Data')
incProgress(0.4)
}
)
return(untidyData)
})
# Download - TidyData
output$downloadData <- downloadHandler(
# This function returns a string which tells the client
# browser what name to use when saving the file.
filename = function() {
if (input$dataFile$type ==
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
paste(input$dataFile$name)
}
}
,
# This function should write data to a file given to it by
# the argument 'file'.
content = function(file) {
# Write to a file specified by the 'file' argument
if (input$dataFile$type ==
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
withProgress(
message = "Writing processed data...",
value = 0.4,
{
pbrDataFile <- loadWorkbook(file = input$dataFile$datapath)
if (is.na(match('TidyData', getSheetNames(file = input$dataFile$datapath)))) {
addWorksheet(pbrDataFile, 'TidyData')
}
if (!is.null(rangesView$x)) {
tidyData <- dataProcessed() %>% filter(.$time > floor(rangesView$x[1])) %>% filter(.$time < ceiling(rangesView$x[2]))
} else {
tidyData <- dataProcessed()
}
writeData(pbrDataFile, 'TidyData', tidyData)
saveWorkbook(pbrDataFile, paste0(input$dataFile$datapath, '-tempDW'), overwrite = TRUE)
incProgress(0.4)
file.copy(paste0(input$dataFile$datapath, '-tempDW'), file)
}
)
}
}
)
# Download - Analysis
output$downloadAnalysis <- downloadHandler(
# This function returns a string which tells the client
# browser what name to use when saving the file.
filename = function() {
paste(input$dataFile$name)
}
,
# This function should write data to a file given to it by
# the argument 'file'.
content = function(file) {
# Write to a file specified by the 'file' argument
if (input$dataFile$type ==
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
withProgress(
message = "Writing processed data...",
value = 0.4,
{
pbrDataFile <- loadWorkbook(file = input$dataFile$datapath)
if (is.na(match('TidyData', getSheetNames(file = input$dataFile$datapath)))) {
addWorksheet(pbrDataFile, 'TidyData')
}
writeData(pbrDataFile, 'TidyData', dataProcessed())
if (is.na(match('Analysis', getSheetNames(file = input$dataFile$datapath)))) {
addWorksheet(pbrDataFile, 'Analysis')
}
writeData(pbrDataFile, 'Analysis', growthRates())
saveWorkbook(pbrDataFile, paste0(input$dataFile$datapath, '-tempDA'), overwrite = TRUE)
incProgress(0.4)
file.copy(paste0(input$dataFile$datapath, '-tempDA'), file)
}
)
}
}
)
# Data processing ====
dataProcessed <- reactive({
# Read data and remove empty columns
untidyData <- Filter(function(x)!all(is.na(x)), dataInput())
if (is.null(untidyData)) {
return(NULL)
}
selectChoices <- merge(data.frame(X = colnames(untidyData)), dataColumnNames)
if(input$selectDataView == "") {
selected <- "OD680, AU"
} else {
selected = input$selectDataView
}
if(input$selectDataView2 == "" || input$selectDataView2 == "-") {
selected2 <- "-"
} else {
selected2 = input$selectDataView2
}
updateSelectInput(session, 'selectDataView', choices = selectChoices$Y, selected = selected)
updateSelectInput(session, 'selectDataView2', choices = c("-", selectChoices$Y), selected = selected2)
if (input$interval > 0) {
factor <- 3600 / (input$interval * 60)
untidyData$time <- round(untidyData$time * factor) / factor
untidyData %>%
fill(eval(2 : ncol(.))) %>%
group_by(time) %>%
summarize_all(mean) %>%
arrange(time)
} else {
untidyData %>% fill(eval(2 : ncol(.)))
}
})
# Growth rates calculation
growthRates <- reactive({
data <- dataProcessed()
pumps <- grep('pumps.pump-.$', colnames(data), value = TRUE)
if (length(pumps) < 1) {
return(data.frame(time = c(0), mu = c(0), R2 = c(0), Dt = c(0)))
}
columnName <- dataColumnNames$X[match(input$selectDilutionPump, dataColumnNames$Y)]
if(is.null(data[[columnName]]) || (sum(data[[columnName]], na.rm = TRUE) == 0)) {
return(data.frame(time = c(0), mu = c(0), R2 = c(0), Dt = c(0)))
}
withProgress(
message = "Calculating growth rates...",
value = 0.0,
{
pumpOn <- which(data[[columnName]] > 0)
expFitStart <- c()
expFitStop <- c()
time <- c()
mu <- c()
R2 <- c()
Dt <- c()
for (i in 2:(length(pumpOn) - 1)) {
if (data[[columnName]][pumpOn[i] - 1] == 0) expFitStop <- c(expFitStop, pumpOn[i])
else if (data[[columnName]][pumpOn[i] + 1] == 0) expFitStart <- c(expFitStart, pumpOn[i] + 1)
}
incProgress(0.15)
for (j in 1:length(expFitStart)) {
interval <- c((expFitStart[j] + floor(input$lagTime/input$interval)):expFitStop[j])
incProgress(0.15 + (j / length(expFitStart)) * 0.85)
if (length(interval) < 4) {
next
}
timeFit <- data$time[interval]
dataFit <- data[[dataColumnNames$X[match(input$selectGrowthRatesData, dataColumnNames$Y)]]][interval]
fit <- nls(dataFit ~ exp(a + b * timeFit),
start = list(a = 0, b = 0.1),
control = list(maxiter = 99, minFactor = 1/2048, warnOnly = TRUE))
time <- c(time, timeFit[length(timeFit)])
mu <- c(mu, coef(fit)[2] * 24)
R2 <- c(R2, cor(dataFit, predict(fit)))
Dt <- c(Dt, 1 / coef(fit)[2] * log(2))
}
})
return(data.frame(time, mu, R2, Dt))
})
# Outputs hadling ====
output$fileName <- renderText({
if (!is.null(input$dataFile$name))
paste("Uploaded file name is ", input$dataFile$name)
})
output$fileSize <- renderText({
if (!is.null(input$dataFile$size))
paste("Uploaded file size is ",
round(input$dataFile$size / 1024),
" kB")
})
output$dataDim <- renderText({
if (!is.null(input$dataFile$size))
paste(
"Uploaded/processed table dimensions are ",
dim(dataInput())[1],
"/",
dim(dataProcessed())[1],
" x ",
dim(dataInput())[2],
"/",
dim(dataProcessed())[2],
" (rows x cols)"
)
})
output$dataViewPlot <- renderPlot({
data <- dataProcessed()
if (!is.null(data)) {
if(input$selectDataView2 == "" || input$selectDataView2 == "-") {
plot(x = data$time,
y = data[[dataColumnNames$X[match(input$selectDataView, dataColumnNames$Y)]]],
xlim = rangesView$x,
ylim = rangesView$y,
xlab = 'Experiment duration, h',
ylab = input$selectDataView)
} else {
if (!is.null(rangesView$y)) {
ymin = min(data[[dataColumnNames$X[match(input$selectDataView, dataColumnNames$Y)]]], na.rm = TRUE)
ymax = max(data[[dataColumnNames$X[match(input$selectDataView, dataColumnNames$Y)]]], na.rm = TRUE)
y2min = min(data[[dataColumnNames$X[match(input$selectDataView2, dataColumnNames$Y)]]], na.rm = TRUE)
y2max = max(data[[dataColumnNames$X[match(input$selectDataView2, dataColumnNames$Y)]]], na.rm = TRUE)
y2lim <- c((rangesView$y[1] - ymin) / (ymax - ymin) * (y2max - y2min) + y2min, y2max - (ymax - rangesView$y[2]) / (ymax - ymin) * (y2max - y2min))
} else {
y2lim <- NULL
}
twoord.plot(lx = data$time,
ly = data[[dataColumnNames$X[match(input$selectDataView, dataColumnNames$Y)]]],
rx = data$time,
ry = data[[dataColumnNames$X[match(input$selectDataView2, dataColumnNames$Y)]]],
xlim = rangesView$x,
lylim = rangesView$y,
rylim = y2lim,
xlab = 'Experiment duration, h',
ylab = input$selectDataView,
rylab = input$selectDataView2,
rcol = 3,
rpch = 1)
}
}
})
# https://rstudio.github.io/DT/options.html
# https://datatables.net/reference/option/dom
output$dataProcessingTable <- DT::renderDataTable({
if (!is.null(dataProcessed()))
datatable(growthRates(),
options = list(dom = 'tlp', pageLength = 8, lengthChange = FALSE, searching = FALSE)) %>%
formatRound(c('time', 'mu', 'R2', 'Dt'), digits = 2)
},
server = FALSE
)
output$dataProcessingPlot <- renderPlot({
if (!is.null(dataProcessed())) {
s1 = NULL
s2 = input$dataProcessingTable_rows_selected
gRates <- growthRates()
# plot(x = gRates$time, y = gRates$Dt, xlim = rangesProcessing$x, ylim = rangesProcessing$y, xlab = "Experiment duration, h", ylab = "Doubling time, h")
twoord.plot(lx = gRates$time,
ly = gRates$Dt,
rx = gRates$time,
ry = gRates$mu,
xlim = rangesProcessing$x,
lylim = rangesProcessing$y,
# rylim = y2lim,
xlab = 'Experiment duration, h',
ylab = 'Doubling time, h',
rylab = 'Specific growth rate, 1/day',
rcol = 3,
rpch = 1)
if (length(s1)) {
points(gRates[s1, , drop = FALSE], pch = 19, cex = 1, col = 'green')
}
if (length(s2)) {
points(gRates$time[s2], gRates$Dt[s2], pch = 19, cex = 1.25)
}
}
})
}
# This is the user-interface definition of a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
# UI Layout ====
ui <- fluidPage(
tags$head(includeScript('google-analytics.js')),
titlePanel("", windowTitle = "PBR Data Analysis"),
sidebarLayout(
# UI - Sidebar panel ====
sidebarPanel(
conditionalPanel(
condition = 'input.conditionedSidePanels == 1',
fluidRow(
fileInput(
'dataFile',
"Choose data file to upload",
accept = c(
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'.xlsx'
)
)
),
fluidRow(
sliderInput(
'interval',
"Averaging interval, min",
value = 1, min = 0.0, max = 60, step = 0.1
),
bsTooltip(
'interval',
"Interval that is used for lumping and averaging untidy data. Provided in minutes.",
'right', options = list(container = 'body')
)
),
tags$hr(),
fluidRow(
selectInput('selectDataView', "Data to view", "OD680, AU")
),
fluidRow(
selectInput('selectDataView2', "Additional data to view", "-")
),
tags$hr(),
fluidRow(
downloadButton('downloadData', "Download"),
textOutput('downloadSize')
)
),
conditionalPanel(
condition = 'input.conditionedSidePanels == 2',
fluidRow(
sliderInput(
'lagTime',
"Lag time, min",
value = 5, min = 0, max = 30, step = 1
),
bsTooltip(
'lagTime',
"Length of lag time that defines part of data that are influenced by the dilution. Provided in minutes.",
'right',
options = list(container = 'body')
)
),
tags$hr(),
fluidRow(
selectInput('selectGrowthRatesData', "Data for growth calculation", c("OD680, AU", "OD720, AU"), "OD680, AU")
),
fluidRow(
selectInput('selectDilutionPump', "Pump used for dilutions", c("Pump 0", "Pump 3", "Pump 4", "Pump 5", "Pump 6", "Pump 7"), "Pump 0")
),
tags$hr(),
fluidRow(
downloadButton('downloadAnalysis', "Download")
)
),
width = 3
),
# UI - Main panel ====
mainPanel(
tabsetPanel(
type = 'tabs',
tabPanel(
"Data Processing",
value = 1,
strong(textOutput('count')),
br(),
strong(textOutput('fileName')),
em(textOutput('fileSize')),
br(),
textOutput('dataDim'),
plotOutput('dataViewPlot',
width = '90%',
dblclick = 'dataViewPlot_dblClick',
brush = brushOpts(
id = 'dataViewPlot_brush',
resetOnNew = TRUE
)
)
),
tabPanel(
"Data Analysis",
value = 2,
fluidRow(
column(
4,
br(),
DT::dataTableOutput('dataProcessingTable')
),
column(
8,
plotOutput('dataProcessingPlot',
dblclick = 'dataProcessingPlot_dblClick',
brush = brushOpts(
id = 'dataProcessingPlot_brush',
resetOnNew = TRUE
)
)
)
)
),
id = 'conditionedSidePanels'
),
width = 9
)
),
# UI - Bottom panel ====
fluidRow(
column(
width = 3,
actionLink('register', label = "Register to support further development"),
tags$p(),
conditionalPanel(
'input.register',
textInput('regName', label = "First name", value = "First name"),
textInput('regSurname', label = "Last name", value = "Last name"),
textInput('regEmail', label = "Email", value = "@"),
textInput('regOrganization', label = "Organization", value = "Organization"),
textInput('regDepartment', label = "Department", value = "Department"),
actionButton('regSend', label = "Send"),
tags$hr(),
p(
"@author CzechGlobe - Department of Adaptive Biotechnologies (JaCe)"
),
p(
"@email [email protected]"
)
)
),
column(
width = 7,
actionLink('help', label = "Help"),
conditionalPanel(
'input.help',
p(
"This tool was developed to simplify manipulation with untidy data generated by Photon Systems Instruments (PSI) photobioreactor sofware. The data uploaded to this tool are expected in Excel (.xlsx) format."
),
p(
"For PSI photobioreactor software exported data, the most simple untidy data preparation procedure is to export data from the software in .ods format and then save (as) the file in .xlsx Excel format."
)
),
offset = 1
)
),
fluidRow(
column(
2,
tags$img(src = 'img/Logo-CzechGlobe.jpg', alt = "CzechGlobe", height = 60, align = 'top')
),
column(
1,
tags$img(src = 'img/Logo-C4Sys.jpg', alt = "C4Sys", height = 45, align = 'right')
)
)
)
shinyApp(ui = ui, server = server)