Skip to content

Commit

Permalink
Merge pull request #66 from UCLouvain-CBIO/issue55
Browse files Browse the repository at this point in the history
Issue55
  • Loading branch information
cvanderaa authored Jul 31, 2024
2 parents 63331d3 + 1d4a132 commit 25efd95
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## scp 1.15.2

- fix: fixed x-axis direction annotation for volcano plot on contrast
- fix: solved bug in DA when missing contrast level in modelled
feature (issue #65).

Expand Down
30 changes: 26 additions & 4 deletions R/ScpModel-DifferentialAnalysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -601,11 +601,33 @@ scpVolcanoPlot <- function(differentialList,
ylab("-log10(Adjusted p-value)") +
theme_minimal()
if (!is.null(contrast)) {
pl <- pl + xlab(paste(
contrast[[3]], " <- log2(Fold change) -> ",
contrast[[2]]
))
pl <- pl + .annotateDirection(df$Estimate, contrast)
}
pl
}

## Internal function that generates an intuitively annotates the log
## fold change axis. This is performed by highlighting the direction
## of change for the two groups in the contrast. Note however that the
## annotation is adapted in case that the changes are unidirectional.
## Then, only the contrast group where intensities are estimated to be
## higher is shown.
##
## The function returns an object of class "labels" that can be used
## as the x axis for a ggplot fig.
##
## @param logFoldChange A numeric() containing estimated log fold
## changes.
## @param contrast A `character(3)` with the following elements: 1.
## The name of a categorical variable to test; 2. The name of the
## reference group: 3. The name of the second group to contrast
## against the reference group. `coefficients` and `contrasts`
## cannot be both NULL.
.annotateDirection <- function(logFoldChange, contrast) {
xAnnotation <- "log2(Fold change)"
if (any(logFoldChange > 0))
xAnnotation <- paste(xAnnotation, " ->", contrast[[3]])
if (any(logFoldChange < 0))
xAnnotation <- paste(contrast[[2]], "<- ", xAnnotation)
xlab(xAnnotation)
}
2 changes: 1 addition & 1 deletion R/ScpModel-Workflow.R
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ scpModelWorkflow <- function(object, formula,
##
.fitModel <- function(y, x, effectNames) {
model <- ScpModelFit(nrow(x), ncol(x))
if (!ncol(x)) return(model)
if (!ncol(x) || !nrow(x) || nrow(x) < ncol(x)) return(model)
res <- .fitRidge(y[rownames(x)], x)
scpModelFitCoefficients(model) <- res$coefficients
scpModelFitResiduals(model) <- res$residuals
Expand Down
31 changes: 30 additions & 1 deletion tests/testthat/test-ScpModel-DifferentialAnalysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ test_that("scpVolcanoPlot", {
paste0("col", 1:100)))
))
se$condition <- as.factor(rep(1:2, length.out = ncol(se)))
assay(se)[, se$condition == 2] <- rnorm(nrow(se)) + assay(se)[, se$condition == 2]
assay(se)[, se$condition == 2] <- 1:nrow(se) - nrow(se) / 2 + assay(se)[, se$condition == 2]
set.seed(124)
assay(se) <- assay(se) + rnorm(length(assay(se)))
assay(se)[sample(1:length(assay(se)), length(assay(se))/2)] <- NA
Expand Down Expand Up @@ -920,3 +920,32 @@ test_that(".plotVolcano", {
textBy = "names", contrast = c("condition", "A", "B"))
)
})

test_that(".annotateDirection", {
## Annotate axis with 2 directions, that is logFoldChange contains
## positive and negative values
expect_identical(
.annotateDirection(-10:10, c("Group", "foo", "bar")),
xlab("foo <- log2(Fold change) -> bar")
)
## Reverse direction
expect_identical(
.annotateDirection(-10:10, c("Group", "bar", "foo")),
xlab("bar <- log2(Fold change) -> foo")
)
## Only positive direction = right annotation
expect_identical(
.annotateDirection(10:0, c("Group", "foo", "bar")),
xlab("log2(Fold change) -> bar")
)
## Only negative direction = left annotation
expect_identical(
.annotateDirection(-10:0, c("Group", "foo", "bar")),
xlab("foo <- log2(Fold change)")
)
## Only zero = no direction annotation
expect_identical(
.annotateDirection(0, c("Group", "foo", "bar")),
xlab("log2(Fold change)")
)
})

0 comments on commit 25efd95

Please sign in to comment.