The HDF5Array
package provides an on-disk representation
of large datasets without the need to load them into memory. Convenient
lazy evaluation operations allow the user to manipulate such large data
files based on metadata. The DelayedMatrix
class in the
DelayedArray
package provides a way to connect to a large
matrix that is stored on disk.
First, we create a small matrix for constructing the
DelayedMatrix
class.
We add rownames and column names to the matrix object for
compatibility with the MultiAssayExperiment
representation.
rownames(smallMatrix) <- paste0("GENE", seq_len(nrow(smallMatrix)))
colnames(smallMatrix) <- paste0("SampleID", seq_len(ncol(smallMatrix)))
Here we use the DelayedArray
constructor function to
create a DelayedMatrix
object.
## [1] "DelayedMatrix"
## attr(,"package")
## [1] "DelayedArray"
## <50000 x 20> DelayedMatrix object of type "double":
## SampleID1 SampleID2 SampleID3 ... SampleID19 SampleID20
## GENE1 -0.045763344 1.134570311 -0.581182393 . -2.36863936 -1.92232521
## GENE2 -0.183560224 1.968817866 -0.679467990 . -1.03872221 1.55700976
## GENE3 0.378046791 0.007599725 -1.445975892 . -1.01842078 1.72729018
## GENE4 0.311803902 -0.773061212 -0.448818120 . -0.01509106 -1.67974616
## GENE5 -0.840560583 -1.892864177 -0.651014886 . -0.87820842 0.40301442
## ... . . . . . .
## GENE49996 0.97383174 0.29944838 -0.15119584 . 0.02872422 2.44844849
## GENE49997 -0.25086555 1.19247395 0.97800354 . 0.21228438 0.03567966
## GENE49998 -0.88269692 1.35238794 -1.10779374 . 1.39325251 -0.24751297
## GENE49999 -1.41501954 -1.99114661 0.79871603 . 0.79936782 1.14103137
## GENE50000 -0.95884847 0.75513191 0.08065146 . 1.01977106 0.14265662
## [1] 50000 20
Finally, the rhdf5
package stores dimnames
in a standard location.
In order to make use of this functionality, we would use
writeHDF5Array
with the with.dimnames
argument:
testh5 <- tempfile(fileext = ".h5")
writeHDF5Array(smallMatrix, filepath = testh5, name = "smallMatrix",
with.dimnames = TRUE)
## <50000 x 20> HDF5Matrix object of type "double":
## SampleID1 SampleID2 SampleID3 ... SampleID19 SampleID20
## GENE1 -0.045763344 1.134570311 -0.581182393 . -2.36863936 -1.92232521
## GENE2 -0.183560224 1.968817866 -0.679467990 . -1.03872221 1.55700976
## GENE3 0.378046791 0.007599725 -1.445975892 . -1.01842078 1.72729018
## GENE4 0.311803902 -0.773061212 -0.448818120 . -0.01509106 -1.67974616
## GENE5 -0.840560583 -1.892864177 -0.651014886 . -0.87820842 0.40301442
## ... . . . . . .
## GENE49996 0.97383174 0.29944838 -0.15119584 . 0.02872422 2.44844849
## GENE49997 -0.25086555 1.19247395 0.97800354 . 0.21228438 0.03567966
## GENE49998 -0.88269692 1.35238794 -1.10779374 . 1.39325251 -0.24751297
## GENE49999 -1.41501954 -1.99114661 0.79871603 . 0.79936782 1.14103137
## GENE50000 -0.95884847 0.75513191 0.08065146 . 1.01977106 0.14265662
To see the file structure we use h5ls
:
## group name otype dclass dim
## 0 / .smallMatrix_dimnames H5I_GROUP
## 1 /.smallMatrix_dimnames 1 H5I_DATASET STRING 50000
## 2 /.smallMatrix_dimnames 2 H5I_DATASET STRING 20
## 3 / smallMatrix H5I_DATASET FLOAT 50000 x 20
Note that a large matrix from an HDF5 file can also be loaded using
the HDF5ArraySeed
and DelayedArray
functions.
hdf5Data <- HDF5ArraySeed(file = testh5, name = "smallMatrix")
newDelayedMatrix <- DelayedArray(hdf5Data)
class(newDelayedMatrix)
## [1] "HDF5Matrix"
## attr(,"package")
## [1] "HDF5Array"
## <50000 x 20> HDF5Matrix object of type "double":
## SampleID1 SampleID2 SampleID3 ... SampleID19 SampleID20
## GENE1 -0.045763344 1.134570311 -0.581182393 . -2.36863936 -1.92232521
## GENE2 -0.183560224 1.968817866 -0.679467990 . -1.03872221 1.55700976
## GENE3 0.378046791 0.007599725 -1.445975892 . -1.01842078 1.72729018
## GENE4 0.311803902 -0.773061212 -0.448818120 . -0.01509106 -1.67974616
## GENE5 -0.840560583 -1.892864177 -0.651014886 . -0.87820842 0.40301442
## ... . . . . . .
## GENE49996 0.97383174 0.29944838 -0.15119584 . 0.02872422 2.44844849
## GENE49997 -0.25086555 1.19247395 0.97800354 . 0.21228438 0.03567966
## GENE49998 -0.88269692 1.35238794 -1.10779374 . 1.39325251 -0.24751297
## GENE49999 -1.41501954 -1.99114661 0.79871603 . 0.79936782 1.14103137
## GENE50000 -0.95884847 0.75513191 0.08065146 . 1.01977106 0.14265662
DelayedMatrix
with
MultiAssayExperiment
A DelayedMatrix
alone conforms to the
MultiAssayExperiment
API requirements. Shown below, the
DelayedMatrix
can be put into a named list
and
passed into the MultiAssayExperiment
constructor
function.
## DataFrame with 20 rows and 3 columns
## assay primary colname
## <factor> <character> <character>
## 1 smallMatrix SampleID1 SampleID1
## 2 smallMatrix SampleID2 SampleID2
## 3 smallMatrix SampleID3 SampleID3
## 4 smallMatrix SampleID4 SampleID4
## 5 smallMatrix SampleID5 SampleID5
## ... ... ... ...
## 16 smallMatrix SampleID16 SampleID16
## 17 smallMatrix SampleID17 SampleID17
## 18 smallMatrix SampleID18 SampleID18
## 19 smallMatrix SampleID19 SampleID19
## 20 smallMatrix SampleID20 SampleID20
## DataFrame with 20 rows and 0 columns
SummarizedExperiment
with DelayedMatrix
backendA more information rich DelayedMatrix
can be created
when used in conjunction with the SummarizedExperiment
class and it can even include rowRanges
. The flexibility of
the MultiAssayExperiment
API supports classes with minimal
requirements. Additionally, this SummarizedExperiment
with
the DelayedMatrix
backend can be part of a bigger
MultiAssayExperiment
object. Below is a minimal example of
how this would work:
## <50000 x 20> DelayedMatrix object of type "double":
## SampleID1 SampleID2 SampleID3 ... SampleID19 SampleID20
## GENE1 -0.045763344 1.134570311 -0.581182393 . -2.36863936 -1.92232521
## GENE2 -0.183560224 1.968817866 -0.679467990 . -1.03872221 1.55700976
## GENE3 0.378046791 0.007599725 -1.445975892 . -1.01842078 1.72729018
## GENE4 0.311803902 -0.773061212 -0.448818120 . -0.01509106 -1.67974616
## GENE5 -0.840560583 -1.892864177 -0.651014886 . -0.87820842 0.40301442
## ... . . . . . .
## GENE49996 0.97383174 0.29944838 -0.15119584 . 0.02872422 2.44844849
## GENE49997 -0.25086555 1.19247395 0.97800354 . 0.21228438 0.03567966
## GENE49998 -0.88269692 1.35238794 -1.10779374 . 1.39325251 -0.24751297
## GENE49999 -1.41501954 -1.99114661 0.79871603 . 0.79936782 1.14103137
## GENE50000 -0.95884847 0.75513191 0.08065146 . 1.01977106 0.14265662
## A MultiAssayExperiment object of 1 listed
## experiment with a user-defined name and respective class.
## Containing an ExperimentList class object of length 1:
## [1] HDF5SE: SummarizedExperiment with 50000 rows and 20 columns
## Functionality:
## experiments() - obtain the ExperimentList instance
## colData() - the primary/phenotype DataFrame
## sampleMap() - the sample coordination DataFrame
## `$`, `[`, `[[` - extract colData columns, subset, or experiment
## *Format() - convert into a long or wide DataFrame
## assays() - convert ExperimentList to a SimpleList of matrices
## exportClass() - save data to flat files
Additional scenarios are currently in development where an
HDF5Matrix
is hosted remotely. Many opportunities exist
when considering on-disk and off-disk representations of data with
MultiAssayExperiment
.
## 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] HDF5Array_1.35.1 rhdf5_2.51.0
## [3] DelayedArray_0.33.2 SparseArray_1.7.2
## [5] S4Arrays_1.7.1 abind_1.4-8
## [7] Matrix_1.7-1 survminer_0.5.0
## [9] ggpubr_0.6.0 ggplot2_3.5.1
## [11] survival_3.7-0 UpSetR_1.4.0
## [13] RaggedExperiment_1.31.0 MultiAssayExperiment_1.33.1
## [15] SummarizedExperiment_1.37.0 Biobase_2.67.0
## [17] GenomicRanges_1.59.1 GenomeInfoDb_1.43.1
## [19] IRanges_2.41.1 S4Vectors_0.45.2
## [21] BiocGenerics_0.53.3 generics_0.1.3
## [23] MatrixGenerics_1.19.0 matrixStats_1.4.1
## [25] BiocStyle_2.35.0
##
## loaded via a namespace (and not attached):
## [1] gridExtra_2.3 rlang_1.1.4 magrittr_2.0.3
## [4] compiler_4.4.2 vctrs_0.6.5 reshape2_1.4.4
## [7] stringr_1.5.1 pkgconfig_2.0.3 crayon_1.5.3
## [10] fastmap_1.2.0 backports_1.5.0 XVector_0.47.0
## [13] labeling_0.4.3 KMsurv_0.1-5 utf8_1.2.4
## [16] rmarkdown_2.29 markdown_1.13 UCSC.utils_1.3.0
## [19] purrr_1.0.2 xfun_0.49 zlibbioc_1.52.0
## [22] cachem_1.1.0 jsonlite_1.8.9 rhdf5filters_1.19.0
## [25] Rhdf5lib_1.29.0 broom_1.0.7 R6_2.5.1
## [28] bslib_0.8.0 stringi_1.8.4 car_3.1-3
## [31] jquerylib_0.1.4 Rcpp_1.0.13-1 knitr_1.49
## [34] zoo_1.8-12 R.utils_2.12.3 BiocBaseUtils_1.9.0
## [37] splines_4.4.2 R.cache_0.16.0 tidyselect_1.2.1
## [40] yaml_2.3.10 ggtext_0.1.2 lattice_0.22-6
## [43] tibble_3.2.1 plyr_1.8.9 withr_3.0.2
## [46] evaluate_1.0.1 xml2_1.3.6 survMisc_0.5.6
## [49] pillar_1.9.0 BiocManager_1.30.25 carData_3.0-5
## [52] munsell_0.5.1 commonmark_1.9.2 scales_1.3.0
## [55] xtable_1.8-4 glue_1.8.0 maketools_1.3.1
## [58] tools_4.4.2 sys_3.4.3 data.table_1.16.2
## [61] ggsignif_0.6.4 buildtools_1.0.0 grid_4.4.2
## [64] tidyr_1.3.1 colorspace_2.1-1 GenomeInfoDbData_1.2.13
## [67] Formula_1.2-5 cli_3.6.3 km.ci_0.5-6
## [70] fansi_1.0.6 dplyr_1.1.4 gtable_0.3.6
## [73] R.methodsS3_1.8.2 rstatix_0.7.2 R.rsp_0.46.0
## [76] sass_0.4.9 digest_0.6.37 farver_2.1.2
## [79] htmltools_0.5.8.1 R.oo_1.27.0 lifecycle_1.0.4
## [82] httr_1.4.7 gridtext_0.1.5