Contents

1 Overview

beachmat has a few useful utilities outside of the C++ API. This document describes how to use them.

2 Choosing HDF5 chunk dimensions

Given the dimensions of a matrix, users can choose HDF5 chunk dimensions that give fast performance for both row- and column-level access.

library(beachmat)
nrows <- 10000
ncols <- 200
getBestChunkDims(c(nrows, ncols))
## [1] 708  15

In the future, it should be possible to feed this back into the API. Currently, if chunk dimensions are not specified in the C++ code, the API will retrieve them from R via the getHDF5DumpChunkDim() function from HDF5Array. The aim is to also provide a setHDF5DumpChunkDim() function so that any chunk dimension specified in R will be respected.

3 Rechunking a HDF5 file

The most common access patterns for matrices (at least, for high-throughput biological data) is by row or by column. The rechunkByMargins() will take a HDF5 file and convert it to using purely row- or column-based chunks.

library(HDF5Array)
A <- as(matrix(runif(5000), nrow=100, ncol=50), "HDF5Array")
byrow <- rechunkByMargins(A, byrow=TRUE)
byrow
## <100 x 50> HDF5Matrix object of type "double":
##             [,1]      [,2]      [,3] ...      [,49]      [,50]
##   [1,] 0.8570540 0.2718039 0.3109512   . 0.38415832 0.44096151
##   [2,] 0.7049978 0.5093208 0.5793587   . 0.67626533 0.63845107
##   [3,] 0.1429905 0.1727937 0.6491899   . 0.17781961 0.78612775
##   [4,] 0.9027074 0.9916206 0.8333865   . 0.04535817 0.55444957
##   [5,] 0.1834593 0.7244919 0.7768454   . 0.03482194 0.99882906
##    ...         .         .         .   .          .          .
##  [96,] 0.1747163 0.1481265 0.3995648   .  0.4233303  0.5730586
##  [97,] 0.2483783 0.5341786 0.8734796   .  0.6117993  0.4781657
##  [98,] 0.5759660 0.1836422 0.1088920   .  0.8245431  0.5001515
##  [99,] 0.1160643 0.5768999 0.2638522   .  0.3639379  0.4302823
## [100,] 0.9362426 0.6160199 0.6338973   .  0.7132248  0.6445599
bycol <- rechunkByMargins(A, byrow=FALSE)
bycol
## <100 x 50> HDF5Matrix object of type "double":
##             [,1]      [,2]      [,3] ...      [,49]      [,50]
##   [1,] 0.8570540 0.2718039 0.3109512   . 0.38415832 0.44096151
##   [2,] 0.7049978 0.5093208 0.5793587   . 0.67626533 0.63845107
##   [3,] 0.1429905 0.1727937 0.6491899   . 0.17781961 0.78612775
##   [4,] 0.9027074 0.9916206 0.8333865   . 0.04535817 0.55444957
##   [5,] 0.1834593 0.7244919 0.7768454   . 0.03482194 0.99882906
##    ...         .         .         .   .          .          .
##  [96,] 0.1747163 0.1481265 0.3995648   .  0.4233303  0.5730586
##  [97,] 0.2483783 0.5341786 0.8734796   .  0.6117993  0.4781657
##  [98,] 0.5759660 0.1836422 0.1088920   .  0.8245431  0.5001515
##  [99,] 0.1160643 0.5768999 0.2638522   .  0.3639379  0.4302823
## [100,] 0.9362426 0.6160199 0.6338973   .  0.7132248  0.6445599

Rechunking can provide a substantial speed-up to downstream functions, especially those requiring access to random columns or rows. Indeed, the time saved in those functions often offsets the time spent in constructing a new HDF5Matrix.

4 Session information

sessionInfo()
## R version 3.5.0 Patched (2018-05-03 r74699)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server 2012 R2 x64 (build 9600)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=C                          
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] parallel  stats4    stats     graphics  grDevices utils     datasets 
## [8] methods   base     
## 
## other attached packages:
##  [1] HDF5Array_1.8.0     rhdf5_2.24.0        DelayedArray_0.6.0 
##  [4] BiocParallel_1.14.1 IRanges_2.14.10     S4Vectors_0.18.2   
##  [7] BiocGenerics_0.26.0 matrixStats_0.53.1  beachmat_1.2.1     
## [10] knitr_1.20          BiocStyle_2.8.1    
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.17    magrittr_1.5    stringr_1.3.1   tools_3.5.0    
##  [5] xfun_0.1        htmltools_0.3.6 yaml_2.1.19     rprojroot_1.3-2
##  [9] digest_0.6.15   bookdown_0.7    Rhdf5lib_1.2.1  evaluate_0.10.1
## [13] rmarkdown_1.9   stringi_1.2.2   compiler_3.5.0  backports_1.1.2