R/27-dropsim.R

Defines functions dropsim_simulation dropsim_estimation

Documented in dropsim_estimation dropsim_simulation

#' Estimate Parameters From Real Datasets by dropsim
#'
#' This function is used to estimate useful parameters from a real dataset by
#' using `fit_parameters` function in dropsim 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.
#' @return A list contains the estimated parameters and the results of execution
#' detection.
#' @export
#'
#' @references
#' Github URL: <https://github.com/marchinilab/dropsim>
#'
#' @examples
#' \dontrun{
#' ref_data <- simmethods::data
#' ## estimation
#' estimate_result <- simmethods::dropsim_estimation(
#'   ref_data = ref_data,
#'   verbose = TRUE,
#'   seed = 111
#' )
#' }
#'
dropsim_estimation <- function(ref_data,
                               verbose = FALSE,
                               seed
){
  ##############################################################################
  ####                            Environment                                ###
  ##############################################################################
  if(!requireNamespace("dropsim", quietly = TRUE)){
    message("dropsim is not installed on your device...")
    message("Installing dropsim...")
    devtools::install_github("marchinilab/dropsim")
  }
  ##############################################################################
  ####                               Check                                   ###
  ##############################################################################
  if(!is.matrix(ref_data)){
    ref_data <- as.matrix(ref_data)
  }
  ##############################################################################
  ####                            Estimation                                 ###
  ##############################################################################
  if(verbose){
    message("Estimating parameters using dropsim")
  }
  # Seed
  set.seed(seed)
  # Estimation
  estimate_detection <- peakRAM::peakRAM(
    estimate_result <- dropsim::fit_parameters(ref_data, plot = FALSE)
  )
  ##############################################################################
  ####                           Ouput                                       ###
  ##############################################################################
  estimate_output <- list(estimate_result = estimate_result,
                          estimate_detection = estimate_detection)
  return(estimate_output)
}



#' Simulate Datasets by dropsim
#'
#' This function is used to simulate datasets from learned parameters by `simulateDGE`
#' function in dropsim package.
#'
#' @param parameters A object generated by [dropsim::fit_parameters()]
#' @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.
#' @importFrom methods slot<-
#' @export
#' @details
#' In dropsim, users can only set `nCells` and `nGenes` directly.
#' For more parameters in dropsim, check [dropsim::simulateDGE()] and see `Examples`.
#'
#' @references
#'
#' Github URL: <https://github.com/marchinilab/dropsim>
#'
#' @examples
#' \dontrun{
#' ref_data <- simmethods::data
#' ## estimation
#' estimate_result <- simmethods::dropsim_estimation(
#'   ref_data = ref_data,
#'   verbose = TRUE,
#'   seed = 111
#' )
#'
#' # 1) Simulate with default parameters
#' simulate_result <- simmethods::dropsim_simulation(
#'   parameters = estimate_result[["estimate_result"]],
#'   other_prior = NULL,
#'   return_format = "list",
#'   verbose = TRUE,
#'   seed = 111
#' )
#' ## counts
#' counts <- simulate_result[["simulate_result"]][["count_data"]]
#' dim(counts)
#'
#' # 2) 2000 cells and 5000 genes
#' simulate_result <- simmethods::dropsim_simulation(
#'   parameters = estimate_result[["estimate_result"]],
#'   other_prior = list(nCells = 2000,
#'                      nGenes = 5000),
#'   return_format = "list",
#'   verbose = TRUE,
#'   seed = 111
#' )
#'
#' ## counts
#' counts <- simulate_result[["simulate_result"]][["count_data"]]
#' dim(counts)
#' }
#'
dropsim_simulation <- function(parameters,
                               other_prior = NULL,
                               return_format,
                               verbose = FALSE,
                               seed
){
  ##############################################################################
  ####                            Environment                                ###
  ##############################################################################
  if(!requireNamespace("dropsim", quietly = TRUE)){
    message("dropsim is not installed on your device...")
    message("Installing dropsim...")
    devtools::install_github("marchinilab/dropsim")
  }
  other_prior[["parameters"]] <- parameters
  ## nCells
  if(!is.null(other_prior[["nCells"]])){
    methods::slot(other_prior[["parameters"]], "n_cells") <- as.integer(other_prior[["nCells"]])
  }
  ## nGenes
  if(!is.null(other_prior[["nGenes"]])){
    methods::slot(other_prior[["parameters"]], "n_genes") <- as.integer(other_prior[["nGenes"]])
  }
  ##############################################################################
  ####                               Check                                   ###
  ##############################################################################
  simulate_formals <- simutils::change_parameters(function_expr = "dropsim::simulateDGE",
                                                  other_prior = other_prior,
                                                  step = "simulation")
  # Return to users
  message(paste0("nCells: ", other_prior[['parameters']]@n_cells))
  message(paste0("nGenes: ", other_prior[['parameters']]@n_genes))
  ##############################################################################
  ####                            Simulation                                 ###
  ##############################################################################
  if(verbose){
    message("Simulating datasets using dropsim")
  }
  # Seed
  simulate_formals[["seed"]] <- seed
  # Simulation
  simulate_detection <- peakRAM::peakRAM(
    simulate_result <- do.call(dropsim::simulateDGE, simulate_formals)
  )
  ##############################################################################
  ####                        Format Conversion                              ###
  ##############################################################################
  counts <- as.matrix(simulate_result[["counts"]])
  ## colnames rownames
  colnames(counts) <- paste0("Cell", 1:ncol(counts))
  rownames(counts) <- paste0("Gene", 1:nrow(counts))
  ## col_data
  col_data <- data.frame("cell_name" = colnames(counts))
  ## row data
  row_data <- data.frame("gene_name" = rownames(counts))
  # 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.