Skip to content

Use Pizzarr for Zarr; More usage of Bioconductor classes #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LazyData: false
Language: en-US
StagedInstall: no
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
VignetteBuilder: knitr
Imports:
Matrix,
Expand Down
9 changes: 6 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
export(get_giotto_obj)
export(get_sce_obj)
export(get_seurat_obj)
export(get_seurat_v5_obj)
export(get_spe_obj)
export(giotto_to_anndata_zarr)
export(giotto_to_spe)
export(obj_list)
export(sce_to_anndata_zarr)
export(seurat_to_anndata_zarr)
export(seurat_to_sce)
export(seurat_to_spe)
export(spe_to_anndata_zarr)
export(spe_to_ome_zarr)
import(Seurat)
import(SeuratObject)
importFrom(SingleCellExperiment,"reducedDims<-")
importFrom(SingleCellExperiment,int_colData)
importFrom(SingleCellExperiment,reducedDims)
Expand All @@ -19,7 +23,6 @@ importFrom(SummarizedExperiment,colData)
importFrom(grDevices,as.raster)
importFrom(grDevices,col2rgb)
importFrom(methods,new)
importFrom(methods,slot)
importFrom(stats,rnorm)
importFrom(stats,rpois)
importFrom(stats,runif)
127 changes: 127 additions & 0 deletions R/bioc_to_zarr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#' Save a SingleCellExperiment to an AnnData-Zarr store.
#'
#' @param sce_obj The object to save.
#' @param out_path A path to the output Zarr store.
#' @return TRUE if the conversion succeeds.
#'
#' @export
#' @examples
#' obj <- get_sce_obj()
#' sce_to_anndata_zarr(obj, out_path = "data/sce.zarr")
#' @importFrom SingleCellExperiment reducedDims reducedDims<-
sce_to_anndata_zarr <- function(sce_obj, out_path, to_dense = TRUE) {
obsm_keys <- names(as.list(reducedDims(sce_obj)))
for(obsm_key in obsm_keys) {
# If there are column names, then the obsm element will be stored as a data.frame,
# but Vitessce can only handle array obsm, so we need to remove any column names.
# Reference: https://github.com/theislab/zellkonverter/blob/e1e95b1/R/SCE2AnnData.R#L159
colnames(reducedDims(sce_obj)[[obsm_key]]) <- NULL
}

store <- pizzarr::DirectoryStore$new(out_path)
anndataR::from_SingleCellExperiment(sce_obj, "ZarrAnnData", store = store, to_dense = to_dense)
return(TRUE)
}

#' Save a SpatialExperiment to an AnnData-Zarr store.
#'
#' @param spe_obj The object to save.
#' @param out_path A path to the output Zarr store.
#' @return TRUE if the conversion succeeds.
#'
#' @export
#' @importFrom SummarizedExperiment colData
#' @importFrom SingleCellExperiment int_colData
#' @importFrom SpatialExperiment colData<-
spe_to_anndata_zarr <- function(spe_obj, out_path) {
internal_col_data <- int_colData(spe_obj)

colData(spe_obj) <- cbind(
colData(spe_obj),
internal_col_data$spatialCoords,
# spatialData deprecated in 1.5.2
# internal_col_data$spatialData,
internal_col_data$reducedDims
)

success <- sce_to_anndata_zarr(spe_obj, out_path)
return(success)
}

#' Save an image in a SpatialExperiment to an OME-Zarr store
#'
#' @param spe_obj The object containing the image.
#' @param sample_id The sample_id in the imgData data frame.
#' @param image_id The image_id in the imgData data frame.
#' @param out_path A path to the output Zarr store.
#' @return TRUE if the conversion succeeds.
#'
#' @export
#' @examples
#' obj <- get_spe_obj()
#' spe_to_ome_zarr(obj, "sample1", "image1", "data/spe_image.zarr")
#' @importFrom SpatialExperiment getImg
#' @importFrom grDevices as.raster col2rgb
spe_to_ome_zarr <- function(spe_obj, sample_id, image_id, out_path) {
img_arr <- apply(as.matrix(as.raster(getImg(spe_obj, image_id = image_id, sample_id = sample_id))), c(1, 2), col2rgb)

# Use basilisk
proc <- basilisk::basiliskStart(py_env)
on.exit(basilisk::basiliskStop(proc))

success <- basilisk::basiliskRun(proc, function(img_arr, sample_id, image_id, out_path) {
zarr <- reticulate::import("zarr")
ome_zarr <- reticulate::import("ome_zarr")

z_root <- zarr$open_group(out_path, mode = "w")

# Need to copy this here since can't refer to functions in the outside environment.
obj_list <- function(...) {
retval <- stats::setNames(list(), character(0))
param_list <- list(...)
for(key in names(param_list)) {
retval[[key]] = param_list[[key]]
}
retval
}

default_window <- obj_list(
start = 0,
min = 0,
max = 255,
end = 255
)

ome_zarr$writer$write_image(
image = img_arr,
group = z_root,
axes = "cyx",
omero = obj_list(
name = image_id,
version = "0.3",
rdefs = obj_list(

),
channels = list(
obj_list(
label = "r",
color = "FF0000",
window = default_window
),
obj_list(
label = "g",
color = "00FF00",
window = default_window
),
obj_list(
label = "b",
color = "0000FF",
window = default_window
)
)
)
)
return(TRUE)
}, img_arr = img_arr, sample_id = sample_id, image_id = image_id, out_path = out_path)
return(success)
}
Loading
Loading