diff --git a/DESCRIPTION b/DESCRIPTION index a1f603d..07c15b6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,7 +14,8 @@ Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 Suggests: testthat (>= 3.0.0), - Matrix + Matrix, + SeuratObject (>= 5.0.0) Config/testthat/edition: 3 Imports: methods, diff --git a/NAMESPACE b/NAMESPACE index 12bc2ec..f91a3b1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(counts_matrix_from_assay) export(create_bugreport) export(create_bugreport_from_seurat) export(create_loupe) diff --git a/R/err.R b/R/err.R index dd9b50f..5735b55 100644 --- a/R/err.R +++ b/R/err.R @@ -60,9 +60,10 @@ create_bugreport_from_seurat <- function(obj) { assay <- namedAssay[[1]] clusters <- select_clusters(obj) projections <- select_projections(obj) + counts <- counts_matrix_from_assay(assay) create_bugreport( - assay@counts, + counts, clusters, projections, assay_name = assay_name, diff --git a/R/lib.R b/R/lib.R index 6c92fcf..d7645a1 100644 --- a/R/lib.R +++ b/R/lib.R @@ -44,6 +44,7 @@ create_loupe_from_seurat <- function( assay_name <- names(namedAssay) assay <- namedAssay[[1]] + counts <- counts_matrix_from_assay(assay) clusters <- select_clusters(obj, dedup=dedup_clusters) projections <- select_projections(obj) @@ -58,7 +59,7 @@ create_loupe_from_seurat <- function( } success <- create_loupe( - assay@counts, + counts, clusters=clusters, projections=projections, output_dir=output_dir, diff --git a/R/util.R b/R/util.R index 343c93d..26c223d 100644 --- a/R/util.R +++ b/R/util.R @@ -56,8 +56,9 @@ select_assay <- function(obj) { for (i in seq_along(assay_priority)) { name <- names(assay_priority[i]) assay <- Seurat::GetAssay(obj, assay=name) + counts <- counts_matrix_from_assay(assay) - if (length(assay@counts) > 0) { + if (length(counts) > 0) { result = list() result[[name]] = assay return(result) @@ -67,6 +68,25 @@ select_assay <- function(obj) { NULL } +#' Extract the counts matrix from the Assay +#' +#' @param assay A SeuratObject::Assay or SeuratObject::Assay5 +#' +#' @return A sparse counts matrix +#' +#' @export +counts_matrix_from_assay <- function(assay) { + if (packageVersion("Seurat") >= 5) { + return(assay$counts) + } else { + if (is(assay, 'Assay5')) { + stop("Cannot get count matrix: Please upgrade to Seurat > 5 to support dataset") + } + + return(assay@counts) + } +} + #' Select clusters from the assay #' #' @param obj A Seurat Object diff --git a/README.md b/README.md index c99fefa..d901987 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,12 @@ library("loupeR") # Gene Expression RNA assay assay <- seurat_obj[["RNA"]] +# get counts matrix from either the old or newer formats of assay +counts <- counts_matrix_from_assay(assay) + # convert the count matrix, clusters, and projections into a Loupe file create_loupe( - assay@counts, + counts, clusters = select_clusters(seurat_obj), projections = select_projections(seurat_obj) ) diff --git a/man/counts_matrix_from_assay.Rd b/man/counts_matrix_from_assay.Rd new file mode 100644 index 0000000..09d4c76 --- /dev/null +++ b/man/counts_matrix_from_assay.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/util.R +\name{counts_matrix_from_assay} +\alias{counts_matrix_from_assay} +\title{Extract the counts matrix from the Assay} +\usage{ +counts_matrix_from_assay(assay) +} +\arguments{ +\item{assay}{A SeuratObject::Assay or SeuratObject::Assay5} +} +\value{ +A sparse counts matrix +} +\description{ +Extract the counts matrix from the Assay +} diff --git a/tests/testthat/test-util.R b/tests/testthat/test-util.R index 7d72acd..0ba587f 100644 --- a/tests/testthat/test-util.R +++ b/tests/testthat/test-util.R @@ -4,15 +4,29 @@ test_that("select_assay selects active assay", { rna3 <- create_count_mat(5, 5) obj <- Seurat::CreateSeuratObject(rna1, assay="rna1") - obj[["rna2_"]] = Seurat::CreateAssayObject(rna2, key="rna2_") - obj[["rna3_"]] = Seurat::CreateAssayObject(rna3, key="rna3_") + obj[["rna2"]] = Seurat::CreateAssayObject(rna2, key="rna2_") + obj[["rna3"]] = Seurat::CreateAssayObject(rna3, key="rna3_") expect_equal(Seurat::DefaultAssay(object = obj), "rna1") - Seurat::DefaultAssay(object = obj) <- "rna2_" - expect_equal(Seurat::DefaultAssay(object = obj), "rna2_") + Seurat::DefaultAssay(object = obj) <- "rna2" + expect_equal(Seurat::DefaultAssay(object = obj), "rna2") assay <- select_assay(obj) - expect_equal(names(assay), "rna2_") + expect_equal(names(assay), "rna2") +}) + +test_that("counts_matrix_from_assay works on different assay version", { + rna1 <- create_count_mat(5, 5) + rna2 <- create_count_mat(5, 5) + rna3 <- create_count_mat(5, 5) + + obj <- Seurat::CreateSeuratObject(rna1, assay="rna1") + obj[["rna2"]] = Seurat::CreateAssayObject(rna2, key="rna2_") + obj[["rna3"]] = SeuratObject::CreateAssay5Object(rna3, key="rna3_") + + expect_equal(counts_matrix_from_assay(obj[["rna1"]]), rna1) + expect_equal(counts_matrix_from_assay(obj[["rna2"]]), rna2) + expect_equal(counts_matrix_from_assay(obj[["rna3"]]), rna3) }) test_that("select_clusters selects Idents", {