server.R 20.8 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
dmattek's avatar
Mod:  
dmattek committed
18
library(colorspace) # for palettes (ised 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
dmattek's avatar
dmattek committed
24

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

dmattek's avatar
dmattek committed
28
shinyServer(function(input, output, session) {
29
  useShinyjs()
dmattek's avatar
dmattek committed
30
  
31 32 33 34 35 36
  # 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
37 38
    dataLoadNuc  = isolate(input$inButLoadNuc),
    dataLoadTrajRem = isolate(input$inButLoadTrajRem)
39
    #dataLoadStim = isolate(input$inButLoadStim)
dmattek's avatar
dmattek committed
40 41
  )
  
dmattek's avatar
dmattek committed
42 43 44
  ####
  ## UI for side panel
  
dmattek's avatar
dmattek committed
45
  # FILE LOAD
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
  # 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())
  })
  
  # 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
73 74 75 76 77 78 79
  # 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
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
  # 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
113 114
  
  # COLUMN SELECTION
dmattek's avatar
dmattek committed
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
  output$varSelTrackLabel = renderUI({
    cat(file = stderr(), 'UI varSelTrackLabel\n')
    locCols = getDataNucCols()
    locColSel = locCols[locCols %like% 'rack'][1] # index 1 at the end in case more matches; select 1st
    
    selectInput(
      'inSelTrackLabel',
      'Select Track Label (e.g. objNuc_Track_ObjectsLabel):',
      locCols,
      width = '100%',
      selected = locColSel
    )
  })
  
  output$varSelTime = renderUI({
    cat(file = stderr(), 'UI varSelTime\n')
    locCols = getDataNucCols()
    locColSel = locCols[locCols %like% 'RealTime'][1] # index 1 at the end in case more matches; select 1st
    
    cat(locColSel, '\n')
    selectInput(
      'inSelTime',
      'Select time column (e.g. RealTime):',
      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')
    locCols = getDataNucCols()
    
    if (!is.null(locCols)) {
      locColSel = locCols[locCols %like% 'ite']
      if (length(locColSel) == 0)
        locColSel = locCols[locCols %like% 'eries'][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. Site, Well, Channel):',
        locCols,
        width = '100%',
        selected = locColSel,
        multiple = TRUE
      )
    }
    
  })
  
  output$varSelSite = renderUI({
    cat(file = stderr(), 'UI varSelSite\n')
    
dmattek's avatar
Added:  
dmattek committed
175 176 177 178 179 180 181 182 183 184 185 186
    if (!input$chBtrackUni) {
      locCols = getDataNucCols()
      locColSel = locCols[locCols %like% 'ite'][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
      )
    }
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)) {
dmattek's avatar
dmattek committed
197 198
      locColSel = locCols[locCols %like% 'objCyto_Intensity_MeanIntensity_imErkCor.*' |
                            locCols %like% 'Ratio'][1] # index 1 at the end in case more matches; select 1st
dmattek's avatar
dmattek committed
199

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

dmattek's avatar
dmattek committed
219 220 221 222 223 224 225 226 227 228
      selectInput(
        'inSelMeas2',
        'Select 2nd measurement',
        locCols,
        width = '100%',
        selected = locColSel
      )
    }
  })
  
dmattek's avatar
dmattek committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  # 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
253
  
dmattek's avatar
dmattek committed
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
  # 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
285 286
        value = c(locRTmin, 0.1 * locRTmax), 
        step = 1
dmattek's avatar
dmattek committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
      )
    }
  })
  
  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
306 307
                   label = 'Normalisation grouping',
                   choices = list('Entire dataset' = 'none', 'Per facet' = 'group', 'Per trajectory (Korean way)' = 'id'))
dmattek's avatar
dmattek committed
308 309 310 311
    }
  })
  
  
dmattek's avatar
dmattek committed
312 313 314 315 316
  # UI for removing outliers
  output$uiSlOutliers = renderUI({
    cat(file = stderr(), 'UI uiSlOutliers\n')
    
    if (input$chBoutliers) {
dmattek's avatar
Mod:  
dmattek committed
317
      
dmattek's avatar
dmattek committed
318 319 320 321 322
      sliderInput(
        'slOutliersPerc',
        label = 'Percentage of middle data',
        min = 90,
        max = 100,
dmattek's avatar
Fixed:  
dmattek committed
323
        value = 99.5, 
dmattek's avatar
dmattek committed
324 325
        step = 0.1
      )
dmattek's avatar
dmattek committed
326
      
dmattek's avatar
Mod:  
dmattek committed
327
      
dmattek's avatar
dmattek committed
328 329 330
    }
  })
  
dmattek's avatar
dmattek committed
331 332 333 334 335 336 337 338 339
  output$uiTxtOutliers = renderUI({
    if (input$chBoutliers) {
      
      p("Total tracks")
      
    }
    
  })
  
dmattek's avatar
dmattek committed
340
  
dmattek's avatar
dmattek committed
341 342 343 344 345 346 347 348 349
  ####
  ## data processing
  
  # generate random dataset 1
  dataGen1 <- eventReactive(input$inDataGen1, {
    cat("dataGen1\n")
    
    return(userDataGen())
  })
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
  
  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
399
  getDataNucCols <- reactive({
400 401 402 403 404 405 406 407 408 409 410
    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
411
    cat(file = stderr(), 'dataMod\n')
412 413
    loc.dt = dataInBoth()
    
dmattek's avatar
dmattek committed
414
    if (is.null(loc.dt))
415 416
      return(NULL)
    
dmattek's avatar
Added:  
dmattek committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    if (!input$chBtrackUni) {
      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
437
    } else {
dmattek's avatar
Added:  
dmattek committed
438
      loc.dt[, trackObjectsLabelUni := get(input$inSelTrackLabel)]
dmattek's avatar
Added:  
dmattek committed
439 440
    }
    
dmattek's avatar
dmattek committed
441
    
dmattek's avatar
Added:  
dmattek committed
442 443 444 445 446 447 448 449 450
    # remove trajectories based on uploaded csv

    if (input$chBtrajRem) {
      cat(file = stderr(), 'dataMod: trajRem not NULL\n')
      
      loc.dt.rem = dataLoadTrajRem()
      loc.dt = loc.dt[!(trackObjectsLabelUni %in% loc.dt.rem$id)]
    }
    
451 452 453
    return(loc.dt)
  })
  
dmattek's avatar
dmattek committed
454 455 456 457 458
  # 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()
459
    
dmattek's avatar
dmattek committed
460 461 462 463
    if (is.null(loc.dt))
      return(NULL)
    else
      return(unique(loc.dt$trackObjectsLabelUni))
464 465
  })
  
dmattek's avatar
Mod:  
dmattek committed
466
  
dmattek's avatar
dmattek committed
467 468 469
  # 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
470 471 472
  getDataTpts <- reactive({
    cat(file = stderr(), 'getDataTpts\n')
    loc.dt = dataMod()
473
    
dmattek's avatar
dmattek committed
474 475 476 477
    if (is.null(loc.dt))
      return(NULL)
    else
      return(unique(loc.dt[[input$inSelTime]]))
478 479
  })
  
dmattek's avatar
dmattek committed
480
  
481 482 483
  
  # prepare data for plotting time courses
  # returns dt with these columns:
dmattek's avatar
dmattek committed
484
  #    realtime - selected from input
dmattek's avatar
dmattek committed
485
  #    y        - measurement selected from input
dmattek's avatar
dmattek committed
486 487 488 489 490
  #               (can be a single column or result of an operation on two cols)
  #    id       - trackObjectsLabelUni (created in dataMod)
  #    group    - grouping variable for facetting from input
  #    mid.in   - column with trajectory selection status from the input file or
  #               highlight status from UI
491
  data4trajPlot <- reactive({
dmattek's avatar
dmattek committed
492
    cat(file = stderr(), 'data4trajPlot\n')
493 494
    
    loc.dt = dataMod()
dmattek's avatar
dmattek committed
495
    if (is.null(loc.dt))
496 497 498
      return(NULL)
    
    
dmattek's avatar
dmattek committed
499
    if (input$inSelMath == '')
500 501 502 503 504 505 506 507 508
      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)
    
    # create expression for parsing
    # creates a merged column based on other columns from input
    # used for grouping of plot facets
dmattek's avatar
dmattek committed
509 510 511 512
    if(length(input$inSelGroup) == 0)
      return(NULL)
    loc.s.gr = sprintf("paste(%s, sep=';')",
                       paste(input$inSelGroup, sep = '', collapse = ','))
513 514 515
    
    loc.s.rt = input$inSelTime
    
dmattek's avatar
dmattek committed
516 517 518 519
    # Assign tracks selected for highlighting in UI
    loc.tracks.highlight = input$inSelHighlight
    locBut = input$chBhighlightTraj
    
dmattek's avatar
Added:  
dmattek committed
520 521
    
    # Find column names with position
dmattek's avatar
Mod:  
dmattek committed
522 523
    loc.s.pos.x = names(loc.dt)[names(loc.dt) %like% c('.*ocation.*X') | names(loc.dt) %like% c('.*os.x')]
    loc.s.pos.y = names(loc.dt)[names(loc.dt) %like% c('.*ocation.*Y') | names(loc.dt) %like% c('.*os.y')]
dmattek's avatar
Added:  
dmattek committed
524 525 526 527 528 529
    
    if (length(loc.s.pos.x) == 1 & length(loc.s.pos.y) == 1)
      locPos = TRUE
    else
      locPos = FALSE
    
530 531 532
    # if dataset contains column mid.in with trajectory filtering status,
    # then, include it in plotting
    if (sum(names(loc.dt) %in% 'mid.in') > 0) {
dmattek's avatar
Added:  
dmattek committed
533 534 535 536 537 538
      if (locPos) # position columns present
        loc.out = loc.dt[, .(
          y = eval(parse(text = loc.s.y)),
          id = trackObjectsLabelUni,
          group = eval(parse(text = loc.s.gr)),
          realtime = eval(parse(text = loc.s.rt)),
dmattek's avatar
Mod:  
dmattek committed
539 540
          pos.x = get(loc.s.pos.x),
          pos.y = get(loc.s.pos.y),
dmattek's avatar
Added:  
dmattek committed
541
          mid.in = mid.in
dmattek's avatar
Mod:  
dmattek committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
        )] else
          loc.out = loc.dt[, .(
            y = eval(parse(text = loc.s.y)),
            id = trackObjectsLabelUni,
            group = eval(parse(text = loc.s.gr)),
            realtime = eval(parse(text = loc.s.rt)),
            mid.in = mid.in
          )]
        
        
        
        
        # add 3rd level with status of track selection
        # to a column with trajectory filtering status
        if (locBut) {
          loc.out[, mid.in := ifelse(id %in% loc.tracks.highlight, 'SELECTED', mid.in)]
        }
        
560
    } else {
dmattek's avatar
Added:  
dmattek committed
561 562 563 564 565
      if (locPos) # position columns present
        loc.out = loc.dt[, .(
          y = eval(parse(text = loc.s.y)),
          id = trackObjectsLabelUni,
          group = eval(parse(text = loc.s.gr)),
dmattek's avatar
Mod:  
dmattek committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
          realtime = eval(parse(text = loc.s.rt)),
          pos.x = get(loc.s.pos.x),
          pos.y = get(loc.s.pos.y)
        )] else
          loc.out = loc.dt[, .(
            y = eval(parse(text = loc.s.y)),
            id = trackObjectsLabelUni,
            group = eval(parse(text = loc.s.gr)),
            realtime = eval(parse(text = loc.s.rt))
          )]
        
        
        # add a column with status of track selection
        if (locBut) {
          loc.out[, mid.in := ifelse(id %in% loc.tracks.highlight, 'SELECTED', 'NOT SEL')]
        }
582
    }
583
    
dmattek's avatar
Added:  
dmattek committed
584 585
    # add XY location if present in the dataset
    
dmattek's avatar
Added:  
dmattek committed
586 587 588 589 590 591
    # remove NAs 
    # (doesn't make sense to remove here anyway; 
    # NA's are already removed in tCourseSelected.csv
    # Such datapoints are missing, therefore they require interpolation.
    # If a row of long-format dt is removed, an NA appears after casting anyway if that grid point is missing)
    # Remove NAs in data4clust()
dmattek's avatar
dmattek committed
592
    loc.out = loc.out[complete.cases(loc.out)]
dmattek's avatar
Mod:  
dmattek committed
593
    
dmattek's avatar
dmattek committed
594 595 596 597
    # Trim x-axis (time)
    if(input$chBtimeTrim) {
      loc.out = loc.out[realtime >= input$slTimeTrim[[1]] & realtime <= input$slTimeTrim[[2]] ]
    }
dmattek's avatar
dmattek committed
598 599
    
    # Normalization
dmattek's avatar
dmattek committed
600
    # F-n myNorm adds additional column with .norm suffix
dmattek's avatar
dmattek committed
601 602 603 604 605 606 607 608 609 610 611 612
    if (input$chBnorm) {
      loc.out = myNorm(
        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
613 614
      # Column with normalized data is renamed to the original name
      # Further code assumes column name y produced by data4trajPlot
dmattek's avatar
dmattek committed
615 616 617 618
      loc.out[, y := NULL]
      setnames(loc.out, 'y.norm', 'y')
    }
    
dmattek's avatar
dmattek committed
619 620 621 622 623 624
    ##### 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
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    # 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
642
    
dmattek's avatar
dmattek committed
643
    return(loc.out)
dmattek's avatar
dmattek committed
644 645
  })
  
dmattek's avatar
dmattek committed
646 647 648 649 650 651 652 653 654 655 656 657 658
  
  
  # 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
659
    #print(loc.dt)
dmattek's avatar
dmattek committed
660
    loc.out = dcast(loc.dt, id ~ realtime, value.var = 'y')
dmattek's avatar
Added:  
dmattek committed
661
    #print(loc.out)
dmattek's avatar
dmattek committed
662 663
    loc.rownames = loc.out$id
    
dmattek's avatar
Mod:  
dmattek committed
664
    
dmattek's avatar
dmattek committed
665 666
    loc.out = as.matrix(loc.out[, -1])
    rownames(loc.out) = loc.rownames
dmattek's avatar
Added:  
dmattek committed
667 668 669 670 671 672 673
    
    # 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
674
    return(loc.out)
dmattek's avatar
Mod:  
dmattek committed
675
  }) 
dmattek's avatar
dmattek committed
676
  
dmattek's avatar
dmattek committed
677
  
dmattek's avatar
Added:  
dmattek committed
678 679 680 681 682 683 684 685 686
  # 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)
    }
  )
  
dmattek's avatar
dmattek committed
687
  
dmattek's avatar
dmattek committed
688 689 690 691
  ####
  ## UI for trajectory plot
  output$varSelHighlight = renderUI({
    cat(file = stderr(), 'UI varSelHighlight\n')
dmattek's avatar
dmattek committed
692
    
dmattek's avatar
dmattek committed
693 694 695
    locBut = input$chBhighlightTraj
    if (!locBut)
      return(NULL)
dmattek's avatar
dmattek committed
696
    
dmattek's avatar
dmattek committed
697
    loc.v = getDataTrackObjLabUni()
dmattek's avatar
dmattek committed
698
    if (!is.null(loc.v)) {
699
      selectInput(
dmattek's avatar
dmattek committed
700 701 702
        'inSelHighlight',
        'Select one or more rajectories:',
        loc.v,
703
        width = '100%',
dmattek's avatar
dmattek committed
704
        multiple = TRUE
705
      )
dmattek's avatar
dmattek committed
706 707 708
    }
  })
  
dmattek's avatar
Added:  
dmattek committed
709
  ###### Trajectory plotting
dmattek's avatar
Mod:  
dmattek committed
710
  callModule(modTrajPlot, 'modTrajPlot', data4trajPlot)
dmattek's avatar
dmattek committed
711
  
dmattek's avatar
Added:  
dmattek committed
712 713 714
  ###### AUC caluclation and plotting
  callModule(modAUCplot, 'tabAUC', data4trajPlot)
  
dmattek's avatar
Added:  
dmattek committed
715 716
  ###### Box-plot
  callModule(tabBoxPlot, 'tabBoxPlot', data4trajPlot)
dmattek's avatar
dmattek committed
717
  
dmattek's avatar
dmattek committed
718 719
  
  
dmattek's avatar
dmattek committed
720 721 722
  ###### Scatter plot
  callModule(tabScatterPlot, 'tabScatter', data4trajPlot)
  
dmattek's avatar
dmattek committed
723
  ##### Hierarchical clustering
dmattek's avatar
Added:  
dmattek committed
724
  callModule(clustHier, 'tabClHier', data4clust, data4trajPlot)
dmattek's avatar
dmattek committed
725 726
  
  ##### Sparse hierarchical clustering using sparcl
dmattek's avatar
Added:  
dmattek committed
727
  callModule(clustHierSpar, 'tabClHierSpar', data4clust, data4trajPlot)
dmattek's avatar
dmattek committed
728

dmattek's avatar
Mod:  
dmattek committed
729
  
dmattek's avatar
dmattek committed
730
})