From 2ca7d9c9cefd6d21e3fa21bac0c4a5a691a553c8 Mon Sep 17 00:00:00 2001 From: vjcitn Date: Mon, 25 Nov 2024 10:14:28 -0500 Subject: [PATCH] draft of "use_geoms" --- DESCRIPTION | 2 +- NAMESPACE | 3 ++ R/use_geoms.R | 67 ++++++++++++++++++++++++++ man/add_points.Rd | 18 +++++++ man/add_tx.Rd | 22 +++++++++ man/viewShape.Rd | 16 +++++++ vignettes/use_geoms.Rmd | 102 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 R/use_geoms.R create mode 100644 man/add_points.Rd create mode 100644 man/add_tx.Rd create mode 100644 man/viewShape.Rd create mode 100644 vignettes/use_geoms.Rmd diff --git a/DESCRIPTION b/DESCRIPTION index 5960769..148d403 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/NAMESPACE b/NAMESPACE index c8ecce5..195292d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/use_geoms.R b/R/use_geoms.R new file mode 100644 index 0000000..37df185 --- /dev/null +++ b/R/use_geoms.R @@ -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) +} diff --git a/man/add_points.Rd b/man/add_points.Rd new file mode 100644 index 0000000..5cc824a --- /dev/null +++ b/man/add_points.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/use_geoms.R +\name{add_points} +\alias{add_points} +\title{Use geom_point more generally than add_tx} +\usage{ +add_points(sdobj, featurename, size = 0.1) +} +\arguments{ +\item{sdobj}{SpatialData instance} + +\item{featurename}{character(1) name of a shapes component of sdobj} + +\item{size}{numeric(1) target size for glyph} +} +\description{ +Use geom_point more generally than add_tx +} diff --git a/man/add_tx.Rd b/man/add_tx.Rd new file mode 100644 index 0000000..ed4d304 --- /dev/null +++ b/man/add_tx.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/use_geoms.R +\name{add_tx} +\alias{add_tx} +\title{Use geom_point to enhance a visualization with viewShape} +\usage{ +add_tx(sdobj, featurename, size = 0.1) +} +\arguments{ +\item{sdobj}{SpatialData instance} + +\item{featurename}{character(1) name of a shapes component of sdobj} + +\item{size}{numeric(1) target size for glyph} +} +\description{ +Use geom_point to enhance a visualization with viewShape +} +\examples{ +example(use_sdio) # produces br2fov +viewShape(br2fov, "cell_boundaries") + add_tx(br2fov, "EPCAM") +} diff --git a/man/viewShape.Rd b/man/viewShape.Rd new file mode 100644 index 0000000..0273423 --- /dev/null +++ b/man/viewShape.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/use_geoms.R +\name{viewShape} +\alias{viewShape} +\title{Use geom_sf to view a shapes component} +\usage{ +viewShape(sdobj, elem) +} +\arguments{ +\item{sdobj}{SpatialData instance} + +\item{elem}{character(1) name of a shapes component of sdobj} +} +\description{ +Use geom_sf to view a shapes component +} diff --git a/vignettes/use_geoms.Rmd b/vignettes/use_geoms.Rmd new file mode 100644 index 0000000..8e5a3c8 --- /dev/null +++ b/vignettes/use_geoms.Rmd @@ -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) +```