## ----setup, include=FALSE-----------------------------------------------------
library(BiocStyle)

## ----install, eval=FALSE------------------------------------------------------
# if (!require("BiocManager", quietly=TRUE))
#     install.packages("BiocManager")
# BiocManager::install("ZarrArray")

## ----load, message=FALSE------------------------------------------------------
library(ZarrArray)

## ----ZarrArray_construction---------------------------------------------------
zarr_path <- system.file(package="Rarr", "extdata",
                         "zarr_examples", "column-first", "int32.zarr")
A <- ZarrArray(zarr_path)
A

## ----check_ZarrArray_class----------------------------------------------------
class(A)

is(A, "DelayedArray")

## ----ZarrArray_basic_ops------------------------------------------------------
dim(A)
length(A)
A[1:5, , 1]
aperm(A)
max(A)
sum(A)
A - 0.5
A^3
A == 0L
sqrt(A)

## ----ZarrArray_other_ops------------------------------------------------------
path(A)
type(A)
chunkdim(A)
a <- as.array(A)

## ----writeZarrArray_1---------------------------------------------------------
path1 <- tempfile(fileext=".zarr")
writeZarrArray(A, path1, chunkdim=c(3, 5, 2))

## ----writeZarrArray_2---------------------------------------------------------
path2 <- tempfile(fileext=".zarr")
A2 <- sqrt(t(A[ , , 1]) + 1)  # all these operations are delayed

## This will realize the delayed operations carried by 'A2' block by block:
writeZarrArray(A2, path2)

## ----install_and_load_anndataR, message=FALSE---------------------------------
if (!require("anndataR", quietly=TRUE))
    BiocManager::install("anndataR")

library(anndataR)
anndataR_extdata <- system.file(package="anndataR", "extdata")
list.files(anndataR_extdata)  # 3 files

## ----unzip_example_v3.zarr.zip------------------------------------------------
zad_basename <- "example_v3.zarr"  # zad: Zarr-based AnnData
zad_zip <- paste0(zad_basename, ".zip")
zad_zip_path <- file.path(anndataR_extdata, zad_zip)
exdir <- tempdir()
unzip(zad_zip_path, exdir=exdir)
zad_store <- file.path(exdir, zad_basename)

## ----ZarrSparseMatrix_construction--------------------------------------------
ZSM <- ZarrSparseMatrix(zad_store, "/X")
ZSM

## ----check_ZarrSparseMatrix_class---------------------------------------------
class(ZSM)

is(ZSM, "DelayedMatrix")

## The DelayedMatrix class extends the DelayedArray class:
is(ZSM, "DelayedArray")

## ----ZarrSparseMatrix_basic_ops-----------------------------------------------
dim(ZSM)
length(ZSM)

ZSM[1:5, 1:6]
ZSM[1:5 , 50]
t(ZSM)

max(ZSM)
sum(ZSM)

ZSM - 0.5
ZSM^3
ZSM == 0L
sqrt(ZSM)

head(rowSums(ZSM))
head(rowMaxs(ZSM))
head(colVars(ZSM))

## ----ZarrSparseMatrix_other_ops-----------------------------------------------
path(ZSM)
group(ZSM)

type(ZSM)
chunkdim(ZSM)
is_sparse(ZSM)
zsm <- as.matrix(ZSM)

## ----writeZarrSparseMatrix_1--------------------------------------------------
m0 <- matrix(0L, nrow=25, ncol=12,
             dimnames=list(letters[1:25], LETTERS[1:12]))
m0[cbind(2:24, c(12:1, 2:12))] <- 100L + sample(55L, 23, replace=TRUE)

zarr_store <- tempfile(fileext=".zarr")
ZSM0 <- writeZarrSparseMatrix(m0, zarr_store, group="/X0")
ZSM0

## ----path_and_group_of_ZarrSparseMatrix---------------------------------------
path(ZSM0)
group(ZSM0)

## ----writeZarrSparseMatrix_2--------------------------------------------------
library(Matrix)
dgc <- as(m0, "dgCMatrix")
ZSM1 <- writeZarrSparseMatrix(dgc, zarr_store, group="/X1")
ZSM1

path(ZSM1)
group(ZSM1)

## ----writeZarrSparseMatrix_3--------------------------------------------------
SM2 <- log(ZSM1[ , 7:12] + 1)  # all these operations are delayed
is_sparse(SM2)

## This will realize the delayed operations carried by 'SM2' block by block:
ZSM2 <- writeZarrSparseMatrix(SM2, zarr_store, group="/X2")
ZSM2

path(ZSM2)
group(ZSM2)

## ----session_info-------------------------------------------------------------
sessionInfo()

