satijalab / seurat

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

SCTransform fails with error: no applicable method for 'as.sparse' applied to an object of class "c('double', 'numeric')" #8912

Closed nigiord closed 4 months ago

nigiord commented 5 months ago

I have the following Seurat object (v5)

An object of class Seurat 
23515 features across 10001 samples within 1 assay 
Active assay: RNA (23515 features, 0 variable features)
 1 layer present: counts

that I simply try to normalize using the following command

seuratobj <-  Seurat::SCTransform(seuratobj, vst.flavor = "v2", vars.to.regress = "percent.mt", min_cells = 5, verbose = TRUE)

but after computing everything, it fails at the end with:

Error in UseMethod(generic = "as.sparse", object = x) :
no applicable method for 'as.sparse' applied to an object of class "c('double', 'numeric')"

I only have this issue with this particular object (it's one sample among dozens, after multiple filtering of barcodes). The issue is reproducible on multiple platform (HPC cluster, locally on linux, locally on osx-64). I tried to update Matrix, Seurat and SeuratObject with no effect. I think there might be something wrong with my object but I'm unsure what, it was build in my pipeline the same way then all the others. Googling the error did not help much, and I tried diving into Seurat and sctransform code base but no clue so far.

The object is only 50 MB so I created a link at the end of this issue to download it.

Extra information

Complete log:

Running SCTransform on assay: RNA
Running SCTransform on layer: counts
vst.flavor='v2' set. Using model with fixed slope and excluding poisson genes.
Variance stabilizing transformation of count matrix of size 23515 by 10001
Model formula is y ~ log_umi
Get Negative Binomial regression parameters per gene
Using 2000 genes, 5000 cells
Found 168 outliers - those will be ignored in fitting/regularization step

Second step: Get residuals using fitted parameters for 23515 genes
Computing corrected count matrix for 23515 genes
Calculating gene attributes
Wall clock passed: Time difference of 35.39095 secs
Determine variable features
Regressing out percent.mt
  |======================================================================| 100%
Centering data matrix
  |======================================================================| 100%
Getting residuals for block 1(of 3) for counts dataset
Getting residuals for block 2(of 3) for counts dataset
Getting residuals for block 3(of 3) for counts dataset
Error in UseMethod(generic = "as.sparse", object = x) :
  no applicable method for 'as.sparse' applied to an object of class "c('double', 'numeric')"

sessionInfo()

R version 4.3.2 (2023-10-31)
Platform: x86_64-conda-linux-gnu (64-bit)
Running under: Debian GNU/Linux trixie/sid

Matrix products: default
BLAS/LAPACK: <snip>.miniconda3/envs/scrnaseq-analysis/lib/libopenblasp-r0.3.27.so;  LAPACK version 3.12.0

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

time zone: Europe/Paris
tzcode source: system (glibc)

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

other attached packages:
[1] Seurat_5.0.1       SeuratObject_5.0.1 sp_2.1-4          

loaded via a namespace (and not attached):
  [1] RColorBrewer_1.1-3          jsonlite_1.8.8             
  [3] magrittr_2.0.3              spatstat.utils_3.0-4       
  [5] zlibbioc_1.48.0             vctrs_0.6.5                
  [7] ROCR_1.0-11                 DelayedMatrixStats_1.24.0  
  [9] spatstat.explore_3.2-6      RCurl_1.98-1.14            
 [11] htmltools_0.5.8.1           S4Arrays_1.2.0             
 [13] SparseArray_1.2.2           sctransform_0.4.1          
 [15] parallelly_1.37.1           KernSmooth_2.23-24         
 [17] htmlwidgets_1.6.4           ica_1.0-3                  
 [19] plyr_1.8.9                  plotly_4.10.4              
 [21] zoo_1.8-12                  igraph_2.0.3               
 [23] mime_0.12                   lifecycle_1.0.4            
 [25] pkgconfig_2.0.3             Matrix_1.6-5               
 [27] R6_2.5.1                    fastmap_1.2.0              
 [29] GenomeInfoDbData_1.2.11     MatrixGenerics_1.14.0      
 [31] fitdistrplus_1.1-11         future_1.33.2              
 [33] shiny_1.8.1.1               digest_0.6.35              
 [35] colorspace_2.1-0            patchwork_1.2.0            
 [37] S4Vectors_0.40.2            tensor_1.5                 
 [39] RSpectra_0.16-1             irlba_2.3.5.1              
 [41] GenomicRanges_1.54.1        progressr_0.14.0           
 [43] fansi_1.0.6                 spatstat.sparse_3.0-3      
 [45] httr_1.4.7                  polyclip_1.10-6            
 [47] abind_1.4-5                 compiler_4.3.2             
 [49] fastDummies_1.7.3           MASS_7.3-60                
 [51] DelayedArray_0.28.0         tools_4.3.2                
 [53] lmtest_0.9-40               httpuv_1.6.15              
 [55] future.apply_1.11.2         goftest_1.2-3              
 [57] glmGamPoi_1.14.0            glue_1.7.0                 
 [59] nlme_3.1-164                promises_1.3.0             
 [61] grid_4.3.2                  Rtsne_0.17                 
 [63] cluster_2.1.6               reshape2_1.4.4             
 [65] generics_0.1.3              gtable_0.3.5               
 [67] spatstat.data_3.0-4         tidyr_1.3.1                
 [69] data.table_1.15.2           utf8_1.2.4                 
 [71] XVector_0.42.0              BiocGenerics_0.48.1        
 [73] spatstat.geom_3.2-9         RcppAnnoy_0.0.22           
 [75] ggrepel_0.9.5               RANN_2.6.1                 
 [77] pillar_1.9.0                stringr_1.5.1              
 [79] spam_2.10-0                 RcppHNSW_0.6.0             
 [81] later_1.3.2                 splines_4.3.2              
 [83] dplyr_1.1.4                 lattice_0.22-6             
 [85] survival_3.6-4              deldir_2.0-4               
 [87] tidyselect_1.2.1            miniUI_0.1.1.1             
 [89] pbapply_1.7-2               gridExtra_2.3              
 [91] IRanges_2.36.0              SummarizedExperiment_1.32.0
 [93] scattermore_1.2             stats4_4.3.2               
 [95] Biobase_2.62.0              matrixStats_1.0.0          
 [97] stringi_1.8.4               lazyeval_0.2.2             
 [99] codetools_0.2-20            tibble_3.2.1               
[101] cli_3.6.2                   uwot_0.1.16                
[103] xtable_1.8-4                reticulate_1.36.1          
[105] munsell_0.5.1               Rcpp_1.0.12                
[107] GenomeInfoDb_1.38.1         globals_0.16.3             
[109] spatstat.random_3.2-3       png_0.1-8                  
[111] parallel_4.3.2              ggplot2_3.5.0              
[113] dotCall64_1.1-1             sparseMatrixStats_1.14.0   
[115] bitops_1.0-7                listenv_0.9.1              
[117] viridisLite_0.4.2           scales_1.3.0               
[119] ggridges_0.5.6              leiden_0.4.3.1             
[121] purrr_1.0.2                 crayon_1.5.2               
[123] rlang_1.1.3                 cowplot_1.1.3    

Reproducible example

Download the Rds object [49.3 MB] here and run:

library(Seurat)
seuratobj <- readRDS("DUMMYNAME.scrnaseq.filtered-only-tumor-cells.seurat-object.Rds")
Seurat::SCTransform(seuratobj, vst.flavor = "v2", vars.to.regress = "percent.mt", min_cells = 5, verbose = TRUE)

Any help would be greatly appreciated!

Cheers, −Nils

zskylarli commented 4 months ago

Hi! I tried to debug your object, and it seems like because there are exactly 10001 cells, the last single cell is being put into a separate third block when calculating residuals, since the number of subsampled cells used to build the nonbinomial regression is set to 5000 by default. If you just set the ncells argument to another number, i.e. 3000, this should be fixed. Let me know if you have additional questions!

Seurat::SCTransform(seuratobj, vst.flavor = "v2", vars.to.regress = "percent.mt", min_cells = 5, verbose = TRUE, ncells=3000)

nigiord commented 4 months ago

@zskylarli I see, that’s really a nasty corner case. Thank you for your help! I would suggest adding a simple test such as:

colnames(seuratobj) %% ncells >= min_cells

to save other users from the really confusing error message (hopefully they should end up here if they google it though).

Cheers,