server.R 23.1 KB
Newer Older
dmattek's avatar
dmattek committed
1

2

dmattek's avatar
dmattek committed
3

dmattek's avatar
dmattek committed
4
5
6
7
8
9
10
11
12
13
# 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
#

library(shiny)
library(shinyjs) #http://deanattali.com/shinyjs/
library(data.table)
library(ggplot2)
dmattek's avatar
dmattek committed
14
library(gplots) # for heatmap.2
dmattek's avatar
dmattek committed
15
library(plotly)
dmattek's avatar
dmattek committed
16
17
library(d3heatmap) # for interactive heatmap
library(dendextend) # for color_branches
18
library(colorspace) # for palettes (used to colour dendrogram)
dmattek's avatar
dmattek committed
19
20
21
library(RColorBrewer)
library(sparcl) # sparse hierarchical and k-means
library(scales) # for percentages on y scale
dmattek's avatar
Added:    
dmattek committed
22
23
library(dtw) # for dynamic time warping
library(imputeTS) # for interpolating NAs
24
library(tca) # for time series manipulatiom, e.g. normTraj, genTraj, plotTrajRibbon
dmattek's avatar
dmattek committed
25

26
# increase file upload limit
dmattek's avatar
Added:    
dmattek committed
27
options(shiny.maxRequestSize = 200 * 1024 ^ 2)
dmattek's avatar
dmattek committed
28

dmattek's avatar
dmattek committed
29
shinyServer(function(input, output, session) {
30
  useShinyjs()
dmattek's avatar
dmattek committed
31
  
32
33
34
35
36
37
  # 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),
dmattek's avatar
Added:    
dmattek committed
38
39
    dataLoadNuc  = isolate(input$inButLoadNuc),
    dataLoadTrajRem = isolate(input$inButLoadTrajRem)
40
    #dataLoadStim = isolate(input$inButLoadStim)
dmattek's avatar
dmattek committed
41
42
  )
  
dmattek's avatar
dmattek committed
43
44
45
  ####
  ## UI for side panel
  
dmattek's avatar
dmattek committed
46
  # FILE LOAD
47
48
49
50
51
52
53
54
55
56
  # 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")
    
57
    return(tca::genTraj(in.nwells = 3))
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  })
  
  # 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))
    }
  })
  
dmattek's avatar
dmattek committed
74
75
76
77
78
79
80
  # This button will reset the inFileLoad
  observeEvent(input$butReset, {
    reset("inFileLoadNuc")  # reset is a shinyjs function
    #    reset("inFileStimLoad")  # reset is a shinyjs function
    
  })
  
dmattek's avatar
Added:    
dmattek committed
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
  # UI for loading csv with cell IDs for trajectory removal
  output$uiFileLoadTrajRem = renderUI({
    cat(file = stderr(), 'UI uiFileLoadTrajRem\n')
    
    if(input$chBtrajRem) 
      fileInput(
        'inFileLoadTrajRem',
        'Select data file (e.g. badTraj.csv) and press "Load Data"',
        accept = c('text/csv', 'text/comma-separated-values,text/plain')
      )
  })
  
  output$uiButLoadTrajRem = renderUI({
    cat(file = stderr(), 'UI uiButLoadTrajRem\n')
    
    if(input$chBtrajRem)
      actionButton("inButLoadTrajRem", "Load Data")
  })

  # load main data file
  dataLoadTrajRem <- eventReactive(input$inButLoadTrajRem, {
    cat(file = stderr(), "dataLoadTrajRem\n")
    locFilePath = input$inFileLoadTrajRem$datapath
    
    counter$dataLoadTrajRem <- input$inButLoadTrajRem - 1
    
    if (is.null(locFilePath) || locFilePath == '')
      return(NULL)
    else {
      return(fread(locFilePath))
    }
  })
  
dmattek's avatar
dmattek committed
114
115
  
  # COLUMN SELECTION
dmattek's avatar
dmattek committed
116
117
118
  output$varSelTrackLabel = renderUI({
    cat(file = stderr(), 'UI varSelTrackLabel\n')
    locCols = getDataNucCols()
119
    locColSel = locCols[grep('(T|t)rack|ID|id', locCols)[1]] # index 1 at the end in case more matches; select 1st; matches TrackLabel, tracklabel, Track Label etc
dmattek's avatar
dmattek committed
120
121
122
    
    selectInput(
      'inSelTrackLabel',
dmattek's avatar
dmattek committed
123
      'Select Track Label (e.g. objNuc_TrackObjects_Label):',
dmattek's avatar
dmattek committed
124
125
126
127
128
129
130
131
132
      locCols,
      width = '100%',
      selected = locColSel
    )
  })
  
  output$varSelTime = renderUI({
    cat(file = stderr(), 'UI varSelTime\n')
    locCols = getDataNucCols()
133
    locColSel = locCols[grep('(T|t)ime|Metadata_T', locCols)[1]] # index 1 at the end in case more matches; select 1st; matches RealTime, realtime, real time, etc.
dmattek's avatar
dmattek committed
134
135
136
137
    
    cat(locColSel, '\n')
    selectInput(
      'inSelTime',
dmattek's avatar
dmattek committed
138
      'Select time column (e.g. Metadata_T, RealTime):',
dmattek's avatar
dmattek committed
139
140
141
142
143
144
145
146
147
148
149
150
151
      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')
    
dmattek's avatar
dmattek committed
152
153
154
155
156
    if (input$chBgroup) {
      
      locCols = getDataNucCols()
      
      if (!is.null(locCols)) {
157
158
159
        locColSel = locCols[grep('(G|g)roup|(S|s)tim_All|(S|s)timulation|(S|s)ite', locCols)[1]]

        #cat('UI varSelGroup::locColSel ', locColSel, '\n')
dmattek's avatar
dmattek committed
160
161
162
163
164
165
166
167
        selectInput(
          'inSelGroup',
          'Select one or more facet groupings (e.g. Site, Well, Channel):',
          locCols,
          width = '100%',
          selected = locColSel,
          multiple = TRUE
        )
dmattek's avatar
dmattek committed
168
169
170
171
172
173
174
      }
    }
  })
  
  output$varSelSite = renderUI({
    cat(file = stderr(), 'UI varSelSite\n')
    
175
    if (input$chBtrackUni) {
dmattek's avatar
Added:    
dmattek committed
176
      locCols = getDataNucCols()
177
      locColSel = locCols[grep('(S|s)ite|(S|s)eries', locCols)[1]] # index 1 at the end in case more matches; select 1st
dmattek's avatar
Added:    
dmattek committed
178
179
180
181
182
183
184
185
186
      
      selectInput(
        'inSelSite',
        'Select FOV (e.g. Metadata_Site or Metadata_Series):',
        locCols,
        width = '100%',
        selected = locColSel
      )
    }
dmattek's avatar
dmattek committed
187
188
189
190
191
192
193
194
195
196
  })
  
  
  
  
  output$varSelMeas1 = renderUI({
    cat(file = stderr(), 'UI varSelMeas1\n')
    locCols = getDataNucCols()
    
    if (!is.null(locCols)) {
197
      locColSel = locCols[grep('objCyto_Intensity_MeanIntensity_imErkCor|(R|r)atio|(I|i)ntensity|y', locCols)[1]] # index 1 at the end in case more matches; select 1st
dmattek's avatar
dmattek committed
198

dmattek's avatar
dmattek committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
      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 / '))) {
216
      locColSel = locCols[grep('objNuc_Intensity_MeanIntensity_imErkCor', locCols)[1]] # index 1 at the end in case more matches; select 1st
dmattek's avatar
dmattek committed
217

dmattek's avatar
dmattek committed
218
219
220
221
222
223
224
225
226
227
      selectInput(
        'inSelMeas2',
        'Select 2nd measurement',
        locCols,
        width = '100%',
        selected = locColSel
      )
    }
  })
  
dmattek's avatar
dmattek committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  # UI for trimming x-axis (time)
  output$uiSlTimeTrim = renderUI({
    cat(file = stderr(), 'UI uiSlTimeTrim\n')
    
    if (input$chBtimeTrim) {
      locTpts  = getDataTpts()
      
      if(is.null(locTpts))
        return(NULL)
      
      locRTmin = min(locTpts)
      locRTmax = max(locTpts)
      
      sliderInput(
        'slTimeTrim',
        label = 'Time range to include',
        min = locRTmin,
        max = locRTmax,
        value = c(locRTmin, locRTmax),
        step = 1
      )
      
    }
  })
dmattek's avatar
dmattek committed
252
  
dmattek's avatar
dmattek committed
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
  # UI for normalization
  
  output$uiChBnorm = renderUI({
    cat(file = stderr(), 'UI uiChBnorm\n')
    
    if (input$chBnorm) {
      radioButtons(
        'rBnormMeth',
        label = 'Select method',
        choices = list('fold-change' = 'mean', 'z-score' = 'z.score')
      )
    }
  })
  
  output$uiSlNorm = renderUI({
    cat(file = stderr(), 'UI uiSlNorm\n')
    
    if (input$chBnorm) {
      locTpts  = getDataTpts()
      
      if(is.null(locTpts))
        return(NULL)
      
      locRTmin = min(locTpts)
      locRTmax = max(locTpts)
      
      sliderInput(
        'slNormRtMinMax',
        label = 'Time range for norm.',
        min = locRTmin,
        max = locRTmax,
dmattek's avatar
dmattek committed
284
285
        value = c(locRTmin, 0.1 * locRTmax), 
        step = 1
dmattek's avatar
dmattek committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
      )
    }
  })
  
  output$uiChBnormRobust = renderUI({
    cat(file = stderr(), 'UI uiChBnormRobust\n')
    
    if (input$chBnorm) {
      checkboxInput('chBnormRobust',
                    label = 'Robust stats',
                    FALSE)
    }
  })
  
  output$uiChBnormGroup = renderUI({
    cat(file = stderr(), 'UI uiChBnormGroup\n')
    
    if (input$chBnorm) {
      radioButtons('chBnormGroup',
dmattek's avatar
Mod:    
dmattek committed
305
306
                   label = 'Normalisation grouping',
                   choices = list('Entire dataset' = 'none', 'Per facet' = 'group', 'Per trajectory (Korean way)' = 'id'))
dmattek's avatar
dmattek committed
307
308
309
310
    }
  })
  
  
dmattek's avatar
dmattek committed
311
312
313
314
315
  # UI for removing outliers
  output$uiSlOutliers = renderUI({
    cat(file = stderr(), 'UI uiSlOutliers\n')
    
    if (input$chBoutliers) {
dmattek's avatar
Mod:    
dmattek committed
316
      
dmattek's avatar
dmattek committed
317
318
319
320
321
      sliderInput(
        'slOutliersPerc',
        label = 'Percentage of middle data',
        min = 90,
        max = 100,
dmattek's avatar
Fixed:    
dmattek committed
322
        value = 99.5, 
dmattek's avatar
dmattek committed
323
324
        step = 0.1
      )
dmattek's avatar
dmattek committed
325
      
dmattek's avatar
Mod:    
dmattek committed
326
      
dmattek's avatar
dmattek committed
327
328
329
    }
  })
  
dmattek's avatar
dmattek committed
330
331
332
333
334
335
336
337
338
  output$uiTxtOutliers = renderUI({
    if (input$chBoutliers) {
      
      p("Total tracks")
      
    }
    
  })
  
dmattek's avatar
dmattek committed
339
  
dmattek's avatar
dmattek committed
340
341
342
  ####
  ## data processing
  
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
  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
dmattek's avatar
dmattek committed
391
  getDataNucCols <- reactive({
392
393
394
395
396
397
398
399
400
401
402
    cat(file = stderr(), 'getDataNucCols: in\n')
    loc.dt = dataInBoth()
    
    if (is.null(loc.dt))
      return(NULL)
    else
      return(colnames(loc.dt))
  })
  
  # return dt with an added column with unique track object label
  dataMod <- reactive({
dmattek's avatar
dmattek committed
403
    cat(file = stderr(), 'dataMod\n')
404
405
    loc.dt = dataInBoth()
    
dmattek's avatar
dmattek committed
406
    if (is.null(loc.dt))
407
408
      return(NULL)
    
409
    if (input$chBtrackUni) {
dmattek's avatar
Added:    
dmattek committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
      loc.types = lapply(loc.dt, class)
      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 = "_")]
      }
dmattek's avatar
Added:    
dmattek committed
429
    } else {
dmattek's avatar
Added:    
dmattek committed
430
      loc.dt[, trackObjectsLabelUni := get(input$inSelTrackLabel)]
dmattek's avatar
Added:    
dmattek committed
431
432
    }
    
dmattek's avatar
dmattek committed
433
    
dmattek's avatar
Added:    
dmattek committed
434
435
436
437
438
439
    # remove trajectories based on uploaded csv

    if (input$chBtrajRem) {
      cat(file = stderr(), 'dataMod: trajRem not NULL\n')
      
      loc.dt.rem = dataLoadTrajRem()
dmattek's avatar
dmattek committed
440
441
      
      loc.dt = loc.dt[!(trackObjectsLabelUni %in% loc.dt.rem[[1]])]
dmattek's avatar
Added:    
dmattek committed
442
443
    }
    
444
445
446
    return(loc.dt)
  })
  
dmattek's avatar
dmattek committed
447
448
449
450
451
  # return all unique track object labels (created in dataMod)
  # This will be used to display in UI for trajectory highlighting
  getDataTrackObjLabUni <- reactive({
    cat(file = stderr(), 'getDataTrackObjLabUni\n')
    loc.dt = dataMod()
452
    
dmattek's avatar
dmattek committed
453
454
455
456
    if (is.null(loc.dt))
      return(NULL)
    else
      return(unique(loc.dt$trackObjectsLabelUni))
457
458
  })
  
dmattek's avatar
Mod:    
dmattek committed
459
  
dmattek's avatar
dmattek committed
460
461
462
  # return all unique time points (real time)
  # This will be used to display in UI for box-plot
  # These timepoints are from the original dt and aren't affected by trimming of x-axis
dmattek's avatar
dmattek committed
463
464
465
  getDataTpts <- reactive({
    cat(file = stderr(), 'getDataTpts\n')
    loc.dt = dataMod()
466
    
dmattek's avatar
dmattek committed
467
468
469
470
    if (is.null(loc.dt))
      return(NULL)
    else
      return(unique(loc.dt[[input$inSelTime]]))
471
472
  })
  
dmattek's avatar
dmattek committed
473
  
474
475
476
  
  # prepare data for plotting time courses
  # returns dt with these columns:
dmattek's avatar
dmattek committed
477
  #    realtime - selected from input
dmattek's avatar
dmattek committed
478
  #    y        - measurement selected from input
dmattek's avatar
dmattek committed
479
  #               (can be a single column or result of an operation on two cols)
480
481
  #    id       - trackObjectsLabelUni; created in dataMod based on TrackObjects_Label
  #               and FOV column such as Series or Site (if TrackObjects_Label not unique across entire dataset)
dmattek's avatar
dmattek committed
482
483
  #    group    - grouping variable for facetting from input
  #    mid.in   - column with trajectory selection status from the input file or
484
485
486
487
  #               highlight status from UI 
  #               (column created if mid.in present in uploaded data or tracks are selected in the UI)
  #    obj.num  - created if ObjectNumber column present in the input data 
  #    pos.x,y  - created if columns with x and y positions present in the input data
488
  data4trajPlot <- reactive({
dmattek's avatar
dmattek committed
489
    cat(file = stderr(), 'data4trajPlot\n')
490
491
    
    loc.dt = dataMod()
dmattek's avatar
dmattek committed
492
    if (is.null(loc.dt))
493
494
      return(NULL)
    
495
    # create expression for 'y' column based on measurements and math operations selected in UI
dmattek's avatar
dmattek committed
496
    if (input$inSelMath == '')
497
498
499
500
501
502
      loc.s.y = input$inSelMeas1
    else if (input$inSelMath == '1 / ')
      loc.s.y = paste0(input$inSelMath, input$inSelMeas1)
    else
      loc.s.y = paste0(input$inSelMeas1, input$inSelMath, input$inSelMeas2)
    
503
    # create expression for 'group' column
504
505
    # creates a merged column based on other columns from input
    # used for grouping of plot facets
dmattek's avatar
dmattek committed
506
507
508
509
510
511
512
513
514
515
516
    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')"
    }
517
    
dmattek's avatar
dmattek committed
518
519

    # column name with time
520
521
    loc.s.rt = input$inSelTime
    
dmattek's avatar
dmattek committed
522
523
    # Assign tracks selected for highlighting in UI
    loc.tracks.highlight = input$inSelHighlight
524
    locButHighlight = input$chBhighlightTraj
dmattek's avatar
dmattek committed
525
    
dmattek's avatar
Added:    
dmattek committed
526
527
    
    # Find column names with position
528
    loc.s.pos.x = names(loc.dt)[grep('(L|l)ocation.*X|(P|p)os.x|(P|p)osx', names(loc.dt))[1]]
529
    loc.s.pos.y = names(loc.dt)[grep('(L|l)ocation.*Y|(P|p)os.y|(P|p)osy', names(loc.dt))[1]]
dmattek's avatar
Added:    
dmattek committed
530
    
531
532
533
    cat(loc.s.pos.x, loc.s.pos.y, '\n')
    
    if (!is.na(loc.s.pos.x) & !is.na(loc.s.pos.y))
dmattek's avatar
Added:    
dmattek committed
534
535
536
537
      locPos = TRUE
    else
      locPos = FALSE
    
538
539
540
541
    
    # Find column names with ObjectNumber
    # This is different from TrackObject_Label and is handy to keep
    # because labels on segmented images are typically ObjectNumber
542
543
544
545
546
547
    loc.s.objnum = names(loc.dt)[grep('(O|o)bject(N|n)umber', names(loc.dt))[1]]
    #cat('data4trajPlot::loc.s.objnum ', loc.s.objnum, '\n')
    if (is.na(loc.s.objnum)) {
      locObjNum = FALSE
    }
    else {
dmattek's avatar
dmattek committed
548
      loc.s.objnum = loc.s.objnum[1]
549
      locObjNum = TRUE
dmattek's avatar
dmattek committed
550
    }
551
552
    
    
553
554
    # if dataset contains column mid.in with trajectory filtering status,
    # then, include it in plotting
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
    if (sum(names(loc.dt) %in% 'mid.in') > 0)
      locMidIn = TRUE
    else
      locMidIn = FALSE
    
    ## Build expression for selecting columns from loc.dt
    # Core columns
    s.colexpr = paste0('.(y = ', loc.s.y,
                       ', id = trackObjectsLabelUni', 
                       ', group = ', loc.s.gr,
                       ', realtime = ', loc.s.rt)
    
    # account for the presence of 'mid.in' column in uploaded data
    if(locMidIn)
      s.colexpr = paste0(s.colexpr, 
                         ', mid.in = mid.in')
    
    # include position x,y columns in uploaded data
    if(locPos)
      s.colexpr = paste0(s.colexpr, 
                         ', pos.x = ', loc.s.pos.x,
                         ', pos.y = ', loc.s.pos.y)
    
    # include ObjectNumber column
    if(locObjNum)
      s.colexpr = paste0(s.colexpr, 
                         ', obj.num = ', loc.s.objnum)
    
    # 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))]
    
    
    # if track selection ON
    if (locButHighlight){
      # add a 3rd level with status of track selection
      # to a column with trajectory filtering status in the uploaded file
      if(locMidIn)
        loc.out[, mid.in := ifelse(id %in% loc.tracks.highlight, 'SELECTED', mid.in)]
      else
dmattek's avatar
Mod:    
dmattek committed
597
        # add a column with status of track selection
598
        loc.out[, mid.in := ifelse(id %in% loc.tracks.highlight, 'SELECTED', 'NOT SEL')]
599
    }
600
      
dmattek's avatar
dmattek committed
601

602
603
604
605
606
607
    ## Interpolate NA's and data points not include
    # From: https://stackoverflow.com/questions/28073752/r-how-to-add-rows-for-missing-values-for-unique-group-sequences
    # Tracks are interpolated only within min and max realtime of every cell id
    setkey(loc.out, group, id, realtime)
    loc.out = loc.out[setkey(loc.out[, .(min(realtime):max(realtime)), by = .(group, id)], group, id, V1)]

dmattek's avatar
dmattek committed
608
609
610
611
612
613
614
    # x-check: print all rows with NA's
    print('Rows with NAs:')
    print(loc.out[rowSums(is.na(loc.out)) > 0, ])
    
    # Merge will create NA's where a realtime is missing.
    # Also, NA's may be already present in the dataset'.
    # Interpolate (linear) them with na.interpolate
615
    if(locPos)
dmattek's avatar
dmattek committed
616
      s.cols = c('y', 'pos.x', 'pos.y')
617
    else
dmattek's avatar
dmattek committed
618
      s.cols = c('y')
619
620
    
    loc.out[, (s.cols) := lapply(.SD, na.interpolation), by = id, .SDcols = s.cols]
dmattek's avatar
dmattek committed
621
622
623
624
625
626
627
628
629
630
631
632
    

    # !!! Current issue with interpolation:
    # The column mid.in is not taken into account.
    # If a trajectory is selected in the UI,
    # the mid.in column is added (if it doesn't already exist in the dataset),
    # and for the interpolated point, it will still be NA. Not really an issue.
    #
    # Also, think about the current option of having mid.in column in the uploaded dataset.
    # Keep it? Expand it?
    # Create a UI filed for selecting the column with mid.in data.
    # What to do with that column during interpolation (see above)
dmattek's avatar
Mod:    
dmattek committed
633
    
634
    ## Trim x-axis (time)
dmattek's avatar
dmattek committed
635
636
637
    if(input$chBtimeTrim) {
      loc.out = loc.out[realtime >= input$slTimeTrim[[1]] & realtime <= input$slTimeTrim[[2]] ]
    }
dmattek's avatar
dmattek committed
638
    
639
    ## Normalization
dmattek's avatar
dmattek committed
640
    # F-n myNorm adds additional column with .norm suffix
dmattek's avatar
dmattek committed
641
    if (input$chBnorm) {
642
      loc.out = tca::normTraj(
dmattek's avatar
dmattek committed
643
644
645
646
647
648
649
650
651
652
        in.dt = loc.out,
        in.meas.col = 'y',
        in.rt.col = 'realtime',
        in.rt.min = input$slNormRtMinMax[1],
        in.rt.max = input$slNormRtMinMax[2],
        in.type = input$rBnormMeth,
        in.robust = input$chBnormRobust,
        in.by.cols = if(input$chBnormGroup %in% 'none') NULL else input$chBnormGroup
      )
      
dmattek's avatar
dmattek committed
653
654
      # Column with normalized data is renamed to the original name
      # Further code assumes column name y produced by data4trajPlot
dmattek's avatar
dmattek committed
655
656
657
658
      loc.out[, y := NULL]
      setnames(loc.out, 'y.norm', 'y')
    }
    
dmattek's avatar
dmattek committed
659
660
661
662
663
664
    ##### MOD HERE
    ## display number of filtered tracks in textUI: uiTxtOutliers
    ## How? 
    ## 1. through reactive values?
    ## 2. through additional comumn to tag outliers?
    
dmattek's avatar
dmattek committed
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
    # Remove outliers
    # 1. Scale all points (independently per track)
    # 2. Pick time points that exceed the bounds
    # 3. Identify IDs of outliers
    # 4. Select cells that don't have these IDs
    
    cat('Ncells orig = ', length(unique(loc.out$id)), '\n')
    
    if (input$chBoutliers) {
      loc.out[, y.sc := scale(y)]  
      loc.tmp = loc.out[ y.sc < quantile(y.sc, (1 - input$slOutliersPerc * 0.01)*0.5) | 
                           y.sc > quantile(y.sc, 1 - (1 - input$slOutliersPerc * 0.01)*0.5)]
      loc.out = loc.out[!(id %in% unique(loc.tmp$id))]
      loc.out[, y.sc := NULL]
    }
    
    cat('Ncells trim = ', length(unique(loc.out$id)), '\n')
dmattek's avatar
Mod:    
dmattek committed
682
    
dmattek's avatar
dmattek committed
683
    return(loc.out)
dmattek's avatar
dmattek committed
684
685
  })
  
dmattek's avatar
dmattek committed
686
687
688
689
690
691
692
693
694
695
696
697
698
  
  
  # prepare data for clustering
  # return a matrix with:
  # cells as columns
  # time points as rows
  data4clust <- reactive({
    cat(file = stderr(), 'data4clust\n')
    
    loc.dt = data4trajPlot()
    if (is.null(loc.dt))
      return(NULL)
    
dmattek's avatar
Added:    
dmattek committed
699
    #print(loc.dt)
dmattek's avatar
dmattek committed
700
    loc.out = dcast(loc.dt, id ~ realtime, value.var = 'y')
dmattek's avatar
Added:    
dmattek committed
701
    #print(loc.out)
dmattek's avatar
dmattek committed
702
703
    loc.rownames = loc.out$id
    
dmattek's avatar
Mod:    
dmattek committed
704
    
dmattek's avatar
dmattek committed
705
706
    loc.out = as.matrix(loc.out[, -1])
    rownames(loc.out) = loc.rownames
dmattek's avatar
Added:    
dmattek committed
707
708
709
710
711
712
713
    
    # Remove NA's
    # na.interpolation from package imputeTS works with multidimensional data
    # but imputation is performed for each column independently
    # The matrix for clustering contains time series in rows, hence transposing it twice
    loc.out = t(na.interpolation(t(loc.out)))
    
dmattek's avatar
dmattek committed
714
    return(loc.out)
dmattek's avatar
Mod:    
dmattek committed
715
  }) 
dmattek's avatar
dmattek committed
716
  
dmattek's avatar
dmattek committed
717
  
dmattek's avatar
Added:    
dmattek committed
718
719
720
721
722
723
724
725
726
  # download data as prepared for plotting
  # after all modification
  output$downloadDataClean <- downloadHandler(
    filename = 'tCoursesSelected_clean.csv',
    content = function(file) {
      write.csv(data4trajPlot(), file, row.names = FALSE)
    }
  )
  
727
728
729
730
  ###### Trajectory plotting
  callModule(modTrajRibbonPlot, 'modTrajRibbon', data4trajPlot)
  callModule(modTrajRibbonPlot, 'modTrajRibbon', 
             in.data = data4trajPlot)
dmattek's avatar
dmattek committed
731
  
732
733
734
735
736
  ###### Trajectory plotting
  callModule(modTrajPlot, 'modTrajPlot', data4trajPlot)
  
  ## UI for selecting trajectories
  # The output data table of data4trajPlot is modified based on inSelHighlight field
dmattek's avatar
dmattek committed
737
738
  output$varSelHighlight = renderUI({
    cat(file = stderr(), 'UI varSelHighlight\n')
dmattek's avatar
dmattek committed
739
    
dmattek's avatar
dmattek committed
740
741
742
    locBut = input$chBhighlightTraj
    if (!locBut)
      return(NULL)
dmattek's avatar
dmattek committed
743
    
dmattek's avatar
dmattek committed
744
    loc.v = getDataTrackObjLabUni()
dmattek's avatar
dmattek committed
745
    if (!is.null(loc.v)) {
746
      selectInput(
dmattek's avatar
dmattek committed
747
748
749
        'inSelHighlight',
        'Select one or more rajectories:',
        loc.v,
750
        width = '100%',
dmattek's avatar
dmattek committed
751
        multiple = TRUE
752
      )
dmattek's avatar
dmattek committed
753
754
755
    }
  })
  
756
  ###### AUC calculation and plotting
dmattek's avatar
Added:    
dmattek committed
757
758
  callModule(modAUCplot, 'tabAUC', data4trajPlot)
  
dmattek's avatar
Added:    
dmattek committed
759
760
  ###### Box-plot
  callModule(tabBoxPlot, 'tabBoxPlot', data4trajPlot)
dmattek's avatar
dmattek committed
761
  
dmattek's avatar
dmattek committed
762
763
764
  ###### Scatter plot
  callModule(tabScatterPlot, 'tabScatter', data4trajPlot)
  
dmattek's avatar
dmattek committed
765
  ##### Hierarchical clustering
dmattek's avatar
Added:    
dmattek committed
766
  callModule(clustHier, 'tabClHier', data4clust, data4trajPlot)
dmattek's avatar
dmattek committed
767
768
  
  ##### Sparse hierarchical clustering using sparcl
dmattek's avatar
Added:    
dmattek committed
769
  callModule(clustHierSpar, 'tabClHierSpar', data4clust, data4trajPlot)
dmattek's avatar
dmattek committed
770

dmattek's avatar
Mod:    
dmattek committed
771
  
dmattek's avatar
dmattek committed
772
})