forked from jokergoo/ComplexHeatmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorate.R
executable file
·374 lines (348 loc) · 9.77 KB
/
decorate.R
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# == title
# Decorate Heatmap Bodies
#
# == param
# -heatmap Name of the heatmap which is set as ``name`` argument in `Heatmap` function.
# -code Code that adds graphics in the selected heatmap body.
# -slice Index of the row slice in the heatmap.
# -row_slice Index of the row slice in the heatmap.
# -column_slice Index of the column slice in the heatmap.
# -envir Where to look for variables inside ``code``.
#
# == details
# There is a viewport for each slice in each heatmap.
# This function contructs the name of the viewport,
# goes to the viewport by `grid::seekViewport`, runs the code
# to that viewport and finally goes back to the original viewport.
#
# == value
# This function returns no value.
#
# == seealso
# https://jokergoo.github.io/ComplexHeatmap-reference/book/heatmap-decoration.html
#
# == author
# Zuguang Gu <[email protected]>
#
# == example
# set.seed(123)
# Heatmap(matrix(rnorm(100), 10), name = "mat")
# decorate_heatmap_body("mat", {
# grid.circle(gp = gpar(fill = "#FF000080"))
# })
#
decorate_heatmap_body = function(heatmap, code,
slice = 1, row_slice = slice, column_slice = 1,
envir = new.env(parent = parent.frame())) {
current_vp = current.viewport()$name
if(current_vp == "ROOT") {
current_vp = "global"
}
vp_name = paste0(heatmap, "_heatmap_body_", row_slice, "_", column_slice)
seekViewport(vp_name)
eval(substitute(code), envir = envir)
seekViewport(current_vp)
}
# == title
# Decorate Heatmap Dendrograms
#
# == param
# -heatmap Name of the heatmap.
# -code Code that adds graphics in the selected heatmap dendrogram.
# -slice Index of the row slice or column slice in the heatmap.
# -which Is the dendrogram on rows or on columns?
# -envir Where to look for variables inside ``code``.
#
# == details
#
# If you know the number of leaves in the dendrogram, it is
# simple to calculate the position of every leave in the dendrogram.
# E.g., for the column dendrogram, the i^th leave is located at:
#
# # assume nc is the number of columns in the column slice
# unit((i-0.5)/nc, "npc")
#
#
# == value
# This function returns no value.
#
# == seealso
# https://jokergoo.github.io/ComplexHeatmap-reference/book/heatmap-decoration.html
#
# == author
# Zuguang Gu <[email protected]>
#
# == example
# set.seed(123)
# Heatmap(matrix(rnorm(100), 10), name = "mat", km = 2)
# decorate_dend("mat", {
# grid.rect(gp = gpar(fill = "#FF000080"))
# }, which = "row", slice = 2)
#
decorate_dend = function(heatmap, code, slice = 1, which = c("column", "row"),
envir = new.env(parent = parent.frame())) {
current_vp = current.viewport()$name
if(current_vp == "ROOT") {
current_vp = "global"
}
which = match.arg(which)[1]
vp_name = paste0(heatmap, "_dend_", which, "_", slice)
seekViewport(vp_name)
e = new.env(parent = parent.frame())
eval(substitute(code), envir = envir)
seekViewport(current_vp)
}
# == title
# Decorate Heatmap Column Dendrograms
#
# == param
# -... Pass to `decorate_dend`.
# -envir Where to look for variables inside ``code``.
#
# == details
# This is a wrapper function which pre-defined ``which`` argument in `decorate_dend`.
#
# == value
# The function returns no value.
#
# == author
# Zuguang Gu <[email protected]>
#
decorate_column_dend = function(..., envir = new.env(parent = parent.frame())) {
decorate_dend(..., which = "column", envir = envir)
}
# == title
# Decorate Heatmap Row Dendrograms
#
# == param
# -... Pass to `decorate_dend`.
# -envir Where to look for variables inside ``code``?
#
# == details
# This is a helper function which pre-defined ``which`` argument in `decorate_dend`.
#
# == value
# The function returns no value.
#
# == author
# Zuguang Gu <[email protected]>
#
decorate_row_dend = function(..., envir = new.env(parent = parent.frame())) {
decorate_dend(..., which = "row", envir = envir)
}
# == title
# Decorate Heatmap Dimension Names
#
# == param
# -heatmap Name of the heatmap.
# -code Code that adds graphics in the selected viewport.
# -slice Index of the row slice or column slice in the heatmap.
# -which on rows or on columns?
# -envir where to look for variables inside ``code``.
#
# == details
# If you know the dimensions of the matrix, it is
# simple to calculate the position of every row name or column name in the heatmap.
# E.g., for the column column, the i^th name is located at:
#
# # assume nc is the number of columns in the column slice
# unit((i-0.5)/nc, "npc")
#
#
# == value
# The function returns no value.
#
# == author
# Zuguang Gu <[email protected]>
#
# == example
# set.seed(123)
# mat = matrix(rnorm(100), 10)
# rownames(mat) = letters[1:10]
# colnames(mat) = LETTERS[1:10]
# Heatmap(mat, name = "mat", km = 2)
#
# decorate_dimnames("mat", {
# grid.rect(gp = gpar(fill = "#FF000080"))
# }, which = "row", slice = 2)
#
decorate_dimnames = function(heatmap, code, slice = 1, which = c("column", "row"),
envir = new.env(parent = parent.frame())) {
current_vp = current.viewport()$name
if(current_vp == "ROOT") {
current_vp = "global"
}
which = match.arg(which)[1]
vp_name = paste0(heatmap, "_", which, "_names_", slice)
seekViewport(vp_name)
eval(substitute(code), envir = envir)
seekViewport(current_vp)
}
# == title
# Decorate Heatmap Row Names
#
# == param
# -... Pass to `decorate_dimnames`.
# -envir wWhere to look for variables inside ``code``.
#
# == details
# This is a helper function which pre-defined ``which`` argument in `decorate_dimnames`.
#
# == value
# The function returns no value.
#
# == author
# Zuguang Gu <[email protected]>
#
decorate_row_names = function(..., envir = new.env(parent = parent.frame())) {
decorate_dimnames(..., which = "row", envir = envir)
}
# == title
# Decorate Heatmap Column Names
#
# == param
# -... Pass to `decorate_dimnames`.
# -envir Where to look for variables inside ``code``.
#
# == details
# This is a helper function which pre-defined ``which`` argument in `decorate_dimnames`.
#
# == value
# The function returns no value.
#
# == author
# Zuguang Gu <[email protected]>
#
decorate_column_names = function(..., envir = new.env(parent = parent.frame())) {
decorate_dimnames(..., which = "column", envir = envir)
}
# == title
# Decorate Heatmap Titles
#
# == param
# -heatmap Name of the heatmap.
# -code Code that adds graphics in the selected viewport.
# -slice Index of the row slice or column slice in the heatmap.
# -which Is it a row title or a column title?
# -envir Where to look for variables inside ``code``.
#
# == details
# There is a viewport for row titles and column title in the heatmap.
# This function contructs the name of the viewport,
# goes to the viewport by `grid::seekViewport` , runs code
# to that viewport and finally goes back to the original viewport.
#
# == value
# The function returns no value.
#
# == seealso
# https://jokergoo.github.io/ComplexHeatmap-reference/book/heatmap-decoration.html
#
# == author
# Zuguang Gu <[email protected]>
#
# == example
# set.seed(123)
# Heatmap(matrix(rnorm(100), 10), name = "mat", km = 2)
# decorate_title("mat", {
# grid.rect(gp = gpar(fill = "#FF000080"))
# }, which = "row", slice = 2)
#
decorate_title = function(heatmap, code, slice = 1, which = c("column", "row"),
envir = new.env(parent = parent.frame())) {
current_vp = current.viewport()$name
if(current_vp == "ROOT") {
current_vp = "global"
}
which = match.arg(which)[1]
vp_name = paste0(heatmap, "_", which, "_title_", slice)
seekViewport(vp_name)
eval(substitute(code), envir = envir)
seekViewport(current_vp)
}
# == title
# Decorate Heatmap Row Titles
#
# == param
# -... Pass to `decorate_title`.
# -envir Where to look for variables inside ``code``.
#
# == details
# This is a helper function which pre-defined ``which`` argument in `decorate_title`.
#
# == value
# The function returns no value.
#
# == author
# Zuguang Gu <[email protected]>
#
decorate_row_title = function(..., envir = new.env(parent = parent.frame())) {
decorate_title(..., which = "row", envir = envir)
}
# == title
# Decorate Heatmap Column Titles
#
# == param
# -... Pass to `decorate_title`.
# -envir Where to look for variables inside ``code``.
#
# == details
# This is a helper function which pre-defined ``which`` argument in `decorate_title`.
#
# == value
# The function returns no value.
#
# == author
# Zuguang Gu <[email protected]>
#
decorate_column_title = function(..., envir = new.env(parent = parent.frame())) {
decorate_title(..., which = "column", envir = envir)
}
# == title
# Decorate Heatmap Annotation
#
# == param
# -annotation Name of the annotation.
# -code Code that adds graphics in the selected heatmap annotation.
# -slice Index of the row slices or the column slice in the heatmap.
# -envir Where to look for variables inside ``code``.
#
# == details
# There is a viewport for every column annotation and row annotation.
# This function contructs the name of the viewport,
# goes to the viewport by `grid::seekViewport`, runs code
# to that viewport, and finally goes back to the original viewport.
#
# == value
# The function returns no value.
#
# == seealso
# https://jokergoo.github.io/ComplexHeatmap-reference/book/heatmap-decoration.html
#
# == author
# Zuguang Gu <[email protected]>
#
# == example
# set.seed(123)
# ha1 = HeatmapAnnotation(df = data.frame(type = rep(letters[1:2], 5)))
# ha2 = rowAnnotation(point = anno_points(runif(10), which = "row"))
# Heatmap(matrix(rnorm(100), 10), name = "mat", km = 2,
# top_annotation = ha1) + ha2
# decorate_annotation("type", {
# grid.circle(x = unit(c(0.2, 0.4, 0.6, 0.8), "npc"),
# gp = gpar(fill = "#FF000080"))
# })
# decorate_annotation("point", {
# grid.rect(gp = gpar(fill = "#FF000080"))
# }, slice = 2)
#
decorate_annotation = function(annotation, code, slice = 1, envir = new.env(parent = parent.frame())) {
current_vp = current.viewport()$name
if(current_vp == "ROOT") {
current_vp = "global"
}
vp_name = paste0("annotation_", annotation, "_", slice)
seekViewport(vp_name)
eval(substitute(code), envir = envir)
seekViewport(current_vp)
}