Author: MultiAssay Special Interest Group
Date: 2026-04-28

# Using DelayedMatrix with MultiAssayExperiment

MultiAssay Special Interest Group

#### April 28, 2026

# Contents

# 1 Integrating an HDF5 backend for MultiAssayExperiment

## 1.1 Dependencies

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

## 1.2 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  1.0161829 -0.6491459 -1.4398309   .   0.9202204  -0.8468152
    ##     GENE2  0.6624366 -1.9337643  0.4961703   .  -0.5734565  -0.1374376
    ##     GENE3  0.4742758 -0.3033952  0.2736009   .   2.6436598   0.1640290
    ##     GENE4  1.1262911  0.0118819 -1.3302299   .   2.4551652  -0.4391092
    ##     GENE5 -0.9560959  0.2917624  0.9418241   .  -1.5330722   0.4526351
    ##       ...          .          .          .   .           .           .
    ## GENE49996  0.8717977 -2.0052280 -1.2329269   . -0.12193582  0.84886167
    ## GENE49997 -0.5636634  0.6989066 -1.0442377   .  0.02671234 -0.46587789
    ## GENE49998 -1.0157749 -1.7042893 -0.2544029   . -0.06199914 -1.64606897
    ## GENE49999 -0.4409091 -0.1520911  0.4814195   .  0.77992563 -0.48240451
    ## GENE50000  1.9929758  2.1100958 -0.7994102   . -1.03724494 -0.89060120

    dim(smallMatrix)

    ## [1] 50000    20

## 1.3 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  1.0161829 -0.6491459 -1.4398309   .   0.9202204  -0.8468152
    ##     GENE2  0.6624366 -1.9337643  0.4961703   .  -0.5734565  -0.1374376
    ##     GENE3  0.4742758 -0.3033952  0.2736009   .   2.6436598   0.1640290
    ##     GENE4  1.1262911  0.0118819 -1.3302299   .   2.4551652  -0.4391092
    ##     GENE5 -0.9560959  0.2917624  0.9418241   .  -1.5330722   0.4526351
    ##       ...          .          .          .   .           .           .
    ## GENE49996  0.8717977 -2.0052280 -1.2329269   . -0.12193582  0.84886167
    ## GENE49997 -0.5636634  0.6989066 -1.0442377   .  0.02671234 -0.46587789
    ## GENE49998 -1.0157749 -1.7042893 -0.2544029   . -0.06199914 -1.64606897
    ## GENE49999 -0.4409091 -0.1520911  0.4814195   .  0.77992563 -0.48240451
    ## GENE50000  1.9929758  2.1100958 -0.7994102   . -1.03724494 -0.89060120

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

## 1.4 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  1.0161829 -0.6491459 -1.4398309   .   0.9202204  -0.8468152
    ##     GENE2  0.6624366 -1.9337643  0.4961703   .  -0.5734565  -0.1374376
    ##     GENE3  0.4742758 -0.3033952  0.2736009   .   2.6436598   0.1640290
    ##     GENE4  1.1262911  0.0118819 -1.3302299   .   2.4551652  -0.4391092
    ##     GENE5 -0.9560959  0.2917624  0.9418241   .  -1.5330722   0.4526351
    ##       ...          .          .          .   .           .           .
    ## GENE49996  0.8717977 -2.0052280 -1.2329269   . -0.12193582  0.84886167
    ## GENE49997 -0.5636634  0.6989066 -1.0442377   .  0.02671234 -0.46587789
    ## GENE49998 -1.0157749 -1.7042893 -0.2544029   . -0.06199914 -1.64606897
    ## GENE49999 -0.4409091 -0.1520911  0.4814195   .  0.77992563 -0.48240451
    ## GENE50000  1.9929758  2.1100958 -0.7994102   . -1.03724494 -0.89060120

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

### 1.5.1 `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  1.0161829 -0.6491459 -1.4398309   .   0.9202204  -0.8468152
    ##     GENE2  0.6624366 -1.9337643  0.4961703   .  -0.5734565  -0.1374376
    ##     GENE3  0.4742758 -0.3033952  0.2736009   .   2.6436598   0.1640290
    ##     GENE4  1.1262911  0.0118819 -1.3302299   .   2.4551652  -0.4391092
    ##     GENE5 -0.9560959  0.2917624  0.9418241   .  -1.5330722   0.4526351
    ##       ...          .          .          .   .           .           .
    ## GENE49996  0.8717977 -2.0052280 -1.2329269   . -0.12193582  0.84886167
    ## GENE49997 -0.5636634  0.6989066 -1.0442377   .  0.02671234 -0.46587789
    ## GENE49998 -1.0157749 -1.7042893 -0.2544029   . -0.06199914 -1.64606897
    ## GENE49999 -0.4409091 -0.1520911  0.4814195   .  0.77992563 -0.48240451
    ## GENE50000  1.9929758  2.1100958 -0.7994102   . -1.03724494 -0.89060120

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

# 2 Session info

    sessionInfo()

    ## R version 4.6.0 Patched (2026-04-24 r89963)
    ## Platform: aarch64-apple-darwin23
    ## Running under: macOS Tahoe 26.3.1
    ##
    ## Matrix products: default
    ## BLAS:   /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRblas.0.dylib
    ## LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1
    ##
    ## locale:
    ## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    ##
    ## time zone: America/New_York
    ## tzcode source: internal
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] HDF5Array_1.40.0            h5mread_1.4.0
    ##  [3] rhdf5_2.56.0                DelayedArray_0.38.0
    ##  [5] SparseArray_1.12.0          S4Arrays_1.12.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.0                RaggedExperiment_1.36.0
    ## [15] MultiAssayExperiment_1.38.0 SummarizedExperiment_1.42.0
    ## [17] Biobase_2.72.0              GenomicRanges_1.64.0
    ## [19] Seqinfo_1.2.0               IRanges_2.46.0
    ## [21] S4Vectors_0.50.0            BiocGenerics_0.58.0
    ## [23] generics_0.1.4              MatrixGenerics_1.24.0
    ## [25] matrixStats_1.5.0           BiocStyle_2.40.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] grid_4.6.0           Rhdf5lib_2.0.0       scales_1.4.0
    ## [25] dichromat_2.0-0.1    tinytex_0.59         cli_3.6.6
    ## [28] rmarkdown_2.31       otel_0.2.0           reshape2_1.4.5
    ## [31] commonmark_2.0.0     BiocBaseUtils_1.14.0 cachem_1.1.0
    ## [34] stringr_1.6.0        splines_4.6.0        BiocManager_1.30.27
    ## [37] XVector_0.52.0       vctrs_0.7.3          jsonlite_2.0.0
    ## [40] carData_3.0-6        litedown_0.9         bookdown_0.46
    ## [43] car_3.1-5            rstatix_0.7.3        Formula_1.2-5
    ## [46] magick_2.9.1         tidyr_1.3.2          jquerylib_0.1.4
    ## [49] glue_1.8.1           ggtext_0.1.2         stringi_1.8.7
    ## [52] gtable_0.3.6         tibble_3.3.1         pillar_1.11.1
    ## [55] rhdf5filters_1.24.0  htmltools_0.5.9      R6_2.6.1
    ## [58] evaluate_1.0.5       lattice_0.22-9       markdown_2.0
    ## [61] backports_1.5.1      gridtext_0.1.6       broom_1.0.12
    ## [64] bslib_0.10.0         Rcpp_1.1.1-1.1       gridExtra_2.3
    ## [67] xfun_0.57            pkgconfig_2.0.3