DelayedRandomArray 1.6.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> matrix of class RandomUnifMatrix and type "double":
## [,1] [,2] [,3] ... [,999999] [,1000000]
## [1,] 0.04573010 0.65059924 0.59999300 . 0.4687060 0.5642550
## [2,] 0.67073922 0.66363005 0.80829888 . 0.5127843 0.9479877
## [3,] 0.40638606 0.20832686 0.04116015 . 0.1637485 0.9404700
## [4,] 0.79936377 0.93790085 0.81896811 . 0.7123159 0.3861774
## [5,] 0.56154323 0.81770244 0.37476081 . 0.8249914 0.0369208
## ... . . . . . .
## [999996,] 0.90155756 0.34430256 0.44022733 . 0.6413327 0.7970580
## [999997,] 0.97882085 0.63071811 0.58350017 . 0.8974202 0.5021385
## [999998,] 0.54537854 0.56044549 0.56547904 . 0.3078053 0.2801667
## [999999,] 0.26616786 0.06841579 0.61842213 . 0.9495944 0.5534554
## [1000000,] 0.91958396 0.77803157 0.16353031 . 0.2709708 0.7283858
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> matrix of class RandomNormMatrix and type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 1.55528716 0.31719521 -0.08241200 . 0.28738777 -2.20649588
## [2,] 1.58987611 1.04177743 -0.01203431 . 0.76012703 0.68279631
## [3,] 0.48672229 0.31997577 0.29537301 . 0.31104622 -0.80097596
## [4,] 1.02897864 -1.39741973 -0.49733726 . -0.01600282 -0.78411328
## [5,] 0.64679153 1.80169246 -0.75969804 . -1.39795550 0.77249905
## ... . . . . . .
## [96,] 0.1099218 -0.8246488 0.5815088 . -1.1905684 -0.5079294
## [97,] -0.6049937 -1.0106008 0.5504460 . 0.2863816 -2.9167693
## [98,] -0.8815639 -0.6216436 -0.5853708 . -0.1756517 -1.0359378
## [99,] 1.6509502 0.7287753 -1.4944717 . 0.3556818 0.7606911
## [100,] 0.4397914 0.5703556 0.7404284 . -0.4040864 3.0321344
RandomPoisArray(c(100, 50), lambda=5)
## <100 x 50> matrix of class RandomPoisMatrix and type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 10 6 3 . 6 5
## [2,] 4 4 1 . 5 2
## [3,] 6 5 4 . 9 10
## [4,] 3 2 2 . 6 7
## [5,] 4 5 7 . 10 2
## ... . . . . . .
## [96,] 3 2 4 . 3 5
## [97,] 5 6 5 . 5 4
## [98,] 5 5 3 . 1 4
## [99,] 3 4 4 . 4 5
## [100,] 8 2 5 . 4 5
RandomGammaArray(c(100, 50), shape=2, rate=5)
## <100 x 50> matrix of class RandomGammaMatrix and type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 0.26231245 0.69446630 0.25472828 . 0.31570112 0.39529275
## [2,] 1.18825589 0.25093914 0.24730054 . 0.08665834 0.26893947
## [3,] 1.06226177 0.29113772 0.18967895 . 0.07457025 0.20874985
## [4,] 0.34302221 0.37564215 0.45487567 . 0.14987995 0.46773497
## [5,] 0.17560257 0.09596861 0.27974409 . 0.18386626 0.41400279
## ... . . . . . .
## [96,] 0.24669443 0.66659631 0.43860827 . 0.03148077 0.09278977
## [97,] 0.22612674 0.30822802 0.94955531 . 0.61259877 0.40360596
## [98,] 0.26839768 0.49225578 0.62963208 . 0.47628678 0.46042988
## [99,] 0.05701946 0.02668090 0.36199690 . 1.01485909 0.44122217
## [100,] 0.65009268 0.24725616 0.06756197 . 0.65572956 0.42681039
RandomWeibullArray(c(100, 50), shape=5)
## <100 x 50> matrix of class RandomWeibullMatrix and type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 0.9979342 0.8593263 0.8811482 . 0.9585632 0.6601364
## [2,] 0.8876490 1.0052098 1.1150330 . 0.7401560 0.6275827
## [3,] 1.0697242 0.9130379 1.0319468 . 1.1354915 1.0195005
## [4,] 1.1104291 1.1032006 1.1423977 . 0.9418005 0.9288542
## [5,] 0.9247534 1.0218364 0.7797919 . 0.9264983 0.9930363
## ... . . . . . .
## [96,] 1.1311176 0.9701935 0.8195085 . 1.0315783 1.1154366
## [97,] 0.9054922 0.7795762 0.7905017 . 0.7337628 0.5597514
## [98,] 0.8441021 0.9718044 0.5560229 . 0.7530214 1.1848423
## [99,] 0.6408970 0.9125183 0.7851929 . 0.8390015 0.8724000
## [100,] 1.1819380 0.7112706 1.0262829 . 0.6182496 0.9177421
Distributional parameters can either be scalars:
RandomNormArray(c(100, 50), mean=1)
## <100 x 50> matrix of class RandomNormMatrix and type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 0.5901170 0.4274824 0.7988096 . 1.4606146 2.3187295
## [2,] 2.1237740 0.9354750 0.9947153 . 0.6706762 1.7750928
## [3,] -0.7387783 1.6334786 -0.4945214 . 0.5458465 1.8694586
## [4,] 1.5476065 0.6211566 2.0352014 . 2.6818852 2.3072774
## [5,] -1.2017936 0.4250105 1.7897422 . 1.3619662 1.8932200
## ... . . . . . .
## [96,] 1.75055287 -0.73074051 0.04760945 . -0.1369662 2.4088737
## [97,] 1.24981197 0.82630171 -0.25002665 . 0.4007348 2.0639989
## [98,] 0.81083753 1.70777214 -0.70093729 . 0.6741271 -0.7542133
## [99,] 1.44243641 0.01573252 2.87688218 . 0.5564812 1.3010963
## [100,] 1.26059094 -0.41518971 0.66300901 . 1.3322970 -0.8811052
Or vectors, which are recycled along the length of the array:
RandomNormArray(c(100, 50), mean=1:100)
## <100 x 50> matrix of class RandomNormMatrix and type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 0.0506731 1.5648422 1.4679184 . -0.2807192 1.0924195
## [2,] 2.2894684 3.5993539 1.5596241 . 3.3327407 2.8480401
## [3,] 2.8396640 4.4278966 1.4066890 . 4.1297815 4.6418944
## [4,] 4.6867813 4.8432099 2.7620662 . 2.5691893 4.3216085
## [5,] 4.1532698 6.4333868 4.6591518 . 5.8140798 4.9879408
## ... . . . . . .
## [96,] 94.75097 95.73439 95.18160 . 95.44160 95.59000
## [97,] 96.65449 95.81757 97.16714 . 97.25793 97.27011
## [98,] 96.65163 99.11131 97.01746 . 98.78106 98.76188
## [99,] 98.79891 98.88826 100.65226 . 97.23283 100.06200
## [100,] 99.90397 99.30455 101.68292 . 99.61260 101.51475
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> matrix of class RandomPoisMatrix and type "double":
## [,1] [,2] [,3] ... [,49] [,50]
## [1,] 1 2 2 . 2 0
## [2,] 1 0 2 . 1 0
## [3,] 3 0 1 . 1 1
## [4,] 3 0 0 . 1 1
## [5,] 0 0 0 . 1 1
## ... . . . . . .
## [96,] 0 1 1 . 0 0
## [97,] 0 1 1 . 0 2
## [98,] 3 3 5 . 1 1
## [99,] 5 0 1 . 0 1
## [100,] 0 1 1 . 2 3
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> matrix of class RandomNbinomMatrix and type "double":
## [,1] [,2] [,3] ... [,999999] [,1000000]
## [1,] 52 24 5 . 34 34
## [2,] 7 10 1 . 11 0
## [3,] 37 0 14 . 41 54
## [4,] 59 12 77 . 28 0
## [5,] 0 3 0 . 0 2
## ... . . . . . .
## [19996,] 166 10 83 . 3 388
## [19997,] 8 0 1 . 132 0
## [19998,] 1 297 9 . 7 1
## [19999,] 48 17 9 . 26 5
## [20000,] 32 53 4 . 0 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> matrix of class RandomUnifMatrix and type "double":
## [,1] [,2] [,3] ... [,499] [,500]
## [1,] 0.1347730 0.4672568 0.3238538 . 0.3123918 0.2662531
## [2,] 0.2990979 0.7172555 0.5180883 . 0.7860387 0.5569007
## [3,] 0.2365042 0.3423593 0.1778448 . 0.7026736 0.3256180
## [4,] 0.3835321 0.7340019 0.4742630 . 0.2154976 0.6943552
## [5,] 0.0760848 0.3728443 0.9266789 . 0.8139965 0.3616437
## ... . . . . . .
## [996,] 0.3110727 0.9024577 0.8750858 . 0.03007815 0.43577888
## [997,] 0.4547437 0.3260847 0.3932177 . 0.09345711 0.31358455
## [998,] 0.1382840 0.8558491 0.7543305 . 0.06971324 0.98315773
## [999,] 0.4794114 0.1940769 0.3703764 . 0.39516250 0.50787554
## [1000,] 0.5690870 0.8135662 0.2427483 . 0.93679621 0.46462087
# Column-wise chunks:
RandomUnifArray(c(1000, 500), chunkdim=c(1000, 1))
## <1000 x 500> matrix of class RandomUnifMatrix and type "double":
## [,1] [,2] [,3] ... [,499] [,500]
## [1,] 0.86925712 0.50068220 0.52170823 . 0.70344761 0.08867233
## [2,] 0.59814800 0.50573872 0.70887864 . 0.39755032 0.42724734
## [3,] 0.53810206 0.34844631 0.50719275 . 0.58874249 0.43772730
## [4,] 0.96802468 0.74904814 0.69294255 . 0.84205458 0.74689663
## [5,] 0.02913796 0.90883037 0.77770144 . 0.40181702 0.39968632
## ... . . . . . .
## [996,] 0.026798335 0.005987106 0.374979652 . 0.82664902 0.25409159
## [997,] 0.715598401 0.478608368 0.888751985 . 0.46831229 0.07449594
## [998,] 0.603648157 0.297646082 0.938225926 . 0.83271641 0.21462500
## [999,] 0.050800876 0.957142839 0.309525000 . 0.42439833 0.56847391
## [1000,] 0.750209818 0.131287742 0.871379280 . 0.87232125 0.17320948
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> matrix of class RandomUnifMatrix and 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> matrix of class RandomUnifMatrix and 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> matrix of class RandomNormMatrix and 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> matrix of class RandomNormMatrix and 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> matrix of class RandomPoisMatrix and 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 matrix of class RandomPoisMatrix and 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.2.1 (2022-06-23)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.16-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] DelayedRandomArray_1.6.0 DelayedArray_0.24.0 IRanges_2.32.0
## [4] S4Vectors_0.36.0 MatrixGenerics_1.10.0 matrixStats_0.62.0
## [7] BiocGenerics_0.44.0 Matrix_1.5-1 BiocStyle_2.26.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.9 knitr_1.40 magrittr_2.0.3
## [4] lattice_0.20-45 R6_2.5.1 rlang_1.0.6
## [7] fastmap_1.1.0 stringr_1.4.1 tools_4.2.1
## [10] grid_4.2.1 xfun_0.34 dqrng_0.3.0
## [13] cli_3.4.1 jquerylib_0.1.4 htmltools_0.5.3
## [16] yaml_2.3.6 digest_0.6.30 bookdown_0.29
## [19] BiocManager_1.30.19 sass_0.4.2 cachem_1.0.6
## [22] evaluate_0.17 rmarkdown_2.17 stringi_1.7.8
## [25] compiler_4.2.1 bslib_0.4.0 jsonlite_1.8.3