Skip to content

Commit

Permalink
simplify GLMPCA method definitions and set default assay to 'counts' …
Browse files Browse the repository at this point in the history
…addresses #5 . Moves the L argument in front of assay since it is a required positional argument by glmpca
  • Loading branch information
willtownes committed Aug 26, 2020
1 parent f97d9ab commit d1c8824
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 34 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ export(nullResiduals)
exportMethods(GLMPCA)
exportMethods(devianceFeatureSelection)
exportMethods(nullResiduals)
import(glmpca)
importFrom(Matrix,t)
importFrom(SingleCellExperiment,"reducedDim<-")
importFrom(SummarizedExperiment,"assay<-")
importFrom(SummarizedExperiment,"rowData<-")
importFrom(SummarizedExperiment,assay)
importFrom(SummarizedExperiment,rowData)
importFrom(glmpca,glmpca)
importFrom(methods,as)
importFrom(methods,is)
3 changes: 1 addition & 2 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' methods.
#' @export
setGeneric(
name = "devianceFeatureSelection", #signature = 'object',
name = "devianceFeatureSelection",
def = function(object, ...) {
standardGeneric("devianceFeatureSelection")
}
Expand All @@ -18,7 +18,6 @@ setGeneric(
#' @export
setGeneric(
name = "GLMPCA",
signature = 'object',
def = function(object, ...) {
standardGeneric("GLMPCA")
}
Expand Down
35 changes: 14 additions & 21 deletions R/glmpca.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#' \code{\link[glmpca]{glmpca}}.
#'
#' @param object A \code{\link{SingleCellExperiment}} or
#' \link{SummarizedExperiment} object. Alternatively, a matrix of integer
#' counts.
#' @param assay a character or integer specifying which assay to use for GLM-PCA
#' (default = 1). Ignored if \code{object} is a matrix.
#' \link{SummarizedExperiment} object. Alternatively, a matrix-like object
#' of non-negative integer counts (such as a sparse \code{\link{Matrix}}).
#' @param L the desired number of latent dimensions (integer).
#' @param assay a character or integer specifying which assay to use for GLM-PCA
#' (default = 'counts'). Ignored if \code{object} is a matrix.
#' @param ... further arguments passed to \code{\link[glmpca]{glmpca}}
#'
#' @return The original \code{SingleCellExperiment} or
Expand All @@ -26,35 +26,28 @@
#' sce <- SingleCellExperiment::SingleCellExperiment(assays=list(counts=u))
#' GLMPCA(sce, L = 2)
#'
#' @import glmpca
#' @importFrom SingleCellExperiment reducedDim<-
#' @importFrom methods is
#' @export
setMethod(f = "GLMPCA",
signature = signature(object = "SummarizedExperiment"),
definition = function(object, assay = 1, L, ...){
res <- glmpca(Y = assay(object, assay), L = L, ...)
setMethod(f = "GLMPCA","SummarizedExperiment",
definition = function(object, L, assay = "counts", ...){
res <- glmpca::glmpca(Y = assay(object, assay), L = L, ...)
if(is(object, 'SingleCellExperiment')){
reducedDim(object, "GLMPCA") <- res$factors
}
object@metadata$glmpca <- res
return(object)
})

#this function is only needed because the glmpca signature has "Y" not "object"
glmpca_wrapper<-function(object, L, ...){
glmpca::glmpca(object, L, ...)
}

#' @rdname GLMPCA
#' @importFrom glmpca glmpca
#' @export
setMethod(f = "GLMPCA",
signature = signature(object = "matrix"),
definition = function(object, assay = 1, L, ...){
glmpca(Y = object, L = L, ...)
})
setMethod(f = "GLMPCA", "matrix", definition=glmpca_wrapper)

#' @rdname GLMPCA
#' @importFrom glmpca glmpca
#' @export
setMethod(f = "GLMPCA",
signature = signature(object = "Matrix"),
definition = function(object, assay = 1, L, ...){
glmpca(Y = object, L = L, ...)
})
setMethod(f = "GLMPCA", "Matrix", definition=glmpca_wrapper)
16 changes: 8 additions & 8 deletions man/GLMPCA.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-glmpca.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
context("Test GLMPCA")
require(SingleCellExperiment)
set.seed(1234)

test_that("GLMPCA works", {
ncells <- 100
u <- matrix(rpois(2000, 5), ncol=ncells)
v <- log2(u + 1)

require(SingleCellExperiment)
se <- SummarizedExperiment(assays=list(counts=u, logcounts=v))
sce <- SingleCellExperiment(assays=list(counts=u, logcounts=v))

Expand Down

0 comments on commit d1c8824

Please sign in to comment.