From d6d7dba68630c30003c1cf9891bed8628552811d Mon Sep 17 00:00:00 2001
From: Luke Zappia <lazappi@users.noreply.github.com>
Date: Fri, 11 Oct 2019 14:48:08 +1100
Subject: [PATCH] Check for counts assay when estimating from SCE

Fixes #82
---
 DESCRIPTION                          |  2 +-
 NEWS.md                              |  6 +++++-
 R/BASiCS-estimate.R                  |  2 +-
 R/SCE-functions.R                    | 22 ++++++++++++++++++++++
 R/kersplat-estimate.R                |  2 +-
 R/lun-estimate.R                     |  2 +-
 R/lun2-estimate.R                    |  2 +-
 R/mfa-estimate.R                     |  2 +-
 R/pheno-estimate.R                   |  2 +-
 R/simple-estimate.R                  |  2 +-
 R/sparseDC-estimate.R                |  2 +-
 R/splat-estimate.R                   |  2 +-
 R/zinb-estimate.R                    |  2 +-
 man/getCounts.Rd                     | 18 ++++++++++++++++++
 tests/testthat/test-simpleEstimate.R | 15 +++++++++++++++
 15 files changed, 71 insertions(+), 12 deletions(-)
 create mode 100644 man/getCounts.Rd

diff --git a/DESCRIPTION b/DESCRIPTION
index 795ecc7..05b3a97 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,7 +1,7 @@
 Package: splatter
 Type: Package
 Title: Simple Simulation of Single-cell RNA Sequencing Data
-Version: 1.9.7.9019
+Version: 1.9.8
 Date: 2019-10-11
 Authors@R:
     c(person("Luke", "Zappia", role = c("aut", "cre"),
diff --git a/NEWS.md b/NEWS.md
index 90be82e..0b9e316 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,6 +1,10 @@
 # DEVELOPMENT VERSION
 
-## Version 1.9.8
+## Version 1.9.8 (2019-10-11)
+
+* Add Kersplat simulation! Still experimental but is useable.
+* Check for counts assay when estimating from SingleCellExperiment objects
+  (Fixes #82)
 
 ### Version 1.9.7.9019 (2019-10-11)
 
diff --git a/R/BASiCS-estimate.R b/R/BASiCS-estimate.R
index 229cddf..4375d64 100644
--- a/R/BASiCS-estimate.R
+++ b/R/BASiCS-estimate.R
@@ -62,7 +62,7 @@ BASiCSEstimate.SingleCellExperiment <- function(counts, spike.info = NULL,
                                                 params = newBASiCSParams(),
                                                 verbose = TRUE, progress = TRUE,
                                                 ...) {
-    counts <- BiocGenerics::counts(counts)
+    counts <- getCounts(counts)
     BASiCSEstimate(counts, spike.info, batch, n, thin, burn, regression,
                    params, verbose, progress, ...)
 }
diff --git a/R/SCE-functions.R b/R/SCE-functions.R
index 5e4a874..609345b 100644
--- a/R/SCE-functions.R
+++ b/R/SCE-functions.R
@@ -142,3 +142,25 @@ addGeneLengths <- function(sce, method = c("generate", "sample"), loc = 7.9,
 
     return(sce)
 }
+
+#' Get counts
+#'
+#' Get counts matrix from a SingleCellExperiment object. If counts is missing
+#' a warning is issued and the first assay is returned.
+#'
+#' @param sce
+#'
+#' @return Counts matrix
+getCounts <- function(sce) {
+
+    checkmate::assertClass(sce, "SingleCellExperiment")
+
+    if ("counts" %in% SummarizedExperiment::assayNames(sce)) {
+        counts <- SingleCellExperiment::counts(sce)
+    } else {
+        warning("counts assay is missing, using the first assay instead")
+        counts <- SummarizedExperiment::assay(sce)
+    }
+
+    return(counts)
+}
diff --git a/R/kersplat-estimate.R b/R/kersplat-estimate.R
index 2bc5b81..9eaa4c0 100644
--- a/R/kersplat-estimate.R
+++ b/R/kersplat-estimate.R
@@ -34,7 +34,7 @@ kersplatEstimate <- function(counts, params = newKersplatParams(),
 kersplatEstimate.SingleCellExperiment <- function(counts,
                                                  params = newKersplatParams(),
                                                  verbose = TRUE) {
-    counts <- BiocGenerics::counts(counts)
+    counts <- getCounts(counts)
     kersplatEstimate(counts, params, verbose)
 }
 
diff --git a/R/lun-estimate.R b/R/lun-estimate.R
index a014402..140019b 100644
--- a/R/lun-estimate.R
+++ b/R/lun-estimate.R
@@ -29,7 +29,7 @@ lunEstimate <- function(counts, params = newLunParams()) {
 #' @rdname lunEstimate
 #' @export
 lunEstimate.SingleCellExperiment <- function(counts, params = newLunParams()) {
-    counts <- BiocGenerics::counts(counts)
+    counts <- getCounts(counts)
     lunEstimate(counts, params)
 }
 
diff --git a/R/lun2-estimate.R b/R/lun2-estimate.R
index 0bcc212..f9332d9 100644
--- a/R/lun2-estimate.R
+++ b/R/lun2-estimate.R
@@ -43,7 +43,7 @@ lun2Estimate.SingleCellExperiment <- function(counts, plates,
                                               params = newLun2Params(),
                                               min.size = 200, verbose = TRUE,
                                               BPPARAM = SerialParam()) {
-    counts <- BiocGenerics::counts(counts)
+    counts <- getCounts(counts)
     lun2Estimate(counts, plates, params, min.size = min.size, verbose = verbose,
                  BPPARAM = BPPARAM)
 }
diff --git a/R/mfa-estimate.R b/R/mfa-estimate.R
index c05f174..dcff5ae 100644
--- a/R/mfa-estimate.R
+++ b/R/mfa-estimate.R
@@ -33,7 +33,7 @@ mfaEstimate <- function(counts, params = newMFAParams()) {
 #' @export
 mfaEstimate.SingleCellExperiment <- function(counts,
                                              params = newMFAParams()) {
-    counts <- BiocGenerics::counts(counts)
+    counts <- getCounts(counts)
     mfaEstimate(counts, params)
 }
 
diff --git a/R/pheno-estimate.R b/R/pheno-estimate.R
index 76b95e6..47c92d7 100644
--- a/R/pheno-estimate.R
+++ b/R/pheno-estimate.R
@@ -33,7 +33,7 @@ phenoEstimate <- function(counts, params = newPhenoParams()) {
 #' @export
 phenoEstimate.SingleCellExperiment <- function(counts,
                                                params = newPhenoParams()) {
-    counts <- BiocGenerics::counts(counts)
+    counts <- getCounts(counts)
     phenoEstimate(counts, params)
 }
 
diff --git a/R/simple-estimate.R b/R/simple-estimate.R
index 5bb2fb3..c5e208f 100644
--- a/R/simple-estimate.R
+++ b/R/simple-estimate.R
@@ -32,7 +32,7 @@ simpleEstimate <- function(counts, params = newSimpleParams()) {
 #' @export
 simpleEstimate.SingleCellExperiment <- function(counts,
                                                 params = newSimpleParams()) {
-    counts <- BiocGenerics::counts(counts)
+    counts <- getCounts(counts)
     simpleEstimate(counts, params)
 }
 
diff --git a/R/sparseDC-estimate.R b/R/sparseDC-estimate.R
index a0784be..f284799 100644
--- a/R/sparseDC-estimate.R
+++ b/R/sparseDC-estimate.R
@@ -46,7 +46,7 @@ sparseDCEstimate <- function(counts, conditions, nclusters, norm = TRUE,
 sparseDCEstimate.SingleCellExperiment <- function(counts, conditions, nclusters,
                                                   norm = TRUE,
                                                   params = newSparseDCParams()) {
-    counts <- BiocGenerics::counts(counts)
+    counts <- getCounts(counts)
     sparseDCEstimate(counts, conditions, nclusters, norm, params)
 }
 
diff --git a/R/splat-estimate.R b/R/splat-estimate.R
index 459606d..179f7dc 100644
--- a/R/splat-estimate.R
+++ b/R/splat-estimate.R
@@ -32,7 +32,7 @@ splatEstimate <- function(counts, params = newSplatParams()) {
 #' @export
 splatEstimate.SingleCellExperiment <- function(counts,
                                                params = newSplatParams()) {
-    counts <- BiocGenerics::counts(counts)
+    counts <- getCounts(counts)
     splatEstimate(counts, params)
 }
 
diff --git a/R/zinb-estimate.R b/R/zinb-estimate.R
index 38d3acb..b978fbd 100644
--- a/R/zinb-estimate.R
+++ b/R/zinb-estimate.R
@@ -56,7 +56,7 @@ zinbEstimate.SingleCellExperiment <- function(counts, design.samples = NULL,
                                               params = newZINBParams(),
                                               verbose = TRUE,
                                               BPPARAM = SerialParam(), ...) {
-    counts <- BiocGenerics::counts(counts)
+    counts <- getCounts(counts)
     zinbEstimate(counts, design.samples, design.genes, common.disp,
                  iter.init, iter.opt, stop.opt, params, verbose, BPPARAM, ...)
 }
diff --git a/man/getCounts.Rd b/man/getCounts.Rd
new file mode 100644
index 0000000..4117bbd
--- /dev/null
+++ b/man/getCounts.Rd
@@ -0,0 +1,18 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/SCE-functions.R
+\name{getCounts}
+\alias{getCounts}
+\title{Get counts}
+\usage{
+getCounts(sce)
+}
+\arguments{
+\item{sce}{}
+}
+\value{
+Counts matrix
+}
+\description{
+Get counts matrix from a SingleCellExperiment object. If counts is missing
+a warning is issued and the first assay is returned.
+}
diff --git a/tests/testthat/test-simpleEstimate.R b/tests/testthat/test-simpleEstimate.R
index 1d6506c..1a4df18 100644
--- a/tests/testthat/test-simpleEstimate.R
+++ b/tests/testthat/test-simpleEstimate.R
@@ -8,3 +8,18 @@ test_that("simpleEstimate works", {
     params <- simpleEstimate(counts)
     expect_true(validObject(params))
 })
+
+test_that("simpleEstimate works with SingleCellExperiment", {
+    sce <- SingleCellExperiment::SingleCellExperiment(
+        assays = list(counts = counts)
+    )
+    params <- simpleEstimate(sce)
+    expect_true(validObject(params))
+})
+
+test_that("simpleEstimate works with SingleCellExperiment without counts", {
+    sce <- SingleCellExperiment::SingleCellExperiment(
+        assays = list(TEST = counts)
+    )
+    expect_warning(simpleEstimate(sce), "counts assay is missing")
+})
-- 
GitLab