Skip to content

Commit

Permalink
Merge pull request #1030 from josschavezf/suite_dev
Browse files Browse the repository at this point in the history
add function giottoToAnndataZarr
  • Loading branch information
josschavezf authored Sep 19, 2024
2 parents 1b2e080 + c57fedd commit e5334e4
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: Giotto
Title: Spatial Single-Cell Transcriptomics Toolbox
Version: 4.1.2
Version: 4.1.3
Authors@R: c(
person("Ruben", "Dries", email = "[email protected]",
role = c("aut", "cre"), comment = c(ORCID = "0000-0001-7650-7754")),
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export(createGiottoPolygon)
export(createGiottoPolygonsFromDfr)
export(createGiottoPolygonsFromGeoJSON)
export(createGiottoPolygonsFromMask)
export(createGiottoVisiumHDObject)
export(createGiottoVisiumObject)
export(createGiottoXeniumObject)
export(createMerscopeLargeImage)
Expand Down Expand Up @@ -255,6 +256,7 @@ export(giottoPoints)
export(giottoPolygon)
export(giottoSankeyPlan)
export(giottoToAnnData)
export(giottoToAnndataZarr)
export(giottoToSeurat)
export(giottoToSeuratV4)
export(giottoToSeuratV5)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Giotto 4.1.3

## New
* Add `giottoToAnndataZarr()` to create a local anndata zarr folder and interact with the vitessceR package.

# Giotto 4.1.2

Expand Down
123 changes: 123 additions & 0 deletions R/interactivity.R
Original file line number Diff line number Diff line change
Expand Up @@ -754,3 +754,126 @@ plotInteractive3D <- function(

shiny::runGadget(ui, server)
}

#' Create a local anndata zarr folder
#'
#' @param gobject giotto object
#' @param spat_unit spatial unit (e.g. "cell")
#' @param feat_type feature type (e.g. "rna", "dna", "protein")
#' @param expression expression values to extract (e.g. "raw", "normalized", "scaled")
#' @param output_path path to create and save the anndata zarr folder
#'
#' @return local anndata zarr folder
#' @export
#'
#' @examples
#' # using the mini visium object
#' giotto_object <- GiottoData::loadGiottoMini("visium")
#'
#' giottoToAnndataZarr(giotto_object,
#' expression = "raw"
#' output_path = tempdir())
#'
#' # using the mini vizgen object
#' giotto_object <- GiottoData::loadGiottoMini("vizgen")
#'
#' giottoToAnndataZarr(giotto_object,
#' spat_unit = "aggregate",
#' expression = "scaled",
#' output_path = tempdir())
giottoToAnndataZarr <- function(gobject, spat_unit = NULL,
feat_type = NULL, expression = "raw",
output_path) {

proc <- basilisk::basiliskStart(GiottoClass::instructions(gobject = gobject,
param = "python_path"))
on.exit(basilisk::basiliskStop(proc))

success <- basilisk::basiliskRun(
proc,
function(gobject,
output_path,
expression) {

anndata <- reticulate::import("anndata")
zarr <- reticulate::import("zarr")

# extract expression matrix
X <- t(as.matrix(GiottoClass::getExpression(gobject = gobject,
spat_unit = spat_unit,
feat_type = feat_type,
values = expression,
output = "matrix")))

# extract cell metadata
obs <- as.data.frame(GiottoClass::getCellMetadata(gobject = gobject,
spat_unit = spat_unit,
feat_type = feat_type,
output = "data.table"))

rownames(obs) <- obs$cell_ID

# extract feature metadata
var <- as.data.frame(GiottoClass::getFeatureMetadata(gobject = gobject,
spat_unit = spat_unit,
feat_type = feat_type,
output = "data.table"))

obsm <- list()

# extract spatial locations
spatial_locs <- as.data.frame(GiottoClass::getSpatialLocations(gobject = gobject,
spat_unit = spat_unit,
output = "data.table"))

if (!is.null(spatial_locs)) {
rownames(spatial_locs) <- spatial_locs$cell_ID
spatial_locs <- spatial_locs[obs$cell_ID,]
spatial_locs_matrix <- as.matrix(spatial_locs[,1:2])

obsm[["spatial"]] <- spatial_locs_matrix
}

# extract pca
dim_reducs_pca <- GiottoClass::getDimReduction(gobject = gobject,
spat_unit = spat_unit,
feat_type = feat_type,
reduction_method = "pca",
output = "matrix")

if(!is.null(dim_reducs_pca)) {
obsm[["pca"]] <- dim_reducs_pca[obs$cell_ID,]
}

# extract umap
dim_reducs_umap <- GiottoClass::getDimReduction(gobject = gobject,
spat_unit = spat_unit,
feat_type = feat_type,
reduction_method = "umap",
name = "umap",
output = "matrix")

if(!is.null(dim_reducs_umap)) {
obsm[["umap"]] <- dim_reducs_umap[obs$cell_ID,]
}

# extract tSNE
dim_reducs_tsne <- GiottoClass::getDimReduction(gobject = gobject,
spat_unit = spat_unit,
feat_type = feat_type,
reduction_method = "tsne",
name = "tsne",
output = "matrix")

if(!is.null(dim_reducs_tsne)) {
obsm[["tsne"]] <- dim_reducs_tsne[obs$cell_ID,]
}

adata <- anndata$AnnData(X = X, obs = obs, var = var, obsm = obsm)

adata$write_zarr(output_path)
return(TRUE)
}, gobject = gobject, output_path = output_path, expression = expression)

return(success)
}
44 changes: 44 additions & 0 deletions man/createGiottoVisiumHDObject.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions man/giottoToAnndataZarr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/importVisiumHD.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e5334e4

Please sign in to comment.