---
title: Saving `TreeSummarizedExperiment`s to artifacts and back again
date: "Revised: October 29, 2026"
format:
  html:
    toc: true
    number-sections: true
    embed-resources: true
vignette: >
  %\VignetteIndexEntry{Saving and loading TreeSummarizedExperiments}
  %\VignetteEngine{quarto::html}
  %\VignetteEncoding{UTF-8}
knitr:
  opts_chunk:
    error: false
    warning: false
    message: false
---

```{r setup}
#| echo: false
library(BiocStyle)
self <- Biocpkg("alabaster.treese")
```

# Overview 

The `r self` package implements methods to save `TreeSummarizedExperiment`
objects to file artifacts and load them back into R. It extends
`r Biocpkg("alabaster.sce")`, which handles everything from the
`SingleCellExperiment` parent class. Check out the `r Biocpkg("alabaster.base")`
for more details on the motivation and concepts of the **alabaster** framework.

# Quick start

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

```{r}
#| label: save
# Mock up a TreeSE with a row tree and reference sequences.
library(TreeSummarizedExperiment)
data(tinyTree)
mat <- matrix(rpois(100, 10), ncol = 10)
rownames(mat) <- tinyTree[["tip.label"]]
colnames(mat) <- letters[1:10]

tse <- TreeSummarizedExperiment(list(counts = mat), rowTree = tinyTree)
tse[["stuff"]] <- LETTERS[1:10]
referenceSeq(tse) <- Biostrings::DNAStringSet(rep("ACGT", nrow(tse)))
tse

# Save it into a staging directory.
library(alabaster.treese)
tmp <- tempfile()
saveObject(tse, tmp)

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

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

```{r}
#| label: read
# Load the TreeSE back from disk.
roundtrip <- readObject(tmp)
class(roundtrip)
```

# On-disk representation

Each tree in `rowTree()` or `colTree()` is saved as a list containing the
fields of its `phylo` object, with the `edge` matrix split into `parent` and
`child` vectors. We do not use Newick as it does not preserve the node numbering
(which is referenced by `rowLinks()` and `colLinks()`) or labels containing
special characters. Users who need a Newick file for other tools can always
create one with `ape::write.tree()`. The links themselves are saved as data
frames, while the `referenceSeq()` is saved as FASTA via
`r Biocpkg("alabaster.string")`.

# Session information {.unnumbered}

```{r}
#| label: session-info
sessionInfo()
```
