Skip to content

Commit

Permalink
add read_zarr and write_zarr (#33)
Browse files Browse the repository at this point in the history
* add read_zarr and write_zarr

* update pkgdown

* update news
  • Loading branch information
rcannood authored Nov 15, 2024
1 parent a352d42 commit 0e1d063
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 56 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ export(read_loom)
export(read_mtx)
export(read_text)
export(read_umi_tools)
export(read_zarr)
export(write_csvs)
export(write_h5ad)
export(write_loom)
export(write_zarr)
importFrom(Matrix,sparseMatrix)
importFrom(R6,R6Class)
importFrom(assertthat,assert_that)
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# anndata 0.7.5.6
# anndata 0.8.0

* FUNCTIONALITY: Added support for `read_zarr()` and `write_zarr()` (PR #33).

* BUG FIX: Use the right interface for the `all.equal()` function (PR #32).

Expand Down
27 changes: 19 additions & 8 deletions R/class_anndata.R
Original file line number Diff line number Diff line change
Expand Up @@ -552,19 +552,18 @@ AnnDataR6 <- R6::R6Class(
#' }
write_csvs = function(dirname, skip_data = TRUE, sep = ",") {
dirname <- normalizePath(dirname, mustWork = FALSE)
private$.anndata$write_csvs(
invisible(py_to_r_ifneedbe(private$.anndata$write_csvs(
dirname = dirname,
skip_data = skip_data,
sep = sep
)
)))
},

#' @description Write .h5ad-formatted hdf5 file.
#'
#' Generally, if you have sparse data that are stored as a dense matrix, you can
#' dramatically improve performance and reduce disk space by converting to a csr_matrix:
#'
#' @param anndata An [AnnData()] object
#' @param filename Filename of data file. Defaults to backing file.
#' @param compression See the h5py [filter pipeline](http://docs.h5py.org/en/latest/high/dataset.html#dataset-compression).
#' Options are `"gzip"`, `"lzf"` or `NULL`.
Expand All @@ -591,17 +590,16 @@ AnnDataR6 <- R6::R6Class(
#' }
write_h5ad = function(filename, compression = NULL, compression_opts = NULL, as_dense = list()) {
filename <- normalizePath(filename, mustWork = FALSE)
private$.anndata$write_h5ad(
invisible(py_to_r_ifneedbe(private$.anndata$write_h5ad(
filename = filename,
compression = compression,
compression_opts = compression_opts,
as_dense = as_dense
)
)))
},

#' @description Write .loom-formatted hdf5 file.
#'
#' @param anndata An [AnnData()] object
#' @param filename The filename.
#' @param write_obsm_varm Whether or not to also write the varm and obsm.
#'
Expand All @@ -625,10 +623,23 @@ AnnDataR6 <- R6::R6Class(
#' }
write_loom = function(filename, write_obsm_varm = FALSE) {
filename <- normalizePath(filename, mustWork = FALSE)
private$.anndata$write_loom(
invisible(py_to_r_ifneedbe(private$.anndata$write_loom(
filename = filename,
write_obsm_varm = write_obsm_varm
)
)))
},

#' @description Write a hierarchical Zarr array store.
#' @param store The filename, a MutableMapping, or a Zarr storage class.
#' @param chunks Chunk size.
write_zarr = function(store, chunks = NULL) {
if (is.character(store)) {
store <- normalizePath(store, mustWork = FALSE)
}
invisible(py_to_r_ifneedbe(private$.anndata$write_zarr(
store = store,
chunks = chunks
)))
},

#' @description Print AnnData object
Expand Down
30 changes: 15 additions & 15 deletions R/read_zarr.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# #' Read from a hierarchical Zarr array store.
# #'
# #' @param store The filename, a MutableMapping, or a Zarr storage class.
# #'
# #' @export
# #'
# #' @examples
# #' \dontrun{
# #' ad <- read_umi_tools("...")
# #' }
# read_zarr <- function(store) {
# read_zarr(
# store = store
# )
# }
#' Read from a hierarchical Zarr array store.
#'
#' @param store The filename, a MutableMapping, or a Zarr storage class.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' ad <- read_zarr("...")
#' }
read_zarr <- function(store) {
read_zarr(
store = store
)
}
61 changes: 33 additions & 28 deletions R/write_zarr.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# #' Write a hierarchical Zarr array store.
# #'
# #' @param anndata An [AnnData()] object
# #' @param store The filename, a MutableMapping, or a Zarr storage class.
# #' @param chunks Chunk shape.
# #'
# #' @export
# #'
# #' @examples
# #' ad <- AnnData(
# #' X = matrix(c(0, 1, 2, 3), nrow = 2, byrow = TRUE),
# #' obs = data.frame(group = c("a", "b"), row.names = c("s1", "s2")),
# #' var = data.frame(type = c(1L, 2L), row.names = c("var1", "var2")),
# #' varm = list(
# #' ones = matrix(rep(1L, 10), nrow = 2),
# #' rand = matrix(rnorm(6), nrow = 2),
# #' zeros = matrix(rep(0L, 10), nrow = 2)
# #' ),
# #' uns = list(a = 1, b = 2, c = list(c.a = 3, c.b = 4))
# #' )
# #'
# #' write_zarr(ad, "output.zarr")
# write_zarr <- function(anndata, store, chunks = NULL) {
# invisible(py_to_r_ifneedbe(anndata$write_zarr(
# store = store,
# chunks = chunks
# )))
# }
#' Write a hierarchical Zarr array store.
#'
#' @param anndata An [AnnData()] object
#' @param store The filename, a MutableMapping, or a Zarr storage class.
#' @param chunks Chunk shape.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' ad <- AnnData(
#' X = matrix(c(0, 1, 2, 3), nrow = 2, byrow = TRUE),
#' obs = data.frame(group = c("a", "b"), row.names = c("s1", "s2")),
#' var = data.frame(type = c(1L, 2L), row.names = c("var1", "var2")),
#' varm = list(
#' ones = matrix(rep(1L, 10), nrow = 2),
#' rand = matrix(rnorm(6), nrow = 2),
#' zeros = matrix(rep(0L, 10), nrow = 2)
#' ),
#' uns = list(a = 1, b = 2, c = list(c.a = 3, c.b = 4))
#' )
#'
#' ad$write_zarr("output.zarr")
#' write_zarr(ad, "output.zarr")
#'
#' unlink("output.zarr", recursive = TRUE)
#' }
write_zarr <- function(anndata, store, chunks = NULL) {
invisible(py_to_r_ifneedbe(anndata$write_zarr(
store = store,
chunks = chunks
)))
}
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ reference:
- read_mtx
- read_text
- read_umi_tools
- read_zarr
- title: Writing an AnnData object to a file
- contents:
- write_csvs
- write_h5ad
- write_loom
- write_zarr
- title: Installer
desc: Helper function for installing the anndata Python package
- contents:
Expand Down
24 changes: 20 additions & 4 deletions man/AnnData.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/read_zarr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions man/write_zarr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e1d063

Please sign in to comment.