9

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.)

Konrad Rudolph
  • 4,845
  • 14
  • 45
Peter
  • 2,634
  • 15
  • 33

3 Answers3

10

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.

Peter
  • 2,634
  • 15
  • 33
  • 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 – llrs Mar 22 '18 at 10:38
0

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")

Brunox13
  • 151
  • 1
  • 4
0

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.

Code42
  • 282
  • 1
  • 9