Skip to content

Commit

Permalink
Merge 116a9fe into ac07278
Browse files Browse the repository at this point in the history
  • Loading branch information
irzamsarfraz authored Oct 8, 2023
2 parents ac07278 + 116a9fe commit 2f9d9db
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 29 deletions.
2 changes: 1 addition & 1 deletion R/doubletFinder_doubletDetection.R
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ runDoubletFinder <- function(inSCE,
if (length(seuratPcs) > ncol(mat)) {
seuratPcs <- seq(ncol(mat))
}
.withSeed(seed, {
withr::with_seed(seed, {
result <- .runDoubletFinder(
counts = mat,
seuratPcs = seuratPcs,
Expand Down
3 changes: 3 additions & 0 deletions R/miscFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@ getGenesetNamesFromCollection <- function(inSCE, geneSetCollectionName) {
return(names(geneSet))
}

# deprecating
# does not work as intended
# instead directly using withr::with_seed
.withSeed <- function(seed, code) {
if (is.null(seed)) {
code
Expand Down
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 seed 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
2 changes: 1 addition & 1 deletion R/runCluster.R
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ runKMeans <- function(inSCE, nCenters, useReducedDim = "PCA",
if (nComp < ncol(mat)) {
mat <- mat[,seq(nComp)]
}
.withSeed(seed, {
withr::with_seed(seed, {
clust.kmeans <- stats::kmeans(mat, centers = nCenters, iter.max = nIter,
nstart = nStart, algorithm = algorithm)
})
Expand Down
2 changes: 1 addition & 1 deletion R/runTSNE.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ runTSNE <- function(inSCE, useReducedDim = "PCA", useAssay = NULL,
# Rtsne requires a matrix input
mat <- as.matrix(mat)
message(paste0(date(), " ... Computing Rtsne."))
.withSeed(seed, {
withr::with_seed(seed, {
tsneOut <- Rtsne::Rtsne(mat, pca_scale = scale, pca_center = center,
pca = pca, partial_pca = partialPCA,
perplexity = perplexity,
Expand Down
4 changes: 2 additions & 2 deletions R/runUMAP.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ runUMAP <- function(inSCE, useReducedDim = "PCA", useAssay = NULL,
samples[i], "'."))
}

.withSeed(seed, {
umapRes <- scater::calculateUMAP(sceSample, exprs_values = useAssayTemp,
umapRes <- withr::with_seed(seed, {
scater::calculateUMAP(sceSample, exprs_values = useAssayTemp,
dimred = useReducedDim, scale = scale,
n_neighbors = nNeighbors,
learning_rate = alpha,
Expand Down
2 changes: 1 addition & 1 deletion R/scDblFinder_doubletDetection.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ runScDblFinder <- function(inSCE,
inSCE <- inSCE[,-rm.ix]
sample <- sample[-rm.ix]
}
.withSeed(seed, {
withr::with_seed(seed, {
inSCE <- scDblFinder::scDblFinder(sce = inSCE,
samples = sample,
artificialDoublets = simDoublets,
Expand Down
2 changes: 1 addition & 1 deletion R/scater_PCA.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ scaterPCA <- function(inSCE, useAssay = "logcounts", useFeatureSubset = NULL,
subset_row <- .parseUseFeatureSubset(inSCE, useFeatureSubset,
altExpObj = sce, returnType = "logical")
message(paste0(date(), " ... Computing Scater PCA."))
.withSeed(seed, {
withr::with_seed(seed, {
pca <- scater::calculatePCA(useMat$mat, ncomponents = nComponents,
scale = scale, ntop = ntop,
subset_row = subset_row, BPPARAM = BPPARAM)
Expand Down
6 changes: 3 additions & 3 deletions R/scds_doubletdetection.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ runCxds <- function(
nGene <- ntop
while (!inherits(result, "SingleCellExperiment") & nGene > 0) {
try({
result <- .withSeed(seed, {
result <- withr::with_seed(seed, {
scds::cxds(sce = sceSample,
ntop = nGene,
binThresh = binThresh,
Expand Down Expand Up @@ -210,7 +210,7 @@ runBcds <- function(
nGene <- ntop
while (!inherits(result, "SingleCellExperiment") & nGene > 0) {
try({
result <- .withSeed(seed, {
result <- withr::with_seed(seed, {
scds::bcds(sce = sceSample,
ntop = nGene,
srat = srat,
Expand Down Expand Up @@ -349,7 +349,7 @@ runCxdsBcdsHybrid <- function(inSCE,
nGene <- min(nGene.cxds, nGene.bcds)
while (!inherits(result, "SingleCellExperiment") & nGene > 0) {
try({
result <- .withSeed(seed, {
result <- withr::with_seed(seed, {
scds::cxds_bcds_hybrid(sce = sceSample,
cxdsArgs = c(list(ntop = nGene.cxds),
cxdsArgs),
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 2f9d9db

Please sign in to comment.