Skip to content

Commit c62cc30

Browse files
committed
Add some options to plot_heatmap.
1 parent f97f683 commit c62cc30

File tree

14 files changed

+121
-73
lines changed

14 files changed

+121
-73
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Description: Transform RNA-Seq count data so that variance due to biological
1010
Authors@R: person("Paul", "Harrison", email = "[email protected]", role = c("aut", "cre"))
1111
Maintainer: Paul Harrison <[email protected]>
1212
URL: https://github.com/MonashBioinformaticsPlatform/varistran
13-
Version: 1.0.4
13+
Version: 1.0.5
1414
License: LGPL-2.1 | file LICENSE
1515
Depends:
1616
grid
@@ -27,4 +27,5 @@ Suggests:
2727
DESeq2,
2828
biomaRt,
2929
NBPSeq
30-
RoxygenNote: 7.1.0
30+
RoxygenNote: 7.3.2
31+
Encoding: UTF-8

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ paper.pdf : paper.md paper.bib
3434
pandoc --filter pandoc-citeproc -o paper.pdf paper.md
3535

3636

37-
test : test_advice test_biplot test_heatmap test_pathological test_vst_plot test_vst_stability_plots
37+
test : test_advice test_biplot test_heatmap test_heatmap_big test_pathological test_vst_plot test_vst_stability_plots
3838

3939
test_advice :
4040
Rscript test/advice.R
@@ -45,6 +45,9 @@ test_biplot :
4545
test_heatmap :
4646
Rscript test/heatmap.R
4747

48+
test_heatmap_big :
49+
Rscript test/heatmap_big.R
50+
4851
test_pathological :
4952
Rscript test/pathological.R
5053

NEWS

Lines changed: 0 additions & 26 deletions
This file was deleted.

NEWS.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# varistran 1.0.5
2+
3+
* plot_heatmap now has parameters baseline_to and scale_to giving control over the ranges of the scales, and parameters show_baseline and show_tree giving control over what parts of the plot are shown.
4+
5+
# varistran 1.0.4
6+
7+
* Increase robustness of heatmap to missing values.
8+
* Baseline plot is not shown in heatmap if all zero.
9+
10+
# varistran 1.0.3
11+
12+
* No code changes. Updated READEME with references and supporting/contributing section.
13+
14+
# 1.0.2
15+
16+
* Added n parameter to plot_heatmap, to show only the top n rows by span of expression levels.
17+
* Remove dependency on ggdenro, which is not available in R 3.4.1.
18+
* Added packages needed for testing to suggested dependencies: biomaRt, DESeq2, NBPSeq
19+

R/grid_util.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,18 @@ signed_colors <- hsv(
175175
#'
176176
#' @param vp_name Viewport name for heatmap grob. This is useful if you will need to refer to the grob later, for example for interaction.
177177
#'
178+
#' @param to Heatmap scale will include this value if given.
179+
#'
178180
#' @return A list containing a heatmap grob and associated legend grob.
179181
#'
180182
#' @export
181-
heatmap_grob <- function(data, signed=TRUE, legend_title="", vp_name=NULL) {
183+
heatmap_grob <- function(data, signed=TRUE, legend_title="", vp_name=NULL, to=NULL) {
182184
if (signed) {
183-
radius <- max(abs(data), na.rm=TRUE)
185+
radius <- max(abs(c(data, to)), na.rm=TRUE)
184186
range <- c(-radius, radius)
185187
col <- signed_colors
186188
} else {
187-
range <- c(0, max(data, na.rm=TRUE))
189+
range <- c(0, max(data,to, na.rm=TRUE))
188190
col <- unsigned_colors
189191
}
190192

R/heatmap.R

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#'
1212
#' 3. Branches in the hierarchical clustering are flipped to minimize sharp changes between neighbours, using the seriation package's OLO (Optimal Leaf Ordering) method.
1313
#'
14+
#'
1415
#' @param y A matrix of expression levels, such as a transformed counts matrix as produced by \code{varistran::vst}.
1516
#' @param cluster_samples Should samples (columns) be clustered?
1617
#' @param cluster_features Should features (rows) be clustered?
@@ -20,6 +21,10 @@
2021
#' @param baseline_label Text description of what the baseline is.
2122
#' @param scale_label Text description of what the heatmap colors represent (after baseline is subtracted).
2223
#' @param n Show only this many rows. Rows are selected in order of greatest span of expression level.
24+
#' @param baseline_to If provided, the scale for the row means or baseline will include this value or these values. Use this if there is some meaningful "zero" for your data.
25+
#' @param scale_to If provided, the heatmap color scale will include this value. Use this to provide consistency of scales between heatmaps (note values larger than scale_to will cause the scale to be extended).
26+
#' @param show_baseline Show baseline barplot?
27+
#' @param show_tree Show dendrogram tree(s)? These dendrograms arguably over-interpret the data without adding much of values, so it may be better to hide them.
2328
#'
2429
#' @return A grid grob. print()-ing this value will cause it to be displayed.
2530
#'
@@ -45,26 +50,29 @@ plot_heatmap <- function(
4550
baseline=NULL,
4651
baseline_label="row\nmean",
4752
scale_label="difference from\nrow mean",
48-
n=Inf) {
53+
n=Inf,
54+
baseline_to=NULL,
55+
scale_to=NULL,
56+
show_baseline=TRUE,
57+
show_tree=TRUE) {
4958
y <- as.matrix(y)
50-
59+
5160
if (is.null(sample_labels) && !is.null(colnames(y)))
5261
sample_labels <- colnames(y)
53-
62+
5463
if (is.null(sample_labels))
5564
sample_labels <- rep("", ncol(y))
56-
65+
5766
sample_labels[is.na(sample_labels)] <- ""
58-
59-
67+
6068
if (is.null(feature_labels) && !is.null(rownames(y)))
6169
feature_labels <- rownames(y)
62-
70+
6371
if (is.null(feature_labels))
6472
feature_labels <- rep("", nrow(y))
65-
73+
6674
feature_labels[is.na(feature_labels)] <- ""
67-
75+
6876
if (!is.null(baseline)) {
6977
if (length(baseline) == 1)
7078
baseline <- rep(baseline, nrow(y))
@@ -73,7 +81,7 @@ plot_heatmap <- function(
7381
} else {
7482
means <- rowMeans(y, na.rm=TRUE)
7583
}
76-
84+
7785
# Show only a subset of rows, if desired
7886
if (n < nrow(y)) {
7987
y_span <- apply(y,1,max,-Inf,na.rm=TRUE) - apply(y,1,min,Inf,na.rm=TRUE)
@@ -85,46 +93,49 @@ plot_heatmap <- function(
8593
feature_labels <- feature_labels[selection]
8694
means <- means[selection]
8795
}
88-
89-
96+
9097
y_centered <- y - means
9198

9299
y_scaled <- y_centered / sqrt(rowMeans(y_centered*y_centered, na.rm=TRUE))
93100
y_scaled[ is.na(y_scaled) ] <- 0.0
94101

95102
row_order <- make_ordering(y_scaled, enable=cluster_features)
96-
103+
97104
y_centered_clean <- y_centered
98105
y_centered_clean[ is.na(y_centered_clean) ] <- 0.0
99106
col_order <- make_ordering(t(y_centered_clean), enable=cluster_samples)
100-
107+
101108
pad <- 0.25
102-
103-
row_ordering_grob <- ordering_grob(row_order, transpose=TRUE, mirror=TRUE)
104-
105-
col_ordering_grob <- ordering_grob(col_order)
106-
109+
110+
if (show_tree) {
111+
row_ordering_grob <- ordering_grob(row_order, transpose=TRUE, mirror=TRUE)
112+
col_ordering_grob <- ordering_grob(col_order)
113+
} else {
114+
row_ordering_grob <- nullGrob()
115+
col_ordering_grob <- nullGrob()
116+
}
117+
107118
heatmap <- heatmap_grob(
108119
y_centered[row_order$order,col_order$order,drop=F],
109120
signed=TRUE,
110121
legend_title=paste0(scale_label),
111-
vp_name="heatmap")
112-
113-
mean_range <- range(means, na.rm=TRUE)
122+
vp_name="heatmap",
123+
to=scale_to)
114124

115-
need_means <- mean_range[1] != 0 || mean_range[2] != 0
125+
mean_range <- range(means, baseline_to, na.rm=TRUE)
116126

117127
if (mean_range[2] == mean_range[1])
118128
mean_range[2] <- mean_range[2]+1
119129

120-
if (need_means) {
130+
if (show_baseline) {
121131
mean_graph <- rectGrob(
122132
x=rep(mean_range[1],nrow(y)),
123133
y=seq_len(nrow(y))-1,
124134
width=means[row_order$order]-mean_range[1],
125135
height=rep(1,nrow(y)),
126136
just=c(0,0),
127137
default.units="native",
138+
gp=gpar(col=NA, fill="#aaaaaa"),
128139
vp=viewport(xscale=mean_range,yscale=c(0,nrow(y)))
129140
)
130141
mean_axis <- xaxisGrob(
@@ -159,24 +170,24 @@ plot_heatmap <- function(
159170
just=c(1,0.5),
160171
vp=viewport(xscale=c(0,ncol(y)),yscale=c(0,1))
161172
)
162-
173+
163174
frame <- frameGrob(layout=grid.layout(nrow=3,ncol=4))
164-
175+
165176
frame <- packGrob(frame, varistran_grob(col_ordering_grob,height="inherit",pad=pad), row=1,col=2)
166177
frame <- packGrob(frame, varistran_grob(mean_label,height="inherit",pad=mean_pad), row=1,col=3)
167-
178+
168179
frame <- packGrob(frame, varistran_grob(row_ordering_grob,width="inherit",pad=pad), row=2,col=1)
169180
frame <- packGrob(frame, varistran_grob(heatmap$heatmap,pad=pad), row=2, col=2)
170181
frame <- packGrob(frame, varistran_grob(mean_graph,width=mean_width,pad=mean_pad), row=2,col=3)
171182
frame <- packGrob(frame, varistran_grob(feature_label_grob,width="inherit",pad=pad), row=2,col=4)
172-
183+
173184
frame <- packGrob(frame, varistran_grob(sample_label_grob,height="inherit",pad=pad), row=3,col=2)
174185
frame <- packGrob(frame, varistran_grob(mean_axis,height=unit(3,"lines"),pad=mean_pad), row=3,col=3)
175-
186+
176187
outer <- frameGrob()
177188
outer <- packGrob(outer, varistran_grob(frame), row=1,col=1)
178189
outer <- packGrob(outer, varistran_grob(heatmap$legend,height="inherit",pad=pad), row=2, col=1)
179-
190+
180191
result <- varistran_grob(outer, pad=pad)
181192
result$info <- list(
182193
row_order=row_order,

R/shiny_plots.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ shiny_heatmap <- function(y, sample_labels=NULL, feature_labels=NULL, prefix="")
209209
shiny::p("Features are selected based on span of expression levels."),
210210
shiny::numericInput(ns("n"), "Number of features to show", 50, min=10,max=2000,step=10),
211211
shiny::checkboxInput(ns("cluster_samples"), "Cluster samples", FALSE),
212+
shiny::checkboxInput(ns("show_tree"), "Show tree(s)", TRUE),
212213
call_ui(plot$component_ui, request),
213214
#shiny::uiOutput(ns("selected_text")),
214215
parenthetically("This plot is produced by varistran::plot_heatmap.")
@@ -222,14 +223,14 @@ shiny_heatmap <- function(y, sample_labels=NULL, feature_labels=NULL, prefix="")
222223
env[[ns("selection")]] <- shiny::reactive({
223224
n <- env$input[[ns("n")]]
224225
if (n > 2000) stop("Drawing large heatmaps uses excessive system resources. Sorry.")
225-
226+
226227
y_span <- apply(y_val(),1,max) - apply(y_val(),1,min)
227228
selection <- rep(FALSE,nrow(y_val()))
228229
selection[ order(-y_span)[ seq_len(n) ] ] <- TRUE
229230
selection <- which(selection)
230231

231232
if (length(selection) < 1) stop("No features to show.")
232-
233+
233234
selection
234235
})
235236

@@ -238,7 +239,8 @@ shiny_heatmap <- function(y, sample_labels=NULL, feature_labels=NULL, prefix="")
238239
y=y_val()[e("selection"),,drop=FALSE],
239240
sample_labels=sample_labels(env),
240241
feature_labels=feature_labels(env)[e("selection")],
241-
cluster_samples=env$input[[ns("cluster_samples")]]
242+
cluster_samples=env$input[[ns("cluster_samples")]],
243+
show_tree=env$input[[ns("show_tree")]]
242244
)
243245
})
244246

R/shiny_util.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ ensure_reactive <- function(item, name=NULL, env=NULL, default=function() stop("
6464

6565
#' Call object with "request" if it is callable.
6666
#'
67-
#' This is used to support older Shiny UI code which doesn't wrap UI in function(request) { ... }.
67+
#' This is used to support older Shiny UI code which doesn't wrap UI in \code{function(request) { ... }}.
6868
#'
6969
#' @param ui A UI object, or preferably a function(request) to produce a UI object.
7070
#'

man/call_ui.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/heatmap_grob.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)