9
$\begingroup$

I have done some work in R and would like to try a Python tool.

What is a good way to import the data (and its annotations etc) as a Python object?

I am particularly interested in converting a Seurat object into an AnnData object. (Either directly or as a Python object which can be converted into an AnnData.)

$\endgroup$
10
$\begingroup$

A simple solution for converting Seurat objects into AnnData, as described in this vignette:

library(reticulate)
seuratobject_ad <- Convert(from=seuratobject, to="anndata", filename="seuratobject.h5ad")

Alternatively, one can use Loom, "a file format designed to store and work with single-cell RNA-seq data".

In R, install LoomR:

devtools::install_github(repo = "mojaveazure/loomR")

Convert from Seurat object to loom:

pfile <- Convert(from = pbmc_small, to = "loom", filename = "pbmc_small.loom")
pfile$close() # close loom objects when finished using them.

Then import loom object in Python using loompy, or directly as AnnData:

scanpy.api.read_loom


Alternatively, see feather.


Or export as some text format (csv, json) then import into Python.

$\endgroup$
1
  • $\begingroup$ I would recommend to follow the KISS rule. If you can export the data in .csv or json better, but that depends on what is the original data format $\endgroup$ – llrs Mar 22 '18 at 10:38
0
$\begingroup$

The answers have been outdated. Convert() only works for Seurat2 objects. For Seurat3 objects you can only convert them into loom for Scanpy to import. And you can feel free to use Scanpy to write your anndata object into a h5ad file.

$\endgroup$
0
$\begingroup$

Seurat's Convert() may not work for certain versions of Scanpy. Whenever it doesn't work, you could save the data into matrixmarket files and import them into python.

Alternatively, you could try Sanger's sceasy

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.