Skip to content

Commit

Permalink
Fix foldchange calculation for all normalization approaches
Browse files Browse the repository at this point in the history
  • Loading branch information
saketkc committed Jul 6, 2023
1 parent e08e108 commit 992c1a9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
45 changes: 29 additions & 16 deletions R/differential_expression.R
Original file line number Diff line number Diff line change
Expand Up @@ -1095,24 +1095,37 @@ FoldChange.Assay <- function(
) {
pseudocount.use <- pseudocount.use %||% 1
data <- GetAssayData(object = object, slot = slot)
default.mean.fxn <- function(x) {
# By default run as if LogNormalize is done
log1pdata.mean.fxn <- function(x) {
return(log(x = rowMeans(x = expm1(x = x)) + pseudocount.use, base = base))
}
scaledata.mean.fxn <- rowMeans
counts.mean.fxn <- function(x) {
return(log(x = rowMeans(x = x) + pseudocount.use, base = base))
}
mean.fxn <- mean.fxn %||% switch(
EXPR = slot,
'data' = switch(
EXPR = norm.method %||% '',
'LogNormalize' = function(x) {
return(log(x = rowMeans(x = expm1(x = x)) + pseudocount.use, base = base))
},
default.mean.fxn
),
'scale.data' = rowMeans,
'counts' = function(x) {
return(log(x = rowMeans(x = x) + pseudocount.use, base = base))
},
default.mean.fxn
)
if (!is.null(x = norm.method)) {
# For anything apart from log normalization set to rowMeans
if (norm.method!="LogNormalize") {
new.mean.fxn <- counts.mean.fxn
} else {
new.mean.fxn <- counts.mean.fxn
if (slot == "data") {
new.mean.fxn <- log1pdata.mean.fxn
} else if (slot == "scale.data") {
new.mean.fxn <- scaledata.mean.fxn
}
}
} else {
# If no normalization method is passed use slots to decide mean function
new.mean.fxn <- switch(
EXPR = slot,
'data' = log1pdata.mean.fxn,
'scale.data' = scaledata.mean.fxn,
'counts' = counts.mean.fxn,
log1pdata.mean.fxn
)
}
mean.fxn <- mean.fxn %||% new.mean.fxn
# Omit the decimal value of e from the column name if base == exp(1)
base.text <- ifelse(
test = base == exp(1),
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test_differential_expression.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ test_that("FindAllMarkers works as expected", {

# CLR normalization
expect_equal(results.clr[1, "p_val"], 1.209462e-11)

expect_equal(results.clr[1, "avg_log2FC"], -1.079924, tolerance = 1e-6)
expect_equal(results.clr[1, "pct.1"], 0.083)
expect_equal(results.clr[1, "pct.2"], 0.909)
Expand Down

0 comments on commit 992c1a9

Please sign in to comment.