From 83dd41f475d6b7f678d4d9baf21902a41c6aad31 Mon Sep 17 00:00:00 2001 From: Mark Keller <7525285+keller-mark@users.noreply.github.com> Date: Tue, 18 Jun 2024 12:43:43 -0400 Subject: [PATCH 1/9] WIP: use pizzarr and anndataR --- R/data_to_zarr.R | 53 +++--------------------------------------------- 1 file changed, 3 insertions(+), 50 deletions(-) diff --git a/R/data_to_zarr.R b/R/data_to_zarr.R index 8fa9a3c..dff87ac 100644 --- a/R/data_to_zarr.R +++ b/R/data_to_zarr.R @@ -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. From a341cf8edb2ff5d037dd5422031f3e9e597187c2 Mon Sep 17 00:00:00 2001 From: Mark Keller <7525285+keller-mark@users.noreply.github.com> Date: Thu, 20 Jun 2024 10:56:39 -0400 Subject: [PATCH 2/9] WIP: working --- R/data_to_zarr.R | 17 +++++++++++++++-- R/mock_objects.R | 7 +++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/R/data_to_zarr.R b/R/data_to_zarr.R index dff87ac..ace0ac4 100644 --- a/R/data_to_zarr.R +++ b/R/data_to_zarr.R @@ -9,13 +9,26 @@ #' @export #' @examples #' 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) { +#' seurat_to_anndata_zarr(obj, out_path = "data/seurat.zarr") +seurat_to_anndata_zarr <- function(seurat_obj, out_path) { sce <- Seurat::as.SingleCellExperiment(seurat_obj) store <- pizzarr::DirectoryStore$new(out_path) anndataR::from_SingleCellExperiment(sce, "ZarrAnnData", store = store) } +test_conversion <- function(num_workers = 8) { + options( + pizzarr.parallel_read_enabled = TRUE, + pizzarr.parallel_write_enabled = TRUE + ) + doParallel::registerDoParallel(num_workers) + + obj <- get_seurat_obj() + seurat_to_anndata_zarr(obj, "test.zarr") + + doParallel::stopImplicitCluster() +} + #' Save a SingleCellExperiment to an AnnData-Zarr store. #' #' @param sce_obj The object to save. diff --git a/R/mock_objects.R b/R/mock_objects.R index 866435c..3c98185 100644 --- a/R/mock_objects.R +++ b/R/mock_objects.R @@ -1,3 +1,6 @@ +library(Seurat) +library(SeuratObject) + #' Create a mock Seurat object for tests and examples. #' @return The object. #' @keywords internal @@ -9,6 +12,10 @@ get_seurat_obj <- function() { if(!requireNamespace("Seurat", quietly = TRUE)) { stop("Install 'Seurat' to enable creation of Seurat objects.") } + + SeuratData::InstallData("pbmcsca") + obj <- SeuratData::LoadData("pbmcsca") + return(obj) ncells <- 100 u <- matrix(rpois(20000, 5), ncol=ncells) From 722e7db71195cfafaca7f81e29ba352cd2e199f0 Mon Sep 17 00:00:00 2001 From: Mark Keller <7525285+keller-mark@users.noreply.github.com> Date: Thu, 20 Jun 2024 12:26:04 -0400 Subject: [PATCH 3/9] Refactoring --- DESCRIPTION | 2 +- NAMESPACE | 3 + R/{data_to_zarr.R => bioc_to_zarr.R} | 116 +-------------------------- R/giotto_to_bioc.R | 3 + R/mock_objects.R | 19 +++-- R/seurat_to_bioc.R | 62 ++++++++++++++ 6 files changed, 86 insertions(+), 119 deletions(-) rename R/{data_to_zarr.R => bioc_to_zarr.R} (52%) create mode 100644 R/giotto_to_bioc.R create mode 100644 R/seurat_to_bioc.R diff --git a/DESCRIPTION b/DESCRIPTION index 279374c..53eb6d6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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, diff --git a/NAMESPACE b/NAMESPACE index 6d02c14..2a43186 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ 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(obj_list) @@ -10,6 +11,8 @@ export(sce_to_anndata_zarr) export(seurat_to_anndata_zarr) export(spe_to_anndata_zarr) export(spe_to_ome_zarr) +import(Seurat) +import(SeuratObject) importFrom(SingleCellExperiment,"reducedDims<-") importFrom(SingleCellExperiment,int_colData) importFrom(SingleCellExperiment,reducedDims) diff --git a/R/data_to_zarr.R b/R/bioc_to_zarr.R similarity index 52% rename from R/data_to_zarr.R rename to R/bioc_to_zarr.R index ace0ac4..157dd2e 100644 --- a/R/data_to_zarr.R +++ b/R/bioc_to_zarr.R @@ -1,34 +1,3 @@ - -#' Save a Seurat object to an AnnData-Zarr store. -#' -#' @param seurat_obj The object to save. -#' @param out_path A path to the output Zarr store. -#' @param assay The name of the assay to save. -#' @return TRUE if the conversion succeeds. -#' -#' @export -#' @examples -#' obj <- get_seurat_obj() -#' seurat_to_anndata_zarr(obj, out_path = "data/seurat.zarr") -seurat_to_anndata_zarr <- function(seurat_obj, out_path) { - sce <- Seurat::as.SingleCellExperiment(seurat_obj) - store <- pizzarr::DirectoryStore$new(out_path) - anndataR::from_SingleCellExperiment(sce, "ZarrAnnData", store = store) -} - -test_conversion <- function(num_workers = 8) { - options( - pizzarr.parallel_read_enabled = TRUE, - pizzarr.parallel_write_enabled = TRUE - ) - doParallel::registerDoParallel(num_workers) - - obj <- get_seurat_obj() - seurat_to_anndata_zarr(obj, "test.zarr") - - doParallel::stopImplicitCluster() -} - #' Save a SingleCellExperiment to an AnnData-Zarr store. #' #' @param sce_obj The object to save. @@ -48,20 +17,10 @@ sce_to_anndata_zarr <- function(sce_obj, out_path) { # Reference: https://github.com/theislab/zellkonverter/blob/e1e95b1/R/SCE2AnnData.R#L159 colnames(reducedDims(sce_obj)[[obsm_key]]) <- NULL } - - # Use basilisk - proc <- basilisk::basiliskStart(py_env) - on.exit(basilisk::basiliskStop(proc)) - - success <- basilisk::basiliskRun(proc, function(sce_obj, out_path) { - anndata <- reticulate::import("anndata") - zarr <- reticulate::import("zarr") - - adata <- zellkonverter::SCE2AnnData(sce_obj) - adata$write_zarr(out_path) - return(TRUE) - }, sce_obj = sce_obj, out_path = out_path) - return(success) + + store <- pizzarr::DirectoryStore$new(out_path) + anndataR::from_SingleCellExperiment(sce_obj, "ZarrAnnData", store = store) + return(TRUE) } #' Save a SpatialExperiment to an AnnData-Zarr store. @@ -166,70 +125,3 @@ spe_to_ome_zarr <- function(spe_obj, sample_id, image_id, out_path) { }, img_arr = img_arr, sample_id = sample_id, image_id = image_id, out_path = out_path) return(success) } - -#' Save a Giotto object to an AnnData-Zarr store -#' -#' @param giotto_obj The object to save. -#' @param out_path A path to the output Zarr store. -#' @param X_slot The name of the slot in the Giotto object to use for adata.X -#' @return TRUE if the conversion succeeds. -#' -#' @export -#' @examples -#' obj <- get_giotto_obj() -#' giotto_to_anndata_zarr(obj, "data/giotto.zarr") -#' @importFrom methods slot -giotto_to_anndata_zarr <- function(giotto_obj, out_path, X_slot = "raw_exprs") { - - # Use basilisk - proc <- basilisk::basiliskStart(py_env) - on.exit(basilisk::basiliskStop(proc)) - - success <- basilisk::basiliskRun(proc, function(giotto_obj, out_path, X_slot) { - anndata <- reticulate::import("anndata") - zarr <- reticulate::import("zarr") - - # Reference: https://github.com/theislab/zellkonverter/blob/master/R/SCE2AnnData.R#L237 - make_numpy_friendly <- function(x, transpose = TRUE) { - if (transpose) { - x <- Matrix::t(x) - } - if (DelayedArray::is_sparse(x)) { - methods::as(x, "dgCMatrix") - } else { - as.matrix(x) - } - } - - X <- make_numpy_friendly(slot(giotto_obj, X_slot)) - obs <- slot(giotto_obj, "cell_metadata") - var <- slot(giotto_obj, "gene_metadata") - - adata <- anndata$AnnData(X = X, obs = obs, var = var) - - obsm <- list() - - if(!is.null(slot(giotto_obj, "spatial_locs"))) { - spatial_locs <- slot(giotto_obj, "spatial_locs") - obsm[['spatial']] <- t(as.matrix(spatial_locs[, c("sdimx", "sdimy")])) - } - - if(!is.null(slot(giotto_obj, "dimension_reduction"))) { - dim_reducs <- slot(giotto_obj, "dimension_reduction")$cells - for(dim_reduc_name in names(dim_reducs)) { - dim_reduc_coords <- dim_reducs[[dim_reduc_name]][[dim_reduc_name]]$coordinates - obsm[[dim_reduc_name]] <- t(as.matrix(dim_reduc_coords)) - } - } - - if(length(obsm) > 0) { - # TODO make_numpy_friendly is outside scope - obsm <- lapply(obsm, make_numpy_friendly) - adata$obsm <- obsm - } - - adata$write_zarr(out_path) - return(TRUE) - }, giotto_obj = giotto_obj, out_path = out_path, X_slot = X_slot) - return(success) -} diff --git a/R/giotto_to_bioc.R b/R/giotto_to_bioc.R new file mode 100644 index 0000000..6a3dc69 --- /dev/null +++ b/R/giotto_to_bioc.R @@ -0,0 +1,3 @@ +giotto_to_spe <- function(gio) { + return(GiottoClass::giottoToSpatialExperiment(gio)) +} \ No newline at end of file diff --git a/R/mock_objects.R b/R/mock_objects.R index 3c98185..7d33b7a 100644 --- a/R/mock_objects.R +++ b/R/mock_objects.R @@ -1,5 +1,16 @@ -library(Seurat) -library(SeuratObject) +#' Get a Seurat v5 object for tests and examples. +#' @return The object. +#' @keywords internal +#' @export +#' @examples +#' obj <- get_seurat_v5_obj() +#' @import Seurat +#' @import SeuratObject +get_seurat_v5_obj <- function() { + SeuratData::InstallData("pbmcsca") + obj <- SeuratData::LoadData("pbmcsca") + return(obj) +} #' Create a mock Seurat object for tests and examples. #' @return The object. @@ -12,10 +23,6 @@ get_seurat_obj <- function() { if(!requireNamespace("Seurat", quietly = TRUE)) { stop("Install 'Seurat' to enable creation of Seurat objects.") } - - SeuratData::InstallData("pbmcsca") - obj <- SeuratData::LoadData("pbmcsca") - return(obj) ncells <- 100 u <- matrix(rpois(20000, 5), ncol=ncells) diff --git a/R/seurat_to_bioc.R b/R/seurat_to_bioc.R new file mode 100644 index 0000000..b87cdff --- /dev/null +++ b/R/seurat_to_bioc.R @@ -0,0 +1,62 @@ +seurat_to_sce <- function(seu) { + sce <- Seurat::as.SingleCellExperiment(seu) + return(sce) +} + +seurat_to_spe <- function(seu, sample_id = NA, img_id = NA) { + # Reference: https://github.com/drighelli/SpatialExperiment/issues/115 + sce <- seurat_to_sce(seu) + + if(is.na(sample_id)) { + sample_id <- "sample0" + } + + if(length(object@images) >= 1) { + + if(is.na(img_id)) { + img_id <- names(object@images)[1] + } + + ## Extract spatial coordinates + if("VisiumV2" %in% class(seu@images[[img_id]])) { + spatialCoords <- as.matrix( + seu@images[[img_id]]@boundaries$centroids@coords + ) + } else { + spatialCoords <- as.matrix( + seu@images[[img_id]]@coordinates[, c("imagecol", "imagerow")] + ) + } + + + ## Extract and process image data + img <- SpatialExperiment::SpatialImage( + x = as.raster(seu@images[[img_id]]@image) + ) + + imgData <- S4Vectors::DataFrame( + sample_id = sample_id, + image_id = img_id, + data = I(list(img)), + scaleFactor = seu@images[[img_id]]@scale.factors$lowres + ) + } + + # Convert to SpatialExperiment + spe <- SpatialExperiment::SpatialExperiment( + assays = SummarizedExperiment::assays(sce), + rowData = SingleCellExperiment::rowData(sce), + colData = SingleCellExperiment::colData(sce), + metadata = S4Vectors::metadata(sce), + reducedDims = SingleCellExperiment::reducedDims(sce), + altExps = SingleCellExperiment::altExps(sce), + sample_id = sample_id, + spatialCoords = spatialCoords, + imgData = imgData + ) + # indicate all spots are on the tissue + spe$in_tissue <- 1 + spe$sample_id <- sample_id + # Return Spatial Experiment object + return(spe) +} \ No newline at end of file From 299b754562458563ef5887924adfc6d61f37c4f3 Mon Sep 17 00:00:00 2001 From: Mark Keller <7525285+keller-mark@users.noreply.github.com> Date: Thu, 20 Jun 2024 12:36:15 -0400 Subject: [PATCH 4/9] Update --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index a8d0986..5e4b6ef 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ Installation requires R 4.0.0 or greater. ```r install.packages("devtools") +devtools::install_github("keller-mark/pizzarr") +devtools::install_github("keller-mark/anndataR", ref="keller-mark/zarr-and-rarr") + devtools::install_github("vitessce/vitessceAnalysisR") ``` From 7c7ac878f69e3e39df22b2d7566bf2715db256f5 Mon Sep 17 00:00:00 2001 From: Mark Keller <7525285+keller-mark@users.noreply.github.com> Date: Thu, 20 Jun 2024 12:48:49 -0400 Subject: [PATCH 5/9] Update exports --- NAMESPACE | 3 --- man/get_seurat_v5_obj.Rd | 18 ++++++++++++++++++ man/giotto_to_anndata_zarr.Rd | 25 ------------------------- man/sce_to_anndata_zarr.Rd | 2 +- man/seurat_to_anndata_zarr.Rd | 25 ------------------------- man/spe_to_anndata_zarr.Rd | 2 +- man/spe_to_ome_zarr.Rd | 2 +- 7 files changed, 21 insertions(+), 56 deletions(-) create mode 100644 man/get_seurat_v5_obj.Rd delete mode 100644 man/giotto_to_anndata_zarr.Rd delete mode 100644 man/seurat_to_anndata_zarr.Rd diff --git a/NAMESPACE b/NAMESPACE index 2a43186..72cd43e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,10 +5,8 @@ export(get_sce_obj) export(get_seurat_obj) export(get_seurat_v5_obj) export(get_spe_obj) -export(giotto_to_anndata_zarr) export(obj_list) export(sce_to_anndata_zarr) -export(seurat_to_anndata_zarr) export(spe_to_anndata_zarr) export(spe_to_ome_zarr) import(Seurat) @@ -22,7 +20,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) diff --git a/man/get_seurat_v5_obj.Rd b/man/get_seurat_v5_obj.Rd new file mode 100644 index 0000000..9b1e337 --- /dev/null +++ b/man/get_seurat_v5_obj.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/mock_objects.R +\name{get_seurat_v5_obj} +\alias{get_seurat_v5_obj} +\title{Get a Seurat v5 object for tests and examples.} +\usage{ +get_seurat_v5_obj() +} +\value{ +The object. +} +\description{ +Get a Seurat v5 object for tests and examples. +} +\examples{ +obj <- get_seurat_v5_obj() +} +\keyword{internal} diff --git a/man/giotto_to_anndata_zarr.Rd b/man/giotto_to_anndata_zarr.Rd deleted file mode 100644 index 9f4a786..0000000 --- a/man/giotto_to_anndata_zarr.Rd +++ /dev/null @@ -1,25 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data_to_zarr.R -\name{giotto_to_anndata_zarr} -\alias{giotto_to_anndata_zarr} -\title{Save a Giotto object to an AnnData-Zarr store} -\usage{ -giotto_to_anndata_zarr(giotto_obj, out_path, X_slot = "raw_exprs") -} -\arguments{ -\item{giotto_obj}{The object to save.} - -\item{out_path}{A path to the output Zarr store.} - -\item{X_slot}{The name of the slot in the Giotto object to use for adata.X} -} -\value{ -TRUE if the conversion succeeds. -} -\description{ -Save a Giotto object to an AnnData-Zarr store -} -\examples{ -obj <- get_giotto_obj() -giotto_to_anndata_zarr(obj, "data/giotto.zarr") -} diff --git a/man/sce_to_anndata_zarr.Rd b/man/sce_to_anndata_zarr.Rd index 372681b..efe07b1 100644 --- a/man/sce_to_anndata_zarr.Rd +++ b/man/sce_to_anndata_zarr.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data_to_zarr.R +% Please edit documentation in R/bioc_to_zarr.R \name{sce_to_anndata_zarr} \alias{sce_to_anndata_zarr} \title{Save a SingleCellExperiment to an AnnData-Zarr store.} diff --git a/man/seurat_to_anndata_zarr.Rd b/man/seurat_to_anndata_zarr.Rd deleted file mode 100644 index 76633ff..0000000 --- a/man/seurat_to_anndata_zarr.Rd +++ /dev/null @@ -1,25 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data_to_zarr.R -\name{seurat_to_anndata_zarr} -\alias{seurat_to_anndata_zarr} -\title{Save a Seurat object to an AnnData-Zarr store.} -\usage{ -seurat_to_anndata_zarr(seurat_obj, out_path, assay) -} -\arguments{ -\item{seurat_obj}{The object to save.} - -\item{out_path}{A path to the output Zarr store.} - -\item{assay}{The name of the assay to save.} -} -\value{ -TRUE if the conversion succeeds. -} -\description{ -Save a Seurat object to an AnnData-Zarr store. -} -\examples{ -obj <- get_seurat_obj() -seurat_to_anndata_zarr(obj, out_path = "data/seurat.zarr", assay = "RNA") -} diff --git a/man/spe_to_anndata_zarr.Rd b/man/spe_to_anndata_zarr.Rd index 59596d8..0d340ef 100644 --- a/man/spe_to_anndata_zarr.Rd +++ b/man/spe_to_anndata_zarr.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data_to_zarr.R +% Please edit documentation in R/bioc_to_zarr.R \name{spe_to_anndata_zarr} \alias{spe_to_anndata_zarr} \title{Save a SpatialExperiment to an AnnData-Zarr store.} diff --git a/man/spe_to_ome_zarr.Rd b/man/spe_to_ome_zarr.Rd index bdbe5cb..d8cde20 100644 --- a/man/spe_to_ome_zarr.Rd +++ b/man/spe_to_ome_zarr.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data_to_zarr.R +% Please edit documentation in R/bioc_to_zarr.R \name{spe_to_ome_zarr} \alias{spe_to_ome_zarr} \title{Save an image in a SpatialExperiment to an OME-Zarr store} From 9a1521e6b7c8e970c99a2f398e26f9c41d111b13 Mon Sep 17 00:00:00 2001 From: Mark Keller <7525285+keller-mark@users.noreply.github.com> Date: Thu, 20 Jun 2024 12:56:05 -0400 Subject: [PATCH 6/9] Update --- NAMESPACE | 3 +++ R/giotto_to_bioc.R | 1 + R/seurat_to_bioc.R | 2 ++ 3 files changed, 6 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 72cd43e..6bb8769 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,8 +5,11 @@ export(get_sce_obj) export(get_seurat_obj) export(get_seurat_v5_obj) export(get_spe_obj) +export(giotto_to_spe) export(obj_list) export(sce_to_anndata_zarr) +export(seurat_to_sce) +export(seurat_to_spe) export(spe_to_anndata_zarr) export(spe_to_ome_zarr) import(Seurat) diff --git a/R/giotto_to_bioc.R b/R/giotto_to_bioc.R index 6a3dc69..9be701a 100644 --- a/R/giotto_to_bioc.R +++ b/R/giotto_to_bioc.R @@ -1,3 +1,4 @@ +#' @export giotto_to_spe <- function(gio) { return(GiottoClass::giottoToSpatialExperiment(gio)) } \ No newline at end of file diff --git a/R/seurat_to_bioc.R b/R/seurat_to_bioc.R index b87cdff..8e0b7e5 100644 --- a/R/seurat_to_bioc.R +++ b/R/seurat_to_bioc.R @@ -1,8 +1,10 @@ +#' @export seurat_to_sce <- function(seu) { sce <- Seurat::as.SingleCellExperiment(seu) return(sce) } +#' @export seurat_to_spe <- function(seu, sample_id = NA, img_id = NA) { # Reference: https://github.com/drighelli/SpatialExperiment/issues/115 sce <- seurat_to_sce(seu) From ca6757bc1d0905a128c273b6414a466adf65d7d3 Mon Sep 17 00:00:00 2001 From: Mark Keller <7525285+keller-mark@users.noreply.github.com> Date: Thu, 20 Jun 2024 12:57:57 -0400 Subject: [PATCH 7/9] Update --- R/seurat_to_bioc.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/seurat_to_bioc.R b/R/seurat_to_bioc.R index 8e0b7e5..27e034c 100644 --- a/R/seurat_to_bioc.R +++ b/R/seurat_to_bioc.R @@ -13,10 +13,10 @@ seurat_to_spe <- function(seu, sample_id = NA, img_id = NA) { sample_id <- "sample0" } - if(length(object@images) >= 1) { + if(length(seu@images) >= 1) { if(is.na(img_id)) { - img_id <- names(object@images)[1] + img_id <- names(seu@images)[1] } ## Extract spatial coordinates From ed889a0bac8e6ffc293db11b8ae76468369ffd40 Mon Sep 17 00:00:00 2001 From: Mark Keller <7525285+keller-mark@users.noreply.github.com> Date: Thu, 20 Jun 2024 14:46:42 -0400 Subject: [PATCH 8/9] to_dense --- R/bioc_to_zarr.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/bioc_to_zarr.R b/R/bioc_to_zarr.R index 157dd2e..9c08ee9 100644 --- a/R/bioc_to_zarr.R +++ b/R/bioc_to_zarr.R @@ -19,7 +19,7 @@ sce_to_anndata_zarr <- function(sce_obj, out_path) { } store <- pizzarr::DirectoryStore$new(out_path) - anndataR::from_SingleCellExperiment(sce_obj, "ZarrAnnData", store = store) + anndataR::from_SingleCellExperiment(sce_obj, "ZarrAnnData", store = store, to_dense = TRUE) return(TRUE) } From 3000b4d42516097a57357f825dbd203dd4d3b7bf Mon Sep 17 00:00:00 2001 From: Mark Keller <7525285+keller-mark@users.noreply.github.com> Date: Thu, 20 Jun 2024 14:46:49 -0400 Subject: [PATCH 9/9] Update param --- R/bioc_to_zarr.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/bioc_to_zarr.R b/R/bioc_to_zarr.R index 9c08ee9..9b271e2 100644 --- a/R/bioc_to_zarr.R +++ b/R/bioc_to_zarr.R @@ -9,7 +9,7 @@ #' 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) { +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, @@ -19,7 +19,7 @@ sce_to_anndata_zarr <- function(sce_obj, out_path) { } store <- pizzarr::DirectoryStore$new(out_path) - anndataR::from_SingleCellExperiment(sce_obj, "ZarrAnnData", store = store, to_dense = TRUE) + anndataR::from_SingleCellExperiment(sce_obj, "ZarrAnnData", store = store, to_dense = to_dense) return(TRUE) }