Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixes
- Fixed a bug where running `AnnotateCells(..., method = "nmf")` could unintentionally propagate normalization changes from an internal temporary `Seurat` object to the returned object.

## [0.17.0] - 2026-03-23

### Added
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ importFrom(R6,R6Class)
importFrom(SeuratObject,"Key<-")
importFrom(SeuratObject,"LayerData<-")
importFrom(SeuratObject,"VariableFeatures<-")
importFrom(SeuratObject,AddMetaData)
importFrom(SeuratObject,Assays)
importFrom(SeuratObject,Cells)
importFrom(SeuratObject,CreateAssay5Object)
Expand Down
44 changes: 27 additions & 17 deletions R/annotate.R
Original file line number Diff line number Diff line change
Expand Up @@ -336,34 +336,44 @@ AnnotateCells <- function(
) {
expect_RcppML()

# Apply normalization
# When normalization is required for NMF: normalize the reference in place, and build
# a separate minimal Seurat from the query counts for NormalizeData.
if (!skip_normalization) {
if (verbose && check_global_verbosity()) {
cli::cli_alert_info("Normalizing reference and query data using method {.str {normalization_method}}")
}
reference <-
Seurat::NormalizeData(
reference,
normalization.method = normalization_method,
margin = 2,
verbose = verbose
)
object <-
Seurat::NormalizeData(
object,
normalization.method = normalization_method,
margin = 2,
verbose = verbose
)
reference <- Seurat::NormalizeData(
reference,
assay = reference_assay,
normalization.method = normalization_method,
margin = 2,
verbose = verbose
)
query_counts <- SeuratObject::LayerData(object, assay = query_assay, layer = "counts")
query_for_nm <- SeuratObject::CreateSeuratObject(
counts = query_counts,
assay = query_assay,
meta.data = data.frame(row.names = colnames(query_counts))
)
Comment on lines +352 to +357
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

query_counts <- LayerData(object, assay = query_assay, layer = "counts") assumes a single counts layer exists. For Seurat v5 Assay5/PNAAssay5 objects (common after merge()), count layers are typically counts.1, counts.2, ... and code elsewhere in the repo calls JoinLayers() before accessing layer = "counts". Please make this robust for multi-layer assays (e.g., join layers on a temporary copy of the assay/object, or explicitly combine all counts.* layers) so AnnotateCells(..., method = "nmf") works with assay version v5 merged objects without mutating the input object.

Copilot uses AI. Check for mistakes.
query_for_nm <- Seurat::NormalizeData(
query_for_nm,
assay = query_assay,
normalization.method = normalization_method,
margin = 2,
verbose = verbose
)
}

if (verbose && check_global_verbosity()) {
cli::cli_alert_info("Fetching overlapping data from reference and query assays.")
}

# Get data
reference_data <- SeuratObject::LayerData(reference, assay = reference_assay)
target_data <- SeuratObject::LayerData(object, assay = query_assay)
target_data <- if (!skip_normalization) {
SeuratObject::LayerData(query_for_nm, assay = query_assay)
} else {
SeuratObject::LayerData(object, assay = query_assay)
}

# Subset data to use overlapping markers
shared_markers <- intersect(rownames(reference_data), rownames(target_data))
Expand Down
1 change: 1 addition & 0 deletions R/pixelatorR-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
#' @importFrom R6 R6Class
#' @importFrom rlang on_load
#' @importFrom rlang run_on_load
#' @importFrom SeuratObject AddMetaData
#' @importFrom SeuratObject Assays
#' @importFrom SeuratObject Cells
#' @importFrom SeuratObject CreateAssay5Object
Expand Down
38 changes: 36 additions & 2 deletions tests/testthat/test-AnnotateCells.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ reference <- SeuratObject::CreateSeuratObject(

test_that("annotate_cells works as expected", {
# NMF default
expect_no_error(seur <- AnnotateCells(
expect_no_error(seur_anno <- AnnotateCells(
seur,
reference,
query_assay = "PNA",
Expand All @@ -35,7 +35,7 @@ test_that("annotate_cells works as expected", {
verbose = FALSE
))
expect_equal(
seur$cell_type %>% unname(),
seur_anno$cell_type %>% unname(),
c(
"Mono CD16+", "pDC", "CD4T", "CD4T", "CD4T", "Mono CD16+",
"pDC", "CD4T", "CD4T", "CD4T", "Mono CD16+", "pDC", "CD4T", "CD4T",
Expand Down Expand Up @@ -63,6 +63,40 @@ test_that("annotate_cells works as expected", {
"CD4T", "CD4T", "Mono CD16+", "Unknown", "CD4T", "CD4T", "CD4T"
)
)

# Test that normalization is preserved
reference <- read_pbmc_reference()
seur_sub <-
seur %>%
Seurat::NormalizeData(normalization.method = "CLR", margin = 2) %>%
# Z score scaling
Seurat::ScaleData() %>%
Seurat::RunPCA(npcs = 3, features = rownames(seur)) %>%
Seurat::FindNeighbors(reduction = "pca", dims = 1:3) %>%
Seurat::FindClusters(resolution = 0.5) %>%
subset(features = head(rownames(seur), 50))


# NMF default
expect_no_error(seur_anno <- AnnotateCells(
seur_sub,
reference,
reference_groups = c("celltype.l1", "celltype.l2"),
# Summarise annotations by cluster, adding an extra column with majority vote
summarize_by_column = "seurat_clusters",
query_assay = "PNA",
reference_assay = "PNA",
method = "nmf"
))

expect_equal(
LayerData(seur_anno, "counts"),
LayerData(seur_sub, "counts")
)
expect_equal(
LayerData(seur_anno, "data"),
LayerData(seur_sub, "data")
)
})


Expand Down
Loading