Using anndataR to read and convert
Robrecht Cannoodt
Luke Zappia
Martin Morgan
Louise Deconinck
Source:vignettes/getting-started.Rmd
getting-started.Rmd
Introduction
This package allows users to work with .h5ad
files,
access various slots in the datasets and convert these files to
SingleCellExperiment
objects and
SeuratObject
s, and vice versa.
Check out ?anndataR
for a full list of the functions
provided by this package.
Installation
Install using:
if (!require("BiocManager", quietly = TRUE)) {
install.packages("BiocManager")
}
BiocManager::install("anndataR")
Usage
This package provides an abstract interface for AnnData
objects. This abstract interface models its Python counterpart closely,
and stores a data matrix X
and annotations corresponding to
observations (obs
, obsm
, obsp
)
and variables (var
, varm
, varp
)
and unstructured metadata uns
.
This abstract interface is implemented by different backends. Currently, the following backends are implemented:
InMemoryAnnData
HDF5AnnData
The InMemoryAnnData
backend allows you to construct an
AnnData
object in memory. The HDF5AnnData
backend allows you to read in an AnnData
object from an
.h5ad
file.
HDF5AnnData
backend
Here is an example of how to read in an .h5ad
file.
file <- system.file("extdata", "example.h5ad", package = "anndataR")
adata <- read_h5ad(file, to = "InMemoryAnnData")
The contents can be accessed as well:
X <- adata$X
layers <- adata$layers
obs <- adata$obs
var <- adata$var
InMemoryAnnData
backend
The following example details how to construct an InMemoryAnnData and access its contents.
adata <- AnnData(
X = matrix(1:15, 3L, 5L),
layers = list(
A = matrix(5:1, 3L, 5L),
B = matrix(letters[1:5], 3L, 5L)
),
obs = data.frame(row.names = LETTERS[1:3], cell = 1:3),
var = data.frame(row.names = letters[1:5], gene = 1:5)
)
adata
#> AnnData object with n_obs × n_vars = 3 × 5
#> obs: 'cell'
#> var: 'gene'
#> layers: 'A', 'B'
The contents can be accessed as well:
X <- adata$X
layers <- adata$layers
obs <- adata$obs
var <- adata$var
You can convert the AnnData object to a SingleCellExperiment object or to a SeuratObject in the following way:
sce <- adata$to_SingleCellExperiment()
seurat <- adata$to_Seurat()
#> Warning in matrix(data = as.numeric(x = x), ncol = nc): NAs introduced by
#> coercion
Session info
sessionInfo()
#> R version 4.4.2 (2024-10-31)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 22.04.5 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.20.so; LAPACK version 3.10.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] stats4 stats graphics grDevices utils datasets methods
#> [8] base
#>
#> other attached packages:
#> [1] anndataR_0.99.0 SingleCellExperiment_1.28.0
#> [3] SummarizedExperiment_1.36.0 Biobase_2.66.0
#> [5] GenomicRanges_1.58.0 GenomeInfoDb_1.42.0
#> [7] IRanges_2.40.0 S4Vectors_0.44.0
#> [9] BiocGenerics_0.52.0 MatrixGenerics_1.18.0
#> [11] matrixStats_1.4.1 SeuratObject_5.0.2
#> [13] sp_2.1-4 BiocStyle_2.34.0
#>
#> loaded via a namespace (and not attached):
#> [1] dotCall64_1.2 spam_2.11-0 xfun_0.49
#> [4] bslib_0.8.0 lattice_0.22-6 tools_4.4.2
#> [7] generics_0.1.3 parallel_4.4.2 Matrix_1.7-1
#> [10] desc_1.4.3 lifecycle_1.0.4 GenomeInfoDbData_1.2.13
#> [13] compiler_4.4.2 textshaping_0.4.0 codetools_0.2-20
#> [16] htmltools_0.5.8.1 sass_0.4.9 yaml_2.3.10
#> [19] pkgdown_2.1.1 crayon_1.5.3 jquerylib_0.1.4
#> [22] cachem_1.1.0 DelayedArray_0.32.0 abind_1.4-8
#> [25] parallelly_1.39.0 digest_0.6.37 future_1.34.0
#> [28] hdf5r_1.3.11 bookdown_0.41 listenv_0.9.1
#> [31] fastmap_1.2.0 grid_4.4.2 cli_3.6.3
#> [34] SparseArray_1.6.0 S4Arrays_1.6.0 future.apply_1.11.3
#> [37] UCSC.utils_1.2.0 bit64_4.5.2 rmarkdown_2.29
#> [40] XVector_0.46.0 httr_1.4.7 globals_0.16.3
#> [43] bit_4.5.0 progressr_0.15.0 ragg_1.3.3
#> [46] evaluate_1.0.1 knitr_1.48 rlang_1.1.4
#> [49] Rcpp_1.0.13-1 BiocManager_1.30.25 jsonlite_1.8.9
#> [52] R6_2.5.1 systemfonts_1.1.0 fs_1.6.5
#> [55] zlibbioc_1.52.0