Skip to content

Commit

Permalink
neighbour annotation grids are merged if they have the same value
Browse files Browse the repository at this point in the history
  • Loading branch information
jokergoo committed Aug 3, 2021
1 parent a5c8bc3 commit 7e46f0a
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 46 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ CHANGES in VERSION 2.9.3
* set `wrap = TRUE` in `grid.grabExpr()` when capturing the legend objects.
* `make_comb_mat()`: support `GRangesList` object as input.
* legends: fixed a bug of the grid heights were not correctedly calculated.
* discrete annotations: neighbour grids are merged into one single grid if they have the
same values.

=========================

Expand Down
48 changes: 24 additions & 24 deletions R/00_S4_generic_methods.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
setGeneric('draw_heatmap_legend', function(object, ...) standardGeneric('draw_heatmap_legend'))
setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
setGeneric('draw', function(object, ...) standardGeneric('draw'))
setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
setGeneric('draw_dend', function(object, ...) standardGeneric('draw_dend'))
setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
setGeneric('map_to_colors', function(object, ...) standardGeneric('map_to_colors'))
setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
setGeneric('add_heatmap', function(object, ...) standardGeneric('add_heatmap'))
setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
setGeneric('draw', function(object, ...) standardGeneric('draw'))
setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
setGeneric('color_mapping_legend', function(object, ...) standardGeneric('color_mapping_legend'))
setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
51 changes: 42 additions & 9 deletions R/AnnotationFunction-function.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ subset_vector = function(x, i) x[i]
# anno = anno_simple(cbind(1:10, 10:1), pch = pch)
# draw(anno, test = "matrix, pch is a matrix with NA values")
anno_simple = function(x, col, na_col = "grey",
which = c("column", "row"), border = FALSE, gp = gpar(col = NA),
which = c("column", "row"), border = FALSE, gp = gpar(),
pch = NULL, pt_size = unit(1, "snpc")*0.8, pt_gp = gpar(),
simple_anno_size = ht_opt$simple_anno_size,
width = NULL, height = NULL) {
Expand Down Expand Up @@ -230,8 +230,16 @@ anno_simple = function(x, col, na_col = "grey",
pch = pch[index, , drop = FALSE]

for(i in seq_len(nc)) {
fill = map_to_colors(color_mapping, value[index, i])
grid.rect(x = (i-0.5)/nc, y, height = 1/n, width = 1/nc, gp = do.call("gpar", c(list(fill = fill), gp)))
if(color_mapping@type == "continuous") {
fill = map_to_colors(color_mapping, value[index, i])
if(is.null(gp$col)) gp$col = fill
grid.rect(x = (i-0.5)/nc, y, height = 1/n, width = 1/nc, gp = do.call("gpar", c(list(fill = fill), gp)))
} else {
r = rle(value[index, i])
fill = map_to_colors(color_mapping, r$values)
if(is.null(gp$col)) gp$col = fill
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)))
}
if(!is.null(pch)) {
l = !is.na(pch[, i])
if(any(l)) {
Expand All @@ -249,8 +257,16 @@ anno_simple = function(x, col, na_col = "grey",
}
}
} else {
fill = map_to_colors(color_mapping, value[index])
grid.rect(x = 0.5, y, height = 1/n, width = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
if(color_mapping@type == "continuous") {
fill = map_to_colors(color_mapping, value[index])
if(is.null(gp$col)) gp$col = fill
grid.rect(x = 0.5, y, height = 1/n, width = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
} else {
r = rle(value[index])
fill = map_to_colors(color_mapping, r$values)
if(is.null(gp$col)) gp$col = fill
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)))
}
if(!is.null(pch)) {
pch = pch[index]
pt_size = pt_size[index]
Expand Down Expand Up @@ -282,8 +298,16 @@ anno_simple = function(x, col, na_col = "grey",
pch = pch[index, , drop = FALSE]

for(i in seq_len(nc)) {
fill = map_to_colors(color_mapping, value[index, i])
grid.rect(x, y = (nc-i +0.5)/nc, width = 1/n, height = 1/nc, gp = do.call("gpar", c(list(fill = fill), gp)))
if(color_mapping@type == "continuous") {
fill = map_to_colors(color_mapping, value[index, i])
if(is.null(gp$col)) gp$col = fill
grid.rect(x, y = (nc-i +0.5)/nc, width = 1/n, height = 1/nc, gp = do.call("gpar", c(list(fill = fill), gp)))
} else {
r = rle(value[index, i])
fill = map_to_colors(color_mapping, r$values)
if(is.null(gp$col)) gp$col = fill
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)))
}
if(!is.null(pch)){
l = !is.na(pch[, i])
if(any(l)) {
Expand All @@ -301,8 +325,16 @@ anno_simple = function(x, col, na_col = "grey",
}
}
} else {
fill = map_to_colors(color_mapping, value[index])
grid.rect(x, y = 0.5, width = 1/n, height = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
if(color_mapping@type == "continuous") {
fill = map_to_colors(color_mapping, value[index])
if(is.null(gp$col)) gp$col = fill
grid.rect(x, y = 0.5, width = 1/n, height = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
} else {
r = rle(value[index])
fill = map_to_colors(color_mapping, r$values)
if(is.null(gp$col)) gp$col = fill
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)))
}
if(!is.null(pch)) {
pch = pch[index]
pt_size = pt_size[index]
Expand Down Expand Up @@ -361,6 +393,7 @@ anno_simple = function(x, col, na_col = "grey",
return(anno)
}


# == title
# Image Annotation
#
Expand Down
2 changes: 1 addition & 1 deletion man/add_heatmap-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Method dispatch page for \code{add_heatmap}.
\code{add_heatmap} can be dispatched on following classes:

\itemize{
\item \code{\link{add_heatmap,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
\item \code{\link{add_heatmap,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{add_heatmap,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{add_heatmap,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/anno_simple.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Simple Annotation
}
\usage{
anno_simple(x, col, na_col = "grey",
which = c("column", "row"), border = FALSE, gp = gpar(col = NA),
which = c("column", "row"), border = FALSE, gp = gpar(),
pch = NULL, pt_size = unit(1, "snpc")*0.8, pt_gp = gpar(),
simple_anno_size = ht_opt$simple_anno_size,
width = NULL, height = NULL)
Expand Down
2 changes: 1 addition & 1 deletion man/component_height-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{component_height}.
\code{component_height} can be dispatched on following classes:

\itemize{
\item \code{\link{component_height,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{component_height,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{component_height,Heatmap-method}}, \code{\link{Heatmap-class}} class method
}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/component_width-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{component_width}.
\code{component_width} can be dispatched on following classes:

\itemize{
\item \code{\link{component_width,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{component_width,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{component_width,Heatmap-method}}, \code{\link{Heatmap-class}} class method
}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/copy_all-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{copy_all}.
\code{copy_all} can be dispatched on following classes:

\itemize{
\item \code{\link{copy_all,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{copy_all,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
\item \code{\link{copy_all,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
}
}
\examples{
Expand Down
6 changes: 3 additions & 3 deletions man/draw-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Method dispatch page for \code{draw}.

\itemize{
\item \code{\link{draw,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{draw,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{draw,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
\item \code{\link{draw,Legends-method}}, \code{\link{Legends-class}} class method
\item \code{\link{draw,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
\item \code{\link{draw,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{draw,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{draw,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
\item \code{\link{draw,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/draw_title-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{draw_title}.
\code{draw_title} can be dispatched on following classes:

\itemize{
\item \code{\link{draw_title,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{draw_title,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{draw_title,Heatmap-method}}, \code{\link{Heatmap-class}} class method
}
}
\examples{
Expand Down
2 changes: 1 addition & 1 deletion man/row_dend-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Method dispatch page for \code{row_dend}.
\code{row_dend} can be dispatched on following classes:

\itemize{
\item \code{\link{row_dend,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{row_dend,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{row_dend,Heatmap-method}}, \code{\link{Heatmap-class}} class method
}
}
\examples{
Expand Down
6 changes: 3 additions & 3 deletions man/show-dispatch.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Method dispatch page for \code{show}.
\code{show} can be dispatched on following classes:

\itemize{
\item \code{\link{show,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
\item \code{\link{show,SingleAnnotation-method}}, \code{\link{SingleAnnotation-class}} class method
\item \code{\link{show,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{show,Heatmap-method}}, \code{\link{Heatmap-class}} class method
\item \code{\link{show,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
\item \code{\link{show,HeatmapAnnotation-method}}, \code{\link{HeatmapAnnotation-class}} class method
\item \code{\link{show,ColorMapping-method}}, \code{\link{ColorMapping-class}} class method
\item \code{\link{show,AnnotationFunction-method}}, \code{\link{AnnotationFunction-class}} class method
\item \code{\link{show,HeatmapList-method}}, \code{\link{HeatmapList-class}} class method
}
}
\examples{
Expand Down

0 comments on commit 7e46f0a

Please sign in to comment.