diff --git a/NAMESPACE b/NAMESPACE index bb2b631..cc73aab 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,6 +11,7 @@ export(annotation_point) export(annotation_tile) export(as_ComplexHeatmap) export(heatmap) +export(layer_symbol) export(layer_arrow_down) export(layer_arrow_up) export(layer_diamond) @@ -40,6 +41,7 @@ importFrom(grDevices,colorRampPalette) importFrom(grid,gpar) importFrom(grid,grid.grabExpr) importFrom(grid,grid.points) +importFrom(grid,grid.text) importFrom(grid,unit) importFrom(grid,unit.c) importFrom(lifecycle,deprecate_warn) diff --git a/R/functions.R b/R/functions.R index 8c04e58..dd70922 100644 --- a/R/functions.R +++ b/R/functions.R @@ -446,7 +446,10 @@ add_annotation = function(my_input_heatmap, #' setGeneric("layer_symbol", function(.data, ..., - symbol = "point") + symbol = "point", + freetext = "", + color = "#161616", + size = 3) standardGeneric("layer_symbol")) #' layer_symbol @@ -461,7 +464,10 @@ setGeneric("layer_symbol", function(.data, #' setMethod("layer_symbol", "InputHeatmap", function(.data, ..., - symbol = "point"){ + symbol = "point", + freetext = "", + color = "#161616", + size = 3){ .data_drame = .data@data @@ -474,7 +480,8 @@ setMethod("layer_symbol", "InputHeatmap", function(.data, arrow_up = 24, arrow_down = 25, star = 8, - asterisk = 42 + asterisk = 42, + freetext = 0 ) if(!symbol %in% names(symbol_dictionary) | length(symbol) != 1) @@ -502,8 +509,20 @@ setMethod("layer_symbol", "InputHeatmap", function(.data, row = !!.vertical %>% as.factor() %>% as.integer() ) |> filter(...) |> - select(column, row) |> - mutate(shape = symbol_dictionary[[symbol]]) + mutate( + shape = symbol_dictionary[[symbol]], + freetext = !!enquo(freetext), + color = !!enquo(color), + size = !!enquo(size) + ) %>% +# { +# if(!is.null(freetext_col)){ +# mutate(freetext = !!enquo(freetext_col)) %>% +# }else{ +# mutate(freetext = "") %>% +# } +# } %>% + select(column, row, shape, freetext, color, size) ) .data diff --git a/R/methods.R b/R/methods.R index 22a8034..9d53d21 100644 --- a/R/methods.R +++ b/R/methods.R @@ -36,7 +36,7 @@ InputHeatmap<-setClass( left_annotation = tibble(col_name = character(), orientation = character(), col_orientation = character(), data = list(), fx = list(), annot = list(), annot_type= character(), idx = integer(), color = list(), further_arguments = list()), group_top_annotation = list(), group_left_annotation = list(), - layer_symbol = tibble(column = integer(), row = integer(), shape = integer()) + layer_symbol = tibble(column = integer(), row = integer(), shape = integer(), freetext = character(), color = character(), size = integer()) ) ) @@ -49,8 +49,8 @@ InputHeatmap<-setClass( #' #' @importFrom methods show #' @importFrom tibble rowid_to_column -#' @importFrom grid grid.points -#' +#' @importFrom grid grid.points grid.text +#' #' #' @name as_ComplexHeatmap #' @@ -130,15 +130,23 @@ setMethod("as_ComplexHeatmap", "InputHeatmap", function(tidyHeatmap){ # Filter just points to label inner_join(tidyHeatmap@layer_symbol, by = c("row", "column")) |> - select(`index_column_wise`, `shape`) + select(`index_column_wise`, `shape`, `freetext`, `color`, `size`) - if(nrow(ind)>0) + ind_symbol = ind %>% filter(shape != 0) + ind_freetext = ind %>% filter(shape == 0) + if(nrow(ind_symbol)>0) grid.points( - x[ind$index_column_wise], y[ind$index_column_wise], - pch = ind$shape , - size = unit(3, "mm"), - gp = gpar(col = NULL, fill="#161616") + x[ind_symbol$index_column_wise], y[ind_symbol$index_column_wise], + pch = ind_symbol$shape , + size = unit(ind_symbol$size, "mm"), + gp = gpar(col = NULL, fill=ind_symbol$color) ) + + if(nrow(ind_freetext) > 0){ + grid.text( + ind_freetext$freetext, x[ind_freetext$index_column_wise], y[ind_freetext$index_column_wise], + gp = gpar(fontsize = ind_freetext$size, col = ind_freetext$color)) + } } return(do.call(Heatmap, tidyHeatmap@input))