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)
## DelayedMatrix object of 6 x 20 doubles:
## SampleID1 SampleID2 SampleID3 ... SampleID19 SampleID20
## GENE1 -0.1140682 -0.2531084 -0.5234615 . -0.09995287 0.48694386
## GENE2 1.2310824 0.4148007 0.5861362 . -1.35960203 -0.74054106
## GENE3 0.3582445 0.2806000 0.5130520 . 2.26094547 0.03256646
## GENE4 -0.1464091 0.6128401 -0.8593648 . 2.16320015 -0.32609652
## GENE5 0.7147241 -1.5627576 -0.4008509 . -0.76595696 -0.73083197
## GENE6 1.1938911 -1.4039067 -0.9002094 . 0.04508759 -1.47344635
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)
## DelayedMatrix object of 6 x 20 doubles:
## [,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)
## DelayedMatrix object of 50000 x 20 doubles:
## SampleID1 SampleID2 SampleID3 ... SampleID19 SampleID20
## GENE1 -0.1140682 -0.2531084 -0.5234615 . -0.09995287 0.48694386
## GENE2 1.2310824 0.4148007 0.5861362 . -1.35960203 -0.74054106
## GENE3 0.3582445 0.2806000 0.5130520 . 2.26094547 0.03256646
## GENE4 -0.1464091 0.6128401 -0.8593648 . 2.16320015 -0.32609652
## GENE5 0.7147241 -1.5627576 -0.4008509 . -0.76595696 -0.73083197
## ... . . . . . .
## GENE49996 1.8856946 0.8270514 -0.3319978 . -0.1469894 0.8287415
## GENE49997 -1.0936419 -1.5337894 1.7632318 . -1.1832049 -0.5066101
## GENE49998 -0.7650005 -0.7288294 1.7537747 . -0.5263391 0.7192430
## GENE49999 -0.2275631 0.3792932 -0.2582466 . -0.1824195 2.0462370
## GENE50000 0.5327876 -1.0175391 0.8952815 . 1.7042072 -1.3396292
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
.