---
title: Saving `SingleCellExperiment`s to artifacts and back again
author:
- name: Aaron Lun
  email: infinite.monkeys.with.keyboards@gmail.com
package: alabaster.sce
date: "Revised: November 29, 2023"
output:
  BiocStyle::html_document
vignette: >
  %\VignetteIndexEntry{Saving and loading SingleCellExperiments}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, echo=FALSE}
library(BiocStyle)
self <- Biocpkg("alabaster.sce")
knitr::opts_chunk$set(error=FALSE, warning=FALSE, message=FALSE)
```

# Overview 

The `r self` package implements methods to save `SingleCellExperiment` objects to file artifacts and load them back into R.
Check out the `r Biocpkg("alabaster.base")` for more details on the motivation and concepts of the **alabaster** framework.

# Quick start

Given a `SingleCellExperiment`, we can use `saveObject()` to save it inside a staging directory:

```{r}
library(SingleCellExperiment)
mat <- matrix(rpois(10000, 10), ncol=10)
colnames(mat) <- letters[1:10]
rownames(mat) <- sprintf("GENE_%i", seq_len(nrow(mat)))

sce <- SingleCellExperiment(list(counts=mat))
sce$stuff <- LETTERS[1:10]
sce$blah <- runif(10)
reducedDims(sce) <- list(
 PCA=matrix(rnorm(ncol(sce)*10), ncol=10),
 TSNE=matrix(rnorm(ncol(sce)*2), ncol=2)
)
altExps(sce) <- list(spikes=SummarizedExperiment(list(counts=mat[1:2,])))
sce

library(alabaster.sce)
tmp <- tempfile()
saveObject(sce, tmp)

list.files(tmp, recursive=TRUE)
```

We can then load it back into the session with `loadObject()`.

```{r}
roundtrip <- readObject(tmp)
class(roundtrip)
```

# Session information {-}

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

