Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReadH5AD error: object of type 'environment' is not subsettable #2485

Closed
tkisss opened this issue Jan 8, 2020 · 38 comments
Closed

ReadH5AD error: object of type 'environment' is not subsettable #2485

tkisss opened this issue Jan 8, 2020 · 38 comments
Labels
more-information-needed We need more information before this can be addressed

Comments

@tkisss
Copy link

tkisss commented Jan 8, 2020

When I used ReadH5AD to read a .h5ad file generated from scanpy, there was an error:

Pulling expression matrices and metadata
Data is unscaled
Error in file[["obs"]][]: object of type 'environment' is not subsettable
Traceback:

and my code is adata = ReadH5AD(file ="../adata_raw.h5ad",verbose = TRUE)

adata_raw.h5ad information:
AnnData object with n_obs × n_vars = 52049 × 19760
obs: 'donor', 'disease', 'tissue', 'celltype', 'name', 'protocol', 'gse_id', 'n_genes'
var: 'n_cells'

When I used ReadH5AD to read a smaller adata.h5ad(15704 x 14895) file, it did not report an error.
AnnData object with n_obs × n_vars = 15704 × 14895
obs: 'batch', 'celltype', 'disease', 'donor', 'library', 'name', 'protocol', 'gse_id', 'n_genes'
var: 'n_cells'

Does anyone give me some advice? Thanks!

@sergiopolid
Copy link

I'm having the same error:
Error in file[["obs"]][]: object of type 'environment' is not subsettable

sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS/LAPACK: /n/app/openblas/0.2.19/lib/libopenblas_core2p-r0.2.19.so

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

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

other attached packages:
[1] Seurat_3.0.2

loaded via a namespace (and not attached):
[1] nlme_3.1-137 tsne_0.1-3 bitops_1.0-6
[4] bit64_0.9-7 RColorBrewer_1.1-2 httr_1.4.1
[7] sctransform_0.2.1 tools_3.5.1 backports_1.1.5
[10] R6_2.4.0 irlba_2.3.3 KernSmooth_2.23-15
[13] lazyeval_0.2.2 colorspace_1.4-1 npsurv_0.4-0
[16] tidyselect_0.2.5 gridExtra_2.3 bit_1.1-14
[19] compiler_3.5.1 hdf5r_1.3.0 plotly_4.9.1
[22] caTools_1.17.1.2 scales_1.0.0 lmtest_0.9-37
[25] ggridges_0.5.1 pbapply_1.4-2 stringr_1.4.0
[28] digest_0.6.22 R.utils_2.9.0 pkgconfig_2.0.3
[31] htmltools_0.4.0 bibtex_0.4.2 htmlwidgets_1.5.1
[34] rlang_0.4.1 zoo_1.8-6 jsonlite_1.6
[37] ica_1.0-2 gtools_3.8.1 dplyr_0.8.3
[40] R.oo_1.23.0 magrittr_1.5 Matrix_1.2-17
[43] Rcpp_1.0.3 munsell_0.5.0 ape_5.3
[46] reticulate_1.13 lifecycle_0.1.0 R.methodsS3_1.7.1
[49] stringi_1.4.3 gbRd_0.4-11 MASS_7.3-51.4
[52] gplots_3.0.1.1 Rtsne_0.15 plyr_1.8.4
[55] grid_3.5.1 parallel_3.5.1 gdata_2.18.0
[58] listenv_0.7.0 ggrepel_0.8.1 crayon_1.3.4
[61] lattice_0.20-38 cowplot_1.0.0 splines_3.5.1
[64] SDMTools_1.1-221.1 zeallot_0.1.0 pillar_1.4.2
[67] igraph_1.2.4.1 future.apply_1.3.0 reshape2_1.4.3
[70] codetools_0.2-16 glue_1.3.1 lsei_1.2-0
[73] metap_1.1 data.table_1.12.6 vctrs_0.2.0
[76] png_0.1-7 Rdpack_0.11-0 gtable_0.3.0
[79] RANN_2.6.1 purrr_0.3.3 tidyr_1.0.0
[82] future_1.15.0 assertthat_0.2.1 ggplot2_3.2.1
[85] rsvd_1.0.2 survival_3.1-6 viridisLite_0.3.0
[88] tibble_2.1.3 cluster_2.0.7-1 globals_0.12.4
[91] fitdistrplus_1.0-14 ROCR_1.0-7

@lsundaram
Copy link

Not sure if this is the fix, but i faced the same error but the error was because the h5ad was constructed in scanpy 1.4.5 . when i downgraded to scanpy 1.4.4.post1 ( which worked fine before) and recreated the h5ad, there is no error

@thomas-davis
Copy link

Also getting this error, on both scanpy 1.4.5 and scanpy 1.4.4.post1 on Seurat 3.0.2

@apblair
Copy link

apblair commented Feb 5, 2020

Hoping to up the priority for this fix. I'm also receiving the error for scanpy 1.4.5 and Seurat 3.0.2.

@lrafa
Copy link

lrafa commented Feb 5, 2020

I was having this problem until few minutes ago.
Some files were working with ReadH5AD, some weren't. I figured out that the files which were working were saved with an older version of of anndata.

So I downgraded anndata-0.7.1 to 0.6.22.post1, recomputed the data and overwrote the files. Now seurat reads it properly. I'm using Seurat-3.1.2 btw.

@kmuench
Copy link

kmuench commented Feb 11, 2020

I'm also getting this error for scanpy 1.4.5 and Seurat 3.1.0:

Pulling expression matrices and metadata
Data is scaled
Error in file[["obs"]][] : 
  object of type 'environment' is not subsettable

I tried changing my install of anndata by running:
pip install anndata==0.6.22.post1
...and then re-exporting the file and re-testing ReadH5AD.

I then get this new output:

Pulling expression matrices and metadata
Data is scaled
Error in UseMethod(generic = "as.sparse", object = x) : 
  no applicable method for 'as.sparse' applied to an object of class "c('H5D', 'H5RefClass', 'R6')"

Installed X11, upgraded to Seurat 3.1.2 using

devtools::install_version(package='Seurat', version=package_version('3.1.2')
But I'm still getting the same 'as.sparse' error from above when I try to use H5AD.
Hope this helps

@lrafa
Copy link

lrafa commented Feb 11, 2020

@kmuench
Did you copy the anndata data (while it was still sparse) into the .raw attribute before scaling and saving it? E.g.

adata = load_sparse_data
adata.raw = adata
sc.pp.scale(adata)
adata.write('myfile.h5ad')

@kmuench
Copy link

kmuench commented Feb 11, 2020

Ahh, I didn't explicitly copy anything into raw, but I'm wondering if my issue is that I'm importing one of the test datasets? (Development of Myeloid Progenitors)

I'm trying to save the output of the DCA pipeline (https://github.com/theislab/dca) which may not be affecting the same data slots as pp.scale:

import scanpy as sc
paul15_ae = sc.datasets.paul15()
sc.pp.filter_genes(paul15_ae, min_counts=1)
dca(paul15_ae, threads=1)
sc.pp.normalize_per_cell(paul15_ae)
sc.pp.log1p(paul15_ae)
sc.pp.pca(paul15_ae)

import anndata as an
an.AnnData.write(paul15_ae, filename = 'paul15_ae.h5ad')

@asn32
Copy link

asn32 commented Feb 15, 2020

Hi, has anyone come up with a solution to this? I'm having the same issue.

@tiancaigg
Copy link

same issue.

@apblair
Copy link

apblair commented Feb 26, 2020

I found if you set ReadH5AD's argument layers=FALSE the error goes away.

@Acribbs
Copy link

Acribbs commented Feb 26, 2020

Unfortunately not for me.

I still get

adata = ReadH5AD(file ="./adata.h5ad", layers=FALSE)

Pulling expression matrices and metadata
Data is unscaled
Error in file[["obs"]][] : 
  object of type 'environment' is not subsettable

@satijalab
Copy link
Collaborator

Apologies for the delay in responding. Please can you post the h5ad file (you can post a link for us to download, we would suggest making the object as small as possible to facilitate debugging), and we will take a look

@satijalab satijalab reopened this Mar 5, 2020
@andrewwbutler andrewwbutler added the more-information-needed We need more information before this can be addressed label Mar 5, 2020
@franciscogrisanti
Copy link

@satijalab

This toy dataset recreates the error -->

(https://www.dropbox.com/s/c6kz82kcy6w0wbw/test.h5ad?dl=0)

Thanks in advance!

Errror ->

Error in file[["obs"]][]: object of type 'environment' is not subsettable
Traceback:

1. ReadH5AD(file = "../../data/test.h5ad")
2. ReadH5AD.character(file = "../../data/test.h5ad")
3. ReadH5AD(file = hfile, assay = assay, layers = layers, verbose = verbose, 
 .     ...)
4. ReadH5AD.H5File(file = hfile, assay = assay, layers = layers, 
 .     verbose = verbose, ...)

@dweemx
Copy link

dweemx commented Mar 6, 2020

Regarding #2373,
This line seems to raise the error:

dims = rev(x = hdf5r::h5attr(x = x, which = 'h5sparse_shape'))

There is a mismatch of dimension shapes between indptr and rev(x = hdf5r::h5attr(x = x, which = 'h5sparse_shape'))

> file[['raw.X']]
Class: H5Group
Group: /raw.X
Attributes: h5sparse_format, h5sparse_shape
Listing:
    name    obj_type dataset.dims dataset.type_class
    data H5I_DATASET     33518842          H5T_FLOAT
 indices H5I_DATASET     33518842        H5T_INTEGER
  indptr H5I_DATASET         8960        H5T_INTEGER
> rev(x = hdf5r::h5attr(x = file[['raw.X']], which = 'h5sparse_shape'))
[1]  8959 12241

This solves the issue (removing the dims parameter):

sparseMat <- Matrix::sparseMatrix(
  i = x[['indices']][] + 1,
  p = x[['indptr']][],
  x = x[['data']][]
)

Although,I currently don't have a clue why the dimension shapes do not match

N.B.: Loading the same H5AD file in Scanpy works fine

@franciscogrisanti
Copy link

franciscogrisanti commented Mar 6, 2020

Hi @dweemx

I tried that fix but it didn't work out either. Could you check my approach to see if it match yours?

Thanks in advance!

I created a custom "as.sparse.H5Group"

custom_as.sparse.H5Group <- function(x, ...) {
  CheckDots(...)
  for (i in c('data', 'indices', 'indptr')) {
    if (!x$exists(name = i) || !is(object = x[[i]], class2 = 'H5D')) {
      stop("Invalid H5Group specification for a sparse matrix, missing dataset ", i)
    }
  }
  if ('h5sparse_shape' %in% hdf5r::h5attr_names(x = x)) {
    return(Matrix::sparseMatrix(
      i = x[['indices']][] + 1,
      p = x[['indptr']][],
      x = x[['data']][]
      #dims = rev(x = hdf5r::h5attr(x = file[['raw.X']], which = 'h5sparse_shape'))
    ))
  }
  return(Matrix::sparseMatrix(
    i = x[['indices']][] + 1,
    p = x[['indptr']][],
    x = x[['data']][]
  ))
}

Set the function up and running

environment(custom_as.sparse.H5Group) <- asNamespace('Seurat')
assignInNamespace("as.sparse.H5Group", custom_as.sparse.H5Group, ns = "Seurat")

Check is correct

getAnywhere(as.sparse.H5Group)

A single object matching ‘as.sparse.H5Group’ was found
It was found in the following places
  registered S3 method for as.sparse from namespace Seurat
  namespace:Seurat
with value

function(x, ...) {
  CheckDots(...)
  for (i in c('data', 'indices', 'indptr')) {
    if (!x$exists(name = i) || !is(object = x[[i]], class2 = 'H5D')) {
      stop("Invalid H5Group specification for a sparse matrix, missing dataset ", i)
    }
  }
  if ('h5sparse_shape' %in% hdf5r::h5attr_names(x = x)) {
    return(Matrix::sparseMatrix(
      i = x[['indices']][] + 1,
      p = x[['indptr']][],
      x = x[['data']][]
      #dims = rev(x = hdf5r::h5attr(x = file[['raw.X']], which = 'h5sparse_shape'))
    ))
  }
  return(Matrix::sparseMatrix(
    i = x[['indices']][] + 1,
    p = x[['indptr']][],
    x = x[['data']][]
  ))
}
<environment: namespace:Seurat>

But I get again while loading -->

test <- ReadH5AD(file = '../../data/test.h5ad')

Error in file[["obs"]][]: object of type 'environment' is not subsettable
Traceback:

1. ReadH5AD(file = "../../data/test.h5ad")
2. ReadH5AD.character(file = "../../data/test.h5ad")
3. ReadH5AD(file = hfile, assay = assay, layers = layers, verbose = verbose, 
 .     ...)
4. ReadH5AD.H5File(file = hfile, assay = assay, layers = layers, 
 .     verbose = verbose, ...)

@dweemx
Copy link

dweemx commented Mar 9, 2020

Hey @franciscogrisanti,
I just tested your solution, it's working for me!

@franciscogrisanti
Copy link

Thanks for the prompt response @dweemx

Could you provide me your scanpy/seurat version?

Thanks again!

Mine are ->

For scanpy

scanpy==1.4.5.1 anndata==0.7.1 umap==0.3.10 numpy==1.17.3 scipy==1.4.1 pandas==0.25.3 scikit-learn==0.22.1 statsmodels==0.11.0 python-igraph==0.7.1 louvain==0.6.1
For Seurat

R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Catalina 10.15

Matrix products: default
BLAS/LAPACK: /Users/franciscogrisanti/anaconda3/envs/seurat/lib/R/lib/libRblas.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] parallel  stats4    stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] patchwork_1.0.0             loomR_0.2.1.9000           
 [3] hdf5r_1.3.1                 R6_2.4.1                   
 [5] scater_1.14.0               ggplot2_3.2.1              
 [7] SingleCellExperiment_1.8.0  SummarizedExperiment_1.16.0
 [9] DelayedArray_0.12.0         BiocParallel_1.20.0        
[11] matrixStats_0.55.0          Biobase_2.46.0             
[13] GenomicRanges_1.38.0        GenomeInfoDb_1.22.0        
[15] IRanges_2.20.0              S4Vectors_0.24.0           
[17] BiocGenerics_0.32.0         Seurat_3.1.4               

loaded via a namespace (and not attached):
  [1] Rtsne_0.15               ggbeeswarm_0.6.0         colorspace_1.4-1        
  [4] ggridges_0.5.2           IRdisplay_0.7.0          XVector_0.26.0          
  [7] base64enc_0.1-3          BiocNeighbors_1.4.0      leiden_0.3.3            
 [10] listenv_0.8.0            npsurv_0.4-0             bit64_0.9-7             
 [13] ggrepel_0.8.1            codetools_0.2-16         splines_3.6.1           
 [16] lsei_1.2-0               IRkernel_1.1             jsonlite_1.6.1          
 [19] ica_1.0-2                cluster_2.1.0            png_0.1-7               
 [22] uwot_0.1.5               sctransform_0.2.1        compiler_3.6.1          
 [25] httr_1.4.1               assertthat_0.2.1         Matrix_1.2-18           
 [28] lazyeval_0.2.2           BiocSingular_1.2.0       htmltools_0.4.0         
 [31] tools_3.6.1              rsvd_1.0.2               igraph_1.2.4.2          
 [34] gtable_0.3.0             glue_1.3.1               GenomeInfoDbData_1.2.2  
 [37] RANN_2.6.1               reshape2_1.4.3           dplyr_0.8.0.1           
 [40] Rcpp_1.0.3               gdata_2.18.0             ape_5.3                 
 [43] nlme_3.1-145             DelayedMatrixStats_1.8.0 gbRd_0.4-11             
 [46] lmtest_0.9-37            stringr_1.4.0            globals_0.12.5          
 [49] irlba_2.3.3              gtools_3.8.1             future_1.16.0           
 [52] MASS_7.3-51.5            zlibbioc_1.32.0          zoo_1.8-7               
 [55] scales_1.0.0             RColorBrewer_1.1-2       reticulate_1.14         
 [58] pbapply_1.4-2            gridExtra_2.3            stringi_1.4.3           
 [61] caTools_1.18.0           bibtex_0.4.2.2           repr_1.1.0              
 [64] Rdpack_0.11-1            rlang_0.4.5              pkgconfig_2.0.3         
 [67] bitops_1.0-6             evaluate_0.14            lattice_0.20-40         
 [70] ROCR_1.0-7               purrr_0.3.3              htmlwidgets_1.5.1       
 [73] bit_1.1-14               cowplot_1.0.0            tidyselect_0.2.5        
 [76] RcppAnnoy_0.0.12         plyr_1.8.6               magrittr_1.5            
 [79] gplots_3.0.3             pbdZMQ_0.3-3             pillar_1.4.3            
 [82] withr_2.1.2              fitdistrplus_1.0-14      survival_3.1-8          
 [85] RCurl_1.95-4.12          tibble_2.1.3             future.apply_1.4.0      
 [88] tsne_0.1-3               crayon_1.3.4             uuid_0.1-2              
 [91] KernSmooth_2.23-15       plotly_4.9.2             viridis_0.5.1           
 [94] grid_3.6.1               data.table_1.12.2        metap_1.1               
 [97] digest_0.6.25            tidyr_0.8.3              RcppParallel_4.4.4      
[100] munsell_0.5.0            beeswarm_0.2.3           viridisLite_0.3.0       
[103] vipor_0.4.5

@dweemx
Copy link

dweemx commented Mar 9, 2020

Mine are these:

Scanpy

anndata==0.6.22.post1
annoy==1.15.2
numpy==1.17.2
pandas==0.25.1
scanpy==1.4.4.post1
scikit-learn==0.21.3
scipy==1.3.1
sklearn==0.0
statsmodels==0.10.1
umap-learn==0.3.10

Seurat

R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS/LAPACK:~/software/OpenBLAS/0.2.20-GCC-6.4.0-2.28/lib/libopenblas_haswellp-r0.2.20.so

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 
 
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   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] IRanges_2.18.2       S4Vectors_0.22.0     Biobase_2.44.0       BiocGenerics_0.30.0 
[7] Seurat_3.1.2         reticulate_1.14     

loaded via a namespace (and not attached):
  [1] nlme_3.1-139        tsne_0.1-3          bitops_1.0-6        bit64_0.9-7         RcppAnnoy_0.0.12    RColorBrewer_1.1-2 
  [7] httr_1.4.1          sctransform_0.2.0   tools_3.6.0         backports_1.1.4     R6_2.4.0            irlba_2.3.3        
 [13] KernSmooth_2.23-15  DBI_1.0.0           uwot_0.1.5          lazyeval_0.2.2      colorspace_1.4-1    npsurv_0.4-0       
 [19] tidyselect_0.2.5    gridExtra_2.3       bit_1.1-14          compiler_3.6.0      hdf5r_1.2.0         plotly_4.9.0       
 [25] caTools_1.17.1.2    scales_1.0.0        lmtest_0.9-37       ggridges_0.5.1      pbapply_1.4-0       rappdirs_0.3.1     
 [31] stringr_1.4.0       digest_0.6.19       R.utils_2.9.0       pkgconfig_2.0.2     htmltools_0.3.6     bibtex_0.4.2       
 [37] htmlwidgets_1.3     rlang_0.4.0         RSQLite_2.1.2       rstudioapi_0.10     zoo_1.8-6           jsonlite_1.6       
 [43] ica_1.0-2           gtools_3.8.1        dplyr_0.8.2         R.oo_1.22.0         magrittr_1.5        Matrix_1.2-17      
 [49] Rcpp_1.0.1          munsell_0.5.0       ape_5.3             lifecycle_0.1.0     R.methodsS3_1.7.1   stringi_1.4.3      
 [55] gbRd_0.4-11         MASS_7.3-51.4       gplots_3.0.1.1      Rtsne_0.15          plyr_1.8.4          blob_1.2.0         
 [61] grid_3.6.0          gdata_2.18.0        listenv_0.7.0       ggrepel_0.8.1       crayon_1.3.4        lattice_0.20-38    
 [67] cowplot_0.9.4       splines_3.6.0       SDMTools_1.1-221.1  zeallot_0.1.0       pillar_1.4.2        igraph_1.2.4.2     
 [73] future.apply_1.3.0  reshape2_1.4.3      codetools_0.2-16    leiden_0.3.1        glue_1.3.1          lsei_1.2-0         
 [79] metap_1.1           RcppParallel_4.4.3  data.table_1.12.2   vctrs_0.2.0         png_0.1-7           Rdpack_0.11-0      
 [85] gtable_0.3.0        RANN_2.6.1          purrr_0.3.2         tidyr_1.0.0         future_1.14.0       assertthat_0.2.1   
 [91] ggplot2_3.2.1       rsvd_1.0.2          survival_2.44-1.1   viridisLite_0.3.0   tibble_2.1.3        memoise_1.1.0      
 [97] cluster_2.0.8       globals_0.12.4      fitdistrplus_1.0-14 ROCR_1.0-7

Seems that you are using the latest version of scanpy and anndata

@franciscogrisanti
Copy link

Thank you so much @dweemx I was able to load the H5AD object into seurat 😁

@dweemx
Copy link

dweemx commented Mar 9, 2020

So what was the issue ? Did you fix it by downgrading scanpy and anndata ?

@franciscogrisanti
Copy link

I think the issue is in h5py==2.10.0. Any .h5ad object exported with this version breaks while loading into Seurat 3.4.1. I then exported my objects as .loom

Downgraded to h5py==2.9.0, loaded the .loom, exported as .h5ad again. And finally, I was able to read them in Seurat.

I had to did the .loom trick, because apparently there is no backward compatibility between h5py==2.10.0 and h5py==2.9.0 for the h5ad files either.

@Acribbs
Copy link

Acribbs commented Mar 9, 2020

My issue was resolved with downgrading h5py to 2.9.0 too and remaking the .h5ad files. Thanks for the solution.

@kleurless
Copy link

Downgrading h5py to 2.9.0 and remaking the .5had file did not fix the issue for me.
@satijalab any progress regarding this bug?

@hejian41
Copy link

I think the issue is in h5py==2.10.0. Any .h5ad object exported with this version breaks while loading into Seurat 3.4.1. I then exported my objects as .loom

Downgraded to h5py==2.9.0, loaded the .loom, exported as .h5ad again. And finally, I was able to read them in Seurat.

I had to did the .loom trick, because apparently there is no backward compatibility between h5py==2.10.0 and h5py==2.9.0 for the h5ad files either.

Neither did it work for me. Did you downgrade scanpy or anndata?

@no-response
Copy link

no-response bot commented Mar 19, 2020

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

@no-response no-response bot closed this as completed Mar 19, 2020
@Acribbs
Copy link

Acribbs commented Mar 20, 2020

I installed all my software using Conda. Scanpy==1.4.4.post1, anndata==0.6.22.post1.

@Puriney
Copy link

Puriney commented Mar 30, 2020

I decided to export to scipy.io.mmwrite and create the three files similar to 10x.

@brianpenghe
Copy link

Currently it seems that the latest version of Scanpy doesn't produce a h5ad file that Seurat can read. The layers=FALSE trick also doesn't work for me. Is the Seurat team going to change the h5ad reading method or we have to use older versions of Scanpy?

@sjfleming
Copy link

This issue is a major blocker

@Manikgarg
Copy link

Using the library SeuratDisk worked for me with the latest versions of Seurat and Scanpy.

#2748 (comment)

@joschif
Copy link

joschif commented Aug 4, 2020

I'm having the same issue. Setting layers=FALSE did not work for me. If I downgrade to h5py==2.9.0, scanpy==1.4.4.post1 and anndata==0.6.22.post1 , I get a different error:

Error in validObject(r) : 
  invalid class “dgTMatrix” object: all row indices (slot 'i') must be between 0 and nrow-1 in a TsparseMatrix

@SBata
Copy link

SBata commented Aug 16, 2020

using SeuratDisk worked for me :)

@Averiian
Copy link

SeuratDisk unfortunately didn't work out for me.
I posted my problem in the following issue.

mojaveazure/seurat-disk#18

does anyone face a similar issue?

@ColetheStatistician
Copy link

ColetheStatistician commented Sep 4, 2020

SeuratDisk unfortunately didn't work out for me.
I posted my problem in the following issue.

mojaveazure/seurat-disk#18

does anyone face a similar issue?

SeuratDisk did not work for me either.

The .hda5 file that I am using is here. It was generated in this blog post where the blog writer has video links to how they converted multiple .tar.gz and .csv files into this one file.
I am not familiar enough with scanpy to be able to recreate this file quickly.

@kvshams
Copy link

kvshams commented Sep 4, 2020

Hi,
I found a work around this using reticulate, mostly modified from here . Though its not straight forward, but this works for me!. Keeping it as a reference for others!.

suppressPackageStartupMessages({
    library("reticulate")
    library("ggplot2")
    library("scater")
    library("Seurat")
})

Import scanpy and object

sc <- import("scanpy")
adata <- sc$read_h5ad("myAnndata.h5ad")
adata
AnnData object with n_obs × n_vars = 21391 × 30983
    obs: 'n_genes', 'n_counts', 'percent_mito', 'leiden_res_1.3', 'leiden_res_2',  'leiden_labels', 'leiden_res_0.9', 'leiden_res_1.1', 'leiden_res_0.5'
    var: 'n_cells', 'percent_cells', 'robust', 'highly_variable_features', 'mean', 'var', 'hvf_loess', 'hvf_rank'
    uns: 'Channels', 'PCs', 'fmat_highly_variable_features', 'leiden_labels_colors', 'leiden_res_0.5_colors', 'leiden_res_0.9_colors', 'leiden_res_1.1_colors', 'pca'
    obsm: 'X_pca',  'X_umap'
    varm: 'PCs', 'de_res'
    layers: 'counts', 'winsorized'
    obsp: 'connectivities', 'distances'

Format count and normalized data for their respective slots

counts <- t(adata$layers["counts"])
colnames(counts) <- adata$obs_names$to_list()
rownames(counts) <- adata$var_names$to_list()
counts <- Matrix::Matrix(as.matrix(counts), sparse = T)

data <- t(adata$layers["winsorized"])
colnames(data) <- adata$obs_names$to_list()
rownames(data) <- adata$var_names$to_list()
data <- Matrix::Matrix(as.matrix(data), sparse = T)

Create the Seurat object

seurat <- CreateSeuratObject(counts)
seurat <- SetAssayData(seurat, "data", data)
seurat <- AddMetaData(seurat, adata$obs)

Import dim. reductions

embedding <- adata$obsm["X_umap"]
rownames(embedding) <- adata$obs_names$to_list()
colnames(embedding) <- c("umap_1", "umap_2")
seurat[["umap"]] <- CreateDimReducObject(embedding, key = "umap_")
seurat

An object of class Seurat
30983 features across 21391 samples within 1 assay
Active assay: RNA (30983 features, 0 variable features)
1 dimensional reduction calculated: umap

@aditisk
Copy link

aditisk commented Nov 11, 2020

Hi @kvshams, thanks for the detailed example. I am trying to follow your code and getting an error at the "Import scanpy and object" step.

Error in py_get_attr_impl(x, name, silent) : AttributeError: module 'scanpy' has no attribute 'read_h5ad'

Any thoughts on how to get past this error ? Thanks.

@TiongSun
Copy link

TiongSun commented Mar 9, 2021

I would like to share my similar experience while converting seurat to anndata

Code:
SaveH5Seurat(seurat.object, filename = ./file.h5Seurat', overwrite = TRUE)
Convert('./file.h5Seurat', dest = "h5ad", overwrite = TRUE)

Error message:
Transfering meta.data to obs
Error in src[[i]][index] :
object of type 'environment' is not subsettable

I have two datasets. Dataset_A runs completely fine and dataset_B gives the above error. Both have the same length. The only difference is that dataset_B@meta.data contains an additional column "pseudotime". This column contains floats ranging from 0 to 1, with no negative values. Removing this single column solves the problem and the codes run without errors. This "pseudotime" column was not generated from Seurat function but rather I added it manually to the Seurat object.

I suspect that the problem may be one of your columns @meta.data contain prohibited format.

Hope that helps.

Thanks,
TS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more-information-needed We need more information before this can be addressed
Projects
None yet
Development

No branches or pull requests