forked from MaayanLab/paea-shiny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
679 lines (534 loc) · 20 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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
library(shiny)
library(magrittr)
library(nasbMicrotaskViewerHelpers)
source('downloads_handlers.R', local=TRUE)
source('config.R', local=TRUE)
last_modified <- sort(sapply(list.files(), function(x) strftime(file.info(x)$mtime)), decreasing=TRUE)[1]
options(shiny.maxRequestSize=120*1024^2)
perturbations_data <- lapply(
config$data_paths,
function(data_path) {
force(data_path)
shiny::reactive({ nasbMicrotaskViewerHelpers::preprocess(data_path, drop_duplicates=config$drop_duplicates) })
}
)
disease_sigs <- data.table::fread(config$sigs_list_path)
disease_sigs_choices <- setNames(
disease_sigs$file_name,
paste(disease_sigs$disease, disease_sigs$cell_type, disease_sigs$geo_id, sep = ' | ')
)
shiny::shinyServer(function(input, output, session) {
output$last_modified <- shiny::renderText({ last_modified })
values <- shiny::reactiveValues(
# Not required. Just to remind myself what is stored inside
chdir = NULL,
control_samples = NULL,
treatment_samples = NULL,
last_error = NULL,
chdir_params = NULL,
input_name = NULL,
# Required
chdir_running = FALSE,
paea_running = FALSE,
manual_upload = TRUE,
disease_sig_fetch_running = FALSE
)
error_handler <- shiny_error_handler(values)
#' Render last error
#'
output$last_error <- shiny::renderText({
values$last_error$message
})
#' datain panel - ugly hack to be able to clear upload widget
#'
output$datain_container <- shiny::renderUI({
shiny::fileInput(
'datain', 'Choose file to upload',
accept = c(
'text/csv', 'text/comma-separated-values',
'text/tab-separated-values', 'text/plain',
'.csv', '.tsv'
)
)
})
#' datain panel - upload observer
#'
shiny::observe({
if (is.null(input$datain)) return()
datain <- input$datain
values$manual_upload <- TRUE
values$datapath <- datain$datapath
values$input_name <- datain$name
})
#' datain panel - disease signature choice
#'
shiny::updateSelectizeInput(
session, 'disease_sigs_choices',
choices = disease_sigs_choices, server = TRUE
)
output$fetch_disease_sig_container <- shiny::renderUI({
button <- shiny::actionButton('fetch_disease_sig', 'Fetch signature')
if(input$disease_sigs_choices == '' || values$disease_sig_fetch_running) {
list(disabledActionButton(button))
} else {
list(button)
}
})
#' datain panel - disease sig observe
#'
shiny::observe({
if(is.null(input$fetch_disease_sig) || input$fetch_disease_sig == 0) { return() }
values$disease_sig_fetch_running <- TRUE
tryCatch({
choice <- shiny::isolate(input$disease_sigs_choices)
values$input_name <- choice
values$datapath <- if(stringi::stri_startswith_fixed(config$sigs_path, 'http')) {
download.file(file.path(config$sigs_path, choice), file.path(tempdir(), choice))
file.path(tempdir(), choice)
} else {
file.path(config$sigs_path, choice)
}
sig <- disease_sigs %>% dplyr::filter(file_name == values$input_name)
values$control_samples <- unlist(stringi::stri_split_fixed(sig$ctrl_ids, ','))
values$treatment_samples <- unlist(stringi::stri_split_fixed(sig$pert_ids, ','))
values$manual_upload <- FALSE
},
error = error_handler
)
values$disease_sig_fetch_running <- FALSE
})
#' Read input data
#'
datain <- shiny::reactive({
if(!is.null(values$datapath)) {
tryCatch(
if(values$manual_upload) {
data.table::fread(values$datapath, sep=input$sep)
} else {
data.table::fread(values$datapath) %>% dplyr::select(-ID_REF)
},
error = error_handler
)
}
})
#' Is input valid?
#'
datain_valid <- shiny::reactive({ datain_is_valid(datain()) })
#' Apply preprocessing steps to datain
#'
#'
datain_preprocessed <- shiny::reactive({
if(datain_valid()) {
id_filter <- if(input$enable_id_filter) {
config$id_filter
} else { NULL }
datain_preprocess(
datain=datain(),
log2_transform=input$log2_transform,
quantile_normalize=input$quantile_normalize,
id_filter=id_filter
)
} else {
datain()
}
})
#' datain - input data preview
#'
output$contents <- shiny::renderDataTable({
datain_preprocessed()
})
#' datain panel - control/treatment samples checboxes
#'
output$sampleclass_container <- shiny::renderUI({
datain_valid <- datain_valid()
if (!values$manual_upload) {
shiny::helpText('You can choose samples only for manually uploaded data sets.')
} else if (datain_valid) {
shiny::checkboxGroupInput(
'sampleclass',
'Choose control samples',
colnames(datain())[-1]
)
} else {
#' Manual upload and invalid data
shiny::helpText(attributes(datain_valid)$message)
}
})
#' Update lists of control/treatment samples
#'
shiny::observe({
if(!values$manual_upload) { return() }
if(datain_valid()) {
datain <- datain()
samples_mask <- colnames(datain)[-1] %in% input$sampleclass
values$control_samples <- colnames(datain)[-1][samples_mask]
values$treatment_samples <- colnames(datain)[-1][!samples_mask]
} else {
values$control_samples <- NULL
values$treatment_samples <- NULL
}
})
#' datain panel - status message
#'
output$upload_message <- shiny::renderText({
if(is.null(datain())) {
'preview not available...'
}
})
#' datain tab - set plots visibility
#'
output$show_datain_results <- shiny::reactive({ datain_valid() })
shiny::outputOptions(output, 'show_datain_results', suspendWhenHidden = FALSE)
#' datain tab - density plot
#'
shiny::observe({
if(!datain_valid()) { return() }
plot_density(datain_preprocessed()) %>%
ggvis::bind_shiny('datain_density_ggvis')
})
#' chdir panel - run button
#'
#'
output$run_chdir_container <- shiny::renderUI({
button <- shiny::actionButton(inputId = 'run_chdir', label = 'Run Characteristic Direction Analysis', icon = NULL)
if(!datain_valid() | length(values$control_samples) < 2 | length(values$treatment_samples) < 2 | values$chdir_running) {
list(
disabledActionButton(button),
if (is.null(datain())) {
shiny::helpText('Upload your dataset and select control samples first.')
} else {
shiny::helpText('You need at least two control and two treatment samples to run chdir.')
}
)
} else {
button
}
})
#' Not the best solution, but we want to render buttons even if we switch tabs using tourist
#'
shiny::outputOptions(output, 'run_chdir_container', suspendWhenHidden = FALSE)
#' chdir panel - random number generator seed
#'
output$random_seed_container <- shiny::renderUI({
rng_seed_input <- shiny::numericInput(
inputId='random_seed',
label='Random number generator seed',
value=as.integer(Sys.time())
)
if(!input$set_random_seed){
disabledNumericInput(rng_seed_input)
} else {
rng_seed_input
}
})
#' chdir panel - number of probes
#'
output$nprobes <- shiny::renderText({
if(datain_valid()) {
nrow(datain())
}
})
#' chdir panel - number of genes
#'
output$ngenes <- shiny::renderText({
if(datain_valid()) {
length(unique(datain()[[1]]))
}
})
#' chdir panel - control samples
#'
output$control_samples <- shiny::renderText({
paste(values$control_samples, collapse = ', ')
})
#' chdir panel - treatment samples
#'
output$treatment_samples <- shiny::renderText({
paste(values$treatment_samples, collapse = ', ')
})
#' Run Characteristic Direction Analysis
#'
shiny::observe({
if(is.null(input$run_chdir) || input$run_chdir == 0) { return() }
datain <- shiny::isolate(datain_preprocessed())
nnull <- min(as.integer(shiny::isolate(input$chdir_nnull)), 1000)
gamma <- shiny::isolate(input$chdir_gamma)
seed <- shiny::isolate(input$random_seed)
sampleclass <- factor(ifelse(colnames(datain)[-1] %in% values$control_samples, '1', '2'))
set.seed(seed)
values$chdir_running <- TRUE
#' Store parameters.
#'
values$chdir_params <- list(
manual_upload=values$manual_upload,
input_name=values$input_name,
control_samples=values$control_samples,
treatment_samples=values$treatment_samples,
log2_transform=input$log2_transform,
quantile_normalize=input$quantile_normalize,
enable_id_filter=input$enable_id_filter,
gamma=gamma,
nnull=nnull,
seed=seed
)
values$chdir <- tryCatch(
chdir_analysis_wrapper(preprocess_chdir_input(datain), sampleclass, gamma, nnull),
error = error_handler
)
values$chdir_running <- FALSE
})
#' chdir tab - set plots visibility
#'
output$show_chdir_results <- shiny::reactive({ !is.null(values$chdir) })
shiny::outputOptions(output, 'show_chdir_results', suspendWhenHidden = FALSE)
#' Plot top genes from Characteristic Direction Analysis
#'
shiny::observe({
# Not as reactive as it should be
# https://groups.google.com/forum/#!topic/ggvis/kQQsdn1RYaE
if(!is.null(values$chdir)) {
results <- prepare_results(values$chdir$results[[1]])
plot_top_genes(results) %>% ggvis::bind_shiny('chdir_ggvis_plot')
}
})
#' chdir panel number of significant genes to keep
#'
output$ngenes_tokeep_contatiner <- shiny::renderUI({
slider <- shiny::sliderInput(
'ngenes_tokeep', label='Limit number of genes to return',
min=1, max=config$max_ngenes_tokeep, step=1, value=100, round=TRUE
)
if(!is.null(values$chdir)) {
ngenes <- length(values$chdir$results[[1]])
limit <- min(config$max_fgenes_tokeep * ngenes, min(config$max_ngenes_tokeep, ngenes))
slider$children[[2]]$attribs['data-max'] <- limit
slider$children[[2]]$attribs['data-from'] <- ceiling(limit / 2)
}
slider
})
#' chdir panel - download block
#'
output$chdir_downloads_container <- shiny::renderUI({
buttons <- list(
shiny::downloadButton('download_chdir', 'Download chdir'),
shiny::downloadButton('download_chdir_up', 'Download up genes'),
shiny::downloadButton('download_chdir_down', 'Download down genes')
)
if (is.null(values$chdir)) {
append(
lapply(buttons, disabledActionButton),
list(shiny::helpText('No data available. Did you run CHDIR analysis?'))
)
} else {
buttons
}
})
#' See coment for run_chdir_container
#'
shiny::outputOptions(output, 'chdir_downloads_container', suspendWhenHidden = FALSE)
#' chdir panel - number of significant upregulated genes
#'
output$n_sig_up_genes <- shiny::renderText({
if(!is.null(values$chdir)) {
nrow(chdir_up_genes())
}
})
#' chdir panel - number of significant downregulated genes
#'
output$n_sig_down_genes <- shiny::renderText({
if(!is.null(values$chdir)) {
nrow(chdir_down_genes())
}
})
#' chdir panel - chdir download
#'
output$download_chdir <- shiny::downloadHandler(
filename = 'chdir.tsv',
content = chdir_download_handler(prepare_results(values$chdir$chdirprops$chdir[[1]][, 1]))
)
#' chdir panel - prepare down genes
#'
chdir_up_genes <- shiny::reactive({
if(!is.null(values$chdir)) {
head(prepare_up_genes(values$chdir$results[[1]]), input$ngenes_tokeep)
}
})
#' chdir panel - prepare up genes
#'
chdir_down_genes <- shiny::reactive({
if(!is.null(values$chdir)) {
head(prepare_down_genes(values$chdir$results[[1]]), input$ngenes_tokeep)
}
})
#' chdir panel - chdir download
#'
output$download_chdir_up <- shiny::downloadHandler(
filename = 'chdir_up_genes.tsv',
content = chdir_download_handler(chdir_up_genes())
)
#' chdir panel - chdir download
#'
output$download_chdir_down <- shiny::downloadHandler(
filename = 'chdir_up_genes.tsv',
content = chdir_download_handler(chdir_down_genes())
)
#' chdir panel - up genes table
#'
output$chdir_up_genes_table <- shiny::renderDataTable({
if(!is.null(values$chdir)) {
chdir_up_genes() %>% dplyr::rename(Gene = g, 'Characteristic Direction Coefficient' = v)
}
})
#' chdir panel - down genes table
#'
output$chdir_down_genes_table <- shiny::renderDataTable({
if(!is.null(values$chdir)) {
chdir_down_genes() %>% dplyr::rename(Gene = g, 'Characteristic Direction Coefficient' = v)
}
})
#' chdir panel - status message
#'
output$chdir_message <- shiny::renderText({
if(is.null(values$chdir)) {
'results not available...'
}
})
#' chdir panel - Enrichr submit form
#'
output$enrichr_form <- shiny::renderUI({
button <-shiny::tags$button('Analyze with Enrichr', class='btn btn-default')
chdir_diff_genes <- list(up=chdir_up_genes, down=chdir_down_genes)
value <- if(is.null(values$chdir)) { '' } else {
chdir_diff_genes[[input$enrichr_subset]]() %>% prepare_enrichr_input()
}
#' Prepare description string for Enrichr
description <- paste(values$input_name, ' (', input$enrichr_subset, ')', sep='')
shiny::tags$form(
target='_blank', method='post', enctype='multipart/form-data',
action='http://amp.pharm.mssm.edu/Enrichr/enrich',
shiny::tags$input(name='list', type='hidden', value=value),
shiny::tags$input(name='description', type='hidden', value=description),
if(is.null(values$chdir)) { disabledActionButton(button) } else { button }
)
})
#' chdir panel - run summary
#'
output$chdir_run_summary <- shiny::renderUI({
if(!is.null(values$chdir)) {
list_to_defs(values$chdir_params)
}
})
#' paea panel - run button
#'
output$run_paea_container <- shiny::renderUI({
button <- shiny::actionButton(inputId = 'run_paea', label = 'Run Principle Angle Enrichment', icon = NULL)
if (values$paea_running) {
list(disabledActionButton(button))
} else if(is.null(values$chdir)) {
list(
list(disabledActionButton(button)),
shiny::helpText('Before you can run PAEA you have to execute CHDIR analysis.')
)
} else {
list(button)
}
})
#' See coment for run_chdir_container
#'
shiny::outputOptions(output, 'run_paea_container', suspendWhenHidden = FALSE)
#' Choose background dataset
#'
output$background_dataset_container <- shiny::renderUI({
datasets <- names(perturbations_data)
shiny::radioButtons(
'background_dataset', 'Background',
setNames(datasets, stringi::stri_trans_totitle(datasets))
)
})
#' paea panel - workflow flowchart
#'
output$paea_strategy_chart <- shiny::renderImage(
list(src=file.path('www/img', paste(input$paea_strategy, 'png', sep='.')), contentType='image/png'),
deleteFile=FALSE
)
#' Run Principle Angle Enrichment Analysis
#'
shiny::observe({
if(is.null(input$run_paea) || input$run_paea == 0) { return() }
chdir <- shiny::isolate(values$chdir)
casesensitive <- shiny::isolate(input$paea_casesensitive)
background_dataset <- shiny::isolate(input$background_dataset)
if(!(is.null(chdir))) {
values$paea_running <- TRUE
values$paea_params <- append(
list(
casesensitive=casesensitive,
background_dataset=background_dataset
),
values$chdir_params
)
values$paea <- tryCatch(
shiny::withProgress(message = '', value = 0, {
lapply(paea_analysis_dispatch(
chdirresults=chdir$chdirprops,
gmtfile=prepare_gene_sets(perturbations_data[[background_dataset]]()$genes),
casesensitive=casesensitive,
with_progress=TRUE
), paea_to_df)
}),
error = error_handler
)
values$paea_running <- FALSE
}
})
#' PAEA results
#'
paea_results <- shiny::reactive({
if(!is.null(values$paea)) {
description <- perturbations_data[[values$paea_params$background_dataset]]()$description
prepare_paea_results(combine_results(values$paea, input$paea_strategy), description)
}
})
#' PAEA output
#'
output$paea_results_table <- shiny::renderDataTable({
if(!is.null(paea_results())) {
paea_results()
}
})
#' paea panel - download block
#'
output$paea_downloads_container <- shiny::renderUI({
button <- shiny::downloadButton('download_paea', 'Download current view')
if (is.null(values$paea)) {
list(
disabledActionButton(button),
shiny::helpText('No data available. Did you run PAEA analysis?')
)
} else {
list(button)
}
})
#' See coment for run_chdir_container
#'
shiny::outputOptions(output, 'paea_downloads_container', suspendWhenHidden = FALSE)
#' paea panel - downloads handler
#'
output$download_paea <- shiny::downloadHandler(
filename = 'paea_results.tsv',
content = paea_download_handler(paea_results())
)
#' paea panel - status message
#'
output$paea_message <- shiny::renderText({
if(is.null(values$paea)) {
'results not available...'
}
})
#' chdir panel - run summary
#'
output$paea_run_summary <- shiny::renderUI({
if(!is.null(values$paea)) {
list_to_defs(values$paea_params)
}
})
})