library(dbMatrix)
#>
#> Attaching package: 'dbMatrix'
#> The following object is masked from 'package:base':
#>
#> load
dbMatrix arithmetic
dbMatrix
objects support Arith
and
Ops
operations. We will demonstrate how to perform
arithmetic operations on dbSparseMatrix
objects.
Note: Some operations with zero values are not yet
supported with dbMatrix objects. In addition, certain arithmetic
operations between dbMatrix
objects are also not yet
supported. We welcome user feedback and reporting issues on the Github page.
Get test data
The test file is a dgCMatrix
or compressed sparse column
matrix representing a single cell gene expression matrix. The file is in
the data
directory of the package.
Let’s load the .rds file and preview the object.
dgc <- readRDS("../data/dgc.rds")
dplyr::glimpse(dgc)
#> Loading required package: Matrix
#> Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> ..@ i : int [1:170625] 0 6 10 17 21 22 25 31 33 35 ...
#> ..@ p : int [1:625] 0 227 510 758 980 1293 1631 1976 2223 2434 ...
#> ..@ Dim : int [1:2] 634 624
#> ..@ Dimnames:List of 2
#> .. ..$ : chr [1:634] "Gna12" "Ccnd2" "Btbd17" "Sox9" ...
#> .. ..$ : chr [1:624] "AAAGGGATGTAGCAAG-1" "AAATGGCATGTCTTGT-1" "AAATGGTCAATGTGCC-1" "AAATTAACGGGTAGCT-1" ...
#> ..@ x : num [1:170625] 1 1 1 1 1 1 6 2 1 1 ...
#> ..@ factors : list()
The file contains 634 rows and 624 columns. The rows represent gene names and the columns represent cell names. The values are integers and represent the number of times a gene is detected in a cell. Like most single-cell RNA-seq data, the matrix is sparse.
Create a dbMatrix object
Let’s create a dbSparseMatrix
object from the above
dgc
object.
# Note: by default the constructor creates a dbMatrix object in-memory
con <- DBI::dbConnect(duckdb::duckdb(), ":memory:")
dbsm <- dbMatrix(value = dgc,
con = con,
name = 'visium',
class = "dbSparseMatrix",
overwrite = TRUE)
# preview the object
dbsm
#> 634 x 624 matrix of class "dbSparseMatrix"
#> [[ Colnames 'AAAGGGATGTAGCAAG-1', 'AAATGGCATGTCTTGT-1', 'AAATGGTCAATGTGCC-1' ... suppressing 618 ...'TTGTCGTTCAGTTACC-1', 'TTGTGGCCCTGACAGT-1', 'TTGTTCAGTGTGCTAC-1' ]]
#>
#> Gna12 1 2 1 1 9 1 3 5 3 .
#> Ccnd2 . 1 1 . . 1 . 1 1 .
#> Btbd17 . 1 1 1 . . 2 . . .
#>
#> ......suppressing 614 columns and 628 rows
#>
#> Gm19935 . 1 . . . . . . . .
#> 9630013A20Rik . . . . . . . . . .
#> 2900040C04Rik 1 . . . . . . . . 1
Scalar Arithmetic
dbMatrix
emulates scalar arithmetic in the
Matrix
package.
Note: Addition or subtraction with non-zero addends on a
dbSparseMatrix
results in a dbDenseMatrix
.
dbsm + 1
#> ℹ Densifying 'dbSparseMatrix' on the fly...
#> 634 x 624 matrix of class "dbDenseMatrix"
#> [[ Colnames 'AAAGGGATGTAGCAAG-1', 'AAATGGCATGTCTTGT-1', 'AAATGGTCAATGTGCC-1' ... suppressing 618 ...'TTGTCGTTCAGTTACC-1', 'TTGTGGCCCTGACAGT-1', 'TTGTTCAGTGTGCTAC-1' ]]
#>
#> Gna12 2 3 2 2 10 2 4 6 4 1
#> Ccnd2 1 2 2 1 1 2 1 2 2 1
#> Btbd17 1 2 2 2 1 1 3 1 1 1
#>
#> ......suppressing 614 columns and 628 rows
#>
#> Gm19935 1 2 1 1 1 1 1 1 1 1
#> 9630013A20Rik 1 1 1 1 1 1 1 1 1 1
#> 2900040C04Rik 2 1 1 1 1 1 1 1 1 2
dbsm * 100
#> 634 x 624 matrix of class "dbSparseMatrix"
#> [[ Colnames 'AAAGGGATGTAGCAAG-1', 'AAATGGCATGTCTTGT-1', 'AAATGGTCAATGTGCC-1' ... suppressing 618 ...'TTGTCGTTCAGTTACC-1', 'TTGTGGCCCTGACAGT-1', 'TTGTTCAGTGTGCTAC-1' ]]
#>
#> Gna12 100 200 100 100 900 100 300 500 300 .
#> Ccnd2 . 100 100 . . 100 . 100 100 .
#> Btbd17 . 100 100 100 . . 200 . . .
#>
#> ......suppressing 614 columns and 628 rows
#>
#> Gm19935 . 100 . . . . . . . .
#> 9630013A20Rik . . . . . . . . . .
#> 2900040C04Rik 100 . . . . . . . . 100
Matrix Arithmetic
dbMatrix
also supports matrix arithmetic for
dbMatrix
objects that are conformable.
dbsm + dbsm
#> 634 x 624 matrix of class "dbSparseMatrix"
#> [[ Colnames 'AAAGGGATGTAGCAAG-1', 'AAATGGCATGTCTTGT-1', 'AAATGGTCAATGTGCC-1' ... suppressing 618 ...'TTGTCGTTCAGTTACC-1', 'TTGTGGCCCTGACAGT-1', 'TTGTTCAGTGTGCTAC-1' ]]
#>
#> Gna12 2 4 2 2 18 2 6 10 6 .
#> Ccnd2 . 2 2 . . 2 . 2 2 .
#> Btbd17 . 2 2 2 . . 4 . . .
#>
#> ......suppressing 614 columns and 628 rows
#>
#> Gm19935 . 2 . . . . . . . .
#> 9630013A20Rik . . . . . . . . . .
#> 2900040C04Rik 2 . . . . . . . . 2
Matrix Multiplication
Hadamard product
dbsm * dbsm
#> 634 x 624 matrix of class "dbSparseMatrix"
#> [[ Colnames 'AAAGGGATGTAGCAAG-1', 'AAATGGCATGTCTTGT-1', 'AAATGGTCAATGTGCC-1' ... suppressing 618 ...'TTGTCGTTCAGTTACC-1', 'TTGTGGCCCTGACAGT-1', 'TTGTTCAGTGTGCTAC-1' ]]
#>
#> Gna12 1 4 1 1 81 1 9 25 9 .
#> Ccnd2 . 1 1 . . 1 . 1 1 .
#> Btbd17 . 1 1 1 . . 4 . . .
#>
#> ......suppressing 614 columns and 628 rows
#>
#> Gm19935 . 1 . . . . . . . .
#> 9630013A20Rik . . . . . . . . . .
#> 2900040C04Rik 1 . . . . . . . . 1
Session Info
sessionInfo()
#> R version 4.4.1 (2024-06-14)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 22.04.4 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so; LAPACK version 3.10.0
#>
#> locale:
#> [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
#> [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
#> [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
#> [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: UTC
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] Matrix_1.7-0 dbMatrix_0.0.0.9023
#>
#> loaded via a namespace (and not attached):
#> [1] bit_4.0.5 jsonlite_1.8.8 crayon_1.5.3
#> [4] dplyr_1.1.4 compiler_4.4.1 tidyselect_1.2.1
#> [7] blob_1.2.4 jquerylib_0.1.4 systemfonts_1.1.0
#> [10] textshaping_0.4.0 yaml_2.3.10 fastmap_1.2.0
#> [13] lattice_0.22-6 R6_2.5.1 generics_0.1.3
#> [16] knitr_1.48 tibble_3.2.1 desc_1.4.3
#> [19] MatrixGenerics_1.16.0 DBI_1.2.3 bslib_0.8.0
#> [22] pillar_1.9.0 rlang_1.1.4 utf8_1.2.4
#> [25] cachem_1.1.0 xfun_0.47 fs_1.6.4
#> [28] sass_0.4.9 bit64_4.0.5 cli_3.6.3
#> [31] withr_3.0.1 pkgdown_2.1.1 magrittr_2.0.3
#> [34] digest_0.6.37 grid_4.4.1 dbplyr_2.5.0
#> [37] lifecycle_1.0.4 vctrs_0.6.5 evaluate_1.0.0
#> [40] glue_1.7.0 data.table_1.16.0 duckdb_1.0.0-2
#> [43] ragg_1.3.3 fansi_1.0.6 purrr_1.0.2
#> [46] rmarkdown_2.28 matrixStats_1.4.1 tools_4.4.1
#> [49] pkgconfig_2.0.3 htmltools_0.5.8.1