satijalab / seurat

R toolkit for single cell genomics
http://www.satijalab.org/seurat
Other
2.26k stars 905 forks source link

invalid class “Seurat” object: all cells in reductions must be in the same order as the Seurat object #8297

Closed RaghadShu closed 8 months ago

RaghadShu commented 8 months ago

Hello, I am working with multiome data (RNA+ATAC). I am working with 5 samples, and I have first integrated the RNA assay with rpca, the ATAC assay with rlsi, then combined both using wnn approach.

I am following the analysis conducted here: https://satijalab.org/seurat/articles/weighted_nearest_neighbor_analysis#wnn-analysis-of-10x-multiome-rna-atac, specifically the accessible regions part. With my integrated object named "seurat", this is what I am running (which is the exact same code as the vignette): DefaultAssay(seurat) <- "ATAC" pwm_set <- getMatrixSet(x = JASPAR2020, opts = list(species = 9606, all_versions = FALSE)) # 9606 is human motif.matrix <- CreateMotifMatrix(features = granges(seurat), pwm = pwm_set, genome = 'hg38', use.counts = FALSE) motif.object <- CreateMotifObject(data = motif.matrix, pwm = pwm_set) seurat <- SetAssayData(seurat, assay = 'ATAC', slot = 'motifs', new.data = motif.object)

Running the last line produces this error: Error in validObject(object = x) : invalid class “Seurat” object: all cells in reductions must be in the same order as the Seurat object

Can I get some help on how to fix this?

RaghadShu commented 8 months ago

I have ensured to update Seurat and Signac to the latest versions, but the problem is still there.

`sessionInfo() R version 4.2.2 Patched (2022-11-10 r83330) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 18.04.6 LTS

Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=es_ES.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=es_ES.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=es_ES.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] stats4 stats graphics grDevices utils datasets methods
[8] base

other attached packages: [1] patchwork_1.2.0 BSgenome.Hsapiens.UCSC.hg38_1.4.5 [3] BSgenome_1.66.3 rtracklayer_1.58.0
[5] Biostrings_2.66.0 XVector_0.38.0
[7] TFBSTools_1.36.0 JASPAR2020_0.99.10
[9] bigstatsr_1.5.12 RSpectra_0.16-1
[11] irlba_2.3.5.1 Matrix_1.6-4
[13] ggplot2_3.4.4 dplyr_1.1.4
[15] EnsDb.Hsapiens.v86_2.99.0 ensembldb_2.22.0
[17] AnnotationFilter_1.22.0 GenomicFeatures_1.50.4
[19] AnnotationDbi_1.60.2 Biobase_2.58.0
[21] GenomicRanges_1.50.2 GenomeInfoDb_1.34.9
[23] IRanges_2.32.0 S4Vectors_0.36.2
[25] BiocGenerics_0.44.0 Signac_1.12.9003
[27] Seurat_5.0.1 SeuratObject_5.0.1
[29] sp_2.1-2

loaded via a namespace (and not attached): [1] utf8_1.2.4 R.utils_2.12.3
[3] spatstat.explore_3.2-5 reticulate_1.34.0
[5] tidyselect_1.2.0 poweRlaw_0.70.6
[7] RSQLite_2.3.4 htmlwidgets_1.6.4
[9] grid_4.2.2 BiocParallel_1.32.6
[11] Rtsne_0.17 munsell_0.5.0
[13] codetools_0.2-19 ica_1.0-3
[15] future_1.33.1 miniUI_0.1.1.1
[17] withr_2.5.2 spatstat.random_3.2-2
[19] colorspace_2.1-0 progressr_0.14.0
[21] filelock_1.0.3 ROCR_1.0-11
[23] tensor_1.5 listenv_0.9.0
[25] MatrixGenerics_1.10.0 GenomeInfoDbData_1.2.9
[27] polyclip_1.10-6 bit64_4.0.5
[29] parallelly_1.36.0 vctrs_0.6.5
[31] generics_0.1.3 BiocFileCache_2.6.1
[33] doParallel_1.0.17 bigassertr_0.1.6
[35] R6_2.5.1 bitops_1.0-7
[37] spatstat.utils_3.0-4 cachem_1.0.8
[39] DelayedArray_0.24.0 promises_1.2.1
[41] BiocIO_1.8.0 scales_1.3.0
[43] gtable_0.3.4 globals_0.16.2
[45] processx_3.8.3 goftest_1.2-3
[47] spam_2.10-0 seqLogo_1.64.0
[49] rlang_1.1.3 flock_0.7
[51] RcppRoll_0.3.0 splines_4.2.2
[53] lazyeval_0.2.2 spatstat.geom_3.2-7
[55] bigparallelr_0.3.2 yaml_2.3.8
[57] reshape2_1.4.4 abind_1.4-5
[59] httpuv_1.6.13 tools_4.2.2
[61] ellipsis_0.3.2 RColorBrewer_1.1-3
[63] ggridges_0.5.5 Rcpp_1.0.12
[65] plyr_1.8.9 progress_1.2.3
[67] zlibbioc_1.44.0 purrr_1.0.2
[69] RCurl_1.98-1.14 ps_1.7.5
[71] prettyunits_1.2.0 deldir_2.0-2
[73] pbapply_1.7-2 cowplot_1.1.2
[75] zoo_1.8-12 SummarizedExperiment_1.28.0 [77] ggrepel_0.9.4 cluster_2.1.6
[79] magrittr_2.0.3 data.table_1.14.10
[81] scattermore_1.2 lmtest_0.9-40
[83] RANN_2.6.1 ProtGenerics_1.30.0
[85] fitdistrplus_1.1-11 matrixStats_1.2.0
[87] hms_1.1.3 mime_0.12
[89] xtable_1.8-4 XML_3.99-0.16
[91] fastDummies_1.7.3 gridExtra_2.3
[93] compiler_4.2.2 biomaRt_2.54.1
[95] tibble_3.2.1 KernSmooth_2.23-22
[97] crayon_1.5.2 R.oo_1.25.0
[99] htmltools_0.5.7 tzdb_0.4.0
[101] later_1.3.2 tidyr_1.3.0
[103] DBI_1.2.0 dbplyr_2.4.0
[105] MASS_7.3-60 rappdirs_0.3.3
[107] readr_2.1.4 cli_3.6.2
[109] R.methodsS3_1.8.2 parallel_4.2.2
[111] dotCall64_1.1-1 igraph_1.6.0
[113] pkgconfig_2.0.3 TFMPvalue_0.0.9
[115] GenomicAlignments_1.34.1 plotly_4.10.3
[117] spatstat.sparse_3.0-3 xml2_1.3.6
[119] foreach_1.5.2 annotate_1.76.0
[121] DirichletMultinomial_1.40.0 callr_3.7.3
[123] stringr_1.5.1 digest_0.6.34
[125] pracma_2.4.4 sctransform_0.4.1
[127] RcppAnnoy_0.0.21 CNEr_1.34.0
[129] spatstat.data_3.0-3 leiden_0.4.3.1
[131] fastmatch_1.1-4 uwot_0.1.16
[133] restfulr_0.0.15 curl_5.2.0
[135] gtools_3.9.5 shiny_1.8.0
[137] Rsamtools_2.14.0 rjson_0.2.21
[139] lifecycle_1.0.4 nlme_3.1-164
[141] jsonlite_1.8.8 desc_1.4.3
[143] viridisLite_0.4.2 fansi_1.0.6
[145] pillar_1.9.0 lattice_0.22-5
[147] pkgbuild_1.4.3 GO.db_3.16.0
[149] KEGGREST_1.38.0 fastmap_1.1.1
[151] httr_1.4.7 survival_3.5-7
[153] remotes_2.4.2.1 glue_1.7.0
[155] png_0.1-8 iterators_1.0.14
[157] bit_4.0.5 stringi_1.8.3
[159] blob_1.2.4 RcppHNSW_0.5.0
[161] caTools_1.18.2 memoise_2.0.1
[163] future.apply_1.11.1`

RaghadShu commented 8 months ago

I also tried to use older versions of Seurat ("4.4.0") and SeuratObject ("4.1.4").. No luck.. This problem remains even when I'm simply trying to remove an Assay from the seurat object: > seurat[["RNA"]] <- NULL

Error in validObject(object = x) : 
  invalid class "Seurat" object: all cells in reductions must be in the same order as the Seurat object
zskylarli commented 8 months ago

Hi - could you share what your object looks like prior to running the code from the WNN vignette? It seems like there could be an issue occuring before trying to store the assay.

RaghadShu commented 8 months ago

Hi, thanks for the response. Please bear with me because I have played around a lot since I initially posted the question.

I'm not sure what you would like to see in the object, so please let me know if there is something specific. Here is the original object I posted the question about:

image

I was fiddling around with this object, specifically the peakunion assay (which was created as recommended here: https://github.com/stuart-lab/signac/discussions/455), and noticed that it only had 4 Fragment files instead of 5 like the number of samples I had. I went back and ensured the correct fragment paths were supplied, and integrated the ATAC assay again. I am now trying to combine the RNA object with ATAC using WNN like I did many times before, but I keep getting the same error message: Error in validObject(object = x) : invalid class "Seurat" object: all cells in reductions must be in the same order as the Seurat object I also get the same error message when I simply try to remove any assay from the original wnn object, or the newly integerated atac object using something like seurat[["RNA"]] <- NULL. Which is why in my replies, I tried to upgrade (and later downgrade) the versions of the packages.

I'm not understanding the error message or how to check for the order of the cells in the reduction to make sure they match the order of the Seurat object.

I hope this is clear. Sorry for any confusion!

sessionInfo() R version 4.2.2 Patched (2022-11-10 r83330) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 18.04.6 LTS

Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=es_ES.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=es_ES.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=es_ES.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] stats4 stats graphics grDevices utils datasets methods
[8] base

other attached packages: [1] bigstatsr_1.5.12 RSpectra_0.16-1
[3] irlba_2.3.5.1 Matrix_1.6-5
[5] ggplot2_3.4.4 EnsDb.Hsapiens.v86_2.99.0 [7] ensembldb_2.22.0 AnnotationFilter_1.22.0
[9] GenomicFeatures_1.50.4 AnnotationDbi_1.60.2
[11] Biobase_2.58.0 GenomicRanges_1.50.2
[13] GenomeInfoDb_1.34.9 IRanges_2.32.0
[15] S4Vectors_0.36.2 BiocGenerics_0.44.0
[17] Signac_1.12.9003 Seurat_5.0.1.9001
[19] SeuratObject_5.0.1 sp_2.1-2
[21] dplyr_1.1.4

loaded via a namespace (and not attached): [1] utf8_1.2.4 spatstat.explore_3.2-5
[3] reticulate_1.34.0 tidyselect_1.2.0
[5] RSQLite_2.3.4 htmlwidgets_1.6.4
[7] grid_4.2.2 BiocParallel_1.32.6
[9] Rtsne_0.17 munsell_0.5.0
[11] codetools_0.2-19 ica_1.0-3
[13] future_1.33.1 miniUI_0.1.1.1
[15] withr_2.5.2 spatstat.random_3.2-2
[17] colorspace_2.1-0 progressr_0.14.0
[19] filelock_1.0.3 ROCR_1.0-11
[21] tensor_1.5 listenv_0.9.0
[23] MatrixGenerics_1.10.0 GenomeInfoDbData_1.2.9
[25] polyclip_1.10-6 bit64_4.0.5
[27] parallelly_1.36.0 vctrs_0.6.5
[29] generics_0.1.3 BiocFileCache_2.6.1
[31] doParallel_1.0.17 bigassertr_0.1.6
[33] R6_2.5.1 bitops_1.0-7
[35] spatstat.utils_3.0-4 cachem_1.0.8
[37] DelayedArray_0.24.0 promises_1.2.1
[39] BiocIO_1.8.0 scales_1.3.0
[41] gtable_0.3.4 globals_0.16.2
[43] processx_3.8.3 goftest_1.2-3
[45] spam_2.10-0 rlang_1.1.3
[47] flock_0.7 RcppRoll_0.3.0
[49] splines_4.2.2 rtracklayer_1.58.0
[51] lazyeval_0.2.2 spatstat.geom_3.2-7
[53] bigparallelr_0.3.2 yaml_2.3.8
[55] reshape2_1.4.4 abind_1.4-5
[57] httpuv_1.6.13 tools_4.2.2
[59] ellipsis_0.3.2 RColorBrewer_1.1-3
[61] ggridges_0.5.5 Rcpp_1.0.12
[63] plyr_1.8.9 progress_1.2.3
[65] zlibbioc_1.44.0 purrr_1.0.2
[67] RCurl_1.98-1.14 ps_1.7.5
[69] prettyunits_1.2.0 deldir_2.0-2
[71] pbapply_1.7-2 cowplot_1.1.2
[73] zoo_1.8-12 SummarizedExperiment_1.28.0 [75] ggrepel_0.9.5 cluster_2.1.6
[77] magrittr_2.0.3 data.table_1.14.10
[79] scattermore_1.2 lmtest_0.9-40
[81] RANN_2.6.1 ProtGenerics_1.30.0
[83] fitdistrplus_1.1-11 matrixStats_1.2.0
[85] hms_1.1.3 patchwork_1.2.0
[87] mime_0.12 xtable_1.8-4
[89] XML_3.99-0.16 fastDummies_1.7.3
[91] gridExtra_2.3 compiler_4.2.2
[93] biomaRt_2.54.1 tibble_3.2.1
[95] KernSmooth_2.23-22 crayon_1.5.2
[97] htmltools_0.5.7 later_1.3.2
[99] tidyr_1.3.0 DBI_1.2.0
[101] dbplyr_2.4.0 MASS_7.3-60
[103] rappdirs_0.3.3 cli_3.6.2
[105] parallel_4.2.2 dotCall64_1.1-1
[107] igraph_1.6.0 pkgconfig_2.0.3
[109] GenomicAlignments_1.34.1 plotly_4.10.3
[111] spatstat.sparse_3.0-3 foreach_1.5.2
[113] xml2_1.3.6 XVector_0.38.0
[115] stringr_1.5.1 callr_3.7.3
[117] digest_0.6.34 sctransform_0.4.1
[119] RcppAnnoy_0.0.21 spatstat.data_3.0-3
[121] Biostrings_2.66.0 leiden_0.4.3.1
[123] fastmatch_1.1-4 uwot_0.1.16
[125] restfulr_0.0.15 curl_5.2.0
[127] shiny_1.8.0 Rsamtools_2.14.0
[129] rjson_0.2.21 lifecycle_1.0.4
[131] nlme_3.1-164 jsonlite_1.8.8
[133] desc_1.4.3 viridisLite_0.4.2
[135] fansi_1.0.6 pillar_1.9.0
[137] lattice_0.22-5 KEGGREST_1.38.0
[139] fastmap_1.1.1 httr_1.4.7
[141] pkgbuild_1.4.3 survival_3.5-7
[143] glue_1.7.0 remotes_2.4.2.1
[145] iterators_1.0.14 png_0.1-8
[147] bit_4.0.5 stringi_1.8.3
[149] blob_1.2.4 RcppHNSW_0.5.0
[151] memoise_2.0.1 future.apply_1.11.1

RaghadShu commented 8 months ago

Hi, I think I found the issue and the solution for this. During my RNA integration, I put my 5 samples in a list as SMM1 - MGUS1 - MM1 - MM2 - MM3. During my ATAC integration, I fed the samples in as MGUS1 - SMM1 - MM1 - MM2 - MM3. I think this is the source of discrepancy and what caused the cells to be ordered differently in the reductions when I moved forward with my WNN analysis.

A quick fix around this was that I extracted the ATAC reduction rows, and re-ordered them to match the order of the reduction created by RNA. This is the code I used:


# Match order of reduction cells 
rna_df <- as.data.frame(seurat@reductions$pca@cell.embeddings)
atac_df <- as.data.frame(seurat@reductions$integratedLSI@cell.embeddings)
# Match order 
matched_order <- match(rownames(rna_df), rownames(atac_df))
# Reorder atac_df based on the matched order
atac_df_matched <- atac_df[matched_order, , drop = FALSE]
# create a new Dimensionality Reduction object
e <- as.matrix(atac_df_matched)
l <- atac_int@reductions$integrated_lsi@feature.loadings
atac_dmr <- CreateDimReducObject(embeddings = e, loadings = l, assay = "peakunion")
# Add it to atac_int
atac_int@reductions$integrated_lsi <- NULL
atac_int@reductions$integrated_lsi <- atac_dmr

I am closing the issue as this worked with me. Please let me know if you see errors in the suggested solution.

GuiSeSanz commented 7 months ago

Thanks a lot! I was having this same issue and this answer made my day! Thanks for sharing your workaround!

CeliaJuarillo commented 7 months ago

Hi, I have the same problem and I have tried your code @RaghadShu but it doesn't seem to be working for me. I think I don't quite know what object are you using for "atac_int". I only have my Seurat object with the RNA, ATAC and the WNN integration assays. Could you clarify how you obtain the "atac_int"?

RaghadShu commented 7 months ago

Hello @CeliaJuarillo, atac_int is the ATAC chromatin assay. Something like atac_int <- seurat[["ATAC"]]. Hope this helps!

yurasong commented 6 months ago

Hi! I have tried same approach on my 10X multiome suggested by @RaghadShu and it does not seems to be working for my side.

What I tried is:

Match order

matched_order <- match(rownames(rna_df), rownames(atac_df))

Reorder atac_df based on the matched order

atac_df_matched <- atac_df[matched_order, , drop = FALSE]

create a new Dimensionality Reduction object

e <- as.matrix(atac_df_matched) l <- seuset@reductions$lsi@feature.loadings atac_dmr <- CreateDimReducObject(embeddings = e, loadings = l, assay = "peakunion")

Add it to seurat object

seuset@reductions$lsi <- NULL seuset@reductions$lsi <- atac_dmr

What I got are as below: Error in validObject(object = x) : invalid class “Seurat” object: 1: all cells in assays must be in the same order as the Seurat object invalid class “Seurat” object: 2: all cells in assays must be in the same order as the Seurat object invalid class “Seurat” object: 3: all cells in reductions must be in the same order as the Seurat object invalid class “Seurat” object: 4: all cells in reductions must be in the same order as the Seurat object invalid class “Seurat” object: 5: all cells in reductions must be in the same order as the Seurat object invalid class “Seurat” object: 6: all cells in graphs must be in the same order as the Seurat object (offending: RNA_nn) invalid class “Seurat” object: 7: all cells in graphs must be in the same order as the Seurat object (offending: RNA_snn) invalid class “Seurat” object: 8: all cells in graphs must be in the same order as the Seurat object (offending: wknn)