-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for assays stored as SeuratObject::Assay5 #23
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,26 @@ 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 | ||
|
||
#' @importFrom methods as | ||
#' | ||
#' @export | ||
counts_matrix_from_assay <- function(assay) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding this method to get the counts matrix from either of the assay versions instead of updating the |
||
# convert to older format so that we can consistently access the counts matrix | ||
# Seurat::Assay5 was introduced with Seurat-5.0 and stores the data completely differently | ||
# within the `layers` slot. | ||
if (is(assay, "Assay5")) { | ||
assay <- suppressWarnings(methods::as(object = assay, Class = 'Assay')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. During conversion using Another alternative would be to do something like conversion logic can be seen here |
||
} | ||
|
||
assay@counts | ||
} | ||
|
||
#' Select clusters from the assay | ||
#' | ||
#' @param obj A Seurat Object | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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_") | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated change, but the names here didn't need to have an underscore even though the |
||||||||||||||||||||||||||
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_") | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Seurat package doesn't export the underlying v5 create method like it does with the non v5.
Comment on lines
+19
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
R will pass function arguments by value rather than reference so you shouldn't need to construct multiple instances yourself There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually want to create three different matrices because I want their values to be different for the tests. Turns out R is copy by value, but only if the value is modified. |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
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", { | ||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is used in the tests to create a Seurat dataset with a v5 assay. running
devtools::check
complains if we don't add this as a dependency. For test dependencies it is recommended to add these to suggests