Contents

1 Introduction

The alabaster.base package (and its family) implements methods to save common Bioconductor objects to file artifacts and load them back into R. This aims to provide a functional equivalent to RDS-based serialization that is:

2 Quick start

To demonstrate, let’s mock up a DataFrame object from the S4Vectors package.

library(S4Vectors)
df <- DataFrame(X=1:10, Y=letters[1:10])
df
## DataFrame with 10 rows and 2 columns
##            X           Y
##    <integer> <character>
## 1          1           a
## 2          2           b
## 3          3           c
## 4          4           d
## 5          5           e
## 6          6           f
## 7          7           g
## 8          8           h
## 9          9           i
## 10        10           j

We’ll save this DataFrame to file inside a “staging directory”:

staging <- tempfile()

library(alabaster.base)
quickStageObject(df, staging, "my_favorite_df")

And reading it back in:

quickLoadObject(staging, "my_favorite_df")
## DataFrame with 10 rows and 2 columns
##            X           Y
##    <integer> <character>
## 1          1           a
## 2          2           b
## 3          3           c
## 4          4           d
## 5          5           e
## 6          6           f
## 7          7           g
## 8          8           h
## 9          9           i
## 10        10           j

3 Class-specific methods

Each class implements a staging and loading method for use inside the alabaster framework. The staging method (for the stageObject() generic) will save the object to one or more files, given a staging directory and a path inside it:

tmp <- tempfile()
dir.create(tmp)

# DataFrame method already defined for the stageObject generic:
meta <- stageObject(df, tmp, path="my_df")
str(meta)
## List of 5
##  $ $schema       : chr "csv_data_frame/v1.json"
##  $ path          : chr "my_df/simple.csv.gz"
##  $ is_child      : logi FALSE
##  $ data_frame    :List of 6
##   ..$ columns    :List of 2
##   .. ..$ :List of 2
##   .. .. ..$ name: chr "X"
##   .. .. ..$ type: chr "integer"
##   .. ..$ :List of 2
##   .. .. ..$ name: chr "Y"
##   .. .. ..$ type: chr "string"
##   ..$ row_names  : logi FALSE
##   ..$ column_data: NULL
##   ..$ other_data : NULL
##   ..$ dimensions : int [1:2] 10 2
##   ..$ version    : num 2
##  $ csv_data_frame:List of 1
##   ..$ compression: chr "gzip"
# Writing the metadata to file.
invisible(writeMetadata(meta, tmp))

list.files(tmp, recursive=TRUE)
## [1] "my_df/simple.csv.gz"      "my_df/simple.csv.gz.json"

Conversely, the loading function will - as the name suggests - load the object back into memory, given the staging directory and the file’s metadata. The correct loading function for each class is automatically called by the loadObject() function:

remeta <- acquireMetadata(tmp, "my_df/simple.csv.gz")
loadObject(remeta, tmp)
## DataFrame with 10 rows and 2 columns
##            X           Y
##    <integer> <character>
## 1          1           a
## 2          2           b
## 3          3           c
## 4          4           d
## 5          5           e
## 6          6           f
## 7          7           g
## 8          8           h
## 9          9           i
## 10        10           j

alabaster.base itself supports a small set of classes from the S4Vectors packages; support for additional classes can be found in other packages like alabaster.ranges and alabaster.se. Third-party developers can also add support for their own classes by defining new methods, see the Extensions vignette for details.

Incidentally, readLocalObject() and saveLocalObject() are just convenience wrappers acround stageObject() and loadObject(), respectively.

4 What’s a staging directory?

For some concept of a “project”, the staging directory contains all of the artifacts generated for that project. Multiple objects can be saved into a single staging directory, usually in separate subdirectories to avoid conflicts. For example:

# Creating a nested DF to be a little spicy:
df2 <- DataFrame(Z=factor(1:5), AA=I(DataFrame(B=runif(5), C=rnorm(5))))

meta2 <- stageObject(df2, tmp, path="my_df2")
list.files(tmp, recursive=TRUE)
## [1] "my_df/simple.csv.gz"               "my_df/simple.csv.gz.json"         
## [3] "my_df2/column1/simple.txt.gz"      "my_df2/column1/simple.txt.gz.json"
## [5] "my_df2/column2/simple.csv.gz"      "my_df2/column2/simple.csv.gz.json"
## [7] "my_df2/simple.csv.gz"

We use the staging directory as a root for the hard-coded paths in the metadata returned by stageObject() (i.e., meta and meta2). Thus, we can easily copy the entire directory to a new file system and everything will still be correctly referenced.

meta2$path
## [1] "my_df2/simple.csv.gz"
new.dir <- tempfile()
dir.create(new.dir)
invisible(file.copy(tmp, new.dir, recursive=TRUE))
loadObject(meta2, file.path(new.dir, basename(tmp)))
## DataFrame with 5 rows and 2 columns
##          Z                 AA
##   <factor>        <DataFrame>
## 1        1 0.210833:-0.683045
## 2        2 0.645253: 0.716689
## 3        3 0.246018: 0.294770
## 4        4 0.813641:-1.181135
## 5        5 0.550318: 0.922041

The simplest way to share objects is to just zip or tar the staging directory for ad hoc distribution. For more serious applications, alabaster.base can be used in conjunction with centralized file storage systems - see the Applications vignette for more details.

5 Saving and validating metadata

Each file artifact is associated with JSON-formatted metadata, denoted by a file with an additional .json suffix. This defines the interpretation of the contents of the file as well as pointing to other files that are required to represent the corresponding object in an R session. Users can use the .writeMetadata() utility to quickly write the metadata to an appropriate location inside the staging directory.

# Writing the metadata to file.
invisible(writeMetadata(meta, dir=tmp))
invisible(writeMetadata(meta2, dir=tmp))

# Reading a snippet.
meta.path <- file.path(tmp, paste0(meta$path, ".json"))
cat(head(readLines(meta.path), 20), sep="\n")
## {
##   "$schema": "csv_data_frame/v1.json",
##   "path": "my_df/simple.csv.gz",
##   "is_child": false,
##   "data_frame": {
##     "columns": [
##       {
##         "name": "X",
##         "type": "integer"
##       },
##       {
##         "name": "Y",
##         "type": "string"
##       }
##     ],
##     "row_names": false,
##     "dimensions": [10, 2],
##     "version": 2
##   },
##   "csv_data_frame": {

The JSON metadata for each artifact follows the constraints of the schema specified in the $schema property. This schema determines what fields can be stored and which are mandatory; the loading method uses such fields to reliably restore the object into memory. .writeMetadata() will automatically validate the metadata against the schema to ensure that the contents are appropriate.

meta.fail <- meta
meta.fail[["data_frame"]][["columns"]] <- NULL
try(writeMetadata(meta.fail, dir=tmp))
## Error : 1 error validating json:
##  - /data_frame (#/properties/data_frame/required): must have required property 'columns'

The schema will also list the exact R function that is required to load an object into memory. This means that developers can create third-party extensions for new data structures by defining new schemas, without requiring changes to alabaster.base’s code. Indeed, loadObject() automatically uses the schema to determine how it should load the files back into memory:

re.read <- acquireMetadata(tmp, "my_df2/simple.csv.gz")
loadObject(re.read, tmp)
## DataFrame with 5 rows and 2 columns
##          Z                 AA
##   <factor>        <DataFrame>
## 1        1 0.210833:-0.683045
## 2        2 0.645253: 0.716689
## 3        3 0.246018: 0.294770
## 4        4 0.813641:-1.181135
## 5        5 0.550318: 0.922041

Session information

sessionInfo()
## R version 4.3.2 (2023-10-31)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.18-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: America/New_York
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] S4Vectors_0.40.1     BiocGenerics_0.48.1  Matrix_1.6-1.1      
## [4] alabaster.base_1.2.1 BiocStyle_2.30.0    
## 
## loaded via a namespace (and not attached):
##  [1] jsonlite_1.8.7          compiler_4.3.2          BiocManager_1.30.22    
##  [4] crayon_1.5.2            Rcpp_1.0.11             rhdf5filters_1.14.1    
##  [7] alabaster.matrix_1.2.0  jquerylib_0.1.4         IRanges_2.36.0         
## [10] yaml_2.3.7              fastmap_1.1.1           lattice_0.22-5         
## [13] jsonvalidate_1.3.2      R6_2.5.1                XVector_0.42.0         
## [16] S4Arrays_1.2.0          curl_5.1.0              knitr_1.45             
## [19] DelayedArray_0.28.0     bookdown_0.36           MatrixGenerics_1.14.0  
## [22] bslib_0.5.1             rlang_1.1.2             V8_4.4.0               
## [25] cachem_1.0.8            HDF5Array_1.30.0        xfun_0.41              
## [28] sass_0.4.7              SparseArray_1.2.1       cli_3.6.1              
## [31] Rhdf5lib_1.24.0         zlibbioc_1.48.0         digest_0.6.33          
## [34] grid_4.3.2              alabaster.schemas_1.2.0 rhdf5_2.46.0           
## [37] evaluate_0.23           abind_1.4-5             rmarkdown_2.25         
## [40] matrixStats_1.0.0       tools_4.3.2             htmltools_0.5.7