Contents

1 Integrating an HDF5 backend for MultiAssayExperiment

Dependencies

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

1.1 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"
head(smallMatrix)
## <6 x 20> matrix of class DelayedMatrix and type "double":
##         SampleID1   SampleID2   SampleID3 ...  SampleID19  SampleID20
## GENE1  0.74501485  0.52985189  1.16398343   . -0.30351028  0.25072887
## GENE2 -0.16722240 -0.08609273  0.42934976   . -1.53737341 -1.42637728
## GENE3 -0.70621633 -1.59048680 -0.52020241   .  0.41439342  0.12198794
## GENE4 -1.91259488  1.19924832 -0.78735659   .  0.71246478 -0.11163931
## GENE5 -2.23602413  1.71502942  0.21919775   . -0.13738340  0.03136577
## GENE6  0.81701991 -1.66958285 -1.30594716   . -0.79740789 -0.16575123
dim(smallMatrix)
## [1] 50000    20

1.2 Importing HDF5 files

Note that a large matrix from an HDF5 file can also be loaded using the HDF5Array function.

For example:

dataLocation <- system.file("extdata", "exMatrix.h5", package =
                              "MultiAssayExperiment", mustWork = TRUE)
h5ls(dataLocation)
##   group     name       otype dclass       dim
## 0     / exMatrix H5I_DATASET  FLOAT 5000 x 20
hdf5Data <- HDF5ArraySeed(file = dataLocation, name = "exMatrix")
newDelayedMatrix <- DelayedArray(hdf5Data)
class(newDelayedMatrix)
## [1] "HDF5Matrix"
## attr(,"package")
## [1] "HDF5Array"
head(newDelayedMatrix)
## <6 x 20> matrix of class DelayedMatrix and type "double":
##            [,1]       [,2]       [,3] ...      [,19]      [,20]
## [1,]  0.3261516  0.4149151  0.8154378   . -0.1876063  0.4156044
## [2,]  0.7243018 -0.9416687 -1.1290878   . -1.2820178 -0.3591841
## [3,]  1.5073255  0.7597899 -0.2756298   . -1.5666680 -0.1523462
## [4,]  0.1668286  1.2684049  0.9082990   .  0.3486139  1.8019041
## [5,]  0.5640491 -2.0222537  0.2881079   .  0.1210501 -1.4873598
## [6,] -0.3504778 -0.4149494  0.9145470   .  0.4291890 -0.4986399

1.2.1 Dimnames from HDF5 file

Currently, the rhdf5 package does not store dimnames in the h5 file by default. A request for this feature has been sent to the maintainer of the rhdf5 package and any further development to the HDF5Array package is contingent on such lower level dimension name storage.

1.3 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.3.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.74501485  0.52985189  1.16398343   . -0.30351028  0.25072887
##     GENE2 -0.16722240 -0.08609273  0.42934976   . -1.53737341 -1.42637728
##     GENE3 -0.70621633 -1.59048680 -0.52020241   .  0.41439342  0.12198794
##     GENE4 -1.91259488  1.19924832 -0.78735659   .  0.71246478 -0.11163931
##     GENE5 -2.23602413  1.71502942  0.21919775   . -0.13738340  0.03136577
##       ...           .           .           .   .           .           .
## GENE49996  -2.2998550  -0.1698877   1.1098784   .  1.03157310  0.35914526
## GENE49997  -0.7987016   0.1047761  -1.4323008   . -0.45004821 -0.85953775
## GENE49998   0.2849064   1.1629426  -1.2871878   .  0.02477404 -0.78906500
## GENE49999  -0.5889637   0.2972950  -0.1531479   .  1.33275080 -0.17672597
## GENE50000  -0.4498280   0.5072592   0.3449607   .  0.51654019  1.54391186
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 
## Features: 
##  experiments() - obtain the ExperimentList instance 
##  colData() - the primary/phenotype DFrame 
##  sampleMap() - the sample availability DFrame 
##  `$`, `[`, `[[` - extract colData columns, subset, or experiment 
##  *Format() - convert into a long or wide DFrame 
##  assays() - convert ExperimentList to a SimpleList of matrices

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.