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

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

# Overview 

The `r self` package implements methods to save `XStringSet` 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 an `XStringSet`, we can use `saveObject()` to save it inside a staging directory:

```{r}
library(Biostrings)
x <- DNAStringSet(c(seq1="CTCNACCAGTAT", seq2="TTGA", seq3="TACCTAGAG"))
mcols(x)$score <- runif(length(x))
x

library(alabaster.string)
tmp <- tempfile()
saveObject(x, tmp)

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

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

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

More details on the metadata and on-disk layout are provided in the [schema](https://artifactdb.github.io/BiocObjectSchemas/html/sequence_string_set/v1.html).

# Quality scaled strings

The same approach works with `QualityScaledXStringSet` objects:

```{r}
x <- DNAStringSet(c("TTGA", "CTCN"))
q <- PhredQuality(c("*+,-", "6789"))
y <- QualityScaledDNAStringSet(x, q)

library(alabaster.string)
tmp <- tempfile()
saveObject(y, tmp)

roundtrip <- readObject(tmp)
class(roundtrip)
```

# Session information {-}

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

