R/01-Splat.R

Defines functions Splat_simulation Splat_estimation

Documented in Splat_estimation Splat_simulation

#' Estimate Parameters From Real Datasets by Splat
#'
#' This function is used to estimate useful parameters from a real dataset by
#' using `splatEstimate` function in Splatter package.
#'
#' @param ref_data A count matrix. Each row represents a gene and each column
#' represents a cell.
#' @param verbose Logical.
#' @param seed An integer of a random seed.
#' @importFrom peakRAM peakRAM
#' @importFrom splatter splatEstimate
#' @importFrom SingleCellExperiment counts
#' @return A list contains the estimated parameters and the results of execution
#' detection.
#' @export
#' @references
#' Zappia L, Phipson B, Oshlack A. Splatter: simulation of single-cell RNA sequencing data. Genome biology, 2017, 18(1): 1-15. <https://doi.org/10.1186/s13059-017-1305-0>
#'
#' Bioconductor URL: <https://bioconductor.org/packages/release/bioc/html/splatter.html>
#'
#' Github URL: <https://github.com/Oshlack/splatter>
#' @examples
#' \dontrun{
#' ref_data <- simmethods::data
#' estimate_result <- simmethods::Splat_estimation(ref_data = data,
#'                                                 verbose = TRUE,
#'                                                 seed = 10)
#' estimate_result <- estimate_result[["estimate_result"]]
#' ## Check the class
#' class(estimate_result) == "SplatParams"
#' }
#'
Splat_estimation <- function(ref_data,
                             verbose = FALSE,
                             seed
){
  ##############################################################################
  ####                               Check                                   ###
  ##############################################################################
  if(!is.matrix(ref_data)){
    ref_data <- as.matrix(ref_data)
  }
  ##############################################################################
  ####                            Estimation                                 ###
  ##############################################################################
  if(verbose){
    message("Estimating parameters using Splat")
  }
  # Seed
  set.seed(seed)
  # Estimation
  estimate_detection <- peakRAM::peakRAM(
    estimate_result <- splatter::splatEstimate(ref_data)
  )
  ##############################################################################
  ####                           Ouput                                       ###
  ##############################################################################
  estimate_output <- list(estimate_result = estimate_result,
                          estimate_detection = estimate_detection)
  return(estimate_output)
}


#' Simulate Datasets by Splat
#'
#' This function is used to simulate datasets from learned parameters by `splatSimulate`
#' function in Splatter package.
#'
#' @param parameters A object generated by [splatter::splatEstimate()]
#' @param other_prior A list with names of certain parameters. Some methods need
#' extra parameters to execute the estimation step, so you must input them. In
#' simulation step, the number of cells, genes, groups, batches, the percent of
#' DEGs are usually customed, so before simulating a dataset you must point it out.
#' See `Details` below for more information.
#' @param return_format A character. Alternative choices: list, SingleCellExperiment,
#' Seurat, h5ad. If you select `h5ad`, you will get a path where the .h5ad file saves to.
#' @param verbose Logical. Whether to return messages or not.
#' @param seed A random seed.
#' @details
#' In addtion to simulate datasets with default parameters, users want to simulate
#' other kinds of datasets, e.g. a counts matrix with 2 or more cell groups. In
#' Splat, you can set extra parameters to simulate datasets.
#'
#' The customed parameters you can set are below:
#' 1. nCells. In Splat, you can not set nCells directly and should set batchCells instead. For example, if you want to simulate 1000 cells, you can type `other_prior = list(batchCells = 1000)`. If you type `other_prior = list(batchCells = c(500, 500))`, the simulated data will have two batches.
#' 2. nGenes. You can directly set `other_prior = list(nGenes = 5000)` to simulate 5000 genes.
#' 3. nGroups. You can not directly set `other_prior = list(nGroups = 3)` to simulate 3 groups. Instead, you should set `other_prior = list(prob.group = c(0.2, 0.3, 0.5))` where the sum of group probabilities must equal to 1.
#' 4. de.prob. You can directly set `other_prior = list(de.prob = 0.2)` to simulate DEGs that account for 20 percent of all genes.
#' 5. prob.group. You can directly set `other_prior = list(prob.group = c(0.2, 0.3, 0.5))` to assign three proportions of cell groups. Note that the number of groups always equals to the length of the vector.
#' 6. nBatches. You can not directly set `other_prior = list(nBatches = 3)` to simulate 3 batches. Instead, you should set `other_prior = list(batchCells = c(500, 500, 500))` to reach the goal and the total cells are 1500.
#' 7. If users want to simulate datasets for trajectory inference, just set `other_prior = list(paths = TRUE)`. Simulating trajectory datasets can also specify the parameters of group and batch. See `Examples`.
#'
#' For more customed parameters in Splat, please check [splatter::SplatParams()].
#' For detailed information about Splat, go to <https://www.bioconductor.org/packages/release/bioc/vignettes/splatter/inst/doc/splat_params.html>.
#'
#' @importFrom splatter getParams setParam splatSimulate
#' @importFrom assertthat assert_that
#' @importFrom SingleCellExperiment counts colData rowData
#' @importFrom stringr str_replace
#'
#' @export
#' @references
#' Zappia L, Phipson B, Oshlack A. Splatter: simulation of single-cell RNA sequencing data. Genome biology, 2017, 18(1): 1-15. <https://doi.org/10.1186/s13059-017-1305-0>
#'
#' Bioconductor URL: <https://bioconductor.org/packages/release/bioc/html/splatter.html>
#'
#' Github URL: <https://github.com/Oshlack/splatter>
#' @examples
#' \dontrun{
#' # Load data
#' ref_data <- simmethods::data
#' # Estimate parameters
#' estimate_result <- simmethods::Splat_estimation(ref_data = ref_data,
#'                                                 verbose = TRUE,
#'                                                 seed = 10)
#'
#' # (1) Simulate 500 cells (Since we can not set nCells directly, so we can set
#' # batchCells (a numeric vector)) and 2000 genes
#' simulate_result <- simmethods::Splat_simulation(parameters = estimate_result[["estimate_result"]],
#'                                                 other_prior = list(batchCells = 500,
#'                                                                    nGenes = 2000),
#'                                                 return_format = "list",
#'                                                 verbose = TRUE,
#'                                                 seed = 111)
#' count_data <- simulate_result[["simulate_result"]][["count_data"]]
#' dim(count_data)
#'
#'
#' # (2) Simulate one group and one batch
#' simulate_result <- simmethods::Splat_simulation(parameters = estimate_result[["estimate_result"]],
#'                                                 other_prior = NULL,
#'                                                 return_format = "list",
#'                                                 verbose = TRUE,
#'                                                 seed = 111)
#' count_data <- simulate_result[["simulate_result"]][["count_data"]]
#' dim(count_data)
#'
#'
#' # (3) Simulate two groups (de.prob = 0.1) and one batch
#' simulate_result <- simmethods::Splat_simulation(parameters = estimate_result[["estimate_result"]],
#'                                                 other_prior = list(prob.group = c(0.4, 0.6)),
#'                                                 return_format = "list",
#'                                                 verbose = TRUE,
#'                                                 seed = 111)
#' count_data <- simulate_result[["simulate_result"]][["count_data"]]
#' dim(count_data)
#' ## cell information
#' col_data <- simulate_result[["simulate_result"]][["col_meta"]]
#' table(col_data$group)
#' ## gene information
#' row_data <- simulate_result[["simulate_result"]][["row_meta"]]
#' ### The result of Splat contains the factors of different groups and uses can
#' ### calculate the fold change by division. For example, the DEFactors of A gene
#' ### in Group1 and Group2 are respectively 2 and 1, and the fold change of A gene
#' ### is 2/1=2 or 1/2=0.5.
#' fc_group1_to_group2 <- row_data$DEFacGroup2/row_data$DEFacGroup1
#' table(fc_group1_to_group2 != 1)[2]/4000 ## de.prob = 0.1
#' ### number of all DEGs
#' table(row_data$de_gene)
#'
#'
#' # (4) Simulate two groups (de.prob = 0.2) and two batches
#' ## Since we can not set nBatches directly, so we can set batchCells (a numeric vector)
#' ## to determin the number of batches and cells simutaniously.
#' simulate_result <- simmethods::Splat_simulation(parameters = estimate_result[["estimate_result"]],
#'                                                 other_prior = list(prob.group = c(0.4, 0.6),
#'                                                                    batchCells = c(80, 80),
#'                                                                    de.prob = 0.2),
#'                                                 return_format = "list",
#'                                                 verbose = TRUE,
#'                                                 seed = 111)
#' count_data <- simulate_result[["simulate_result"]][["count_data"]]
#' dim(count_data)
#' col_data <- simulate_result[["simulate_result"]][["col_meta"]]
#' table(col_data$group)
#' table(col_data$batch)
#'
#'
#' # (5) Simulate three groups (de.prob = 0.2) and two batches
#' ## Since we can not set nBatches directly, so we can set batchCells (a numeric vector)
#' ## to determin the number of batches and cells simutaniously.
#' simulate_result <- simmethods::Splat_simulation(parameters = estimate_result[["estimate_result"]],
#'                                                 other_prior = list(prob.group = c(0.4, 0.3, 0.3),
#'                                                                    batchCells = c(80, 80),
#'                                                                    de.prob = 0.2),
#'                                                 return_format = "list",
#'                                                 verbose = TRUE,
#'                                                 seed = 111)
#' count_data <- simulate_result[["simulate_result"]][["count_data"]]
#' dim(count_data)
#' col_data <- simulate_result[["simulate_result"]][["col_meta"]]
#' table(col_data$group)
#' table(col_data$batch)
#' ## row data
#' row_data <- simulate_result[["simulate_result"]][["row_meta"]]
#' ### DEGs
#' table(row_data$de_gene)
#' ### fold change of Group1 to Group2
#' fc_group1_to_group2 <- row_data$DEFacGroup2/row_data$DEFacGroup1
#' table(fc_group1_to_group2 != 1)[2]/4000
#' ### fold change of Group1 to Group3
#' fc_group1_to_group3 <- row_data$DEFacGroup3/row_data$DEFacGroup1
#' table(fc_group1_to_group3 != 1)[2]/4000
#' ### fold change of Group2 to Group3
#' fc_group2_to_group3 <- row_data$DEFacGroup3/row_data$DEFacGroup2
#' table(fc_group2_to_group3 != 1)[2]/4000
#'
#' # 6) Simulate trajectory (only one group is simulated by default)
#' simulate_result <- simmethods::Splat_simulation(
#'   parameters = estimate_result[["estimate_result"]],
#'   other_prior = list(paths = TRUE),
#'   return_format = "SingleCellExperiment",
#'   verbose = TRUE,
#'   seed = 111
#' )
#' ## plot
#' result <- scater::logNormCounts(simulate_result[["simulate_result"]])
#' result <- scater::runPCA(result)
#' plotPCA(result, colour_by = "group")
#'
#' # 7) Simulate trajectory (three groups)
#' simulate_result <- simmethods::Splat_simulation(
#'   parameters = estimate_result[["estimate_result"]],
#'   other_prior = list(paths = TRUE,
#'                      group.prob = c(0.3, 0.4, 0.3),
#'                      de.facLoc = 0.5,
#'                      de.prob = 0.5),
#'   return_format = "SingleCellExperiment",
#'   verbose = TRUE,
#'   seed = 111
#' )
#' ## plot
#' result <- scater::logNormCounts(simulate_result[["simulate_result"]])
#' result <- scater::runPCA(result)
#' plotPCA(result, colour_by = "group")
#' }
#'
Splat_simulation <- function(parameters,
                             other_prior = NULL,
                             return_format,
                             verbose = FALSE,
                             seed
){
  ##############################################################################
  ####                               Check                                   ###
  ##############################################################################
  assertthat::assert_that(class(parameters) == "SplatParams")
  if(!is.null(other_prior)){
    parameters <- simutils::set_parameters(parameters = parameters,
                                           other_prior = other_prior,
                                           method = "Splat")
  }
  if(!is.null(other_prior[["prob.group"]])){
    parameters <- splatter::setParam(parameters,
                                     name = "group.prob",
                                     value = other_prior[["prob.group"]])

  }
  # Get params to check
  params_check <- splatter::getParams(parameters, c("nBatches",
                                                    "batchCells",
                                                    "nGroups",
                                                    "group.prob",
                                                    "de.prob",
                                                    "nCells",
                                                    "nGenes"))

  # check batches
  if(length(params_check[["batchCells"]]) > 1){
    assertthat::assert_that(params_check[["nBatches"]] > 1)
  }else{
    assertthat::assert_that(params_check[["nBatches"]] == 1)
  }
  # check groups
  if(length(params_check[["group.prob"]]) > 1){
    assertthat::assert_that(params_check[["nGroups"]] > 1)
  }else{
    assertthat::assert_that(params_check[["nGroups"]] == 1)
  }

  # DEGs proportion
  de.prob <- params_check[["de.prob"]]
  # Return to users
  message(paste0("nCells: ", params_check[['nCells']]))
  message(paste0("nGenes: ", params_check[['nGenes']]))
  message(paste0("nGroups: ", params_check[['nGroups']]))
  message(paste0("de.prob: ", de.prob))
  message(paste0("nBatches: ", params_check[['nBatches']]))
  ##############################################################################
  ####                            Simulation                                 ###
  ##############################################################################
  if(verbose){
    message("Simulating datasets using Splat")
  }
  # Seed
  parameters <- splatter::setParam(parameters, name = "seed", value = seed)
  parameters <- splatter::setParam(parameters,
                                   name = "de.prob",
                                   value = de.prob/params_check[['nGroups']])
  # Simulation
  if(!is.null(other_prior[["paths"]])){
    cat("Simulating trajectory datasets by Splat \n")
    submethod <- "paths"
    if(!is.null(other_prior[["path.from"]])){
      parameters <- splatter::setParam(parameters,
                                       name = "path.from",
                                       value = other_prior[["path.from"]])
    }else{
      parameters <- splatter::setParam(parameters,
                                       name = "path.from",
                                       value = seq(1:params_check[['nGroups']])-1)
    }
  }else{
    if(params_check[["nGroups"]] == 1){
      submethod <- "single"
    }else if(params_check[["nGroups"]] != 1){
      submethod <- "groups"
    }
  }
  simulate_detection <- peakRAM::peakRAM(
    simulate_result <- splatter::splatSimulate(parameters,
                                               method = submethod,
                                               verbose = verbose))
  ##############################################################################
  ####                        Format Conversion                              ###
  ##############################################################################
  # counts
  counts <- as.matrix(SingleCellExperiment::counts(simulate_result))
  # col_data
  col_data <- as.data.frame(SummarizedExperiment::colData(simulate_result))
  if(params_check[['nGroups']] == 1){
    col_data[, 3] <- rep("Group1", ncol(simulate_result))
  }else{
    col_data <- col_data[, -4]
  }
  colnames(col_data) <- c("cell_name", "batch", "group")
  # row_data
  row_data <- as.data.frame(SummarizedExperiment::rowData(simulate_result))
  if(params_check[['nGroups']] == 1){
    row_data <- as.data.frame(row_data[, -c(2:4)])
    colnames(row_data) <- "gene_name"
  }else{
    group_fac <- row_data[, grep(colnames(row_data), pattern = "^DE")]
    batch_fac <- row_data[, grep(colnames(row_data), pattern = "^Batch")]
    total_sum <- rowSums(group_fac)
    de_gene <- ifelse(total_sum == params_check[['nGroups']], "no", "yes")
    row_data[, 2] <- de_gene
    row_data <- row_data[, -c(3:4)]
    colnames(row_data) <- c("gene_name", "de_gene", colnames(batch_fac), colnames(group_fac))
  }
  # Establish SingleCellExperiment
  simulate_result <- SingleCellExperiment::SingleCellExperiment(list(counts = counts),
                                                                colData = col_data,
                                                                rowData = row_data)
  simulate_result <- simutils::data_conversion(SCE_object = simulate_result,
                                               return_format = return_format)

  ##############################################################################
  ####                           Ouput                                       ###
  ##############################################################################
  simulate_output <- list(simulate_result = simulate_result,
                          simulate_detection = simulate_detection)
  return(simulate_output)
}
duohongrui/simmethods documentation built on June 17, 2024, 10:49 a.m.