spatial_hm: Plot Spatial Heatmaps

spatial_hmR Documentation

Plot Spatial Heatmaps

Description

The input are a pair of annotated SVG (aSVG) file and formatted data (vector, data.frame, SummarizedExperiment). In the former, spatial features are represented by shapes and assigned unique identifiers, while the latter are numeric values measured from these spatial features and organized in specific formats. In biological cases, aSVGs are anatomical or cell structures, and data are measurements of genes, proteins, metabolites, etc. in different samples (e.g. cells, tissues). Data are mapped to the aSVG according to identifiers of assay samples and aSVG features. Only the data from samples having matching counterparts in aSVG features are mapped. The mapped features are filled with colors translated from the data, and the resulting images are termed spatial heatmaps. Note, "sample" and "feature" are two equivalent terms referring to cells, tissues, organs etc. where numeric values are measured. Matching means a target sample in data and a target spatial feature in aSVG have the same identifier.
This function is designed as much flexible as to achieve optimal visualization. For example, subplots of spatial heatmaps can be organized by gene or condition for easy comparison, in multi-layer anotomical structures selected tissues can be set transparent to expose burried features, color scale is customizable to highlight difference among features. This function also works with many other types of spatial data, such as population data plotted to geographic maps.

Usage

## S4 method for signature 'SVG'
spatial_hm(
  svg,
  data,
  assay.na = NULL,
  sam.factor = NULL,
  con.factor = NULL,
  ID,
  charcoal = FALSE,
  alpha.overlay = 1,
  lay.shm = "gene",
  ncol = 2,
  col.com = c("yellow", "orange", "red"),
  col.bar = "selected",
  thr = c(NA, NA),
  cores = NA,
  bar.width = 0.08,
  bar.title = NULL,
  bar.title.size = 0,
  scale = NULL,
  ft.trans = NULL,
  tis.trans = ft.trans,
  lis.rematch = NULL,
  legend.r = 0.9,
  sub.title.size = 11,
  sub.title.vjust = 2,
  legend.plot = "all",
  ft.legend = "identical",
  bar.value.size = 10,
  legend.plot.title = "Legend",
  legend.plot.title.size = 11,
  legend.ncol = NULL,
  legend.nrow = NULL,
  legend.position = "bottom",
  legend.direction = NULL,
  legend.key.size = 0.02,
  legend.text.size = 12,
  angle.text.key = NULL,
  position.text.key = NULL,
  legend.2nd = FALSE,
  position.2nd = "bottom",
  legend.nrow.2nd = NULL,
  legend.ncol.2nd = NULL,
  legend.key.size.2nd = 0.03,
  legend.text.size.2nd = 10,
  angle.text.key.2nd = 0,
  position.text.key.2nd = "right",
  add.feature.2nd = FALSE,
  label = FALSE,
  label.size = 4,
  label.angle = 0,
  hjust = 0,
  vjust = 0,
  opacity = 1,
  key = TRUE,
  line.width = 0.2,
  line.color = "grey70",
  relative.scale = NULL,
  verbose = TRUE,
  out.dir = NULL,
  animation.scale = 1,
  selfcontained = FALSE,
  video.dim = "640x480",
  res = 500,
  interval = 1,
  framerate = 1,
  bar.width.vdo = 0.1,
  legend.value.vdo = NULL,
  ...
)

Arguments

svg

An SVG object containing one or multiple aSVG instances (see SVG and read_svg). In the aSVGs, spatial features (tissues, organs, etc) having counterparts with the same identifiers in the 'bulk' data will be colored accoording to expression profiles of chosen biomolecules (genes, proteins, etc).

data

An 'SHM' class that containing the numeric data and aSVG instances for plotting SHMs or co-visualization plots. See SPHM.

assay.na

The name of target assay to use when data is SummarizedExperiment.

sam.factor

The column name corresponding to spatial features in colData of SummarizedExperiment. If the column names in the assay slot already follows the scheme "spatialFeature__variable", then the colData slot is not required and accordingly this argument could be NULL.

con.factor

The column name corresponding to experimental variables in colData of SummarizedExperiment. It can be NULL if column names of in the assay slot already follows the scheme "spatialFeature__variable", or no variable is associated with the data.

ID

A character vector of assyed items (e.g. genes, proteins) whose abudance values are used to color the aSVG.

charcoal

Logical, if TRUE the raster image will be turned black and white.

alpha.overlay

The opacity of the raster image under the SHM when superimposing raster images with SHMs. The default is 1.

lay.shm

One of 'gene', 'con', or 'none'. If 'gene', SHMs are organized horizontally by each biomolecule (gene, protein, or metabolite, etc.) and variables are sorted under each biomolecule. If 'con', SHMs are organized horizontally by each experiment vairable and biomolecules are sorted under each variable. If 'none', SHMs are organized by the biomolecule order in ID and variables follow the order they appear in data.

ncol

The number of columns to display SHMs, which does not include the legend plot.

col.com

A vector of color components used to build the color scale. The default is ‘c(’yellow', 'orange', 'red')'.

col.bar

One of 'selected' or 'all', the former uses expression values of ID to build the color scale while the latter uses all expression values from the data. The default is 'selected'.

thr

A two-numeric vector of expression value thresholds (the range of the color bar). The first and the second element will be the minmun and maximum threshold in the color bar respectively. Values above the max or below min will be assigned the same color as the max or min respectively. The default is c(NA, NA) and the min and max values in the data will be used. If one needs to change only max or min, the other should be NA.

cores

The number of CPU cores for parallelization. The default is 'NA', and the number of used cores is 1 or 2 depending on the availability.

bar.width

The width of color bar that ranges from 0 to 1. The default is 0.08.

bar.title, bar.title.size

The title and title size of the color key.

scale

One of no (default), selected, all, or row, corresponding to no scaling, scaling selected rows as a whole, scaling all rows as a whole, or scaling each row independently.

ft.trans

A character vector of spatial features that will be set transparent. When features of interest are covered by overlapping features on the top layers and the latter can be set transparent.

tis.trans

This argument is deprecated and replaced by ft.trans.

lis.rematch

The list for re-matching in SHMs (only bulk data) or matching between single-cell and bulk data in co-visualization.

SHMs

A named list for rematching spatial features between numeric data (ftA, ftB) and aSVGs (ftC, ftD, ftE). In each slot, the slot name is a spatial feature from the data and the corresponding element is one or multiple spatial features from the aSVG. E.g. list(ftA = c('ftC', 'ftD'), ftB = c('ftE')).

Co-visualization plots

Mapping cells to tissues: a named list, where cell group labels from colData(sce. dimred)[, 'cell.group'] are the name slots and aSVG features are the corresponding list elements. Mapping tissues to cells: a named list, where tissues are the name slots and cells from colData(sce.dimred)[, 'cell.group'] are the corresponding list elements. Applicable when cell grouping methods are annodation labels, marker genes, clustering, or manual assignments.

legend.r

A numeric (-1 to 1) to adjust the legend plot size.

sub.title.size

A numeric of the subtitle font size of each individual spatial heatmap. The default is 11.

sub.title.vjust

A numeric of vertical adjustment for subtitle. The default is 2.

legend.plot

A vector of suffix(es) of aSVG file name(s) such as c('shm1', 'shm2'). Only aSVG(s) whose suffix(es) are assigned to this arugment will have a legend plot on the right. The default is all and each aSVG will have a legend plot. If NULL, no legend plot is shown.

ft.legend

One of "identical", "all", or a character vector of tissue/spatial feature identifiers from the aSVG file. The default is "identical" and all the identical/matching tissues/spatial features between the data and aSVG file are colored in the legend plot. If "all", all tissues/spatial features in the aSVG are shown. If a vector, only the tissues/spatial features in the vector are shown.

bar.value.size

A numeric of value size in the y-axis of the color bar. The default is 10.

legend.plot.title

The title of the legend plot. The default is 'Legend'.

legend.plot.title.size

The title size of the legend plot. The default is 11.

legend.ncol

An integer of the total columns of keys in the legend plot. The default is NULL. If both legend.ncol and legend.nrow are used, the product of the two arguments should be equal or larger than the total number of shown spatial features.

legend.nrow

An integer of the total rows of keys in the legend plot. The default is NULL. It is only applicable to the legend plot. If both legend.ncol and legend.nrow are used, the product of the two arguments should be equal or larger than the total number of matching spatial features.

legend.position

the default position of legends ("none", "left", "right", "bottom", "top", "inside")

legend.direction

layout of items in legends ("horizontal" or "vertical")

legend.key.size

A numeric of the legend key size ("npc"), applicable to the legend plot. The default is 0.02.

legend.text.size

A numeric of the legend label size, applicable to the legend plot. The default is 12.

angle.text.key

Key text angle in legend plots. The default is NULL, equivalent to 0.

position.text.key

The position of key text in legend plots, one of 'top', 'right', 'bottom', 'left'. Default is NULL, equivalent to 'right'.

legend.2nd

Logical. If 'TRUE', the secondary legend is added to each SHM, which are the numeric values of each colored spatial features. The default its 'FALSE'. Only applies to the static image.

position.2nd

The position of the secondary legend in SHMs, one of 'top', 'right', 'bottom', 'left', or a two-component numeric vector. The default is 'bottom'. Applies to images and videos.

legend.nrow.2nd

An integer of rows of the secondary legend keys in SHMs. Applies to static images and videos.

legend.ncol.2nd

An integer of columns of the secondary legend keys in SHMs. Applies to static images and videos.

legend.key.size.2nd

A numeric of legend key size in SHMs. The default is 0.03. Applies to static images and videos.

legend.text.size.2nd

A numeric of the secondary legend text size in SHMs. The default is 10. Applies to static images and videos.

angle.text.key.2nd

Angle of the key text in the secondary legend in SHMs. Default is 0. Applies to static images and videos.

position.text.key.2nd

The position of key text in the secondary legend in SHMs, one of 'top', 'right', 'bottom', 'left'. Default is 'right'. Applies to static images and videos.

add.feature.2nd

Logical. If 'TRUE', feature identifiers are added to the secondary legend. The default is FALSE. Applies to static images of SHMs.

label

Logical. If 'TRUE', the same spatial features between numeric data and aSVG are labeled by their identifiers. The default is 'FALSE'. It is useful when spatial features are labeled by similar colors.

label.size

The size of spatial feature labels in legend plots. The default is 4.

label.angle

The angle of spatial feature labels in legend plots. Default is 0.

hjust, vjust

The value to horizontally or vertically adjust positions of spatial feature labels in legend plots respectively. Default of both is 0.

opacity

The transparency of colored spatial features in legend plots. Default is 1. If 0, features are totally transparent.

key

Logical. If 'TRUE' (default), keys are added in legend plots. If label is TRUE, the keys could be removed.

line.width

The thickness of each shape outline in the aSVG is maintained in spatial heatmaps, i.e. the stroke widths in Inkscape. This argument is the extra thickness added to all outlines. Default is 0.2 in case stroke widths in the aSVG are 0.

line.color

A character of the shape outline color. Default is "grey70".

relative.scale

A numeric to adjust the relative sizes between multiple aSVGs. Applicable only if multiple aSVGs are provided. Default is NULL and all aSVGs have the same size.

verbose

Logical. If 'TRUE' (default), intermediate messages will be printed.

out.dir

The directory to save SHMs as interactive HTML files and videos. Default is 'NULL', and the HTML files and videos are not saved.

animation.scale

A numeric to scale the SHM size in the HTML files. The default is 1, and the height is 550px and the width is calculated according to the original aspect ratio in the aSVG file.

selfcontained

Whether to save the HTML as a single self-contained file (with external resources base64 encoded) or a file with external resources placed in an adjacent directory.

video.dim

A single character of the dimension of video frame in form of 'widthxheight', such as '1920x1080', '1280x800', '320x568', '1280x1024', '1280x720', '320x480', '480x360', '600x600', '800x600', '640x480' (default). The aspect ratio of SHMs are decided by width and height.

res

Resolution of the video in dpi.

interval

The time interval (seconds) between SHM frames in the video. Default is 1.

framerate

An integer of video framerate in frames per seconds. Default is 1. Larger values make the video smoother.

bar.width.vdo

The color bar width (0-1) in videos.

legend.value.vdo

Logical. If 'TRUE', numeric values of colored spatial features are added to the video legend. The default is NULL.

...

additional element specifications not part of base ggplot2. In general, these should also be defined in the ⁠element tree⁠ argument. Splicing a list is also supported.

Value

An image of spatial heatmap(s), a two-component list of the spatial heatmap(s) in ggplot format and a data.frame of mapping between assayed samples and aSVG features.

Details

See the package vignette (browseVignettes('spatialHeatmap')).

Author(s)

Jianhai Zhang jianhai.zhang@email.ucr.edu
Dr. Thomas Girke thomas.girke@ucr.edu

References

https://www.gimp.org/tutorials/
https://inkscape.org/en/doc/tutorials/advanced/tutorial-advanced.en.html
http://www.microugly.com/inkscape-quickguide/ Martin Morgan, Valerie Obenchain, Jim Hester and Hervé Pagès (2018). SummarizedExperiment: SummarizedExperiment container. R package version 1.10.1
H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2016.
Jeroen Ooms (2018). rsvg: Render SVG Images into PDF, PNG, PostScript, or Bitmap Arrays. R package version 1.3. https://CRAN.R-project.org/package=rsvg
R. Gentleman, V. Carey, W. Huber and F. Hahne (2017). genefilter: genefilter: methods for filtering genes from high-throughput experiments. R package version 1.58.1
Paul Murrell (2009). Importing Vector Graphics: The grImport Package for R. Journal of Statistical Software, 30(4), 1-37. URL http://www.jstatsoft.org/v30/i04/
Baptiste Auguie (2017). gridExtra: Miscellaneous Functions for "Grid" Graphics. R package version 2.3. https://CRAN.R-project.org/package=gridExtra
R Core Team (2018). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. RL https://www.R-project.org/
https://github.com/ebi-gene-expression-group/anatomogram/tree/master/src/svg
Yu, G., 2020. ggplotify: Convert Plot to ’grob’ or ’ggplot’ Object. R package version 0.0.5.URLhttps://CRAN.R-project.org/package=ggplotify30
Keays, Maria. 2019. ExpressionAtlas: Download Datasets from EMBL-EBI Expression Atlas
Love, Michael I., Wolfgang Huber, and Simon Anders. 2014. "Moderated Estimation of Fold Change and Dispersion for RNA-Seq Data with DESeq2." Genome Biology 15 (12): 550. doi:10.1186/s13059-014-0550-8
Guangchuang Yu (2020). ggplotify: Convert Plot to 'grob' or 'ggplot' Object. R package version 0.0.5. https://CRAN.R-project.org/package=ggplotify
Cardoso-Moreira, Margarida, Jean Halbert, Delphine Valloton, Britta Velten, Chunyan Chen, Yi Shao, Angélica Liechti, et al. 2019. “Gene Expression Across Mammalian Organ Development.” Nature 571 (7766): 505–9 Marques A et al. (2016). Oligodendrocyte heterogeneity in the mouse juvenile and adult central nervous system. Science 352(6291), 1326-1329. Amezquita R, Lun A, Becht E, Carey V, Carpp L, Geistlinger L, Marini F, Rue-Albrecht K, Risso D, Soneson C, Waldron L, Pages H, Smith M, Huber W, Morgan M, Gottardo R, Hicks S (2020). “Orchestrating single-cell analysis with Bioconductor.” Nature Methods, 17, 137–145. https://www.nature.com/articles/s41592-019-0654-x.

Examples

## In the following examples, the 2 toy data come from an RNA-seq analysis on development of 7
## chicken organs under 9 time points (Cardoso-Moreira et al. 2019). For conveninece, they are
## included in this package. The complete raw count data are downloaded using the R package
## ExpressionAtlas (Keays 2019) with the accession number "E-MTAB-6769". Toy data1 is used as
## a "data frame" input to exemplify data of simple samples/conditions, while toy data2 as
## "SummarizedExperiment" to illustrate data involving complex samples/conditions.   

## Set up toy data.

# Access toy data1.
cnt.chk.simple <- system.file('extdata/shinyApp/data/count_chicken_simple.txt',
package='spatialHeatmap')
df.chk <- read.table(cnt.chk.simple, header=TRUE, row.names=1, sep='\t', check.names=FALSE)
# Columns follow the namig scheme "sample__condition", where "sample" and "condition" stands
# for organs and time points respectively.
df.chk[1:3, ]

# A column of gene annotation can be appended to the data frame, but is not required.  
ann <- paste0('ann', seq_len(nrow(df.chk))); ann[1:3]
df.chk <- cbind(df.chk, ann=ann)
df.chk[1:3, ]

# Access toy data2. 
cnt.chk <- system.file('extdata/shinyApp/data/count_chicken.txt', package='spatialHeatmap')
count.chk <- read.table(cnt.chk, header=TRUE, row.names=1, sep='\t')
count.chk[1:3, 1:5]

# A targets file describing samples and conditions is required for toy data2. It should be made
# based on the experiment design, which is accessible through the accession number 
# "E-MTAB-6769" in the R package ExpressionAtlas. An example targets file is included in this
# package and accessed below. 
# Access the example targets file. 
tar.chk <- system.file('extdata/shinyApp/data/target_chicken.txt', package='spatialHeatmap')
target.chk <- read.table(tar.chk, header=TRUE, row.names=1, sep='\t')
# Every column in toy data2 corresponds with a row in targets file. 
target.chk[1:5, ]
# Store toy data2 in "SummarizedExperiment".
library(SummarizedExperiment)
se.chk <- SummarizedExperiment(assay=count.chk, colData=target.chk)
# The "rowData" slot can store a data frame of gene annotation, but not required.
rowData(se.chk) <- DataFrame(ann=ann)

## As conventions, raw sequencing count data should be normalized, aggregated, and filtered to
## reduce noise.

# Normalize count data.
# The normalizing function "calcNormFactors" (McCarthy et al. 2012) with default settings
# is used.  
df.nor.chk <- norm_data(data=df.chk, norm.fun='CNF', log2.trans=TRUE)
se.nor.chk <- norm_data(data=se.chk, norm.fun='CNF', log2.trans=TRUE)
# Aggregate count data.
# Aggregate "sample__condition" replicates in toy data1.
df.aggr.chk <- aggr_rep(data=df.nor.chk, aggr='mean')
df.aggr.chk[1:3, ]
# Aggregate "sample_condition" replicates in toy data2, where "sample" is "organism_part" and
# "condition" is "age". 
se.aggr.chk <- aggr_rep(data=se.nor.chk, sam.factor='organism_part', con.factor='age',
aggr='mean')
assay(se.aggr.chk)[1:3, 1:3]
# Filter out genes with low counts and low variance. Genes with counts over 5 (log2 unit) in
# at least 1% samples (pOA), and coefficient of variance (CV) between 0.2 and 100 are retained.
# Filter toy data1.
df.fil.chk <- filter_data(data=df.aggr.chk, pOA=c(0.01, 5), CV=c(0.2, 100))
# Filter toy data2.
se.fil.chk <- filter_data(data=se.aggr.chk, sam.factor='organism_part', con.factor='age',
pOA=c(0.01, 5), CV=c(0.2, 100))

## Spatial heatmaps.

# The target chicken aSVG is downloaded from the EBI aSVG repository
# (https://github.com/ebi-gene-expression-group/anatomogram/tree/master/src/svg) directly with
# function "return_feature". It is included in this package and accessed as below. Details on
# how this aSVG is selected are documented in function "return_feature".
svg.chk <- system.file("extdata/shinyApp/data", "gallus_gallus.svg",
package="spatialHeatmap")

# Reading the chicken aSVG file.
svg.chk <- read_svg(svg.path=svg.chk)

# Plot spatial heatmaps on gene "ENSGALG00000019846".
# Toy data1. 
spatial_hm(svg=svg.chk, data=df.fil.chk, ID='ENSGALG00000019846', height=0.4,
legend.r=1.9, sub.title.size=7, ncol=3)
# Save spaital heatmaps as HTML and video files by assigning "out.dir" "~/test". 

if (!dir.exists('~/test')) dir.create('~/test')
spatial_hm(svg=svg.chk, data=df.fil.chk, ID='ENSGALG00000019846', height=0.4,
legend.r=1.9, sub.title.size=7, ncol=3, out.dir='~/test')

# Toy data2.
spatial_hm(svg=svg.chk, data=se.fil.chk, ID='ENSGALG00000019846', legend.r=1.9,
legend.nrow=2, sub.title.size=7, ncol=3)


jianhaizhang/spatialHeatmap documentation built on Nov. 28, 2024, 4:44 p.m.