Skip to content

Commit

Permalink
draft of "use_geoms"
Browse files Browse the repository at this point in the history
  • Loading branch information
vjcitn committed Nov 25, 2024
1 parent 2151f9d commit 2ca7d9c
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: SpatialData.plot
Title: SpatialData visualization
Depends: R (>= 4.4), SpatialData
Version: 0.99.1
Version: 0.99.101
Description: Visualization suit for 'SpatialData' (R). Current functionality
includes handling of multiscale 'images', visualizing 'labels', 'points',
and 'shapes'. For the latter, POINT, POLYGON, and MULTIPOLYGON geometries
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Generated by roxygen2: do not edit by hand

export(add_points)
export(add_tx)
export(plotSpatialData)
export(viewShape)
exportMethods(plotImage)
exportMethods(plotLabel)
exportMethods(plotPoint)
Expand Down
67 changes: 67 additions & 0 deletions R/use_geoms.R
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)
}
18 changes: 18 additions & 0 deletions man/add_points.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/add_tx.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions man/viewShape.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 102 additions & 0 deletions vignettes/use_geoms.Rmd
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)
```

0 comments on commit 2ca7d9c

Please sign in to comment.