From 17b86b3f117f3f26820b189860a132f7682afb36 Mon Sep 17 00:00:00 2001 From: Joshua Shapiro Date: Tue, 10 Dec 2024 13:51:58 -0500 Subject: [PATCH] add handling of altexps with underscores in names --- R/make-seurat.R | 12 ++++++++++++ tests/testthat/test-make-seurat.R | 10 ++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/R/make-seurat.R b/R/make-seurat.R index 0b039fd..aabb4f0 100644 --- a/R/make-seurat.R +++ b/R/make-seurat.R @@ -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) diff --git a/tests/testthat/test-make-seurat.R b/tests/testthat/test-make-seurat.R index b61de32..df03a77 100644 --- a/tests/testthat/test-make-seurat.R +++ b/tests/testthat/test-make-seurat.R @@ -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))