Skip to content

Commit

Permalink
various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jokergoo committed Oct 23, 2020
1 parent 3a14127 commit 8b0c851
Show file tree
Hide file tree
Showing 24 changed files with 520 additions and 137 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export("SingleAnnotation")
exportMethods("add_heatmap")
exportMethods("adjust_heatmap_list")
exportMethods("annotation_legend_size")
exportMethods("attach_annotation")
exportMethods("color_mapping_legend")
exportMethods("column_dend")
exportMethods("column_order")
Expand Down
5 changes: 4 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ CHANGES in VERSION 2.5.6
can set `at` that are not all in the matrix but are in the `col`.
* padding of the whole plot and spaces of column titles are adjusted to fit ggplot2
* add `row_gap` and `column_gap` in `Legend()`.
* `oncoPrint()`: now draw legends as `alter_fun`.
* `oncoPrint()`: now draw legends the same as `alter_fun`.
* add a new function `attach_annotation()`.
* legends for row annotations can be grouped with column annotation legends.
* annotation name allows rotations.

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

Expand Down
47 changes: 24 additions & 23 deletions R/00_S4_generic_methods.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
setGeneric('row_order', function(object, ...) standardGeneric('row_order'))
setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
setGeneric('color_mapping_legend', function(object, ...) standardGeneric('color_mapping_legend'))
setGeneric('draw_dend', function(object, ...) standardGeneric('draw_dend'))
setGeneric('heatmap_legend_size', function(object, ...) standardGeneric('heatmap_legend_size'))
setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
setGeneric('attach_annotation', function(object, ...) standardGeneric('attach_annotation'))
setGeneric('column_order', function(object, ...) standardGeneric('column_order'))
setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
setGeneric('draw_heatmap_list', function(object, ...) standardGeneric('draw_heatmap_list'))
setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
setGeneric('make_layout', function(object, ...) standardGeneric('make_layout'))
setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
setGeneric('add_heatmap', function(object, ...) standardGeneric('add_heatmap'))
setGeneric('draw_heatmap_legend', function(object, ...) standardGeneric('draw_heatmap_legend'))
setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
setGeneric('make_column_cluster', function(object, ...) standardGeneric('make_column_cluster'))
setGeneric('column_dend', function(object, ...) standardGeneric('column_dend'))
setGeneric('get_color_mapping_list', function(object, ...) standardGeneric('get_color_mapping_list'))
setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
setGeneric('row_dend', function(object, ...) standardGeneric('row_dend'))
setGeneric('draw_heatmap_legend', function(object, ...) standardGeneric('draw_heatmap_legend'))
setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
setGeneric('get_legend_param_list', function(object, ...) standardGeneric('get_legend_param_list'))
setGeneric('set_component_height', function(object, ...) standardGeneric('set_component_height'))
setGeneric('add_heatmap', function(object, ...) standardGeneric('add_heatmap'))
setGeneric('re_size', function(object, ...) standardGeneric('re_size'))
setGeneric('map_to_colors', function(object, ...) standardGeneric('map_to_colors'))
setGeneric('component_width', function(object, ...) standardGeneric('component_width'))
setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
setGeneric('copy_all', function(object, ...) standardGeneric('copy_all'))
setGeneric('set_component_width', function(object, ...) standardGeneric('set_component_width'))
setGeneric('draw_heatmap_body', function(object, ...) standardGeneric('draw_heatmap_body'))
setGeneric('draw_title', function(object, ...) standardGeneric('draw_title'))
setGeneric('map_to_colors', function(object, ...) standardGeneric('map_to_colors'))
setGeneric('component_height', function(object, ...) standardGeneric('component_height'))
setGeneric('draw_dimnames', function(object, ...) standardGeneric('draw_dimnames'))
setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
setGeneric('draw_annotation', function(object, ...) standardGeneric('draw_annotation'))
setGeneric('draw_annotation_legend', function(object, ...) standardGeneric('draw_annotation_legend'))
setGeneric('draw_dend', function(object, ...) standardGeneric('draw_dend'))
setGeneric('draw', function(object, ...) standardGeneric('draw'))
setGeneric('adjust_heatmap_list', function(object, ...) standardGeneric('adjust_heatmap_list'))
setGeneric('make_row_cluster', function(object, ...) standardGeneric('make_row_cluster'))
setGeneric('prepare', function(object, ...) standardGeneric('prepare'))
setGeneric('annotation_legend_size', function(object, ...) standardGeneric('annotation_legend_size'))
91 changes: 89 additions & 2 deletions R/HeatmapAnnotation-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ HeatmapAnnotation = setClass("HeatmapAnnotation",
# -annotation_name_gp Graphic parameters for anntation names. Graphic paramters can be vectors.
# -annotation_name_offset Offset to the annotation names, a `grid::unit` object. The value can be a vector.
# -annotation_name_side Side of the annotation names.
# -annotation_name_rot Rotation of the annotation names, it can only take values in ``c(00, 90, 180, 270)``. The value can be a vector.
# -annotation_name_rot Rotation of the annotation names. The value can be a vector.
# -annotation_name_align Whether to align the annotation names.
# -annotation_height Height of each annotation if annotations are column annotations.
# -annotation_width Width of each annotation if annotations are row annotations.
Expand Down Expand Up @@ -900,7 +900,7 @@ setMethod(f = "add_heatmap",
# ha3 = HeatmapAnnotation(sth = cbind(1:10, 10:1))
# ha = c(ha1, ha2, ha3, gap = unit(c(1, 4), "mm"))
# ha
c.HeatmapAnnotation = function(..., gap = unit(0, "mm")) {
c.HeatmapAnnotation = function(..., gap = unit(1, "points")) {
anno_list = list(...)
if(length(anno_list) == 1) {
return(anno_list[[1]])
Expand Down Expand Up @@ -1410,3 +1410,90 @@ has_zoomed_anno_empty = function(ha) {
}
return(FALSE)
}

# == title
# Attach heatmap annotations to the heatmap
#
# == param
# -object A `Heatmap-class` object.
# -ha A `HeatmapAnnotation-class` object.
# -side Which side of the heatmap. Value should be in "top", "bottom", "left", "right".
# -gap Space between the two heatmap annotations.
#
# == example
# m = matrix(rnorm(100), 10)
# ht = Heatmap(m)
# ha = HeatmapAnnotation(foo = 1:10)
# ht = attach_annotation(ht, ha)
# ht
# ha2 = HeatmapAnnotation(bar = letters[1:10])
# ht = attach_annotation(ht, ha2)
# ht
setMethod(f = "attach_annotation",
signature = "Heatmap",
definition = function(object, ha, side = c("top", "bottom", "left", "right"),
gap = unit(1, "points")) {

if(missing(side)) {
side = ifelse(ha@which == "column", "top", "left")
} else {
side = match.arg(side)[1]
}
ha_which = ha@which
if(ha_which == "column" && side %in% c("left", "right")) {
stop_wrap("Column annotations can only be attached to the top/bottom side of the heatmap.")
} else if(ha_which == "row" && side %in% c("top", "bottom")) {
stop_wrap("Row annotations can only be attached to the left/right side of the heatmap.")
}

if(side == "top") {
if(is.null(object@top_annotation)) {
object@top_annotation = ha
h = height(ha) + ht_opt$COLUMN_ANNO_PADDING
h = convertHeight(h, "mm")
object@top_annotation_param = list(height = h)
} else {
object@top_annotation = c(object@top_annotation, ha, gap = gap)
h = height(object@top_annotation) + height(ha) + gap
h = convertHeight(h, "mm")
object@top_annotation_param = list(height = h)
}
} else if(side == "bottom") {
if(is.null(object@bottom_annotation)) {
object@bottom_annotation = ha
h = height(ha) + ht_opt$COLUMN_ANNO_PADDING
h = convertHeight(h, "mm")
object@bottom_annotation_param = list(height = h)
} else {
object@bottom_annotation = c(object@bottom_annotation, ha, gap = gap)
h = height(object@bottom_annotation) + height(ha) + gap
h = convertHeight(h, "mm")
object@bottom_annotation_param = list(height = h)
}
} else if(side == "left") {
if(is.null(object@left_annotation)) {
object@left_annotation = ha
w = width(ha) + ht_opt$ROW_ANNO_PADDING
w = convertWidth(w, "mm")
object@left_annotation_param = list(width = w)
} else {
object@left_annotation = c(object@left_annotation, ha, gap = gap)
w = width(object@left_annotation) + width(ha) + gap
w = convertWidth(w, "mm")
object@left_annotation_param = list(width = w)
}
} else if(side == "right") {
if(is.null(object@right_annotation)) {
object@right_annotation = ha
w = width(ha) + ht_opt$ROW_ANNO_PADDING
w = convertWidth(w, "mm")
object@right_annotation_param = list(width = w)
} else {
object@right_annotation = c(object@right_annotation, ha, gap = gap)
w = width(object@right_annotation) + width(ha) + gap
w = convertWidth(w, "mm")
object@right_annotation_param = list(height = w)
}
}
object
})
4 changes: 4 additions & 0 deletions R/HeatmapList-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ setMethod(f = "add_heatmap",
# -align_heatmap_legend How to align the legends to heatmap. Possible values are "heatmap_center", "heatmap_top" and "global_center". If the value is ``NULL``,
# it automatically picks the proper value from the three options.
# -align_annotation_legend How to align the legends to heatmap. Possible values are "heatmap_center", "heatmap_top" and "global_center".
# -legend_grouping How the legends are grouped. Values should be "adjusted" or "original". If it is set as
# "original", all annotation legends are grouped together.
# -gap gap between heatmaps/annotations
# -ht_gap same as ``gap``.
# -main_heatmap index of main heatmap. The value can be a numeric index or the heatmap name
Expand Down Expand Up @@ -320,6 +322,7 @@ setMethod(f = "draw",
annotation_legend_list = list(),
align_heatmap_legend = NULL,
align_annotation_legend = NULL,
legend_grouping = c("adjusted", "original"),

gap = unit(2, "mm"),
ht_gap = gap,
Expand Down Expand Up @@ -513,6 +516,7 @@ setMethod(f = "draw",
annotation_legend_list = annotation_legend_list,
align_heatmap_legend = align_heatmap_legend,
align_annotation_legend = align_annotation_legend,
legend_grouping = legend_grouping,

ht_gap = ht_gap,

Expand Down
Loading

0 comments on commit 8b0c851

Please sign in to comment.