Skip to content

Instantly share code, notes, and snippets.

@vjcitn
Created November 22, 2024 11:54
Show Gist options
  • Save vjcitn/dd7ec30c239de685e5e1cd4b98edbc37 to your computer and use it in GitHub Desktop.
Save vjcitn/dd7ec30c239de685e5e1cd4b98edbc37 to your computer and use it in GitHub Desktop.
---
title: "`Some SpatialData visualizations`"
date: "`r format(Sys.Date(), '%B %d, %Y')`"
package: "`r BiocStyle::pkg_ver('SpatialData')`"
author:
- name: Helena Lucia Crowell
- name: Louise Deconinck
- name: Artür Manukyan
- name: Dario Righelli
- name: Estella Dong
- name: Vince Carey
output:
BiocStyle::html_document
vignette: |
%\VignetteIndexEntry{Some SpatialData visualizations}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r backing, echo=FALSE}
# shape rendering support
# could transition to S4 if needed
.shapenames = function(sdobj) {
stopifnot(is(sdobj, "SpatialData"))
names(shapes(sdobj))
}
.shapes2sf = function(sdobj, elem) {
stopifnot(elem %in% .shapenames(sdobj))
sf::st_as_sf(SpatialData::data(SpatialData::shapes(sdobj)[[elem]]))
}
.pointnames = function(sdobj) {
stopifnot(is(sdobj, "SpatialData"))
names(points(sdobj))
}
.txdf = function(sdobj) {
stopifnot("transcripts" %in% .pointnames(sdobj))
as.data.frame(data(points(sdobj)$transcripts))
}
.pointdf = function(sdobj, elem) {
stopifnot(elem %in% .pointnames(sdobj))
as.data.frame(data(points(sdobj)[[elem]]))
}
.available_transcripts = function(sdobj) { # maybe too specific? 'points'?
txdf = .txdf(sdobj)
as.character(unique(txdf$feature_name)) # valid? feature_name comes back as *factor*
}
viewShape = function(sdobj, elem) {
thesf = .shapes2sf(sdobj, elem)
ggplot2::ggplot(thesf) + geom_sf()
}
add_tx = function(sdobj, featurename, size=.1) {
txdf = .txdf(sdobj) |> dplyr::filter(feature_name == featurename)
ggplot2::geom_point(data=txdf, aes(x=x, y=y), size=size)
}
add_points = function(sdobj, featurename, size=.1) {
pointdf = .pointdf(sdobj) |> dplyr::filter(feature_name == featurename)
ggplot2::geom_point(data=pointdf, aes(x=x, y=y), size=size)
}
```
# Introduction
In this vignette we explore how shape and point data in Xenium and
MERFISH experiments can be visualized. We use examples that
can be retrieved from a Bioconductor bucket and cached in
BiocFileCache, using utilities in SpatialData package.
The xenium examples are:
```{r lkx, message=FALSE}
library(SpatialData)
available_10x_xen_zips()
```
[1] "Xenium_V1_human_Breast_2fov_outs.zip"
[2] "Xenium_V1_human_Lung_2fov_outs.zip"
# Breast sample, "two fields of view"
## A view of "image data"
```{r getbr, message=FALSE}
pa <- path_to_10x_xen_demo("Xenium_V1_human_Breast_2fov_outs.zip")
dir.create(td <- tempfile())
unzip(pa, exdir=td)
# read & write to .zarr w/ 'spatialdata-io'
target <- tempfile()
nn = use_sdio("xenium", srcdir=td, dest=target) # returns NULL
# read into R
(br2fov <- readSpatialData(target))
plotSpatialData() + plotImage(br2fov)
```
## A view of the segmentation
```{r lkcb, message=FALSE}
library(ggplot2)
viewShape(br2fov, "cell_boundaries")
```
## Add points for a specific 'transcript'
```{r lkcb2, message=FALSE}
viewShape(br2fov, "cell_boundaries") + add_tx(br2fov, "EPCAM")
unlink(target, recursive=TRUE)
```
# Lung sample, "two fields of view"
## Image
```{r getlu, message=FALSE}
pa <- path_to_10x_xen_demo("Xenium_V1_human_Lung_2fov_outs.zip")
dir.create(td <- tempfile())
unzip(pa, exdir=td)
target <- tempfile()
nn = use_sdio("xenium", srcdir=td, dest=target) # returns NULL
(lu2fov <- readSpatialData(target))
plotSpatialData() + plotImage(lu2fov)
```
## Transcripts on segmentation
```{r lklu2, message=FALSE}
viewShape(lu2fov, "cell_boundaries") + add_tx(lu2fov, "EPCAM")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment