Skip to content

Commit

Permalink
More fixes for reproducibility
Browse files Browse the repository at this point in the history
  • Loading branch information
irzamsarfraz committed Oct 8, 2023
1 parent 398b31d commit c7bc603
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
15 changes: 9 additions & 6 deletions R/runBatchCorrection.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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: ",
Expand All @@ -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",
Expand Down
32 changes: 19 additions & 13 deletions R/seuratFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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") {
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions man/runHarmony.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/runSeuratFindClusters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c7bc603

Please sign in to comment.