R/p06_signal_heatmap.R

Defines functions signal_heatmap

Documented in signal_heatmap

## old: polII_heatmap_secondary()
#' Heatmap for polII binding signal
#'
#' This function generate a polII binding signal heatmap using ComplexHeatmap package
#' function Heatmap().
#'
#' @param log2_matrix a log2 matrix with polII expression values to be plotted as Heatmap
#' @param htName heatmap name. Default: in not provided, col_title is used
#' @param col_title column title for heatmap. If not provided, matrix column names are used. Default: NULL
#' @param legend_title legend title
#' @param color color for heamap generated by ColorRamp2
#' @param htSplit if heatmap rows are to be split, the dataframe or vector with split information. Default: NULL
#' @param showLegend Binary: whether to show legend or not. Default: TRUE
#' @param ... Other arguments to Heatmap function
#'
#' @return A complexheatmap object i.e. a heatmap which can be directly plotted or added to heatmap list
#' @export
#'
#' @examples NA
signal_heatmap = function(log2_matrix,
                          htName = NULL,
                          col_title = NULL,
                          legend_title,
                          color,
                          htSplit = NULL,
                          showLegend = TRUE,
                          ...){

  if (is.null(col_title)) {
    col_title <- colnames(log2_matrix)
  }

  if(is.null(htName)){
    htName = col_title[1]
  }

  cat("Generating signal heatmap for sample:", col_title, "\n")

  ## column name as annotation for Heatmap
  ## reason for using column names in form of Annotation: *(yet to verify this)
  ## this heatmap is plotted next to profile hetamap. The top panel of profile heatmap is for
  ## average signal line annotation which is default. Now if we use the column names for
  ## Heatmap, the column names and profile heatmap are not placed at same height.
  ## So we make the names of Heatmap as annotation so that they are aligned properly
  colNameAnn <- HeatmapAnnotation(
    colName = anno_text(x = c(col_title),
                        rot = 90, just = "left",
                        location = unit(1, "mm")),
    annotation_height = unit.c(max_text_width(col_title))
  )


  if (! is.matrix(log2_matrix)) {
    stop("Argument log2_matrix should be a matrix. Detected ", class(log2_matrix), "\n")
  }

  ## draw heatmap
  ht = Heatmap(log2_matrix, name = htName,
               col = color,
               top_annotation = colNameAnn,
               show_row_names = FALSE, show_column_names = FALSE,
               width = unit(1 * ncol(log2_matrix), "cm"),
               show_heatmap_legend = showLegend,
               heatmap_legend_param = list(title = legend_title,
                                           # legend_height = unit(3, "cm"),
                                           color_bar = "continuous"),
               split = htSplit,
               ...
  )

  cat("Done!!!\n")

  return(ht)
}

##################################################################################
lakhanp1/chipmine documentation built on March 6, 2021, 9:06 a.m.