-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
446 lines (360 loc) · 13.9 KB
/
server.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
# Analyzes HOECHST-stained single-cell data
# Fits cell cycle models
#
# Data input - csv with following columns:
# 1. unique cell ids OR cell ids and a grouping column, where cell ids are unique within that group, e.g. FOV
# 2. total hoechst per nucleus
# 3. grouping column, e.g. well or a condition
#
# The entire dataset is a population snapshot at a particular time point
library(shiny)
library(shinyjs) #http://deanattali.com/shinyjs/
library(data.table)
# increase file upload limit
options(shiny.maxRequestSize = 120 * 1024 ^ 2)
shinyServer(function(input, output, session) {
useShinyjs()
# This is only set at session start
# we use this as a way to determine which input was
# clicked in the dataInBoth reactive
counter <- reactiveValues(
# The value of inDataGen1,2 actionButton is the number of times they were pressed
dataGen1 = isolate(input$inDataGen1),
dataLoadNuc = isolate(input$inButLoadNuc)
#dataLoadStim = isolate(input$inButLoadStim)
)
####
## UI for side panel
# FILE LOAD
# This button will reset the inFileLoad
observeEvent(input$inButReset, {
reset("inFileLoadNuc") # reset is a shinyjs function
#reset("inButLoadStim") # reset is a shinyjs function
})
# generate random dataset 1
dataGen1 <- eventReactive(input$inDataGen1, {
cat("dataGen1\n")
return(userDataGen_cellCycle())
})
# load main data file
dataLoadNuc <- eventReactive(input$inButLoadNuc, {
cat("dataLoadNuc\n")
locFilePath = input$inFileLoadNuc$datapath
counter$dataLoadNuc <- input$inButLoadNuc - 1
if (is.null(locFilePath) || locFilePath == '')
return(NULL)
else {
return(fread(locFilePath))
}
})
# This button will reset the inFileLoad
observeEvent(input$butReset, {
reset("inFileLoadNuc") # reset is a shinyjs function
# reset("inFileStimLoad") # reset is a shinyjs function
})
# UI FOR COLUMN SELECTION
# observer: sets chBcellID true if column names contain cellID
# here we check the rpesence of 'ObjectNumber' string in column names
observe({
locCols = getDataNucCols()
updateCheckboxInput(session,
"chBcellID",
value = sum(locCols %like% 'ObjectNumber') > 0)
})
# observer: sets chBcellID true if column names contain grouping (e.g. treatment)
# here we check the presence of 'Treat' string in column names
observe({
locCols = getDataNucCols()
updateCheckboxInput(session, "chBgroup", value = sum(locCols %like% 'Treat') > 0)
})
output$uiChBcellIDunique = renderUI({
if (input$chBcellID) {
checkboxInput('chBtrackUni',
'Cell IDs unique across entire dataset',
TRUE)
}
})
output$varSelTrackLabel = renderUI({
cat(file = stderr(), 'UI varSelTrackLabel\n')
if (input$chBcellID) {
locCols = getDataNucCols()
locColSel = locCols[locCols %like% 'ObjectNumber'][1] # index 1 at the end in case more matches; select 1st
selectInput(
'inSelTrackLabel',
'Select Track Label (e.g. objNuclei_ObjectNumber):',
locCols,
width = '100%',
selected = locColSel
)
}
})
# This is to select FOV
# The value is used to create dataset-wide unique cell ids
# Cell ids should be uniwue within group selected here
output$varSelSite = renderUI({
cat(file = stderr(), 'UI varSelSite\n')
if (input$chBcellID) {
if (!input$chBtrackUni) {
locCols = getDataNucCols()
locColSel = locCols[locCols %like% 'Site' |
locCols %like% 'Series'][1] # index 1 at the end in case more matches; select 1st
selectInput(
'inSelSite',
'Select FOV (e.g. Metadata_Site or Metadata_Series):',
locCols,
width = '100%',
selected = locColSel
)
}
}
})
# This is main field to select plot facet grouping
# It's typically a column with the entire experimental description,
# e.g. in Yannick's case it's Stim_All_Ch or Stim_All_S.
# In Coralie's case it's a combination of 3 columns called Stimulation_...
output$varSelGroup = renderUI({
cat(file = stderr(), 'UI varSelGroup\n')
if (input$chBgroup) {
locCols = getDataNucCols()
if (!is.null(locCols)) {
locColSel = locCols[locCols %like% 'Treat']
if (length(locColSel) == 0)
locColSel = locCols[locCols %like% 'Conc'][1] # index 1 at the end in case more matches; select 1st
else if (length(locColSel) > 1) {
locColSel = locColSel[1]
}
# cat('UI varSelGroup::locColSel ', locColSel, '\n')
selectInput(
'inSelGroup',
'Select one or more facet groupings (e.g. Metadata_Well, Metadata_TreatConc):',
locCols,
width = '100%',
selected = locColSel,
multiple = TRUE
)
}
}
})
output$varSelMeas1 = renderUI({
cat(file = stderr(), 'UI varSelMeas1\n')
locCols = getDataNucCols()
if (!is.null(locCols)) {
locColSel = locCols[locCols %like% 'IntegratedIntensity'][1] # index 1 at the end in case more matches; select 1st
selectInput(
'inSelMeas1',
'Select 1st measurement:',
locCols,
width = '100%',
selected = locColSel
)
}
})
output$varSelMeas2 = renderUI({
cat(file = stderr(), 'UI varSelMeas2\n')
locCols = getDataNucCols()
if (!is.null(locCols) &&
!(input$inSelMath %in% c('', '1 / ', 'log10'))) {
locColSel = locCols[locCols %like% 'objNuc_Intensity_MeanIntensity_imErkCor.*'][1] # index 1 at the end in case more matches; select 1st
selectInput(
'inSelMeas2',
'Select 2nd measurement',
locCols,
width = '100%',
selected = locColSel
)
}
})
# UI for trimming measurement
output$uiSlMeasTrim = renderUI({
cat(file = stderr(), 'UI uiSlMeasTrim\n')
if (input$chBmeasTrim) {
loc.dt = data4histPlot()
if (is.null(loc.dt))
return(NULL)
locMeasMin = floor(min(loc.dt$y))
locMeasMax = ceiling(max(loc.dt$y))
locMeasQ3 = round(quantile(loc.dt$y, 0.9)) # set initial upper limit to 0.9 quantile
sliderInput(
'slMeasTrim',
label = 'Range of values to include',
step = 0.1,
min = locMeasMin,
max = locMeasMax,
value = c(locMeasMin, locMeasQ3)
)
}
})
####
## data processing
dataInBoth <- reactive({
# Without direct references to inDataGen1,2 and inFileLoad, inDataGen2
# does not trigger running this reactive once inDataGen1 is used.
# This is one of the more nuanced areas of reactive programming in shiny
# due to the if else logic, it isn't fetched once inDataGen1 is available
# The morale is use direct retrieval of inputs to guarantee they are available
# for if else logic checks!
locInGen1 = input$inDataGen1
locInLoadNuc = input$inButLoadNuc
#locInLoadStim = input$inButLoadStim
cat(
"dataInBoth\ninGen1: ",
locInGen1,
" prev=",
isolate(counter$dataGen1),
"\ninDataNuc: ",
locInLoadNuc,
" prev=",
isolate(counter$dataLoadNuc),
# "\ninDataStim: ",
# locInLoadStim,
# " prev=",
# isolate(counter$dataLoadStim),
"\n"
)
# isolate the checks of counter reactiveValues
# as we set the values in this same reactive
if (locInGen1 != isolate(counter$dataGen1)) {
cat("dataInBoth if inDataGen1\n")
dm = dataGen1()
# no need to isolate updating the counter reactive values!
counter$dataGen1 <- locInGen1
} else if (locInLoadNuc != isolate(counter$dataLoadNuc)) {
cat("dataInBoth if inDataLoadNuc\n")
dm = dataLoadNuc()
# no need to isolate updating the counter reactive values!
counter$dataLoadNuc <- locInLoadNuc
} else {
cat("dataInBoth else\n")
dm = NULL
}
return(dm)
})
# return column names of the main dt
getDataNucCols <- reactive({
cat(file = stderr(), 'getDataNucCols: in\n')
loc.dt = dataInBoth()
if (is.null(loc.dt))
return(NULL)
else
return(colnames(loc.dt))
})
#############
## NEXT: modify here to account for UI choices
## In the simplest case one should be able to load a single-column dataset with measurement only
## What's passed further to other modules is a dt with 3 columns: cellid, group, measurement
## The 1st two columns should be created artificially if no choices are indicated in UI
# return dt with an added column with dataset-wide unique object label
# Typically, object lables are unique within 1 a single FOV
dataMod <- reactive({
cat(file = stderr(), 'dataMod\n')
loc.dt = dataInBoth()
if (is.null(loc.dt))
return(NULL)
if (input$chBcellID) {
if (input$chBtrackUni) {
loc.dt[, trackObjectsLabelUni := get(input$inSelTrackLabel)]
} else {
loc.types = lapply(loc.dt, class)
# Make sure that UI fields that are checked further down aren't empty
if (!(input$inSelTrackLabel == "") &
!(input$inSelSite == "")) {
if (loc.types[[input$inSelTrackLabel]] %in% c('numeric', 'integer') &
loc.types[[input$inSelSite]] %in% c('numeric', 'integer'))
{
loc.dt[, trackObjectsLabelUni := paste(sprintf("%03d", get(input$inSelSite)),
sprintf("%04d", get(input$inSelTrackLabel)),
sep = "_")]
} else {
if (loc.types[[input$inSelTrackLabel]] %in% c('numeric', 'integer')) {
loc.dt[, trackObjectsLabelUni := paste(sprintf("%s", get(input$inSelSite)),
sprintf("%04d", get(input$inSelTrackLabel)),
sep = "_")]
} else {
if (loc.types[[input$inSelSite]] %in% c('numeric', 'integer')) {
loc.dt[, trackObjectsLabelUni := paste(sprintf("%03d", get(input$inSelSite)),
sprintf("%s", get(input$inSelTrackLabel)),
sep = "_")]
} else {
loc.dt[, trackObjectsLabelUni := paste(sprintf("%s", get(input$inSelSite)),
sprintf("%s", get(input$inSelTrackLabel)),
sep = "_")]
}
}
}
}
}
}
return(loc.dt)
})
# prepares a data table with 3 columns based on fields with column selection
# y - measurement value to analyse (that's the main necessary column)
# id - dataset-wide unique cell id
# group - grouping for facetting
# This table DOES NOT affect any mods such as data trimming or outlier removal
data4histPlot <- reactive({
cat(file = stderr(), 'data4histPlot\n')
loc.dt = dataMod()
if (is.null(loc.dt))
return(NULL)
#create expression for 'y' column based on measurements and math operations selected in UI
if (input$inSelMath == '')
loc.s.y = input$inSelMeas1
else if (input$inSelMath == '1 / ')
loc.s.y = paste0(input$inSelMath, input$inSelMeas1)
else if (input$inSelMath == 'log10')
loc.s.y = paste0('1 + log10(', input$inSelMeas1, ')')
else
loc.s.y = paste0(input$inSelMeas1, input$inSelMath, input$inSelMeas2)
# switch(input$inSelMath,
# '' = {loc.s.y = input$inSelMeas1},
# '1 / ' = {loc.s.y = paste0(input$inSelMath, input$inSelMeas1)},
# 'log10' = {loc.s.y = paste0('log10(', input$inSelMeas1, ')')},
# {loc.s.y = paste0(input$inSelMeas1, input$inSelMath, input$inSelMeas2)}
# )
# create expression for 'group' column
# creates a merged column based on other columns from input
# used for grouping of plot facets
if (input$chBgroup) {
if(length(input$inSelGroup) == 0)
return(NULL)
loc.s.gr = sprintf("paste(%s, sep=';')",
paste(input$inSelGroup, sep = '', collapse = ','))
} else {
# if no grouping required, fill 'group' column with 0
# because all the plotting relies on the presence of the group column
loc.s.gr = "paste('0')"
}
## Build expression for selecting columns from loc.dt
# Core columns
s.colexpr = paste0('.(y = ', loc.s.y,
', group = ', loc.s.gr)
# add data-wide unique trackID
if(input$chBcellID)
s.colexpr = paste0(s.colexpr,
', id = trackObjectsLabelUni')
# close bracket, finish the expression
s.colexpr = paste0(s.colexpr, ')')
# create final dt for output based on columns selected above
loc.out = loc.dt[, eval(parse(text = s.colexpr))]
# Reorder levels according to the output of unique.
# Once reordered, ggplot will plot correctly.
# This is to avoid having levels ordered as: '10 um', '100 um', '20 um'...
loc.out$group = factor(loc.out$group, unique(loc.out$group))
return(loc.out)
})
# prepares final data table for analysis
# accounts for outlier removal and data trimming
# columns y, id, and group kept
data4histPlotMod <- reactive({
cat(file = stderr(), 'data4histPlotMod\n')
loc.dt = data4histPlot()
if (is.null(loc.dt))
return(NULL)
if (input$chBmeasTrim)
loc.dt = loc.dt[y >= input$slMeasTrim[1] &
y <= input$slMeasTrim[2]]
return(loc.dt)
})
callModule(tabHistPlot, 'myTabHistPlot', data4histPlotMod)
callModule(tabFitModel, 'myTabFitModel', data4histPlotMod)
})