question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error When Saving File as .h5ad with adata.write

See original GitHub issue

Hello, I am trying to save my .h5ad files to my desktop from scanpy, but adata.write is giving me an error; I posted on the Scanpy forum, but maybe this is a better place for this issue. The data has been run through Kallisto Bustools and is being loaded in as an .h5ad file, but the error appears even if I try to save it immediately after loading it in, without any modifications. Unfortunately, the error does not happen when I try it on other data, such as the pbmc3k data.

Minimal code sample

zf_48 = anndata.read_h5ad("/Users/julius/Desktop/48hpf_adata.h5ad")
zf_48.var["gene_ids"] = zf_48.var.index.values

t2g = pd.read_csv("/Users/julius/Desktop/zf_transcripts_to_genes_no_rm.txt", header=None, names=["tid", "gene_id", "gene_name"], sep="\t")
t2g.index = t2g.gene_id
t2g = t2g.loc[~t2g.index.duplicated(keep='first')]
zf_48.var["gene_name"] = zf_48.var.gene_ids.map(t2g["gene_name"])
zf_48.var.index = zf_48.var["gene_name"] 

zf_48.write_h5ad("/Users/julius/Desktop/zf_48.h5ad")

Here is the error:

RuntimeError                              Traceback (most recent call last)
~/opt/anaconda3/lib/python3.8/site-packages/anndata/_io/utils.py in func_wrapper(elem, key, val, *args, **kwargs)
    208         try:
--> 209             return func(elem, key, val, *args, **kwargs)
    210         except Exception as e:

~/opt/anaconda3/lib/python3.8/site-packages/anndata/_io/h5ad.py in write_array(f, key, value, dataset_kwargs)
    184         value = _to_hdf5_vlen_strings(value)
--> 185     f.create_dataset(key, data=value, **dataset_kwargs)
    186 

~/opt/anaconda3/lib/python3.8/site-packages/h5py/_hl/group.py in create_dataset(self, name, shape, dtype, data, **kwds)
    138             if name is not None:
--> 139                 self[name] = dset
    140             return dset

~/opt/anaconda3/lib/python3.8/site-packages/h5py/_hl/group.py in __setitem__(self, name, obj)
    372             if isinstance(obj, HLObject):
--> 373                 h5o.link(obj.id, self.id, name, lcpl=lcpl, lapl=self._lapl)
    374 

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5o.pyx in h5py.h5o.link()

RuntimeError: Unable to create link (name already exists)

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
~/opt/anaconda3/lib/python3.8/site-packages/anndata/_io/utils.py in func_wrapper(elem, key, val, *args, **kwargs)
    208         try:
--> 209             return func(elem, key, val, *args, **kwargs)
    210         except Exception as e:

~/opt/anaconda3/lib/python3.8/site-packages/anndata/_io/h5ad.py in write_series(group, key, series, dataset_kwargs)
    283         write_array(group, category_key, categories, dataset_kwargs=dataset_kwargs)
--> 284         write_array(group, key, codes, dataset_kwargs=dataset_kwargs)
    285 

~/opt/anaconda3/lib/python3.8/site-packages/anndata/_io/utils.py in func_wrapper(elem, key, val, *args, **kwargs)
    211             parent = _get_parent(elem)
--> 212             raise type(e)(
    213                 f"{e}\n\n"

RuntimeError: Unable to create link (name already exists)

Above error raised while writing key 'gene_name' of <class 'h5py._hl.group.Group'> from /.

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
~/opt/anaconda3/lib/python3.8/site-packages/anndata/_io/utils.py in func_wrapper(elem, key, val, *args, **kwargs)
    208         try:
--> 209             return func(elem, key, val, *args, **kwargs)
    210         except Exception as e:

~/opt/anaconda3/lib/python3.8/site-packages/anndata/_io/h5ad.py in write_dataframe(f, key, df, dataset_kwargs)
    262     for col_name, (_, series) in zip(col_names, df.items()):
--> 263         write_series(group, col_name, series, dataset_kwargs=dataset_kwargs)
    264 

~/opt/anaconda3/lib/python3.8/site-packages/anndata/_io/utils.py in func_wrapper(elem, key, val, *args, **kwargs)
    211             parent = _get_parent(elem)
--> 212             raise type(e)(
    213                 f"{e}\n\n"

RuntimeError: Unable to create link (name already exists)

Above error raised while writing key 'gene_name' of <class 'h5py._hl.group.Group'> from /.

Above error raised while writing key 'gene_name' of <class 'h5py._hl.group.Group'> from /.

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
<ipython-input-21-ded14f7730cd> in <module>
      8 zf_48.var.index = zf_48.var["gene_name"]
      9 
---> 10 zf_48.write_h5ad("/Users/julius/Desktop/zf_48.h5ad")

~/opt/anaconda3/lib/python3.8/site-packages/anndata/_core/anndata.py in write_h5ad(self, filename, compression, compression_opts, force_dense, as_dense)
   1903             filename = self.filename
   1904 
-> 1905         _write_h5ad(
   1906             Path(filename),
   1907             self,

~/opt/anaconda3/lib/python3.8/site-packages/anndata/_io/h5ad.py in write_h5ad(filepath, adata, force_dense, as_dense, dataset_kwargs, **kwargs)
    110             write_attribute(f, "raw", adata.raw, dataset_kwargs=dataset_kwargs)
    111         write_attribute(f, "obs", adata.obs, dataset_kwargs=dataset_kwargs)
--> 112         write_attribute(f, "var", adata.var, dataset_kwargs=dataset_kwargs)
    113         write_attribute(f, "obsm", adata.obsm, dataset_kwargs=dataset_kwargs)
    114         write_attribute(f, "varm", adata.varm, dataset_kwargs=dataset_kwargs)

~/opt/anaconda3/lib/python3.8/functools.py in wrapper(*args, **kw)
    873                             '1 positional argument')
    874 
--> 875         return dispatch(args[0].__class__)(*args, **kw)
    876 
    877     funcname = getattr(func, '__name__', 'singledispatch function')

~/opt/anaconda3/lib/python3.8/site-packages/anndata/_io/h5ad.py in write_attribute_h5ad(f, key, value, *args, **kwargs)
    128     if key in f:
    129         del f[key]
--> 130     _write_method(type(value))(f, key, value, *args, **kwargs)
    131 
    132 

~/opt/anaconda3/lib/python3.8/site-packages/anndata/_io/utils.py in func_wrapper(elem, key, val, *args, **kwargs)
    210         except Exception as e:
    211             parent = _get_parent(elem)
--> 212             raise type(e)(
    213                 f"{e}\n\n"
    214                 f"Above error raised while writing key {key!r} of {type(elem)}"

RuntimeError: Unable to create link (name already exists)

Above error raised while writing key 'gene_name' of <class 'h5py._hl.group.Group'> from /.

Above error raised while writing key 'gene_name' of <class 'h5py._hl.group.Group'> from /.

Above error raised while writing key 'var' of <class 'h5py._hl.files.File'> from /.

Am I doing something wrong with this data? Or should I be using a different command?

Versions

anndata 0.7.6 scanpy 1.8.1 sinfo 0.3.4 PIL 8.2.0 anndata2ri 1.0.6 anyio NA appnope 0.1.2 attr 20.3.0 babel 2.9.0 backcall 0.2.0 backports NA bottleneck 1.3.2 brotli NA certifi 2020.12.05 cffi 1.14.5 chardet 4.0.0 cloudpickle 1.6.0 colorama 0.4.4 cycler 0.10.0 cython_runtime NA cytoolz 0.11.0 dask 2021.04.0 dateutil 2.8.1 decorator 5.0.6 dunamai 1.6.0 fsspec 0.9.0 get_version 3.5 h5py 2.10.0 idna 2.10 igraph 0.9.6 ipykernel 5.3.4 ipython_genutils 0.2.0 ipywidgets 7.6.3 jedi 0.17.2 jinja2 2.11.3 joblib 1.0.1 json5 NA jsonschema 3.2.0 jupyter_server 1.4.1 jupyterlab_server 2.4.0 kiwisolver 1.3.1 leidenalg 0.8.7 llvmlite 0.35.0 markupsafe 1.1.1 matplotlib 3.3.4 mkl 2.3.0 mpl_toolkits NA natsort 7.1.1 nbclassic NA nbformat 5.1.3 numba 0.52.0 numexpr 2.7.3 numpy 1.20.1 packaging 20.9 pandas 1.2.4 parso 0.7.0 pexpect 4.8.0 pickleshare 0.7.5 pkg_resources NA prometheus_client NA prompt_toolkit 3.0.17 psutil 5.8.0 ptyprocess 0.7.0 pvectorc NA pycparser 2.20 pygments 2.8.1 pynndescent 0.5.4 pyparsing 2.4.7 pyrsistent NA pytz 2021.1 requests 2.25.1 rpy2 3.4.5 samalg 0.8.6 scipy 1.6.2 seaborn 0.11.1 send2trash NA six 1.15.0 sklearn 0.24.1 skmisc 0.1.4 sniffio 1.2.0 socks 1.7.1 sphinxcontrib NA statsmodels 0.12.2 storemagic NA tables 3.6.1 tblib 1.7.0 terminado 0.9.4 texttable 1.6.4 tlz 0.11.0 toolz 0.11.1 tornado 6.1 traitlets 5.0.5 typing_extensions NA tzlocal NA umap 0.5.1 urllib3 1.26.4 wcwidth 0.2.5 yaml 5.4.1 zmq 20.0.0 zope NA IPython 7.22.0 jupyter_client 6.1.12 jupyter_core 4.7.1 jupyterlab 3.0.14 notebook 6.3.0 Python 3.8.8 (default, Apr 13 2021, 12:59:45) [Clang 10.0.0 ] macOS-10.16-x86_64-i386-64bit 16 logical CPU cores, i386

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Koncopdcommented, Sep 16, 2021

And if you do zf_48.var.index = zf_48.var["gene_name"].tolist() or zf_48.var.index = zf_48.var["gene_name"].astype(str).tolist() ?

0reactions
jtabincommented, Sep 16, 2021

zf_48.var.index = zf_48.var["gene_name"].tolist() worked! The problem was immediately solved. In case it is helpful, zf_48.var["gene_name"]'s dtype was dtype('O'). Thank you so much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error When Saving File as .h5ad with adata.write #1982 - GitHub
Hello, I am trying to save my .h5ad files to my desktop from scanpy, but adata.write is giving me an error. The data...
Read more >
Can you save pictures as a .h5ad format - Help - Scanpy
This is the first time I use the scanpy, and I meet a problem in the step “adata.write(results_file)”. And results_file = 'write/pbmc3k.h5ad'.
Read more >
AnnData.write_h5ad - Read the Docs
Write .h5ad -formatted hdf5 file. Note. Setting compression to 'gzip' can save disk space but will slow down writing and subsequent reading.
Read more >
Anndata Import Error with 0.8.0 - scrna-seq - Galaxy Help
Was having issues uploading h5ad files to galaxy (see error below). Turned out I was saving files with scanpy but the version of...
Read more >
Clustering 10K PBMCs - Pitt CRC
writing an h5ad cache file to speedup reading next time. In [4]:. adata.var_names_make_unique() # this is unnecessary if using 'gene_ids'. In [5]:. adata....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found