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.80399468  0.50815172 -0.02316151   . -0.54098492 -0.96893298
##     GENE2 -0.80319129  0.46013358  0.20537253   . -0.41437861  0.07674045
##     GENE3 -1.27326115 -1.57579022  1.53255030   .  0.68620378 -0.83028710
##     GENE4 -0.96505405 -1.04454412 -0.73367545   . -0.65204835 -1.06914701
##     GENE5  0.34942768 -0.60651758 -0.11535891   . -0.40744714 -0.09084918
##       ...           .           .           .   .           .           .
## GENE49996 -1.13524961  1.32101748 -0.15047922   .   0.6288352   0.5950607
## GENE49997  1.68875924 -1.45494531 -1.59715836   .  -0.9912991  -1.1723425
## GENE49998 -0.08055186  0.14621383 -0.42844272   .  -0.9288056   0.2760688
## GENE49999  0.52000018 -1.46762371 -0.44022810   .   0.2554728  -0.8721299
## GENE50000  1.22276041 -1.15382471 -2.85727570   .  -0.6534565  -2.3452513
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.80399468  0.50815172 -0.02316151   . -0.54098492 -0.96893298
##     GENE2 -0.80319129  0.46013358  0.20537253   . -0.41437861  0.07674045
##     GENE3 -1.27326115 -1.57579022  1.53255030   .  0.68620378 -0.83028710
##     GENE4 -0.96505405 -1.04454412 -0.73367545   . -0.65204835 -1.06914701
##     GENE5  0.34942768 -0.60651758 -0.11535891   . -0.40744714 -0.09084918
##       ...           .           .           .   .           .           .
## GENE49996 -1.13524961  1.32101748 -0.15047922   .   0.6288352   0.5950607
## GENE49997  1.68875924 -1.45494531 -1.59715836   .  -0.9912991  -1.1723425
## GENE49998 -0.08055186  0.14621383 -0.42844272   .  -0.9288056   0.2760688
## GENE49999  0.52000018 -1.46762371 -0.44022810   .   0.2554728  -0.8721299
## GENE50000  1.22276041 -1.15382471 -2.85727570   .  -0.6534565  -2.3452513

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.80399468  0.50815172 -0.02316151   . -0.54098492 -0.96893298
##     GENE2 -0.80319129  0.46013358  0.20537253   . -0.41437861  0.07674045
##     GENE3 -1.27326115 -1.57579022  1.53255030   .  0.68620378 -0.83028710
##     GENE4 -0.96505405 -1.04454412 -0.73367545   . -0.65204835 -1.06914701
##     GENE5  0.34942768 -0.60651758 -0.11535891   . -0.40744714 -0.09084918
##       ...           .           .           .   .           .           .
## GENE49996 -1.13524961  1.32101748 -0.15047922   .   0.6288352   0.5950607
## GENE49997  1.68875924 -1.45494531 -1.59715836   .  -0.9912991  -1.1723425
## GENE49998 -0.08055186  0.14621383 -0.42844272   .  -0.9288056   0.2760688
## GENE49999  0.52000018 -1.46762371 -0.44022810   .   0.2554728  -0.8721299
## GENE50000  1.22276041 -1.15382471 -2.85727570   .  -0.6534565  -2.3452513

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.80399468  0.50815172 -0.02316151   . -0.54098492 -0.96893298
##     GENE2 -0.80319129  0.46013358  0.20537253   . -0.41437861  0.07674045
##     GENE3 -1.27326115 -1.57579022  1.53255030   .  0.68620378 -0.83028710
##     GENE4 -0.96505405 -1.04454412 -0.73367545   . -0.65204835 -1.06914701
##     GENE5  0.34942768 -0.60651758 -0.11535891   . -0.40744714 -0.09084918
##       ...           .           .           .   .           .           .
## GENE49996 -1.13524961  1.32101748 -0.15047922   .   0.6288352   0.5950607
## GENE49997  1.68875924 -1.45494531 -1.59715836   .  -0.9912991  -1.1723425
## GENE49998 -0.08055186  0.14621383 -0.42844272   .  -0.9288056   0.2760688
## GENE49999  0.52000018 -1.46762371 -0.44022810   .   0.2554728  -0.8721299
## GENE50000  1.22276041 -1.15382471 -2.85727570   .  -0.6534565  -2.3452513
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 all data to 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.