From cfbad4d54e82497a60ff9c4b940a804b2456eb38 Mon Sep 17 00:00:00 2001 From: Luke Zappia <lazappi@users.noreply.github.com> Date: Fri, 29 Sep 2017 17:34:11 +1000 Subject: [PATCH] Update scDDEstimate --- NAMESPACE | 1 + R/scDD-estimate.R | 76 +++++++++++++++++++++++++-------------------- man/scDDEstimate.Rd | 26 ++++++++++------ 3 files changed, 59 insertions(+), 44 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 216436b..16a874d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ S3method(lun2Estimate,matrix) S3method(lunEstimate,SingleCellExperiment) S3method(lunEstimate,matrix) S3method(scDDEstimate,SingleCellExperiment) +S3method(scDDEstimate,default) S3method(scDDEstimate,matrix) S3method(simpleEstimate,SingleCellExperiment) S3method(simpleEstimate,matrix) diff --git a/R/scDD-estimate.R b/R/scDD-estimate.R index 7253c61..1ae1521 100644 --- a/R/scDD-estimate.R +++ b/R/scDD-estimate.R @@ -6,6 +6,8 @@ #' containing count data to estimate parameters from. #' @param conditions Vector giving the condition that each cell belongs to. #' Conditions can be 1 or 2. +#' @param condition String giving the column that represents biological group of +#' interest. #' @param params SCDDParams object to store estimated values in. #' @param verbose logical. Whether to show progress messages. #' @param BPPARAM A \code{\link[BiocParallel]{BiocParallelParam}} instance @@ -29,64 +31,70 @@ #' } #' @importFrom BiocParallel SerialParam #' @export -scDDEstimate <- function(counts, conditions, params = newSCDDParams(), - verbose = TRUE, BPPARAM = SerialParam()) { +scDDEstimate <- function(counts, params = newSCDDParams(), + verbose = TRUE, BPPARAM = SerialParam(), ...) { + + if (!requireNamespace("scDD", quietly = TRUE)) { + stop("The scDD simulation requires the 'scDD' package.") + } + UseMethod("scDDEstimate") } +#' @rdname scDDEstimate +#' @importFrom SingleCellExperiment SingleCellExperiment +#' @export +scDDEstimate.matrix <- function(counts, conditions, params = newSCDDParams(), + verbose = TRUE, BPPARAM = SerialParam(), ...) { + + checkmate::assertMatrix(counts, mode = "numeric", any.missing = FALSE, + min.rows = 1, min.cols = 1, row.names = "unique", + col.names = "unique") + checkmate::assertIntegerish(conditions, len = ncol(counts), lower = 1, + upper = 2) + + counts <- SingleCellExperiment(assays = list(counts = counts), + colData = data.frame(condition = conditions)) + scDDEstimate.default(counts, "condition", params, verbose, BPPARAM) +} + #' @rdname scDDEstimate #' @export -scDDEstimate.SingleCellExperiment <- function(counts, conditions, +scDDEstimate.SingleCellExperiment <- function(counts, condition = "condition", params = newSCDDParams(), verbose = TRUE, - BPPARAM = SerialParam()) { - counts <- BiocGenerics::counts(counts) - scDDEstimate(counts, conditions, params, verbose, BPPARAM) + BPPARAM = SerialParam(), ...) { + scDDEstimate.default(counts, condition, params, verbose, BPPARAM) } #' @rdname scDDEstimate #' @importFrom methods as #' @export -scDDEstimate.matrix <- function(counts, conditions, params = newSCDDParams(), - verbose = TRUE, BPPARAM = SerialParam()) { - - if (!requireNamespace("scDD", quietly = TRUE)) { - stop("The scDD simulation requires the 'scDD' package.") - } +scDDEstimate.default <- function(counts, condition, params, verbose, BPPARAM) { checkmate::assertClass(params, "SCDDParams") - checkmate::assertMatrix(counts, mode = "numeric", any.missing = FALSE, - min.rows = 1, min.cols = 1, row.names = "unique", - col.names = "unique") - checkmate::assertIntegerish(conditions, len = ncol(counts), lower = 1, - upper = 2) - - counts.list <- list(Cond1 = counts[, conditions == 1], - Cond2 = counts[, conditions == 2]) + checkmate::assertClass(counts, "SingleCellExperiment") + checkmate::assertCharacter(condition, min.chars = 1, any.missing = FALSE, + len = 1) + if (!(condition %in% colnames(SummarizedExperiment::colData(counts)))) { + stop("'condition' must be the name of a column in `colData(counts)`") + } if (verbose) { - processed <- scDD::preprocess(counts.list, c("Cond1", "Cond2"), - median_norm = TRUE) + processed <- scDD::preprocess(counts, condition, median_norm = TRUE) } else { suppressMessages( - processed <- scDD::preprocess(counts.list, c("Cond1", "Cond2"), - median_norm = TRUE) + processed <- scDD::preprocess(counts, condition, median_norm = TRUE) ) } - assays <- S4Vectors::SimpleList(NormCounts = processed) - - colData <- S4Vectors::DataFrame(condition = conditions, - row.names = colnames(processed)) - - SCdat <- SummarizedExperiment::SummarizedExperiment(assays = assays, - colData = colData) - if (verbose) { - SCdat <- scDD::scDD(SCdat, testZeroes = FALSE, param = BPPARAM) + SCdat <- scDD::scDD(processed, testZeroes = FALSE, param = BPPARAM, + condition = condition) } else { dummy <- utils::capture.output(suppressMessages( - SCdat <- scDD::scDD(SCdat, testZeroes = FALSE, param = BPPARAM) + SCdat <- scDD::scDD(processed, testZeroes = FALSE, param = BPPARAM, + condition = condition) )) } diff --git a/man/scDDEstimate.Rd b/man/scDDEstimate.Rd index 6eb052e..ca73df5 100644 --- a/man/scDDEstimate.Rd +++ b/man/scDDEstimate.Rd @@ -2,26 +2,26 @@ % Please edit documentation in R/scDD-estimate.R \name{scDDEstimate} \alias{scDDEstimate} -\alias{scDDEstimate.SingleCellExperiment} \alias{scDDEstimate.matrix} +\alias{scDDEstimate.SingleCellExperiment} +\alias{scDDEstimate.default} \title{Estimate scDD simulation parameters} \usage{ -scDDEstimate(counts, conditions, params = newSCDDParams(), verbose = TRUE, - BPPARAM = SerialParam()) - -\method{scDDEstimate}{SingleCellExperiment}(counts, conditions, - params = newSCDDParams(), verbose = TRUE, BPPARAM = SerialParam()) +scDDEstimate(counts, params = newSCDDParams(), verbose = TRUE, + BPPARAM = SerialParam(), ...) \method{scDDEstimate}{matrix}(counts, conditions, params = newSCDDParams(), - verbose = TRUE, BPPARAM = SerialParam()) + verbose = TRUE, BPPARAM = SerialParam(), ...) + +\method{scDDEstimate}{SingleCellExperiment}(counts, condition = "condition", + params = newSCDDParams(), verbose = TRUE, BPPARAM = SerialParam(), ...) + +\method{scDDEstimate}{default}(counts, condition, params, verbose, BPPARAM) } \arguments{ \item{counts}{either a counts matrix or a SingleCellExperiment object containing count data to estimate parameters from.} -\item{conditions}{Vector giving the condition that each cell belongs to. -Conditions can be 1 or 2.} - \item{params}{SCDDParams object to store estimated values in.} \item{verbose}{logical. Whether to show progress messages.} @@ -29,6 +29,12 @@ Conditions can be 1 or 2.} \item{BPPARAM}{A \code{\link[BiocParallel]{BiocParallelParam}} instance giving the parallel back-end to be used. Default is \code{\link[BiocParallel]{SerialParam}} which uses a single core.} + +\item{conditions}{Vector giving the condition that each cell belongs to. +Conditions can be 1 or 2.} + +\item{condition}{String giving the column that represents biological group of +interest.} } \value{ SCDDParams object containing the estimated parameters. -- GitLab