Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

guess which automatically for HeatmapAnnotation #1140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions R/Heatmap-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,12 @@ Heatmap = function(matrix, col, name,
.Object@column_dend_param$cluster_slices = cluster_column_slices

######### annotations #############
# the `top_annotation` will only be evaluated here due to the lazy
# evaluation, we set `.ENV$current_annotation_which` in order to
# HeatmapAnnotation can automatically set its `which` argument.
# the same will be applied for bottom, left, right annotation
old = set_annotation_which("column") # return the initial value
on.exit(set_annotation_which(old), add = TRUE)
.Object@top_annotation = top_annotation # a `HeatmapAnnotation` object
if(is.null(top_annotation)) {
.Object@top_annotation_param$height = unit(0, "mm")
Expand All @@ -858,7 +864,7 @@ Heatmap = function(matrix, col, name,
if(!is.null(top_annotation)) {
validate_anno_names_with_matrix(matrix, top_annotation, "column")
}

set_annotation_which("column")
.Object@bottom_annotation = bottom_annotation # a `HeatmapAnnotation` object
if(is.null(bottom_annotation)) {
.Object@bottom_annotation_param$height = unit(0, "mm")
Expand All @@ -884,7 +890,7 @@ Heatmap = function(matrix, col, name,
if(!is.null(bottom_annotation)) {
validate_anno_names_with_matrix(matrix, bottom_annotation, "column")
}

set_annotation_which("row")
.Object@left_annotation = left_annotation # a `rowAnnotation` object
if(is.null(left_annotation)) {
.Object@left_annotation_param$width = unit(0, "mm")
Expand All @@ -910,7 +916,7 @@ Heatmap = function(matrix, col, name,
if(!is.null(left_annotation)) {
validate_anno_names_with_matrix(matrix, left_annotation, "row")
}

set_annotation_which("row")
.Object@right_annotation = right_annotation # a `rowAnnotation` object
if(is.null(right_annotation)) {
.Object@right_annotation_param$width = unit(0, "mm")
Expand Down
8 changes: 3 additions & 5 deletions R/HeatmapAnnotation-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,10 @@ HeatmapAnnotation = function(...,
is_width_set = !missing(width)
is_annotation_height_set = !missing(annotation_height)
is_annotation_width_set = !missing(annotation_width)

.ENV$current_annotation_which = NULL
which = match.arg(which)[1]
.ENV$current_annotation_which = which
which = get_annotation_which(which)
old = set_annotation_which(which) # return the initial value
on.exit({
.ENV$current_annotation_which <- NULL
.ENV$current_annotation_which <- old
dev.off2()
})

Expand Down
2 changes: 1 addition & 1 deletion R/global.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ ht_opt = setGlobalOptions(
ht_global_opt = function(..., RESET = FALSE, READ.ONLY = NULL, LOCAL = FALSE, ADD = FALSE) {}
ht_global_opt = ht_opt

.ENV = new.env()
.ENV = new.env(parent = emptyenv())
.ENV$current_annotation_which = NULL
.ENV$row_order = NULL
.ENV$row_pos = NULL
Expand Down
14 changes: 14 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ increase_color_mapping_index = function() {
INDEX_ENV$I_COLOR_MAPPING = INDEX_ENV$I_COLOR_MAPPING + 1
}

get_annotation_which = function(which) {
out = .ENV$current_annotation_which
if(is.null(out)) {
out = match.arg(which, c("column", "row"))
}
out
}

set_annotation_which = function(which) {
old = .ENV$current_annotation_which
.ENV$current_annotation_which = which
invisible(old)
}

# default colors for matrix or annotations
# this function should be improved later
default_col = function(x, main_matrix = FALSE) {
Expand Down