DelayedRandomArray 1.14.0
The DelayedRandomArray package implements DelayedArray
subclasses containing dynamically sampled random values.
Specifically, the actual values are never fully held in memory but are generated when the relevant part of the array is accessed.
This allows users to create very large arrays of random values that would not otherwise be possible by filling an ordinary matrix.
To install the package, follow the instructions on DelayedRandomArray landing page. Using the package is then as simple as:
library(DelayedRandomArray)
X <- RandomUnifArray(c(1e6, 1e6))
X
## <1000000 x 1000000> RandomUnifMatrix object of type "double":
## [,1] [,2] [,3] ... [,999999] [,1000000]
## [1,] 0.08817391 0.39148987 0.38273859 . 0.12225764 0.40398581
## [2,] 0.77429561 0.03485824 0.73866158 . 0.54549260 0.08253855
## [3,] 0.69082892 0.90020211 0.78120851 . 0.91666774 0.09091790
## [4,] 0.90466023 0.71546941 0.19597285 . 0.85322366 0.11890695
## [5,] 0.37851144 0.76947780 0.69946681 . 0.88102026 0.06135456
## ... . . . . . .
## [999996,] 0.974150363 0.222059883 0.970248806 . 0.8315941 0.5774910
## [999997,] 0.156866841 0.363178949 0.816545888 . 0.8787454 0.3220291
## [999998,] 0.469359386 0.992726300 0.262606502 . 0.2698490 0.1663358
## [999999,] 0.733778387 0.255601033 0.108765121 . 0.7956547 0.6724155
## [1000000,] 0.007646952 0.055140541 0.367624403 . 0.9125965 0.7338153
The resulting array can be used in any pipeline that is compatible with DelayedArray
objects.
This object occupies only 64 MB in memory,
whereas an ordinary matrix
would require 8 PB instead.
Almost every distribution in stats is available here. To list a few:
RandomNormArray(c(100, 50))
## <100 x 50> RandomNormMatrix object of type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 0.34528036 0.04453029 0.88554002 . 0.54203865 -2.24551335
## [2,] -0.49565045 -0.67366849 -0.99465906 . -0.73901940 -0.02809125
## [3,] 0.98514050 -1.73405537 -1.00509012 . -1.29482875 -0.07046152
## [4,] 1.17417683 1.33107982 -1.49771137 . 0.24291299 0.26735995
## [5,] 2.00218429 -1.53846538 -0.66544986 . -0.06610241 0.36562617
## ... . . . . . .
## [96,] -1.814281865 -0.742654434 -0.289996066 . -2.9730615 -0.9603054
## [97,] -0.785783569 -0.578044498 -1.440020340 . 0.1587641 -1.1280750
## [98,] 0.005566075 -0.826914441 -1.434274147 . -1.7601750 -0.2423120
## [99,] -1.230939519 -0.368991301 -0.266576339 . -1.0380849 0.1916262
## [100,] 2.059545479 0.032995183 0.959541956 . 0.6464433 0.4193521
RandomPoisArray(c(100, 50), lambda=5)
## <100 x 50> RandomPoisMatrix object of type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 4 2 3 . 4 6
## [2,] 4 6 3 . 3 3
## [3,] 10 4 5 . 4 8
## [4,] 3 4 7 . 4 4
## [5,] 3 3 5 . 3 1
## ... . . . . . .
## [96,] 9 4 4 . 5 10
## [97,] 5 4 4 . 7 6
## [98,] 4 5 1 . 9 6
## [99,] 7 7 6 . 5 3
## [100,] 6 4 4 . 3 5
RandomGammaArray(c(100, 50), shape=2, rate=5)
## <100 x 50> RandomGammaMatrix object of type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 1.2546436 0.5775379 0.7556036 . 0.38435850 0.50163631
## [2,] 0.8682903 0.4295402 0.4350582 . 0.56759065 0.86551524
## [3,] 0.3114927 0.5551068 0.3318388 . 0.42495650 0.15268162
## [4,] 0.8733942 0.3650470 0.2662891 . 0.33289519 0.03364659
## [5,] 0.6473035 0.1476695 0.4121684 . 0.27717312 0.17314961
## ... . . . . . .
## [96,] 0.48478620 0.15881932 0.24280660 . 0.54636762 0.22647533
## [97,] 1.08160180 0.19839666 0.44532055 . 0.28821598 0.39288605
## [98,] 0.57123710 0.17925709 0.30610609 . 0.15704457 0.20998599
## [99,] 0.46320986 0.13688796 0.63091914 . 0.06509057 0.40107733
## [100,] 0.11799290 0.08300917 0.15181755 . 0.32715561 0.10825684
RandomWeibullArray(c(100, 50), shape=5)
## <100 x 50> RandomWeibullMatrix object of type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 0.8924932 1.1035315 1.0573898 . 1.0496507 0.6576943
## [2,] 0.5876997 0.9450823 1.3459059 . 1.0776106 0.7747670
## [3,] 0.9809835 1.3546995 1.0043994 . 0.9373447 0.9590319
## [4,] 1.4098051 0.9084388 1.1222908 . 0.7231499 1.0090556
## [5,] 0.9918381 0.9322618 1.0074133 . 0.8931463 0.6332097
## ... . . . . . .
## [96,] 0.9347995 0.8267527 1.0856011 . 0.7372500 0.9615171
## [97,] 0.9816467 0.7958733 1.1036725 . 0.8943920 0.8046584
## [98,] 0.7729774 1.1824563 0.8454147 . 0.8600082 0.7838721
## [99,] 1.1941277 1.1576735 1.0208955 . 0.8505224 0.6609199
## [100,] 0.7792474 1.2144713 0.8896312 . 0.9369244 1.0154956
Distributional parameters can either be scalars:
RandomNormArray(c(100, 50), mean=1)
## <100 x 50> RandomNormMatrix object of type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 0.8247256 2.0524235 -0.0293392 . 0.8186611 1.0319933
## [2,] 1.0451684 2.1544899 2.5200065 . 1.7294111 1.9952514
## [3,] 0.7957778 1.1635673 0.3749614 . 1.1092834 0.2303591
## [4,] 1.1864744 0.1932773 0.9012413 . 1.9125477 3.1880659
## [5,] 0.6475880 -0.3267396 1.1227090 . -0.1790540 0.3154785
## ... . . . . . .
## [96,] 2.50407874 0.02240694 0.91580246 . 1.9356015 0.8041837
## [97,] 0.79271523 0.36338595 0.17950471 . 1.6277892 0.4798133
## [98,] 0.64777306 0.20939387 0.31125570 . 0.6892518 2.0849299
## [99,] 0.43151807 0.21785701 0.54398792 . 0.2755585 1.7952166
## [100,] 1.57219917 1.25990803 1.69668738 . 0.2441823 2.9374633
Or vectors, which are recycled along the length of the array:
RandomNormArray(c(100, 50), mean=1:100)
## <100 x 50> RandomNormMatrix object of type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] -0.9522197 -0.8688989 0.1114329 . 1.25606579 -0.16633901
## [2,] 2.4741391 2.5481973 1.7497772 . 1.80280140 0.09719626
## [3,] 2.1399748 5.0255312 5.0582467 . 3.30930077 2.35170445
## [4,] 4.8636613 4.4369685 4.0774230 . 3.48998043 4.97082506
## [5,] 4.5739080 3.6826179 3.7633595 . 3.86755871 3.69551562
## ... . . . . . .
## [96,] 96.27248 97.45789 95.68152 . 97.14233 96.50783
## [97,] 96.91332 96.18852 97.47114 . 95.63766 96.28370
## [98,] 97.19748 96.16954 97.90870 . 99.41663 98.04239
## [99,] 96.94171 98.92156 100.32810 . 99.37071 100.73413
## [100,] 99.41470 99.59509 97.97107 . 100.37579 99.77654
Or other arrays of the same dimensions, which are used to sample the corresponding parts of the random array:
means <- RandomNormArray(c(100, 50))
RandomPoisArray(c(100, 50), lambda=2^means)
## <100 x 50> RandomPoisMatrix object of type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 0 0 0 . 0 0
## [2,] 6 2 0 . 0 5
## [3,] 0 0 1 . 0 0
## [4,] 0 2 1 . 1 0
## [5,] 2 2 3 . 1 3
## ... . . . . . .
## [96,] 0 0 3 . 1 2
## [97,] 6 4 1 . 0 1
## [98,] 0 2 1 . 0 0
## [99,] 1 1 4 . 0 0
## [100,] 0 2 9 . 3 0
For example, a hypothetical simulation of a million-cell single-cell RNA-seq dataset might look like this:
ngenes <- 20000
log.abundances <- runif(ngenes, -2, 5)
nclusters <- 20 # define 20 clusters and their population means.
cluster.means <- matrix(2^rnorm(ngenes*nclusters, log.abundances, sd=2), ncol=nclusters)
ncells <- 1e6
clusters <- sample(nclusters, ncells, replace=TRUE) # randomly allocate cells
cell.means <- DelayedArray(cluster.means)[,clusters]
dispersions <- 0.05 + 10/cell.means # typical mean variance trend.
y <- RandomNbinomArray(c(ngenes, ncells), mu=cell.means, size=1/dispersions)
y
## <20000 x 1000000> RandomNbinomMatrix object of type "double":
## [,1] [,2] [,3] ... [,999999] [,1000000]
## [1,] 57 54 2 . 4 4
## [2,] 6 10 0 . 2 8
## [3,] 1 14 1 . 1 16
## [4,] 0 1 0 . 7 0
## [5,] 0 0 0 . 1 0
## ... . . . . . .
## [19996,] 0 4 7 . 0 3
## [19997,] 10 4 30 . 28 5
## [19998,] 0 35 0 . 0 4
## [19999,] 0 0 0 . 0 0
## [20000,] 13 0 3 . 6 0
Each random DelayedArray
s is broken into contiguous rectangular chunks of identical size and shape.
Each chunk is assigned a seed at construction time that is used to initialize a random number stream (using the PCG32 generator from the dqrng package).
When the user accesses any part of the array, we generate the random numbers in the overlapping chunks and return the desired values.
This provides efficient random access to any subarray without the need to use any jump-ahead functionality.
The chunking scheme determines the efficiency of accessing our random DelayedArray
s.
Chunks that are too large require unnecessary number generation when a subarray is requested, while chunks that are too small would increase memory usage and book-keeping overhead.
The “best” choice also depends on the downstream access pattern, if such information is known.
For example, in a matrix where each column is a chunk, retrieval of a column would be very efficient while retrieval of a single row would be very slow.
The default chunk dimensions are set to the square root of the array dimensions (or 100, whichever is larger), providing a reasonable compromise between all of these considerations.
This can also be manually specified with the chunkdim=
argument.
# Row-wise chunks:
RandomUnifArray(c(1000, 500), chunkdim=c(1, 500))
## <1000 x 500> RandomUnifMatrix object of type "double":
## [,1] [,2] [,3] ... [,499] [,500]
## [1,] 0.15942723 0.65967566 0.94929352 . 0.97573934 0.18369314
## [2,] 0.75928810 0.22501930 0.63304955 . 0.51772483 0.23867138
## [3,] 0.63189438 0.23270744 0.73121778 . 0.27779683 0.06114338
## [4,] 0.65437411 0.09519933 0.63483186 . 0.38284206 0.00167226
## [5,] 0.75155589 0.89749204 0.18987453 . 0.25100917 0.62095986
## ... . . . . . .
## [996,] 0.246154567 0.103128992 0.668834633 . 0.67813840 0.63695942
## [997,] 0.002600564 0.781320616 0.002458860 . 0.39209889 0.95007603
## [998,] 0.794596161 0.957415725 0.937763002 . 0.05175867 0.33987032
## [999,] 0.105120109 0.507806389 0.673989940 . 0.75692216 0.43088662
## [1000,] 0.499450253 0.634894461 0.328648097 . 0.18869727 0.42759286
# Column-wise chunks:
RandomUnifArray(c(1000, 500), chunkdim=c(1000, 1))
## <1000 x 500> RandomUnifMatrix object of type "double":
## [,1] [,2] [,3] ... [,499] [,500]
## [1,] 0.98393117 0.85074924 0.89608424 . 0.4006212 0.8830605
## [2,] 0.60679410 0.87052147 0.03460849 . 0.7662884 0.2065403
## [3,] 0.23693210 0.27671937 0.64003071 . 0.3151748 0.4961160
## [4,] 0.79508722 0.43426220 0.48514702 . 0.2801800 0.5851887
## [5,] 0.57907836 0.85810253 0.38993988 . 0.3117836 0.6539745
## ... . . . . . .
## [996,] 0.09965386 0.13883863 0.93885411 . 0.05374686 0.86080391
## [997,] 0.74963669 0.74679142 0.21303052 . 0.60455260 0.44072013
## [998,] 0.05953712 0.72737233 0.40482242 . 0.66436154 0.42384928
## [999,] 0.93515395 0.54835810 0.89596393 . 0.17793795 0.20055637
## [1000,] 0.19163371 0.04636463 0.50977451 . 0.51529320 0.71261790
Unlike other chunk-based DelayedArray
s, the actual values of the random DelayedArray
are dependent on the chunk parameters.
This is because the sampling is done within each chunk and any alteration to the chunk shape or size will rearrange the stream of random numbers within the array.
Thus, even when the seed is set, a different chunkdim
will yield different results:
set.seed(199)
RandomUnifArray(c(10, 5), chunkdim=c(1, 5))
## <10 x 5> RandomUnifMatrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.4432813900 0.7119991907 0.7257159729 0.9557158216 0.2104000037
## [2,] 0.3134128384 0.0670862596 0.3807734565 0.1526811279 0.1971345709
## [3,] 0.5525409468 0.7807079460 0.9905007351 0.0898367104 0.8541555854
## [4,] 0.8914577304 0.3764661429 0.6028462166 0.2576166289 0.0145520803
## [5,] 0.5042253651 0.2618340398 0.2405915416 0.6800763716 0.5603335327
## [6,] 0.3953960796 0.1314800435 0.9008538304 0.1165704445 0.9035755624
## [7,] 0.6270407308 0.0001234491 0.3542078000 0.3546805161 0.9760325255
## [8,] 0.2882098067 0.9133890264 0.8018615395 0.7615402588 0.6458653482
## [9,] 0.2233627134 0.2957882064 0.7371763836 0.3001928469 0.8730950402
## [10,] 0.6481108814 0.5491673953 0.6821353873 0.4434014931 0.1461723484
set.seed(199)
RandomUnifArray(c(10, 5), chunkdim=c(10, 1))
## <10 x 5> RandomUnifMatrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.44328139 0.31341284 0.55254095 0.89145773 0.50422537
## [2,] 0.71199919 0.06708626 0.78070795 0.37646614 0.26183404
## [3,] 0.72571597 0.38077346 0.99050074 0.60284622 0.24059154
## [4,] 0.95571582 0.15268113 0.08983671 0.25761663 0.68007637
## [5,] 0.21040000 0.19713457 0.85415559 0.01455208 0.56033353
## [6,] 0.63126383 0.10118984 0.76830283 0.17583353 0.62183470
## [7,] 0.87478047 0.26076972 0.58018989 0.92146566 0.90680388
## [8,] 0.55180537 0.20464839 0.38008429 0.56606812 0.75101891
## [9,] 0.40323090 0.20083487 0.28559824 0.67769322 0.11943279
## [10,] 0.13404760 0.94959186 0.61432837 0.67675354 0.64178865
Like any other random process, the seed should be set to achieve reproducible results.
We stress that the R-level seed only needs to be set before construction of the random DelayedArray
; it is not necessary to set the seed during its use.
This is because the class itself will define further seeds (one per chunk) and store these in the object for use in per-chunk sampling.
set.seed(999)
RandomNormArray(c(10, 5))
## <10 x 5> RandomNormMatrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.07299897 -0.62967402 0.48686043 -1.00406271 0.09839211
## [2,] -1.87549362 0.11653501 0.98496315 -0.85515988 -1.44932474
## [3,] 0.62150210 -0.24704984 -0.69766033 -1.60089511 0.94752723
## [4,] -1.68035676 0.20939894 -3.29663662 0.57270346 0.94413669
## [5,] 0.22933664 1.12333362 -0.77725398 1.59886411 1.07042538
## [6,] 0.43082191 -0.22740387 1.25381398 0.32842239 0.20448699
## [7,] -0.71424097 0.27773134 1.53748664 0.01761569 1.15470498
## [8,] 0.35908991 0.35618136 -0.49375892 -0.38652760 0.95591577
## [9,] 0.06472627 0.79333115 -0.73803086 -0.56434493 0.22473943
## [10,] -2.41389122 0.76273286 0.47567035 -0.08524682 -1.17849232
set.seed(999)
RandomNormArray(c(10, 5))
## <10 x 5> RandomNormMatrix object of type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.07299897 -0.62967402 0.48686043 -1.00406271 0.09839211
## [2,] -1.87549362 0.11653501 0.98496315 -0.85515988 -1.44932474
## [3,] 0.62150210 -0.24704984 -0.69766033 -1.60089511 0.94752723
## [4,] -1.68035676 0.20939894 -3.29663662 0.57270346 0.94413669
## [5,] 0.22933664 1.12333362 -0.77725398 1.59886411 1.07042538
## [6,] 0.43082191 -0.22740387 1.25381398 0.32842239 0.20448699
## [7,] -0.71424097 0.27773134 1.53748664 0.01761569 1.15470498
## [8,] 0.35908991 0.35618136 -0.49375892 -0.38652760 0.95591577
## [9,] 0.06472627 0.79333115 -0.73803086 -0.56434493 0.22473943
## [10,] -2.41389122 0.76273286 0.47567035 -0.08524682 -1.17849232
For certain distributions, it is possible to indicate that the array is sparse. This does not change the result or efficiency of the sampling process, but can still be useful as it allows downstream functions to use more efficient sparse algorithms. Of course, this is only relevant if the distributional parameters are such that sparsity is actually observed.
RandomPoisArray(c(1e6, 1e6), lambda=0.5) # dense by default
## <1000000 x 1000000> RandomPoisMatrix object of type "double":
## [,1] [,2] [,3] ... [,999999] [,1000000]
## [1,] 0 0 1 . 1 0
## [2,] 0 1 1 . 0 1
## [3,] 0 1 1 . 0 0
## [4,] 0 1 0 . 1 0
## [5,] 0 0 0 . 1 0
## ... . . . . . .
## [999996,] 1 0 0 . 1 1
## [999997,] 0 1 0 . 1 1
## [999998,] 2 0 1 . 0 0
## [999999,] 1 0 1 . 0 1
## [1000000,] 0 0 0 . 1 0
RandomPoisArray(c(1e6, 1e6), lambda=0.5, sparse=TRUE) # treat as sparse
## <1000000 x 1000000> sparse RandomPoisMatrix object of type "double":
## [,1] [,2] [,3] ... [,999999] [,1000000]
## [1,] 0 1 1 . 1 0
## [2,] 0 1 1 . 1 0
## [3,] 0 1 1 . 0 0
## [4,] 1 2 0 . 0 0
## [5,] 0 1 0 . 0 2
## ... . . . . . .
## [999996,] 0 0 1 . 0 0
## [999997,] 0 0 0 . 1 0
## [999998,] 3 0 0 . 1 1
## [999999,] 1 0 0 . 1 0
## [1000000,] 0 2 1 . 1 0
sessionInfo()
## R version 4.4.1 (2024-06-14)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.20-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0
##
## 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
##
## time zone: America/New_York
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] DelayedRandomArray_1.14.0 DelayedArray_0.32.0
## [3] SparseArray_1.6.0 S4Arrays_1.6.0
## [5] IRanges_2.40.0 abind_1.4-8
## [7] S4Vectors_0.44.0 MatrixGenerics_1.18.0
## [9] matrixStats_1.4.1 BiocGenerics_0.52.0
## [11] Matrix_1.7-1 BiocStyle_2.34.0
##
## loaded via a namespace (and not attached):
## [1] crayon_1.5.3 cli_3.6.3 knitr_1.48
## [4] rlang_1.1.4 xfun_0.48 jsonlite_1.8.9
## [7] dqrng_0.4.1 htmltools_0.5.8.1 sass_0.4.9
## [10] rmarkdown_2.28 grid_4.4.1 evaluate_1.0.1
## [13] jquerylib_0.1.4 fastmap_1.2.0 yaml_2.3.10
## [16] lifecycle_1.0.4 bookdown_0.41 BiocManager_1.30.25
## [19] compiler_4.4.1 Rcpp_1.0.13 XVector_0.46.0
## [22] lattice_0.22-6 digest_0.6.37 R6_2.5.1
## [25] bslib_0.8.0 tools_4.4.1 zlibbioc_1.52.0
## [28] cachem_1.1.0