R/Enrichments.R

Defines functions PlotEnrichments

Documented in PlotEnrichments

#' Perform GO/Pathway enrichment for DEGs or DBRs via enrichR
#'
#' \code{PlotEnrichments} performs and plots enrichment analyses for a list of 
#' \linkS4class{DESeqResults} objects as generated by \code{\link{RunDESeq2}} or
#' \code{\link{ProcessDEGs}} or a list of \code{data.frames} as generated by
#' \code{\link{RunDiffBind}} or \code{\link{ProcessDBRs}}. It queries the 
#' enrichr web server to do so. Results are saved both as tables and plots.
#'
#' Available libraries can be viewed with \code{\link[enrichR]{listEnrichrDbs}} 
#' from the \code{enrichR} package.
#'
#' The enrichr web server can be found at 
#' \url{https://amp.pharm.mssm.edu/Enrichr/}. If you use this function, you 
#' should be sure to cite the original authors.
#'
#' @param res.list Either:
#'   \itemize{
#'     \item A named list containing \linkS4class{DESeqResults} objects for 
#'       all comparisons generated by \code{\link{ProcessDEGs}}, or
#'     \item A named list containing two or three \linkS4class{csAnno} objects 
#'       for a given comparison generated by \code{\link{ProcessDBRs}} if 
#'       \code{chip = TRUE}. First must contain all DBRs, second/third contain
#'       DBRs up in one group versus the other.
#'   }
#' @param outpath Path to directory to be used for output. Additional 
#'   directories will be generated within this folder.
#' @param padj.thresh Number indicating the adjusted p-value 
#'   cutoff to be used for determining "significant" differential expression
#'   or differential binding (if \code{chip = TRUE}).
#' @param fc.thresh Number indicating the log2 fold-change 
#'   cutoff to be used for determining "significant" differential expression.
#'   or differential binding (if \code{chip = TRUE}).
#' @param libraries A vector of valid \code{enrichR} libraries to test the genes 
#'   against.
#' @param chip Boolean indicating whether \code{res.list} is the results from
#'   \code{\link[DiffBind]{dba.analyze}}.
#' 
#' @importFrom EZscRNA RunEnrichr VizEnrichments
#' @importFrom enrichR enrichr
#'
#' @export
#'
#' @references \href{https://www.ncbi.nlm.nih.gov/pubmed/27141961}{Kuleshov MV, 
#'   Jones MR, Rouillard AD, et al. Enrichr: a comprehensive gene set enrichment 
#'   analysis web server 2016 update. Nucleic Acids Research. 2016; gkw377}
#'
#' @author Jared Andrews
#'
#' @seealso 
#' \code{\link[EZscRNA]{RunEnrichr}}, \code{\link[EZscRNA]{VizEnrichments}}, and
#' \code{\link[enrichR]{enrichr}} for additional enrichment options. 
#'
#' \code{\link{ProcessDEGs}} for generating \linkS4class{DESeqResults} objects.
#' \code{\link{ProcessDBRs}}, for generating \linkS4class{csAnno} objects.
#'
PlotEnrichments <- function(res.list, outpath, padj.thresh, 
  fc.thresh, libraries, chip = FALSE) {

  if (!chip) {
    for (r in seq_along(res.list)) {
      res <- res.list[[r]]
      comp <- names(res.list[r])
      g1 <- unlist(strsplit(comp, "-v-"))[1]
      g2 <- unlist(strsplit(comp, "-v-"))[2]

      # Create directories.
      base <- paste0(outpath, "/Enrichments/", comp, "/padj.", padj.thresh, 
        ".log2fc.", fc.thresh)
      dir.create(file.path(base, "AllGenes"), showWarnings = FALSE, 
        recursive = TRUE)
      dir.create(file.path(base, paste0(g1, ".up")), showWarnings = FALSE, 
        recursive = TRUE)
      dir.create(file.path(base, paste0(g2, ".up")), showWarnings = FALSE, 
        recursive = TRUE)

      one.two <- res[(res[, 'padj'] <= padj.thresh) %in% TRUE & 
        abs(res[, 'log2FoldChange']) >= fc.thresh, ]

      one.up <- res[(res[, 'padj'] <= padj.thresh) %in% TRUE & 
        res[, 'log2FoldChange'] >= fc.thresh, ]

      two.up <- res[(res[, 'padj'] <= padj.thresh) %in% TRUE & 
        res[, 'log2FoldChange'] <= -fc.thresh, ]
      
      one.two.terms <- .quiet(RunEnrichr(rownames(one.two), 
        libraries = libraries, outdir = paste0(base, "/AllGenes")))
      VizEnrichments(one.two.terms, outdir = paste0(base, "/AllGenes"))

      one.up.terms <- .quiet(RunEnrichr(rownames(one.up), 
        outdir = paste0(base, "/", g1, ".up")))
      VizEnrichments(one.up.terms, outdir = paste0(base, "/", g1, ".up"))

      two.up.terms <- .quiet(RunEnrichr(rownames(two.up), 
        outdir = paste0(base, "/", g2, ".up")))
      VizEnrichments(two.up.terms, outdir = paste0(base, "/", g2, ".up"))
    }
  } else {
    comp <- names(res.list)[1]
    g1 <- unlist(strsplit(comp, "-v-"))[1]
    g2 <- unlist(strsplit(comp, "-v-"))[2]

    # Create directories.
    base <- paste0(outpath, "/Enrichments/", comp, "/padj.", padj.thresh, 
      ".log2fc.", fc.thresh)
    dir.create(file.path(base, "AllDBR.Genes"), showWarnings = FALSE, 
      recursive = TRUE)
    dir.create(file.path(base, paste0(g1, ".up")), showWarnings = FALSE, 
      recursive = TRUE)
    dir.create(file.path(base, paste0(g2, ".up")), showWarnings = FALSE, 
      recursive = TRUE)

    one.two <- res.list[[1]]

    one.two.terms <- .quiet(RunEnrichr(one.two@anno$SYMBOL, 
      libraries = libraries,
      outdir = paste0(base, "/AllDBR.Genes")))
    if (length(one.two.terms) > 0) {
      VizEnrichments(one.two.terms, outdir = paste0(base, "/AllDBR.Genes"))
    }

    if (!is.null(res.list[[paste0(g1, ".up")]])) {
      one.up <- res.list[[paste0(g1, ".up")]]
      one.up.terms <- .quiet(RunEnrichr(one.up@anno$SYMBOL, 
        outdir = paste0(base, "/", g1, ".up")))
      if (length(one.up.terms) > 0) {
        VizEnrichments(one.up.terms, outdir = paste0(base, "/", g1, ".up"))
      }
    }

    if (!is.null(res.list[[paste0(g2, ".up")]])) {
      two.up <- res.list[[paste0(g2, ".up")]]
      two.up.terms <- .quiet(RunEnrichr(two.up@anno$SYMBOL, 
        outdir = paste0(base, "/", g2, ".up")))
      if (length(two.up.terms) > 0) {
        VizEnrichments(two.up.terms, outdir = paste0(base, "/", g2, ".up"))
      }
    }
  }
}
j-andrews7/AndrewsBCellLymphoma documentation built on Sept. 9, 2021, 11 a.m.