R/plotDrugConnectivity.R

Defines functions plotDrugConnectivity

Documented in plotDrugConnectivity

#' Plot Drug Connectivity
#'
#' @param dsea is a drug set Enrichment object is the output of getActiveDSEA
#'
#' @return a plot of drug connectivity
#' @export
#' @import graphics
#' @import utils
#'
#' @examples # from the data-sets provided as examples within the package load the .rda files
#' # DrugsAnnot, mDrugEnrich, mFC
#' dsea <- getActiveDSEA(mDrugEnrich, DrugsAnnot, mFC, contr = colnames(mFC)[1])
#' plotDrugConnectivity(dsea)
plotDrugConnectivity <- function(dsea) {
  dt <- dsea$table

  if (nrow(dt) == 0) {
    return(NULL)
  }

  rnk <- dsea$stats
  if (length(rnk) == 0) {
    return(NULL)
  }

  ## ENPLOT TYPE
  if (nrow(dt) == 1) {
    par(oma = c(1, 1, 1, 1))
    par(mfrow = c(1, 1), mar = c(4, 4, 1.1, 2), mgp = c(2.3, 0.9, 0))
    lab.cex <- 1
    xlab <- "Rank in ordered dataset"
    ylab <- "Rank metric"
    nc <- 1
  } else {
    dt <- utils::head(dt, 16)
    lab.cex <- 0.75
    xlab <- ""
    ylab <- ""
    nc <- ceiling(sqrt(nrow(dt)))
    par(oma = c(0, 1.6, 0, 0))
    par(mfrow = c(nc, nc), mar = c(0.3, 1.0, 1.3, 0), mgp = c(1.9, 0.6, 0))
  }

  i <- 1
  for (i in 1:nrow(dt)) {
    dx <- rownames(dt)[i]

    gmtdx <- grep(dx, rownames(rnk), fixed = TRUE, value = TRUE) ## L1000 naming
    length(gmtdx)

    dx1 <- substring(dx, 1, 26)
    par(cex.axis = 0.001)
    if (i %% nc == 1) par(cex.axis = 0.98)
    suppressWarnings(
      gsea.enplot(rnk, gmtdx,
        main = dx1, cex.main = 1.2,
        xlab = xlab, ylab = ylab
      )
    )
    nes <- round(dt$NES[i], 2)
    qv <- round(dt$padj[i], 3)
    tt <- c(paste("NES=", nes), paste("q=", qv))
    graphics::legend("topright", legend = tt, cex = 0.8, y.intersp = 0.85, bty = "n")
    if (i %% nc == 1 && nrow(dt) > 1) {
      graphics::mtext("rank metric", side = 2, line = 1.8, cex = lab.cex)
    }
  }
}
bigomics/SPACEconnect documentation built on March 19, 2022, 3:39 a.m.