-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
697 lines (612 loc) · 25.5 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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#' Server
#'
#' Query strings are supported as follows:
#' ?genes=[list of genes]&taxa=[human|mouse|rat]&...
server <- function(input, output, session) {
session$onSessionEnded(function() {
session$userData$INTERRUPT <- TRUE
})
#' Advance the progress bar one step for the client
#'
#' @param detail Any text to show in the progress bar
advanceProgress <- function(detail) {
setProgress(session$userData$progress + 1, detail)
}
#' Set the progress bar to a specified step
#'
#' @param progress The step to move to or `NULL` to complete the progress bar
#' @param detail Any text to show in the progress bar
#' @param n.steps The maximum number of steps this progress bar can take
setProgress <- function(progress = NULL, detail = "", n.steps = NULL) {
if (is.null(progress)) {
if (is.null(session$userData$progress.bar)) {
return(NULL)
}
progress <- session$userData$progress.bar$getMax()
n.steps <- progress
}
session$userData$progress <- progress
if (progress == 0) {
session$userData$progress.bar <- shiny::Progress$new(min = 0, max = n.steps)
session$userData$progress.bar$set(message = "Working...", detail = detail)
} else {
session$userData$progress.bar$set(value = progress, detail = detail)
# Enable the search and reset buttons after reaching completion
if (progress >= session$userData$progress.bar$getMax()) {
session$userData$progress.bar$close()
shinyjs::enable("search")
shinyjs::enable("reset")
}
}
}
# On connect, observe the query string. Search if any genes are specified
observeEvent(input$LOAD,
{
output$results_header <- renderUI(generateResultsHeader({
HTML(paste0(
'<div style="margin-bottom: 10px"><h3 style="display: inline">',
paste0(
"Search across <span>", length(DATA.HOLDER), " species</span>, <span>",
CORPUS_STATS$studies, " studies</span>, <span>",
CORPUS_STATS$comparisons, " condition comparisons</span>, <span>",
CORPUS_STATS$assays, " assays</span>"
),
"</h3></div>"
))
}))
query <- getQueryString(session)
options <- getConfig()
genes <- query$genes %>% jsonify()
taxa <- query$taxa %>% jsonify()
sig <- switch(is.null(query$sig) + 1,
jsonify(query$sig) %>% as.numeric(),
options$sig$value
)
fc <- switch(is.null(query$fc) + 1,
jsonify(query$fc) %>% as.numeric(),
options$fc$value
)
categories <- switch(is.null(query$categories) + 1,
jsonify(query$categories),
options$categories$value
)
# Update UI elements from the query string
updateSelectizeInput(session, "genes")
updatePickerInput(session, "taxa", selected = taxa)
session$sendCustomMessage("querySet", genes)
updateSelectizeInput(session, "sig", selected = sig)
updateSliderInput(session, "fc", value = fc)
updateSelectizeInput(session, "categories", selected = categories)
for (mName in Filter(function(x) !(x %in% c("sig", "fc", "categories", "taxa")), names(options))) {
do.update(session, options[[mName]], ifelse(is.null(query[[mName]]), options[[mName]]$value, query[[mName]]))
}
},
once = T,
ignoreNULL = T
)
# Send a message to the client if a file is uploaded
observeEvent(input$genes.csv, {
if (!is.null(input$genes.csv)) {
session$sendCustomMessage("fileUpload", T)
}
})
# Not exactly sure why I implemented it this way. Maybe to make sure it resets?
output$genes.csv.ui <- renderUI({
input$reset
fileInput("genes.csv", "Or Upload CSV", accept = "text/csv", placeholder = "N/A")
})
# Reset the search bar
observeEvent(input$reset, {
session$sendCustomMessage("queryReset", list())
session$sendCustomMessage("fileUpload", F)
updatePickerInput(session, "taxa", selected = getConfig("taxa")$value)
updateSelectizeInput(session, "method", selected = NULL)
updateSelectizeInput(session, "sig", selected = NULL)
options <- getConfig()
for (mName in Filter(function(x) !(x %in% c("taxa", "method", "sig")), names(options))) {
do.update(session, options[[mName]], options[[mName]]$value)
}
})
# Update gene choices depending on taxon(s). Commented out because only works with selectize.js
observeEvent(input$taxa, {
allowed_groups = which(getConfig('taxa')$choices %>% unlist %in% input$taxa)
shinyjs::runjs(glue::glue("update_options({jsonlite::toJSON(allowed_groups)})"))
})
# Search
observeEvent(input$search, {
genes <- input$genes
if (!is.null(input$genes.csv)) {
genes <- read.csv(input$genes.csv$datapath, header = F)$V1 %>% as.character()
}
searchGenes(genes)
})
#' Open other (non-table) tabs
#'
#' @param tab The tab to open
#' @param delay A delay (in ms) before opening
openTab <- function(tab, delay = 0) {
if (delay > 0) {
shinyjs::delay(delay, {
openTab(tab)
})
} else if (!is.null(session$userData$plotData)) {
if (tab == "Gene Info" && is.null(session$userData$genesRendered)) {
session$userData$genesRendered <- T
synchronise({
geneEvidence(
session$userData$plotData$genes,
session$userData$plotData$options$taxa$value
)$
then(function(evidence) {
output$results_genes <- generateGenePage(evidence)
})
})
} else if (tab == "Gene Contributions" && is.null(session$userData$contribsRendered)) {
session$userData$contribsRendered <- T
output$results_contribs <- generateGeneContribs(session$userData$plotData$conditions, session$userData$plotData$options)
}
}
}
# Open other tabs
observeEvent(input$tabs, {
openTab(input$tabs)
})
# Force an updated of the gene view when we get new search results
observeEvent(input$UPDATED, {
session$userData$genesRendered <- NULL
session$userData$contribsRendered <- NULL
# Delay if we're opening the gene info tab (I forget why)
openTab(input$tabs, 100 * (input$tabs == "Gene Info"))
})
# Populate with random genes
observeEvent(input$RANDOM_GENES, {
updateSelectizeInput(session, "genes", options = list(persist = F, create = T, createOnBlur = T))
while (length(genes <- DATA.HOLDER[[ifelse(length(input$taxa) > 1, "human", input$taxa)]]@gene.meta[sample(1:.N, sample(1:10, 1))] %>%
as.data.frame() %>% .[, sample(c(1, 3, 4, 6), 1)] %>%
.[. != "None"]) == 0) {
}
genes <- genes %>%
{
paste0("[", paste0(., collapse = ","), "]")
} %>%
jsonify()
session$sendCustomMessage("queryReset", genes)
})
# Open plot
observeEvent(input$plotData, {
shiny::showModal(modalDialog(
easyClose = T,
size = "l",
footer = NULL,
generatePlotContainer()
), session)
sendPlot <- function() {
output$plot <- renderPlotly({
generateResultsPlot(
session$userData$plotData$genes,
session$userData$plotData$conditions[`Ontology Steps` == 0] %>%
.[1:5, paste0(cf.BaseLongUri, " vs. ", cf.ValLongUri)], # TODO selection strategy
session$userData$plotData$exprInfo,
session$userData$plotData$options,
input$plot_taxa, input$plot_genes, input$plot_conditions, input$plot_type, input$plot_data
)
})
}
# We don't have plot data; we need to make it
if (is.null(session$userData$plotData$exprInfo)) {
if (!is.null(session$userData$plotData$conditions)) {
# Give the UI a tick to update and then fetch the data.
shinyjs::delay(1, {
synchronise({
mExpData <- rbindlist(lapply(
session$userData$plotData$options$taxa$value,
function(i) DATA.HOLDER[[i]]@experiment.meta[, .(rsc.ID, ee.ID, ee.NumSample)]
))
# Choose experiments to display in the plot
getTags(session$userData$plotData$options$taxa$value) %>% # TODO Selection strategy
.[cf.Cat %in% (session$userData$plotData$conditions[`Ontology Steps` == 0] %>% .[1:5, unique(cf.Cat)]) &
cf.BaseLongUri %in% (session$userData$plotData$conditions[`Ontology Steps` == 0] %>% .[1:5, unique(cf.BaseLongUri)]) &
cf.ValLongUri %in% (session$userData$plotData$conditions[`Ontology Steps` == 0] %>% .[1:5, unique(cf.ValLongUri)])] %>%
unique() %>%
merge(mExpData, by = c("rsc.ID", "ee.ID"), sort = F, allow.cartesian = T) %>%
{
if (nrow(.) == 0) {
return(NULL)
}
# Select some samples to queue for gene expression visualization
while ((tmp <- .[sample(1:nrow(.))])[1, ee.NumSample] > getOption("max.gemma")) {
}
tmp[cumsum(ee.NumSample) <= getOption("max.gemma")]
} %>%
{
geneExpression(
.[, unique(ee.ID)],
.[, unique(rsc.ID)],
session$userData$plotData$options$taxa$value,
session$userData$plotData$genes
)$then(function(value) {
# Delegate the plot renderer
session$userData$plotData$exprInfo <- value
# Send options to the UI
updatePickerInput(session, "plot_taxa",
choices = as.list(session$userData$plotData$options$taxa$value),
selected = session$userData$plotData$options$taxa$value[1]
)
if (length(session$userData$plotData$options$taxa$value) > 1) {
mGenes <- unique(session$userData$plotData$genes[, gene.realName])
} else {
mGenes <- DATA.HOLDER[[session$userData$plotData$options$taxa$value]]@gene.meta[entrez.ID %in% unique(session$userData$plotData$genes), gene.Name]
}
updatePickerInput(session, "plot_genes",
choices = as.list(intersect(mGenes, rownames(value$expr))),
selected = intersect(mGenes, rownames(value$expr))
)
updatePickerInput(session, "plot_conditions", # TODO Selection strategy
choices = as.list(session$userData$plotData$conditions[`Ontology Steps` == 0] %>% .[1:5, paste0(cf.BaseLongUri, " vs. ", cf.ValLongUri)]),
selected = data.table::first(session$userData$plotData$conditions[`Ontology Steps` == 0] %>% .[1:5, paste0(cf.BaseLongUri, " vs. ", cf.ValLongUri)])
)
# Send the plot to the UI
sendPlot()
})
}
})
})
}
} else { # We already have plot data; send it
sendPlot()
}
})
#' Display a failure message
#'
#' Ends the search protocol with a failure message
endFailure <- function() {
setProgress()
output$results_header <- renderUI({
generateResultsHeader(HTML('<h3 style="margin-top: 0;">No results</h2>'))
})
output$results <- NULL
}
#' Make suggestions on taxa/genes for users
#'
#' @param original The original gene query
#' @param taxon The original taxa query
#' @param suggestions Suggestions for closest gene matches or otherwise mapped gene IDs
makeSuggestions <- function(original, taxon, suggestions) {
if (!is.null(suggestions)) {
# If we searched only one taxon, make sure it looks appropriate
if (length(taxon) == 1) {
suggested <- lapply(getConfig("taxa")$core, function(t) {
i <- 0
oGenes <- NA
tmp <- tidyGenes(original, t)
if (!is.null(tmp)) {
i <- length(tmp$genes)
oGenes <- tmp$genes
}
list(
n = i,
genes = oGenes,
taxon = t
)
}) %>%
{
# If a different taxon matches our gene set better, suggest that taxon
suggestedTaxon <- lapply(., "[[", "n") %>% which.max()
list(taxon = .[[suggestedTaxon]]$taxon, genes = .[[suggestedTaxon]]$genes)
}
if (suggested$taxon != taxon) {
# Update the UI
output$results_suggestions <- renderUI({
fluidRow(
class = "info-text",
column(12, HTML(paste0(
"Did you mean to search ", paste0("<a search taxon='", suggested$taxon, "'>", suggested$taxon, "</a>"),
" experiments? Otherwise, try using <a search genes='[", paste0(paste0('"', tidyGenes(suggested$genes, c(taxon, suggested$taxon))[taxon != suggested$taxon, gene.realName], '"'), collapse = ","), "]'>", taxon, " orthologs", "</a> of these ", suggested$taxon, " genes."
)))
)
})
shinyjs::delay(100, {
shinyjs::runjs("loadExamples();")
})
return(NULL)
}
}
# Substitute the corrected genes in place for a new search
revisedSearch <- original
for (i in names(suggestions)) {
revisedSearch <- gsub(i, suggestions[i], revisedSearch, fixed = T)
}
# Update the UI
output$results_suggestions <- renderUI({
fluidRow(class = "info-text", column(12, HTML(paste0("Did you mean: ", paste0("<a search genes='[", paste0(paste0('"', revisedSearch, '"'), collapse = ","), "]'>", paste0(suggestions, collapse = ", "), "</a>"), "?"))))
})
shinyjs::delay(100, {
shinyjs::runjs("loadExamples();")
})
}
}
#' Display an empty message
#'
#' Ends the search protocol with no results
endEmpty <- function() {
setProgress()
output$results_header <- renderUI({
generateResultsHeader("No conditions found in scope.")
})
output$results <- NULL
}
#' Display a success message
#'
#' Ends the search protocol successfully
#'
#' @param genes The genes that were searched
#' @param experiments The experiment rankings that were obtained
#' @param conditions The condition rankings that were obtained
#' @param options Any additional options that were passed
endSuccess <- function(genes, experiments, conditions, taxa) {
if (is.data.table(experiments)) {
exps <- experiments$rn
} else {
exps <- lapply(experiments, "[[", "rn") %>% unlist()
}
tmp <- rbindlist(lapply(taxa, function(i) {
DATA.HOLDER[[i]]@experiment.meta[rsc.ID %in% exps, .(rsc.ID, ee.ID, ee.Name, ee.NumSample, ef.IsBatchConfounded)]
}))
# Generate the results header
if (exists("output")) {
studies <- length(unique(tmp$ee.ID))
assays <- tmp[!duplicated(ee.ID), sum(ee.NumSample)]
n_exp <- length(exps)
output$results_header <- renderUI({
generateResultsHeader(HTML(paste0(
'<div style="margin-bottom: 10px"><h2 style="display: inline">Examined ',
format(n_exp, big.mark = ","), " condition comparison", ifelse(n_exp > 1, "s", ""),
" (", format(studies, big.mark = ","), " studies, ", format(assays, big.mark = ","), " assays) for ",
ifelse(nrow(genes) == 1, genes[, gene.Name],
paste0(
'<span data-toggle="tooltip" data-placement="top" title="',
paste0(unique(genes[, gene.Name]), collapse = ", "), '">',
nrow(genes), " gene", ifelse(nrow(genes) > 1, "s", ""), "</span>"
)
),
ifelse(length(taxa) > 1,
paste0(
' across <span data-toggle="tooltip" data-placement="top" title="', paste0(taxa, collapse = ", "), '">',
length(taxa), " taxa</span>"
), ""
),
'</h2><span class="timestamp">in ',
format(difftime(session$userData$endTime, session$userData$startTime), digits = 3), ".</span></span>"
)))
})
}
conditions %<>%
merge(unique(ONTOLOGIES.DEFS[, .(Node_Long = as.character(Node_Long), cf.Base = as.character(Definition))]),
by.x = "cf.BaseLongUri",
by.y = "Node_Long",
sort = F,
allow.cartesian = T,
all.x = T) %>%
merge(unique(ONTOLOGIES.DEFS[, .(Node_Long = as.character(Node_Long), cf.Val = as.character(Definition))]),
by.x = "cf.ValLongUri",
by.y = "Node_Long",
sort = F,
allow.cartesian = T,
all.x = T
)
# Some cleaning
conditions[, `Condition Comparison` := stringi::stri_c("<b>", cf.Base, "</b> vs. <b>", cf.Val, "</b>")]
advanceProgress("Cross-linking")
# Associating experiments with tags
tmp <- tmp %>%
merge(getTags(taxa, exps), sort = F) %>%
.[, N := length(unique(ee.ID)), .(cf.Cat, cf.BaseLongUri, cf.ValLongUri)] %>%
.[N < 0.03 * nrow(tmp)] # Get rid of contrasts that overlap in more than 3% experiments
# Add links to Gemma (short form here to not create long strings which can be memory hungry)
# these are dealt with in JS when the table pages show
tmp[
, Evidence := ifelse(N == 1,
stringi::stri_c('<span data-id="', ee.ID[1], '"', ifelse(any(ef.IsBatchConfounded), stringi::stri_c(' data-conf="', stringi::stri_c(which(ef.IsBatchConfounded)), '"'), ""), ">1 Experiment</span>"),
{
dedup <- !duplicated(ee.ID)
stringi::stri_c(
'<span data-id="', stringi::stri_c(ee.ID[dedup], collapse = ","),
'" data-ee="', stringi::stri_c(ee.Name[dedup], collapse = ","), '"',
ifelse(any(ef.IsBatchConfounded), stringi::stri_c(' data-conf="', stringi::stri_c(which(ef.IsBatchConfounded)), '"'), ""),
">", N, " Experiments</span>"
)
}
),
.(cf.Cat, cf.BaseLongUri, cf.ValLongUri)
]
tmp[, EvidencePlain := {
dedup <- !duplicated(ee.ID)
stringi::stri_c(ee.Name[dedup], collapse = ",")
},.(cf.Cat, cf.BaseLongUri, cf.ValLongUri)]
tmp[, .(cf.Cat, cf.BaseLongUri, cf.ValLongUri, N, Evidence, EvidencePlain)] %>%
unique() %>%
merge(conditions, by = c("cf.Cat", "cf.BaseLongUri", "cf.ValLongUri"), sort = F) %>%
setnames(c("stat", "score", "distance"), c("Effect Size", "Test Statistic", "Ontology Steps"))
}
#' Handle Search
#'
#' Call the processing function to handle searching and update the display.
#'
#' @param genes A character vector of entrez IDs
#' @param options The search options
handleSearch <- function(genes, rawGenes, options) {
advanceProgress("Ranking experiments")
# For some reason genes becomes a Promise and this resolves it?
# TODO this is dirty and needs to be re-examined
genes
future(
{
# First run a search asynchronously
# print(options$mfx$value)
if (!is.null(genes) && length(options$taxa$value) > 1 && "data.table" %in% class(genes)) {
lapply(options$taxa$value, function(t) {
mOp <- options
mOp$taxa$value <- t
vsmSearch(genes[taxon == t, entrez.ID], taxa = mOp$taxa$value, confounds = mOp$confounds$value, filter = mOp$filter$value, mfx = FALSE, geeq = mOp$geeq$value, p_threshold = mOp$pv$value)
})
} else if (length(options$taxa$value) == 1) {
vsmSearch(genes$genes, taxa = options$taxa$value, confounds = FALSE, filter = options$filter$value, mfx = FALSE, geeq = options$geeq$value, p_threshold = 0.05)
}
},
globals = "DATA.HOLDER",
seed = T
) %...>% (function(experiments) {
if (!is.null(genes) && !("data.table" %in% class(genes))) {
makeSuggestions(rawGenes, options$taxa$value, genes$suggestions)
}
if (!is.null(session$userData$INTERRUPT)) {
return(NULL)
}
if (all(sapply(experiments, is.null))) {
endFailure()
} else {
advanceProgress("Enriching")
future(
{
# Do enrichments asynchronously too.
if (length(options$taxa$value) == 1) {
copy(enrich(experiments, taxa = options$taxa$value, dist = options$dist$value,categories = options$categories$value))
} else {
# TODO it's maybe reasonable to mclapply this as long as the
# multicore workers (@seealso dependencies.R) is sufficiently low
lapply(1:length(options$taxa$value), function(t) {
mOp <- options
mOp$taxa$value <- options$taxa$value[t]
mSearchable <- experiments[[t]]
enrich(mSearchable, taxa = mOp$taxa$value, dist = mOp$dist$value,categories = mOp$categories$value) %>%
setnames(genes[taxon == options$taxa$value[t], gene.realName],
genes[taxon == options$taxa$value[t], identifier],
skip_absent = T
) # TODO ?
}) %>%
rbindlist(fill = T) %>%
# reorderTags3() %>% # appears to be redundant as cache is already re-ordered
.[, lapply(.SD, mean, na.rm = T), .(cf.Cat, cf.BaseLongUri, cf.ValLongUri)]
}
},
globals = c("CACHE.BACKGROUND", "NULLS"),
seed = TRUE
) %...>% (function(conditions) {
if (!is.null(session$userData$INTERRUPT)) {
return(NULL)
}
session$userData$endTime <- Sys.time()
if (is.null(conditions)) {
endEmpty()
} else {
if (length(options$taxa$value) > 1) {
geneInfo <- genes %>%
copy() %>%
setnames("identifier", "gene.Name")
mGenes <- genes %>% copy()
} else {
geneInfo <- DATA.HOLDER[[options$taxa$value]]@gene.meta %>%
.[, .(.I, entrez.ID, gene.Name)] %>%
.[entrez.ID %in% genes$genes, .(I, entrez.ID, gene.Name)]
mGenes <- genes$genes
}
conditions <- endSuccess(geneInfo, experiments, conditions, options$taxa$value)
if (!is.null(session$userData$INTERRUPT)) {
return(NULL)
}
getPercentageStat <- function(x, n = 1){
x / n
}
conditions[,'Test Statistic'] <- apply(conditions[,'Test Statistic'], 2, getPercentageStat, n = nrow(geneInfo))
output$results <- generateResults(conditions)
# Prepare some plotting information.
session$userData$plotData <- list(
options = options,
genes = mGenes,
conditions = conditions %>% copy()
)
# Prepare a data download handler
output$dataDownload <- downloadHandler(function() {
paste0("enrichment-", Sys.Date(), ".csv")
}, function(file) {
write.csv(session$userData$plotData$conditions %>%
.[, !c("Condition Comparison", "Evidence")] %>%
data.table::setorder(-`Test Statistic`, `Ontology Steps`) %>%
data.table::setnames(c("cf.Cat", "cf.BaseLongUri", "cf.ValLongUri","EvidencePlain"), c("Category", "Baseline", "Value","Evidence")), file)
}, "text/csv")
}
})
}
})
}
#' Search Genes
#'
#' Begin the gene search process. Cleans the parameters as necessary and calls a function to
#' perform the actual search and update the display.
#'
#' @param genes A character vector of genes (entrez ID, ensembl ID, name or keyword)
#' @param signature A DE signature to search
#' @param taxa The taxon ([human|mouse|rat])
#' @param update Whether or not to update the browser's query string.
searchGenes <- function(genes = input$genes,
signature = input$signature,
taxa = input$taxa,
update = T) {
output$results_suggestions <- NULL
# These will be re-enabled when the progress bar completes (@seealso setProgress)
shinyjs::disable("search")
shinyjs::disable("reset")
session$userData$startTime <- as.POSIXct(Sys.time())
setProgress(0, "Validating input", n.steps = getOption("max.progress.steps"))
# Populate options
options <- lapply(names(getConfig()), function(x) {
ret <- getConfig(x)
if (x == "sig") {
v <- signature
} else {
v <- input[[x]]
}
if (!is.null(v)) {
ret$value <- v
}
ret
}) %>% `names<-`(names(getConfig()))
# Update the query string
if (update) {
query <- paste0("?genes=", switch(min(2, length(genes)),
genes,
paste0("[", paste0(genes, collapse = ","), "]")
))
# TODO have to look into categories and subsets, might need to be treated specially
extras <- lapply(names(getConfig()), function(x) {
if (x == "sig") {
v <- signature
} else {
v <- input[[x]]
}
if (!is.null(v) && v != "" && (!isTRUE(all.equal(v, getConfig(key = x)$value)) || length(v) != length(getConfig(key = x)$value))) {
if (length(v) > 1) {
paste0(x, "=", paste0("[", paste0(v, collapse = ","), "]"))
} else {
paste0(x, "=", v)
}
}
}) %>%
unlist() %>%
paste0(collapse = "&")
if (nchar(extras) > 0) {
query <- paste0(query, "&", extras)
}
updateQueryString(query, "push")
}
if (is.null(taxa)) taxa <- options$taxa$value
# Done processing, handle the search.
# TODO should prevent signature from working for length(taxa) > 1
# TODO This { . } is a weird way to prevent lazy evaluation. Probably a better way
tidyGenes(genes, taxa) %>%
{
.
} %>%
handleSearch(genes, options)
}
}