Skip to content

Commit 57c676b

Browse files
committed
Improve heatmap robustness
1 parent 807a4ce commit 57c676b

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
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.2
13+
Version: 1.0.3
1414
License: LGPL-2.1 | file LICENSE
1515
Depends:
1616
grid

NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11

2+
1.0.3
3+
=====
4+
5+
Increase robustness of heatmap to missing values.
6+
7+
Baseline plot is not shown in heatmap if all zero.
8+
29
1.0.2
310
=====
411

R/heatmap.R

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ plot_heatmap <- function(
5858

5959
# Show only a subset of rows, if desired
6060
if (n < nrow(y)) {
61-
y_span <- apply(y,1,max) - apply(y,1,min)
61+
y_span <- apply(y,1,max,-Inf,na.rm=TRUE) - apply(y,1,min,Inf,na.rm=TRUE)
62+
y_span[ !is.finite(y_span) ] <- -Inf
6263
selection <- rep(FALSE,nrow(y))
6364
selection[ order(-y_span)[ seq_len(n) ] ] <- TRUE
6465

@@ -91,25 +92,40 @@ plot_heatmap <- function(
9192
legend_title=paste0(scale_label),
9293
vp_name="heatmap")
9394

94-
mean_range <- range(means)
95-
if (mean_range[2] == mean_range[1]) mean_range[2] <- mean_range[2]+1
96-
mean_graph <- rectGrob(
97-
x=rep(mean_range[1],nrow(y)),
98-
y=seq_len(nrow(y))-1,
99-
width=means[row_order$order]-mean_range[1],
100-
height=rep(1,nrow(y)),
101-
just=c(0,0),
102-
default.units="native",
103-
vp=viewport(xscale=mean_range,yscale=c(0,nrow(y)))
104-
)
105-
mean_axis <- xaxisGrob(
106-
at=axisTicks(mean_range,log=FALSE,nint=3),
107-
label=TRUE,
108-
vp=viewport(width=1,height=0,y=1,xscale=mean_range),
109-
gp=gpar(cex=0.75)
110-
)
111-
mean_label <- textGrob(baseline_label)
112-
95+
mean_range <- range(means, na.rm=TRUE)
96+
97+
need_means <- mean_range[1] != 0 || mean_range[2] != 0
98+
99+
if (mean_range[2] == mean_range[1])
100+
mean_range[2] <- mean_range[2]+1
101+
102+
if (need_means) {
103+
mean_graph <- rectGrob(
104+
x=rep(mean_range[1],nrow(y)),
105+
y=seq_len(nrow(y))-1,
106+
width=means[row_order$order]-mean_range[1],
107+
height=rep(1,nrow(y)),
108+
just=c(0,0),
109+
default.units="native",
110+
vp=viewport(xscale=mean_range,yscale=c(0,nrow(y)))
111+
)
112+
mean_axis <- xaxisGrob(
113+
at=axisTicks(mean_range,log=FALSE,nint=3),
114+
label=TRUE,
115+
vp=viewport(width=1,height=0,y=1,xscale=mean_range),
116+
gp=gpar(cex=0.75)
117+
)
118+
mean_label <- textGrob(baseline_label)
119+
mean_width <- unit(3,"lines")
120+
mean_pad <- pad
121+
} else {
122+
mean_graph <- textGrob("")
123+
mean_axis <- textGrob("")
124+
mean_label <- textGrob("")
125+
mean_width <- unit(0,"lines")
126+
mean_pad <- 0
127+
}
128+
113129
feature_label_grob <- shrinktext_grob(
114130
feature_labels[row_order$order],
115131
x=rep(0,nrow(y)),
@@ -129,15 +145,15 @@ plot_heatmap <- function(
129145
frame <- frameGrob(layout=grid.layout(nrow=3,ncol=4))
130146

131147
frame <- packGrob(frame, varistran_grob(col_ordering_grob,height="inherit",pad=pad), row=1,col=2)
132-
frame <- packGrob(frame, varistran_grob(mean_label,height="inherit",pad=pad), row=1,col=3)
148+
frame <- packGrob(frame, varistran_grob(mean_label,height="inherit",pad=mean_pad), row=1,col=3)
133149

134150
frame <- packGrob(frame, varistran_grob(row_ordering_grob,width="inherit",pad=pad), row=2,col=1)
135151
frame <- packGrob(frame, varistran_grob(heatmap$heatmap,pad=pad), row=2, col=2)
136-
frame <- packGrob(frame, varistran_grob(mean_graph,width=unit(3,"lines"),pad=pad), row=2,col=3)
152+
frame <- packGrob(frame, varistran_grob(mean_graph,width=mean_width,pad=mean_pad), row=2,col=3)
137153
frame <- packGrob(frame, varistran_grob(feature_label_grob,width="inherit",pad=pad), row=2,col=4)
138154

139155
frame <- packGrob(frame, varistran_grob(sample_label_grob,height="inherit",pad=pad), row=3,col=2)
140-
frame <- packGrob(frame, varistran_grob(mean_axis,height=unit(3,"lines"),pad=pad), row=3,col=3)
156+
frame <- packGrob(frame, varistran_grob(mean_axis,height=unit(3,"lines"),pad=mean_pad), row=3,col=3)
141157

142158
outer <- frameGrob()
143159
outer <- packGrob(outer, varistran_grob(frame), row=1,col=1)

0 commit comments

Comments
 (0)