GreenleafLab / ArchR

ArchR : Analysis of Regulatory Chromatin in R (www.ArchRProject.com)
MIT License
387 stars 140 forks source link

Unconstrained integration error - "more elements supplied than there are to replace" #2136

Open LacquerHed opened 8 months ago

LacquerHed commented 8 months ago

Hi I am running unconstrained integration and getting the following error, which seems to stem from the slots/layers ArchR is pulling from the seRNA (Seurat object) input.

2024-03-14 12:11:53.67366 : Block (1 of 1) : Imputing GeneScoreMatrix, 0.323 mins elapsed. Getting ImputeWeights Warning: Data is of class dgeMatrix. Coercing to dgCMatrix. Error in slot(object = object, name = "features")[[layer]] <- features : more elements supplied than there are to replace

This is happening with Seurat V5 objects but also V4 when I downgraded to that, so not sure if its the new layer structure of Seurat V5. Not sure how to limit elements supplied from the seRNA. Thanks.

ArchR-addGeneIntegrationMatrix-129a04b8342d3-Date-2024-03-14_Time-12-11-30.735514.log

sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.3.1
Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
Random number generation:
 RNG:     L'Ecuyer-CMRG 
 Normal:  Inversion 
 Sample:  Rejection 
 locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: America/New_York
tzcode source: internal
attached base packages:
 [1] parallel  stats4    grid      stats     graphics  grDevices utils     datasets  methods   base     
other attached packages:
 [1] harmony_1.2.0                      uwot_0.1.16                        nabor_0.5.0                       
 [4] Rsamtools_2.18.0                   BSgenome.Mmusculus.UCSC.mm10_1.4.3 BSgenome_1.70.2                   
 [7] rtracklayer_1.62.0                 BiocIO_1.12.0                      Biostrings_2.70.2                 
[10] XVector_0.42.0                     rhdf5_2.46.1                       SummarizedExperiment_1.32.0       
[13] Biobase_2.62.0                     MatrixGenerics_1.14.0              Rcpp_1.0.12                       
[16] Matrix_1.6-5                       GenomicRanges_1.54.1               GenomeInfoDb_1.38.7               
[19] IRanges_2.36.0                     S4Vectors_0.40.2                   BiocGenerics_0.48.1               
[22] matrixStats_1.2.0                  data.table_1.15.2                  stringr_1.5.1                     
[25] plyr_1.8.9                         magrittr_2.0.3                     gtable_0.3.4                      
[28] gtools_3.9.5                       gridExtra_2.3                      ArchR_1.0.2                       
[31] sctransform_0.4.1                  ggplot2_3.5.0                      dplyr_1.1.4                       
[34] Seurat_5.0.2                       SeuratObject_5.0.1                 sp_2.1-3                          
loaded via a namespace (and not attached):
  [1] RcppAnnoy_0.0.22          splines_4.3.2             later_1.3.2               bitops_1.0-7             
  [5] tibble_3.2.1              R.oo_1.26.0               polyclip_1.10-6           XML_3.99-0.16.1          
  [9] fastDummies_1.7.3         lifecycle_1.0.4           doParallel_1.0.17         globals_0.16.3           
 [13] lattice_0.22-5            MASS_7.3-60.0.1           limma_3.58.1              plotly_4.10.4            
 [17] yaml_2.3.8                httpuv_1.6.14             glmGamPoi_1.14.3          spam_2.10-0              
 [21] spatstat.sparse_3.0-3     reticulate_1.35.0         cowplot_1.1.3             pbapply_1.7-2            
 [25] RColorBrewer_1.1-3        abind_1.4-5               zlibbioc_1.48.0           Rtsne_0.17               
 [29] purrr_1.0.2               presto_1.0.0              R.utils_2.12.3            RCurl_1.98-1.14          
 [33] circlize_0.4.16           GenomeInfoDbData_1.2.11   ggrepel_0.9.5             irlba_2.3.5.1            
 [37] listenv_0.9.1             spatstat.utils_3.0-4      pheatmap_1.0.12           goftest_1.2-3            
 [41] RSpectra_0.16-1           spatstat.random_3.2-3     fitdistrplus_1.1-11       parallelly_1.37.1        
 [45] DelayedMatrixStats_1.24.0 leiden_0.4.3.1            codetools_0.2-19          DelayedArray_0.28.0      
 [49] tidyselect_1.2.1          shape_1.4.6.1             farver_2.1.1              spatstat.explore_3.2-6   
 [53] GenomicAlignments_1.38.2  jsonlite_1.8.8            GetoptLong_1.0.5          ellipsis_0.3.2           
 [57] progressr_0.14.0          ggridges_0.5.6            survival_3.5-8            iterators_1.0.14         
 [61] foreach_1.5.2             tools_4.3.2               ica_1.0-3                 glue_1.7.0               
 [65] SparseArray_1.2.4         withr_3.0.0               fastmap_1.1.1             rhdf5filters_1.14.1      
 [69] fansi_1.0.6               digest_0.6.35             R6_2.5.1                  mime_0.12                
 [73] colorspace_2.1-0          Cairo_1.6-2               scattermore_1.2           tensor_1.5               
 [77] spatstat.data_3.0-4       R.methodsS3_1.8.2         RhpcBLASctl_0.23-42       utf8_1.2.4               
 [81] tidyr_1.3.1               generics_0.1.3            httr_1.4.7                htmlwidgets_1.6.4        
 [85] S4Arrays_1.2.1            pkgconfig_2.0.3           ComplexHeatmap_2.18.0     lmtest_0.9-40            
 [89] htmltools_0.5.7           dotCall64_1.1-1           clue_0.3-65               scales_1.3.0             
 [93] png_0.1-8                 rstudioapi_0.15.0         reshape2_1.4.4            rjson_0.2.21             
 [97] nlme_3.1-164              zoo_1.8-12                GlobalOptions_0.1.2       KernSmooth_2.23-22       
[101] miniUI_0.1.1.1            vipor_0.4.7               restfulr_0.0.15           ggrastr_1.0.2            
[105] pillar_1.9.0              vctrs_0.6.5               RANN_2.6.1                promises_1.2.1           
[109] xtable_1.8-4              cluster_2.1.6             beeswarm_0.4.0            cli_3.6.2                
[113] compiler_4.3.2            rlang_1.1.3               crayon_1.5.2              future.apply_1.11.1      
[117] labeling_0.4.3            ggbeeswarm_0.7.2          stringi_1.8.3             BiocParallel_1.36.0      
[121] viridisLite_0.4.2         deldir_2.0-4              munsell_0.5.0             lazyeval_0.2.2           
[125] spatstat.geom_3.2-9       RcppHNSW_0.6.0            patchwork_1.2.0           sparseMatrixStats_1.14.0 
[129] future_1.33.1             Rhdf5lib_1.24.2           statmod_1.5.0             shiny_1.8.0              
[133] ROCR_1.0-11               igraph_2.0.3`
rcorces commented 8 months ago

Hi @LacquerHed! Thanks for using ArchR! Lately, it has been very challenging for me to keep up with maintenance of this package and all of my other responsibilities as a PI. I have not been responding to issue posts and I have not been pushing updates to the software. We are actively searching to hire a computational biologist to continue to develop and maintain ArchR and related tools. If you know someone who might be a good fit, please let us know! In the meantime, your issue will likely go without a reply. Most issues with ArchR right not relate to compatibility. Try reverting to R 4.1 and Bioconductor 3.15. Newer versions of Seurat and Matrix also are causing issues. Sorry for not being able to provide active support for this package at this time.

zvittorio commented 6 months ago

Hi @LacquerHed

I had the same error, in the same step. This is my code (the part that generates the error)

seRNA = readRDS("scRNA-Hematopoiesis-Granja-2019.rds")
seurObj = CreateSeuratObject(counts = CreateAssayObject(assay(seRNA)), meta.data = as.data.frame(colData(seRNA)))

Project = addGeneIntegrationMatrix(
    ArchRProj = Project, 
    useMatrix = "GeneScoreMatrix",
    matrixName = "GeneIntegrationMatrix",
    reducedDims = "IterativeLSI",
    seRNA = seurObj,
    addToArrow = FALSE,
    groupRNA = "BioClassification", 
    nameCell = "predictedCell_Un", 
    nameGroup = "predictedGroup_Un",
    nameScore = "predictedScore_Un"
)

I was running on R-4.3.2 with ArchR-1.0.2. Reverting to an older R version (4.2.2) as suggested by @rcorces does not generate the error and the function runs smoothly. I hope this helps, also on the dev side for further fixes ;)

jiangpuxuan commented 5 months ago

Try update your seurat to 5 , archR to 1.0.3 and bioconductor to 3.19

And try to run Rscript in your shell rather than using rstudio !

jiangpuxuan commented 5 months ago

Hi @LacquerHed

I had the same error, in the same step. This is my code (the part that generates the error)

seRNA = readRDS("scRNA-Hematopoiesis-Granja-2019.rds")
seurObj = CreateSeuratObject(counts = CreateAssayObject(assay(seRNA)), meta.data = as.data.frame(colData(seRNA)))

Project = addGeneIntegrationMatrix(
    ArchRProj = Project, 
    useMatrix = "GeneScoreMatrix",
    matrixName = "GeneIntegrationMatrix",
    reducedDims = "IterativeLSI",
    seRNA = seurObj,
    addToArrow = FALSE,
    groupRNA = "BioClassification", 
    nameCell = "predictedCell_Un", 
    nameGroup = "predictedGroup_Un",
    nameScore = "predictedScore_Un"
)

I was running on R-4.3.2 with ArchR-1.0.2. Reverting to an older R version (4.2.2) as suggested by @rcorces does not generate the error and the function runs smoothly. I hope this helps, also on the dev side for further fixes ;)

I have once been doubted by the same error but it works with latest seurat archr bioconductor.

KyriakisDimitrios commented 5 months ago

Getting the same error with the latest seurat and archR

R version 4.2.2 (as suggested)
Seurat_5.1.0                
SeuratObject_5.0.2
ArchR_1.0.2    
2024-06-07 10:13:24 : Block (1 of 67) : Imputing GeneScoreMatrix, 2.501 mins elapsed.
Getting ImputeWeights
Warning: Data is of class dgeMatrix. Coercing to dgCMatrix.
Error in slot(object = object, name = "features")[[layer]] <- features : 
  more elements supplied than there are to replace
Calls: addGeneIntegrationMatrix ... .CreateStdAssay.list -> LayerData<- -> LayerData<-.Assay5
Execution halted
KyriakisDimitrios commented 5 months ago

Hi @LacquerHed I had the same error, in the same step. This is my code (the part that generates the error)

seRNA = readRDS("scRNA-Hematopoiesis-Granja-2019.rds")
seurObj = CreateSeuratObject(counts = CreateAssayObject(assay(seRNA)), meta.data = as.data.frame(colData(seRNA)))

Project = addGeneIntegrationMatrix(
    ArchRProj = Project, 
    useMatrix = "GeneScoreMatrix",
    matrixName = "GeneIntegrationMatrix",
    reducedDims = "IterativeLSI",
    seRNA = seurObj,
    addToArrow = FALSE,
    groupRNA = "BioClassification", 
    nameCell = "predictedCell_Un", 
    nameGroup = "predictedGroup_Un",
    nameScore = "predictedScore_Un"
)

I was running on R-4.3.2 with ArchR-1.0.2. Reverting to an older R version (4.2.2) as suggested by @rcorces does not generate the error and the function runs smoothly. I hope this helps, also on the dev side for further fixes ;)

I have once been doubted by the same error but it works with latest seurat archr bioconductor.

Can you share the exact version?

Here is mine R version 4.2.2 (as suggested) Seurat_5.1.0
SeuratObject_5.0.2 ArchR_1.0.2

And still, I get the same error. Thanks

zvittorio commented 5 months ago

Hi @KyriakisDimitrios

We later found out that the change that fixed the problem was actually reverting Seurat (and SeuratObject) to version 4.3, which in our case came together with reverting R version automatically, since we are using software modules on a cluster. With this change, we were able to keep the latest R version but with older Seurat.

jiangpuxuan commented 5 months ago

Guys you should test addintegratedmatrix step by step some function in addintegratedmatrix from Seurat should use v4 and some v5

wgao688 commented 5 months ago

Hi @KyriakisDimitrios , I'm wondering if you solved this issue since I am also experiencing the same issue despite the suggestions here. I have tried using an SCE and Seurat object but it results the same error. Thanks!

KyriakisDimitrios commented 5 months ago

Hi @KyriakisDimitrios , I'm wondering if you solved this issue since I am also experiencing the same issue despite the suggestions here. I have tried using an SCE and Seurat object but it results the same error. Thanks!

Hi @wgao688, I solved the issue using @jiangpuxuan's suggestion. I ran the function step by step and identified the problem in the 4th step: Begin Integration. This step is split into 4 substeps. I used Seurat v5 for steps 1 and 2 (Create Seurat RNA and Normalize, Get Gene Score Matrix, and Create Seurat ATAC) and Seurat v4 for steps 3 and 4 (Transfer Anchors, Transfer Data).

ZhangFuchang commented 5 months ago

After repeated testing, I finally know the answer to this question:

This error occurred in the RNAIntegration.R of the ArchR package This code in the RNAIntegration.R file: seuratATAC <- Seurat::CreateSeuratObject(counts = mat[head(seq_len(nrow(mat)), 5), , drop = FALSE])

The Seurat::CreateSeuratObject function requires that the rownames of mat must be vector, not matrix. But the rownames passed to mat in the previous code was actually a matrix, so you only need to add a command line before this code, this will cast the rownames of mat to a vector:

rownames(mat) <- as.character(rownames(mat))

And then this error was completely eliminated.

ZhangFuchang commented 5 months ago

After repeated testing, I finally know the answer to this question:

This error occurred in the RNAIntegration.R of the ArchR package This code in the RNAIntegration.R file: seuratATAC <- Seurat::CreateSeuratObject(counts = mat[head(seq_len(nrow(mat)), 5), , drop = FALSE])

The Seurat::CreateSeuratObject function requires that the rownames of the mat must be vector, not matrix. But the rownames passed to the mat in the previous code was actually a matrix, so you only need to add a command line before this code, this will cast the rownames of mat to a vector:

rownames(mat) <- as.character(rownames(mat))

And then this error was completely eliminated.

joeyQX commented 4 months ago

After repeated testing, I finally know the answer to this question:

This error occurred in the RNAIntegration.R of the ArchR package This code in the RNAIntegration.R file: seuratATAC <- Seurat::CreateSeuratObject(counts = mat[head(seq_len(nrow(mat)), 5), , drop = FALSE])

The Seurat::CreateSeuratObject function requires that the rownames of mat must be vector, not matrix. But the rownames passed to mat in the previous code was actually a matrix, so you only need to add a command line before this code, this will cast the rownames of mat to a vector:

rownames(mat) <- as.character(rownames(mat))

And then this error was completely eliminated.

Thxs but where is the RNAIntegration.R file? How to modify the code of the addGeneIntegrationMatrix function? I try to edit the code of ArchR but didn't figure out.

ZhangFuchang commented 4 months ago

After repeated testing, I finally know the answer to this question: This error occurred in the RNAIntegration.R of the ArchR package This code in the RNAIntegration.R file: seuratATAC <- Seurat::CreateSeuratObject(counts = mat[head(seq_len(nrow(mat)), 5), , drop = FALSE]) The Seurat::CreateSeuratObject function requires that the rownames of mat must be vector, not matrix. But the rownames passed to mat in the previous code was actually a matrix, so you only need to add a command line before this code, this will cast the rownames of mat to a vector: rownames(mat) <- as.character(rownames(mat)) And then this error was completely eliminated.

Thxs but where is the RNAIntegration.R file? How to modify the code of the addGeneIntegrationMatrix function? I try to edit the code of ArchR but didn't figure out.

I guess you should have installed the ArchR package on Windows or the compiled ArchR package on Linux. So I suggest you download the source code package first, and never download the compiled package. Then enter to the directory of the source code of ArchR: ArchR/R/IntegrativeAnalysis.R Modify it according to the method I proposed earlier. Then install the modified source code again. As for the method of installing from the source code, please search on Google or Baidu yourself.

joeyQX commented 4 months ago

After repeated testing, I finally know the answer to this question: This error occurred in the RNAIntegration.R of the ArchR package This code in the RNAIntegration.R file: seuratATAC <- Seurat::CreateSeuratObject(counts = mat[head(seq_len(nrow(mat)), 5), , drop = FALSE]) The Seurat::CreateSeuratObject function requires that the rownames of mat must be vector, not matrix. But the rownames passed to mat in the previous code was actually a matrix, so you only need to add a command line before this code, this will cast the rownames of mat to a vector: rownames(mat) <- as.character(rownames(mat)) And then this error was completely eliminated.

Thxs but where is the RNAIntegration.R file? How to modify the code of the addGeneIntegrationMatrix function? I try to edit the code of ArchR but didn't figure out.

I guess you should have installed the ArchR package on Windows or the compiled ArchR package on Linux. So I suggest you download the source code package first, and never download the compiled package. Then enter to the directory of the source code of ArchR: ArchR/R/IntegrativeAnalysis.R Modify it according to the method I proposed earlier. Then install the modified source code again. As for the method of installing from the source code, please search on Google or Baidu yourself.

Thanks! It works!

jiangpuxuan commented 4 months ago

Hi Guys I would like to provide another kind of situation THAT do not suit the solution rownames(mat) <- as.character(rownames(mat)) above.

Because the mat ( in addGeneIntegrationMatrix() 308 line ) DO NOT have i p, so mat cannot be SeuratV5:: createSeuratobject-ed.

here is the wrong format: image

here is the correct format for seuratV5::createseuratobject.: image

So, someone could add i p to mat , or just convert mat from dgcmatrix to data.frame.

# LINE 308 in  `addGeneIntegrationMatrix()` 
#seuratATAC <- Seurat::CreateSeuratObject(counts = mat[head(seq_len(nrow(mat)), 
            5), , drop = FALSE])
tmp <- as.data.frame(mat[head(seq_len(nrow(mat)), 5), 
            , drop = FALSE])
seuratATAC <- Seurat::CreateSeuratObject(counts = tmp)

That's OK.

TroyLi99 commented 4 months ago

Hi Guys I would like to provide another kind of situation THAT do not suit the solution rownames(mat) <- as.character(rownames(mat)) above.

  • ArchR1.0.3 Seurat5.1 ( because the seurat 4.4cannot FindTransferAnchors() correctly )
  • My ArchR project was build at ArchR1.0.2 and Seurat object was build at Seurat4.
  • My error is also 'more elements supplied than there are to replace' in Seurat::CreateSeuratObject.

Because the mat ( in addGeneIntegrationMatrix() 308 line ) DO NOT have i p, so mat cannot be SeuratV5:: createSeuratobject-ed.

here is the wrong format: image

here is the correct format for seuratV5::createseuratobject.: image

So, someone could add i p to mat , or just convert mat from dgcmatrix to data.frame.

# LINE 308 in  `addGeneIntegrationMatrix()` 
#seuratATAC <- Seurat::CreateSeuratObject(counts = mat[head(seq_len(nrow(mat)), 
            5), , drop = FALSE])
tmp <- as.data.frame(mat[head(seq_len(nrow(mat)), 5), 
            , drop = FALSE])
seuratATAC <- Seurat::CreateSeuratObject(counts = tmp)

That's OK.

May I know this mat file is generated by your scRNA data? I am running the test data they provided, the dataset doesn't look like yours.

jiangpuxuan commented 4 months ago

May I know this mat file is generated by your scRNA data? I am running the test data they provided, the dataset doesn't look like yours.

the mat file is generated by addGeneIntegration function

markphillippebworth commented 1 month ago

For those still hitting this issue, I've generated a branch of ArchR with this patch implemented (along with additional helpful functions) here: If you're not familiar, you can clone this and use devtools to install it, or simply load it in (devtools::load_all() ) over your existing ArchR installation. https://github.com/aifimmunology/ArchR