File: README.md

package info (click to toggle)
r-bioc-alabaster.matrix 1.6.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 484 kB
  • sloc: cpp: 89; makefile: 2
file content (85 lines) | stat: -rw-r--r-- 3,701 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Save array-like objects to file

|Environment|Status|
|---|---|
|[BioC-release](https://bioconductor.org/packages/release/bioc/html/alabaster.matrix.html)|[![Release OK](https://bioconductor.org/shields/build/release/bioc/alabaster.matrix.svg)](https://bioconductor.org/checkResults/release/bioc-LATEST/alabaster.matrix/)|
|[BioC-devel](https://bioconductor.org/packages/devel/bioc/html/alabaster.matrix.html)|[![Devel OK](https://bioconductor.org/shields/build/devel/bioc/alabaster.matrix.svg)](https://bioconductor.org/checkResults/devel/bioc-LATEST/alabaster.matrix/)|

The **alabaster.matrix** package implements methods for saving and loading matrix- or array-like objects under the **alabaster** framework.
It provides a language-agnostic method for serializing data in arrays or abstractions thereof.
To get started, install the package and its dependencies from Bioconductor:

```r
# install.packages("BiocManager")
BiocManager::install("alabaster.matrix")
```

We can then save a variety of matrices and arrays to file.
For example, a sparse matrix can be saved to a HDF5 file in compressed sparse column format:

```r
library(Matrix)
y <- rsparsematrix(1000, 100, density=0.05)

# Saving it to a directory.
library(alabaster.matrix)
tmp <- tempfile()
saveObject(y, tmp)

# Reading it as a file-backed matrix.
roundtrip <- readObject(tmp)
roundtrip
## <1000 x 100> sparse ReloadedMatrix object of type "double":
##           [,1]   [,2]   [,3] ...  [,99] [,100]
##    [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
##  [997,]      0      0      0   .      0      0
##  [998,]      0      0      0   .      0      0
##  [999,]      0      0      0   .      0      0
## [1000,]      0      0      0   .      0      0

# Coerce this back into an in-memory sparse matrix:
inmemory <- as(roundtrip, "dgCMatrix")
```

We can also handle [`DelayedArray`](https://bioconductor.org/packages/DelayedArray) objects, possibly with preservation of delayed operations.
This uses the [**chihaya** specification](https://github.com/ArtifactDB/chihaya) to represent delayed operations inside a HDF5 file.

```r
library(DelayedArray)
y <- DelayedArray(rsparsematrix(1000, 100, 0.05))
y <- log1p(abs(y) / 1:100) # adding some delayed ops.

# Default method saves without preserving delayed operations.
tmp <- tempfile()
saveObject(y, tmp)
readObjectFile(tmp)$type
## [1] "compressed_sparse_matrix"

# But we can enable the delayed'ness explicitly, if so desired.
tmp2 <- tempfile()
saveObject(y, tmp2, delayedarray.preserve.ops=TRUE)
readObjectFile(tmp2)$type
## [1] "delayed_array"

roundtrip <- readObject(tmp2)
roundtrip
## <1000 x 100> sparse ReloadedMatrix object of type "double":
##           [,1]   [,2]   [,3] ...       [,99]      [,100]
##    [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.000000000 0.007368618
##  [997,]      0      0      0   . 0.000000000 0.000000000
##  [998,]      0      0      0   . 0.000000000 0.000000000
##  [999,]      0      0      0   . 0.000000000 0.000000000
## [1000,]      0      0      0   . 0.000000000 0.000000000
```