Browse code

neighbour annotation grids are merged if they have the same value

Zuguang Gu authored on 03/08/2021 08:49:11
Showing 12 changed files

... ...
@@ -5,6 +5,8 @@ CHANGES in VERSION 2.9.3
5 5
 * set `wrap = TRUE` in `grid.grabExpr()` when capturing the legend objects.
6 6
 * `make_comb_mat()`: support `GRangesList` object as input.
7 7
 * legends: fixed a bug of the grid heights were not correctedly calculated.
8
+* discrete annotations: neighbour grids are merged into one single grid if they have the 
9
+  same values.
8 10
 
9 11
 =========================
10 12
 
... ...
@@ -1,32 +1,32 @@
1
-setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
2
-setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
3
-setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
4
-setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
5
-setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
6
-setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
1
+setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
2
+setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
3
+setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
7 4
 setGeneric('draw_heatmap_legend', function(object, ...) standardGeneric('draw_heatmap_legend'))
8
-setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
9
-setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
10
-setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
11
-setGeneric('draw', function(object, ...) standardGeneric('draw'))
12
-setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
13 5
 setGeneric('draw_dend', function(object, ...) standardGeneric('draw_dend'))
14
-setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
6
+setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
15 7
 setGeneric('map_to_colors', function(object, ...) standardGeneric('map_to_colors'))
16
-setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
17
-setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
18
-setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
8
+setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
9
+setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
19 10
 setGeneric('add_heatmap', function(object, ...) standardGeneric('add_heatmap'))
20
-setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
11
+setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
12
+setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
13
+setGeneric('draw', function(object, ...) standardGeneric('draw'))
14
+setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
15
+setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
16
+setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
17
+setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
21 18
 setGeneric('color_mapping_legend', function(object, ...) standardGeneric('color_mapping_legend'))
19
+setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
20
+setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
21
+setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
22 22
 setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
23
-setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
24
-setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
23
+setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
24
+setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
25 25
 setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
26
-setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
27
-setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
28
-setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
29
-setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
30
-setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
26
+setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
27
+setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
28
+setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
29
+setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
30
+setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
31 31
 setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
32
-setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
32
+setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
... ...
@@ -160,7 +160,7 @@ subset_vector = function(x, i) x[i]
160 160
 # anno = anno_simple(cbind(1:10, 10:1), pch = pch)
161 161
 # draw(anno, test = "matrix, pch is a matrix with NA values")
162 162
 anno_simple = function(x, col, na_col = "grey", 
163
-	which = c("column", "row"), border = FALSE, gp = gpar(col = NA),
163
+	which = c("column", "row"), border = FALSE, gp = gpar(),
164 164
 	pch = NULL, pt_size = unit(1, "snpc")*0.8, pt_gp = gpar(), 
165 165
 	simple_anno_size = ht_opt$simple_anno_size,
166 166
 	width = NULL, height = NULL) {
... ...
@@ -230,8 +230,16 @@ anno_simple = function(x, col, na_col = "grey",
230 230
 			pch = pch[index, , drop = FALSE]
231 231
 
232 232
 			for(i in seq_len(nc)) {
233
-			    fill = map_to_colors(color_mapping, value[index, i])
234
-			    grid.rect(x = (i-0.5)/nc, y, height = 1/n, width = 1/nc, gp = do.call("gpar", c(list(fill = fill), gp)))
233
+				if(color_mapping@type == "continuous") {
234
+				    fill = map_to_colors(color_mapping, value[index, i])
235
+				    if(is.null(gp$col)) gp$col = fill
236
+				    grid.rect(x = (i-0.5)/nc, y, height = 1/n, width = 1/nc, gp = do.call("gpar", c(list(fill = fill), gp)))
237
+				} else {
238
+					r = rle(value[index, i])
239
+					fill = map_to_colors(color_mapping, r$values)
240
+					if(is.null(gp$col)) gp$col = fill
241
+					grid.rect(x = (i-0.5)/nc, y = 1 - cumsum(r$lengths)/n, height = r$length/n, width = 1/nc, just = "bottom", gp = do.call("gpar", c(list(fill = fill), gp)))
242
+				}
235 243
 			    if(!is.null(pch)) {
236 244
 					l = !is.na(pch[, i])
237 245
 					if(any(l)) {
... ...
@@ -249,8 +257,16 @@ anno_simple = function(x, col, na_col = "grey",
249 257
 			    }
250 258
 			}
251 259
 	    } else {
252
-			fill = map_to_colors(color_mapping, value[index])
253
-			grid.rect(x = 0.5, y, height = 1/n, width = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
260
+	    	if(color_mapping@type == "continuous") {
261
+				fill = map_to_colors(color_mapping, value[index])
262
+				if(is.null(gp$col)) gp$col = fill
263
+				grid.rect(x = 0.5, y, height = 1/n, width = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
264
+			} else {
265
+				r = rle(value[index])
266
+				fill = map_to_colors(color_mapping, r$values)
267
+				if(is.null(gp$col)) gp$col = fill
268
+				grid.rect(x = 0.5, y = 1 - cumsum(r$lengths)/n, height = r$length/n, width = 1, just = "bottom", gp = do.call("gpar", c(list(fill = fill), gp)))
269
+			}
254 270
 			if(!is.null(pch)) {
255 271
 			    pch = pch[index]
256 272
 			    pt_size = pt_size[index]
... ...
@@ -282,8 +298,16 @@ anno_simple = function(x, col, na_col = "grey",
282 298
 		    pch = pch[index, , drop = FALSE]
283 299
 				  
284 300
 	        for(i in seq_len(nc)) {
285
-                fill = map_to_colors(color_mapping, value[index, i])
286
-                grid.rect(x, y = (nc-i +0.5)/nc, width = 1/n, height = 1/nc, gp = do.call("gpar", c(list(fill = fill), gp)))
301
+	        	if(color_mapping@type == "continuous") {
302
+	                fill = map_to_colors(color_mapping, value[index, i])
303
+	                if(is.null(gp$col)) gp$col = fill
304
+	                grid.rect(x, y = (nc-i +0.5)/nc, width = 1/n, height = 1/nc, gp = do.call("gpar", c(list(fill = fill), gp)))
305
+	            } else {
306
+	            	r = rle(value[index, i])
307
+					fill = map_to_colors(color_mapping, r$values)
308
+					if(is.null(gp$col)) gp$col = fill
309
+					grid.rect(cumsum(r$lengths)/n, y = (nc-i +0.5)/nc, width = r$length/n, height = 1/nc, just = "right", gp = do.call("gpar", c(list(fill = fill), gp)))
310
+	            }
287 311
 				if(!is.null(pch)){
288 312
 				    l = !is.na(pch[, i])
289 313
 				    if(any(l)) {
... ...
@@ -301,8 +325,16 @@ anno_simple = function(x, col, na_col = "grey",
301 325
 				}
302 326
 		    }
303 327
         } else {
304
-			fill = map_to_colors(color_mapping, value[index])
305
-			grid.rect(x, y = 0.5, width = 1/n, height = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
328
+        	if(color_mapping@type == "continuous") {
329
+				fill = map_to_colors(color_mapping, value[index])
330
+				if(is.null(gp$col)) gp$col = fill
331
+				grid.rect(x, y = 0.5, width = 1/n, height = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
332
+			} else {
333
+				r = rle(value[index])
334
+				fill = map_to_colors(color_mapping, r$values)
335
+				if(is.null(gp$col)) gp$col = fill
336
+				grid.rect(cumsum(r$lengths)/n, y = 0.5, width = r$length/n, height = 1, just = "right", gp = do.call("gpar", c(list(fill = fill), gp)))
337
+			}
306 338
 			if(!is.null(pch)) {
307 339
 				pch = pch[index]
308 340
 				pt_size = pt_size[index]
... ...
@@ -361,6 +393,7 @@ anno_simple = function(x, col, na_col = "grey",
361 393
 	return(anno)      
362 394
 }
363 395
 
396
+
364 397
 # == title
365 398
 # Image Annotation
366 399
 #
... ...
@@ -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,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
13 14
 \item \code{\link{add_heatmap,Heatmap-method}}, \code{\link{Heatmap-class}} class method
14 15
 \item \code{\link{add_heatmap,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
15
-\item \code{\link{add_heatmap,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
16 16
 }
17 17
 }
18 18
 \examples{
... ...
@@ -8,7 +8,7 @@ Simple Annotation
8 8
 }
9 9
 \usage{
10 10
 anno_simple(x, col, na_col = "grey",
11
-    which = c("column", "row"), border = FALSE, gp = gpar(col = NA),
11
+    which = c("column", "row"), border = FALSE, gp = gpar(),
12 12
     pch = NULL, pt_size = unit(1, "snpc")*0.8, pt_gp = gpar(),
13 13
     simple_anno_size = ht_opt$simple_anno_size,
14 14
     width = NULL, height = NULL)
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{component_height}.
10 10
 \code{component_height} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{component_height,Heatmap-method}}, \code{\link{Heatmap-class}} class method
14 13
 \item \code{\link{component_height,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14
+\item \code{\link{component_height,Heatmap-method}}, \code{\link{Heatmap-class}} class method
15 15
 }
16 16
 }
17 17
 \examples{
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{component_width}.
10 10
 \code{component_width} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{component_width,Heatmap-method}}, \code{\link{Heatmap-class}} class method
14 13
 \item \code{\link{component_width,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14
+\item \code{\link{component_width,Heatmap-method}}, \code{\link{Heatmap-class}} class method
15 15
 }
16 16
 }
17 17
 \examples{
... ...
@@ -10,8 +10,8 @@ Method dispatch page for \code{copy_all}.
10 10
 \code{copy_all} can be dispatched on following classes:
11 11
 
12 12
 \itemize{
13
-\item \code{\link{copy_all,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
14 13
 \item \code{\link{copy_all,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
14
+\item \code{\link{copy_all,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
15 15
 }
16 16
 }
17 17
 \examples{
... ...
@@ -11,11 +11,11 @@ Method dispatch page for \code{draw}.
11 11
 
12 12
 \itemize{
13 13
 \item \code{\link{draw,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
14
-\item \code{\link{draw,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
15
-\item \code{\link{draw,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
16 14
 \item \code{\link{draw,Legends-method}}, \code{\link{Legends-class}} class method
17
-\item \code{\link{draw,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
18 15
 \item \code{\link{draw,Heatmap-method}}, \code{\link{Heatmap-class}} class method
16
+\item \code{\link{draw,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
17
+\item \code{\link{draw,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
18
+\item \code{\link{draw,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
19 19
 }
20 20
 }
21 21
 \examples{
... ...
@@ -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{
... ...
@@ -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,12 +10,12 @@ 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,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-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,Heatmap-method}}, \code{\link{Heatmap-class}} class method
15
-\item \code{\link{show,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
16
-\item \code{\link{show,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
17 17
 \item \code{\link{show,ColorMapping-method}}, \code{\link{ColorMapping-class}} class method
18
-\item \code{\link{show,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
18
+\item \code{\link{show,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
19 19
 }
20 20
 }
21 21
 \examples{