Converting between scRNA-seq package formats

While many packages are have some object converters they are not always as feature complete as desired. scCustomize provides a few straightforward converter functions.

FYI: Object converters can be fragile and/or not very flexible. I have tried to make these functions to avoid those issues. However, if that proves too hard a task to maintain long-term I may move them to separate package/deprecate them.

Load Seurat Object & Add QC Data

# read object
pbmc <- pbmc3k.SeuratData::pbmc3k.final
pbmc <- UpdateSeuratObject(pbmc)

pbmc <- Add_Cell_QC_Metrics(seurat_object = pbmc, species = "human")

We’ll also add some random meta data variables to pbmc data form use in this vignette

pbmc$sample_id <- sample(c("sample1", "sample2", "sample3", "sample4", "sample5", "sample6"), size = ncol(pbmc),
    replace = TRUE)

Convert Seurat Objects to LIGER Objects

scCustomize contains the conversion function as.LIGER(). This function has a few advantages over the conversion function rliger::seuratToLiger().

  • as.liger will keep all meta data and transfer it to LIGER by default.
  • If a dimensionality reduction is transferred as.liger will also store a reduction key for use with scCustomize LIGER plotting functions to correctly set plot axes.
  • If converting a list of Seurat objects and keeping Seurat variable genes then as.liger can either take union or intersection of genes between objects.
  • as.liger has additional internal checks to ensure compatibility with both Seurat V3/4 & V5 object structures.
pbmc_liger <- as.LIGER(x = pbmc, group.by = "sample_id")
##  Checking Seurat object validity
##  Creating LIGER object.
##  Normalizing data.
pbmc_liger
## An object of class liger 
## with 6 datasets and
##  2638 total cells.

Confirm that information from meta.data slot was transferred to LIGER object.

head(colnames(x = pbmc_liger@cell.data), 10)
##  [1] "nUMI"               "nGene"              "dataset"           
##  [4] "orig.ident"         "seurat_annotations" "percent.mt"        
##  [7] "RNA_snn_res.0.5"    "seurat_clusters"    "percent_mito"      
## [10] "percent_ribo"

Convert LIGER objects to Seurat Objects

The liger/rliger package already contains a function rliger::seuratToLiger() to convert LIGER objects to Seurat Objects. However, during this transfer a few things have issues crop up:

  • All meta data except the “dataset” column from liger_object@cell.data is lost.
  • Seurat dimensionality reduction is set to tSNE regardless of method used in LIGER analysis
  • Seurat assay name not specified

As of scCustomize v2.1.0 converting to Seurat objects from Liger can be accomplished using as.liger function which functions identically to previous function Liger_to_Seurat(). Liger_to_Seurat() will continue to work until v2.2.0 at which point it will be completely deprecated.

scCustomize contains modified version of this function which extends the Seurat function as.Seurat() that solves these issues with some extra parameters:

  • keep_meta logical. Whether to keep meta data from the @cell.data slot in LIGER object. Default is TRUE.
  • reduction_label Name of dimensionality reduction technique used (e.g., tSNE, UMAP, etc). Ensures dim names are set correctly in Seurat object.
  • seurat_assay Name of assay to use for data in Seurat object. Default is “RNA”.
  • assay_type Specify whether to create V3/4 vs V5 Seurat assays.
new_seurat <- as.Seurat(x = pbmc_liger, reduction_label = "UMAP")
## Preparing & merging matrices.
## Creating final sparse matrix.
## Normalizing layer: counts

Convert Seurat or LIGER objects to Anndata objects

scCustomize also allows for the conversion of Seurat or LIGER objects to python anndata objects for analysis in scanpy or other compatible python packages via the function as.anndata. These functions were inspired/modified/updated from sceasy R package (see as.anndata documentation).

  • as.anndata works with Seurat V3/4, Seurat V5, and LIGER objects.
  • as.anndata requires users have reticulate R package and linked python installation with anndata installed.
    • See reticulate website for more information on installation and setup of reticulate in R.
    • See anndata website for more information on installation and use of anndata in python.
as.anndata(x = pbmc, file_path = "~/Desktop", file_name = "pbmc_anndata.h5ad")
##  Checking Seurat object validity & Extracting Data
## The following columns were removed as they contain identical values for all
## rows:
##  orig.ident
##  Creating anndata object.
##  Writing anndata file: "/Users/marsh_mbp/Desktop/pbmc_anndata.h5ad"
## AnnData object with n_obs × n_vars = 2638 × 13714
##     obs: 'nCount_RNA', 'nFeature_RNA', 'seurat_annotations', 'percent.mt', 'RNA_snn_res.0.5', 'seurat_clusters', 'percent_mito', 'percent_ribo', 'percent_mito_ribo', 'log10GenesPerUMI', 'percent_top50', 'percent_oxphos', 'percent_apop', 'percent_dna_repair', 'percent_ieg', 'S.Score', 'G2M.Score', 'Phase', 'sample_id'
##     var: 'vst.mean', 'vst.variance', 'vst.variance.expected', 'vst.variance.standardized', 'vst.variable'
##     obsm: 'X_pca', 'X_umap'
##     layers: 'counts'

Convert Seurat assay type within an object

The release of Seurat V5+ has brought about two different types of assay structure that can exist within a Seurat object. However, some community tools that interact with Seurat objects have not been updated to work with both assay formats. Therefore it becomes necessary to change assay format for use with certain tools.

scCustomize provides Convert_Assay() for easy method to convert from Assay>Assay5 (V3/4>5) or Assay5>Assay (V5>V3/4).

Convert V3/4 to V5

# Convert to V5/Assay5 structure
pbmc_V5 <- Convert_Assay(seurat_object = pbmc, convert_to = "V5")

pbmc_V5[["RNA"]]
## Assay (v5) data with 13714 features for 2638 cells
## Top 10 variable features:
##  PPBP, LYZ, S100A9, IGLL5, GNLY, FTL, PF4, FTH1, GNG11, S100A8 
## Layers:
##  data, counts, scale.data

Convert V5 > V3/4

# Convert to V3/4/Assay structure
pbmc_V3 <- Convert_Assay(seurat_object = pbmc_V5, convert_to = "V3")

pbmc_V3[["RNA"]]
## Assay data with 13714 features for 2638 cells
## Top 10 variable features:
##  PPBP, LYZ, S100A9, IGLL5, GNLY, FTL, PF4, FTH1, GNG11, S100A8

Accepted values for convert_to

Convert_Assay will accept a range of accepted values for the convert_to parameter to specify desired format.

V3_Assay_Options V5_Assay5_Options
Assay Assay5
assay assay5
V3 V5
v3 v5