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> matrix of class DelayedMatrix and type "double":
##             SampleID1   SampleID2   SampleID3 ...  SampleID19  SampleID20
##     GENE1  -0.4312294   1.4409797  -2.0504121   .   0.7351786  -1.5447315
##     GENE2   0.4393512   0.9604603  -0.3668742   .   0.7862599   1.2339122
##     GENE3   0.1843361  -1.0383368  -0.4712812   .  -0.7851665  -3.1241170
##     GENE4  -0.4610344  -0.5515895  -0.5869010   .   0.6907843   0.4806580
##     GENE5  -0.2497588   0.1431237  -1.4726718   .   1.0122940   0.5614055
##       ...           .           .           .   .           .           .
## GENE49996  0.65586323  1.38831001 -0.17642264   .  0.20264386  1.68516901
## GENE49997 -0.25786834  0.35891334 -0.79591330   . -0.86248700 -0.78932440
## GENE49998 -1.46505565 -0.90918203 -0.71795594   . -1.26711203 -1.03178018
## GENE49999 -0.22687653 -0.31894112 -1.83867213   . -0.45287311  0.09292224
## GENE50000 -0.26584102  1.52669880  0.07709258   .  0.24782881 -2.19351922
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> matrix of class HDF5Matrix and type "double":
##             SampleID1   SampleID2   SampleID3 ...  SampleID19  SampleID20
##     GENE1  -0.4312294   1.4409797  -2.0504121   .   0.7351786  -1.5447315
##     GENE2   0.4393512   0.9604603  -0.3668742   .   0.7862599   1.2339122
##     GENE3   0.1843361  -1.0383368  -0.4712812   .  -0.7851665  -3.1241170
##     GENE4  -0.4610344  -0.5515895  -0.5869010   .   0.6907843   0.4806580
##     GENE5  -0.2497588   0.1431237  -1.4726718   .   1.0122940   0.5614055
##       ...           .           .           .   .           .           .
## GENE49996  0.65586323  1.38831001 -0.17642264   .  0.20264386  1.68516901
## GENE49997 -0.25786834  0.35891334 -0.79591330   . -0.86248700 -0.78932440
## GENE49998 -1.46505565 -0.90918203 -0.71795594   . -1.26711203 -1.03178018
## GENE49999 -0.22687653 -0.31894112 -1.83867213   . -0.45287311  0.09292224
## GENE50000 -0.26584102  1.52669880  0.07709258   .  0.24782881 -2.19351922

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> matrix of class HDF5Matrix and type "double":
##             SampleID1   SampleID2   SampleID3 ...  SampleID19  SampleID20
##     GENE1  -0.4312294   1.4409797  -2.0504121   .   0.7351786  -1.5447315
##     GENE2   0.4393512   0.9604603  -0.3668742   .   0.7862599   1.2339122
##     GENE3   0.1843361  -1.0383368  -0.4712812   .  -0.7851665  -3.1241170
##     GENE4  -0.4610344  -0.5515895  -0.5869010   .   0.6907843   0.4806580
##     GENE5  -0.2497588   0.1431237  -1.4726718   .   1.0122940   0.5614055
##       ...           .           .           .   .           .           .
## GENE49996  0.65586323  1.38831001 -0.17642264   .  0.20264386  1.68516901
## GENE49997 -0.25786834  0.35891334 -0.79591330   . -0.86248700 -0.78932440
## GENE49998 -1.46505565 -0.90918203 -0.71795594   . -1.26711203 -1.03178018
## GENE49999 -0.22687653 -0.31894112 -1.83867213   . -0.45287311  0.09292224
## GENE50000 -0.26584102  1.52669880  0.07709258   .  0.24782881 -2.19351922

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> matrix of class DelayedMatrix and type "double":
##             SampleID1   SampleID2   SampleID3 ...  SampleID19  SampleID20
##     GENE1  -0.4312294   1.4409797  -2.0504121   .   0.7351786  -1.5447315
##     GENE2   0.4393512   0.9604603  -0.3668742   .   0.7862599   1.2339122
##     GENE3   0.1843361  -1.0383368  -0.4712812   .  -0.7851665  -3.1241170
##     GENE4  -0.4610344  -0.5515895  -0.5869010   .   0.6907843   0.4806580
##     GENE5  -0.2497588   0.1431237  -1.4726718   .   1.0122940   0.5614055
##       ...           .           .           .   .           .           .
## GENE49996  0.65586323  1.38831001 -0.17642264   .  0.20264386  1.68516901
## GENE49997 -0.25786834  0.35891334 -0.79591330   . -0.86248700 -0.78932440
## GENE49998 -1.46505565 -0.90918203 -0.71795594   . -1.26711203 -1.03178018
## GENE49999 -0.22687653 -0.31894112 -1.83867213   . -0.45287311  0.09292224
## GENE50000 -0.26584102  1.52669880  0.07709258   .  0.24782881 -2.19351922
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.2.1 (2022-06-23)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.16-bioc/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              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       
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] HDF5Array_1.26.0            rhdf5_2.42.0               
##  [3] DelayedArray_0.24.0         Matrix_1.5-1               
##  [5] survminer_0.4.9             ggpubr_0.4.0               
##  [7] ggplot2_3.3.6               survival_3.4-0             
##  [9] UpSetR_1.4.0                RaggedExperiment_1.22.0    
## [11] MultiAssayExperiment_1.24.0 SummarizedExperiment_1.28.0
## [13] Biobase_2.58.0              GenomicRanges_1.50.0       
## [15] GenomeInfoDb_1.34.0         IRanges_2.32.0             
## [17] S4Vectors_0.36.0            BiocGenerics_0.44.0        
## [19] MatrixGenerics_1.10.0       matrixStats_0.62.0         
## [21] BiocStyle_2.26.0           
## 
## loaded via a namespace (and not attached):
##  [1] ggtext_0.1.2           bitops_1.0-7           R.cache_0.16.0        
##  [4] tools_4.2.1            backports_1.4.1        bslib_0.4.0           
##  [7] utf8_1.2.2             R6_2.5.1               DBI_1.1.3             
## [10] colorspace_2.0-3       rhdf5filters_1.10.0    withr_2.5.0           
## [13] tidyselect_1.2.0       gridExtra_2.3          compiler_4.2.1        
## [16] cli_3.4.1              xml2_1.3.3             labeling_0.4.2        
## [19] bookdown_0.29          sass_0.4.2             scales_1.2.1          
## [22] survMisc_0.5.6         commonmark_1.8.1       stringr_1.4.1         
## [25] digest_0.6.30          rmarkdown_2.17         R.utils_2.12.1        
## [28] XVector_0.38.0         pkgconfig_2.0.3        htmltools_0.5.3       
## [31] fastmap_1.1.0          highr_0.9              rlang_1.0.6           
## [34] jquerylib_0.1.4        farver_2.1.1           generics_0.1.3        
## [37] zoo_1.8-11             jsonlite_1.8.3         dplyr_1.0.10          
## [40] car_3.1-1              R.oo_1.25.0            RCurl_1.98-1.9        
## [43] magrittr_2.0.3         GenomeInfoDbData_1.2.9 Rhdf5lib_1.20.0       
## [46] Rcpp_1.0.9             munsell_0.5.0          fansi_1.0.3           
## [49] abind_1.4-5            lifecycle_1.0.3        R.methodsS3_1.8.2     
## [52] stringi_1.7.8          yaml_2.3.6             carData_3.0-5         
## [55] zlibbioc_1.44.0        plyr_1.8.7             grid_4.2.1            
## [58] lattice_0.20-45        splines_4.2.1          gridtext_0.1.5        
## [61] magick_2.7.3           knitr_1.40             pillar_1.8.1          
## [64] markdown_1.3           ggsignif_0.6.4         glue_1.6.2            
## [67] evaluate_0.17          data.table_1.14.4      BiocManager_1.30.19   
## [70] vctrs_0.5.0            gtable_0.3.1           purrr_0.3.5           
## [73] tidyr_1.2.1            km.ci_0.5-6            assertthat_0.2.1      
## [76] cachem_1.0.6           xfun_0.34              BiocBaseUtils_1.0.0   
## [79] xtable_1.8-4           broom_1.0.1            rstatix_0.7.0         
## [82] tibble_3.1.8           KMsurv_0.1-5           ellipsis_0.3.2        
## [85] R.rsp_0.45.0