Skip to contents

{anndataR} allows users to work with .h5ad files, access various slots in the datasets and convert these files to SingleCellExperiment objects and SeuratObjects, and vice versa.

Check out ?anndataR for a full list of the functions provided by this package.

Installation

Install using:

if (!require("pak", quietly = TRUE)) {
  install.packages("pak")
}
pak::pak("scverse/anndataR")

Usage

Here’s a quick example of how to use {anndataR}. First, we download an h5ad file.

library(anndataR)

h5ad_path <- system.file("extdata", "example.h5ad", package = "anndataR")

Read an h5ad file in memory:

adata <- read_h5ad(h5ad_path)

Read an h5ad file on disk:

adata <- read_h5ad(h5ad_path, to = "HDF5AnnData")

View structure:

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'
#>     obsm: 'X_pca', 'X_umap'
#>     varm: 'PCs'
#>     layers: 'counts', 'csc_counts', 'dense_X', 'dense_counts'
#>     obsp: 'connectivities', 'distances'

Access AnnData slots:

dim(adata$X)
#> [1]  50 100
adata$obs[1:5, 1:6]
#>         Float FloatNA Int IntNA  Bool BoolNA
#> Cell000 42.42     NaN   0    NA FALSE  FALSE
#> Cell001 42.42   42.42   1    42  TRUE     NA
#> Cell002 42.42   42.42   2    42  TRUE   TRUE
#> Cell003 42.42   42.42   3    42  TRUE   TRUE
#> Cell004 42.42   42.42   4    42  TRUE   TRUE
adata$var[1:5, 1:6]
#>          String n_cells_by_counts mean_counts log1p_mean_counts pct_dropout_by_counts total_counts
#> Gene000 String0                44        1.94          1.078410                    12           97
#> Gene001 String1                42        2.04          1.111858                    16          102
#> Gene002 String2                43        2.12          1.137833                    14          106
#> Gene003 String3                41        1.72          1.000632                    18           86
#> Gene004 String4                42        2.06          1.118415                    16          103

Interoperability

Convert the AnnData object to a SingleCellExperiment object:

sce <- adata$to_SingleCellExperiment()
sce
#> class: SingleCellExperiment 
#> dim: 100 50 
#> metadata(0):
#> assays(5): X counts csc_counts dense_X dense_counts
#> rownames(100): Gene000 Gene001 ... Gene098 Gene099
#> rowData names(11): String n_cells_by_counts ... dispersions dispersions_norm
#> colnames(50): Cell000 Cell001 ... Cell048 Cell049
#> colData names(11): Float FloatNA ... log1p_total_counts leiden
#> reducedDimNames(0):
#> mainExpName: NULL
#> altExpNames(0):

Convert the AnnData object to a Seurat object:

obj <- adata$to_Seurat()
obj
#> An object of class Seurat 
#> 500 features across 50 samples within 5 assays 
#> Active assay: RNA (100 features, 0 variable features)
#>  2 layers present: counts, data
#>  4 other assays present: counts, csc_counts, dense_X, dense_counts

Manually create an object

adata <- AnnData(
  X = matrix(rnorm(100), nrow = 10),
  obs = data.frame(
    cell_type = factor(rep(c("A", "B"), each = 5))
  ),
  var = data.frame(
    gene_name = paste0("gene_", 1:10)
  )
)

adata
#> AnnData object with n_obs × n_vars = 10 × 10
#>     obs: 'cell_type'
#>     var: 'gene_name'

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] SingleCellExperiment_1.28.1 SummarizedExperiment_1.36.0
#>  [3] Biobase_2.66.0              GenomicRanges_1.58.0       
#>  [5] GenomeInfoDb_1.42.0         IRanges_2.40.0             
#>  [7] S4Vectors_0.44.0            BiocGenerics_0.52.0        
#>  [9] MatrixGenerics_1.18.0       matrixStats_1.4.1          
#> [11] SeuratObject_5.0.2          sp_2.1-4                   
#> [13] BiocStyle_2.34.0           
#> 
#> loaded via a namespace (and not attached):
#>  [1] sass_0.4.9              future_1.34.0           generics_0.1.3         
#>  [4] SparseArray_1.6.0       lattice_0.22-6          listenv_0.9.1          
#>  [7] digest_0.6.37           evaluate_1.0.1          grid_4.4.2             
#> [10] bookdown_0.41           fastmap_1.2.0           jsonlite_1.8.9         
#> [13] Matrix_1.7-1            BiocManager_1.30.25     httr_1.4.7             
#> [16] spam_2.11-0             UCSC.utils_1.2.0        codetools_0.2-20       
#> [19] textshaping_0.4.0       jquerylib_0.1.4         abind_1.4-8            
#> [22] cli_3.6.3               crayon_1.5.3            rlang_1.1.4            
#> [25] XVector_0.46.0          parallelly_1.39.0       future.apply_1.11.3    
#> [28] DelayedArray_0.32.0     cachem_1.1.0            yaml_2.3.10            
#> [31] S4Arrays_1.6.0          tools_4.4.2             parallel_4.4.2         
#> [34] GenomeInfoDbData_1.2.13 globals_0.16.3          R6_2.5.1               
#> [37] lifecycle_1.0.4         zlibbioc_1.52.0         fs_1.6.5               
#> [40] htmlwidgets_1.6.4       ragg_1.3.3              desc_1.4.3             
#> [43] progressr_0.15.0        pkgdown_2.1.1           bslib_0.8.0            
#> [46] Rcpp_1.0.13-1           systemfonts_1.1.0       xfun_0.49              
#> [49] knitr_1.49              htmltools_0.5.8.1       rmarkdown_2.29         
#> [52] dotCall64_1.2           compiler_4.4.2