---
title: "Interoperability with SpatialExperiment and AnnData"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Interoperability with SpatialExperiment and AnnData}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
resource_files:
- figures/logo.svg
- figures/logo-dark.svg
---
```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(cellspecR)
x <- cs_example()
policy <- cs_signal_policy(cs_markers(x))
```
The SpatialExperiment conversion keeps the selected signal as the main assay,
all raw measurements as an alternative experiment, and stores the images,
channels, provenance and policy in metadata.
```{r}
if (requireNamespace("SpatialExperiment", quietly = TRUE)) {
spe <- cs_as_spe(x, policy)
dim(spe)
y <- cs_from_spe(spe)
identical(cs_measurements(x), cs_measurements(y))
} else {
message("Install SpatialExperiment to run this section.")
}
```
AnnData conversion is optional and requires an `anndataR` installation with a
working Python AnnData runtime.
```{r}
if (requireNamespace("anndataR", quietly = TRUE)) {
result <- tryCatch(cs_as_anndata(x, policy), error = function(e) e)
if (inherits(result, "error")) message(conditionMessage(result)) else result
} else {
message("Install anndataR to run this section.")
}
```