Skip to content

Commit c0af823

Browse files
committed
Improve speed of batch variable inference
`CreateIntegrationGroups()` is faster when: - called on a classical RNA assay - cells are associated to a single layer at most
1 parent 7146679 commit c0af823

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* Add support for scGraph metric (https://doi.org/10.1101/2024.04.02.587824)
44

5+
* Improved speed of `CreateIntegrationGroups` for non-SCT assay in unambiguous cases
6+
57

68
# SeuratIntegrate 0.4.0
79

R/utils.R

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ could.be.connectivity.Matrix <- function(object, check.symmetry = T) {
749749
#' @importFrom methods slot
750750
#' @keywords internal
751751
#' @noRd
752-
############ Copy-paste
752+
############ Adpated from
753753
# https://github.com/satijalab/seurat/blob/1549dcb3075eaeac01c925c4b4bb73c73450fc50/R/integration5.R#L659-L677
754754
CreateIntegrationGroups <- function(object, layers, scale.layer) {
755755
groups <- if (inherits(x = object, what = 'SCTAssay')) {
@@ -762,10 +762,18 @@ CreateIntegrationGroups <- function(object, layers, scale.layer) {
762762
df
763763
} else if (length(x = layers) > 1L) {
764764
cmap <- slot(object = object, name = 'cells')[, layers]
765-
as.data.frame(x = labels(
766-
object = cmap,
767-
values = Cells(x = object, layer = scale.layer)
768-
))
765+
n_layers_per_cell <- rowSums(cmap)
766+
if (all(n_layers_per_cell < 2)) {
767+
cmap <- as.matrix(cmap)[n_layers_per_cell == 1, ] # drop cell when rowsum is 0
768+
intmap <- cmap %*% matrix(seq_along(layers), ncol = 1)
769+
intmap <- intmap[match(Cells(x = object, layer = scale.layer), rownames(intmap), nomatch = 0), 1, drop = TRUE]
770+
setNames(as.data.frame(setNames(layers[intmap], names(intmap))), "group")
771+
} else {
772+
as.data.frame(x = labels(
773+
object = cmap,
774+
values = Cells(x = object, layer = scale.layer)
775+
))
776+
}
769777
}
770778
names(x = groups) <- 'group'
771779
return(groups)

0 commit comments

Comments
 (0)