vignettes/articles/Object_Conversion.Rmd
Object_Conversion.Rmd
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(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)
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.
as.anndata(x = pbmc, file_path = "~/Desktop", file_name = "pbmc_anndata.h5ad")
## • Checking Seurat object validity
## • Extracting Data from RNA assay to transfer to anndata.
## 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', 'percent_hemo', 'S.Score', 'G2M.Score', 'Phase', 'sample_id'
## var: 'names'
## obsm: 'X_pca', 'X_umap'
## layers: 'counts_RNA'
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 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 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