Read .RData in Scanpy

Hello group,

I looked at the tutorial and API but I could not find a way to read .RData to AnnData object. Anyone had this issue and that can suggest how to do this?

Thanks!

Hello!

Scanpy doesn’t have any tools for directly reading serialized R objects, but there are some tools which can help with this. I’d recommend checking out these tutorials on interacting with R. Alternatively, you could write your data to a loom file and read that from scanpy.

Thanks for the response! I checked that the tutorial and that was helpful. However, I couldn’t find how to write the .RData to a loom file. Can you please share a link or additional info on how to achieve that. Thanks!

I’m not sure I can help much with that, as it depends what’s in your .Rdata file. I think you’re just going to have to load that into an R session, figure out what libraries were used, then refer to those libraries’ docs.

Okay, I will look into this. Thanks!

I think I found the solution to it using some external R libraries. You would need to load the data into R first:

  1. Install DropletUtils package in R:

if (!requireNamespace(“BiocManager”, quietly = TRUE)) install.packages(“BiocManager”)BiocManager::install(“DropletUtils”)

Then load it in R:
library(DropletUtils)

  1. Load Rdata into R: Let’s say that mat is your expression matrix stored as dgCmatrix

  2. Use function write10xCounts(‘path_to_folder’, mat) from DropletUtils

In this way the data will be stored in the same format as the data from 10x sequencer: matrix.mtx, barcodes.tsv and genes.tsv files. Since mtx is a sparse matrix data format, loading it into scanpy using function sc.read_10x_mtx would work.

You could also save everything into one script.

Thank you very much!
I will give it a try.

@ivirshup
Hello! Thank you for showing the tutorials of how to read a seurat object in scanpy.
I am following this tutorial and it works well.

However, I am having some trouble in saving it as Anndata. Do you have any suggestions? Thank you in advance!
I used readRDS function to read a seurat object, and tried to save it by adata.write(‘XXX.h5ad’)

Here is the error I got:

TypeError                                 Traceback (most recent call last)
~/conda/conda/lib/python3.8/site-packages/anndata/_io/utils.py in func_wrapper(elem, key, val, *args, **kwargs)
    187         try:
--> 188             return func(elem, key, val, *args, **kwargs)
    189         except Exception as e:

~/conda/conda/lib/python3.8/site-packages/anndata/_io/h5ad.py in write_series(group, key, series, dataset_kwargs)
    293     else:
--> 294         group[key] = series.values
    295 

~/conda/conda/lib/python3.8/site-packages/h5py/_hl/group.py in __setitem__(self, name, obj)
    410             else:
--> 411                 ds = self.create_dataset(None, data=obj)
    412                 h5o.link(ds.id, self.id, name, lcpl=lcpl)

~/conda/conda/lib/python3.8/site-packages/h5py/_hl/group.py in create_dataset(self, name, shape, dtype, data, **kwds)
    147 
--> 148             dsid = dataset.make_new_dset(group, shape, dtype, data, name, **kwds)
    149             dset = dataset.Dataset(dsid)

~/conda/conda/lib/python3.8/site-packages/h5py/_hl/dataset.py in make_new_dset(parent, shape, dtype, data, name, chunks, compression, shuffle, fletcher32, maxshape, compression_opts, fillvalue, scaleoffset, track_times, external, track_order, dcpl, allow_unknown_filter)
     88             dtype = numpy.dtype(dtype)
---> 89         tid = h5t.py_create(dtype, logical=1)
     90 

h5py/h5t.pyx in h5py.h5t.py_create()

h5py/h5t.pyx in h5py.h5t.py_create()

h5py/h5t.pyx in h5py.h5t.py_create()

TypeError: Object dtype dtype('O') has no native HDF5 equivalent

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

TypeError                                 Traceback (most recent call last)
~/conda/conda/lib/python3.8/site-packages/anndata/_io/utils.py in func_wrapper(elem, key, val, *args, **kwargs)
    187         try:
--> 188             return func(elem, key, val, *args, **kwargs)
    189         except Exception as e:

~/conda/conda/lib/python3.8/site-packages/anndata/_io/h5ad.py in write_dataframe(f, key, df, dataset_kwargs)
    267     for colname, series in df.items():
--> 268         write_series(group, colname, series, dataset_kwargs=dataset_kwargs)
    269 

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

TypeError: Object dtype dtype('O') has no native HDF5 equivalent

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

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

TypeError                                 Traceback (most recent call last)
<ipython-input-129-00f4135176ed> in <module>
----> 1 adata.write('combine_IRI_BBKNN.h5ad', compression='gzip')

~/conda/conda/lib/python3.8/site-packages/anndata/_core/anndata.py in write_h5ad(self, filename, compression, compression_opts, force_dense, as_dense)
   1842             filename = self.filename
   1843 
-> 1844         _write_h5ad(
   1845             Path(filename),
   1846             self,

~/conda/conda/lib/python3.8/site-packages/anndata/_io/h5ad.py in write_h5ad(filepath, adata, force_dense, as_dense, dataset_kwargs, **kwargs)
    116         else:
    117             write_attribute(f, "raw", adata.raw, dataset_kwargs=dataset_kwargs)
--> 118         write_attribute(f, "obs", adata.obs, dataset_kwargs=dataset_kwargs)
    119         write_attribute(f, "var", adata.var, dataset_kwargs=dataset_kwargs)
    120         write_attribute(f, "obsm", adata.obsm, dataset_kwargs=dataset_kwargs)

~/conda/conda/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')

~/conda/conda/lib/python3.8/site-packages/anndata/_io/h5ad.py in write_attribute_h5ad(f, key, value, *args, **kwargs)
    137     if key in f:
    138         del f[key]
--> 139     _write_method(type(value))(f, key, value, *args, **kwargs)
    140 
    141 

~/conda/conda/lib/python3.8/site-packages/anndata/_io/utils.py in func_wrapper(elem, key, val, *args, **kwargs)
    189         except Exception as e:
    190             parent = _get_parent(elem)
--> 191             raise type(e)(
    192                 f"{e}\n\n"
    193                 f"Above error raised while writing key {key!r} of {type(elem)}"

TypeError: Object dtype dtype('O') has no native HDF5 equivalent

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

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

It looks like whatever data you have in adata.obs['nFeature_RNA'] isn’t a supported type for some reason. I’d suggest trying to convert it to a number. E.g.:

adata.obs["nFeature_RNA"] = adata.obs["nFeature_RNA"].astype(float)
# Or
adata.obs["nFeature_RNA"] = adata.obs["nFeature_RNA"].astype(int)

Converting it to an integer may fail if there are any missing values in there.

1 Like