Skip to content

Commit

Permalink
total counts as log10
Browse files Browse the repository at this point in the history
  • Loading branch information
ivokwee committed Nov 2, 2023
1 parent f1f0dee commit 3b2ba5b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
23 changes: 13 additions & 10 deletions components/board.dataview/R/dataview_plot_totalcounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "<ungrouped>") {
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
)
Expand Down Expand Up @@ -103,20 +103,23 @@ dataview_plot_totalcounts_server <- function(id,
"<extra></extra>"
)
) %>%
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(
Expand Down
27 changes: 5 additions & 22 deletions components/board.dataview/R/dataview_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -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."))
Expand All @@ -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 != "<ungrouped>" && 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...)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion components/board.dataview/R/dataview_ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 3b2ba5b

Please sign in to comment.