## ----general-setup, echo = FALSE, message = FALSE-----------------------------
knitr::opts_chunk$set(echo = TRUE, message = FALSE)
library(BiocStyle)

## ----install, eval = FALSE----------------------------------------------------
# #' Install BiocManager if not already installed
# install.packages("BiocManager")
# 
# #' Install SpectraStash from Bioconductor
# BiocManager::install("SpectraStash")

## ----setup, message = FALSE---------------------------------------------------
library(Spectra)
library(SpectraStash)
library(MsDataHub)
fls <- c(X20171016_POOL_POS_1_105.134.mzML(),
         X20171016_POOL_POS_3_105.134.mzML())
sps <- Spectra(fls)
sps

## ----filter-------------------------------------------------------------------
sps <- filterRt(sps, c(20, 200))
sps <- filterMzRange(sps, c(110, 120))
sps

## ----save-alabaster-----------------------------------------------------------
#' Define the location of the stash
d <- file.path(tempfile(), "spectra_stash")

#' Configure the format and location
ap <- AlabasterParam(d)

#' Save the `Spectra` object to the stash
saveMsObject(sps, ap)

## ----show-directory-content---------------------------------------------------
library(fs)
dir_tree(d)

## ----read-alabaster-----------------------------------------------------------
res <- readMsObject(Spectra(), ap)
res

## ----read-alabaster-with-alabaster--------------------------------------------
library(alabaster.base)
be <- readObject(file.path(d, "backend"))
be

## ----read-backend-------------------------------------------------------------
be <- readMsObject(MsBackendMzR(), AlabasterParam(file.path(d, "backend")))
be

## ----consolidate-save-alabaster-----------------------------------------------
d2 <- file.path(tempdir(), "spectra_stash2")
saveMsObject(sps, AlabasterParam(d2), consolidate = TRUE)

## ----consolidate-show-directory-content---------------------------------------
dir_tree(d2)

## ----memory-backend-----------------------------------------------------------
sps <- setBackend(sps, MsBackendMemory())
sps

## ----memory-save-alabaster, warning = FALSE-----------------------------------
#' Remove the existing SepctraStash
unlink(d2, recursive = TRUE)

#' Store the `Spectra` object in alabaster format
saveMsObject(sps, AlabasterParam(d2))

## ----memory-show-directory----------------------------------------------------
dir_tree(d2)

## ----memory-read-alabaster----------------------------------------------------
readMsObject(Spectra(), AlabasterParam(d2))

## ----memory-read-alabaster-backend--------------------------------------------
readMsObject(MsBackendMemory(), AlabasterParam(file.path(d2, "backend")))

## ----memory-read-alabaster-backend-backend------------------------------------
readMsObject(MsBackendHdf5Peaks(),
             AlabasterParam(file.path(d2, "backend", "backend")))

## ----session-info-------------------------------------------------------------
sessionInfo()

