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"
# show method
smallMatrix
## <50000 x 20> matrix of class DelayedMatrix and type "double":
## SampleID1 SampleID2 SampleID3 ... SampleID19 SampleID20
## GENE1 -0.98185094 -1.09247785 -2.44049822 . -0.0736439 0.2035806
## GENE2 -1.26963461 0.37376259 -0.55900720 . 1.0342642 -0.3150900
## GENE3 0.52504578 0.12308230 1.15378774 . -2.3556618 1.2136911
## GENE4 0.63845904 1.10536408 -0.07392009 . 0.4737823 0.3357675
## GENE5 -1.91778230 -1.18807778 -1.78248933 . -0.3238059 -1.0944165
## ... . . . . . .
## GENE49996 0.19318540 0.97453064 0.63262294 . -1.54549761 -0.94665246
## GENE49997 2.06692566 0.34898517 -1.56896585 . 1.27972835 1.05390276
## GENE49998 1.49203102 0.71770173 0.40263054 . 0.28028922 1.98605602
## GENE49999 -0.82881929 -0.78914410 -1.17557005 . 0.01503384 -1.21377941
## GENE50000 -0.37533534 -0.47031175 0.07635521 . -2.75357943 0.70741429
dim(smallMatrix)
## [1] 50000 20
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.98185094 -1.09247785 -2.44049822 . -0.0736439 0.2035806
## GENE2 -1.26963461 0.37376259 -0.55900720 . 1.0342642 -0.3150900
## GENE3 0.52504578 0.12308230 1.15378774 . -2.3556618 1.2136911
## GENE4 0.63845904 1.10536408 -0.07392009 . 0.4737823 0.3357675
## GENE5 -1.91778230 -1.18807778 -1.78248933 . -0.3238059 -1.0944165
## ... . . . . . .
## GENE49996 0.19318540 0.97453064 0.63262294 . -1.54549761 -0.94665246
## GENE49997 2.06692566 0.34898517 -1.56896585 . 1.27972835 1.05390276
## GENE49998 1.49203102 0.71770173 0.40263054 . 0.28028922 1.98605602
## GENE49999 -0.82881929 -0.78914410 -1.17557005 . 0.01503384 -1.21377941
## GENE50000 -0.37533534 -0.47031175 0.07635521 . -2.75357943 0.70741429
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
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.98185094 -1.09247785 -2.44049822 . -0.0736439 0.2035806
## GENE2 -1.26963461 0.37376259 -0.55900720 . 1.0342642 -0.3150900
## GENE3 0.52504578 0.12308230 1.15378774 . -2.3556618 1.2136911
## GENE4 0.63845904 1.10536408 -0.07392009 . 0.4737823 0.3357675
## GENE5 -1.91778230 -1.18807778 -1.78248933 . -0.3238059 -1.0944165
## ... . . . . . .
## GENE49996 0.19318540 0.97453064 0.63262294 . -1.54549761 -0.94665246
## GENE49997 2.06692566 0.34898517 -1.56896585 . 1.27972835 1.05390276
## GENE49998 1.49203102 0.71770173 0.40263054 . 0.28028922 1.98605602
## GENE49999 -0.82881929 -0.78914410 -1.17557005 . 0.01503384 -1.21377941
## GENE50000 -0.37533534 -0.47031175 0.07635521 . -2.75357943 0.70741429
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> matrix of class DelayedMatrix and type "double":
## SampleID1 SampleID2 SampleID3 ... SampleID19 SampleID20
## GENE1 -0.98185094 -1.09247785 -2.44049822 . -0.0736439 0.2035806
## GENE2 -1.26963461 0.37376259 -0.55900720 . 1.0342642 -0.3150900
## GENE3 0.52504578 0.12308230 1.15378774 . -2.3556618 1.2136911
## GENE4 0.63845904 1.10536408 -0.07392009 . 0.4737823 0.3357675
## GENE5 -1.91778230 -1.18807778 -1.78248933 . -0.3238059 -1.0944165
## ... . . . . . .
## GENE49996 0.19318540 0.97453064 0.63262294 . -1.54549761 -0.94665246
## GENE49997 2.06692566 0.34898517 -1.56896585 . 1.27972835 1.05390276
## GENE49998 1.49203102 0.71770173 0.40263054 . 0.28028922 1.98605602
## GENE49999 -0.82881929 -0.78914410 -1.17557005 . 0.01503384 -1.21377941
## GENE50000 -0.37533534 -0.47031175 0.07635521 . -2.75357943 0.70741429
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
.