swh:1:snp:0c004a03453a29b80f921a24433f7e780b9ceb53
Raw File
Tip revision: 7a7752866743aa5a893aab2b3cdf2dcb066c99c3 authored by Alexey Sergushichev on 11 May 2018, 11:53:02 UTC
Depending on svglite
Tip revision: 7a77528
gseaPlot.R
#' Returns path to an svg file with enrichment plot
#' @param fData list of annotation columns
#' @param fvarLabels vector of column names
#' @param rankBy name of the numeric column used for gene ranking
#' @param selectedGenes indexes of selected genes (starting from one, in the order of fData)
#' @param width width of the image (in inches)
#' @param height height of the image (in inches)
#' @return path to an svg file
#' @importFrom fgsea plotEnrichment fgsea
#' @importFrom ggplot2 ggtitle
#' @import svglite
gseaPlot <- function(fData, fvarLabels, rankBy, selectedGenes, width, height) {
    featureData <- data.frame(fData)
    colnames(featureData) <- fvarLabels

    ranks <- setNames(featureData[, rankBy], as.character(seq_len(nrow(featureData))))

    pathway <- as.character(selectedGenes)

    fgseaRes <- fgsea(list(pathway), ranks, nperm=2000, nproc=1)

    pvalString <- if (fgseaRes$nMoreExtreme == 0) {
        " < 1e-3"
    } else {
        sprintf(" = %g", fgseaRes$pval)
    }

    labelString <- sprintf("p-value %s, NES = %s", pvalString, fgseaRes$NES)


    p <- plotEnrichment(pathway, ranks) + ggtitle(labelString)
    f <- tempfile(pattern = "enrichment", tmpdir = getwd(), fileext = ".svg")
    ggsave(p, filename = f, width=6, height=4)
    # ggsave(p, filename = f, width=width, height=height)
    jsonlite::toJSON(f)
}
back to top