How to Make Cell Order identical Between Two Seurat Objects in R?
1
0
Entering edit mode
10 days ago

I'm working with single-cell RNA-seq data in R using the Seurat package, and I need to align the cell order between two Seurat objects (seurat_obj1 and seurat_obj2) so that their cell barcodes are in the same order.

I have tried

# Find common cells
common_cells <- intersect(colnames(seurat_obj1), colnames(seurat_obj2))

seurat_obj1 <- seurat_obj1[, common_cells]
seurat_obj2 <- seurat_obj2[, common_cells]

length(colnames(seurat_obj1)) == length(colnames(seurat_obj2))
TRUE
all(colnames(seurat_obj1) == colnames(seurat_obj2))
FALSE
R seurat scRNA-seq • 477 views
ADD COMMENT
0
Entering edit mode

Can you validate that there are even colnames present?

ADD REPLY
0
Entering edit mode

Yeah, I have printed colnames(seurat_obj1) and colnames(seurat_obj2), It's ok, I dont know why when I choose cells by seurat_obj1[, common_cells], its seems only choose cells but not order by common_cells

ADD REPLY
1
Entering edit mode
9 days ago

From the Seurat v5 cheat sheet:

Since Seurat v5 object doesn’t require all assays have the same cells, Cells() is designed to get cell names of the default assay and colnames() is deigned to get cell names of the entire object

This operation reorders the default assay only
seurat_obj1 <- seurat_obj1[, common_cells]

This operation gets the colnames of the entire object which are validated and not required to be the same as the default assay
colnames(seurat_obj1)

What you should do:
common_cells <- intersect(Cells(seurat_obj1), Cells(seurat_obj2))
seurat_obj1[["RNA"]] <- subset(seurat_obj1[["RNA"]], cells = common_cells)
seurat_obj2[["RNA"]] <- subset(seurat_obj2[["RNA"]], cells = common_cells)

It's good to get into the habit of working with Seurat Assays inside the main object separately since that is how Seurat v5 operates on its component Assays.

ADD COMMENT
0
Entering edit mode

Hi @yura.grabovska,

Thanks a lot, I sure manual order assays like RNA is work, but in fact, Its seems that I need also order reductions and other slots manualy, like seurat_obj1@reductions$pca@cell.embeddings <- seurat_obj1@reductions$pca@cell.embeddings[common_cells, ]. However, doing so can only yield an unstable, temporary working Seurat object, and it will report some difficult-to-resolve errors in many operations.

ADD REPLY
0
Entering edit mode

Hi, I would very much recommend ordering the data upfront and then regenerating the embeddings etc. You can use a common set of HVG and unless you are working with an extremely large object, it shouldn't take too long. While it's possible to reorder all these things at once, I'm not sure I see a reason to when it's more consistent to generate embeddings from a correctly ordered dataset to begin with. As you said, reordering things is too error-prone.

ADD REPLY
0
Entering edit mode

Essentially, I agree with your point of view, but we have encountered some tricky problems. In simplified terms, in my requirements, seurat_obj1 is derived from the Seurat process, while seurat_obj2 comes from some Python processes that start with loom files (depending on some special algorithms implemented only by python). Essentially, if we want to ensure that the order of cells in seurat_obj2 is consistent with seurat_obj1 from the beginning, it is also difficult to handle.

ADD REPLY
0
Entering edit mode

I see what you mean. This being a fairly edge case it is probably good to get in touch with the Seurat package guys on Github and get their take on this too. You won't be the first to have had this issue and it will be helpful to others also.

ADD REPLY

Login before adding your answer.

Traffic: 2537 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6