diff --git a/DESCRIPTION b/DESCRIPTION index d386e22..366ce8a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -43,7 +43,7 @@ VignetteBuilder: knitr LazyData: false URL: https://bioconductor.org/packages/scry.html BugReports: https://github.com/kstreet13/scry/issues -RoxygenNote: 7.2.2 +RoxygenNote: 7.3.1 Encoding: UTF-8 biocViews: DimensionReduction, diff --git a/NAMESPACE b/NAMESPACE index bfd7b21..e43c64a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,6 +11,7 @@ importClassesFrom(BiocSingular,LowRankMatrix) importClassesFrom(DelayedArray,DelayedMatrix) importFrom(DelayedArray,DelayedArray) importFrom(DelayedArray,colSums) +importFrom(Matrix,Diagonal) importFrom(Matrix,colSums) importFrom(Matrix,rowSums) importFrom(Matrix,t) diff --git a/R/featureSelection.R b/R/featureSelection.R index 06f8b8a..5929562 100644 --- a/R/featureSelection.R +++ b/R/featureSelection.R @@ -2,14 +2,15 @@ #...or SummarizedExperiment #' @importFrom methods as +#' @importFrom Matrix Diagonal sparseBinomialDeviance<-function(X,sz){ #X has features in cols, observations in rows #assume X is a sparseMatrix object X<-as(X,"CsparseMatrix") - LP<-L1P<-X/sz #recycling + LP<-L1P<-Matrix::Diagonal(x = 1/sz) %*% X #recycling LP@x<-log(LP@x) #log transform nonzero elements only L1P@x<-log1p(-L1P@x) #rare case: -Inf if only a single gene nonzero in a cell - ll_sat<-Matrix::colSums(X*(LP-L1P)+sz*L1P, na.rm=TRUE) + ll_sat<-Matrix::colSums(X*(LP-L1P)+Matrix::Diagonal(x = sz) %*% L1P, na.rm=TRUE) sz_sum<-sum(sz) feature_sums<-Matrix::colSums(X) p<-feature_sums/sz_sum @@ -35,7 +36,7 @@ denseBinomialDeviance<-function(X,sz){ sparsePoissonDeviance<-function(X,sz){ #X has features in cols, observations in rows X<-as(X,"CsparseMatrix") - LP<-X/sz #recycling + LP<-Matrix::Diagonal(x = 1/sz) %*% X #recycling LP@x<-log(LP@x) #log transform nonzero elements only ll_sat<-Matrix::colSums(X*LP, na.rm=TRUE) feature_sums<-Matrix::colSums(X) @@ -88,7 +89,7 @@ densePoissonDeviance<-function(X,sz){ fam=c("binomial","poisson"), batch=NULL){ #deviance but with batch indicator (batch=a factor) - m<-object; rm(object) + m<-object; rm(object) fam<-match.arg(fam) stopifnot(is.null(batch) || is(batch,"factor")) if(is.null(batch)){ @@ -113,14 +114,14 @@ densePoissonDeviance<-function(X,sz){ #' feature has a constant rate. Features with large deviance are likely to be #' informative. Uninformative, low deviance features can be discarded to speed #' up downstream analyses and reduce memory footprint. -#' +#' #' @param object an object inheriting from \code{\link{SummarizedExperiment}} #' (such as #' \code{\link{SingleCellExperiment}}). Alternatively, a matrix or matrix-like -#' object (such as a sparse \code{\link{Matrix}}) of non-negative integer +#' object (such as a sparse \code{\link{Matrix}}) of non-negative integer #' counts. #' @param assay a string or integer specifying which assay contains the count -#' data (default = 'counts'). Ignored if \code{object} is a matrix-like +#' data (default = 'counts'). Ignored if \code{object} is a matrix-like #' object. #' @param fam a string specifying the model type to be used for calculating the #' residuals. Binomial (the default) is the closest approximation to @@ -135,11 +136,11 @@ densePoissonDeviance<-function(X,sz){ #' @param sorted logical, should the \code{object} be returned with rows sorted #' in decreasing order of deviance? Default: FALSE, unless nkeep is specified, #' in which case it is forced to be TRUE. Ignored for matrix-like inputs. -#' +#' #' @return The original \code{SingleCellExperiment} or #' \code{SummarizedExperiment} object with the deviance statistics for each #' feature appended to the rowData. The new column name will be either -#' binomial_deviance or poisson_deviance. If the input was a matrix-like +#' binomial_deviance or poisson_deviance. If the input was a matrix-like #' object, output is a numeric vector containing the deviance statistics for #' each row. #' @@ -167,8 +168,8 @@ densePoissonDeviance<-function(X,sz){ #' @importFrom SummarizedExperiment rowData<- #' @export setMethod("devianceFeatureSelection", "SummarizedExperiment", - definition = function(object, assay = "counts", - fam = c("binomial", "poisson"), batch = NULL, + definition = function(object, assay = "counts", + fam = c("binomial", "poisson"), batch = NULL, nkeep = NULL, sorted = FALSE){ fam<-match.arg(fam) m <- assay(object, assay) @@ -197,7 +198,7 @@ setMethod("devianceFeatureSelection", "SummarizedExperiment", #' @rdname devianceFeatureSelection #' @export -setMethod("devianceFeatureSelection", "matrix", +setMethod("devianceFeatureSelection", "matrix", definition=.compute_deviance_batch) #' @rdname devianceFeatureSelection diff --git a/man/devianceFeatureSelection.Rd b/man/devianceFeatureSelection.Rd index cb57dfc..174d748 100644 --- a/man/devianceFeatureSelection.Rd +++ b/man/devianceFeatureSelection.Rd @@ -29,14 +29,14 @@ devianceFeatureSelection(object, ...) \item{object}{an object inheriting from \code{\link{SummarizedExperiment}} (such as \code{\link{SingleCellExperiment}}). Alternatively, a matrix or matrix-like -object (such as a sparse \code{\link{Matrix}}) of non-negative integer +object (such as a sparse \code{\link{Matrix}}) of non-negative integer counts.} \item{...}{for the generic, additional arguments to pass to object-specific methods.} \item{assay}{a string or integer specifying which assay contains the count -data (default = 'counts'). Ignored if \code{object} is a matrix-like +data (default = 'counts'). Ignored if \code{object} is a matrix-like object.} \item{fam}{a string specifying the model type to be used for calculating the @@ -60,7 +60,7 @@ in which case it is forced to be TRUE. Ignored for matrix-like inputs.} The original \code{SingleCellExperiment} or \code{SummarizedExperiment} object with the deviance statistics for each feature appended to the rowData. The new column name will be either - binomial_deviance or poisson_deviance. If the input was a matrix-like + binomial_deviance or poisson_deviance. If the input was a matrix-like object, output is a numeric vector containing the deviance statistics for each row. }