R/plotPathways.R

Defines functions PlotOncogenicPathways

Documented in PlotOncogenicPathways

#' Plot oncogenic pathways
#' @references Sanchez-Vega F, Mina M, Armenia J, Chatila WK, Luna A, La KC, Dimitriadoy S, Liu DL, Kantheti HS, Saghafinia S et al. 2018. Oncogenic Signaling Pathways in The Cancer Genome Atlas. Cell 173: 321-337 e310
#'
#' @details
#' Draws oncoplot of oncogenic pathway.
#' @param maf an \code{\link{MAF}} object generated by \code{\link{read.maf}}
#' @param pathways Name of pathways to be drawn
#' @param fullPathway Include all genes from the pathway. Defaulr FALS only plots mutated genes
#' @param removeNonMutated Default TRUE
#' @param tsgCol Color for tumro suppressor genes. Default red
#' @param ogCol Color for onco genes. Default royalblue
#' @param fontSize Default 0.6
#' @param showTumorSampleBarcodes logical to include sample names.
#' @param SampleNamefontSize font size for sample names. Default 10
#' @param sampleOrder Manually speify sample names for oncolplot ordering. Default NULL.
#' @seealso \code{\link{OncogenicPathways}}
#' @export
#' @examples
#' laml.maf <- system.file("extdata", "tcga_laml.maf.gz", package = "maftools")
#' laml <- read.maf(maf = laml.maf)
#' PlotOncogenicPathways(maf = laml, pathways = "RTK-RAS")
PlotOncogenicPathways = function(maf, pathways = NULL, fullPathway = FALSE,
                                 removeNonMutated = TRUE, tsgCol = "red", ogCol = "royalblue", fontSize = 0.6, showTumorSampleBarcodes = FALSE, sampleOrder = NULL, SampleNamefontSize = 0.6){

  pathdb <- system.file("extdata", "oncogenic_sig_patwhays.tsv", package = "maftools")
  pathdb = data.table::fread(input = pathdb)
  #pathdb_size = pathdb[,.N,Pathway]
  pathdb = split(pathdb, as.factor(pathdb$Pathway))

  pathways = pathways[pathways %in% names(pathdb)]

  if(length(pathways) == 0){
    message("Available pathways..")
    print(names(pathdb))
    stop()
  }

  totSamps = as.numeric(maf@summary[3,summary])
  tsbs = levels(getSampleSummary(x = maf)[,Tumor_Sample_Barcode])

  par(mfrow = c(length(pathways), 1), bty="n",
      mar=c(2,4,2,2)+.1, las=1, tcl=-.25, cex=1)

  for(i in 1:length(pathways)){
    oncopath = pathdb[[pathways[i]]]
    data.table::setDF(x = oncopath, rownames = oncopath$Gene)
    oncopath$color_code = ifelse(test = oncopath$OG_TSG == "OG", yes = ogCol,
                                 no = ifelse(test = oncopath$OG_TSG == "TSG", yes = tsgCol, no = "black"))
    oncopath$color_code = ifelse(test = is.na(oncopath$color_code), yes = "black", no = oncopath$color_code)
    genes = oncopath$Gene
    path_mat = createOncoMatrix(m = maf, g = genes)
    nm = path_mat$numericMatrix

    if(fullPathway){
      genes.missing = genes[!genes %in% rownames(nm)]
      if(length(genes.missing) > 0){
        genes.missing.numat = matrix(data = 0, ncol = ncol(nm), nrow = length(genes.missing))
        rownames(genes.missing.numat) = genes.missing
        nm = rbind(nm, genes.missing.numat)
      }
    }

    genes = rownames(nm)

    if(!removeNonMutated){
      tsb.include = matrix(data = 0, nrow = length(genes), ncol = length(tsbs[!tsbs %in% colnames(nm)]))
      colnames(tsb.include) = tsbs[!tsbs %in% colnames(nm)]
      rownames(tsb.include) = rownames(nm)
      nm = cbind(nm, tsb.include)
    }

    nm = t(apply(nm, 2, rev))
    nm[nm == 0] = NA
    nm[!is.na(nm)] = 1

    if(!is.null(sampleOrder)){
      sampleOrder = as.character(sampleOrder)
      sampleOrder = sampleOrder[sampleOrder %in% rownames(nm)]
      if(length(sampleOrder) == 0){
        stop("None of the provided samples are present in the input MAF")
      }
      nm = nm[sampleOrder, ,drop = FALSE]
    }

    if(showTumorSampleBarcodes){
      par(mar = c(4, 4, 2, 2), xpd = TRUE)
    }

    image(x = 1:nrow(nm), y = 1:ncol(nm), z = nm, axes = FALSE, xaxt="n", yaxt="n", xlab="", ylab="", col = "brown", ) #col = "#FC8D62"
    abline(h = (1:ncol(nm)) + 0.5, col = "white")
    abline(v = (1:nrow(nm)) + 0.5, col = "white")
    points(which(is.na(nm), arr.ind = TRUE), pch=".", col = "gray70")
    mtext(text = colnames(nm), side = 2, at = 1:ncol(nm), col = oncopath[colnames(nm), "color_code",],
          font = 3, line = 0.4, cex = fontSize)
    if(showTumorSampleBarcodes){
      # mtext(text = rownames(nm), side = 1, at = 1:nrow(nm),
      #       font = 3, line = 0.1, cex = SampleNamefontSize, las = 2)
      text(x =1:nrow(nm), y = par("usr")[3] - 0.2,
           labels = rownames(nm), srt = 45, font = 3, cex = SampleNamefontSize, adj = 1)
    }
    title(main = paste0(pathways[i], " pathway"), adj = 0)
  }
}
thesushantpatil/maftools documentation built on May 18, 2020, 9:54 p.m.