From 3b2ba5b27dd3d1d5116407b9889cbe7022c2b877 Mon Sep 17 00:00:00 2001 From: Ivo Kwee Date: Thu, 2 Nov 2023 02:40:49 +0100 Subject: [PATCH] total counts as log10 --- .../R/dataview_plot_totalcounts.R | 23 +++++++++------- components/board.dataview/R/dataview_server.R | 27 ++++--------------- components/board.dataview/R/dataview_ui.R | 2 +- 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/components/board.dataview/R/dataview_plot_totalcounts.R b/components/board.dataview/R/dataview_plot_totalcounts.R index f301168e0..4cc837d42 100644 --- a/components/board.dataview/R/dataview_plot_totalcounts.R +++ b/components/board.dataview/R/dataview_plot_totalcounts.R @@ -28,25 +28,25 @@ dataview_plot_totalcounts_ui <- function( } dataview_plot_totalcounts_server <- function(id, - getCountsTable, + getCountStatistics, r.data_groupby = reactive(""), watermark = FALSE) { moduleServer(id, function(input, output, session) { plot_data <- shiny::reactive({ data_groupby <- r.data_groupby() - tbl <- getCountsTable() + tbl <- getCountStatistics() req(tbl) - ylab <- "Total counts" + ylab <- "total counts (log10)" if (data_groupby != "") { - ylab <- "Mean total counts" + ylab <- "average total counts (log10)" } res <- list( df = data.frame( sample = names(tbl$total.counts), - counts = tbl$total.counts + counts = log10(tbl$total.counts) ), ylab = ylab ) @@ -103,20 +103,23 @@ dataview_plot_totalcounts_server <- function(id, "" ) ) %>% + plotly_default() %>% plotly::layout( xaxis = list(title = FALSE), yaxis = list(title = res$ylab), - font = list(family = "Lato"), - margin = list(l = 10, r = 10, b = 10, t = 10) - ) %>% - plotly_default() + margin = list(l = 30, r = 0, t = 0, b = 0) + ) fig } modal_plotly.RENDER <- function() { plotly.RENDER() %>% - plotly_modal_default() + plotly_modal_default() %>% + plotly::layout( + margin = list(l = 35, r = 0, t = 0, b = 0) + ) + } PlotModuleServer( diff --git a/components/board.dataview/R/dataview_server.R b/components/board.dataview/R/dataview_server.R index c2581ea1d..d43105c43 100644 --- a/components/board.dataview/R/dataview_server.R +++ b/components/board.dataview/R/dataview_server.R @@ -279,7 +279,7 @@ DataViewBoard <- function(id, pgx) { getCountStatistics <- reactiveVal() - observeEvent(c(input$data_groupby, input$data_samplefilter), + observeEvent( c(input$data_groupby, input$data_samplefilter, input$data_type), { shiny::req(pgx$X, pgx$Y, pgx$samples) shiny::validate(shiny::need("counts" %in% names(pgx), "no 'counts' in object.")) @@ -288,35 +288,19 @@ DataViewBoard <- function(id, pgx) { samples <- colnames(pgx$X) samples <- playbase::selectSamplesFromSelectedLevels(pgx$Y, input$data_samplefilter) nsamples <- length(samples) - if ("counts" %in% names(pgx)) { + if (input$data_type == "counts") { counts <- pgx$counts[, samples, drop = FALSE] } else { - cat("WARNING:: no counts table. estimating from X\n") counts <- pmax(2**pgx$X - 1, 0) - k <- grep("lib.size", colnames(pgx$samples))[1] - if (length(k) > 0) { - libsize <- pgx$samples[colnames(counts), k] - libsize - counts <- t(t(counts) * libsize) - } - } - if (sum(is.na(counts)) > 0) { - cat("WARNING:: plot counts: counts has missing values!\n") } grpvar <- input$data_groupby gr <- pgx$Y[samples, grpvar] grps <- sort(unique(gr)) if (input$data_groupby != "" && length(grps) > 1) { - newx <- c() - for (g in grps) { - mx <- rowMeans(counts[, which(gr == g), drop = FALSE], na.rm = TRUE) - newx <- cbind(newx, mx) - } - if (NCOL(newx) == 1) newx <- matrix(newx, ncol = 1) - rownames(newx) <- rownames(counts) - colnames(newx) <- grps - counts <- newx + mx <- tapply( 1:ncol(counts), gr, + function(ii) rowMeans(counts[,ii,drop=FALSE],na.rm=TRUE)) + counts <- do.call(cbind, mx) } ## if too many samples (like scRNA-seq do subsampling...) @@ -378,7 +362,6 @@ DataViewBoard <- function(id, pgx) { colnames(log2counts) <- substring(colnames(log2counts), 1, 30) colnames(prop.counts) <- substring(colnames(prop.counts), 1, 30) - res <- list( total.counts = total.counts, subtt = subtt, diff --git a/components/board.dataview/R/dataview_ui.R b/components/board.dataview/R/dataview_ui.R index b48cb97e0..9a30d3a94 100644 --- a/components/board.dataview/R/dataview_ui.R +++ b/components/board.dataview/R/dataview_ui.R @@ -168,7 +168,7 @@ DataViewUI <- function(id) { ), dataview_plot_boxplot_ui( ns("counts_boxplot"), - title = "Median counts distribution", + title = "Counts boxplots", info.text = "The samples (or cells) can be grouped/ungrouped in the grouped setting under the main Options.", caption = "Distribution of total counts per sample/group. The center horizontal bar correspond to the median.", height = c("100%", TABLE_HEIGHT_MODAL),