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> DelayedMatrix object of type "double":
## SampleID1 SampleID2 SampleID3 ... SampleID19 SampleID20
## GENE1 -0.359164557 0.006381214 0.804683509 . 0.09268673 -0.18194204
## GENE2 -2.301042537 -0.179489084 1.422183406 . 2.44672692 -0.68703646
## GENE3 -1.190395193 -0.918707976 0.300218562 . 0.76711260 -1.53001855
## GENE4 -0.672100528 -0.743413816 -1.130025666 . -0.64196577 1.36263318
## GENE5 0.416523031 0.129494724 -1.500312032 . -0.84616310 -1.77571720
## GENE6 -0.312229251 0.550848399 0.430698951 . -0.01452894 -0.27588908
dim(smallMatrix)
## [1] 50000 20
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> DelayedMatrix object of 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
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.
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
SummarizedExperiment
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> DelayedMatrix object of type "double":
## SampleID1 SampleID2 SampleID3 ... SampleID19
## GENE1 -0.359164557 0.006381214 0.804683509 . 0.09268673
## GENE2 -2.301042537 -0.179489084 1.422183406 . 2.44672692
## GENE3 -1.190395193 -0.918707976 0.300218562 . 0.76711260
## GENE4 -0.672100528 -0.743413816 -1.130025666 . -0.64196577
## GENE5 0.416523031 0.129494724 -1.500312032 . -0.84616310
## ... . . . . .
## GENE49996 3.1111549 0.3235445 0.4540421 . -0.738766107
## GENE49997 1.2320915 0.9093916 1.0571306 . -0.001940732
## GENE49998 -0.3506334 -0.3025980 -0.8978435 . 0.052818895
## GENE49999 1.1930148 -0.9868347 0.8147342 . -0.692181607
## GENE50000 -0.3594167 1.1189452 -0.4782563 . 0.420406907
## SampleID20
## GENE1 -0.18194204
## GENE2 -0.68703646
## GENE3 -1.53001855
## GENE4 1.36263318
## GENE5 -1.77571720
## ... .
## GENE49996 0.284079527
## GENE49997 -1.228424834
## GENE49998 -1.120665979
## GENE49999 -0.715243026
## GENE50000 -0.787983112
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 DataFrame
## sampleMap() - the sample availability DataFrame
## `$`, `[`, `[[` - extract colData columns, subset, or experiment
## *Format() - convert into a long or wide DataFrame
## 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
.