-
Notifications
You must be signed in to change notification settings - Fork 927
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
Fix access to layer data for DietSeurat in V5. #8197
Conversation
I want to nudge this PR given that it has been a long time since submitted, several patches to Seurat V5 have been released and recently version 5.1.0 came out without a fix, and people are still having problems as indicated by all the imaginative ways used to overcome this in #8054. Tagging @mojaveazure since the issue was caused by a change in SeuratObject. This shows the problem with the latest version of Seurat. Using DietSeurat on pbmc_small dataset drops the dimensionality reduction objects but all the data objects remain.
Specifying which layers to remove also does not work:
Internally DietSeurat tries to remove the layers using
The right way to accomplish this given the changes in SeuratObject is to use single brakets:
I hope this helps implementing a fix into the main tree. Perhaps there is some reason I cannot see that prevents this fix from working. In that case I apologize. Just wanted to get this from my mind if possible! |
Is there anything preventing this fix from being merged? |
@ddiez thank you for the bug fix and apologies for the long delay in addressing this PR! With CI checks now properly configured, we aim to be much more responsive to pull requests going forward. As soon as #9544 is approved and merged, checks should pass, and this PR will be next in line for merging. 👍 Before that (but after #9544 is merged), there are a couple of housekeeping items to take care of:
Additionally, a simple unit test for DietSeurat would be a great addition. If you have time, please consider adding one (e.g., in tests/testthat/test_diet_seurat.R). If not, no worries—I can add it myself soon. 🤓 One other thought—I have an inkling that we may need to consider the value of The PR's one-year anniversary is Monday—let's aim to celebrate by merging it in! 👶 🎂 🥳 Thanks again for your contribution and patience. |
Thanks @dcollins15 for the update and happy to know it is in the process of being merged. Next week I am busy until Thursday and won't be able to do anything about this. I can do it from Thursday or Friday if by then #9544 has been merged (not yet at the moment it seems). I can take care of the changes, I think. One question, for the rebase, is it enough if I use the "sync fork" button on top of my fork of the repository? I may try to add a unit test too. I have to take a look first at a few of existing examples first to try to follow the same style as much as possible. Regarding version, I may need to check again the code in the PR, as I cannot remember much about it, at the moment 😥But I remember seeing some cases before in which doing things depending on the version of the object was necessary. I see there is also the option to use |
Oh, for the rebase maybe I can use |
Thanks for the prompt reply @ddiez!
That's the one 🚀 My original message had a typo in it, I meant library(Seurat)
data(pbmc_small)
test_case <- pbmc_small
options(Seurat.object.assay.brackets = "v5")
# Returns the assay's `counts` layer.
test_case[["RNA"]]["counts"]
options(Seurat.object.assay.brackets = "v3")
# Returns the specified gene values from the assay's default layer.
test_case[["RNA"]]["MS4A1"] Weirdly, the option does not affect the behavior of single brackets when used as a setter: options(Seurat.object.assay.brackets = "v5")
# Drops the assay's `counts` layer.
test_case[["RNA"]]["counts"] <- NULL
test_case <- pbmc_small
options(Seurat.object.assay.brackets = "v3")
# Still drops the assay's `counts` layer.
test_case[["RNA"]]["counts"] <- NULL Which is all to say, your fix works as expected 🚀 I'm most of the way through writing a comprehensive set of unit tests for library(testthat)
library(Seurat)
# A test fixture to generate a random counts matrix. Column names are prefixed
# with "Cell" and then feature a zero-padded index, e.g. "Cell05". Row names
# are formatted similarly except with a "Gene" prefix, e.g. "Gene08".
get_random_counts <- function(
n_cells = 10,
n_genes = 10,
min_count = 0,
max_count = 100
) {
# Generate a random matrix with values between `min_count` and `max_count`.
raw_counts <- matrix(
sample(min_count:max_count, n_genes * n_cells, replace = TRUE),
nrow = n_genes,
ncol = n_cells
)
# Set the appropriate column and row names.
colnames(raw_counts) <- sprintf("Cell%0*d", nchar(n_cells), seq_len(n_cells))
rownames(raw_counts) <- sprintf("Cell%0*d", nchar(n_genes), seq_len(n_genes))
# Convert the counts to a `dgCMatrix`.
counts <- as.sparse(as.matrix(raw_counts))
return(counts)
}
test_that("`DietSeurat` can drop the `data` layer from an `Assay` instance", {
counts <- get_random_counts()
assay <- CreateAssayObject(counts)
assay@data <- NormalizeData(counts)
assay@scale.data <- ScaleData(assay@data)
test_case <- CreateSeuratObject(assay)
# Try to retain just the `counts` layer.
# ToDo: Determine why this is failing.
layer_names <- "counts"
result <- DietSeurat(test_case, layers = layer_names)
expect_equal(Layers(result), layer_names)
# Try to drop the `data` layer.
# ToDo: Determine why this is failing.
layer_names <- c("counts", "scale.data")
result <- DietSeurat(test_case, layers = )
expect_equal(Layers(result), layer_names)
}) Running the script yields:
That is all to say, I'll need to follow up with another fix anyway, so I can introduce unit tests then 👌 As soon as I'm unblocked on #9544 we can shepherd this one on home 🐑 (I think I have commit access to your branch, in which case I'm happy to take care of the actual button pressing) |
2cb433a
to
611bc60
Compare
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.
I hope you don't mind that I went ahead and took care of those final two ToDos—I was pretty keen on finishing this PR up on its anniversary 🥳
Thanks again for the fix! I'll tag you in any follow-up PRs I open, in case you're interested 🙂
@dcollins15 thanks a lot for taking care of this and sorry for the late reply. I would be happy to get tagged here in case of additional changes needed. Regarding v3 objects, I reached the same conclusion when briefly testing this previously. I may be wrong in my understanding, but I think that this is by design (?). In v3 objects, even when you create an object with I am not sure I understand the need for |
@dcollins15 I think you forgot to mention this fix in the release notes for Seurat 5.2.0 |
This fixes #8054 which is caused by a change in satijalab/seurat-object@d6aa9af that changed the behavior of brakets.