diff --git a/R/runBatchCorrection.R b/R/runBatchCorrection.R index fae4a846b..696d87ba7 100644 --- a/R/runBatchCorrection.R +++ b/R/runBatchCorrection.R @@ -296,6 +296,7 @@ runFastMNN <- function(inSCE, useAssay = "logcounts", useReducedDim = NULL, #' make soft kmeans cluster approach hard clustering. Default \code{0.1}. #' @param nIter An integer. The max number of iterations to perform. Default #' \code{10L}. +#' @param Set seed for reproducibility. Default is \code{12345}. #' @param verbose Whether to print progress messages. Default \code{TRUE}. #' @param ... Other arguments passed to \code{\link[harmony]{HarmonyMatrix}}. #' See details. @@ -318,7 +319,7 @@ runFastMNN <- function(inSCE, useAssay = "logcounts", useReducedDim = NULL, runHarmony <- function(inSCE, useAssay = "logcounts", useReducedDim = NULL, batch = "batch", reducedDimName = "HARMONY", nComponents = 50, lambda = 0.1, theta = 5, - sigma = 0.1, nIter = 10, verbose = TRUE, ...) { + sigma = 0.1, nIter = 10, seed = 12345, verbose = TRUE, ...) { if (!requireNamespace("harmony", quietly = TRUE)) { stop("The Harmony package is required to run this function. ", "Install harmony with: ", @@ -342,11 +343,13 @@ runHarmony <- function(inSCE, useAssay = "logcounts", useReducedDim = NULL, } mat <- mat[,seq(nComponents)] } - h <- harmony::HarmonyMatrix(data_mat = mat, meta_data = batchVec, - do_pca = do_pca, npcs = nComponents, - lambda = lambda, theta = theta, - sigma = sigma, max.iter.harmony = nIter, - verbose = verbose, return_object = FALSE, ...) + h <- withr::with_seed(seed, { + harmony::HarmonyMatrix(data_mat = mat, meta_data = batchVec, + do_pca = do_pca, npcs = nComponents, + lambda = lambda, theta = theta, + sigma = sigma, max.iter.harmony = nIter, + verbose = verbose, return_object = FALSE, ...) + }) SingleCellExperiment::reducedDim(inSCE, reducedDimName) <- h S4Vectors::metadata(inSCE)$batchCorr[[reducedDimName]] <- list(useAssay = useAssay, origLogged = TRUE, method = "harmony", diff --git a/R/seuratFunctions.R b/R/seuratFunctions.R index bb595c615..ea17b515b 100644 --- a/R/seuratFunctions.R +++ b/R/seuratFunctions.R @@ -733,6 +733,7 @@ plotSeuratReduction <- #' not. Default \code{TRUE}. #' @param resolution Set the resolution parameter to find larger (value above 1) #' or smaller (value below 1) number of communities. Default \code{0.8}. +#' @param seed Specify the seed value. Default \code{12345}. #' @param externalReduction Pass DimReduc object if PCA/ICA computed through #' other libraries. Default \code{NULL}. #' @param verbose Logical value indicating if informative messages should @@ -755,6 +756,7 @@ runSeuratFindClusters <- function(inSCE, algorithm = c("louvain", "multilevel", "SLM"), groupSingletons = TRUE, resolution = 0.8, + seed = 12345, externalReduction = NULL, verbose = TRUE) { algorithm <- match.arg(algorithm) @@ -773,12 +775,14 @@ runSeuratFindClusters <- function(inSCE, } } - seuratObject <- Seurat::FindNeighbors( - seuratObject, - reduction = useReduction, - dims = seq(dims), - verbose = verbose - ) + seuratObject <- withr::with_seed(seed, { + Seurat::FindNeighbors( + seuratObject, + reduction = useReduction, + dims = seq(dims), + verbose = verbose + ) + }) no_algorithm <- 1 if (algorithm == "louvain") { @@ -790,13 +794,15 @@ runSeuratFindClusters <- function(inSCE, } tempSeuratObject <- seuratObject tempSeuratObject@meta.data <- data.frame() - tempSeuratObject <- Seurat::FindClusters( - tempSeuratObject, - algorithm = no_algorithm, - group.singletons = groupSingletons, - resolution = resolution, - verbose = verbose - ) + tempSeuratObject <- withr::with_seed(seed, { + Seurat::FindClusters( + tempSeuratObject, + algorithm = no_algorithm, + group.singletons = groupSingletons, + resolution = resolution, + verbose = verbose + ) + }) seuratObject@meta.data$seurat_clusters <- tempSeuratObject@meta.data$seurat_clusters inSCE <- .addSeuratToMetaDataSCE(inSCE, seuratObject) diff --git a/man/runHarmony.Rd b/man/runHarmony.Rd index 687bd5dc4..061082870 100644 --- a/man/runHarmony.Rd +++ b/man/runHarmony.Rd @@ -15,6 +15,7 @@ runHarmony( theta = 5, sigma = 0.1, nIter = 10, + seed = 12345, verbose = TRUE, ... ) @@ -59,6 +60,8 @@ make soft kmeans cluster approach hard clustering. Default \code{0.1}.} \item{...}{Other arguments passed to \code{\link[harmony]{HarmonyMatrix}}. See details.} + +\item{Set}{seed for reproducibility. Default is \code{12345}.} } \value{ The input \linkS4class{SingleCellExperiment} object with diff --git a/man/runSeuratFindClusters.Rd b/man/runSeuratFindClusters.Rd index af118f591..d54036eef 100644 --- a/man/runSeuratFindClusters.Rd +++ b/man/runSeuratFindClusters.Rd @@ -14,6 +14,7 @@ runSeuratFindClusters( algorithm = c("louvain", "multilevel", "SLM"), groupSingletons = TRUE, resolution = 0.8, + seed = 12345, externalReduction = NULL, verbose = TRUE ) @@ -41,6 +42,8 @@ not. Default \code{TRUE}.} \item{resolution}{Set the resolution parameter to find larger (value above 1) or smaller (value below 1) number of communities. Default \code{0.8}.} +\item{seed}{Specify the seed value. Default \code{12345}.} + \item{externalReduction}{Pass DimReduc object if PCA/ICA computed through other libraries. Default \code{NULL}.}