Switch to unified view

a b/ATAC/Functions/Great.enrich.R
1
2
wrapText <- function(x, len) {
3
    sapply(x, function(y) paste(strwrap(y, len), collapse = "\n"), USE.NAMES = FALSE)
4
}
5
Great.enrich <- function(gr, bg, title, species = "hg38", fdr.threshold = 0.05, Type = "GO Biological Process", n_terms = 20){
6
    require(rGREAT)
7
    require(tidyverse)
8
    require(cowplot)
9
    theme_set(theme_cowplot())
10
    set.seed(101)
11
    job <- submitGreatJob(gr = gr, bg = bg, species = species)
12
    tb <- getEnrichmentTables(job)
13
    region.graph <- plotRegionGeneAssociationGraphs(job)
14
15
    # "GO Molecular Function" "GO Biological Process" "GO Cellular Component"
16
    sig.tb <- lapply(names(tb), function(x){
17
        enr <- tb[[x]]
18
        enr$Term <- rep(x, nrow(enr))
19
        sig <- which(enr$Hyper_Adjp_BH < fdr.threshold)
20
        if(length(sig>0)){
21
            return(enr[sig,])
22
        }else{
23
            return(data.frame())
24
        }
25
    })
26
    rGREAT.res <- Reduce(rbind, sig.tb)
27
    if(nrow(rGREAT.res) > 0){
28
        if(!is.null(Type)){
29
            rGREAT.res <- rGREAT.res[which(rGREAT.res$Term == Type),]
30
        }
31
32
        if(nrow(rGREAT.res) > 0){
33
            if(nrow(rGREAT.res) >= n_terms){
34
            rGREAT.res <- rGREAT.res[1:n_terms,]
35
            }
36
            rGREAT.res$wrap <- wrapText(rGREAT.res$name, 45)
37
            p <- ggplot(rGREAT.res, aes(y = log(Hyper_Fold_Enrichment), x = reorder(wrap, log(Hyper_Fold_Enrichment)))) +
38
                geom_bar(stat='identity', aes(color = name, fill = name)) +
39
                xlab('') + theme(legend.position="none") + ggtitle(title) + coord_flip()
40
            print(p)
41
        }
42
    }
43
    return(rGREAT.res)
44
}