seurat icon indicating copy to clipboard operation
seurat copied to clipboard

Error when subsetting or when running other functions: value' must be a 'matrix' or 'dgCMatrix'

Open chrarnold opened this issue 2 years ago • 18 comments

This issue still exists despite two Issues with the same topic being prematurely closed (#7100 and #7208)

Happens with the newest Seurat v5 and a Seurat object that has been created with Seurat v4, multiome object with RNA and ATAC.

seu.subset.s = subset(seu.s, subset = condition2 == "CD34") Error in LayerData<-(object = *tmp*, layer = i, ..., value = value) : 'value' must be a 'matrix' or 'dgCMatrix'

Same error is thrown here, probably the same origin:

seu.s <- Signac::FindTopFeatures(seu.s, min.cutoff = 'q0')

Nothing works, I tried all suggestions from the other 2 closed Issues, looks like a new bug in v5 that should be addressed as it worked just fine in v4.

chrarnold avatar Aug 15 '23 13:08 chrarnold

Can you share session info? Previously this was related to Signac versioning issues which is why those issues were closed.

Does seu.s have a chromatin assay?

Gesmira avatar Aug 16 '23 20:08 Gesmira

Yes, it does, the error only comes when a chromatin assay is present afaik.

I tried to subset the Seurat object to make it available here, but even that fails with the same error message:

seu2.s = seu.s[1:100,1:00] Error in LayerData<-(object = *tmp*, layer = i, ..., value = value) : 'value' must be a 'matrix' or 'dgCMatrix'

I can provide the full object, it is a 900 MB qs compressed Seurat object if needed (but not sure I am comfortable sharing it here online, a private option may be better).

This is an error that many in my group stumble upon and should really be addressed, thank you!

Here a sessionInfo():

R version 4.2.2 (2022-10-31) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Rocky Linux 8.8 (Green Obsidian)

Matrix products: default BLAS/LAPACK: /g/easybuild/x86_64/Rocky/8/haswell/software/FlexiBLAS/3.2.1-GCC-12.2.0/lib64/libflexiblas.so.3.2

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

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

other attached packages: [1] Signac_1.9.0 Seurat_4.9.9.9059 SeuratObject_4.9.9.9091 sp_1.6-0 biomaRt_2.54.0

loaded via a namespace (and not attached): [1] backports_1.4.1 spam_2.9-1 circlize_0.4.15 fastmatch_1.1-3 BiocFileCache_2.6.1
[6] plyr_1.8.8 igraph_1.4.1 lazyeval_0.2.2 splines_4.2.2 RcppHNSW_0.4.1
[11] BiocParallel_1.32.5 listenv_0.9.0 scattermore_1.2 GenomeInfoDb_1.34.9 ggplot2_3.4.1
[16] digest_0.6.31 ensembldb_2.22.0 htmltools_0.5.4 fansi_1.0.4 magrittr_2.0.3
[21] checkmate_2.1.0 memoise_2.0.1 tensor_1.5 cluster_2.1.4 ROCR_1.0-11
[26] globals_0.16.2 Biostrings_2.66.0 matrixStats_0.63.0 spatstat.sparse_3.0-1 prettyunits_1.1.1
[31] colorspace_2.1-0 blob_1.2.4 rappdirs_0.3.3 ggrepel_0.9.3 dplyr_1.1.0
[36] crayon_1.5.2 RCurl_1.98-1.10 jsonlite_1.8.4 spatstat.data_3.0-1 progressr_0.13.0
[41] survival_3.5-5 zoo_1.8-11 glue_1.6.2 polyclip_1.10-4 gtable_0.3.1
[46] zlibbioc_1.44.0 XVector_0.38.0 leiden_0.4.3 DelayedArray_0.24.0 future.apply_1.10.0
[51] shape_1.4.6 BiocGenerics_0.44.0 abind_1.4-5 scales_1.2.1 DBI_1.1.3
[56] spatstat.random_3.1-4 miniUI_0.1.1.1 Rcpp_1.0.10 viridisLite_0.4.1 xtable_1.8-4
[61] progress_1.2.2 reticulate_1.28 bit_4.0.5 dotCall64_1.0-2 stats4_4.2.2
[66] htmlwidgets_1.6.1 httr_1.4.5 RColorBrewer_1.1-3 ellipsis_0.3.2 ica_1.0-3
[71] pkgconfig_2.0.3 XML_3.99-0.13 uwot_0.1.16 deldir_1.0-6 dbplyr_2.3.1
[76] utf8_1.2.3 reshape2_1.4.4 tidyselect_1.2.0 rlang_1.1.0 later_1.3.0
[81] AnnotationDbi_1.60.2 munsell_0.5.0 tools_4.2.2 cachem_1.0.7 cli_3.6.0
[86] generics_0.1.3 RSQLite_2.3.0 ggridges_0.5.4 stringr_1.5.0 fastmap_1.1.1
[91] goftest_1.2-3 yaml_2.3.7 bit64_4.0.5 fitdistrplus_1.1-8 purrr_1.0.1
[96] RANN_2.6.1 KEGGREST_1.38.0 AnnotationFilter_1.22.0 nlme_3.1-162 pbapply_1.7-0
[101] future_1.32.0 mime_0.12 RcppRoll_0.3.0 xml2_1.3.3 compiler_4.2.2
[106] rstudioapi_0.14 plotly_4.10.1 filelock_1.0.2 curl_5.0.0 png_0.1-8
[111] spatstat.utils_3.0-2 tibble_3.2.0 stringi_1.7.12 GenomicFeatures_1.50.4 RSpectra_0.16-1
[116] lattice_0.20-45 ProtGenerics_1.30.0 Matrix_1.5-3 vctrs_0.6.0 pillar_1.8.1
[121] lifecycle_1.0.3 spatstat.geom_3.1-0 lmtest_0.9-40 GlobalOptions_0.1.2 RcppAnnoy_0.0.21
[126] data.table_1.14.8 cowplot_1.1.1 bitops_1.0-7 irlba_2.3.5.1 httpuv_1.6.9
[131] patchwork_1.1.2 rtracklayer_1.58.0 GenomicRanges_1.50.2 R6_2.5.1 BiocIO_1.8.0
[136] promises_1.2.0.1 gridExtra_2.3 KernSmooth_2.23-20 IRanges_2.32.0 parallelly_1.34.0
[141] codetools_0.2-19 fastDummies_1.6.3 MASS_7.3-58.3 SummarizedExperiment_1.28.0 rjson_0.2.21
[146] sctransform_0.3.5 GenomicAlignments_1.34.1 Rsamtools_2.14.0 S4Vectors_0.36.2 GenomeInfoDbData_1.2.9
[151] parallel_4.2.2 hms_1.1.2 grid_4.2.2 tidyr_1.3.0 MatrixGenerics_1.10.0
[156] Rtsne_0.16 spatstat.explore_3.1-0 Biobase_2.58.0 shiny_1.7.4 restfulr_0.0.15

chrarnold avatar Aug 23 '23 09:08 chrarnold

I also tried with the newest Signac version from the dev branch ('1.10.9001'), same error

chrarnold avatar Aug 23 '23 10:08 chrarnold

@Gesmira A completely different object, also with a chromatin assay, has the same error, triggered here:

subset(seu.s, subset = nCount_RNA > 223) Error in LayerData<-(object = *tmp*, layer = i, ..., value = value) : 'value' must be a 'matrix' or 'dgCMatrix'

Here, it fails, BUT the next works:

subset(seu.s, subset = nCount_RNA > 222) An object of class Seurat 402666 features across 100 samples within 2 assays Active assay: RNA (27510 features, 0 variable features) 2 layers present: counts, data 1 other assay present: ATAC

This object I can subset but weirdly, the ATAC assay seems to be gone then:

seu.s An object of class Seurat 402666 features across 100 samples within 2 assays Active assay: RNA (27510 features, 0 variable features) 2 layers present: counts, data 1 other assay present: ATAC

seu.s[1:100,1:100] An object of class Seurat 100 features across 100 samples within 1 assay Active assay: RNA (100 features, 0 variable features) 2 layers present: counts, data

Object is here (very small 30 MB), maybe it helps finding the root cause: https://www.dropbox.com/scl/fi/1u3yp0wkps6tqp48b9ltz/seuratObj.subset100.qs?rlkey=2nv63qr6ow0hbf23cbh7xvlry&dl=0 Can be read into R like this: seu.s= qs::qread(...)

chrarnold avatar Aug 23 '23 11:08 chrarnold

Error also happens when using Seuratv5 with BPCells, in this context:

counts.mat.RNA <- open_matrix_dir(dir = "bla") seu.s["RNA"]]$counts <- counts.mat.RNA

Error in LayerData<-: ! 'value' must be a 'matrix' or 'dgCMatrix' Run rlang::last_trace() to see where the error occurred.

rlang::last_trace() <error/rlang_error> Error in LayerData<-: ! 'value' must be a 'matrix' or 'dgCMatrix'


Backtrace: ▆

  1. ├─base::$<-(*tmp*, counts, value = <MatrixDr[,100]>)
  2. └─SeuratObject:::$<-.Assay(*tmp*, counts, value = <MatrixDr[,100]>)
  3. ├─SeuratObject::LayerData<-(object = *tmp*, layer = i, value = value)
  4. └─SeuratObject:::LayerData<-.Assay(object = *tmp*, layer = i, value = value)
  5. └─rlang::abort(message = "'value' must be a 'matrix' or 'dgCMatrix'")
    

chrarnold avatar Aug 23 '23 11:08 chrarnold

It seems like you have only tried with the master and develop branch based on your versions. For Seurat v5, you should use the "seurat5" branch of Signac. Have you tried installing this version of Signac? If not, restart R and install like so: remotes::install_github("stuart-lab/signac", "seurat5")

Gesmira avatar Aug 23 '23 13:08 Gesmira

See one of my later comments, I also re-installed Signac from the dev branch ('1.10.9001'), same error

chrarnold avatar Aug 23 '23 13:08 chrarnold

You could try downloading the object, it is very small (30 MB) and should recapitulate the error

chrarnold avatar Aug 23 '23 13:08 chrarnold

Not the develop branch, the "seurat5" branch: version "1.11.9000". The code to install this branch is included in my last comment.

When I download your object, I can not reproduce the error if I have the seurat5 branch of Signac installed:

> subset(seu.s, subset = nCount_RNA > 223)
An object of class Seurat 
402666 features across 99 samples within 2 assays 
Active assay: RNA (27510 features, 0 variable features)
 2 layers present: counts, data
 1 other assay present: ATAC

relevant versions:

Signac_1.11.9000        Seurat_4.9.9.9058              SeuratObject_4.9.9.9091                          

Gesmira avatar Aug 23 '23 14:08 Gesmira

Thanks for the quick reply, I did miss this distinction. I actually did have this version installed before but then tried also the dev branch.

The specific error related to subset(seu.s, subset = nCount_RNA > 223) seems to work now, but this still happens:

seurat_cocultures = qs::qread("seuratObj.subset100.qs") write_matrix_dir(mat = seurat_cocultures[["RNA"]]$counts, dir = "counts.RNA", overwrite = TRUE) counts.mat.RNA <- open_matrix_dir(dir = "counts.RNA") seurat_cocultures[["RNA"]]$counts <- counts.mat.RNA

Error in LayerData<-: ! 'value' must be a 'matrix' or 'dgCMatrix'


Backtrace: ▆

  1. ├─base::$<-(*tmp*, counts, value = <MatrixDr[,100]>)
  2. └─SeuratObject:::$<-.Assay(*tmp*, counts, value = <MatrixDr[,100]>)
  3. ├─SeuratObject::LayerData<-(object = *tmp*, layer = i, value = value)
  4. └─SeuratObject:::LayerData<-.Assay(object = *tmp*, layer = i, value = value)
  5. └─rlang::abort(message = "'value' must be a 'matrix' or 'dgCMatrix'")
    

Session Info:

R version 4.2.2 (2022-10-31) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Rocky Linux 8.8 (Green Obsidian)

Matrix products: default BLAS/LAPACK: /g/easybuild/x86_64/Rocky/8/haswell/software/FlexiBLAS/3.2.1-GCC-12.2.0/lib64/libflexiblas.so.3.2

locale: ...

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

other attached packages: [1] BPCells_0.1.0 Signac_1.11.9000 Seurat_4.9.9.9059 SeuratObject_4.9.9.9091 sp_1.6-0 GRaNIE_1.5.2
[7] biomaRt_2.54.0 GenomeInfoDb_1.34.9 IRanges_2.32.0 S4Vectors_0.36.2 BiocGenerics_0.44.0 lubridate_1.9.2
[13] forcats_1.0.0 stringr_1.5.0 dplyr_1.1.0 purrr_1.0.1 readr_2.1.4 tidyr_1.3.0
[19] tibble_3.2.0 ggplot2_3.4.1 tidyverse_2.0.0
...

chrarnold avatar Aug 23 '23 14:08 chrarnold

Ah, ok this is a separate issue due to the fact that "RNA" is a v3 assay, not an Assay5 class. You can convert it to an Assay5 with the following code: seu.s[["RNA"]] <- as(seu.s[["RNA"]], "Assay5"). You can only set BPCells matrices as the counts in v5 Assays, so hopefully this should fix that issue.

Gesmira avatar Aug 23 '23 14:08 Gesmira

I still also have the "missing" ATAC modality still:

ATAC modality still here:

test.s An object of class Seurat 402666 features across 100 samples within 2 assays Active assay: RNA (27510 features, 0 variable features) 2 layers present: counts, data 1 other assay present: ATAC

Here it goes missing:

test.s[1:100,1:100] An object of class Seurat 100 features across 100 samples within 1 assay Active assay: RNA (100 features, 0 variable features) 2 layers present: counts, data

chrarnold avatar Aug 23 '23 14:08 chrarnold

I believe this is due to the fact that the first 100 rownames are from your RNA Assay, so they are not present in the ATAC assay. For example, this works:

seu.s[,1:100] An object of class Seurat 402666 features across 100 samples within 2 assays Active assay: RNA (27510 features, 0 variable features) 2 layers present: counts, data 1 other assay present: ATAC

But the rownames you are pulling out don't belong to the ATAC data so it is removed.

Gesmira avatar Aug 23 '23 14:08 Gesmira

Thank you for this fix, this part works now indeed! Maybe this could be added to the main branch to "force" it into an Assay5 structure, or at least a check or warning in case it is not.

If ok with you, let's keep the Issue open for another week than I close it, I will try to test and see whether all occurences of this error are now gone, thank you!

chrarnold avatar Aug 23 '23 14:08 chrarnold

You're right, we can put a more informative error message there. Sure, that's fine!

Gesmira avatar Aug 23 '23 14:08 Gesmira

I believe this is due to the fact that the first 100 rownames are from your RNA Assay, so they are not present in the ATAC assay. For example, this works:

seu.s[,1:100] An object of class Seurat 402666 features across 100 samples within 2 assays Active assay: RNA (27510 features, 0 variable features) 2 layers present: counts, data 1 other assay present: ATAC

But the rownames you are pulling out don't belong to the ATAC data so it is removed.

I see, that makes sense, I didnt realize this option! All good then for now, thanks for clarifying! I think also in this case, a "warning" message should be issued that an assay has been removed because of the subsetting. It is surprising for users to have the modality suddenly missing although I understand it makes sense.

chrarnold avatar Aug 23 '23 14:08 chrarnold

Encountering the same issue here:

Signac: 1.12.0 Seurat: 5.0.0 SeuratObject: 5.0.0

Attempting to assign a new assay to the Seurat object:

Seurat_merge_multiome[["RNA"]] <- as(Seurat_merge_multiome[["RNA"]], "Assay5")

Trying to split the Seurat object by donor:

multiome_list <- SplitObject(Seurat_merge_multiome, split.by = "donor")

However, an error occurs during the execution of the SplitObject function:

Error in LayerData<-(object = *tmp*, layer = i, ..., value = value) :

'value' must be a 'matrix' or 'dgCMatrix' in v3 Assays, not a ‘data.frame’

Further attempts to merge and recreate the data fail:

Error in [<-.data.frame(*tmp*, , i, value = new("Seurat", assays = list( :

replacement has 36601 rows, data has 149064

All of these issues occurred after upgrading to Seurat5!

GhamsariReza avatar Nov 13 '23 10:11 GhamsariReza

@mreza-ef Can you provide a small reproducible example?

Gesmira avatar Mar 01 '24 20:03 Gesmira

Closing now due to lack of response. Please feel free to reopen if issues persist!

Gesmira avatar May 29 '24 15:05 Gesmira