dynverse / anndata

Annotated multivariate observation data in R
https://anndata.dynverse.org
Other
43 stars 4 forks source link

Error in h5ad loading: invalid class “dgRMatrix” object: 'Dim' slot does not have length 2 #25

Closed pati-ni closed 9 months ago

pati-ni commented 1 year ago

Hi,

the package is non functional in my setup when I try to load an h5ad file:

> sc = anndata::read_h5ad("~/Downloads/pbmc_1k_protein_v3.h5ad")
> sc$X
Error in validObject(.Object) :
  invalid class “dgRMatrix” object: 'Dim' slot does not have length 2

My sessionInfo():

sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Arch Linux

Matrix products: default
BLAS:   /usr/lib/libblas.so.3.11.0
LAPACK: /usr/lib/liblapack.so.3.11.0

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

time zone: America/New_York
tzcode source: system (glibc)

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

loaded via a namespace (and not attached):
 [1] assertthat_0.2.1 R6_2.5.1         Matrix_1.5-4.1   lattice_0.21-8
 [5] reticulate_1.30  rappdirs_0.3.3   png_0.1-8        cli_3.6.1
 [9] grid_4.3.1       withr_2.5.0      pbdZMQ_0.3-9     compiler_4.3.1
[13] rprojroot_2.0.3  here_1.0.1       Rcpp_1.0.10      rlang_1.1.1
[17] jsonlite_1.8.7   anndata_0.7.5.6

and my python 3.10 with anndata version 0.9.1

mmoisse commented 1 year ago

I have the same issue with the small example

 X <- as(matrix(1:6, nrow = 2), "dgCMatrix")
  obs <- data.frame(group = c("a", "b"), row.names = c("s1", "s2"))
  var <- data.frame(type = c(1L, 2L, 3L), row.names = c("var1", "var2", "var3"))

  ad <- anndata::AnnData(
    X = X,
    obs = obs,
    var = var,
    layers = list(
      spliced = matrix(4:9, nrow = 2),
      unspliced = matrix(8:13, nrow = 2)
    ),
    obsm = list(
      ones = matrix(rep(1L, 10), nrow = 2),
      rand = matrix(rnorm(6), nrow = 2),
      zeros = matrix(rep(0L, 10), nrow = 2)
    ),
    varm = list(
      ones = matrix(rep(1L, 12), nrow = 3),
      rand = matrix(rnorm(6), nrow = 3),
      zeros = matrix(rep(0L, 12), nrow = 3)
    ),
    uns = list(
      a = 1,
      b = data.frame(i = 1:3, j = 4:6, value = runif(3)),
      c = list(c.a = 3, c.b = 4)
    )
  )

With the following error,

ad$X
Error in validObject(.Object) : 
  invalid class “dgCMatrix” object: 'Dim' slot does not have length 2
rcannood commented 1 year ago

It seems like scipy renamed a few things again (see https://github.com/rstudio/reticulate/blame/main/R/conversion.R#L649-L656).

Please downgrade scipy to <1.11 or upgrade reticulate to the latest development version by running remotes::install_github("rstudio/reticulate").

Can you confirm this solves your problem?

mmoisse commented 1 year ago

Upgrade reticulate to the latest development version worked for me

zhangdae commented 9 months ago

@rcannood As your advice,I upgrade the reticulate.It`s actually solved my problem,and brought a new error

> data <- CreateSeuratObject(counts = t(data$X), meta.data = data$obs)
Error in t.default(data$X) : argument is not a matrix

It can be solved by add as.matrix to data$X

> data <- CreateSeuratObject(counts = t(as.matrix(data$X)), meta.data = data$obs)
Warning message:
In asMethod(object) :
  sparse->dense coercion: allocating vector of size 9.2 GiB
rcannood commented 9 months ago

Happy to hear this has solved your issue!

With respect to the newest error: this is because the $X in this case seems to be a sparse matrix. If you want to transpose a sparse matrix, you first need to call library(Matrix) or use Matrix::t(data$X) instead. This is usually more efficient than converting it to a dense matrix.

Can you confirm whether this also solves your problem?

I'm closing this issue since the original problem was solved. Feel free to open up a new issue for other problems that may occur :)

zhangdae commented 9 months ago

Thanks for your replying. After calling library(Matrix) at first,no error reports

> library(anndata)
> library(Matrix)
> library(Seurat)
Attaching SeuratObject
> data <- read_h5ad("ARHL_lyaer_counts_harmonyed.h5ad")
> data
AnnData object with n_obs × n_vars = 61322 × 20180
    obs: 'batch', 'nUMIs', 'mito_perc', 'detected_genes', 'cell_complexity', 'n_genes', 'doublet_score', 'predicted_doublet', 'passing_mt', 'passing_nUMIs', 'passing_ngenes'
    var: 'gene_ids', 'feature_types', 'mt', 'n_cells', 'percent_cells', 'robust', 'mean', 'var', 'residual_variances', 'highly_variable_rank', 'highly_variable_features'
    uns: 'batch_colors', 'hvg', 'layers_counts', 'layers_raw', 'log1p', 'neighbors', 'scaled|original|cum_sum_eigenvalues', 'scaled|original|pca_var_ratios', 'scrublet', 'umap'
    obsm: 'X_harmony', 'X_pca', 'X_umap', 'scaled|original|X_pca'
    obsp: 'connectivities', 'distances'
> Matrix::rowMeans(data$X[1:10,])
AAACCCAAGAGGGTGG-1 AAACCCACAAATGATG-1 AAACCCACACGGTAGA-1 AAACCCACAGCTTCGG-1 AAACCCACATGGAATA-1 AAACCCAGTCGGATTT-1 AAACCCAGTCTTCATT-1 AAACCCATCAGTGATC-1 
        0.10138751         0.06541130         0.04479683         0.07571853         0.12462834         0.02730426         0.14400396         0.07507433 
AAACCCATCTCTGCTG-1 AAACGAAAGCGAGAAA-1 
        0.14335976         0.07735382 
> data1 <- CreateSeuratObject(counts = t(data$X), meta.data = data$obs)
> data2 <- CreateSeuratObject(counts = Matrix::t(data$X), meta.data = data$obs)

no error/warning appears,and more efficient than use as.matrix It perfectly works