From 6f488ee2bd266db2e1ef638736bac8b765a5ece2 Mon Sep 17 00:00:00 2001
From: Luke Zappia <lazappi@users.noreply.github.com>
Date: Wed, 4 Oct 2017 14:06:32 +1100
Subject: [PATCH] Add phenoSimulate

---
 NAMESPACE                            |  3 +
 R/pheno-simulate.R                   | 84 ++++++++++++++++++++++++++++
 man/PhenoParams.Rd                   | 34 +++++++++++
 man/newParams.Rd                     |  7 ++-
 man/phenoSimulate.Rd                 | 43 ++++++++++++++
 man/setParam.Rd                      |  7 ++-
 tests/testthat/test-pheno-simulate.R |  5 ++
 7 files changed, 179 insertions(+), 4 deletions(-)
 create mode 100644 R/pheno-simulate.R
 create mode 100644 man/PhenoParams.Rd
 create mode 100644 man/phenoSimulate.Rd
 create mode 100644 tests/testthat/test-pheno-simulate.R

diff --git a/NAMESPACE b/NAMESPACE
index 5017918..b97422b 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -31,9 +31,11 @@ export(mfaSimulate)
 export(newLun2Params)
 export(newLunParams)
 export(newMFAParams)
+export(newPhenoParams)
 export(newSCDDParams)
 export(newSimpleParams)
 export(newSplatParams)
+export(phenoSimulate)
 export(scDDEstimate)
 export(scDDSimulate)
 export(setParam)
@@ -49,6 +51,7 @@ export(summariseDiff)
 exportClasses(Lun2Params)
 exportClasses(LunParams)
 exportClasses(MFAParams)
+exportClasses(PhenoParams)
 exportClasses(SCDDParams)
 exportClasses(SimpleParams)
 exportClasses(SplatParams)
diff --git a/R/pheno-simulate.R b/R/pheno-simulate.R
new file mode 100644
index 0000000..70042e4
--- /dev/null
+++ b/R/pheno-simulate.R
@@ -0,0 +1,84 @@
+#' PhenoPath simulation
+#'
+#' Simulate counts from a pseudotime trajectory using the PhenoPath method.
+#'
+#' @param params PhenoParams 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[phenopath]{simulate_phenopath}} that takes a
+#' \code{\link{PhenoParams}}, runs the simulation then converts the
+#' output to a \code{\link[SingleCellExperiment]{SingleCellExperiment}} object.
+#' See \code{\link[phenopath]{simulate_phenopath}} and the PhenoPath 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).
+#'
+#' Paper: \url{10.1101/159913}
+#'
+#' Code: \url{https://github.com/kieranrcampbell/phenopath}
+#'
+#' @examples
+#' sim <- phenoSimulate()
+#'
+#' @export
+#' @importFrom SingleCellExperiment SingleCellExperiment
+phenoSimulate <- function(params = newPhenoParams(), verbose = TRUE, ...) {
+
+    checkmate::assertClass(params, "PhenoParams")
+    params <- setParams(params, ...)
+
+    # Set random seed
+    seed <- getParam(params, "seed")
+    set.seed(seed)
+
+    # Get the parameters we are going to use
+    nCells <- getParam(params, "nCells")
+    nGenes <- getParam(params, "nGenes")
+    n.de <- getParam(params, "n.de")
+    n.pst <- getParam(params, "n.pst")
+    n.pst.beta <- getParam(params, "n.pst.beta")
+    n.de.pst.beta <- getParam(params, "n.de.pst.beta")
+
+    if (verbose) {message("Simulating counts...")}
+    pheno.sim <- phenopath::simulate_phenopath(N = nCells,
+                                               G_de = n.de,
+                                               G_pst = n.pst,
+                                               G_pst_beta = n.pst.beta,
+                                               G_de_pst_beta = n.de.pst.beta)
+
+    if (verbose) {message("Creating final dataset...")}
+    cell.names <- paste0("Cell", seq_len(nCells))
+    gene.names <- paste0("Gene", seq_len(nGenes))
+
+    counts <- t(pheno.sim$y)
+    rownames(counts) <- gene.names
+    colnames(counts) <- cell.names
+
+    cells <- data.frame(Cell = cell.names,
+                        Covariate = pheno.sim$x,
+                        Pseudotime = pheno.sim$z)
+    rownames(cells) <- cell.names
+
+    features <- data.frame(Gene = gene.names,
+                           Alpha = pheno.sim$parameters$alpha,
+                           Lambda = pheno.sim$parameters$lambda,
+                           Beta = pheno.sim$parameters$beta,
+                           Regime = pheno.sim$parameters$regime)
+    rownames(features) <- gene.names
+
+    sim <- SingleCellExperiment(assays = list(counts = counts),
+                                rowData = features,
+                                colData = cells,
+                                metadata = list(params = params))
+
+    return(sim)
+}
diff --git a/man/PhenoParams.Rd b/man/PhenoParams.Rd
new file mode 100644
index 0000000..b1e135f
--- /dev/null
+++ b/man/PhenoParams.Rd
@@ -0,0 +1,34 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/AllClasses.R
+\docType{class}
+\name{PhenoParams}
+\alias{PhenoParams}
+\alias{PhenoParams-class}
+\title{The PhenoParams class}
+\description{
+S4 class that holds parameters for the PhenoPath simulation.
+}
+\section{Parameters}{
+
+
+The PhenoPath simulation uses the following parameters:
+
+\describe{
+    \item{\code{nGenes}}{The number of genes to simulate.}
+    \item{\code{nCells}}{The number of cells to simulate.}
+    \item{\code{[seed]}}{Seed to use for generating random numbers.}
+    \item{\code{[n.de]}}{Number of genes to simulate from the differential
+    expression regime}
+    \item{\code{[n.pst]}}{Number of genes to simulate from the pseudotime
+    regime}
+    \item{\code{[n.pst.beta]}}{Number of genes to simulate from the
+    pseudotime + beta interactions regime}
+    \item{\code{[n.de.pst.beta]}}{Number of genes to simulate from the
+    differential expression + pseudotime + interactions regime}
+}
+
+The parameters not shown in brackets can be estimated from real data using
+\code{\link{phenoEstimate}}. For details of the PhenoPath simulation
+see \code{\link{phenoSimulate}}.
+}
+
diff --git a/man/newParams.Rd b/man/newParams.Rd
index de3e6b8..106202a 100644
--- a/man/newParams.Rd
+++ b/man/newParams.Rd
@@ -1,12 +1,13 @@
 % Generated by roxygen2: do not edit by hand
 % Please edit documentation in R/AllGenerics.R, R/Lun2Params-methods.R,
-%   R/LunParams-methods.R, R/MFAParams-methods.R, R/SCDDParams-methods.R,
-%   R/SimpleParams-methods.R, R/SplatParams-methods.R
+%   R/LunParams-methods.R, R/MFAParams-methods.R, R/PhenoParams-methods.R,
+%   R/SCDDParams-methods.R, R/SimpleParams-methods.R, R/SplatParams-methods.R
 \name{newParams}
 \alias{newParams}
 \alias{newLun2Params}
 \alias{newLunParams}
 \alias{newMFAParams}
+\alias{newPhenoParams}
 \alias{newSCDDParams}
 \alias{newSimpleParams}
 \alias{newSplatParams}
@@ -18,6 +19,8 @@ newLunParams(...)
 
 newMFAParams(...)
 
+newPhenoParams(...)
+
 newSCDDParams(...)
 
 newSimpleParams(...)
diff --git a/man/phenoSimulate.Rd b/man/phenoSimulate.Rd
new file mode 100644
index 0000000..2effe20
--- /dev/null
+++ b/man/phenoSimulate.Rd
@@ -0,0 +1,43 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/pheno-simulate.R
+\name{phenoSimulate}
+\alias{phenoSimulate}
+\title{PhenoPath simulation}
+\usage{
+phenoSimulate(params = newPhenoParams(), verbose = TRUE, ...)
+}
+\arguments{
+\item{params}{PhenoParams 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 from a pseudotime trajectory using the PhenoPath method.
+}
+\details{
+This function is just a wrapper around
+\code{\link[phenopath]{simulate_phenopath}} that takes a
+\code{\link{PhenoParams}}, runs the simulation then converts the
+output to a \code{\link[SingleCellExperiment]{SingleCellExperiment}} object.
+See \code{\link[phenopath]{simulate_phenopath}} and the PhenoPath paper for
+more details about how the simulation works.
+}
+\examples{
+sim <- phenoSimulate()
+
+}
+\references{
+Campbell K, Yau C. Uncovering genomic trajectories with heterogeneous genetic
+and environmental backgrounds across single-cells and populations. bioRxiv
+(2017).
+
+Paper: \url{10.1101/159913}
+
+Code: \url{https://github.com/kieranrcampbell/phenopath}
+}
diff --git a/man/setParam.Rd b/man/setParam.Rd
index 9c1ba53..ce8a32c 100644
--- a/man/setParam.Rd
+++ b/man/setParam.Rd
@@ -1,13 +1,14 @@
 % Generated by roxygen2: do not edit by hand
 % Please edit documentation in R/AllGenerics.R, R/Lun2Params-methods.R,
-%   R/LunParams-methods.R, R/Params-methods.R, R/SCDDParams-methods.R,
-%   R/SplatParams-methods.R
+%   R/LunParams-methods.R, R/Params-methods.R, R/PhenoParams-methods.R,
+%   R/SCDDParams-methods.R, R/SplatParams-methods.R
 \docType{methods}
 \name{setParam}
 \alias{setParam}
 \alias{setParam,Lun2Params-method}
 \alias{setParam,LunParams-method}
 \alias{setParam,Params-method}
+\alias{setParam,PhenoParams-method}
 \alias{setParam,SCDDParams-method}
 \alias{setParam,SplatParams-method}
 \title{Set a parameter}
@@ -20,6 +21,8 @@ setParam(object, name, value)
 
 \S4method{setParam}{Params}(object, name, value)
 
+\S4method{setParam}{PhenoParams}(object, name, value)
+
 \S4method{setParam}{SCDDParams}(object, name, value)
 
 \S4method{setParam}{SplatParams}(object, name, value)
diff --git a/tests/testthat/test-pheno-simulate.R b/tests/testthat/test-pheno-simulate.R
new file mode 100644
index 0000000..f67a25e
--- /dev/null
+++ b/tests/testthat/test-pheno-simulate.R
@@ -0,0 +1,5 @@
+context("PhenoPath simulation")
+
+test_that("PhenoPath simulation output is valid", {
+    expect_true(validObject(phenoSimulate()))
+})
-- 
GitLab