Skip to content
Snippets Groups Projects
scDD-simulate.R 4.68 KiB
Newer Older
Luke Zappia's avatar
Luke Zappia committed
#' scDD simulation
#'
#' Simulate counts using the scDD method.
#'
#' @param params SCDDParams object containing simulation parameters.
#' @param plots logical. whether to generate scDD fold change and validation
#' plots.
#' @param plot.file File path to save plots as PDF.
#' @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[scDD]{simulateSet}} that
#' takes a \code{\link{SCDDParams}}, runs the simulation then converts the
#' output to an \code{\link[scater]{SCESet}} object. See
#' \code{\link[scDD]{simulateSet}} for more details of how the simulation works.
#'
#' @return SCESet containing simulated counts
#'
#' @references
Luke Zappia's avatar
Luke Zappia committed
#' Korthauer KD, Chu L-F, Newton MA, Li Y, Thomson J, Stewart R, et al. A
Luke Zappia's avatar
Luke Zappia committed
#' statistical approach for identifying differential distributions in
Luke Zappia's avatar
Luke Zappia committed
#' single-cell RNA-seq experiments. Genome Biology (2016).
Luke Zappia's avatar
Luke Zappia committed
#'
Luke Zappia's avatar
Luke Zappia committed
#' Paper: \url{10.1186/s13059-016-1077-y}
Luke Zappia's avatar
Luke Zappia committed
#'
#' Code: \url{https://github.com/kdkorthauer/scDD}
#'
#' @examples
Luke Zappia's avatar
Luke Zappia committed
#' sim <- scDDSimulate()
Luke Zappia's avatar
Luke Zappia committed
#' @export
#' @importFrom scater newSCESet
scDDSimulate <- function(params = newSCDDParams(), plots = FALSE,
                         plot.file = NULL, verbose = TRUE, ...) {

    checkmate::assertClass(params, "SCDDParams")
    params <- setParams(params, ...)

    nCells <- getParam(params, "nCells")

    if (verbose) {message("Simulating counts with scDD...")}
    # Restore the default varInflation to NULL
    varInflation <- getParam(params, "varInflation")
    if (all(varInflation == 1)) {
        varInflation <- NULL
    }
    if (verbose) {
        scDD.sim <- scDD::simulateSet(SCdat = getParam(params, "SCdat"),
                                      numSamples = nCells,
                                      nDE = getParam(params, "nDE"),
                                      nDP = getParam(params, "nDP"),
                                      nDM = getParam(params, "nDM"),
                                      nDB = getParam(params, "nDB"),
                                      nEE = getParam(params, "nEE"),
                                      nEP = getParam(params, "nEP"),
                                      sd.range = getParam(params, "sd.range"),
                                      modeFC = getParam(params, "modeFC"),
                                      plots = plots,
                                      plot.file = plot.file,
                                      random.seed = getParam(params, "seed"),
Luke Zappia's avatar
Luke Zappia committed
                                      varInflation = varInflation,
                                      condition = getParam(params, "condition"))
Luke Zappia's avatar
Luke Zappia committed
    } else {
        suppressMessages(
        scDD.sim <- scDD::simulateSet(SCdat = getParam(params, "SCdat"),
                                      numSamples = nCells,
                                      nDE = getParam(params, "nDE"),
                                      nDP = getParam(params, "nDP"),
                                      nDM = getParam(params, "nDM"),
                                      nDB = getParam(params, "nDB"),
                                      nEE = getParam(params, "nEE"),
                                      nEP = getParam(params, "nEP"),
                                      sd.range = getParam(params, "sd.range"),
                                      modeFC = getParam(params, "modeFC"),
                                      plots = plots,
                                      plot.file = plot.file,
                                      random.seed = getParam(params, "seed"),
Luke Zappia's avatar
Luke Zappia committed
                                      varInflation = varInflation,
                                      condition = getParam(params, "condition"))
Luke Zappia's avatar
Luke Zappia committed
        )
    }

    counts <- scDD.sim[[1]]
    foldchanges <- scDD.sim[[2]]
    de.status <- rownames(counts)

    if (verbose) {message("Creating SCESet...")}
Luke Zappia's avatar
Luke Zappia committed
    cell.names <- paste0("Cell", seq_len(nCells * 2))
    gene.names <- paste0("Gene", seq_len(getParam(params, "nGenes")))
Luke Zappia's avatar
Luke Zappia committed

    rownames(counts) <- gene.names
    colnames(counts) <- cell.names
    phenos <- new("AnnotatedDataFrame",
                  data = data.frame(Cell = cell.names,
                                    Condition = rep(1:2, each = nCells)))
    rownames(phenos) <- cell.names
    features <- new("AnnotatedDataFrame",
                    data = data.frame(Gene = gene.names, DEStatus = de.status,
                                      FoldChange = foldchanges))
    rownames(features) <- gene.names
    sim <- newSCESet(countData = counts, phenoData = phenos,
                     featureData = features)

    if (verbose) {message("Done!")}

    return(sim)
Luke Zappia's avatar
Luke Zappia committed
}