#' GO term enrichment using Fisher's exact test
#'
#' @param sigGenes either vector of genes of interest or numeric vector containing all genes
#' @param allGenes background of genes
#' @param annotations data base containing all annotations
#' @param inverse inverse enrichment
#' @param ontology which ontology to be used (CC, BP, MF)
#' @param nodeSize minimum node size
#' @param save should result table be saved
#' @param name name
#' @param destination where to save
#' @param view view tables
#' @param print should a graphical output be printed
#'
#' @return
#' @export
#'
#' @import topGO
#'
enrichGOGroups <- function(sigGenes, annotations = info[["annotations"]], inverse = F, ontology = "CC", nodeSize = 10, save = F, name, destination, view = T, print = F) {
groups <- unique(sigGenes)
results <- list()
for(group in groups) {
genes <- as.numeric(sigGenes == group)
if(inverse) {
genes <- as.numeric(sigGenes != group)
}
names(genes) <- names(sigGenes)
GOdata <- new("topGOdata",
ontology = ontology,
allGenes = as.factor(genes),
nodeSize = nodeSize,
annot = annFUN.gene2GO,
gene2GO = annotations[names(genes)])
test.stat <- new("classicCount", testStatistic = GOFisherTest, name = "Fisher test")
resultFisher <- getSigGroups(GOdata, test.stat)
allRes <- GenTable(GOdata,
classic = resultFisher,
orderBy = "classic",
topNodes = 20)
allRes$proteins <- NA
allRes$genes <- NA
allRes$names <- NA
for(i in 1:nrow(allRes)) {
p <- intersect(sigGenes, getALLProteins(allRes[i, 1], annotationDB = info[["annotations"]]))
allRes$proteins[i] <- paste(p, collapse = ";")
allRes$genes[i] <- paste(raw.data[[1]][p, "gene"], collapse = ";")
allRes$names[i] <- paste(raw.data[[1]][p, "name"], collapse = ";")
}
results[[length(results) + 1]] <- list(GOdata = GOdata,
resultFisher = resultFisher,
table = allRes)
names(results)[[length(results)]] <- group
if(view) {
View(allRes, paste(if(hasArg(name)) name, ontology, group, sep = "_"))
}
}
if(save) {
saveThis(results, name = name, destination = destination)
}
if(print) {
if(!dir.exists("./GO")) {
dir.create("./GO")
}
if(!hasArg(name)) {
name <- paste(groups, collapse = "_")
}
if(dir.exists(paste0("./GO/", name))) {
x <- 1
while(dir.exists(paste0("./GO/", name, "_", x))) {
x <- x + 1
}
path <- paste0("./GO/", name, "_", x)
}
else {
path <- paste0("./GO/", name)
}
dir.create(path)
setwd(path)
for(i in 1:length(groups)) {
printGraph(results[[i]][[1]],
results[[i]][[2]],
firstSigNodes = 20,
fn.prefix = paste(ontology, groups[i], sep = "_"),
useInfo = "all",
pdfSW = TRUE)
}
wd()
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.