Single cell objects interconversion

Daniel_AdediranDaniel_Adediran
3 min read

Majorities are familiar with the availability of different single-cell objects due to the development of different tools to analyse single-cell data by different packages or libraries depending on the programming language being used. For example, the Seurat package from the R programming language takes in the Seurat object as input, while the scanpy library from the python community takes in ann data object as input and some data formats

In a case, whereby am familiar with the Seurat package but am presented with ann data object, this could be a stopping point in trying to carry out single-cell RNA analysis especially if the upstream dataset for that particular study is not available. In this blog, I would like to explain how to transform a Seurat object into ann data object and viz.

There are many tools for carrying out this type of transformation such sceasy, the SeuratDisk package, and a Seurat extension. The SeuratDisk package provides functions to save Seurat objects as h5Seurat files, and functions for rapid on-disk conversion between h5Seurat and AnnData formats to enhance interoperability between Seurat and Scanpy.

In this blog post, would like to use an all tool for performing transformation from one single cell data object to another. Here is the code for require for installation and transformation to ann data format. Most of the content and code cell used in this blog is from https://github.com/cellgeni/ sceasy/ repository.

Usage

The dataset to work through these process can be obtained by installing the SeuratData package from CRAN package manager.

Before converting your single cell data object, kindly load the following packages in your R session:

Sceasy package

sceasy is a package that helps easy conversion of different single-cell data formats to each other. Converting to AnnData creates a file that can be directly used in cellxgene which is an interactive explorer for single-cell transcriptomics datasets.

Installation

sceasy is installable either as a bioconda package:

conda install -c bioconda r-sceasy

or as an R package:

devtools::install_github("cellgeni/sceasy")

which will require the bioconductor packages BiocManager and LoomExperiment:

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install(c("LoomExperiment", "SingleCellExperiment"))

To use sceasy ensure the anndata package is installed:

conda install anndata -c bioconda

If you plan to convert between loom and anndata, please also ensure that the loompy package is installed:

conda install loompy -c bioconda

You will also need to install the reticulate package:

install.packages('reticulate')

Usage

Before converting your data please load the following libraries in your R session:

library(sceasy)
library(reticulate)
use_condaenv('EnvironmentName')
loompy <- reticulate::import('loompy')

Installation of SeuratData can be accomplished through devtools

devtools::install_github('satijalab/seurat-data')

Now the same result obtained from the conversion of the Seurat object to ann data object would be used viz. i.e ann data would be converted back to the Seurat data object for all the steps based on the result obtained.

Seurat to AnnData

sceasy::convertFormat(seurat_object, from="seurat", to="anndata",
                       outFile='filename.h5ad')

AnnData to Seurat

sceasy::convertFormat(h5ad_file, from="anndata", to="seurat",
                       outFile='filename.rds')

Seurat to SingleCellExperiment

sceasy::convertFormat(seurat_object, from="seurat", to="sce",
                       outFile='filename.rds')

SingleCellExperiment to AnnData

sceasy::convertFormat(sce_object, from="sce", to="anndata",
                       outFile='filename.h5ad')

SingleCellExperiment to Loom

sceasy::convertFormat(sce_object, from="sce", to="loom",
                       outFile='filename.loom')

Loom to AnnData

sceasy::convertFormat('filename.loom', from="loom", to="anndata",
                       outFile='filename.h5ad')

Loom to SingleCellExperiment

sceasy::convertFormat('filename.loom', from="loom", to="sce",
                       outFile='filename.rds')

References

https://github.com/cellgeni/ sceasy/

4
Subscribe to my newsletter

Read articles from Daniel_Adediran directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Daniel_Adediran
Daniel_Adediran