heatmap_se | R Documentation |
Heatmap for SummarizedExperiment data
heatmap_se(
se,
sestats = NULL,
hm_name = NULL,
hm_title = NULL,
rows = NULL,
row_type = "rows",
column_type = "samples",
data_type = "expression",
correlation = FALSE,
assay_name = NULL,
contrast_names = NULL,
contrast_suffix = "",
cutoff_name = NULL,
alt_sestats = NULL,
alt_assay_name = assay_name,
alt_contrast_names = NULL,
alt_contrast_suffix = "",
alt_cutoff_name = NULL,
isamples = colnames(se),
normgroup_colname = NULL,
centerby_colnames = NULL,
controlSamples = NULL,
control_label = "",
controlFloor = NA,
naControlAction = c("na", "row", "floor", "min"),
naControlFloor = 0,
top_colnames = NULL,
top_annotation = NULL,
top_annotation_name_gp = grid::gpar(),
rowData_colnames = NULL,
left_annotation = NULL,
left_annotation_name_gp = grid::gpar(),
left_annotation_name_rot = 90,
right_annotation = NULL,
simple_anno_size = grid::unit(8, "mm"),
legend_title_gp = grid::gpar(fontsize = 10),
legend_labels_gp = grid::gpar(fontsize = 10),
legend_grid_cex = 1,
row_names_gp = NULL,
row_split = NULL,
row_subcluster = NULL,
row_title_rot = 0,
sample_color_list = NULL,
legend_at = NULL,
legend_labels = NULL,
subset_legend_colors = TRUE,
row_cex = 0.8,
column_cex = 1,
row_anno_fontsize = 11,
useMedian = FALSE,
show_row_names = NULL,
show_row_dend = length(rows) < 2000,
mark_rows = NULL,
mark_labels_gp = grid::gpar(),
column_title = character(0),
apply_hm_column_title = FALSE,
hm_title_buffer = 0,
show_heatmap_legend = TRUE,
show_top_legend = TRUE,
show_left_legend = TRUE,
legend_border_color = "black",
show_top_annotation_name = TRUE,
show_left_annotation_name = TRUE,
row_label_colname = NULL,
cluster_columns = FALSE,
cluster_column_slices = FALSE,
cluster_rows = function(x, ...) {
amap::hcluster(jamba::rmNA(naValue = 0, x), ...,
method = "euclidean", link = "ward")
},
cluster_row_slices = FALSE,
column_names_gp = NULL,
column_split = NULL,
column_split_sep = ",",
color_max = 3,
color_floor = 0,
lens = 2,
rename_contrasts = TRUE,
rename_alt_contrasts = TRUE,
use_raster = TRUE,
verbose = FALSE,
debug = FALSE,
...
)
se |
|
sestats |
one of the following types of data:
|
hm_name |
|
hm_title |
|
rows |
|
row_type |
|
column_type |
|
data_type |
|
correlation |
|
assay_name |
|
contrast_names |
|
contrast_suffix |
|
cutoff_name |
|
alt_sestats , alt_assay_name , alt_contrast_names , alt_contrast_suffix |
arguments analogous to those described above for |
isamples |
|
normgroup_colname |
|
centerby_colnames |
either:
|
controlSamples |
|
control_label |
|
controlFloor , naControlAction , naControlFloor |
passed to
|
top_colnames |
one of the following types:
|
top_annotation |
specific heatmap annotation as defined by
|
top_annotation_name_gp |
|
rowData_colnames |
|
left_annotation |
specific heatmap annotation as defined by
|
left_annotation_name_gp |
|
left_annotation_name_rot |
|
right_annotation |
specific heatmap annotation as defined by
|
simple_anno_size |
|
legend_title_gp |
|
legend_labels_gp |
|
legend_grid_cex |
|
row_names_gp |
|
row_split |
is used to define heatmap split by row, ultimately
passed to
|
row_subcluster |
|
row_title_rot |
|
sample_color_list |
named |
legend_at , legend_labels |
|
subset_legend_colors |
|
row_cex , column_cex |
|
row_anno_fontsize |
|
useMedian |
|
show_row_names , show_row_dend |
|
mark_rows |
|
mark_labels_gp |
|
column_title |
|
apply_hm_column_title |
|
hm_title_buffer |
|
show_heatmap_legend , show_left_legend , show_top_legend |
|
legend_border_color |
|
show_top_annotation_name , show_left_annotation_name |
|
row_label_colname |
|
cluster_columns , cluster_rows |
|
column_names_gp |
|
column_split |
|
column_split_sep |
|
color_max |
|
color_floor |
|
lens |
|
rename_contrasts , rename_alt_contrasts |
|
use_raster |
|
verbose |
|
debug |
|
... |
additional arguments are passed to supporting functions. |
Note: Still a work in progress. This function is the basis for the majority of heatmaps created for Omics data.
This function is a bold attempt to simplify the intricate task
of creating an expression heatmap, using ComplexHeatmap::Heatmap()
,
given a SummarizedExperiment
object.
It attempts to enable:
selection of assays(se)
to use in the heatmap
use of rowData(se)
or colData(se)
to produce row and
column annotations, respectively.
re-use of defined colors for annotations, see platjam::design2colors()
define and adjust heatmap color gradient and scale
data centering by row: versus all columns, or specific controls, optionally within independent centering groups
filtering rows to show only the statistical hits
display annotation of statistical hits beside the heatmap
split rows or columns using rowData(se)
and colData(se)
, respectively
heatmap title to display key options used, for easy reference
data centering can be disabled with centerby_colnames=FALSE
.
alternative hits can be displayed using alt_sestats
. It does not
subset heatmap rows, it inherits rows from sestats
.
display a subset of columns after row centering, useful to hide the control group for certain figures.
option to display correlation heatmap, using the same data centering, then calculates Pearson correlation across sample columns.
labels and legend grids can be customized to exact sizes
with grid::gpar()
and grid::unit()
definitions, for manuscript figures.
mark annotations option to label a subset of rows
row subclusters can be visualized using row_subcluster
to drill down
into specific subclusters from hierarchical clustering, k-means clustering,
or any row_split
.
The intent is to display expression values from assays(se)
,
centered across all columns, or with customization defined by
centerby_colnames
and normgroup_colnames
. The resulting centered
data can be subsetted by argument isamples
, which occurs after
centering in order to decouple the centering step from the display
of resulting data. To subset samples involved in centering itself,
either subset the input se
data, or supply controlSamples
to
define a subset of samples used as the baseline in centering.
See jamma::centerGeneData()
for more details.
Paired data, also called repeated measures data, can be visualized
by including the pairing as centerby_colnames
so that centering
is calculated within each pairing subgroup. In this case if also using
controlSamples
to define a "time zero" or "baseline", then all
baseline samples will have exactly zero, if there is only one replicate
per pairing group at the baseline. In this case, it may be useful
to create the full heatmap once to confirm the centering is performing
as intended, then create a second heatmap using isamples
to show only
the non-baseline samples - thus removing the large chunk of values with 0.
Note: data centering can be disabled with centerby_colnames=FALSE
.
A heatmap title is returned as an attribute attr(hm, "hm_title")
,
which describes:
total rows displayed, with row_type
indicating the measured entity
(gene, probe, DEGs, etc.)
total columns displayed, with column_type
indicating the sampled entity
(samples, total replicates, etc.)
the assay_name
for the data being displayed
relevant options for data centering, for example
"global-centered"
(by default) or
"Centered within Cell Line, versus Wildtype"
To include the heatmap title:
ComplexHeatmap::draw(hm, column_title=attr(hm, "hm_title))
The top heatmap annotations use colData(se)
with user-supplied
top_colnames
or by auto-detecting those colnames that apply
to multiple colnames(se)
.
Colors can be supplied using argument sample_color_list
, as
described below.
The an incidence matrix of statistical hits can be displayed
on the left of the heatmap, using arguments sestats
and alt_sestats
.
These arguments can accept either the output of se_contrast_stats()
,
or they can be a numeric
matrix with values c(-1, 0, 1)
, indicating
statistical hits down, no change, and up, respectively.
The contrasts can optionally be subset with contrast_names
,
which corresponds to columns in the matrix if supplied in that format.
When sestats
is supplied, it will subset all heatmap rows to include
only rows with at least one non-zero value in the incidence matrix.
If argument rows
is supplied, then all rownames(se)
matching
rows
are displayed, regardless of statistical hits.
For comparison across other sestats
results, argument alt_sestats
is treated similar to sestats
except that the heatmap is not subset
based upon these values. That means the heatmap will be subset to
match hits defined by sestats
but not alt_sestats
.
The alt_sestats
incidence matrix is displayed to the far left
of the sestats
incidence matrix. For clarity, it can be useful to
add alt_sestats_suffix
to add a suffix to each contrast label,
for example if sestats
represents limma hits, use
sestats_suffix=" limma"
, and if alt_sestats
represents limma-voom
hits, use alt_sestats_suffix=" limmavoom"
.
Argument rowData_colnames
can be supplied, which enables display of
rowData(se)
annotations in the left_annotation
of the heatmap.
Colors can be supplied using argument sample_color_list
.
Argument sample_color_list
is a list
named by each annotation column
to be displayed as top or left annotation. Each list element is either:
a character
vector of R colors named by character
value, or
a function
defined by circlize::colorRamp2()
to be applied
for numeric
column values. In this case the breaks
used to
define the color function are used to define the color legend.
The function platjam::design2colors()
can be used to create
sample_color_list
starting with a data.frame
of annotations,
and will soon be moved into this package.
A custom left_annotation
can be supplied, but this method currently
prevents the other annotations described above from being displayed.
To display automated annotations with rowData_colnames
and custom
row annotations, supply custom annotations with right_annotation
.
Note that annotations must be supplied in exact row order, which
is usually easiest when supplying rows
with specific set of rows.
Data provided in se
is expected to be SummarizedExperiment
, however
other Bioconductor data types are accepted that provide
accessor functions: featureData()
, phenoData()
, and assayData()
,
including for example the "MethyLumiSet"
class.
Note that matrix
input is currently not supported, however it can
be converted to SummarizedExperiment
like this:
se <- SummarizedExperiment::SummarizedExperiment( assays=list(data=matrix), rowData=data.frame(Gene=rownames(matrix)), colData=data.frame(Sample=colnames(matrix)))
Other jamses heatmaps:
detect_heatmap_components()
,
heatmap_column_group_labels()
se <- make_se_test(nrow=1000, ngroups=4, nreps=8)
# optionally define factor levels to force the order of labels
SummarizedExperiment::rowData(se)$Class <- factor(
sample(head(LETTERS, 5), size=nrow(se), replace=TRUE))
# basic heatmap
hm <- heatmap_se(se, rowData_colnames="Class")
# draw by printing hm, or call draw() to add useful options
ComplexHeatmap::draw(hm,
column_title=attr(hm, "hm_title"),
merge_legends=TRUE)
# define specific colors
sample_color_list <- list(
group=colorjam::group2colors(
unique(SummarizedExperiment::colData(se)$group)),
Class=colorjam::group2colors(
unique(SummarizedExperiment::rowData(se)$Class)))
heatmap_se(se,
rowData_colnames="Class",
sample_color_list=sample_color_list)
# split rows by "Class"
heatmap_se(se,
rowData_colnames="Class",
row_split="Class",
sample_color_list=sample_color_list)
# let's have some fun now
hm2 <- heatmap_se(se,
column_split=c("group"),
column_title_rot=90,
row_split=c("Class"),
rowData_colnames=c("Class"),
cluster_row_slices=FALSE,
sample_color_list=sample_color_list)
hm2drawn <- ComplexHeatmap::draw(hm2, merge_legends=TRUE)
# as an example, extract the row order
# technically you should use hm2drawn, but usually hm2 is enough
hro <- jamba::heatmap_row_order(hm2drawn);
jamba::sdim(hro)
lapply(hro, head, 7)
# (the names will differ from values when `row_labels` are customized)
# center by WildType samples
# - controlSamples
# - control_label
hm2 <- heatmap_se(se,
controlSamples=rownames(subset(
SummarizedExperiment::colData(se), group %in% "groupA")),
control_label="vs groupA",
column_split=c("group"),
column_title_rot=90,
row_split=c("Class"),
rowData_colnames=c("Class"),
cluster_row_slices=FALSE,
sample_color_list=sample_color_list)
hm2drawn <- ComplexHeatmap::draw(hm2,
column_title=attr(hm2, "hm_title"),
merge_legends=TRUE)
# add "callout" labels for a subset of rows
mark_rows <- c(sample(jamba::heatmap_row_order(hm2drawn)[[1]], size=5),
sample(jamba::heatmap_row_order(hm2drawn)[[1]], size=3));
# turn off ComplexHeatmap warning when using RStudio
ComplexHeatmap::ht_opt(message=FALSE)
hm3 <- heatmap_se(se,
mark_rows=mark_rows,
controlSamples=rownames(
subset(SummarizedExperiment::colData(se), group %in% "groupA")),
control_label="vs groupA",
column_split=c("group"),
column_title_rot=90,
row_split=c("Class"),
rowData_colnames=c("Class"),
cluster_row_slices=FALSE,
sample_color_list=sample_color_list)
ComplexHeatmap::draw(hm3,
column_title=attr(hm3, "hm_title"),
merge_legends=TRUE)
# sestats can accept list, incidence matrix, hit_array, or sestats
# this example defines random set of hits
sestats_list <- list(
contrast1=setNames(sample(c(1, -1), replace=TRUE, size=50),
sample(rownames(se), size=50)),
contrast2=setNames(sample(c(1, -1), replace=TRUE, size=50),
sample(rownames(se), size=50)))
hm4 <- heatmap_se(se,
controlSamples=rownames(
subset(SummarizedExperiment::colData(se), group %in% "groupA")),
control_label="vs groupA",
sestats=sestats_list,
column_split=c("group"),
row_split=c("Class"),
rowData_colnames=c("Class"),
cluster_row_slices=FALSE,
sample_color_list=sample_color_list)
ComplexHeatmap::draw(hm4,
column_title=attr(hm4, "hm_title"),
merge_legends=TRUE)
# it doesn't take much effort to run stats really quick
sedesign <- groups_to_sedesign(SummarizedExperiment::colData(se)[, "group", drop=FALSE])
contrast_names(sedesign) <- jamba::vigrep("-groupA", contrast_names(sedesign))
sestats <- se_contrast_stats(se=se,
fold_cutoff=4,
sedesign=sedesign, assay_name="counts")
hm4s <- heatmap_se(se,
controlSamples=rownames(
subset(SummarizedExperiment::colData(se), group %in% "groupA")),
control_label="vs groupA",
sestats=sestats,
column_split=c("group"),
row_split=6,
rowData_colnames=c("Class"),
cluster_row_slices=FALSE,
sample_color_list=sample_color_list)
ComplexHeatmap::draw(hm4s,
column_title=attr(hm4s, "hm_title"),
merge_legends=TRUE)
# for fun, "drill down" into cluster 5
hm4s_4 <- heatmap_se(se,
controlSamples=rownames(
subset(SummarizedExperiment::colData(se), group %in% "groupA")),
control_label="vs groupA",
sestats=sestats,
column_split=c("group"),
row_split=6,
row_subcluster=4,
rowData_colnames=c("Class"),
cluster_row_slices=FALSE,
sample_color_list=sample_color_list)
ComplexHeatmap::draw(hm4s_4,
column_title=attr(hm4s_4, "hm_title"),
merge_legends=TRUE)
# sestats can be provided as an incidence matrix
if (jamba::check_pkg_installed("venndir")) {
# convert sestats to list
sestats_hitlist <- hit_array_to_list(sestats)
# convert sestats hitlist to incidence matrix
# - for fun, use only the first two contrasts
sestats_hitim <- venndir::list2im_value(sestats_hitlist[1:2])
print(head(sestats_hitim));
# convert sestats_list to signed incidence matrix
sestats_im <- venndir::list2im_value(sestats_list)
print(head(sestats_im, 10));
# if the list has items (no direction) use venndir::list2im_opt()
hm5 <- heatmap_se(se,
controlSamples=rownames(
subset(SummarizedExperiment::colData(se), group %in% "groupA")),
control_label="vs groupA",
sestats=sestats_hitim,
column_split=c("group"),
rowData_colnames=c("Class"),
cluster_row_slices=FALSE,
sample_color_list=sample_color_list)
ComplexHeatmap::draw(hm5,
column_title=attr(hm5, "hm_title"),
merge_legends=TRUE)
}
# customize column label fonts using column_names_gp
column_bold <- ifelse(
SummarizedExperiment::colData(se)$group %in% "groupA",
2, 1);
hm6 <- heatmap_se(se,
controlSamples=rownames(
subset(SummarizedExperiment::colData(se), group %in% "groupA")),
control_label="vs WildType",
column_names_gp=grid::gpar(col=sample_color_list$group[
as.character(SummarizedExperiment::colData(se)$group)],
font=column_bold),
column_split=c("group"),
row_split=c("Class"),
rowData_colnames=c("Class"),
cluster_row_slices=FALSE,
sample_color_list=sample_color_list)
ComplexHeatmap::draw(hm6,
column_title=attr(hm6, "hm_title"),
merge_legends=TRUE)
# correlation=TRUE, any heatmap becomes a sample correlation heatmap
hm6corr <- heatmap_se(se,
correlation=TRUE,
apply_hm_column_title=TRUE,
controlSamples=rownames(
subset(SummarizedExperiment::colData(se), group %in% "groupA")),
control_label="vs groupA",
column_names_gp=grid::gpar(col=sample_color_list$group[
as.character(SummarizedExperiment::colData(se)$group)],
font=rep(c(1, 2, 1), c(3, 5, 24))),
column_split=c("Group"),
sample_color_list=sample_color_list)
ComplexHeatmap::draw(hm6corr,
merge_legends=TRUE)
## Final heatmap:
# 1. Applies heatmap title automatically.
# 2. Hides the top_colnames
# 3. Adds fancy grouped labels above the heatmap.
#
# apply_hm_column_title=TRUE
# convenient way to define a title,
# but it does not also display column_split labels
#
# hm_title_buffer=4
# convenient way to insert some whitespace lines
#
# heatmap_column_group_labels()
# adds to a drawn heatmap - it must already be drawn
#
SummarizedExperiment::colData(se)$Genotype <- rep(c("WT", "KO"), each=16);
SummarizedExperiment::colData(se)$Treatment <- rep(c("Control", "Dex"), each=8);
hm7 <- heatmap_se(se,
apply_hm_column_title=TRUE,
hm_title_buffer=3,
controlSamples=rownames(
subset(SummarizedExperiment::colData(se), group %in% "groupA")),
control_label="vs groupA",
sestats=sestats_list,
top_colnames=FALSE,
column_split=c("group"),
row_split=c("Class"),
rowData_colnames=c("Class"),
cluster_row_slices=FALSE,
sample_color_list=sample_color_list)
hm7_drawn <- ComplexHeatmap::draw(hm7,
merge_legends=TRUE)
# now add fancy labels
heatmap_column_group_labels(
hm_group_list=c("Treatment", "Genotype"),
se=se,
hm_drawn=hm7_drawn)
# Note: this step does not work consistently inside RStudio plot pane,
# in that case call dev.new() then run the step above to create hm7_drawn,
# then repeat the step below
#
# adjust the height of labels with argument y_offset_lines
# with positive values (upward), or negative values (downward).
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.