Skip to content
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

Subsetting a codex seurat object #6409

Closed
msalaciak opened this issue Sep 14, 2022 · 15 comments
Closed

Subsetting a codex seurat object #6409

msalaciak opened this issue Sep 14, 2022 · 15 comments
Labels
bug Something isn't working

Comments

@msalaciak
Copy link

Hi!
I'm using the feat/imaging branch to work with Codex data. Our data comes from codex performed on a tissue microarray of 24 cores.

I successfully created a Seurat object for each core and then merged them together to perform joint clustering. When working with multi-sample scRNA-seq data, I often like to subset the object by sample ID for further analysis, however, when I do this with codex data I get this error.

> subset(x = codex.merge, subset = sample == "Reg1")
Error in subset.Centroids(x = x[[i]], cells = cells) : 
  None of the requested cells found

Any advice on how to work around this?

Best,
M

@msalaciak msalaciak added the bug Something isn't working label Sep 14, 2022
@k3yavi
Copy link
Member

k3yavi commented Sep 23, 2022

Hi @msalaciak

Can you check if any of the cells have metadata sample assigned to Reg1 ?
you can use table(codex.merge$sample). Most probably none of the cells has Reg1 metadata.

@k3yavi k3yavi closed this as completed Sep 23, 2022
@msalaciak
Copy link
Author

Hi @k3yavi

Thanks for your suggestion. I just checked and all my cells are assigned to a specific Region...see output below.

table(codex.merge$sample)

 Reg1 Reg10 Reg11 Reg12 Reg13 Reg14 Reg15 Reg16 Reg17 Reg18 Reg19  Reg2 Reg20 Reg21 Reg22 Reg23 Reg24  Reg3  Reg4  Reg5  Reg6  Reg7 
 7881 41643 12963 20016 41132 23706 44025 28454 31376  5453 14314 36648 20385 37112 12351 12441 31865 31542 32197 32076 24820   571 
 Reg8  Reg9 
 7157  6579 

@msalaciak
Copy link
Author

hi @k3yavi

I just wanted to follow up on this issue. I noticed it was closed.

Thanks,
Matt

@k3yavi
Copy link
Member

k3yavi commented Oct 18, 2022

Hi @msalaciak, It's not clear what's going on. Are you using an open dataset where I can replicate the issue? If you can share the data and the script to reproduce the error, that would be great.

@msalaciak
Copy link
Author

Hi @k3yavi,
unfortunately, it's not an open dataset so I can not share it. Is there anything else I can share to help solve this problem?

Thanks,
Matt

@alikhuseynov
Copy link

alikhuseynov commented Nov 15, 2022

Hi there,
I was having similar issue with Vizgen data (multiple regions).
I think currently subsetting works only if at least few cells are present in each FOVs (eg Images(object = obj))

Here is my intermediate solution for "FOV-optimized" subset() function. And here is the updated version that subsets the molecules as well.

## test
library(magrittr)
library(Seurat)
source("./subset_obj_seurat.R") # load the intermediate solution function
obj # merged object (several Vizgen samples/region)

# ..using cells only, eg first 100 cells
obj_test <- subset_opt(obj, cells = Cells(obj)[1:100])
obj_test

# or..using a subset, ie keep only selected sample
obj_test <- subset_opt(obj, subset = samples == "vz_region_0")
obj_test

# checks..
obj$samples %>% table
Images(obj) # all FOVs of the merged object
Images(obj_test) # subsetted/kept FOVs
obj_test[[Images(obj_test)[i]]] %>% names # FOV classes
obj_test[[Images(obj_test)[i]]][["centroids"]] %>% str # cell centroids
obj_test[[Images(obj_test)[i]]][["centroids"]] %>% Cells %>% str # cell ids

This issue is still present in the most recent SeuratObject package
Let me know if it works on akoya/codex data.
Hope this helps

@marhirst
Copy link

@alikhuseynov: I was banging my head against a wall to figure out how to subset my merged MERFISH data. This absolutely did the trick! Thank you for sharing!

@alikhuseynov
Copy link

@alikhuseynov: I was banging my head against a wall to figure out how to subset my merged MERFISH data. This absolutely did the trick! Thank you for sharing!

Thanks, I happy it is useful for other users, will submit a PR on that too.

@LooLipin
Copy link

Hi @alikhuseynov, thanks for this solution.

Having struggling to use seurat's subset() to extract my cells of interest from specific regions, I managed to use subset_opt to work on one of my conditions. However, in some situations, I get the error message:

"Error in validObject(object = object[[i]]) :
invalid class “Assay5” object: Layers must be two-dimensional objects"

I can't seem to figure out why this happens in a subset of cells but not others and was hoping perhaps you have an idea.

Best,
Lipin

@alikhuseynov
Copy link

Hi @alikhuseynov, thanks for this solution.

Having struggling to use seurat's subset() to extract my cells of interest from specific regions, I managed to use subset_opt to work on one of my conditions. However, in some situations, I get the error message:

"Error in validObject(object = object[[i]]) : invalid class “Assay5” object: Layers must be two-dimensional objects"

I can't seem to figure out why this happens in a subset of cells but not others and was hoping perhaps you have an idea.

Best, Lipin

I'm glad that, in part, it did help you. subset_opt is just a temporary solution I had for me and shared it here. I have a v2 of subset_opt, not sure if you tried it as well? I should optimize and PR that to SeuratObject just don't have the bandwidth right now. Though, I'm not sure if things have been solved in v5 for subset. Take a look at his issue #7462.
For your bug, It is related to v5 Seurat & SeuratObject, I haven't tested my function on v5 version. Also, at which stage of code that error comes out?

@LooLipin
Copy link

Hi @alikhuseynov, thanks for the advice. I have tried v2 as well but it gives me the same error.

It looks like the error is in updating the seurat objects.

traceback()
5: stop(msg, ": ", errors, domain = NA)
4: validObject(object = object[[i]])
3: UpdateSeuratObject(.)
2: obj_subset %<>% UpdateSeuratObject() at subset_obj_seurat_v2.R#137
1: subset_opt(Sham_slices1, idents = "Glu3")

For context, I had already cropped my sections into 2 halves, which is my first subset based on coordinates. Now I would like to subset again based on cell identity to do different expression analysis. The first subset with coordinates works fine but subsequently when I try to subset based on cell identity I get this error.

Best,
Lipin

@alikhuseynov
Copy link

Hi @alikhuseynov, thanks for the advice. I have tried v2 as well but it gives me the same error.

It looks like the error is in updating the seurat objects.

traceback()
5: stop(msg, ": ", errors, domain = NA)
4: validObject(object = object[[i]])
3: UpdateSeuratObject(.)
2: obj_subset %<>% UpdateSeuratObject() at subset_obj_seurat_v2.R#137
1: subset_opt(Sham_slices1, idents = "Glu3")

For context, I had already cropped my sections into 2 halves, which is my first subset based on coordinates. Now I would like to subset again based on cell identity to do different expression analysis. The first subset with coordinates works fine but subsequently when I try to subset based on cell identity I get this error.

Best, Lipin

ok, if it is about updating the object, then use Update.object = FALSE within subset_opt

@LooLipin
Copy link

Thanks @alikhuseynov , that was an easy fix!

@alikhuseynov
Copy link

alikhuseynov commented Mar 19, 2024

Thanks @alikhuseynov , that was an easy fix!

great! I should update that function removing the UpdateSeuratObject()

@MichaelSchulzGSH
Copy link

AWESOME!!
Thanks so much, was about to give up on subsetting my merged seurat (multiple MERFISH sets).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants