1 Introduction

TileDB implements a framework for local and remote storage of dense and sparse arrays. We can use this as a DelayedArray backend to provide an array-level abstraction, thus allowing the data to be used in many places where an ordinary array or matrix might be used. The TileDBArray package implements the necessary wrappers around TileDB-R to support read/write operations on TileDB arrays within the DelayedArray framework.

2 Creating a TileDBArray

Creating a TileDBArray is as easy as:

X <- matrix(rnorm(1000), ncol=10)
library(TileDBArray)
writeTileDBArray(X)
## <100 x 10> matrix of class TileDBMatrix and type "double":
##               [,1]        [,2]        [,3] ...        [,9]       [,10]
##   [1,]  -1.1608211   0.8433144   0.7498662   .  1.50259182  0.48598305
##   [2,]  -0.8676094  -1.4678745   1.8628641   . -0.88342286  2.64474468
##   [3,]  -1.0478056  -0.7396246  -0.8847237   .  0.11001365  0.42759869
##   [4,]   0.3856187  -0.1162008   0.1747860   . -1.00988283 -0.04337248
##   [5,]   0.5394662   1.3658079  -0.1645872   .  1.44285887  0.40396581
##    ...           .           .           .   .           .           .
##  [96,]  0.85152010  0.10760503  0.08818846   . -0.48316157  0.95730585
##  [97,] -0.74720567  1.16132633 -1.39346887   . -0.49479162  0.54027313
##  [98,]  0.77291353 -0.35277159 -0.77578364   .  1.61181206  1.62404532
##  [99,] -0.26503572  2.23262494  0.55548768   . -0.78713524  1.46384464
## [100,] -0.42790556 -0.38094302  0.73186232   . -0.08940059 -1.36369958

Alternatively, we can use coercion methods:

as(X, "TileDBArray")
## <100 x 10> matrix of class TileDBMatrix and type "double":
##               [,1]        [,2]        [,3] ...        [,9]       [,10]
##   [1,]  -1.1608211   0.8433144   0.7498662   .  1.50259182  0.48598305
##   [2,]  -0.8676094  -1.4678745   1.8628641   . -0.88342286  2.64474468
##   [3,]  -1.0478056  -0.7396246  -0.8847237   .  0.11001365  0.42759869
##   [4,]   0.3856187  -0.1162008   0.1747860   . -1.00988283 -0.04337248
##   [5,]   0.5394662   1.3658079  -0.1645872   .  1.44285887  0.40396581
##    ...           .           .           .   .           .           .
##  [96,]  0.85152010  0.10760503  0.08818846   . -0.48316157  0.95730585
##  [97,] -0.74720567  1.16132633 -1.39346887   . -0.49479162  0.54027313
##  [98,]  0.77291353 -0.35277159 -0.77578364   .  1.61181206  1.62404532
##  [99,] -0.26503572  2.23262494  0.55548768   . -0.78713524  1.46384464
## [100,] -0.42790556 -0.38094302  0.73186232   . -0.08940059 -1.36369958

This process works also for sparse matrices:

Y <- Matrix::rsparsematrix(1000, 1000, density=0.01)
writeTileDBArray(Y)
## <1000 x 1000> sparse matrix of class TileDBMatrix and type "double":
##            [,1]    [,2]    [,3] ...  [,999] [,1000]
##    [1,]       0       0       0   .       0       0
##    [2,]       0       0       0   .       0       0
##    [3,]       0       0       0   .       0       0
##    [4,]       0       0       0   .       0       0
##    [5,]       0       0       0   .       0       0
##     ...       .       .       .   .       .       .
##  [996,]       0       0       0   .     0.0    -0.3
##  [997,]       0       0       0   .     0.0     1.1
##  [998,]       0       0       0   .     0.0    -2.5
##  [999,]       0       0       0   .     0.0     0.0
## [1000,]       0       0       0   .     0.0     0.0

Logical and integer matrices are supported:

writeTileDBArray(Y > 0)
## <1000 x 1000> sparse matrix of class TileDBMatrix and type "logical":
##            [,1]    [,2]    [,3] ...  [,999] [,1000]
##    [1,]   FALSE   FALSE   FALSE   .   FALSE   FALSE
##    [2,]   FALSE   FALSE   FALSE   .   FALSE   FALSE
##    [3,]   FALSE   FALSE   FALSE   .   FALSE   FALSE
##    [4,]   FALSE   FALSE   FALSE   .   FALSE   FALSE
##    [5,]   FALSE   FALSE   FALSE   .   FALSE   FALSE
##     ...       .       .       .   .       .       .
##  [996,]   FALSE   FALSE   FALSE   .   FALSE   FALSE
##  [997,]   FALSE   FALSE   FALSE   .   FALSE    TRUE
##  [998,]   FALSE   FALSE   FALSE   .   FALSE   FALSE
##  [999,]   FALSE   FALSE   FALSE   .   FALSE   FALSE
## [1000,]   FALSE   FALSE   FALSE   .   FALSE   FALSE

As are matrices with dimension names:

rownames(X) <- sprintf("GENE_%i", seq_len(nrow(X)))
colnames(X) <- sprintf("SAMP_%i", seq_len(ncol(X)))
writeTileDBArray(X)
## <100 x 10> matrix of class TileDBMatrix and type "double":
##               SAMP_1      SAMP_2      SAMP_3 ...      SAMP_9     SAMP_10
##   GENE_1  -1.1608211   0.8433144   0.7498662   .  1.50259182  0.48598305
##   GENE_2  -0.8676094  -1.4678745   1.8628641   . -0.88342286  2.64474468
##   GENE_3  -1.0478056  -0.7396246  -0.8847237   .  0.11001365  0.42759869
##   GENE_4   0.3856187  -0.1162008   0.1747860   . -1.00988283 -0.04337248
##   GENE_5   0.5394662   1.3658079  -0.1645872   .  1.44285887  0.40396581
##      ...           .           .           .   .           .           .
##  GENE_96  0.85152010  0.10760503  0.08818846   . -0.48316157  0.95730585
##  GENE_97 -0.74720567  1.16132633 -1.39346887   . -0.49479162  0.54027313
##  GENE_98  0.77291353 -0.35277159 -0.77578364   .  1.61181206  1.62404532
##  GENE_99 -0.26503572  2.23262494  0.55548768   . -0.78713524  1.46384464
## GENE_100 -0.42790556 -0.38094302  0.73186232   . -0.08940059 -1.36369958

3 Manipulating TileDBArrays

TileDBArrays are simply DelayedArray objects and can be manipulated as such. The usual conventions for extracting data from matrix-like objects work as expected:

out <- as(X, "TileDBArray")
dim(out)
## [1] 100  10
head(rownames(out))
## [1] "GENE_1" "GENE_2" "GENE_3" "GENE_4" "GENE_5" "GENE_6"
head(out[,1])
##     GENE_1     GENE_2     GENE_3     GENE_4     GENE_5     GENE_6 
## -1.1608211 -0.8676094 -1.0478056  0.3856187  0.5394662  0.5371006

We can also perform manipulations like subsetting and arithmetic. Note that these operations do not affect the data in the TileDB backend; rather, they are delayed until the values are explicitly required, hence the creation of the DelayedMatrix object.

out[1:5,1:5] 
## <5 x 5> matrix of class DelayedMatrix and type "double":
##            SAMP_1     SAMP_2     SAMP_3     SAMP_4     SAMP_5
## GENE_1 -1.1608211  0.8433144  0.7498662  1.8282469  0.2782223
## GENE_2 -0.8676094 -1.4678745  1.8628641  0.2341848  0.4379516
## GENE_3 -1.0478056 -0.7396246 -0.8847237 -1.1507344  0.7411551
## GENE_4  0.3856187 -0.1162008  0.1747860  0.1110961 -0.3616431
## GENE_5  0.5394662  1.3658079 -0.1645872  0.1120524 -0.9110517
out * 2
## <100 x 10> matrix of class DelayedMatrix and type "double":
##              SAMP_1     SAMP_2     SAMP_3 ...      SAMP_9     SAMP_10
##   GENE_1 -2.3216421  1.6866287  1.4997325   .  3.00518363  0.97196610
##   GENE_2 -1.7352188 -2.9357490  3.7257283   . -1.76684572  5.28948936
##   GENE_3 -2.0956112 -1.4792493 -1.7694474   .  0.22002731  0.85519738
##   GENE_4  0.7712375 -0.2324017  0.3495720   . -2.01976567 -0.08674496
##   GENE_5  1.0789325  2.7316159 -0.3291745   .  2.88571773  0.80793161
##      ...          .          .          .   .           .           .
##  GENE_96  1.7030402  0.2152101  0.1763769   .  -0.9663231   1.9146117
##  GENE_97 -1.4944113  2.3226527 -2.7869377   .  -0.9895832   1.0805463
##  GENE_98  1.5458271 -0.7055432 -1.5515673   .   3.2236241   3.2480906
##  GENE_99 -0.5300714  4.4652499  1.1109754   .  -1.5742705   2.9276893
## GENE_100 -0.8558111 -0.7618860  1.4637246   .  -0.1788012  -2.7273992

We can also do more complex matrix operations that are supported by DelayedArray:

colSums(out)
##      SAMP_1      SAMP_2      SAMP_3      SAMP_4      SAMP_5      SAMP_6 
##  -5.6010188   8.4289318   1.6529164  -6.6769968 -10.6610349   0.5721120 
##      SAMP_7      SAMP_8      SAMP_9     SAMP_10 
##  19.9832112   0.9540254   0.4761990  15.0897551
out %*% runif(ncol(out))
## <100 x 1> matrix of class DelayedMatrix and type "double":
##                   y
##   GENE_1  2.8126273
##   GENE_2  0.1413686
##   GENE_3  0.2564523
##   GENE_4 -2.3042392
##   GENE_5  1.6287493
##      ...          .
##  GENE_96  1.4615069
##  GENE_97  0.4431825
##  GENE_98  2.0895062
##  GENE_99  2.2732214
## GENE_100 -2.5995860

4 Controlling backend creation

We can adjust some parameters for creating the backend with appropriate arguments to writeTileDBArray(). For example, the example below allows us to control the path to the backend as well as the name of the attribute containing the data.

X <- matrix(rnorm(1000), ncol=10)
path <- tempfile()
writeTileDBArray(X, path=path, attr="WHEE")
## <100 x 10> matrix of class TileDBMatrix and type "double":
##              [,1]       [,2]       [,3] ...       [,9]      [,10]
##   [1,]  0.5587964 -0.3083618  0.2450728   .  1.8387422  1.0228856
##   [2,]  0.8728848 -1.0503277 -1.0795448   .  1.1686708 -0.9368012
##   [3,]  1.0604088  1.0319793  2.0379664   .  1.4058148  0.4869698
##   [4,] -0.5220051  0.2219830  1.1282816   .  0.3378249 -1.1100130
##   [5,] -1.9327961  1.6156718 -0.9242893   . -0.3612585  0.2080742
##    ...          .          .          .   .          .          .
##  [96,]  0.7344385 -0.5246624 -1.2787251   .  0.3443644  1.6244442
##  [97,]  1.1423985 -1.1531092  0.1613501   . -0.7620641 -0.4379414
##  [98,] -0.2180469 -0.7432002  1.4732332   .  1.3139830 -2.0202422
##  [99,]  0.3193004  0.9660323  0.6096236   . -2.1811594  0.3797833
## [100,] -1.5453103  1.0867700 -0.1162054   . -0.5226005  0.5944498

As these arguments cannot be passed during coercion, we instead provide global variables that can be set or unset to affect the outcome.

path2 <- tempfile()
setTileDBPath(path2)
as(X, "TileDBArray") # uses path2 to store the backend.
## <100 x 10> matrix of class TileDBMatrix and type "double":
##              [,1]       [,2]       [,3] ...       [,9]      [,10]
##   [1,]  0.5587964 -0.3083618  0.2450728   .  1.8387422  1.0228856
##   [2,]  0.8728848 -1.0503277 -1.0795448   .  1.1686708 -0.9368012
##   [3,]  1.0604088  1.0319793  2.0379664   .  1.4058148  0.4869698
##   [4,] -0.5220051  0.2219830  1.1282816   .  0.3378249 -1.1100130
##   [5,] -1.9327961  1.6156718 -0.9242893   . -0.3612585  0.2080742
##    ...          .          .          .   .          .          .
##  [96,]  0.7344385 -0.5246624 -1.2787251   .  0.3443644  1.6244442
##  [97,]  1.1423985 -1.1531092  0.1613501   . -0.7620641 -0.4379414
##  [98,] -0.2180469 -0.7432002  1.4732332   .  1.3139830 -2.0202422
##  [99,]  0.3193004  0.9660323  0.6096236   . -2.1811594  0.3797833
## [100,] -1.5453103  1.0867700 -0.1162054   . -0.5226005  0.5944498

5 Session information

sessionInfo()
## R Under development (unstable) (2021-10-19 r81077)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.15-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.15-bioc/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] TileDBArray_1.5.0    DelayedArray_0.21.0  IRanges_2.29.0      
## [4] S4Vectors_0.33.0     MatrixGenerics_1.7.0 matrixStats_0.61.0  
## [7] BiocGenerics_0.41.0  Matrix_1.3-4         BiocStyle_2.23.0    
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.7          bslib_0.3.1         compiler_4.2.0     
##  [4] BiocManager_1.30.16 jquerylib_0.1.4     tools_4.2.0        
##  [7] digest_0.6.28       bit_4.0.4           jsonlite_1.7.2     
## [10] evaluate_0.14       lattice_0.20-45     nanotime_0.3.3     
## [13] rlang_0.4.12        RcppCCTZ_0.2.9      yaml_2.2.1         
## [16] xfun_0.27           fastmap_1.1.0       stringr_1.4.0      
## [19] knitr_1.36          sass_0.4.0          bit64_4.0.5        
## [22] grid_4.2.0          R6_2.5.1            rmarkdown_2.11     
## [25] bookdown_0.24       tiledb_0.9.7        magrittr_2.0.1     
## [28] htmltools_0.5.2     stringi_1.7.5       zoo_1.8-9