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.
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] "OBJECT"
## [2] "other_annotations/OBJECT"
## [3] "other_annotations/list_contents.json.gz"
## [4] "range_annotations/OBJECT"
## [5] "range_annotations/basic_columns.h5"
## [6] "ranges.h5"
## [7] "sequence_information/OBJECT"
## [8] "sequence_information/info.h5"
We can then easily load it back in with
readObject()
.
## GRanges object with 100 ranges and 1 metadata column:
## seqnames ranges strand | score
## <Rle> <IRanges> <Rle> | <numeric>
## [1] chrA 39-76 * | 0.346210
## [2] chrA 74 * | 0.355536
## [3] chrA 68-91 * | 0.127136
## [4] chrA 13-16 * | 0.848986
## [5] chrA 2-94 * | 0.465918
## ... ... ... ... . ...
## [96] chrA 99-128 * | 0.8519866
## [97] chrA 29-73 * | 0.3023444
## [98] chrA 66-131 * | 0.0486946
## [99] chrA 98-103 * | 0.5768558
## [100] chrA 80-134 * | 0.2179305
## -------
## seqinfo: 1 sequence from an unspecified genome
The same can be done for GRangesList
and
CompressedList
subclasses.
Metadata is preserved during this round-trip:
## $genome
## [1] "Aaron"
## DataFrame with 100 rows and 1 column
## score
## <numeric>
## 1 0.346210
## 2 0.355536
## 3 0.127136
## 4 0.848986
## 5 0.465918
## ... ...
## 96 0.8519866
## 97 0.3023444
## 98 0.0486946
## 99 0.5768558
## 100 0.2179305
## Seqinfo object with 1 sequence from an unspecified genome:
## seqnames seqlengths isCircular genome
## chrA 1000 NA <NA>
## R version 4.4.2 (2024-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.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.26.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=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: 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.7.0 alabaster.base_1.7.2 GenomicRanges_1.59.1
## [4] GenomeInfoDb_1.43.2 IRanges_2.41.2 S4Vectors_0.45.2
## [7] BiocGenerics_0.53.3 generics_0.1.3 BiocStyle_2.35.0
##
## loaded via a namespace (and not attached):
## [1] httr_1.4.7 cli_3.6.3 knitr_1.49
## [4] rlang_1.1.4 xfun_0.49 UCSC.utils_1.3.0
## [7] jsonlite_1.8.9 buildtools_1.0.0 htmltools_0.5.8.1
## [10] maketools_1.3.1 sys_3.4.3 sass_0.4.9
## [13] rmarkdown_2.29 evaluate_1.0.1 jquerylib_0.1.4
## [16] fastmap_1.2.0 Rhdf5lib_1.29.0 alabaster.schemas_1.7.0
## [19] yaml_2.3.10 lifecycle_1.0.4 BiocManager_1.30.25
## [22] compiler_4.4.2 Rcpp_1.0.13-1 rhdf5filters_1.19.0
## [25] XVector_0.47.0 rhdf5_2.51.0 digest_0.6.37
## [28] R6_2.5.1 GenomeInfoDbData_1.2.13 bslib_0.8.0
## [31] tools_4.4.2 zlibbioc_1.52.0 cachem_1.1.0