1. Prerequisites

None.

2. Introduction

This vignette demonstrates how to convert objects between SPATA2 and Seurat, AnnData, or Giotto. SPATA2 provides the convenient function asSPATA2() to convert objects from the mentioned packages.

3. Seurat

First, an exemplary 10X Visium dataset from the mouse brain is loaded using SeuratData. We are following the Seurat tutorial in the following.

library(Seurat)
library(SeuratData)
library(SPATA2)
library(SPATAData)
library(tidyverse)

options(timeout = 1000)
SeuratData::InstallData("stxBrain", force.reinstall = FALSE)
seurat_object <- SeuratData::LoadData("stxBrain", type = "anterior1")
seurat_object
## An object of class Seurat 
## 31053 features across 2696 samples within 1 assay 
## Active assay: Spatial (31053 features, 0 variable features)
##  1 image present: anterior1

The dataset is normalized using SCT and plotted in Seurat.

seurat_object <- Seurat::SCTransform(seurat_object, assay = "Spatial", verbose = FALSE)
Seurat::SpatialFeaturePlot(seurat_object, features = c("nCount_Spatial")) 

Next, the object is converted to SPATA2 using asSPATA2().

spata2_object <- 
  SPATA2::asSPATA2(
    object = seurat_object,
    sample_name = "mouse_brain",
    image_name = "anterior1", 
    spatial_method = "Visium"
    )

sample_name can be any custom name for the sample, whereas image_name is defined in the Seurat object.
asSPATA2() loads the Seurat default assay (Seurat::DefaultAssay(seurat_object)), in this case "SCT".

Lastly, we plot the converted object in SPATA2.

SPATA2::plotSurface(spata2_object, color_by = c("nCount_Spatial"))

4. AnnData

To demonstrate Scanpy/Squidpy compatibility, we provide a preprocessed 10X Visium dataset from the mouse brain. It was exported as h5ad file from Python using AnnData.write_h5ad().

library(anndata)
library(SPATA2)
library(tidyverse)

# required for anndata R package; can be any python version with anndata installed
reticulate::use_python("D:/Programme/Anaconda/envs/giotto_env/python.exe") 
#adjust `dir`according to your preference
dir = getwd()

curl::curl_download(
  url = "https://www.dropbox.com/s/pfpqyg1ds1d52f3/stab_wound_injury_anndata.h5ad?raw=1",
  destfile = paste0(dir,"mouse_brain_adata.h5ad")
)
# Mouse brain Visium dataset, 219 MB

h5ad files can be loaded into R by using the read_h5ad() function from the anndata R package.

adata_object <- anndata::read_h5ad(filename = paste0(dir,"mouse_brain_adata.h5ad"))

The loaded object contains known AnnData slots, such as $obs and $var:

The object is converted to SPATA2 using asSPATA2().

spata_obj <- SPATA2::asSPATA2(adata_object, sample_name = "visium")

Our example AnnData object does not contain a matrix with scaled counts. Also it does not contain t-SNE coordinates. If no AnnData layers are named, adata.X will be imported as normalized matrix. Custom AnnData layers can be added as either count matrix (count_mtr_name), normalized matrix (normalized_mtr_name), or scaled matrix (normalized_mtr_name) in the spata2 object, for instance as asSPATA2(adata, count_mtr_name = "count_matrix").

5. Giotto

We provide a processed 10X Visium dataset from the mouse brain. The data represents the output of a Giotto Visium tutorial.

Because Giotto image data is based on the magick package and therefore not compatible with saveRDS(), we exported Giotto image files separately from the Giotto object as follows:

# don't run
# magick::image_write(visium_brain@images$image$mg_object, path = paste0(results_folder,"/giotto_mouse_brain_image.png"), format = "png")
# saveRDS(visium_brain@spatial_locs, file = paste0(results_folder,"/giotto_mouse_brain_spatial_locs.rds"))
# visium_brain@images <- NULL
# saveRDS(visium_brain, file = paste0(results_folder,"/giotto_mouse_brain.rds"))

Then, the separate files can be loaded:

my_instructions = Giotto::createGiottoInstructions(python_path = 'D:/Programme/Anaconda/envs/giotto_env/python.exe')
dir = "C:/Users/sfrerich/Downloads/"

# download
curl::curl_download(
  url = "https://www.dropbox.com/s/8x5dxmv0qw9yg9q/mouse_brain_giotto.rds?raw=1",
  destfile = paste0(dir,"mouse_brain_giotto.rds")
  )
curl::curl_download(
  url = "https://www.dropbox.com/s/9nahkb5buf937x8/giotto_mouse_brain_image.png?raw=1",
  destfile = paste0(dir,"giotto_mouse_brain_image.png")
  )
curl::curl_download(
  url = "https://www.dropbox.com/s/dt3vq18dv7wh99k/giotto_mouse_brain_spatial_locs.rds?raw=1",
  destfile = paste0(dir,"giotto_mouse_brain_spatial_locs.rds")
  )

# load to R
spatial_locs <- readRDS(paste0(dir,"/giotto_mouse_brain_spatial_locs.rds"))
colnames(spatial_locs) <- c('x', 'y', 'barcodes')
image_ebi <- magick::as_EBImage(magick::image_read(paste0(dir,"/giotto_mouse_brain_image.png")))
giotto_object <- readRDS(paste0(dir,"mouse_brain_giotto.rds"))
summary(giotto_object)
## Length  Class   Mode 
##      1 giotto     S4
Giotto::spatPlot(giotto_object, cell_color = 'leiden_clus', show_grid = F)

The Giotto object is converted to SPATA2 using asSPATA2() as above.

spata2_obj <- 
  SPATA2::asSPATA2(
    object = giotto_object,
    sample_name = "visium", 
    spatial_method = "Visium", 
    coordinates = spatial_locs, 
    image_ebi = image_ebi
    )

spata2_obj
## An object of class 'spata2' that contains 1 sample named 'visium'.

The arguments coordinates and image_ebi take image coordinates and the image file, respectively. These were imported separately above. These arguments are a workaround for Giotto files and not required for Seurat or AnnData conversion. The spata2 object can be used for any downstream analyses, such as spatial trajectories or image annotations. For correct plotting, the “leiden_clus” metadata is converted from integer to character.

spata2_obj@fdata[[1]]$leiden_clus <- as.factor(spata2_obj@fdata[[1]]$leiden_clus) 

SPATA2::plotSurface(spata2_obj, color_by = "leiden_clus", pt_clrp = "sifre")

Backwards compatibility from SPATA2 to Giotto is provided via asGiotto().