From 9fb6ca428eb155b8c374871e6cb4d9990b2ad48a Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Thu, 18 Aug 2022 10:00:05 +0200 Subject: [PATCH] gsea: Move to separate module --- R/gsea.R | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ R/results.R | 68 ++---------------------------------------- 2 files changed, 88 insertions(+), 66 deletions(-) create mode 100644 R/gsea.R diff --git a/R/gsea.R b/R/gsea.R new file mode 100644 index 0000000..c090036 --- /dev/null +++ b/R/gsea.R @@ -0,0 +1,86 @@ +#' Create a GSEA page. +#' @noRd +gsea_ui <- function(id) { + verticalLayout( + div( + style = "margin-top: 16px", + plotly::plotlyOutput(NS(id, "plot")), + ), + div( + style = "margin-top: 16px", + DT::DTOutput(NS(id, "details")) + ) + ) +} + +#' Create a server for the comparison editor. +#' +#' @param id ID for namespacing the inputs and outputs. +#' @param ranking The ranking to be analyzed. +#' +#' @noRd +gsea_server <- function(id, ranking) { + moduleServer(id, function(input, output, session) { + gsea_analysis <- reactive({ + withProgress( + message = "Querying g:Profiler", + value = 0.0, + { # nolint + setProgress(0.2) + gprofiler2::gost( + ranking()[, gene], + custom_bg = NULL, # TODO + domain_scope = "custom_annotated" + ) + } + ) + }) |> bindCache(ranking()) + + output$plot <- plotly::renderPlotly({ + gprofiler2::gostplot( + gsea_analysis(), + capped = FALSE, + interactive = TRUE + ) + }) + + output$details <- DT::renderDT({ + data <- data.table(gsea_analysis()$result) + setorder(data, p_value) + + data[, total_ratio := term_size / effective_domain_size] + data[, query_ratio := intersection_size / query_size] + data[, increase := (query_ratio - total_ratio) / total_ratio] + + data <- data[, .( + source, + term_name, + total_ratio, + query_ratio, + increase, + p_value + )] + + DT::datatable( + data, + rownames = FALSE, + colnames = c( + "Source", + "Term", + "Total ratio", + "Query ratio", + "Increase", + "p-value" + ), + options = list( + pageLength = 25 + ) + ) |> + DT::formatRound("p_value", digits = 4) |> + DT::formatPercentage( + c("total_ratio", "query_ratio", "increase"), + digits = 2 + ) + }) + }) +} diff --git a/R/results.R b/R/results.R index 899d5f9..eb712b6 100644 --- a/R/results.R +++ b/R/results.R @@ -152,11 +152,7 @@ results_ui <- function(id, options) { title = "g:Profiler", div( style = "margin-top: 16px", - plotly::plotlyOutput("gost_plot"), - ), - div( - style = "margin-top: 16px", - DT::DTOutput(NS(id, "gost_details")) + gsea_ui(NS(id, "gsea")) ) ) ) @@ -407,67 +403,7 @@ results_server <- function(id, options, analysis) { ) }) - gost <- reactive({ - withProgress( - message = "Querying g:Profiler", - value = 0.0, - { # nolint - setProgress(0.2) - gprofiler2::gost( - results_filtered()[, gene], - custom_bg = preset()$gene_ids, - domain_scope = "custom_annotated" - ) - } - ) - }) |> bindCache(results_filtered(), preset()) - - output$gost_plot <- plotly::renderPlotly({ - gprofiler2::gostplot( - gost(), - capped = FALSE, - interactive = TRUE - ) - }) - - output$gost_details <- DT::renderDT({ - data <- data.table(gost()$result) - setorder(data, p_value) - - data[, total_ratio := term_size / effective_domain_size] - data[, query_ratio := intersection_size / query_size] - data[, increase := (query_ratio - total_ratio) / total_ratio] - - data <- data[, .( - source, - term_name, - total_ratio, - query_ratio, - increase, - p_value - )] - - dt <- DT::datatable( - data, - rownames = FALSE, - colnames = c( - "Source", - "Term", - "Total ratio", - "Query ratio", - "Increase", - "p-value" - ), - options = list( - pageLength = 25 - ) - ) |> - DT::formatRound("p_value", digits = 4) |> - DT::formatPercentage( - c("total_ratio", "query_ratio", "increase"), - digits = 2 - ) - }) + gsea_server("gsea", results_filtered) }) }