Skip to content

Commit

Permalink
WIP: use pizzarr and anndataR
Browse files Browse the repository at this point in the history
  • Loading branch information
keller-mark committed Jun 18, 2024
1 parent a8511e6 commit 83dd41f
Showing 1 changed file with 3 additions and 50 deletions.
53 changes: 3 additions & 50 deletions R/data_to_zarr.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,9 @@
#' obj <- get_seurat_obj()
#' seurat_to_anndata_zarr(obj, out_path = "data/seurat.zarr", assay = "RNA")
seurat_to_anndata_zarr <- function(seurat_obj, out_path, assay) {
if(!requireNamespace("SeuratDisk", quietly = TRUE)) {
stop("Install 'SeuratDisk' to enable conversion of Seurat objects to AnnData objects.")
}

h5seurat_path <- paste0(out_path, ".h5Seurat")
h5ad_path <- paste0(out_path, ".h5ad")

# Convert factor columns to string/numeric.
seurat_obj@meta.data <- varhandle::unfactor(seurat_obj@meta.data)

SeuratDisk::SaveH5Seurat(seurat_obj, filename = h5seurat_path, overwrite = TRUE)
SeuratDisk::Convert(h5seurat_path, dest = "h5ad", overwrite = TRUE, assay = assay)

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

success <- basilisk::basiliskRun(proc, function(h5ad_path, out_path) {
anndata <- reticulate::import("anndata")
zarr <- reticulate::import("zarr")

adata <- anndata$read_h5ad(h5ad_path)

cleanup_colnames <- function(df) {
# Reference: https://github.com/theislab/scvelo/issues/255#issuecomment-739995301
new_colnames <- colnames(df)
new_colnames[new_colnames == "_index"] <- "features"
return(new_colnames)
}

noop <- function(cond) { }

tryCatch({
colnames(adata$var) <- cleanup_colnames(adata$var)
}, error = noop)

# Reconstruct, omitting raw and uns.
adata <- anndata$AnnData(
X = adata$X,
obs = as.data.frame(adata$obs),
var = as.data.frame(adata$var),
obsm = adata$obsm,
varm = adata$varm
)

adata$write_zarr(out_path)

return(TRUE)
}, h5ad_path = h5ad_path, out_path = out_path)
return(success)
sce <- Seurat::as.SingleCellExperiment(seurat_obj)
store <- pizzarr::DirectoryStore$new(out_path)
anndataR::from_SingleCellExperiment(sce, "ZarrAnnData", store = store)
}

#' Save a SingleCellExperiment to an AnnData-Zarr store.
Expand Down

0 comments on commit 83dd41f

Please sign in to comment.