Browse code

update

Zuguang Gu authored on 25/10/2021 10:18:45
Showing 19 changed files

... ...
@@ -2,7 +2,7 @@ Package: ComplexHeatmap
2 2
 Type: Package
3 3
 Title: Make Complex Heatmaps
4 4
 Version: 2.9.4
5
-Date: 2021-09-08
5
+Date: 2021-10-17
6 6
 Author: Zuguang Gu
7 7
 Maintainer: Zuguang Gu <z.gu@dkfz.de>
8 8
 Depends: R (>= 3.5.0), methods, grid, graphics, stats, grDevices
... ...
@@ -139,6 +139,7 @@ export("alter_graphic")
139 139
 export("anno_barplot")
140 140
 export("anno_block")
141 141
 export("anno_boxplot")
142
+export("anno_customize")
142 143
 export("anno_density")
143 144
 export("anno_empty")
144 145
 export("anno_histogram")
... ...
@@ -224,6 +225,7 @@ export("set_size")
224 225
 export("smartAlign2")
225 226
 export("subset_gp")
226 227
 export("subset_matrix_by_row")
228
+export("subset_no")
227 229
 export("subset_vector")
228 230
 export("test_alter_fun")
229 231
 export("unify_mat_list")
... ...
@@ -5,6 +5,7 @@ CHANGES in VERSION 2.9.4
5 5
 * Rasterization: the default maximal size for temporary image is set to 30000 px (both for width and height).
6 6
 * add a new argument `beside` in `anno_barplot()` to position bars beside each other.
7 7
 * add `plot()` method for `Heatmap` and `HeatmapList` classes.
8
+* add `anno_customize()`.
8 9
 
9 10
 ========================
10 11
 
... ...
@@ -1,32 +1,32 @@
1
-setGeneric('add_heatmap', function(object, ...) standardGeneric('add_heatmap'))
2
-setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
1
+setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
2
+setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
3
+setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
4
+setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
5
+setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
6
+setGeneric('map_to_colors', function(object, ...) standardGeneric('map_to_colors'))
7
+setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
8
+setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
9
+setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
10
+setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
11
+setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
12
+setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
13
+setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
3 14
 setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
4
-setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
15
+setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
5 16
 setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
6 17
 setGeneric('color_mapping_legend', function(object, ...) standardGeneric('color_mapping_legend'))
7
-setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
8
-setGeneric('draw_dend', function(object, ...) standardGeneric('draw_dend'))
9
-setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
18
+setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
10 19
 setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
11
-setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
12
-setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
13
-setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
20
+setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
14 21
 setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
15
-setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
22
+setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
23
+setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
16 24
 setGeneric('draw_heatmap_legend', function(object, ...) standardGeneric('draw_heatmap_legend'))
17
-setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
18
-setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
19
-setGeneric('map_to_colors', function(object, ...) standardGeneric('map_to_colors'))
20
-setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
21
-setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
22
-setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
23
-setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
24
-setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
25
-setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
26
-setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
27
-setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
28 25
 setGeneric('draw', function(object, ...) standardGeneric('draw'))
29
-setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
26
+setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
27
+setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
28
+setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
30 29
 setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
31
-setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
32
-setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
30
+setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
31
+setGeneric('draw_dend', function(object, ...) standardGeneric('draw_dend'))
32
+setGeneric('add_heatmap', function(object, ...) standardGeneric('add_heatmap'))
... ...
@@ -101,6 +101,18 @@ subset_matrix_by_row = function(x, i) x[i, , drop = FALSE]
101 101
 #
102 102
 subset_vector = function(x, i) x[i]
103 103
 
104
+# == title
105
+# Do not do subseting
106
+#
107
+# == param
108
+# -x A vector.
109
+# -i The indices.
110
+#
111
+# == details
112
+# Mainly used for constructing the `AnnotationFunction-class` object.
113
+#
114
+subset_no = function(x, i) x
115
+
104 116
 # == title
105 117
 # Simple Annotation
106 118
 #
... ...
@@ -4073,4 +4085,119 @@ anno_zoom = function(align_to, panel_fun = function(index, nm = NULL) { grid.rec
4073 4085
 	return(anno)
4074 4086
 }
4075 4087
 
4088
+# == title
4089
+# Customized annotation
4090
+#
4091
+# == param
4092
+# -x A categorical variable.
4093
+# -graphics A list of functions that define graphics for each level in ``x``.
4094
+# -which Is it a row annotation or a column annotation?
4095
+# -width Width of the annotation. The value should be an absolute unit. Width is not allowed to be set for column annotation.
4096
+# -height Height of the annotation. The value should be an absolute unit. Height is not allowed to be set for row annotation.
4097
+# -border Whether to draw border.
4098
+# -verbose Whether to print messages.
4099
+#
4100
+# == details
4101
+# Functions in ``graphics`` define simple graphics drawn in each annotation cell. The function takes four arguments:
4102
+#
4103
+# -x,y Center of the annotation cell.
4104
+# -w,h Width and height of the annotation cell.
4105
+#
4106
+# == value
4107
+# An annotation function which can be used in `HeatmapAnnotation`.
4108
+#
4109
+# == example
4110
+# x = sort(sample(letters[1:3], 10, replace = TRUE))
4111
+# graphics = list(
4112
+#     "a" = function(x, y, w, h) grid.points(x, y, pch = 16),
4113
+#     "b" = function(x, y, w, h) grid.rect(x, y, w*0.8, h*0.8, gp = gpar(fill = "red")),
4114
+#     "c" = function(x, y, w, h) grid.segments(x - 0.5*w, y - 0.5*h, x + 0.5*w, y + 0.5*h, gp = gpar(lty = 2))
4115
+# )
4116
+#
4117
+# anno = anno_customize(x, graphics = graphics)
4118
+#
4119
+# m = matrix(rnorm(100), 10)
4120
+# Heatmap(m, top_annotation = HeatmapAnnotation(bar = x, foo = anno))
4121
+#
4122
+# # Add legends for `foo`
4123
+# ht = Heatmap(m, top_annotation = HeatmapAnnotation(bar = x, foo = anno))
4124
+# lgd = Legend(title = "foo", at = names(graphics), graphics = graphics)
4125
+# draw(ht, annotation_legend_list = list(lgd))
4126
+anno_customize = function(x, graphics = list(), which = c("column", "row"),  
4127
+	border = TRUE, width = NULL, height = NULL, verbose = TRUE) {
4128
+
4129
+	if(is.null(.ENV$current_annotation_which)) {
4130
+		which = match.arg(which)[1]
4131
+	} else {
4132
+		which = .ENV$current_annotation_which
4133
+	}
4134
+
4135
+	anno_size = anno_width_and_height(which, width, height, unit(5, "mm"))
4136
+
4137
+	value = as.character(x)
4138
+	n = length(value)
4139
+
4140
+	if(verbose) {
4141
+		nm = setdiff(value, names(graphics))
4142
+		if(length(nm)) {
4143
+			message(qq("Note: following levels in `x` have no graphics defined:\n    @{paste(nm, collapse = ', ')}.\nSet `verbose = FALSE` in `anno_customize()` to turn off this message."))
4144
+		}
4145
+	}
4146
+
4147
+	row_fun = function(index, k = 1, N = 1) {
4148
+		
4149
+		n = length(index)
4150
+
4151
+		pushViewport(viewport(yscale = c(0.5, n+0.5)))
4152
+		for(i in seq_len(n)) {
4153
+			if(!is.null(graphics[[ value[index[i]] ]])) {
4154
+				fun = graphics[[ value[index[i]] ]]
4155
+				pushViewport(viewport(y = n-i+1, height = 1, default.units = "native"))
4156
+				fun(unit(0.5, "npc"), unit(0.5, "npc"), unit(1, "npc"), unit(1, "npc"))
4157
+				popViewport()
4158
+			}
4159
+		}
4160
+		if(border) grid.rect(gp = gpar(fill = "transparent"))
4161
+		popViewport()
4162
+	}
4163
+
4164
+	column_fun = function(index, k = 1, N = 1) {
4165
+		
4166
+		n = length(index)
4167
+
4168
+		pushViewport(viewport(xscale = c(0.5, n+0.5)))
4169
+		for(i in seq_len(n)) {
4170
+			if(!is.null(graphics[[ value[index[i]] ]])) {
4171
+				fun = graphics[[ value[index[i]] ]]
4172
+				pushViewport(viewport(x = i, width = 1, default.units = "native"))
4173
+				fun(unit(0.5, "npc"), unit(0.5, "npc"), unit(1, "npc"), unit(1, "npc"))
4174
+				popViewport()
4175
+			}
4176
+		}
4177
+		if(border) grid.rect(gp = gpar(fill = "transparent"))
4178
+		popViewport()
4179
+	}
4180
+
4181
+	if(which == "row") {
4182
+		fun = row_fun
4183
+	} else if(which == "column") {
4184
+		fun = column_fun
4185
+	}
4186
+
4187
+	anno = AnnotationFunction(
4188
+		fun = fun,
4189
+		fun_name = "anno_customize",
4190
+		which = which,
4191
+		width = anno_size$width,
4192
+		height = anno_size$height,
4193
+		n = n,
4194
+		var_import = list(value, border, graphics)
4195
+	)
4196
+
4197
+	anno@subset_rule$value = subset_vector
4198
+
4199
+	anno@subsetable = TRUE
4200
+		
4201
+	return(anno) 
4202
+}
4076 4203
 
... ...
@@ -360,7 +360,10 @@ Heatmap = function(matrix, col, name,
360 360
 
361 361
     if(is.data.frame(matrix)) {
362 362
         if(verbose) qqcat("convert data frame to matrix\n")
363
-        warning_wrap("The input is a data frame, convert it to the matrix.")
363
+        warning_wrap("The input is a data frame, convert it to a matrix.")
364
+        if(!all(sapply(matrix, is.numeric))) {
365
+            warning_wrap("Note: not all columns in the data frame are numeric. The data frame will be converted into a character matrix.")
366
+        }
364 367
         matrix = as.matrix(matrix)
365 368
     }
366 369
     fa_level = NULL
... ...
@@ -135,7 +135,7 @@ oncoPrint = function(mat, name,
135 135
 
136 136
 		## check whether there are NA values in the matrix
137 137
 		if(any(is.na(mat))) {
138
-			message_wrap("Found NA values in the matrix and treat as no alteration. If `NA` means no alteration, you can explicitly set it to empty strings like ''. If `NA` is an alteration type, you should format it to a string like `'NA'` and define a graphic for it.")
138
+			message_wrap("Found NA values in the matrix and treat as no alteration. If `NA` means no alteration, you can explicitly set it to empty strings like ''. If `NA` is an alteration type, you should format it to a string like `'NA'` and define graphics for it.")
139 139
 		}
140 140
 
141 141
 		mat_list = lapply(all_type, function(type) {
... ...
@@ -10,9 +10,9 @@ Method dispatch page for \code{add_heatmap}.
10 10
 \code{add_heatmap} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
+\item \code{\link{add_heatmap,Heatmap-method}}, \code{\link{Heatmap-class}} class method
13 14
 \item \code{\link{add_heatmap,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14 15
 \item \code{\link{add_heatmap,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
15
-\item \code{\link{add_heatmap,Heatmap-method}}, \code{\link{Heatmap-class}} class method
16 16
 }
17 17
 }
18 18
 \examples{
19 19
new file mode 100644
... ...
@@ -0,0 +1,52 @@
1
+\name{anno_customize}
2
+\alias{anno_customize}
3
+\title{
4
+Customized annotation
5
+}
6
+\description{
7
+Customized annotation
8
+}
9
+\usage{
10
+anno_customize(x, graphics = list(), which = c("column", "row"),
11
+    border = TRUE, width = NULL, height = NULL, verbose = TRUE)
12
+}
13
+\arguments{
14
+
15
+  \item{x}{A categorical variable.}
16
+  \item{graphics}{A list of functions that define graphics for each level in \code{x}.}
17
+  \item{which}{Is it a row annotation or a column annotation?}
18
+  \item{width}{Width of the annotation. The value should be an absolute unit. Width is not allowed to be set for column annotation.}
19
+  \item{height}{Height of the annotation. The value should be an absolute unit. Height is not allowed to be set for row annotation.}
20
+  \item{border}{Whether to draw border.}
21
+  \item{verbose}{Whether to print messages.}
22
+
23
+}
24
+\details{
25
+Functions in \code{graphics} define simple graphics drawn in each annotation cell. The function takes four arguments:
26
+
27
+\describe{
28
+  \item{x,y}{Center of the annotation cell.}
29
+  \item{w,h}{Width and height of the annotation cell.}
30
+}
31
+}
32
+\value{
33
+An annotation function which can be used in \code{\link{HeatmapAnnotation}}.
34
+}
35
+\examples{
36
+x = sort(sample(letters[1:3], 10, replace = TRUE))
37
+graphics = list(
38
+    "a" = function(x, y, w, h) grid.points(x, y, pch = 16),
39
+    "b" = function(x, y, w, h) grid.rect(x, y, w*0.8, h*0.8, gp = gpar(fill = "red")),
40
+    "c" = function(x, y, w, h) grid.segments(x - 0.5*w, y - 0.5*h, x + 0.5*w, y + 0.5*h, gp = gpar(lty = 2))
41
+)
42
+
43
+anno = anno_customize(x, graphics = graphics)
44
+
45
+m = matrix(rnorm(100), 10)
46
+Heatmap(m, top_annotation = HeatmapAnnotation(bar = x, foo = anno))
47
+
48
+# Add legends for `foo`
49
+ht = Heatmap(m, top_annotation = HeatmapAnnotation(bar = x, foo = anno))
50
+lgd = Legend(title = "foo", at = names(graphics), graphics = graphics)
51
+draw(ht, annotation_legend_list = list(lgd))
52
+}
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{column_dend}.
10 10
 \code{column_dend} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{column_dend,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14 13
 \item \code{\link{column_dend,Heatmap-method}}, \code{\link{Heatmap-class}} class method
14
+\item \code{\link{column_dend,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
15 15
 }
16 16
 }
17 17
 \examples{
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{column_order}.
10 10
 \code{column_order} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{column_order,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14 13
 \item \code{\link{column_order,Heatmap-method}}, \code{\link{Heatmap-class}} class method
14
+\item \code{\link{column_order,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
15 15
 }
16 16
 }
17 17
 \examples{
... ...
@@ -10,11 +10,11 @@ Method dispatch page for \code{draw}.
10 10
 \code{draw} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{draw,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
13
+\item \code{\link{draw,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
14 14
 \item \code{\link{draw,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
15 15
 \item \code{\link{draw,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
16
-\item \code{\link{draw,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
17 16
 \item \code{\link{draw,Heatmap-method}}, \code{\link{Heatmap-class}} class method
17
+\item \code{\link{draw,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
18 18
 \item \code{\link{draw,Legends-method}}, \code{\link{Legends-class}} class method
19 19
 }
20 20
 }
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{draw_title}.
10 10
 \code{draw_title} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{draw_title,Heatmap-method}}, \code{\link{Heatmap-class}} class method
14 13
 \item \code{\link{draw_title,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14
+\item \code{\link{draw_title,Heatmap-method}}, \code{\link{Heatmap-class}} class method
15 15
 }
16 16
 }
17 17
 \examples{
... ...
@@ -18,4 +18,5 @@ Draw heatmap annotations
18 18
 \examples{
19 19
 # There is no example
20 20
 NULL
21
+
21 22
 }
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{row_dend}.
10 10
 \code{row_dend} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{row_dend,Heatmap-method}}, \code{\link{Heatmap-class}} class method
14 13
 \item \code{\link{row_dend,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14
+\item \code{\link{row_dend,Heatmap-method}}, \code{\link{Heatmap-class}} class method
15 15
 }
16 16
 }
17 17
 \examples{
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{row_order}.
10 10
 \code{row_order} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{row_order,Heatmap-method}}, \code{\link{Heatmap-class}} class method
14 13
 \item \code{\link{row_order,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14
+\item \code{\link{row_order,Heatmap-method}}, \code{\link{Heatmap-class}} class method
15 15
 }
16 16
 }
17 17
 \examples{
... ...
@@ -10,11 +10,11 @@ Method dispatch page for \code{show}.
10 10
 \code{show} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
+\item \code{\link{show,ColorMapping-method}}, \code{\link{ColorMapping-class}} class method
13 14
 \item \code{\link{show,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
15
+\item \code{\link{show,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
14 16
 \item \code{\link{show,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
15 17
 \item \code{\link{show,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
16
-\item \code{\link{show,ColorMapping-method}}, \code{\link{ColorMapping-class}} class method
17
-\item \code{\link{show,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
18 18
 \item \code{\link{show,Heatmap-method}}, \code{\link{Heatmap-class}} class method
19 19
 }
20 20
 }
21 21
new file mode 100644
... ...
@@ -0,0 +1,25 @@
1
+\name{subset_no}
2
+\alias{subset_no}
3
+\title{
4
+Do not do subseting
5
+}
6
+\description{
7
+Do not do subseting
8
+}
9
+\usage{
10
+subset_no(x, i)
11
+}
12
+\arguments{
13
+
14
+  \item{x}{A vector.}
15
+  \item{i}{The indices.}
16
+
17
+}
18
+\details{
19
+Mainly used for constructing the \code{\link{AnnotationFunction-class}} object.
20
+}
21
+\examples{
22
+# There is no example
23
+NULL
24
+
25
+}
... ...
@@ -659,3 +659,19 @@ dev.new(width = 3.938326, height = 4.502203)
659 659
 draw(Heatmap(mat, right_annotation = rowAnnotation(foo = anno), 
660 660
 	row_split = row_split))
661 661
 
662
+
663
+
664
+#### anno_custome ###
665
+x = sort(sample(letters[1:3], 10, replace = TRUE))
666
+graphics = list(
667
+	"a" = function(x, y, w, h) grid.points(x, y, pch = 16),
668
+	"b" = function(x, y, w, h) grid.rect(x, y, w*0.8, h*0.8, gp = gpar(fill = "red")),
669
+	"c" = function(x, y, w, h) grid.segments(x - 0.5*w, y - 0.5*h, x + 0.5*w, y + 0.5*h, gp = gpar(lty = 2))
670
+)
671
+
672
+anno = anno_customize(x, graphics = graphics)
673
+draw(anno, index = 1:10, test = "")
674
+
675
+anno = anno_customize(c(x, "d"), graphics = graphics)
676
+
677
+