Dependencies
library(MultiAssayExperiment)
library(HDF5Array)
library(SummarizedExperiment)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 -1.26140808  0.51796963 -3.22942353   .  0.18867938 -0.27656609
## GENE2  0.60973814 -1.14132770 -0.82771432   .  0.46931717  0.19725989
## GENE3  1.82710653  0.90432719  0.63621424   .  0.36233127  1.16858836
## GENE4 -0.14053842 -1.60262078 -1.06252780   .  0.61923870  0.70009043
## GENE5  0.57244026 -1.68501427 -0.13466779   .  0.48603563 -0.24370970
## GENE6  1.19403083  0.05344144  1.22200337   . -0.02488824 -0.27241550dim(smallMatrix)## [1] 50000    20Note 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 20hdf5Data <- 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.4986399Currently, 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.
DelayedMatrix with MultiAssayExperimentA 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  SampleID20colData(HDF5MAE)## DataFrame with 20 rows and 0 columnsSummarizedExperiment with DelayedMatrix backendA 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 -1.2614081  0.5179696 -3.2294235   .   0.1886794  -0.2765661
##     GENE2  0.6097381 -1.1413277 -0.8277143   .   0.4693172   0.1972599
##     GENE3  1.8271065  0.9043272  0.6362142   .   0.3623313   1.1685884
##     GENE4 -0.1405384 -1.6026208 -1.0625278   .   0.6192387   0.7000904
##     GENE5  0.5724403 -1.6850143 -0.1346678   .   0.4860356  -0.2437097
##       ...          .          .          .   .           .           .
## GENE49996  0.6206606  0.1024747  1.5922409   .  1.47735532 -1.24286313
## GENE49997 -1.2544982 -0.1966683 -1.2666870   . -0.02663156  0.95112583
## GENE49998  0.7330015 -0.1144405 -0.4230417   . -1.35270260  0.92871578
## GENE49999  1.4790735  0.6468762 -0.2599957   . -0.48291000 -1.73458808
## GENE50000 -0.1881613 -1.5129953 -0.7367874   .  0.81166740  0.77426377MultiAssayExperiment(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 matricesAdditional 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.