-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement backend for
MatrixH5
class
- Loading branch information
Showing
11 changed files
with
592 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# For every new class | ||
# - add method of subset: Method-subset.R | ||
# - add method of showtree: showtree.R | ||
methods::setClass("BPCellsHDF5Seed", | ||
contains = c("BPCellsSeed", get_class("MatrixH5")) | ||
) | ||
|
||
BPCellsHDF5Seed <- function(x) methods::as(x, "BPCellsHDF5Seed") | ||
|
||
#' @export | ||
#' @rdname BPCellsSeed | ||
methods::setMethod("BPCellsSeed", "MatrixH5", function(x) { | ||
BPCellsHDF5Seed(x = x) | ||
}) | ||
|
||
#' @importClassesFrom DelayedArray DelayedArray | ||
#' @export | ||
#' @rdname BPCellsSeed | ||
#' @include Class-BPCellsMatrix.R | ||
methods::setClass("BPCellsHDF5Array", | ||
contains = "BPCellsArray", | ||
slots = c(seed = "BPCellsHDF5Seed") | ||
) | ||
|
||
#' @importFrom DelayedArray DelayedArray | ||
#' @importFrom DelayedArray new_DelayedArray | ||
#' @export | ||
#' @rdname BPCellsMatrix-class | ||
methods::setMethod( | ||
"DelayedArray", "BPCellsHDF5Seed", | ||
function(seed) new_DelayedArray(seed, Class = "BPCellsHDF5Array") | ||
) | ||
|
||
#' @export | ||
#' @rdname BPCellsMatrix-class | ||
methods::setClass("BPCellsHDF5Matrix", | ||
contains = c("BPCellsMatrix"), | ||
slots = c(seed = "BPCellsHDF5Seed") | ||
) | ||
|
||
#' @importFrom DelayedArray matrixClass | ||
#' @export | ||
#' @rdname BPCellsMatrix-class | ||
methods::setMethod("matrixClass", "BPCellsHDF5Array", function(x) { | ||
"BPCellsHDF5Matrix" | ||
}) | ||
|
||
#' Read/write sparse matrices from (or into) HDF5 file | ||
#' | ||
#' @description | ||
#' - `readBPCellsHDF5Matrix`: read a sparce matrices from a HDF5 file on disk | ||
#' - `writeBPCellsHDF5Array`: Write a sparce matrices into a HDF5 file on disk | ||
#' @param path A string path of the `HDF5` file to read or save data into. | ||
#' @export | ||
#' @name BPCellsHDF5-IO | ||
readBPCellsHDF5Matrix <- function(path, group, buffer_size = 8192L) { | ||
assert_string(path, empty_ok = FALSE) | ||
obj <- BPCells::open_matrix_hdf5( | ||
path = path, group = group, | ||
buffer_size = as.integer(buffer_size) | ||
) | ||
DelayedArray(BPCellsHDF5Seed(obj)) | ||
} | ||
|
||
#' @inherit BPCells::write_matrix_hdf5 details | ||
#' @inheritParams writeBPCellsDirArray | ||
#' @inheritParams BPCells::open_matrix_hdf5 | ||
#' @param gzip Gzip compression level. Default is 0 (no gzip compression). This | ||
#' is recommended when both compression and compatibility with outside programs | ||
#' is required. Using `compress=TRUE` is recommended as it is >10x faster with | ||
#' often similar compression levels. So `gzip` will always be zero when | ||
#' `compress` is `TRUE`. | ||
#' @return A [BPCellsMatrix][BPCellsMatrix-class] object. | ||
#' @export | ||
#' @aliases writeBPCellsHDF5Array | ||
#' @rdname BPCellsHDF5-IO | ||
methods::setGeneric( | ||
"writeBPCellsHDF5Array", | ||
function(x, ...) standardGeneric("writeBPCellsHDF5Array") | ||
) | ||
|
||
.writeBPCellsHDF5Array <- function(x, path, group, bitpacking = TRUE, buffer_size = 8192L, chunk_size = 1024L, overwrite = FALSE, gzip = 0L) { | ||
assert_bool(bitpacking) | ||
assert_bool(overwrite) | ||
if (bitpacking) { | ||
gzip <- 0L | ||
} else { | ||
gzip <- as.integer(gzip) | ||
} | ||
obj <- BPCells::write_matrix_hdf5( | ||
mat = x, path = path, group = group, | ||
compress = bitpacking, | ||
buffer_size = as.integer(buffer_size), | ||
chunk_size = as.integer(chunk_size), | ||
overwrite = overwrite, gzip_level = gzip | ||
) | ||
DelayedArray(BPCellsHDF5Seed(obj)) | ||
} | ||
|
||
#' @export | ||
#' @rdname BPCellsHDF5-IO | ||
methods::setMethod( | ||
"writeBPCellsHDF5Array", "IterableMatrix", .writeBPCellsHDF5Array | ||
) | ||
|
||
#' @export | ||
#' @rdname BPCellsHDF5-IO | ||
methods::setMethod( | ||
"writeBPCellsHDF5Array", "BPCellsSeed", | ||
.writeBPCellsHDF5Array | ||
) | ||
|
||
#' @export | ||
#' @rdname BPCellsHDF5-IO | ||
methods::setMethod("writeBPCellsHDF5Array", "BPCellsMatrix", function(x, ...) { | ||
.writeBPCellsHDF5Array(x = x@seed, ...) | ||
}) | ||
|
||
#' @export | ||
#' @rdname BPCellsHDF5-IO | ||
methods::setMethod("writeBPCellsHDF5Array", "dgCMatrix", .writeBPCellsHDF5Array) | ||
|
||
#' @export | ||
#' @rdname BPCellsHDF5-IO | ||
methods::setMethod("writeBPCellsHDF5Array", "ANY", function(x, ...) { | ||
.writeBPCellsHDF5Array(x = coerce_dgCMatrix(x), ...) | ||
}) | ||
|
||
.as_BPCellsHDF5Array <- function(from) writeBPCellsHDF5Array(from) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.