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.