% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/write_zarr.R
\name{write_zarr}
\alias{write_zarr}
\title{Write Zarr}
\usage{
write_zarr(
  object,
  path,
  compression = c("none", "gzip", "blosc", "zstd", "lzma", "bz2", "zlib", "lz4"),
  mode = c("w-", "r", "r+", "a", "w", "x"),
  ...
)
}
\arguments{
\item{object}{The object to write, either a
\code{\link[SingleCellExperiment:SingleCellExperiment]{SingleCellExperiment::SingleCellExperiment}} or a
\code{\link[SeuratObject:Seurat-class]{SeuratObject::Seurat}} object}

\item{path}{Path of the file to write to}

\item{compression}{The compression algorithm to use when writing the Zarr
file. Can be one of \code{"none"}, \code{"gzip"}, \code{"blosc"}, \code{"zstd"},
\code{"lzma"}, \code{"bz2"}, \code{"zlib"}, \code{"lz4"}. Defaults to \code{"none"}.
See \code{help("compressors", package = "Rarr")}.}

\item{mode}{The mode to open the Zarr file.
\itemize{
\item \code{a} creates a new file or opens an existing one for read/write
\item \verb{r+} opens an existing file for read/write
\item \code{w} creates a file, truncating any existing ones
\item \verb{w-}/\code{x} are synonyms creating a file and failing if it already exists
}}

\item{...}{Additional arguments passed to \code{\link[=as_AnnData]{as_AnnData()}}}
}
\value{
\code{path} invisibly
}
\description{
Write a Zarr file
}
\details{
\subsection{\code{NULL} values}{

For compatibility with changes in Python \strong{anndata} 0.12.0, \code{NULL} values
in \code{uns} are written to Zarr files as a \code{NULL} dataset (instead of not being
written at all). To disable this behaviour, set
\code{option(anndataR.write_null = FALSE)}. This may be required to allow the file
to be read by older versions of Python \strong{anndata}.
}
}
\examples{
adata <- AnnData(
  X = matrix(1:5, 3L, 5L),
  layers = list(
    A = matrix(5:1, 3L, 5L),
    B = matrix(letters[1:5], 3L, 5L)
  ),
  obs = data.frame(row.names = LETTERS[1:3], cell = 1:3),
  var = data.frame(row.names = letters[1:5], gene = 1:5)
)
zarr_store <- tempfile(fileext = ".zarr")
adata$write_zarr(zarr_store)

# Write a SingleCellExperiment as a Zarr store
if (requireNamespace("SingleCellExperiment", quietly = TRUE)) {
  ncells <- 100
  counts <- matrix(rpois(20000, 5), ncol = ncells)
  logcounts <- log2(counts + 1)

  pca <- matrix(runif(ncells * 5), ncells)
  tsne <- matrix(rnorm(ncells * 2), ncells)

  sce <- SingleCellExperiment::SingleCellExperiment(
    assays = list(counts = counts, logcounts = logcounts),
    reducedDims = list(PCA = pca, tSNE = tsne)
  )

  adata <- as_AnnData(sce)
  zarr_store <- tempfile(fileext = ".zarr")
  adata$write_zarr(zarr_store)
}

# Write a Seurat as a Zarr
if (requireNamespace("Seurat", quietly = TRUE)) {
  library(Seurat)

  counts <- matrix(1:15, 5L, 3L)
  dimnames(counts) <- list(
    LETTERS[1:5],
    letters[1:3]
  )
  cell.metadata <- data.frame(
    row.names = letters[1:3],
    cell = 1:3
  )
  obj <- CreateSeuratObject(counts, meta.data = cell.metadata)
  gene.metadata <- data.frame(
    row.names = LETTERS[1:5],
    gene = 1:5
  )
  obj[["RNA"]] <- AddMetaData(GetAssay(obj), gene.metadata)

  adata <- as_AnnData(obj)
  zarr_store <- tempfile(fileext = ".zarr")
  adata$write_zarr(zarr_store)
}
}
