#' Estimate Parameters From Real Datasets by VeloSim
#'
#' This function is used to estimate useful parameters from a real dataset by
#' using \code{make_trees} function in simutils 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.
#' @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.
#' @return A list contains the estimated parameters and the results of execution
#' detection.
#' @export
#' @details
#' In VeloSim, users can input cell group information if it is available. If cell
#' group information is not provided, the procedure will detect cell groups by
#' kmeans automatically.
#' See `Examples` for more instructions.
#'
#' @references
#' Zhang Z, Zhang X. VeloSim: Simulating single cell gene-expression and RNA velocity. BioRxiv, 2021. <https://doi.org/10.1101/2021.01.11.426277>
#'
#' Github URL: <https://github.com/PeterZZQ/VeloSim>
#'
#' @examples
#' \dontrun{
#' ref_data <- simmethods::data
#'
#' estimate_result <- simmethods::VeloSim_estimation(
#' ref_data = ref_data,
#' other_prior = NULL,
#' verbose = TRUE,
#' seed = 111
#' )
#'
#' ## estimation with cell group information
#' group_condition <- paste0("Group", as.numeric(simmethods::group_condition))
#' estimate_result <- simmethods::VeloSim_estimation(
#' ref_data = ref_data,
#' other_prior = list(group.condition = group_condition),
#' verbose = TRUE,
#' seed = 111
#' )
#' }
#'
VeloSim_estimation <- function(ref_data,
verbose = FALSE,
other_prior = NULL,
seed
){
##############################################################################
#### Environment ###
##############################################################################
if(!requireNamespace("VeloSim", quietly = TRUE)){
message("VeloSim is not installed on your device")
message("Installing VeloSim...")
devtools::install_github("PeterZZQ/VeloSim")
}
##############################################################################
#### Check ###
##############################################################################
if(!is.matrix(ref_data)){
ref_data <- as.matrix(ref_data)
}
if(!is.null(other_prior[["group.condition"]])){
group <- other_prior[["group.condition"]]
}else{
group <- NULL
}
##############################################################################
#### Estimation ###
##############################################################################
if(verbose){
message("Estimating parameters using VeloSim")
}
# Estimation
estimate_detection <- peakRAM::peakRAM(
estimate_result <- simutils::make_trees(ref_data = ref_data,
group = group,
is_Newick = FALSE,
is_parenthetic = FALSE)
)
estimate_result <- list(estimate_result = estimate_result,
data_dim = dim(ref_data))
##############################################################################
#### Ouput ###
##############################################################################
estimate_output <- list(estimate_result = estimate_result,
estimate_detection = estimate_detection)
return(estimate_output)
}
#' Simulate Datasets by VeloSim
#'
#' @param parameters A object generated by [simutils::make_trees()]
#' @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 and other variables are usually customed, so before simulating a dataset
#' you must point it out.
#' @param return_format A character. Alternatives choices: list, SingleCellExperiment,
#' Seurat, h5ad
#' @param verbose Logical. Whether to return messages or not.
#' @param seed A random seed.
#' @importFrom VeloSim SimulateVeloTree
#' @export
#' @details
#' In VeloSim, users can only set `nCells` and `nGenes` to specify the number of cells and genes in the
#' simulated dataset. See `Examples` for instructions.
#'
#' @references
#' Zhang Z, Zhang X. VeloSim: Simulating single cell gene-expression and RNA velocity. BioRxiv, 2021. <https://doi.org/10.1101/2021.01.11.426277>
#'
#' Github URL: <https://github.com/PeterZZQ/VeloSim>
#'
#' @examples
#' \dontrun{
#' ref_data <- simmethods::data
#'
#' ## estimation with cell group information
#' group_condition <- paste0("Group", as.numeric(simmethods::group_condition))
#' estimate_result <- simmethods::VeloSim_estimation(
#' ref_data = ref_data,
#' other_prior = list(group.condition = group_condition),
#' verbose = TRUE,
#' seed = 111
#' )
#'
#' # 1) Simulate with default parameters
#' simulate_result <- simmethods::VeloSim_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) 200 cells and 5000 genes
#' simulate_result <- simmethods::VeloSim_simulation(
#' parameters = estimate_result[["estimate_result"]],
#' other_prior = list(nCells = 200,
#' nGenes = 5000),
#' return_format = "list",
#' verbose = TRUE,
#' seed = 111
#' )
#'
#' ## counts
#' counts <- simulate_result[["simulate_result"]][["count_data"]]
#' dim(counts)
#' }
#'
VeloSim_simulation <- function(parameters,
other_prior = NULL,
return_format,
verbose = FALSE,
seed
){
##############################################################################
#### Environment ###
##############################################################################
if(!requireNamespace("VeloSim", quietly = TRUE)){
message("VeloSim is not installed on your device")
message("Installing VeloSim...")
devtools::install_github("PeterZZQ/VeloSim")
}
##############################################################################
#### Check ###
##############################################################################
phyla <- parameters[["estimate_result"]]
other_prior[["phyla"]] <- phyla
other_prior[["randseed"]] <- seed
other_prior[["n_unstable"]] <- 0
# nCells
if(!is.null(other_prior[["nCells"]])){
other_prior[["ncells_total"]] <- other_prior[["nCells"]]
}else{
other_prior[["ncells_total"]] <- parameters[["data_dim"]][2]
}
# nGenes
if(!is.null(other_prior[["nGenes"]])){
other_prior[["ngenes"]] <- other_prior[["nGenes"]]
}else{
other_prior[["ngenes"]] <- parameters[["data_dim"]][1]
}
# Return to users
message(paste0("nCells: ", other_prior[['ncells_total']]))
message(paste0("nGenes: ", other_prior[['ngenes']]))
simulate_formals <- simutils::change_parameters(function_expr = "VeloSim::SimulateVeloTree",
other_prior = other_prior,
step = "simulation")
##############################################################################
#### Simulation ###
##############################################################################
if(verbose){
message("Simulating datasets using VeloSim")
}
simulate_formals[["ncells_total"]] <- simulate_formals[["ncells_total"]] + 2
# Estimation
simulate_detection <- peakRAM::peakRAM(
simulate_result <- do.call(VeloSim::SimulateVeloTree, simulate_formals)
)
##############################################################################
#### Format Conversion ###
##############################################################################
counts <- simulate_result[["counts_s"]]
colnames(counts) <- paste0("Cell", 1:ncol(counts))
rownames(counts) <- paste0("Gene", 1:nrow(counts))
## col_data
group <- as.numeric(as.factor(simulate_result[["backbone"]]))
col_data <- data.frame("cell_name" = colnames(counts),
"group" = paste0("Group", group))
## 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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.