Saving genomic ranges to artifacts and back again

Overview

The alabaster.ranges package implements methods to save genomic ranges (i.e., GRanges and GRangesList objects) to file artifacts and load them back into R. It also supports various CompressedList subclasses, including the somewhat useful CompressedSplitDataFrameList. Check out alabaster.base for more details on the motivation and concepts of the alabaster framework.

Quick start

Given some genomic ranges, we can use saveObject() to save it inside a staging directory:

library(GenomicRanges)
gr <- GRanges("chrA", IRanges(sample(100), width=sample(100)))
mcols(gr)$score <- runif(length(gr))
metadata(gr)$genome <- "Aaron"
seqlengths(gr) <- c(chrA=1000)

library(alabaster.ranges)
tmp <- tempfile()
saveObject(gr, tmp)

list.files(tmp, recursive=TRUE)
## [1] "_environment.json"                      
## [2] "OBJECT"                                 
## [3] "other_annotations/list_contents.json.gz"
## [4] "other_annotations/OBJECT"               
## [5] "range_annotations/basic_columns.h5"     
## [6] "range_annotations/OBJECT"               
## [7] "ranges.h5"                              
## [8] "sequence_information/info.h5"           
## [9] "sequence_information/OBJECT"

We can then easily load it back in with readObject().

roundtrip <- readObject(tmp)
roundtrip
## GRanges object with 100 ranges and 1 metadata column:
##         seqnames    ranges strand |     score
##            <Rle> <IRanges>  <Rle> | <numeric>
##     [1]     chrA     14-69      * |  0.746068
##     [2]     chrA     29-72      * |  0.206309
##     [3]     chrA     13-42      * |  0.360610
##     [4]     chrA    30-112      * |  0.225933
##     [5]     chrA    28-117      * |  0.439400
##     ...      ...       ...    ... .       ...
##    [96]     chrA    63-110      * | 0.4492897
##    [97]     chrA     53-62      * | 0.5059239
##    [98]     chrA     21-39      * | 0.6858022
##    [99]     chrA    65-110      * | 0.1001147
##   [100]     chrA    95-127      * | 0.0781875
##   -------
##   seqinfo: 1 sequence from an unspecified genome

The same can be done for GRangesList and CompressedList subclasses.

Further comments

Metadata is preserved during this round-trip:

metadata(roundtrip)
## $genome
## [1] "Aaron"
mcols(roundtrip)
## DataFrame with 100 rows and 1 column
##         score
##     <numeric>
## 1    0.746068
## 2    0.206309
## 3    0.360610
## 4    0.225933
## 5    0.439400
## ...       ...
## 96  0.4492897
## 97  0.5059239
## 98  0.6858022
## 99  0.1001147
## 100 0.0781875
seqinfo(roundtrip)
## Seqinfo object with 1 sequence from an unspecified genome:
##   seqnames seqnames seqlengths isCircular genome
##   1            chrA       1000         NA   <NA>

Session information

sessionInfo()
## R version 4.6.2 (2026-10-29)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 26.04.1 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.32.so;  LAPACK version 3.12.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [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: Etc/UTC
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] alabaster.ranges_1.13.1 alabaster.base_1.13.4   GenomicRanges_1.65.4   
## [4] Seqinfo_1.3.2           IRanges_2.47.5          S4Vectors_0.51.10      
## [7] BiocGenerics_0.59.12    generics_0.1.4          BiocStyle_2.41.0       
## 
## loaded via a namespace (and not attached):
##  [1] cli_3.6.6                knitr_1.52               rlang_1.3.0             
##  [4] xfun_0.62                otel_0.2.0               jsonlite_2.0.0          
##  [7] buildtools_1.0.0         htmltools_0.5.9          maketools_1.3.2         
## [10] sys_3.4.3                sass_0.4.10              rmarkdown_2.32          
## [13] evaluate_1.0.5           jquerylib_0.1.4          fastmap_1.2.0           
## [16] Rhdf5lib_2.1.0           alabaster.schemas_1.13.0 yaml_2.3.12             
## [19] lifecycle_1.0.5          BiocManager_1.30.27      compiler_4.6.2          
## [22] Rcpp_1.1.2               rhdf5filters_1.25.5      rhdf5_2.57.20           
## [25] digest_0.6.39            R6_2.6.1                 bslib_0.12.0            
## [28] tools_4.6.2              cachem_1.1.0