This vignette demonstrates how to read and write Seurat
objects using the {anndataR} package, leveraging the
interoperability between Seurat
and the
AnnData
format.
Check out ?anndataR
for a full list of the functions
provided by this package.
Introduction
Seurat is a widely used toolkit for single-cell analysis in R.
{anndataR} enables conversion between
Seurat
objects and AnnData
objects, allowing
you to leverage the strengths of both the scverse and Seurat
ecosystems.
Prerequisites
Before you begin, make sure you have both Seurat and {anndataR} installed. You can install them using the following code:
if (!requireNamespace("pak", quietly = TRUE)) {
install.packages("pak")
}
pak::pak("Seurat")
pak::pak("scverse/anndataR")
Converting an AnnData Object to a Seurat Object
Using an example .h5ad
file included in the package, we
will demonstrate how to read an .h5ad
file and convert it
to a Seurat
object.
library(anndataR)
library(Seurat)
#> Loading required package: SeuratObject
#> Loading required package: sp
#> 'SeuratObject' was built under R 4.4.0 but the current version is
#> 4.4.2; it is recomended that you reinstall 'SeuratObject' as the ABI
#> for R may have changed
#>
#> Attaching package: 'SeuratObject'
#> The following objects are masked from 'package:base':
#>
#> intersect, t
h5ad_file <- system.file("extdata", "example.h5ad", package = "anndataR")
Read the .h5ad
file:
adata <- read_h5ad(h5ad_file)
adata
#> AnnData object with n_obs × n_vars = 50 × 100
#> obs: 'Float', 'FloatNA', 'Int', 'IntNA', 'Bool', 'BoolNA', 'n_genes_by_counts', 'log1p_n_genes_by_counts', 'total_counts', 'log1p_total_counts', 'leiden'
#> var: 'String', 'n_cells_by_counts', 'mean_counts', 'log1p_mean_counts', 'pct_dropout_by_counts', 'total_counts', 'log1p_total_counts', 'highly_variable', 'means', 'dispersions', 'dispersions_norm'
#> uns: 'Bool', 'BoolNA', 'Category', 'DataFrameEmpty', 'Int', 'IntNA', 'IntScalar', 'Sparse1D', 'String', 'String2D', 'StringScalar', 'hvg', 'leiden', 'log1p', 'neighbors', 'pca', 'rank_genes_groups', 'umap'
#> obsm: 'X_pca', 'X_umap'
#> varm: 'PCs'
#> layers: 'counts', 'csc_counts', 'dense_X', 'dense_counts'
#> obsp: 'connectivities', 'distances'
Convert to a Seurat
object:
seurat_obj <- adata$to_Seurat()
seurat_obj
#> An object of class Seurat
#> 100 features across 50 samples within 1 assay
#> Active assay: RNA (100 features, 0 variable features)
#> 5 layers present: counts, data, csc_counts, dense_X, dense_counts
#> 2 dimensional reductions calculated: pca, umap
Note that there is no one-to-one mapping possible between the AnnData and SeuratObject data structures, so some information might be lost during conversion. It is recommended to carefully inspect the converted object to ensure that all necessary information has been transferred.
See ?to_Seurat
for more details on how to customize the
conversion process. For instance:
adata$to_Seurat(
assay_name = "ADT",
layers_mapping = c(counts = "dense_counts", data = "dense_X")
)
#> An object of class Seurat
#> 100 features across 50 samples within 1 assay
#> Active assay: ADT (100 features, 0 variable features)
#> 2 layers present: counts, data
#> 2 dimensional reductions calculated: pca, umap
Convert a Seurat Object to an AnnData Object
Here’s an example demonstrating how to create a Seurat
object from scratch, then convert it to AnnData
and save it
as .h5ad
counts <- matrix(rbinom(20000, 1000, .001), nrow = 100)
seurat_obj <- CreateSeuratObject(counts = counts) |>
NormalizeData() |>
FindVariableFeatures() |>
ScaleData() |>
RunPCA(npcs = 10) |>
FindNeighbors() |>
RunUMAP(dims = 1:10)
#> Normalizing layer: counts
#> Finding variable features for layer counts
#> Centering and scaling data matrix
#> PC_ 1
#> Positive: Feature72, Feature61, Feature35, Feature78, Feature52, Feature50, Feature66, Feature8, Feature14, Feature32
#> Feature70, Feature1, Feature56, Feature87, Feature19, Feature97, Feature7, Feature17, Feature86, Feature48
#> Feature81, Feature88, Feature43, Feature67, Feature36, Feature3, Feature26, Feature54, Feature40, Feature33
#> Negative: Feature9, Feature29, Feature4, Feature39, Feature15, Feature27, Feature34, Feature68, Feature62, Feature18
#> Feature22, Feature46, Feature10, Feature74, Feature49, Feature37, Feature12, Feature23, Feature95, Feature100
#> Feature64, Feature30, Feature90, Feature55, Feature47, Feature96, Feature5, Feature31, Feature60, Feature92
#> PC_ 2
#> Positive: Feature41, Feature89, Feature45, Feature98, Feature33, Feature44, Feature68, Feature91, Feature18, Feature59
#> Feature61, Feature86, Feature32, Feature4, Feature51, Feature42, Feature54, Feature67, Feature56, Feature21
#> Feature53, Feature70, Feature8, Feature31, Feature22, Feature14, Feature12, Feature76, Feature62, Feature58
#> Negative: Feature97, Feature13, Feature48, Feature69, Feature49, Feature82, Feature7, Feature5, Feature79, Feature93
#> Feature94, Feature84, Feature23, Feature78, Feature27, Feature40, Feature26, Feature50, Feature81, Feature46
#> Feature29, Feature10, Feature71, Feature72, Feature52, Feature88, Feature87, Feature9, Feature95, Feature16
#> PC_ 3
#> Positive: Feature3, Feature93, Feature2, Feature5, Feature11, Feature79, Feature66, Feature74, Feature50, Feature36
#> Feature78, Feature12, Feature45, Feature98, Feature51, Feature16, Feature83, Feature28, Feature27, Feature9
#> Feature8, Feature95, Feature33, Feature63, Feature21, Feature87, Feature40, Feature53, Feature85, Feature57
#> Negative: Feature58, Feature90, Feature46, Feature81, Feature44, Feature56, Feature69, Feature86, Feature37, Feature97
#> Feature92, Feature26, Feature88, Feature30, Feature96, Feature42, Feature14, Feature25, Feature71, Feature34
#> Feature70, Feature82, Feature23, Feature35, Feature13, Feature19, Feature80, Feature4, Feature29, Feature67
#> PC_ 4
#> Positive: Feature96, Feature11, Feature78, Feature59, Feature31, Feature57, Feature19, Feature80, Feature41, Feature4
#> Feature55, Feature44, Feature60, Feature90, Feature32, Feature50, Feature1, Feature14, Feature9, Feature77
#> Feature85, Feature28, Feature82, Feature99, Feature25, Feature8, Feature27, Feature62, Feature64, Feature29
#> Negative: Feature20, Feature18, Feature81, Feature100, Feature93, Feature10, Feature15, Feature74, Feature83, Feature33
#> Feature58, Feature6, Feature72, Feature89, Feature7, Feature35, Feature53, Feature37, Feature42, Feature43
#> Feature76, Feature46, Feature98, Feature73, Feature52, Feature88, Feature63, Feature49, Feature17, Feature22
#> PC_ 5
#> Positive: Feature77, Feature40, Feature9, Feature21, Feature38, Feature65, Feature80, Feature22, Feature72, Feature18
#> Feature50, Feature71, Feature1, Feature83, Feature82, Feature53, Feature67, Feature63, Feature90, Feature13
#> Feature4, Feature61, Feature56, Feature84, Feature43, Feature54, Feature19, Feature94, Feature37, Feature33
#> Negative: Feature30, Feature51, Feature86, Feature69, Feature45, Feature14, Feature3, Feature26, Feature57, Feature23
#> Feature99, Feature60, Feature66, Feature36, Feature15, Feature68, Feature85, Feature29, Feature78, Feature24
#> Feature16, Feature70, Feature42, Feature41, Feature48, Feature79, Feature10, Feature2, Feature49, Feature97
#> Computing nearest neighbor graph
#> Computing SNN
#> 07:38:11 UMAP embedding parameters a = 0.9922 b = 1.112
#> 07:38:11 Read 200 rows and found 10 numeric columns
#> 07:38:11 Using Annoy for neighbor search, n_neighbors = 30
#> 07:38:11 Building Annoy index with metric = cosine, n_trees = 50
#> 0% 10 20 30 40 50 60 70 80 90 100%
#> [----|----|----|----|----|----|----|----|----|----|
#> **************************************************|
#> 07:38:11 Writing NN index file to temp file /tmp/RtmpdQgYW0/file1f61c1ecf25
#> 07:38:11 Searching Annoy index using 1 thread, search_k = 3000
#> 07:38:12 Annoy recall = 100%
#> 07:38:12 Commencing smooth kNN distance calibration using 1 thread with target n_neighbors = 30
#> 07:38:12 Initializing from normalized Laplacian + noise (using RSpectra)
#> 07:38:12 Commencing optimization for 500 epochs, with 6160 positive edges
#> 07:38:13 Optimization finished
seurat_obj
#> An object of class Seurat
#> 100 features across 200 samples within 1 assay
#> Active assay: RNA (100 features, 100 variable features)
#> 3 layers present: counts, data, scale.data
#> 2 dimensional reductions calculated: pca, umap
You can convert the Seurat
object to an
AnnData
object using the from_Seurat
function:
adata <- from_Seurat(seurat_obj)
adata
#> AnnData object with n_obs × n_vars = 200 × 100
#> obs: 'orig.ident', 'nCount_RNA', 'nFeature_RNA'
#> var: 'vf_vst_counts_mean', 'vf_vst_counts_variance', 'vf_vst_counts_variance.expected', 'vf_vst_counts_variance.standardized', 'vf_vst_counts_variable', 'vf_vst_counts_rank', 'var.features', 'var.features.rank'
#> obsm: 'X_pca', 'X_umap'
#> varm: 'PCs'
#> layers: 'counts', 'data', 'scale.data'
#> obsp: 'connectivities', 'snn'
Again note that there is no one-to-one mapping possible between the AnnData and SeuratObject data structures, so some information might be lost during conversion. It is recommended to carefully inspect the converted object to ensure that all necessary information has been transferred.
See ?from_Seurat
for more details on how to customize
the conversion process. Example:
from_Seurat(
seurat_obj,
assay_name = "RNA",
x_mapping = "data",
layers_mapping = c(foo = "counts")
)
#> AnnData object with n_obs × n_vars = 200 × 100
#> obs: 'orig.ident', 'nCount_RNA', 'nFeature_RNA'
#> var: 'vf_vst_counts_mean', 'vf_vst_counts_variance', 'vf_vst_counts_variance.expected', 'vf_vst_counts_variance.standardized', 'vf_vst_counts_variable', 'vf_vst_counts_rank', 'var.features', 'var.features.rank'
#> obsm: 'X_pca', 'X_umap'
#> varm: 'PCs'
#> layers: 'foo'
#> obsp: 'connectivities', 'snn'
Session info
sessionInfo()
#> R version 4.4.2 (2024-10-31)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.1 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
#>
#> locale:
#> [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
#> [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
#> [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
#> [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: UTC
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] Seurat_5.2.0 SeuratObject_5.0.2 sp_2.1-4 anndataR_0.99.0
#> [5] BiocStyle_2.34.0
#>
#> loaded via a namespace (and not attached):
#> [1] deldir_2.0-4 pbapply_1.7-2 gridExtra_2.3
#> [4] rlang_1.1.4 magrittr_2.0.3 RcppAnnoy_0.0.22
#> [7] spatstat.geom_3.3-4 matrixStats_1.5.0 ggridges_0.5.6
#> [10] compiler_4.4.2 reshape2_1.4.4 png_0.1-8
#> [13] systemfonts_1.1.0 vctrs_0.6.5 hdf5r_1.3.11
#> [16] stringr_1.5.1 pkgconfig_2.0.3 fastmap_1.2.0
#> [19] promises_1.3.2 rmarkdown_2.29 ragg_1.3.3
#> [22] bit_4.5.0.1 purrr_1.0.2 xfun_0.50
#> [25] cachem_1.1.0 jsonlite_1.8.9 goftest_1.2-3
#> [28] later_1.4.1 spatstat.utils_3.1-2 irlba_2.3.5.1
#> [31] parallel_4.4.2 cluster_2.1.6 R6_2.5.1
#> [34] ica_1.0-3 spatstat.data_3.1-4 stringi_1.8.4
#> [37] bslib_0.8.0 RColorBrewer_1.1-3 reticulate_1.40.0
#> [40] spatstat.univar_3.1-1 parallelly_1.41.0 scattermore_1.2
#> [43] lmtest_0.9-40 jquerylib_0.1.4 Rcpp_1.0.14
#> [46] bookdown_0.42 knitr_1.49 tensor_1.5
#> [49] future.apply_1.11.3 zoo_1.8-12 sctransform_0.4.1
#> [52] httpuv_1.6.15 Matrix_1.7-1 splines_4.4.2
#> [55] igraph_2.1.3 tidyselect_1.2.1 abind_1.4-8
#> [58] yaml_2.3.10 spatstat.random_3.3-2 spatstat.explore_3.3-4
#> [61] codetools_0.2-20 miniUI_0.1.1.1 listenv_0.9.1
#> [64] plyr_1.8.9 lattice_0.22-6 tibble_3.2.1
#> [67] shiny_1.10.0 ROCR_1.0-11 evaluate_1.0.3
#> [70] Rtsne_0.17 future_1.34.0 fastDummies_1.7.4
#> [73] desc_1.4.3 survival_3.7-0 polyclip_1.10-7
#> [76] fitdistrplus_1.2-2 pillar_1.10.1 BiocManager_1.30.25
#> [79] KernSmooth_2.23-24 plotly_4.10.4 generics_0.1.3
#> [82] RcppHNSW_0.6.0 ggplot2_3.5.1 munsell_0.5.1
#> [85] scales_1.3.0 globals_0.16.3 xtable_1.8-4
#> [88] glue_1.8.0 lazyeval_0.2.2 tools_4.4.2
#> [91] data.table_1.16.4 RSpectra_0.16-2 RANN_2.6.2
#> [94] fs_1.6.5 dotCall64_1.2 cowplot_1.1.3
#> [97] grid_4.4.2 tidyr_1.3.1 colorspace_2.1-1
#> [100] nlme_3.1-166 patchwork_1.3.0 cli_3.6.3
#> [103] spatstat.sparse_3.1-0 textshaping_0.4.1 spam_2.11-0
#> [106] viridisLite_0.4.2 dplyr_1.1.4 uwot_0.2.2
#> [109] gtable_0.3.6 sass_0.4.9 digest_0.6.37
#> [112] progressr_0.15.1 ggrepel_0.9.6 htmlwidgets_1.6.4
#> [115] farver_2.1.2 htmltools_0.5.8.1 pkgdown_2.1.1
#> [118] lifecycle_1.0.4 httr_1.4.7 mime_0.12
#> [121] bit64_4.5.2 MASS_7.3-61