From 6f6b4866599d20f8f03c1af415cd1c41ddf78dbd Mon Sep 17 00:00:00 2001 From: yun Date: Fri, 24 Nov 2023 15:43:00 +0800 Subject: [PATCH] guess which automatically --- R/Heatmap-class.R | 12 +++++++++--- R/HeatmapAnnotation-class.R | 8 +++----- R/global.R | 2 +- R/utils.R | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/R/Heatmap-class.R b/R/Heatmap-class.R index f745e5c..dce8b43 100755 --- a/R/Heatmap-class.R +++ b/R/Heatmap-class.R @@ -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") @@ -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") @@ -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") @@ -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") diff --git a/R/HeatmapAnnotation-class.R b/R/HeatmapAnnotation-class.R index a47b178..513cc06 100755 --- a/R/HeatmapAnnotation-class.R +++ b/R/HeatmapAnnotation-class.R @@ -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() }) diff --git a/R/global.R b/R/global.R index 48ffcf7..6ce1c09 100755 --- a/R/global.R +++ b/R/global.R @@ -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 diff --git a/R/utils.R b/R/utils.R index 8154bb7..1a25fc2 100755 --- a/R/utils.R +++ b/R/utils.R @@ -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) {