Skip to content

Commit

Permalink
add altexp handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jashapiro committed Dec 6, 2024
1 parent 70fba8f commit 9ef3821
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
16 changes: 16 additions & 0 deletions R/make-seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ sce_to_seurat <- function(
return(mat)
})

# pull out any altExps to handle manually
alt_exps <- altExps(sce)
altExps(sce) <- NULL

# Let Seurat do initial conversion to capture most things
sobj <- Seurat::as.Seurat(sce)

Expand Down Expand Up @@ -107,6 +111,18 @@ sce_to_seurat <- function(
sobj[["spliced"]] <- create_seurat_assay(counts = assay(sce, "spliced"))
}

# add altExps as needed.
for (alt_exp_name in names(alt_exps)) {
alt_exp <- alt_exps[[alt_exp_name]]
stopifnot(
"All altExps must contain a `counts` assay." = "counts" %in% assayNames(alt_exp)
)
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)
}
}

# add sample-level metadata after removing non-vector objects
sobj@misc <- sce_meta |>
purrr::discard(is.object) |>
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-make-seurat.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,21 @@ test_that("SCE to Seurat with id conversion and 10x reference works as expected"
dim(reducedDim(sce, "UMAP"))
)
})

test_that("conversion works with altExps", {
sce <- readRDS(test_path("data", "scpca_sce.rds"))
altsce <- SingleCellExperiment(assays = list(counts = counts(sce), logcounts = logcounts(sce)))
altExps(sce) <- list(
alt1 = altsce,
alt2 = altsce
)

seurat_obj <- sce_to_seurat(sce, use_symbols = FALSE)

expect_equal(dim(seurat_obj), dim(sce))
expect_setequal(rownames(seurat_obj), rownames(sce))
expect_setequal(colnames(seurat_obj), colnames(sce))

expect_setequal(names(seurat_obj@assays), c("RNA", "spliced", "alt1", "alt2"))
expect_equal(Seurat::DefaultAssay(seurat_obj), "RNA")
})

0 comments on commit 9ef3821

Please sign in to comment.