-
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 2 commits
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,23 @@ 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 SeuratObject LayerData | ||
#' | ||
#' @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 |
||
# Support both Assay and Assay5 classes | ||
# The same as using the helper `assay$counts` but less indirection. | ||
# With Assay5 it is important to use this method as oppossed to grabbing | ||
# the data from `assay@layers` directly as that matrix won't contain the dimnames | ||
SeuratObject::LayerData(assay, "counts") | ||
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. Per the above suggestion about keeping Seurat dependency unversioned, you could probably...
Although I am not sure how that would interact with 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. Maybe something like this:
It looks like it is possible to have a Seurat object with a mix of assay versions. 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 only way they get into the 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. that is my understanding |
||
} | ||
|
||
#' 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.
In order to support v5 datasets, we need to bump the min version of Seurat. Also, have a direct dependency on
SeuratObject
because I think using theLayerData
method directly reads better.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.
Could we keep the
Imports
on Seurat unversioned andSuggests
>= 5.0.0? I am not sure if Roxygen will let you specify it that way, but I imagine cutting to Seurat v5 will trigger a lot of support ticketsThere 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.
Agreed, making v5 a requirement is probably going to be an issue in the near term.
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.
will put in effort to not make this a requirement