# Using DelayedMatrix with MultiAssayExperiment

# Integrating an HDF5 backend for MultiAssayExperiment

## Dependencies

    library(MultiAssayExperiment)
    library(HDF5Array)
    library(SummarizedExperiment)

## HDF5Array and DelayedArray Constructor

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.

    smallMatrix <- matrix(rnorm(10e5), ncol = 20)

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.

    smallMatrix <- DelayedArray(smallMatrix)
    class(smallMatrix)

    ## [1] "DelayedMatrix"
    ## attr(,"package")
    ## [1] "DelayedArray"

    # show method
    smallMatrix

    ## <50000 x 20> DelayedMatrix object of type "double":
    ##             SampleID1   SampleID2   SampleID3 ...   SampleID19   SampleID20
    ##     GENE1   0.4468647   0.1185882  -0.6007010   .  0.679896517  0.454905055
    ##     GENE2  -2.0121225  -0.3375326  -0.2462585   . -0.721057950  1.202833911
    ##     GENE3   0.1948504  -0.6316396  -1.0474468   .  1.078857416  0.867241673
    ##     GENE4  -1.4723226  -0.3335832   1.0992490   .  0.308318428 -0.203359720
    ##     GENE5  -1.1581171  -1.1048136  -0.4593716   .  0.004504068 -0.999712484
    ##       ...           .           .           .   .            .            .
    ## GENE49996  1.27143151  0.24749585  1.03957397   .   1.88200964  -0.14969630
    ## GENE49997 -1.89637290  0.43362652  0.15556430   .   0.29585744  -0.50909512
    ## GENE49998 -0.09771661 -0.88977805 -0.24784213   .  -0.51253523  -0.82608283
    ## GENE49999 -1.14763135 -1.22362336  1.66505592   .   0.79435530  -0.04415469
    ## GENE50000 -2.05281155 -0.35942735 -0.27799501   .  -1.77924627   0.99225695

    dim(smallMatrix)

    ## [1] 50000    20

## Writing to a file with dimnames

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.4468647   0.1185882  -0.6007010   .  0.679896517  0.454905055
    ##     GENE2  -2.0121225  -0.3375326  -0.2462585   . -0.721057950  1.202833911
    ##     GENE3   0.1948504  -0.6316396  -1.0474468   .  1.078857416  0.867241673
    ##     GENE4  -1.4723226  -0.3335832   1.0992490   .  0.308318428 -0.203359720
    ##     GENE5  -1.1581171  -1.1048136  -0.4593716   .  0.004504068 -0.999712484
    ##       ...           .           .           .   .            .            .
    ## GENE49996  1.27143151  0.24749585  1.03957397   .   1.88200964  -0.14969630
    ## GENE49997 -1.89637290  0.43362652  0.15556430   .   0.29585744  -0.50909512
    ## GENE49998 -0.09771661 -0.88977805 -0.24784213   .  -0.51253523  -0.82608283
    ## GENE49999 -1.14763135 -1.22362336  1.66505592   .   0.79435530  -0.04415469
    ## GENE50000 -2.05281155 -0.35942735 -0.27799501   .  -1.77924627   0.99225695

To see the file structure we use `h5ls`:

    h5ls(testh5)

    ##                    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

## Importing HDF5 files

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"

    newDelayedMatrix

    ## <50000 x 20> HDF5Matrix object of type "double":
    ##             SampleID1   SampleID2   SampleID3 ...   SampleID19   SampleID20
    ##     GENE1   0.4468647   0.1185882  -0.6007010   .  0.679896517  0.454905055
    ##     GENE2  -2.0121225  -0.3375326  -0.2462585   . -0.721057950  1.202833911
    ##     GENE3   0.1948504  -0.6316396  -1.0474468   .  1.078857416  0.867241673
    ##     GENE4  -1.4723226  -0.3335832   1.0992490   .  0.308318428 -0.203359720
    ##     GENE5  -1.1581171  -1.1048136  -0.4593716   .  0.004504068 -0.999712484
    ##       ...           .           .           .   .            .            .
    ## GENE49996  1.27143151  0.24749585  1.03957397   .   1.88200964  -0.14969630
    ## GENE49997 -1.89637290  0.43362652  0.15556430   .   0.29585744  -0.50909512
    ## GENE49998 -0.09771661 -0.88977805 -0.24784213   .  -0.51253523  -0.82608283
    ## GENE49999 -1.14763135 -1.22362336  1.66505592   .   0.79435530  -0.04415469
    ## GENE50000 -2.05281155 -0.35942735 -0.27799501   .  -1.77924627   0.99225695

## Using a `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.

    HDF5MAE <- MultiAssayExperiment(experiments = list(smallMatrix = smallMatrix))
    sampleMap(HDF5MAE)

    ## 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

    colData(HDF5MAE)

    ## DataFrame with 20 rows and 0 columns

### `SummarizedExperiment` with `DelayedMatrix` backend

A 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:

    HDF5SE <- SummarizedExperiment(assays = smallMatrix)
    assay(HDF5SE)

    ## <50000 x 20> DelayedMatrix object of type "double":
    ##             SampleID1   SampleID2   SampleID3 ...   SampleID19   SampleID20
    ##     GENE1   0.4468647   0.1185882  -0.6007010   .  0.679896517  0.454905055
    ##     GENE2  -2.0121225  -0.3375326  -0.2462585   . -0.721057950  1.202833911
    ##     GENE3   0.1948504  -0.6316396  -1.0474468   .  1.078857416  0.867241673
    ##     GENE4  -1.4723226  -0.3335832   1.0992490   .  0.308318428 -0.203359720
    ##     GENE5  -1.1581171  -1.1048136  -0.4593716   .  0.004504068 -0.999712484
    ##       ...           .           .           .   .            .            .
    ## GENE49996  1.27143151  0.24749585  1.03957397   .   1.88200964  -0.14969630
    ## GENE49997 -1.89637290  0.43362652  0.15556430   .   0.29585744  -0.50909512
    ## GENE49998 -0.09771661 -0.88977805 -0.24784213   .  -0.51253523  -0.82608283
    ## GENE49999 -1.14763135 -1.22362336  1.66505592   .   0.79435530  -0.04415469
    ## GENE50000 -2.05281155 -0.35942735 -0.27799501   .  -1.77924627   0.99225695

    MultiAssayExperiment(list(HDF5SE = HDF5SE))

    ## 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`.

# Session info

    sessionInfo()

    ## R version 4.6.0 (2026-04-24)
    ## Platform: x86_64-pc-linux-gnu
    ## Running under: Ubuntu 24.04.4 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=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] HDF5Array_1.41.0            h5mread_1.5.0
    ##  [3] rhdf5_2.57.1                DelayedArray_0.39.3
    ##  [5] SparseArray_1.13.2          S4Arrays_1.13.0
    ##  [7] abind_1.4-8                 Matrix_1.7-5
    ##  [9] survminer_0.5.2             ggpubr_0.6.3
    ## [11] ggplot2_4.0.3               survival_3.8-6
    ## [13] UpSetR_1.4.1                RaggedExperiment_1.37.0
    ## [15] MultiAssayExperiment_1.39.0 SummarizedExperiment_1.43.0
    ## [17] Biobase_2.73.1              GenomicRanges_1.65.0
    ## [19] Seqinfo_1.3.0               IRanges_2.47.2
    ## [21] S4Vectors_0.51.3            BiocGenerics_0.59.6
    ## [23] generics_0.1.4              MatrixGenerics_1.25.0
    ## [25] matrixStats_1.5.0           BiocStyle_2.41.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] tidyselect_1.2.1     dplyr_1.2.1          farver_2.1.2
    ##  [4] S7_0.2.2             fastmap_1.2.0        digest_0.6.39
    ##  [7] lifecycle_1.0.5      magrittr_2.0.5       compiler_4.6.0
    ## [10] rlang_1.2.0          sass_0.4.10          tools_4.6.0
    ## [13] yaml_2.3.12          knitr_1.51           ggsignif_0.6.4
    ## [16] labeling_0.4.3       plyr_1.8.9           xml2_1.5.2
    ## [19] RColorBrewer_1.1-3   withr_3.0.2          purrr_1.2.2
    ## [22] sys_3.4.3            grid_4.6.0           Rhdf5lib_2.1.0
    ## [25] scales_1.4.0         cli_3.6.6            rmarkdown_2.31
    ## [28] reshape2_1.4.5       commonmark_2.0.0     BiocBaseUtils_1.15.1
    ## [31] cachem_1.1.0         stringr_1.6.0        splines_4.6.0
    ## [34] BiocManager_1.30.27  XVector_0.53.0       vctrs_0.7.3
    ## [37] jsonlite_2.0.0       carData_3.0-6        car_3.1-5
    ## [40] litedown_0.9         rstatix_0.7.3        Formula_1.2-5
    ## [43] maketools_1.3.2      tidyr_1.3.2          jquerylib_0.1.4
    ## [46] glue_1.8.1           ggtext_0.1.2         stringi_1.8.7
    ## [49] gtable_0.3.6         tibble_3.3.1         pillar_1.11.1
    ## [52] htmltools_0.5.9      rhdf5filters_1.25.0  R6_2.6.1
    ## [55] evaluate_1.0.5       lattice_0.22-9       markdown_2.0
    ## [58] backports_1.5.1      gridtext_0.1.6       broom_1.0.13
    ## [61] bslib_0.11.0         Rcpp_1.1.1-1.1       gridExtra_2.3
    ## [64] xfun_0.57            buildtools_1.0.0     pkgconfig_2.0.3