Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
468e8d1
refactor: reorganize the arg order in top level functions
jamespeapen Nov 6, 2025
51e0e94
feat(threads): add BiocParallel worker checking functions
jamespeapen Nov 13, 2025
2e7b5ae
wip: add args for parallelization for scCompartments/arrayCompartments
jamespeapen Nov 13, 2025
2f0a56f
wip: parallelize precomputeBootstrapMeans
jamespeapen Nov 13, 2025
d650a0c
wip: parallelize inference and bootstrapping,
jamespeapen Nov 13, 2025
cf7740e
chore(flake): update with parallelly
jamespeapen Nov 13, 2025
2650389
docs: add parallelization details
jamespeapen Nov 13, 2025
05b021d
docs(vignettes): update call calls with BPPARAM
jamespeapen Nov 13, 2025
b53816a
add logging
jamespeapen Nov 13, 2025
9fd2cd9
fix(getCompartments): remove bpparams setting
jamespeapen Nov 13, 2025
eeacd0a
fix(logging): use futil.logger and move most to debug level
jamespeapen Nov 13, 2025
a39216d
fix(bootstrap): don't show progress bar in the bootstrap step
jamespeapen Nov 13, 2025
aa607e1
fix(parallel): correctly calculate total when outer/inner is 1
jamespeapen Nov 13, 2025
557a3d5
fix(bootstrap): only message about slow bootstrap if bootstrapping
jamespeapen Nov 13, 2025
6ad5144
fix(logging): move bootstrap message after computing message
jamespeapen Nov 13, 2025
e917ae6
docs(parallel): add info on load balancing different configs
jamespeapen Nov 14, 2025
4378c04
feat(parallel): check if user BPPARAM settings are optimal
jamespeapen Nov 14, 2025
c81ce2e
fix(bootstrapping): don't warn about slow serial bootstrapping
jamespeapen Nov 14, 2025
151d60e
fix(parallel): add missing SerialParam import
jamespeapen Dec 19, 2025
4c08fac
chore(parallel): fix 'chromosme' typo
jamespeapen Dec 19, 2025
5ea3e54
docs(parallel): add check_optim
jamespeapen Dec 19, 2025
f72332d
chore(DESCRIPTION): add missing parallelly
jamespeapen Dec 19, 2025
f848061
test: remove message tests since they're now handled by futile.logger
jamespeapen Dec 19, 2025
e049e97
fix(pkgdown): internalize getCompartments
jamespeapen Feb 23, 2026
fcbd673
docs(arrayCompartments): remove parallel arg in example
jamespeapen Feb 23, 2026
61e6376
docs(parallel): fix missing params in docstring
jamespeapen Feb 23, 2026
1de1a5e
fix(arrayCompartments): pass boot.parallel to get_nested_params
jamespeapen Feb 23, 2026
a54cce5
docs: do grouped inference for examples with SerialParam
jamespeapen Feb 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ Depends:
HDF5Array
Imports:
BiocSingular,
BiocParallel,
futile.logger,
rlang,
S4Vectors,
IRanges,
GenomicRanges,
GenomeInfoDb,
parallel,
parallelly,
methods,
grid,
ggplot2,
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ import(HDF5Array)
import(Matrix)
import(RaggedExperiment)
import(SummarizedExperiment)
importFrom(BiocParallel,SerialParam)
importFrom(BiocParallel,bplapply)
importFrom(BiocParallel,bpnworkers)
importFrom(BiocParallel,bpparam)
importFrom(BiocSingular,IrlbaParam)
importFrom(BiocSingular,runSVD)
importFrom(GenomeInfoDb,"seqlevelsStyle<-")
Expand All @@ -59,6 +63,8 @@ importFrom(GenomeInfoDb,seqlengths)
importFrom(GenomeInfoDb,seqlevels)
importFrom(S4Vectors,queryHits)
importFrom(S4Vectors,subjectHits)
importFrom(futile.logger,flog.debug)
importFrom(futile.logger,flog.info)
importFrom(ggplot2,aes)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,geom_raster)
Expand Down
41 changes: 20 additions & 21 deletions R/arrayCompartments.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
#' @param obj Input SummarizedExperiment object
#' @param res Compartment resolution in bp
#' @param chr What chromosome to work on (leave as NULL to run on all chromosomes)
#' @param group Whether to treat this as a group set of samples
#' @param targets Samples/cells to shrink towards
#' @param preprocess Whether to preprocess the arrays prior to compartment inference
#' @param parallel Whether to run samples in parallel
#' @param cores How many cores to use when running samples in parallel
#' @param bootstrap Whether we should perform bootstrapping of inferred compartments
#' @param num.bootstraps How many bootstraps to run
#' @param boot.parallel Whether to run the bootstrapping in parallel
#' @param boot.cores How many cores to use for the bootstrapping
#' @param preprocess Whether to preprocess the arrays prior to compartment inference
#' @param array.type What type of array is this ("hm450", "EPIC")
#' @param genome What genome to work on ("hg19", "hg38", "mm9", "mm10")
#' @param other Another arbitrary genome to compute compartments on
#' @param group Whether to treat this as a group set of samples
#' @param array.type What type of array is this ("hm450", "EPIC")
#' @param boot.parallel Whether to run the bootstrapping in parallel. See details.
#' @param BPPARAM BiocParallelParam object to use for parallelization. See details.
#'
#' @inherit scCompartments details
#'
#' @return A RaggedExperiment of inferred compartments
#' @import SummarizedExperiment
Expand All @@ -34,32 +34,33 @@
#' array_compartments <- arrayCompartments(
#' array.data.chr14,
#' chr="chr14",
#' parallel=FALSE,
#' group=TRUE,
#' bootstrap=FALSE,
#' genome="hg19",
#' array.type="hm450"
#' array.type="hm450",
#' BPPARAM = BiocParallel::SerialParam()
#' )
#' }
arrayCompartments <- function(
obj,
res = 1e6,
chr = NULL,
group = FALSE,
targets = NULL,
preprocess = TRUE,
parallel = TRUE,
cores = 2,
bootstrap = TRUE,
num.bootstraps = 1000,
boot.parallel = TRUE,
boot.cores = 2,
preprocess = TRUE,
array.type = c("hm450", "EPIC"),
genome = c("hg19", "hg38", "mm9", "mm10"),
group = FALSE,
other = NULL,
array.type = c("hm450", "EPIC")
boot.parallel = TRUE,
BPPARAM = bpparam()
) {
verifySE(obj)
verifyCoords(obj)
verifyAssayNames(obj, assay = "array")
bpparams <- get_nested_params(BPPARAM, boot.parallel)
check_worker_count(bpparams, group, length(chr), bootstrap)

# preprocess the arrays
if (preprocess) {
Expand All @@ -83,15 +84,13 @@ arrayCompartments <- function(
obj = obj,
assay = "array",
res = res,
parallel = parallel,
chr = chr,
targets = targets,
cores = cores,
bootstrap = bootstrap,
num.bootstraps = num.bootstraps,
boot.parallel = boot.parallel,
boot.cores = boot.cores,
genome = genome,
group = group
group = group,
boot.parallel = boot.parallel,
bpparams = bpparams
)
}
66 changes: 32 additions & 34 deletions R/bootstrapCompartments.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#' Non-parametric bootstrapping of compartments and summarization of bootstraps/compute confidence intervals
#' Non-parametric bootstrapping of compartments and summarization of
#' bootstraps/compute confidence intervals
#'
#' @name bootstrapCompartments
#'
#' @param obj List object of computed compartments for a sample with 'pc' and 'gr' as elements
#' @param original.obj The original, full input SummarizedExperiment of all samples/cells
#' @param BPPARAM BiocParallelParam for parallelizing bootstrapping
#' @param bootstrap.samples How many bootstraps to run
#' @param chr Which chromosome to operate on
#' @param assay What sort of assay are we working on
#' @param parallel Whether to run the bootstrapping in parallel
#' @param cores How many cores to use for parallel processing
#' @param targets Targets to shrink towards
#' @param res The compartment resolution
#' @param genome What genome are we working on
Expand All @@ -18,7 +18,6 @@
#' @param bootstrap.means Pre-computed bootstrap means matrix
#'
#' @return Compartment estimates with summarized bootstraps and confidence intervals
#' @importFrom parallel mclapply
#' @import SummarizedExperiment
#'
#' @examples
Expand All @@ -29,17 +28,16 @@
bootstrapCompartments <- function(
obj,
original.obj,
BPPARAM,
bootstrap.samples = 1000,
chr = "chr14",
group = FALSE,
assay = c("rna", "atac", "array"),
parallel = TRUE,
cores = 2,
targets = NULL,
res = 1e6,
genome = c("hg19", "hg38", "mm9", "mm10"),
q = 0.95,
svd = NULL,
group = FALSE,
bootstrap.means = NULL
) {
# function for nonparametric bootstrap of compartments and compute 95% CIs
Expand All @@ -61,36 +59,36 @@ bootstrapCompartments <- function(
}

# if (ncol(original.obj) < 6) stop("We need more than 5 samples to bootstrap with for the results to be meaningful.")
if (parallel) {
message("Bootstrapping in parallel with ", cores, " cores.")
} else {
message("Not bootstrapping in parallel will take a long time...")
}

# bootstrap and recompute compartments
resamp.compartments <- mclapply(1:ncol(bmeans), function(b) {
# get the shrunken bins with new global mean
boot.mean <- as.matrix(bmeans[, b])
colnames(boot.mean) <- "globalMean"
s.bins <- shrinkBins(
obj,
original.obj,
prior.means = boot.mean,
chr = chr,
res = res,
assay = assay,
genome = genome
)
cor.bins <- getCorMatrix(s.bins, squeeze = !group)
BiocParallel::bpprogressbar(BPPARAM) <- FALSE
resamp.compartments <- bplapply(
1:ncol(bmeans),
function(b) {
# get the shrunken bins with new global mean
boot.mean <- as.matrix(bmeans[, b])
colnames(boot.mean) <- "globalMean"
s.bins <- shrinkBins(
obj,
original.obj,
prior.means = boot.mean,
chr = chr,
res = res,
assay = assay,
genome = genome
)
cor.bins <- getCorMatrix(s.bins, squeeze = !group)

# Stupid check for perfect correlation with global mean
if (any(is.na(cor.bins$binmat.cor))) {
absig <- matrix(rep(NA, nrow(cor.bins$binmat.cor)))
} else {
absig <- getABSignal(cor.bins, assay = assay)
}
return(absig)
}, mc.cores = ifelse(parallel, cores, 1))
# Stupid check for perfect correlation with global mean
if (any(is.na(cor.bins$binmat.cor))) {
absig <- matrix(rep(NA, nrow(cor.bins$binmat.cor)))
} else {
absig <- getABSignal(cor.bins, assay = assay)
}
return(absig)
},
BPPARAM = BPPARAM
)

# summarize the bootstraps and compute confidence intervals
resamp.compartments <- summarizeBootstraps(resamp.compartments, svd, q = q, assay = assay)
Expand Down
6 changes: 3 additions & 3 deletions R/fixCompartments.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ setGeneric("fixCompartments", function(x, min.conf = 0.8, parallel = FALSE, core
#' @rdname fixCompartments
#' @param x GRanges
setMethod("fixCompartments", "GRanges", function(x, min.conf = 0.8, parallel = FALSE, cores = 1) {
message("Assuming we only have a single sample to process")
message("Fixing compartments using a minimum confidence score of ", min.conf * 100, "%")
flog.debug("Assuming we only have a single sample to process.")
flog.debug("Fixing compartments using a minimum confidence score of %d%%", min.conf * 100)
flipper(x, min.conf)
})

#' @rdname fixCompartments
#' @param x RaggedExperiment
setMethod("fixCompartments", "RaggedExperiment", function(x, min.conf = 0.8, parallel = FALSE, cores = 1) {
obj <- condenseSE(x, sample.name = colnames(assay(x)))
message("Fixing compartments using a minimum confidence score of ", min.conf * 100, "%")
flog.info("Fixing compartments using a minimum confidence score of %d%%", min.conf * 100)
# go through and invert compartments based on the min.conf
flip_compartments_lst <- mclapply(obj, flipper, min.conf, mc.cores = ifelse(parallel, cores, 1))
names(flip_compartments_lst) <- names(obj)
Expand Down
6 changes: 3 additions & 3 deletions R/getABSignal.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ getABSignal <- function(
assay <- match.arg(assay)
gr <- x$gr

message("Calculating eigenvectors.")
flog.debug("Calculating eigenvectors.")
pc <- getSVD(x$binmat.cor, sing.vec = "right")
if (squeeze) pc <- ifisherZ(pc)

message("Smoothing eigenvector.")
flog.debug("Smoothing eigenvector.")
gr$pc <- meanSmoother(pc)
message("Done smoothing.")
flog.debug("Done smoothing.")

if (flipSign(gr, genome)) gr$pc <- -gr$pc
gr$compartments <- extractOpenClosed(gr, assay = assay)
Expand Down
2 changes: 1 addition & 1 deletion R/getBinMatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ getBinMatrix <- function(
ids <- findOverlaps(genloc, gr.bin, select = "first")

binCount <- length(gr.bin)
message(binCount, " bins created...")
flog.debug("%d bins created...", binCount)

mat.bin <- apply(mat, 2, function(x) {
.summarizeBins(x, binCount, ids, FUN)
Expand Down
Loading
Loading