R/rnaheat.R

Defines functions rnaheat

Documented in rnaheat

#' rnaheat function
#'
#' Uses ComplexHeatmap to draw heatmap
#'
#' @param pat2 data frame generated by rnasubset function
#' @param rna data frame of RNAseq values
#' @param deg data frame of differential expressed genes from rnadeg function
#' @param gene character(1) Gene symbol of gene of interest
#' @param n numeric(1) Number of genes you want to plot, default is all significant genes
#'
#' @import data.table
#' @import ComplexHeatmap
#' @import circlize
#' @importFrom edgeR cpm
#'
#' @return ComplexHeatmap of top 100 significant differentially expressed genes
#'
#' @examples
#' data(skcm)
#' gene <- "SOX10"
#' sox10.pat <- rnasubset(pat, rna, gene, 50)
#' sox10.deg <- rnadeg(sox10.pat, rna)
#' rnaheat(sox10.pat, rna, sox10.deg, gene, n)
#'
#' @export
#'
rnaheat <- function(pat2, rna, deg, gene, n = NULL) {
  if (is.null(n)) n <- nrow(deg) else n
  setkey(pat2, gene2)
  rna.log <- data.table(Gene = rna$Gene, cpm(rna[,pat2[!("middle"), name], with = FALSE], log = TRUE, normalized.lib.sizes = FALSE, prior.count = 8))
  setkey(rna.log, Gene)
  dgenes <- deg$genes[1:n][!is.na(deg$genes[1:n])]
  h1 <- as.matrix(rna.log[dgenes, colnames(rna.log)[-1], with = FALSE])
  rownames(h1) <- dgenes
  h1.t <- t(apply(h1, 1, scale))
  colnames(h1.t) <- colnames(h1)
  setkey(rna, Gene)
  df <- data.frame(as.character(pat2[!("middle"),gene2]),
                   pat2[!("middle"), exprs_rank], stringsAsFactors = FALSE)
  colnames(df) <- c(paste0(gene, "_group"), paste0(gene, "_expression"))
  cellcol <- c("#ca0020", "#0571b0")
  names(cellcol) <- c(levels(pat2$gene2)[1], levels(pat2$gene2)[2])
  col1 <- list(Cell = cellcol,
               expression = colorRamp2(c(1, nrow(pat2)), c("white", "purple")))
  names(col1) <- c(paste0(gene, "_group"), paste0(gene, "_expression"))
  top_ha <- HeatmapAnnotation(df = df, col = col1)

  x <- Heatmap(h1.t, top_annotation = top_ha, name = "color scale",
          show_column_names = T,
          row_names_gp = gpar(fontsize = 8),
          column_names_gp = gpar(fontsize = 6),
          column_dend_reorder = as.numeric(df[, 1]))
  draw(x)
  for(an in colnames(df)) {
   decorate_annotation(an, {
      # annotation names on the right
     grid.text(an, unit(1, "npc") + unit(2, "mm"), 0.5, default.units = "npc", just = "left")
      # annotation names on the left
      #grid.text(an, unit(0, "npc") - unit(2, "mm"), 0.5, default.units = "npc", just = "right")
    })
  }
}
pcheng84/TCGAbrowser documentation built on Sept. 7, 2021, 8:28 p.m.