From 07b6db6633f5065ab7549478a5b684f20e7b56b4 Mon Sep 17 00:00:00 2001
From: Luke Zappia <lazappi@users.noreply.github.com>
Date: Wed, 4 Oct 2017 20:30:14 +1100
Subject: [PATCH] Add zinbSimulate

---
 NAMESPACE                           |  1 +
 R/zinb-simulate.R                   | 74 +++++++++++++++++++++++++++++
 man/ZINBParams.Rd                   |  3 +-
 man/zinbSimulate.Rd                 | 46 ++++++++++++++++++
 tests/testthat/test-zinb-simulate.R |  5 ++
 5 files changed, 128 insertions(+), 1 deletion(-)
 create mode 100644 R/zinb-simulate.R
 create mode 100644 man/zinbSimulate.Rd
 create mode 100644 tests/testthat/test-zinb-simulate.R

diff --git a/NAMESPACE b/NAMESPACE
index a45acec..7ecb378 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -52,6 +52,7 @@ export(splatSimulateGroups)
 export(splatSimulatePaths)
 export(splatSimulateSingle)
 export(summariseDiff)
+export(zinbSimulate)
 exportClasses(Lun2Params)
 exportClasses(LunParams)
 exportClasses(MFAParams)
diff --git a/R/zinb-simulate.R b/R/zinb-simulate.R
new file mode 100644
index 0000000..92e93c5
--- /dev/null
+++ b/R/zinb-simulate.R
@@ -0,0 +1,74 @@
+#' ZINB-WaVE simulation
+#'
+#' Simulate counts using the ZINB-WaVE method.
+#'
+#' @param params ZINBParams object containing simulation parameters.
+#' @param verbose logical. Whether to print progress messages
+#' @param ... any additional parameter settings to override what is provided in
+#'        \code{params}.
+#'
+#' @details
+#' This function is just a wrapper around \code{\link[zinbwave]{zinbSim}} that
+#' takes a \code{\link{ZINBParams}}, runs the simulation then converts the
+#' output to a \code{\link[SingleCellExperiment]{SingleCellExperiment}} object.
+#' See \code{\link[zinbwave]{zinbSim}} and the ZINB-WaVE paper for
+#' more details about how the simulation works.
+#'
+#' @return SingleCellExperiment containing simulated counts
+#'
+#' @references
+#' Campbell K, Yau C. Uncovering genomic trajectories with heterogeneous genetic
+#' and environmental backgrounds across single-cells and populations. bioRxiv
+#' (2017).
+#'
+#' Risso D, Perraudeau F, Gribkova S, Dudoit S, Vert J-P. ZINB-WaVE: A general
+#' and flexible method for signal extraction from single-cell RNA-seq data
+#' bioRxiv (2017).
+#'
+#' Paper: \url{10.1101/125112}
+#'
+#' Code: \url{https://github.com/drisso/zinbwave}
+#'
+#' @examples
+#' sim <- zinbSimulate()
+#'
+#' @export
+#' @importFrom SingleCellExperiment SingleCellExperiment
+zinbSimulate <- function(params = newZINBParams(), verbose = TRUE, ...) {
+
+    checkmate::assertClass(params, "ZINBParams")
+    params <- setParams(params, ...)
+
+    # Get the parameters we are going to use
+    nCells <- getParam(params, "nCells")
+    nGenes <- getParam(params, "nGenes")
+    model <- getParam(params, "model")
+    seed <- getParam(params, "seed")
+
+    if (verbose) {message("Simulating counts...")}
+    zinb.sim <- zinbwave::zinbSim(model, seed)
+
+    if (verbose) {message("Creating final dataset...")}
+    cell.names <- paste0("Cell", seq_len(nCells))
+    gene.names <- paste0("Gene", seq_len(nGenes))
+
+    for (item in c("counts", "dataNB", "dataDropouts")) {
+        rownames(zinb.sim[[item]]) <- gene.names
+        colnames(zinb.sim[[item]]) <- cell.names
+    }
+
+    cells <- data.frame(Cell = cell.names)
+    rownames(cells) <- cell.names
+
+    features <- data.frame(Gene = gene.names)
+    rownames(features) <- gene.names
+
+    sim <- SingleCellExperiment(assays = list(counts = zinb.sim$counts,
+                                              TrueCounts = zinb.sim$dataNB,
+                                              Dropouts = zinb.sim$dataDropouts),
+                                rowData = features,
+                                colData = cells,
+                                metadata = list(params = params))
+
+    return(sim)
+}
diff --git a/man/ZINBParams.Rd b/man/ZINBParams.Rd
index 1b6db33..e7fc22a 100644
--- a/man/ZINBParams.Rd
+++ b/man/ZINBParams.Rd
@@ -22,7 +22,8 @@ The ZINB-WaVE simulation uses the following parameters:
 
 The majority of the parameters for this simulation are stored in a
 \code{\link[zinbwave]{ZinbModel}} object. Please refer to the documentation
-for this class for details about all the parameters.
+for this class and its constructor(\code{\link[zinbwave]{zinbModel}}) for
+details about all the parameters.
 
 The parameters not shown in brackets can be estimated from real data using
 \code{\link{zinbEstimate}}. For details of the ZINB-WaVE simulation
diff --git a/man/zinbSimulate.Rd b/man/zinbSimulate.Rd
new file mode 100644
index 0000000..17c6611
--- /dev/null
+++ b/man/zinbSimulate.Rd
@@ -0,0 +1,46 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/zinb-simulate.R
+\name{zinbSimulate}
+\alias{zinbSimulate}
+\title{ZINB-WaVE simulation}
+\usage{
+zinbSimulate(params = newZINBParams(), verbose = TRUE, ...)
+}
+\arguments{
+\item{params}{ZINBParams object containing simulation parameters.}
+
+\item{verbose}{logical. Whether to print progress messages}
+
+\item{...}{any additional parameter settings to override what is provided in
+\code{params}.}
+}
+\value{
+SingleCellExperiment containing simulated counts
+}
+\description{
+Simulate counts using the ZINB-WaVE method.
+}
+\details{
+This function is just a wrapper around \code{\link[zinbwave]{zinbSim}} that
+takes a \code{\link{ZINBParams}}, runs the simulation then converts the
+output to a \code{\link[SingleCellExperiment]{SingleCellExperiment}} object.
+See \code{\link[zinbwave]{zinbSim}} and the ZINB-WaVE paper for
+more details about how the simulation works.
+}
+\examples{
+sim <- zinbSimulate()
+
+}
+\references{
+Campbell K, Yau C. Uncovering genomic trajectories with heterogeneous genetic
+and environmental backgrounds across single-cells and populations. bioRxiv
+(2017).
+
+Risso D, Perraudeau F, Gribkova S, Dudoit S, Vert J-P. ZINB-WaVE: A general
+and flexible method for signal extraction from single-cell RNA-seq data
+bioRxiv (2017).
+
+Paper: \url{10.1101/125112}
+
+Code: \url{https://github.com/drisso/zinbwave}
+}
diff --git a/tests/testthat/test-zinb-simulate.R b/tests/testthat/test-zinb-simulate.R
new file mode 100644
index 0000000..80b2b1c
--- /dev/null
+++ b/tests/testthat/test-zinb-simulate.R
@@ -0,0 +1,5 @@
+context("ZINB-WaVE simulation")
+
+test_that("ZINB-WaVE simulation output is valid", {
+    expect_true(validObject(zinbSimulate()))
+})
-- 
GitLab