File: test-HeatmapAnnotation.R

package info (click to toggle)
r-bioc-complexheatmap 2.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,632 kB
  • sloc: makefile: 5
file content (305 lines) | stat: -rw-r--r-- 10,544 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
library(circlize)
library(ComplexHeatmap)
library(GetoptLong)


ha = HeatmapAnnotation(foo = 1:10)
ha


ha = HeatmapAnnotation(foo = cbind(1:10, 10:1))
ha
draw(ha, test = "matrix as column annotation")

ha = HeatmapAnnotation(foo = 1:10, bar = sample(c("a", "b"), 10, replace = TRUE),
	pt = anno_points(1:10), annotation_name_side = "left")
draw(ha, test = "complex annotations")

ha = HeatmapAnnotation(foo = 1:10, bar = sample(c("a", "b"), 10, replace = TRUE),
	pt = anno_points(1:10), annotation_name_side = "left", height = unit(8, "cm"))
draw(ha, test = "complex annotations")


ha = HeatmapAnnotation(foo = 1:10, bar = sample(c("a", "b"), 10, replace = TRUE))

ha = HeatmapAnnotation(foo = 1:10, 
	bar = cbind(1:10, 10:1),
	pt = anno_points(1:10),
	gap = unit(2, "mm"))
draw(ha, test = "complex annotations")

ha2 = re_size(ha, annotation_height = unit(1:3, "cm"))
draw(ha2, test = "complex annotations")
ha2 = re_size(ha, annotation_height = 1, height = unit(6, "cm"))
draw(ha2, test = "complex annotations")
ha2 = re_size(ha, annotation_height = 1:3, height = unit(6, "cm"))
draw(ha2, test = "complex annotations")
ha2 = re_size(ha, annotation_height = unit(c(1, 2, 3), c("null", "null", "cm")), height = unit(6, "cm"))
draw(ha2, test = "complex annotations")
ha2 = re_size(ha, annotation_height = unit(c(2, 2, 3), c("cm", "null", "cm")), height = unit(6, "cm"))
draw(ha2, test = "complex annotations")
ha2 = re_size(ha, annotation_height = unit(c(2, 2, 3), c("cm", "cm", "cm")))
draw(ha2, test = "complex annotations")
ha2 = re_size(ha[, 1:2], annotation_height = 1, height = unit(4, "cm"))
draw(ha2, test = "complex annotations")
ha2 = re_size(ha[, 1:2], annotation_height = c(1, 4), height = unit(4, "cm"))
draw(ha2, test = "complex annotations")
ha2 = re_size(ha[, 1:2], height = unit(6, "cm"))
draw(ha2, test = "complex annotations")

ha2 = re_size(ha, height = unit(6, "cm"))
draw(ha2, test = "complex annotations")

#### test anno_empty and self-defined anotation function
ha = HeatmapAnnotation(foo = anno_empty(), height = unit(4, "cm"))
draw(ha, 1:10, test = "anno_empty")
ha = HeatmapAnnotation(foo = anno_empty(), bar = 1:10, height = unit(4, "cm"))
draw(ha, 1:10, test = "anno_empty")
ha = HeatmapAnnotation(foo = anno_empty(), bar = 1:10, height = unit(4, "cm"))
draw(ha, 1:10, test = "anno_empty")

ha = HeatmapAnnotation(foo = function(index) {grid.rect()}, bar = 1:10, height = unit(4, "cm"))
draw(ha, 1:10, test = "self-defined function")


lt = lapply(1:10, function(x) cumprod(1 + runif(1000, -x/100, x/100)) - 1)
ha = HeatmapAnnotation(foo = 1:10, bar = sample(c("a", "b"), 10, replace = TRUE),
	anno = anno_horizon(lt), which = "row")
draw(ha, test = "complex annotations on row")

## test row annotation with no heatmap
rowAnnotation(foo = 1:10, bar = anno_points(10:1))

if(0) {
HeatmapAnnotation(1:10)

HeatmapAnnotation(data.frame(1:10))
}


ha = HeatmapAnnotation(summary = anno_summary(height = unit(4, "cm")))
v = sample(letters[1:2], 50, replace = TRUE)
split = sample(letters[1:2], 50, replace = TRUE)

ht = Heatmap(v, top_annotation = ha, width = unit(1, "cm"), split = split)
draw(ht)

ha = HeatmapAnnotation(summary = anno_summary(gp = gpar(fill = 2:3), height = unit(4, "cm")))
v = rnorm(50)
ht = Heatmap(v, top_annotation = ha, width = unit(1, "cm"), split = split)
draw(ht)


### auto adjust
m = matrix(rnorm(100), 10)
ht_list = Heatmap(m, top_annotation = HeatmapAnnotation(foo = 1:10), column_dend_height = unit(4, "cm")) +
	Heatmap(m, top_annotation = HeatmapAnnotation(bar = anno_points(1:10)),
		cluster_columns = FALSE)
draw(ht_list)

fun = function(index) {
	grid.rect()
}
ha = HeatmapAnnotation(fun = fun, height = unit(4, "cm"))
draw(ha, 1:10, test = TRUE)

ha = rowAnnotation(fun = fun, width = unit(4, "cm"))
draw(ha, 1:10, test = TRUE)


## test anno_mark
m = matrix(rnorm(1000), nrow = 100)
ha1 = rowAnnotation(foo = anno_mark(at = c(1:4, 20, 60, 97:100), labels = month.name[1:10]))
ht = Heatmap(m, name = "mat", cluster_rows = FALSE, right_annotation = ha1)
draw(ht)
ht = Heatmap(m, name = "mat", cluster_rows = FALSE) + ha1
draw(ht)

split = rep("a", 100); split[c(1:4, 20, 60, 98:100)] = "b"
ht = Heatmap(m, name = "mat", cluster_rows = FALSE, right_annotation = ha1, row_split = split, gap = unit(1, "cm"))
draw(ht)
ht = Heatmap(m, name = "mat", cluster_rows = FALSE, row_split = split, gap = unit(1, "cm")) + ha1
draw(ht)

# ha has two annotations
ha2 = rowAnnotation(foo = anno_mark(at = c(1:4, 20, 60, 97:100), labels = month.name[1:10]), bar = 1:100)
ht = Heatmap(m, name = "mat", cluster_rows = FALSE, right_annotation = ha2)
draw(ht)
ht = Heatmap(m, name = "mat", cluster_rows = FALSE) + ha2
draw(ht)

ht = Heatmap(m, name = "mat", cluster_rows = FALSE, right_annotation = ha2, row_split = split, gap = unit(1, "cm"))
draw(ht)
ht = Heatmap(m, name = "mat", cluster_rows = FALSE, row_split = split, gap = unit(1, "cm")) + ha2
draw(ht)

## test anno_mark as column annotation
m = matrix(rnorm(1000), ncol = 100)
ha1 = columnAnnotation(foo = anno_mark(at = c(1:4, 20, 60, 97:100), labels = month.name[1:10]))
ht = Heatmap(m, name = "mat", cluster_columns = FALSE, top_annotation = ha1)
draw(ht)
ht_list = ha1 %v% Heatmap(m, name = "mat", cluster_columns = FALSE)
draw(ht_list)

split = rep("a", 100); split[c(1:4, 20, 60, 98:100)] = "b"
ht = Heatmap(m, name = "mat", cluster_columns = FALSE, top_annotation = ha1, column_split = split, column_gap = unit(1, "cm"))
draw(ht)
ht_list = ha1 %v% Heatmap(m, name = "mat", cluster_columns = FALSE, column_split = split, gap = unit(1, "cm"))
draw(ht_list)

# ha has two annotations
ha2 = HeatmapAnnotation(foo = anno_mark(at = c(1:4, 20, 60, 97:100), labels = month.name[1:10]), bar = 1:100)
ht = Heatmap(m, name = "mat", cluster_columns = FALSE, top_annotation = ha2)
draw(ht)
ht_list = ha2 %v% Heatmap(m, name = "mat", cluster_columns = FALSE)
draw(ht_list)

ht = Heatmap(m, name = "mat", cluster_columns = FALSE, top_annotation = ha2, column_split = split, column_gap = unit(1, "cm"))
draw(ht)
ht_list = ha2 %v% Heatmap(m, name = "mat", cluster_columns = FALSE, column_split = split, column_gap = unit(1, "cm"))
draw(ht_list)


### when there are only simple annotations
col_fun = colorRamp2(c(0, 10), c("white", "blue"))
ha = HeatmapAnnotation(
    foo = cbind(a = 1:10, b = 10:1), 
    bar = sample(letters[1:3], 10, replace = TRUE),
    col = list(foo = col_fun,
               bar = c("a" = "red", "b" = "green", "c" = "blue")
    ),
    simple_anno_size = unit(1, "cm")
)
draw(ha, test = TRUE)

set.seed(123)
mat1 = matrix(rnorm(80, 2), 8, 10)
mat1 = rbind(mat1, matrix(rnorm(40, -2), 4, 10))
rownames(mat1) = paste0("R", 1:12)
colnames(mat1) = paste0("C", 1:10)

mat2 = matrix(runif(60, max = 3, min = 1), 6, 10)
mat2 = rbind(mat2, matrix(runif(60, max = 2, min = 0), 6, 10))
rownames(mat2) = paste0("R", 1:12)
colnames(mat2) = paste0("C", 1:10)

ind = sample(12, 12)
mat1 = mat1[ind, ]
mat2 = mat2[ind, ]

ha1 = HeatmapAnnotation(foo1 = 1:10, 
	                    annotation_height = unit(1, "cm"),
	                    simple_anno_size_adjust = TRUE,
                        annotation_name_side = "left")
ha2 = HeatmapAnnotation(df = data.frame(foo1 = 1:10,
                                        foo2 = 1:10,
                                        foo4 = 1:10,
                                        foo5 = 1:10))
ht1 = Heatmap(mat1, name = "rnorm", top_annotation = ha1)
ht2 = Heatmap(mat2, name = "runif", top_annotation = ha2)

draw(ht1 + ht2)

##### test size of a single simple annotation

ha = HeatmapAnnotation(foo1 = 1:10, 
	simple_anno_size = unit(1, "cm")
)
ha = HeatmapAnnotation(foo1 = 1:10, 
	annotation_height = unit(1, "cm"),
	simple_anno_size_adjust = TRUE
)
ha = HeatmapAnnotation(foo1 = 1:10, 
	height = unit(1, "cm"),
	simple_anno_size_adjust = TRUE
)


## annotation with the same names

set.seed(123)
m = matrix(rnorm(100), 10)
ha1 = HeatmapAnnotation(foo = sample(c("a", "b"), 10, replace = TRUE))
ha2 = HeatmapAnnotation(foo = sample(c("b", "c"), 10, replace = TRUE))

ht_list = Heatmap(m, top_annotation = ha1) + 
	Heatmap(m, top_annotation = ha2)
draw(ht_list)

ha1 = HeatmapAnnotation(foo = sample(c("a", "b"), 10, replace = TRUE),
	annotation_legend_param = list(
		foo = list(title = "letters", 
			       at = c("a", "b", "c"),
			       labels = c("A", "B", "C")
			  )
	))
ha2 = HeatmapAnnotation(foo = sample(c("b", "c"), 10, replace = TRUE))

ht_list = Heatmap(m, top_annotation = ha1) + 
	Heatmap(m, top_annotation = ha2)
draw(ht_list)

x = matrix(rnorm(6), ncol=3)
subtype_col = c("Basal" = "purple","Her2" = "black","Normal" = "blue")
h1 <- HeatmapAnnotation("Subtype" = c("Basal","Her2", "Normal"),
                        col = list("Subtype" = subtype_col))
h2 <- HeatmapAnnotation("Subtype" = c("Normal","Normal", "Basal"),
                        col = list("Subtype" = subtype_col))

ht_list = Heatmap(x,top_annotation = h1) + Heatmap(x,top_annotation = h2)
draw(ht_list)


### test annotation_label
ha = HeatmapAnnotation(foo = 1:10, bar = letters[1:10],
	annotation_label = c("anno1", "anno2"))
draw(ha, test = TRUE)

ha = HeatmapAnnotation(foo = 1:10, bar = letters[1:10],
	annotation_label = list(foo = "anno1"))
draw(ha, test = TRUE)


ha = HeatmapAnnotation(foo = 1:10, bar = letters[1:10],
	annotation_label = list(
		foo = gt_render("foo", gp = gpar(box_fill = "red"))))
draw(ha, test = TRUE)

ha = HeatmapAnnotation(foo = 1:10, bar = letters[1:10],
	annotation_label = list(
		foo = gt_render("foo", gp = gpar(box_fill = "red")),
		bar = gt_render("bar", gp = gpar(box_fill = "blue"))))
draw(ha, test = TRUE)


### test whether arguments can be captured
HeatmapAnnotation(a = 1:10)
rowAnnotation(a = 1:10)
columnAnnotation(a = 1:10)
do.call(HeatmapAnnotation, list(a = 1:10))
do.call(rowAnnotation, list(a = 1:10))
do.call(columnAnnotation, list(a = 1:10))
do.call("HeatmapAnnotation", list(a = 1:10))
do.call("rowAnnotation", list(a = 1:10))
do.call("columnAnnotation", list(a = 1:10))

f = function() HeatmapAnnotation(a = 1:10)
f()
f = function() rowAnnotation(a = 1:10)
f()
f = function() columnAnnotation(a = 1:10)
f()

sapply(1, function(x) HeatmapAnnotation(a = 1:10))
sapply(1, function(x) rowAnnotation(a = 1:10))
sapply(1, function(x) columnAnnotation(a = 1:10))

mapply(function(x, y) HeatmapAnnotation(a = 1:10), list(1), list(1))
mapply(function(x, y) rowAnnotation(a = 1:10), list(1), list(1))
mapply(function(x, y) columnAnnotation(a = 1:10), list(1), list(1))


try({
	HeatmapAnnotation(1:10)
	HeatmapAnnotation(df = data.frame(a = 1:10), a = 1:10)
})