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 accepted solution is probably the best for older objects of type seurat created with Seurat package v2. For newer Seurat Objects, there is a new tool designed specifically for this purpose, called SeuratDisk.

There is a nicely documented vignette about the Seurat <-> AnnData conversion. In short:

  1. In R, save the Seurat object as an h5Seurat file:
library(Seurat)
library(SeuratDisk)

SaveH5Seurat(SeuratObject, filename = "file.h5Seurat")
Convert("file.h5Seurat", dest = "h5ad")
  1. In python, simply load the file:
import scanpy

adata = scanpy.read_h5ad("file.h5Seurat")
$\endgroup$
0
$\begingroup$

Since both Seurat and Scanpy are evolving rapidly, the safest way is to save your Seurat object into MatrixMarket format + metadata csv, and import them into Scanpy.

$\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.