Seurat: Issue converting Seurat objects to anndata

Created on 2 Aug 2018  路  15Comments  路  Source: satijalab/seurat

I am trying to convert my Seurat object into anndata to import it in Scanpy to draw a forced graph (hope it is possible). But I get the following error

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: len() of unsized object

Detailed traceback: 
  File "~/miniconda3/lib/python3.6/site-packages/anndata/base.py", line 635, in __init__
    filename=filename, filemode=filemode)
  File "~/miniconda3/lib/python3.6/site-packages/anndata/base.py", line 802, in _init_as_actual
    self._check_dimensions()
  File "~/miniconda3/lib/python3.6/site-packages/anndata/base.py", line 1633, in _check_dimensions

Any suggestions?

if 'obsm' in key and len(self._obsm) != self._n_obs:

Most helpful comment

for converting an h5ad file to a Seurat object, the following worked for me. I made a conda environment and then activated it in R.
In Unix terminal...

conda create --name anndata
source activate anndata
conda install -c anaconda pandas=0.22.0
conda install anndata -c bioconda

I found that the more recent version of pandas does not work.

The packages in this environment are as follows...

conda list
# packages in environment at /Users/orr/miniconda3/envs/anndata:
#
# Name                    Version                   Build  Channel
anndata                   0.6.13                     py_0    bioconda
blas                      1.0                         mkl    anaconda
ca-certificates           2018.03.07                    0    anaconda
certifi                   2018.10.15               py36_0    anaconda
h5py                      2.8.0            py36h878fce3_3  
hdf5                      1.10.2               hfa1e0ec_1  
intel-openmp              2019.0                      118    anaconda
libcxx                    4.0.1                hcfea43d_1    anaconda
libcxxabi                 4.0.1                hcfea43d_1    anaconda
libedit                   3.1.20170329         hb402a30_2    anaconda
libffi                    3.2.1                h475c297_4    anaconda
libgfortran               3.0.1                h93005f0_2    anaconda
mkl                       2019.0                      118    anaconda
mkl_fft                   1.0.6            py36hb8a8100_0    anaconda
mkl_random                1.0.1            py36h5d10147_1    anaconda
natsort                   5.4.1                    py36_0  
ncurses                   6.1                  h0a44026_0    anaconda
numpy                     1.15.3           py36h6a91979_0    anaconda
numpy-base                1.15.3           py36h8a80b8c_0    anaconda
openssl                   1.1.1                h1de35cc_0    anaconda
pandas                    0.22.0           py36h0a44026_0    anaconda
pip                       18.1                     py36_0    anaconda
python                    3.6.7                haf84260_0    anaconda
python-dateutil           2.7.5                    py36_0    anaconda
pytz                      2018.7                   py36_0    anaconda
readline                  7.0                  h1de35cc_5    anaconda
scipy                     1.1.0            py36h28f7352_1  
setuptools                40.5.0                   py36_0    anaconda
six                       1.11.0                   py36_1    anaconda
sqlite                    3.25.2               ha441bb4_0    anaconda
tk                        8.6.8                ha441bb4_0    anaconda
wheel                     0.32.2                   py36_0    anaconda
xz                        5.2.4                h1de35cc_4    anaconda
zlib                      1.2.11               hf3cbc9b_2    anaconda

In R, I activate that environment with...

library(Seurat)
library(reticulate)
use_condaenv("anndata")  # need anndata python library to read h5ad file 

I am using Seurat 2.3.4 and R 3.5.0.

Then the tutorial commands work for me. Hope this helps.

All 15 comments

Similar problem here.
I use windows, R v3.5.1, Python v3.6.6, Seurat v2.3.3 and anndata v0.6.
The mystery is that I managed to convert my Seurat object to anndata once, so it's not an installation/compatibility problem, but then when I tried it again it didn't work anymore, with this error:

> Object_ad <- Convert(from = SeuratObject, to = "anndata", filename = "path_to_the_file")
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: Variables annot. `var` must have number of columns of `X` (19522), but has 0 rows.

Detailed traceback: 
  File "E:\Python\Python37\lib\site-packages\anndata\base.py", line 641, in __init__
    filename=filename, filemode=filemode)
  File "E:\Python\Python37\lib\site-packages\anndata\base.py", line 836, in _init_as_actual
    self._check_dimensions()
  File "E:\Python\Python37\lib\site-packages\anndata\base.py", line 1759, in _check_dimensions
    .format(self._n_vars, self._var.shape[0]))

Any suggestions would be appreciated.

I'm trying to convert from anndata to Seurat and it is also not working:

library(reticulate)
library(Seurat)
ad <- import("anndata", convert = FALSE)
gmsd_ad <- ad$read_h5ad("/Users/angela.pisco/src/graph-matching/files/synthetic_data.h5ad")
gmsd_seurat <- Convert(gmsd_ad, "seurat")
Error in py_get_attr_impl(x, name, silent) :   AttributeError: 'NoneType' object has no attribute 'X' 

Same here, I'm converting seurat object to anndata and i got this error

seurat_ad <- Convert(from = seuratsc, to = "anndata", filename = "seuratscanndata.h5ad")
Error in np_array(X@p) : no slot of name "p" for this object of class "dgTMatrix"

but if anyone is still looking for a way to convert seurat data/rawdata object to scanpy I converted it to .mtx file using writeMM() and read into scanpy using scanpy.api.read().T

I got the same error when converting anndata to Seurat.

library(scater)
library(Seurat)
library(cowplot)
library(reticulate)
ad <- import("anndata", convert = FALSE)
seurat_sc<- ad$read_h5ad("mouse_sample.h5ad")
seurat_ad <- Convert(seurat_sc, to = "seurat")

Error in py_get_attr_impl(x, name, silent) :
AttributeError: 'NoneType' object has no attribute 'X'

I was wondering if it was the version of packages and R that caused the errors. Anyone successfully convert data, could you please show the sessionInfo() and version of python packages?

for converting an h5ad file to a Seurat object, the following worked for me. I made a conda environment and then activated it in R.
In Unix terminal...

conda create --name anndata
source activate anndata
conda install -c anaconda pandas=0.22.0
conda install anndata -c bioconda

I found that the more recent version of pandas does not work.

The packages in this environment are as follows...

conda list
# packages in environment at /Users/orr/miniconda3/envs/anndata:
#
# Name                    Version                   Build  Channel
anndata                   0.6.13                     py_0    bioconda
blas                      1.0                         mkl    anaconda
ca-certificates           2018.03.07                    0    anaconda
certifi                   2018.10.15               py36_0    anaconda
h5py                      2.8.0            py36h878fce3_3  
hdf5                      1.10.2               hfa1e0ec_1  
intel-openmp              2019.0                      118    anaconda
libcxx                    4.0.1                hcfea43d_1    anaconda
libcxxabi                 4.0.1                hcfea43d_1    anaconda
libedit                   3.1.20170329         hb402a30_2    anaconda
libffi                    3.2.1                h475c297_4    anaconda
libgfortran               3.0.1                h93005f0_2    anaconda
mkl                       2019.0                      118    anaconda
mkl_fft                   1.0.6            py36hb8a8100_0    anaconda
mkl_random                1.0.1            py36h5d10147_1    anaconda
natsort                   5.4.1                    py36_0  
ncurses                   6.1                  h0a44026_0    anaconda
numpy                     1.15.3           py36h6a91979_0    anaconda
numpy-base                1.15.3           py36h8a80b8c_0    anaconda
openssl                   1.1.1                h1de35cc_0    anaconda
pandas                    0.22.0           py36h0a44026_0    anaconda
pip                       18.1                     py36_0    anaconda
python                    3.6.7                haf84260_0    anaconda
python-dateutil           2.7.5                    py36_0    anaconda
pytz                      2018.7                   py36_0    anaconda
readline                  7.0                  h1de35cc_5    anaconda
scipy                     1.1.0            py36h28f7352_1  
setuptools                40.5.0                   py36_0    anaconda
six                       1.11.0                   py36_1    anaconda
sqlite                    3.25.2               ha441bb4_0    anaconda
tk                        8.6.8                ha441bb4_0    anaconda
wheel                     0.32.2                   py36_0    anaconda
xz                        5.2.4                h1de35cc_4    anaconda
zlib                      1.2.11               hf3cbc9b_2    anaconda

In R, I activate that environment with...

library(Seurat)
library(reticulate)
use_condaenv("anndata")  # need anndata python library to read h5ad file 

I am using Seurat 2.3.4 and R 3.5.0.

Then the tutorial commands work for me. Hope this helps.

I've tried to make use of conda envs as outlined by @orrzor above but wasn't successful.

Tryng this (to get a better error message):
py_run_string('import anndata')

Error in py_run_string_impl(code, local, convert) :
  ImportError: /usr/prog/GCC/4.8.2/lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by /home/pallagi4/miniconda3/envs/anndata/lib/python3.6/site-packages/scipy/sparse/_sparsetools.cpython-36m-x86_64-linux-gnu.so)

Turns out the problem is that R studio server (that I am using) does not provide correct point for libstdc++.so.6 via LD-LIBRARY-PATH variable, see https://github.com/rstudio/reticulate/issues/292 and https://support.rstudio.com/hc/en-us/community/posts/200645248-Setting-up-LD-LIBRARY-PATH-for-a-rsession.

Running the script in R (and not R studio) after having sourced the anndata environment did not solve the problem. I guess a workaround could be running it on a personal workstation.

I hope this is not completely hijacking this issue, but I am getting the same error as @aopisco and @Suger0917. In my case, I am using the sancpy/anndata data set from Wagner et al. (https://kleintools.hms.harvard.edu/paper_websites/wagner_zebrafish_timecourse2018/mainpage.html).
The tutorial code with the pbmc3k data set works fine.
So my guess there is something missing in these data sets, even though they work without any problems in scanpy. Any suggestions on how to debug this?

Hello @Suger0917 @aopisco @mashehu @shabs24 @esfandyari @orrzor ,

I solved the:

AttributeError: 'NoneType' object has no attribute 'X'

with the following lines of code in python:

  1. Read in your clustered anndata object. Scanpy updates the X array during the preprocessing steps, so this array no longer contains raw or normalized UMI counts. Fortunately, scanpy allows us to add this data back!

clustered_anndata = sc.read('clustered.h5ad')

  1. Read in the raw UMI count matrix or the log normalized matrix to update your clustered_anndata to include raw.X (which is what Seurat looks for). Note, the var and obs columns must be the same as the clustered anndata object. Which means, if you removed any cells or genes you will need to make sure you properly subset from your normalized or raw umi count anndata object. I export the anndata object at each major step in my pipeline to make updating my workflow easier.

clustered_anndata.raw = sc.read('normalized.h5ad')

  1. Export out the clustered_anndata object which now contains raw.X (your raw or normalized UMI matrix)

clustered_anndata.write('seuratReady.h5ad')

From here the vignette steps work for me:

library(reticulate)
library(Seurat)
ad <- import("anndata", convert = FALSE)
convert_ad <- ad$read_h5ad("seuratReady.h5ad")
seurat_obj <- Convert(convert_ad, to = "seurat")

Also, a few major notes about my Python and R environments:
python: 3.6.7
pandas: 0.22.0

R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /mnt/anaconda3/envs/single_cell/lib/libmkl_rt.so

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

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

other attached packages:
[1] Seurat_2.3.4 Matrix_1.2-15 cowplot_0.9.3 ggplot2_3.1.0
[5] reticulate_1.10

loaded via a namespace (and not attached):
[1] Rtsne_0.15 colorspace_1.3-2 class_7.3-14
[4] modeltools_0.2-22 ggridges_0.5.1 rprojroot_1.3-2
[7] mclust_5.4.2 IRdisplay_0.7.0 htmlTable_1.12
[10] fs_1.2.6 base64enc_0.1-3 rstudioapi_0.8
[13] proxy_0.4-22 npsurv_0.4-0 remotes_2.0.2
[16] flexmix_2.3-14 mvtnorm_1.0-8 codetools_0.2-15
[19] splines_3.5.1 R.methodsS3_1.7.1 lsei_1.2-0
[22] robustbase_0.93-3 knitr_1.20 pkgload_1.0.2
[25] IRkernel_0.8.14 Formula_1.2-3 jsonlite_1.5
[28] ica_1.0-2 cluster_2.0.7-1 kernlab_0.9-27
[31] png_0.1-7 R.oo_1.22.0 compiler_3.5.1
[34] httr_1.3.1 backports_1.1.2 assertthat_0.2.0
[37] lazyeval_0.2.1.9000 cli_1.0.1 prettyunits_1.0.2
[40] lars_1.2 acepack_1.4.1 htmltools_0.3.6
[43] tools_3.5.1 bindrcpp_0.2.2 igraph_1.2.2
[46] gtable_0.2.0 glue_1.3.0 reshape2_1.4.3
[49] RANN_2.6 dplyr_0.7.8 Rcpp_1.0.0
[52] trimcluster_0.1-2.1 gdata_2.18.0 ape_5.2
[55] nlme_3.1-137 iterators_1.0.10 fpc_2.1-11.1
[58] gbRd_0.4-11 lmtest_0.9-36 stringr_1.3.1
[61] ps_1.2.1 testthat_2.0.1 irlba_2.3.2
[64] devtools_2.0.1 gtools_3.8.1 DEoptimR_1.0-8
[67] MASS_7.3-51.1 zoo_1.8-4 scales_1.0.0
[70] doSNOW_1.0.16 parallel_3.5.1 RColorBrewer_1.1-2
[73] curl_3.2 memoise_1.1.0 pbapply_1.3-4
[76] gridExtra_2.3 rpart_4.1-13 segmented_0.5-3.0
[79] latticeExtra_0.6-28 stringi_1.2.4 desc_1.2.0
[82] foreach_1.4.4 checkmate_1.8.5 caTools_1.17.1.1
[85] pkgbuild_1.0.2 bibtex_0.4.2 repr_0.18
[88] Rdpack_0.10-1 SDMTools_1.1-221 rlang_0.3.0.1
[91] pkgconfig_2.0.2 dtw_1.20-1 prabclus_2.2-6
[94] bitops_1.0-6 evaluate_0.12 lattice_0.20-38
[97] ROCR_1.0-7 purrr_0.2.5 bindr_0.1.1
[100] htmlwidgets_1.3 processx_3.2.0 tidyselect_0.2.5
[103] plyr_1.8.4 magrittr_1.5 R6_2.3.0
[106] snow_0.4-3 gplots_3.0.1 Hmisc_4.1-1
[109] pbdZMQ_0.3-3 pillar_1.3.0 foreign_0.8-71
[112] withr_2.1.2 fitdistrplus_1.0-11 mixtools_1.1.0
[115] survival_2.43-3 nnet_7.3-12 tsne_0.1-3
[118] tibble_1.4.2 crayon_1.3.4 uuid_0.1-2
[121] KernSmooth_2.23-15 usethis_1.4.0 grid_3.5.1
[124] data.table_1.11.8 callr_3.0.0 metap_1.0
[127] digest_0.6.18 diptest_0.75-7 tidyr_0.8.2
[130] R.utils_2.7.0 stats4_3.5.1 munsell_0.5.0
[133] sessioninfo_1.1.1

Let me know if you have any questions! :)

Closing this as we are currently addressing AnnData conversion (will update in #1021 when we have a fix)

Similar problem here.
I use windows, R v3.5.1, Python v3.6.6, Seurat v2.3.3 and anndata v0.6.
The mystery is that I managed to convert my Seurat object to anndata once, so it's not an installation/compatibility problem, but then when I tried it again it didn't work anymore, with this error:

> Object_ad <- Convert(from = SeuratObject, to = "anndata", filename = "path_to_the_file")
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: Variables annot. `var` must have number of columns of `X` (19522), but has 0 rows.

Detailed traceback: 
  File "E:\Python\Python37\lib\site-packages\anndata\base.py", line 641, in __init__
    filename=filename, filemode=filemode)
  File "E:\Python\Python37\lib\site-packages\anndata\base.py", line 836, in _init_as_actual
    self._check_dimensions()
  File "E:\Python\Python37\lib\site-packages\anndata\base.py", line 1759, in _check_dimensions
    .format(self._n_vars, self._var.shape[0]))

Any suggestions would be appreciated.

Hi all,
Any update on this one? I am getting exactly the same, I have tried several things but it still does not work.
Any help will more than appreciated

Any updates on the question above? I am also getting the same error. This wasn't a problem in previous versions.

Error below:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: Variables annot. `var` must have number of columns of `X` (18113), but has 0 rows. 

Any update? it still doesn't work..

Hi @saramasa22,

In this vignette (https://satijalab.org/seurat/v3.1/conversion_vignette.html), authors inform that they still do not support converting Robj -> h5ad.

They advise that the alternative is to convert to loom instead. I have successfully converted my object to loom however failed while loading it into scanpy and did not investigate further.

Hope this helped

Hi @justinesjw,
I ended up doing the same and, as you also noticed, it failed to load it on Scanpy

Was this page helpful?
0 / 5 - 0 ratings