Skip to content

Commit

Permalink
Merge pull request #21 from eweine/fix_sparse_dense_conversion
Browse files Browse the repository at this point in the history
fixed sparse to dense conversion caused by matrix division
  • Loading branch information
willtownes authored Apr 13, 2024
2 parents f6bc5f7 + 4d8d8aa commit 191dfe4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 13 additions & 12 deletions R/featureSelection.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)){
Expand All @@ -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
Expand All @@ -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.
#'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -197,7 +198,7 @@ setMethod("devianceFeatureSelection", "SummarizedExperiment",

#' @rdname devianceFeatureSelection
#' @export
setMethod("devianceFeatureSelection", "matrix",
setMethod("devianceFeatureSelection", "matrix",
definition=.compute_deviance_batch)

#' @rdname devianceFeatureSelection
Expand Down
6 changes: 3 additions & 3 deletions man/devianceFeatureSelection.Rd

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

0 comments on commit 191dfe4

Please sign in to comment.