Skip to content

Commit

Permalink
Allow contrast= to be a character vector for makeContrasts.
Browse files Browse the repository at this point in the history
Allow coef= to be omitted if contrast= is specified, closes #69.
Expose coldata= in the SummarizedExperiment method.
  • Loading branch information
LTLA committed Aug 7, 2020
1 parent c7a45a7 commit 9c448ed
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -218,6 +218,7 @@ importFrom(limma,contrastAsCoef)
importFrom(limma,contrasts.fit)
importFrom(limma,eBayes)
importFrom(limma,lmFit)
importFrom(limma,makeContrasts)
importFrom(limma,topTable)
importFrom(limma,treat)
importFrom(limma,voom)
Expand Down
21 changes: 17 additions & 4 deletions R/pseudoBulkDGE.R
Expand Up @@ -17,7 +17,8 @@
#' @param coef String or character vector containing the coefficients to drop from the design matrix to form the null hypothesis.
#' Can also be an integer scalar or vector specifying the indices of the relevant columns.
#' @param contrast Numeric vector or matrix containing the contrast of interest.
#' Takes precedence over \code{coef}.
#' Alternatively, a character vector to be passed to \code{\link{makeContrasts}} to create this numeric vector/matrix.
#' If specified, this takes precedence over \code{coef}.
#' @param lfc Numeric scalar specifying the log-fold change threshold to use in \code{\link{glmTreat}} or \code{\link{treat}}.
#' @param assay.type String or integer scalar specifying the assay to use from \code{x}.
#' @param include.intermediates Logical scalar indicating whether the intermediate \pkg{edgeR} objects should be returned.
Expand Down Expand Up @@ -166,6 +167,11 @@ NULL
label <- as.character(label)
method <- match.arg(method)

# Avoid requiring 'coef' if 'contrast' is specified.
if (!is.null(contrast)) {
coef <- NULL
}

for (i in sort(unique(label))) {
chosen <- i==label

Expand Down Expand Up @@ -260,6 +266,7 @@ NULL
#' @importFrom S4Vectors DataFrame metadata metadata<-
#' @importFrom edgeR estimateDisp glmQLFit glmQLFTest getOffset scaleOffset
#' calcNormFactors filterByExpr topTags glmLRT glmFit glmTreat
#' @importFrom limma makeContrasts
.pseudo_bulk_edgeR <- function(y, row.names, curdesign, curcond, coef, contrast,
lfc, null.lfc, include.intermediates, robust=TRUE)
{
Expand All @@ -272,6 +279,9 @@ NULL
if (rank == nrow(curdesign) || rank < ncol(curdesign)) {
return(NULL)
}
if (is.character(contrast)) {
contrast <- makeContrasts(contrasts=contrast, levels=curdesign)
}

lfc.out <- .compute_offsets_by_lfc(design=curdesign, coef=coef,
contrast=contrast, filtered=gkeep, null.lfc=null.lfc)
Expand Down Expand Up @@ -324,7 +334,7 @@ NULL
#' @importFrom S4Vectors DataFrame metadata metadata<-
#' @importFrom edgeR calcNormFactors filterByExpr
#' @importFrom limma voom voomWithQualityWeights lmFit
#' contrasts.fit eBayes treat topTable
#' contrasts.fit eBayes treat topTable makeContrasts
.pseudo_bulk_voom <- function(y, row.names, curdesign, curcond, coef, contrast,
lfc, null.lfc, include.intermediates, qualities=TRUE, robust=TRUE)
{
Expand All @@ -343,6 +353,9 @@ NULL
} else {
v <- voom(y, curdesign)
}
if (is.character(contrast)) {
contrast <- makeContrasts(contrasts=contrast, levels=curdesign)
}

lfc.out <- .compute_offsets_by_lfc(design=curdesign, coef=coef,
contrast=contrast, filtered=gkeep, null.lfc=null.lfc)
Expand Down Expand Up @@ -388,6 +401,6 @@ setMethod("pseudoBulkDGE", "ANY", .pseudo_bulk_master)
#' @export
#' @rdname pseudoBulkDGE
#' @importFrom SummarizedExperiment assay colData
setMethod("pseudoBulkDGE", "SummarizedExperiment", function(x, ..., assay.type=1) {
.pseudo_bulk_master(assay(x, assay.type), col.data=colData(x), ...)
setMethod("pseudoBulkDGE", "SummarizedExperiment", function(x, col.data=colData(x), ..., assay.type=1) {
.pseudo_bulk_master(assay(x, assay.type), col.data=col.data, ...)
})
5 changes: 3 additions & 2 deletions man/pseudoBulkDGE.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 56 additions & 1 deletion tests/testthat/test-pseudo-dge.R
Expand Up @@ -197,7 +197,62 @@ test_that("pseudoBulkDGE works with all the limma settings", {
expect_false(identical(ref, out))
})

test_that("pseudoBulkDGE works with various odds and sods", {
test_that("contrast specification in the pseudoBulkDGE works", {
ref <- pseudoBulkDGE(pseudo[1:10,],
label=pseudo$cluster,
design=~DRUG,
coef=2
)

out1 <- pseudoBulkDGE(pseudo[1:10,],
label=pseudo$cluster,
design=~DRUG,
contrast=c(0, 1)
)

out2 <- pseudoBulkDGE(pseudo[1:10,],
label=pseudo$cluster,
design=~0 + DRUG,
contrast=c(-1, 1)
)

out3 <- pseudoBulkDGE(pseudo[1:10,],
label=pseudo$cluster,
design=~0 + DRUG,
contrast="DRUG2 - DRUG1"
)

for (i in seq_along(ref)) {
expect_equal(ref[[i]]$logFC, out1[[i]]$logFC)
expect_equal(ref[[i]]$logFC, out2[[i]]$logFC)
expect_equal(ref[[i]]$logFC, out3[[i]]$logFC)
expect_equal(ref[[i]]$PValue, out1[[i]]$PValue)
expect_equal(ref[[i]]$PValue, out2[[i]]$PValue)
expect_equal(ref[[i]]$PValue, out3[[i]]$PValue)
}

# Voom is a bit different due to the approximation of the weights.
ref <- pseudoBulkDGE(pseudo[1:10,],
label=pseudo$cluster,
design=~0 + DRUG,
method="voom",
contrast=c(-1, 1)
)

out <- pseudoBulkDGE(pseudo[1:10,],
label=pseudo$cluster,
design=~0 + DRUG,
method="voom",
contrast="DRUG2 - DRUG1"
)

for (i in seq_along(ref)) {
expect_equal(ref[[i]]$logFC, out[[i]]$logFC)
expect_equal(ref[[i]]$PValue, out[[i]]$PValue)
}
})

test_that("sorting in the pseudoBulkDGE works", {
ref <- pseudoBulkDGE(pseudo,
label=pseudo$cluster,
design=~DRUG,
Expand Down

0 comments on commit 9c448ed

Please sign in to comment.