satijalab / seurat

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

! Cannot add new cells with [[<- #7445

Closed elisulvaran closed 1 year ago

elisulvaran commented 1 year ago

Hello! Firstly, thank you so much for creating such an incredible and useful tool. I have a ChromatinAssay object that I would like to add to my Seurat object. I was not sure on what the best way of doing this was, but I followed this section of a signac tutorial

cells_select <- intersect(colnames(ChromatinAssayObject), colnames(SeuratObject))
SeuratObject[["ChromatinAssay"]] <- CreateChromatinAssay(counts = ChromatinAssayObject@counts,
                                                     sep = c(":", "-"), 
                                                     genome = "hg38", 
                                                     fragments = Fragments(ChromatinAssayObject),
                                                     annotation = Annotation(ChromatinAssayObject),
                                                     cells = cells_select)

This is throwing the following error:

Error in `[[<-`:
! Cannot add new cells with [[<-
Backtrace:
 1. methods (local) `[[<-`(`*tmp*`, "ChromatinAssay", value = `<ChrmtnAs[,69020]>`)
 2. SeuratObject (local) `[[<-`(`*tmp*`, "ChromatinAssay", value = `<ChrmtnAs[,69020]>`)

I don't really know how to proceed with this. Here's my info session:

R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.2.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
 [1] SingleR_2.0.0               SummarizedExperiment_1.28.0
 [3] Biobase_2.58.0              GenomicRanges_1.50.2       
 [5] GenomeInfoDb_1.34.9         IRanges_2.32.0             
 [7] S4Vectors_0.36.2            BiocGenerics_0.44.0        
 [9] MatrixGenerics_1.10.0       matrixStats_1.0.0          
[11] scooter_0.0.0.9004          harmony_0.1.1              
[13] Rcpp_1.0.10                 sctransform_0.3.5          
[15] patchwork_1.1.2             ggplot2_3.4.2              
[17] Signac_1.10.0               Seurat_4.9.9.9045          
[19] SeuratObject_4.9.9.9084     sp_1.6-1                   
[21] edgeR_3.40.2                limma_3.54.2               

loaded via a namespace (and not attached):
  [1] rappdirs_0.3.3                scattermore_1.1              
  [3] tidyr_1.3.0                   bit64_4.0.5                  
  [5] knitr_1.43                    irlba_2.3.5.1                
  [7] DelayedArray_0.24.0           data.table_1.14.8            
  [9] KEGGREST_1.38.0               RCurl_1.98-1.12              
 [11] doParallel_1.0.17             generics_0.1.3               
 [13] ScaledMatrix_1.6.0            cowplot_1.1.1                
 [15] RSQLite_2.3.1                 RANN_2.6.1
 [17] future_1.32.0                 bit_4.0.5                    
 [19] tzdb_0.4.0                    spatstat.data_3.0-1          
 [21] xml2_1.3.4                    httpuv_1.6.11                
 [23] ggsci_3.0.0                   xfun_0.39                    
 [25] celldex_1.8.0                 hms_1.1.3                    
 [27] evaluate_0.21                 promises_1.2.0.1             
 [29] fansi_1.0.4                   progress_1.2.2               
 [31] dbplyr_2.3.2                  readxl_1.4.2                 
 [33] igraph_1.4.3                  DBI_1.1.3                    
 [35] htmlwidgets_1.6.2             spatstat.geom_3.2-1          
 [37] purrr_1.0.1                   ellipsis_0.3.2               
 [39] RSpectra_0.16-1               dplyr_1.1.2                  
 [41] backports_1.4.1               deldir_1.0-9                 
 [43] sparseMatrixStats_1.10.0      vctrs_0.6.2                  
 [45] SingleCellExperiment_1.20.1   ROCR_1.0-11                  
 [47] abind_1.4-5                   cachem_1.0.8                 
 [49] withr_2.5.0                   progressr_0.13.0             
 [51] checkmate_2.2.0               prettyunits_1.1.1            
 [53] scran_1.26.2                  goftest_1.2-3                
 [55] cluster_2.1.4                 ExperimentHub_2.6.0          
 [57] dir.expiry_1.6.0              dotCall64_1.0-2              
 [59] lazyeval_0.2.2                crayon_1.5.2                 
 [61] basilisk.utils_1.11.1         spatstat.explore_3.2-1       
 [63] pkgconfig_2.0.3               nlme_3.1-162                 
 [65] rlang_1.1.1                   globals_0.16.2               
 [67] lifecycle_1.0.3               miniUI_0.1.1.1               
 [69] filelock_1.0.2                fastDummies_1.6.3            
 [71] BiocFileCache_2.6.1           rsvd_1.0.5                   
 [73] AnnotationHub_3.6.0           cellranger_1.1.0        
 [75] polyclip_1.10-4               RcppHNSW_0.4.1               
 [77] lmtest_0.9-40                 Matrix_1.5-4.1               
 [79] zoo_1.8-12                    ggridges_0.5.4               
 [81] GlobalOptions_0.1.2           png_0.1-8                    
 [83] viridisLite_0.4.2             rjson_0.2.21                 
 [85] bitops_1.0-7                  KernSmooth_2.23-21           
 [87] spam_2.9-1                    Biostrings_2.66.0            
 [89] blob_1.2.4                    DelayedMatrixStats_1.20.0    
 [91] shape_1.4.6                   stringr_1.5.0                
 [93] parallelly_1.36.0             spatstat.random_3.1-5        
 [95] readr_2.1.4                   beachmat_2.14.2              
 [97] scales_1.2.1                  memoise_2.0.1                
 [99] magrittr_2.0.3                plyr_1.8.8                   
[101] ica_1.0-3                     zlibbioc_1.44.0              
[103] compiler_4.2.1                dqrng_0.3.0                  
[105] RColorBrewer_1.1-3            clue_0.3-64                  
[107] fitdistrplus_1.1-11           Rsamtools_2.14.0             
[109] cli_3.6.1                     XVector_0.38.0               
[111] listenv_0.9.0                 pbapply_1.7-0                
[113] MASS_7.3-60                   tidyselect_1.2.0             
[115] liana_0.1.9                   stringi_1.7.12               
[117] yaml_2.3.7                    BiocSingular_1.14.0          
[119] locfit_1.5-9.7                ggrepel_0.9.3                
[121] grid_4.2.1                    fastmatch_1.1-3              
[123] tools_4.2.1                   future.apply_1.11.0          
[125] parallel_4.2.1                circlize_0.4.15              
[127] rstudioapi_0.14               bluster_1.8.0                
[129] foreach_1.5.2                 metapod_1.6.0  
[131] gridExtra_2.3                 Rtsne_0.16                   
[133] digest_0.6.31                 BiocManager_1.30.20          
[135] shiny_1.7.4                   scuttle_1.8.4                
[137] BiocVersion_3.16.0            later_1.3.1                  
[139] RcppAnnoy_0.0.20              httr_1.4.6                   
[141] AnnotationDbi_1.60.2          ComplexHeatmap_2.14.0        
[143] colorspace_2.1-0              rvest_1.0.3                  
[145] tensor_1.5                    reticulate_1.29              
[147] splines_4.2.1                 uwot_0.1.14                  
[149] RcppRoll_0.3.0                statmod_1.5.0                
[151] OmnipathR_3.7.0               spatstat.utils_3.0-3         
[153] basilisk_1.11.2               plotly_4.10.2                
[155] xtable_1.8-4                  jsonlite_1.8.5               
[157] R6_2.5.1                      pillar_1.9.0                 
[159] htmltools_0.5.5               mime_0.12                    
[161] glue_1.6.2                    fastmap_1.1.1                
[163] BiocParallel_1.32.6           BiocNeighbors_1.16.0         
[165] interactiveDisplayBase_1.36.0 codetools_0.2-19             
[167] utf8_1.2.3                    lattice_0.21-8               
[169] spatstat.sparse_3.0-1         tibble_3.2.1                 
[171] logger_0.2.2                  curl_5.0.0                   
[173] leiden_0.4.3                  survival_3.5-5               
[175] rmarkdown_2.22                munsell_0.5.0                
[177] GetoptLong_1.0.5              GenomeInfoDbData_1.2.9       
[179] iterators_1.0.14              reshape2_1.4.4               
[181] gtable_0.3.3  

Thank you!

mhkowalski commented 1 year ago

Hi, have you tried subsetting SeuratObject so that it only contains in the intersecting cells, or cells_select?

elisulvaran commented 1 year ago

Hi! Yes, so the ChromatinAssay was the object that had cells that were not present in the Seurat object, that's why I had to intersect them using:

cells_select <- intersect(colnames(ChromatinAssayObject), colnames(SeuratObject))

This is how my ChromatinAssay looks like

> ChromatinAssayObject
ChromatinAssay data with 171904 features for 26024 cells
Variable features: 0 
Genome: hg38 
Annotation present: TRUE 
Motifs present: FALSE 
Fragment files: 26 

And this is how my Seurat object looks like

> SeuratObject
An object of class Seurat 
239771 features across 26024 samples within 3 assays 
Active assay: RNA (36601 features, 2000 variable features)
 2 layers present: counts, data
 2 other assays present: ATAC, SCT
 5 dimensional reductions calculated: pca, umap.rna, lsi, umap.atac, wnn.umap

As you can see, they both have the same number of samples. I'm not sure if the problem is arising due to the number of features, but my ATAC and my RNA assay in the SeuratObject have a different number of features, so I don't think that's why the error is showing up.

Thanks!

yuhanH commented 1 year ago

hi @elisulvaran I don't think CreateChromatinAssay usually use cells as the parameter. You can directly subset your counts matrix.

SeuratObject[["ChromatinAssay"]] <- CreateChromatinAssay(counts = ChromatinAssayObject@counts[,cells_select],
                                                     sep = c(":", "-"), 
                                                     genome = "hg38", 
                                                     fragments = Fragments(ChromatinAssayObject),
                                                     annotation = Annotation(ChromatinAssayObject))