---
title: "Using anndataR"
author:
  - name: Robrecht Cannoodt
  - name: Luke Zappia
  - name: Martin Morgan
  - name: Louise Deconinck
package: anndataR
output: BiocStyle::html_document
vignette: >
  %\VignetteIndexEntry{Using anndataR to read and convert}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

# already load SeuratObject and SingleCellExperiment
# so warnings generated by these packages are not shown
library(SeuratObject)
library(SingleCellExperiment)
```

**{anndataR}** 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:
```{r, eval = FALSE}
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 fetch an example `.h5ad` file included in the package:

```{r}
library(anndataR)

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

Read an h5ad file in memory:

```{r}
adata <- read_h5ad(h5ad_path)
```

Read an h5ad file on disk:

```{r}
adata <- read_h5ad(h5ad_path, to = "HDF5AnnData")
```

View structure:

```{r}
adata
```

Access AnnData slots:

```{r}
dim(adata$X)
adata$obs[1:5, 1:6]
adata$var[1:5, 1:6]
```

## Interoperability

Convert the AnnData object to a SingleCellExperiment object:

```{r}
sce <- adata$to_SingleCellExperiment()
sce
```

Convert the AnnData object to a Seurat object:

```{r}
obj <- adata$to_Seurat()
obj
```

## Manually create an object

```{r}
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
```

## Session info

```{r}
sessionInfo()
```
