-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
229 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# functions here emphasize use of the coordinate data | ||
|
||
# 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* | ||
} | ||
|
||
#' Use geom_sf to view a shapes component | ||
#' @import ggplot2 | ||
#' @param sdobj SpatialData instance | ||
#' @param elem character(1) name of a shapes component of sdobj | ||
#' @export | ||
viewShape = function(sdobj, elem) { | ||
thesf = .shapes2sf(sdobj, elem) | ||
ggplot2::ggplot(thesf) + ggplot2::geom_sf() | ||
} | ||
|
||
#' Use geom_point to enhance a visualization with viewShape | ||
#' @param sdobj SpatialData instance | ||
#' @param featurename character(1) name of a shapes component of sdobj | ||
#' @param size numeric(1) target size for glyph | ||
#' @examples | ||
#' example(use_sdio) # produces br2fov | ||
#' viewShape(br2fov, "cell_boundaries") + add_tx(br2fov, "EPCAM") | ||
#' @export | ||
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) | ||
} | ||
|
||
#' Use geom_point more generally than add_tx | ||
#' @param sdobj SpatialData instance | ||
#' @param featurename character(1) name of a shapes component of sdobj | ||
#' @param size numeric(1) target size for glyph | ||
#' @export | ||
add_points = function(sdobj, featurename, size=.1) { | ||
pointdf = .pointdf(sdobj) | ||
ggplot2::geom_point(data=pointdf, aes(x=x, y=y), size=size) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
--- | ||
title: "`Some SpatialData visualizations based on ggplot2 for spatial features`" | ||
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} | ||
--- | ||
|
||
|
||
# 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)) | ||
library(SpatialData.plot) | ||
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") | ||
``` | ||
|
||
# MERFISH example | ||
|
||
In this demo, we show how to take advantage of | ||
size information in the shape data on cells. | ||
|
||
```{r lkmerf} | ||
tf = tempfile() | ||
dir.create(tf) | ||
pa = unzip_merfish_demo(tf) | ||
pa | ||
merf = readSpatialData(pa) | ||
merfs = SpatialData.plot:::.shapes2sf(merf, "cells") | ||
ggplot(merfs) + geom_sf(colour="gray", size=merfs$radius/20) | ||
``` |