[413088]: / docs / interactive.Rmd

Download this file

193 lines (137 with data), 8.2 kB

---
title: "Interactive"
output: 
  html_document:
    toc: true
    toc_float:
      collapsed: false
      smooth_scroll: false
---

<style>
.title{
  display: none;
}
body {
  text-align: justify
}
.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
}
</style>

```{css, echo=FALSE}
.watch-out {
  color: black;
}
```

```{r setup, include=FALSE}
# use rmarkdown::render_site(envir = knitr::knit_global())
knitr::opts_chunk$set(highlight = TRUE, echo = TRUE)
```

<br>

# Interactive Annotation

**VoltRon** includes interactive applications to select and manually label spatial points by drawing polygons and circles. As an example, we will use a Spot-based spatial transcriptomic assay, specifically the **Mouse Brain Serial Section 1/2** datasets, analyzed in the [Niche Clustering](nicheclustering.html) tutorial. You can find the already analyzed data stored as a VoltRon object [here](https://bimsbstatic.mdc-berlin.de/landthaler/VoltRon/NicheClustering/Visium&Visium_data_decon_analyzed.rds)

```{r class.source="watch-out", eval = FALSE}
MBrain_Sec <- readRDS("Visium&Visium_data_decon_analyzed.rds")
```

We can start annotating the spatial assay. By passing arguments used by the **vrSpatialPlot** function to visualize labels (e.g. clusters), we can better select regions within tissue sections for annotation. 

```{r class.source="watch-out", eval = FALSE}
MBrain_Sec <- annotateSpatialData(MBrain_Sec, assay = "Assay1", 
                                  group.by = "clusters", label = "annotation")
```

<img width="100%" height="100%" src="https://bimsbstatic.mdc-berlin.de/landthaler/VoltRon/Package/images/interactive_annotation.png" class="center">

<br>

Here, **annotateSpatialData** function not only labels spots with within regions of interests (ROIs) selected by the user, but also records these regions in ROI assays within the same layer of the annotated Visium assay. The new assay type will be given the name **ROIAnnotations** if otherwise not specified using the **annotation_assay** arguement in the function.

```{r class.source="watch-out", eval = FALSE}
MBrain_Sec 
```

```
VoltRon Object 
Anterior: 
  Layers: Section1 Section2 
Posterior: 
  Layers: Section1 Section2 
Assays: Visium_decon(Main) Visium ROIAnnotation 
```

```{r class.source="watch-out", eval = FALSE}
MBrain_Sec@sample.metadata 
```

```
> MBrain_Sec@sample.metadata
               Assay    Layer    Sample
Assay1        Visium Section1  Anterior
Assay2        Visium Section2  Anterior
Assay3        Visium Section1 Posterior
Assay4        Visium Section2 Posterior
Assay5 ROIAnnotation Section1  Anterior
```

The new annotations are available in the metadata of the spot assay (default assay in this object) and can be visualized if wanted.

```{r class.source="watch-out", eval = FALSE}
head(Metadata(MBrain_Sec))
```

```
                          Count        Assay    Layer   Sample clusters annotation
AAACAAGTATCTCCCA-1_Assay1     1 Visium_decon Section1 Anterior        1   Region 3
AAACACCAATAACTGC-1_Assay1     1 Visium_decon Section1 Anterior        3  undefined
AAACAGAGCGACTCCT-1_Assay1     1 Visium_decon Section1 Anterior        4  undefined
AAACAGCTTTCAGAAG-1_Assay1     1 Visium_decon Section1 Anterior        5   Region 1
AAACAGGGTCTATATT-1_Assay1     1 Visium_decon Section1 Anterior        5   Region 1
AAACATGGTGAGAGGA-1_Assay1     1 Visium_decon Section1 Anterior        3  undefined
```

```{r class.source="watch-out", eval = FALSE}
vrSpatialPlot(MBrain_Sec, assay = "Assay1", group.by = "annotation")
```

<img width="80%" height="80%" src="https://bimsbstatic.mdc-berlin.de/landthaler/VoltRon/Package/images/interactive_annotation_plot.png" class="center">

<br>

You can also overlay the ROI annotations with the clustered spots to visualize multiple assays in the same time.

```{r class.source="watch-out", eval = FALSE}
library(ggnewscale)
vrSpatialPlot(MBrain_Sec, assay = "Assay1", group.by = "clusters") |> 
  addSpatialLayer(MBrain_Sec, assay = "Assay5", group.by = "annotation", alpha = 0.7)
```

<img width="80%" height="80%" src="https://bimsbstatic.mdc-berlin.de/landthaler/VoltRon/Package/images/interactive_annotation_example_overlay_plot.png" class="center">

<br>

# Interactive Visualization

VoltRon incorporates utilities 

* **(i)** to convert VoltRon objects into other spatial data objects and files that could be used platforms, and 
* **(ii)** to incorporate wrapper functions to call methods from package that achieves interactive visualization

<br>

## Vitessce 

We will transform VoltRon objects of Xenium data into zarr arrays, and use them for interactive visualization in [Vitessce](http://vitessce.io/). We should first download the vitessceR package which incorporates wrapper function to visualize zarr arrays interactively in R.

```{r class.source="watch-out", eval = FALSE}
if (!require("devtools", quietly = TRUE))
    install.packages("devtools")
if (!require("vitessceR", quietly = TRUE))
    devtools::install_github("Artur-man/vitessceR")
```

We can convert the VoltRon object into an anndata object and save it as a a zarr array using the **as.AnnData** function which will create the array in the specified location. We use the **flip_coordinates=TRUE** argument to flip the coordinates of cells vertically, hence match it with the top to bottom system of the background DAPI image. Also, we can save an OMETIFF file of the DAPI image using **create.ometiff** arguement to be used by vitessceR later.

```{r class.source="watch-out", eval = FALSE}
xenium_data <- as.AnnData(xenium_data, file = "xenium_data.zarr", assay = "Xenium", 
                          flip_coordinates = TRUE, create.ometiff = TRUE)
```

<br>

We can use the zarr file directly in the **vrSpatialPlot** function to visualize the zarr array interactively in Rstudio viewer. The **reduction** arguement allows the umap of the Xenium data to be visualized alongside with the spatial coordinates of the Xenium cells (thus associated cell segmentations).

```{r class.source="watch-out", eval = FALSE}
vrSpatialPlot("xenium_data.zarr", group.by = "clusters", reduction = "umap")
```

<img width="90%" height="90%" src="https://bimsbstatic.mdc-berlin.de/landthaler/VoltRon/Package/images/interactive_vitessce.png" class="center">

<br>

The vitessce application in the viewer pane allows visualizing background DAPI image and segmentations simultaneously while allowing users to zoom in and control the pane for advanced visualization. 

<img width="90%" height="90%" src="https://bimsbstatic.mdc-berlin.de/landthaler/VoltRon/Package/images/interactive_vitessce_zoom.png" class="center">

<br>

## TissUUmaps

To use **TissUUmaps** for interactive investigation of your spatial omic data, we first need to convert the VoltRon object into an anndata object. However, this time we save the anndata object as an h5ad array using again the **as.AnnData** function which will create the array in the specified location. We use the **flip_coordinates=TRUE** argument to flip the coordinates of cells vertically, hence match it with the top to bottom system of the background DAPI image. 

```{r class.source="watch-out", eval = FALSE}
as.AnnData(xenium_data, file = "xenium_data.h5ad", assay = "Xenium", flip_coordinates = TRUE)
```

To use **TissUUmaps**, you can follow the instructions [here](https://tissuumaps.github.io/installation/). Once installed and executed, simply drag and drop the h5ad file to the main panel of the application

<img width="100%" height="100%" src="https://bimsbstatic.mdc-berlin.de/landthaler/VoltRon/Package/images/interactive_tissuumaps.png" class="center">

<br>

## VoltRon

You can also use the built-in Shiny-based interactive visualizers of the VoltRon package by calling **interactive=TRUE**. You can zoom in by drawing a box on the plot and double-clicking in the selected area. 

```{r class.source="watch-out", eval = FALSE}
vrSpatialPlot(xenium_data, group.by = "clusters", plot.segments = TRUE, interactive = TRUE)
```

<img width="85%" height="85%" src="https://bimsbstatic.mdc-berlin.de/landthaler/VoltRon/Package/images/interactive_voltron.png" class="center">

<br>