Skip to content

Commit

Permalink
add handling of altexps with underscores in names
Browse files Browse the repository at this point in the history
  • Loading branch information
jashapiro committed Dec 10, 2024
1 parent 40c4c96 commit 17b86b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 12 additions & 0 deletions R/make-seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ sce_to_seurat <- function(
stopifnot(
"All altExps must contain a `counts` assay." = "counts" %in% assayNames(alt_exp)
)

# check name compatibility for Seurat
alt_exp_rownames <- rownames(alt_exp)
if (any(grepl("_", alt_exp_rownames))) {
warning(
"Replacing underscores ('_') with dashes ('-') in feature names from ",
alt_exp_name,
" for Seurat compatibility."
)
rownames(alt_exp) <- gsub("_", "-", alt_exp_rownames)
}

sobj[[alt_exp_name]] <- create_seurat_assay(counts = counts(alt_exp))
if ("logcounts" %in% assayNames(alt_exp)) {
sobj[[alt_exp_name]]$data <- logcounts(alt_exp)
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/test-make-seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,15 @@ test_that("SCE to Seurat with id conversion and 10x reference works as expected"

test_that("conversion works with altExps", {
sce <- readRDS(test_path("data", "scpca_sce.rds"))
altsce <- SingleCellExperiment(assays = list(counts = counts(sce), logcounts = logcounts(sce)))
altsce1 <- SingleCellExperiment(assays = list(counts = counts(sce)[1:10, ], logcounts = logcounts(sce)[1:10, ]))
altsce2 <- SingleCellExperiment(assays = list(counts = counts(sce)[1:3, ]))
rownames(altsce2) <- c("F_1", "F_2", "F_3") # check feature names with underscores
altExps(sce) <- list(
alt1 = altsce,
alt2 = altsce
alt1 = altsce1,
alt2 = altsce2
)

seurat_obj <- sce_to_seurat(sce, use_symbols = FALSE)
expect_warning(seurat_obj <- sce_to_seurat(sce, use_symbols = FALSE))
expect_s4_class(seurat_obj, "Seurat")

expect_equal(dim(seurat_obj), dim(sce))
Expand Down

0 comments on commit 17b86b3

Please sign in to comment.