## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## -----------------------------------------------------------------------------
library(lstar)

cells <- paste0("c", 1:6); genes <- paste0("g", 1:4)
m <- as(matrix(as.numeric(1:24), 6, 4, dimnames = list(cells, genes)), "CsparseMatrix")  # cells x genes

ds <- list(
  kind = "sample",
  axes = list(
    cells = list(labels = cells, origin = "observed", role = "observation"),
    genes = list(labels = genes, origin = "observed", role = "feature")),
  fields = list(
    counts = list(role = "measure", span = c("cells", "genes"), state = "raw", values = m),
    cluster = list(role = "label", span = "cells", values = factor(c("a", "a", "b", "b", "a", "b")))))
class(ds) <- "lstar_dataset"

p <- tempfile(fileext = ".lstar.zarr")
lstar_write(ds, p)            # -> a portable Zarr store (also readable from Python and C++)
ds2 <- lstar_read(p)
ds2

## ----eval = FALSE-------------------------------------------------------------
# so  <- write_seurat(ds)          # L* dataset  -> Seurat object
# ds3 <- read_seurat(so)           # Seurat       -> L* dataset
# sce <- write_sce(read_seurat(so))   # Seurat -> SingleCellExperiment, in one line

## ----eval = FALSE-------------------------------------------------------------
# # Python:  lstar.write(read_anndata(ad.read_h5ad("pbmc.h5ad")), "pbmc.lstar.zarr")
# ds_from_h5ad <- lstar_read("pbmc.lstar.zarr")
# saveRDS(write_seurat(ds_from_h5ad), "pbmc.rds")

