#go term analysis in R #https://bioconductor.org/packages/release/bioc/html/topGO.html
###################################################
################################################### options(width = 95)
options(stringsAsFactors = FALSE)
###################################################
################################################### x <- topGO:::.algoComp[, -8] x <- x[, colSums(x) > 0] yesPic <- "\includegraphics[width=3mm]{green_ckmark.png}" noPic <- "\includegraphics[width=3mm]{red_ckmark.png}" x[x == 1] <- yesPic x[x == "0"] <- noPic
###################################################
################################################### if(require(xtable)) print(xtable(x, align = c("l", rep("c", ncol(x)))), type = "latex", sanitize.text.function = function(x) return(x), floating = FALSE)
###################################################
###################################################
###################################################
###################################################
###################################################
################################################### library(topGO) library(ALL) data(ALL) data(geneList)
###################################################
################################################### affyLib <- paste(annotation(ALL), "db", sep = ".") library(package = affyLib, character.only = TRUE)
###################################################
################################################### sum(topDiffGenes(geneList))
###################################################
################################################### sampleGOdata <- new("topGOdata", description = "Simple session", ontology = "BP", allGenes = geneList, geneSel = topDiffGenes, nodeSize = 10, annot = annFUN.db, affyLib = affyLib)
###################################################
################################################### sampleGOdata
###################################################
################################################### resultFisher <- runTest(sampleGOdata, algorithm = "classic", statistic = "fisher")
###################################################
################################################### resultFisher
###################################################
################################################### resultKS <- runTest(sampleGOdata, algorithm = "classic", statistic = "ks") resultKS.elim <- runTest(sampleGOdata, algorithm = "elim", statistic = "ks")
###################################################
################################################### allRes <- GenTable(sampleGOdata, classicFisher = resultFisher, classicKS = resultKS, elimKS = resultKS.elim, orderBy = "elimKS", ranksOf = "classicFisher", topNodes = 10)
###################################################
################################################### if(require(xtable)) print(xtable(apply(allRes, 2, as.character)), floating = FALSE)
###################################################
################################################### colMap <- function(x) { .col <- rep(rev(heat.colors(length(unique(x)))), time = table(x)) return(.col[match(1:length(x), order(x))]) }
###################################################
###################################################
###################################################
################################################### pValue.classic <- score(resultKS) pValue.elim <- score(resultKS.elim)[names(pValue.classic)]
gstat <- termStat(sampleGOdata, names(pValue.classic)) gSize <- gstat$Annotated / max(gstat$Annotated) * 4 gCol <- colMap(gstat$Significant)
par(mfcol = c(1, 2), cex = 1) plot(pValue.classic, pValue.elim, xlab = "p-value classic", ylab = "p-value elim", pch = 19, cex = gSize, col = gCol)
plot(pValue.classic, pValue.elim, log = "xy", xlab = "log(p-value) classic", ylab = "log(p-value) elim", pch = 19, cex = gSize, col = gCol)
###################################################
################################################### sel.go <- names(pValue.classic)[pValue.elim < pValue.classic] cbind(termStat(sampleGOdata, sel.go), elim = pValue.elim[sel.go], classic = pValue.classic[sel.go])
###################################################
###################################################
###################################################
################################################### printGraph(sampleGOdata, resultKS.elim, firstSigNodes = 5, fn.prefix = "tGO", useInfo = "all", pdfSW = TRUE)
###################################################
################################################### library(topGO) library(ALL) data(ALL)
###################################################
################################################### BPterms <- ls(GOBPTerm) head(BPterms)
###################################################
################################################### library(genefilter) selProbes <- genefilter(ALL, filterfun(pOverA(0.20, log2(100)), function(x) (IQR(x) > 0.25))) eset <- ALL[selProbes, ]
###################################################
################################################### geneID2GO <- readMappings(file = system.file("examples/geneid2go.map", package = "topGO")) str(head(geneID2GO))
###################################################
################################################### GO2geneID <- inverseList(geneID2GO) str(head(GO2geneID))
###################################################
################################################### geneNames <- names(geneID2GO) head(geneNames)
###################################################
################################################### myInterestingGenes <- sample(geneNames, length(geneNames) / 10) geneList <- factor(as.integer(geneNames %in% myInterestingGenes)) names(geneList) <- geneNames str(geneList)
###################################################
################################################### GOdata <- new("topGOdata", ontology = "MF", allGenes = geneList, annot = annFUN.gene2GO, gene2GO = geneID2GO)
###################################################
################################################### GOdata
###################################################
################################################### y <- as.integer(sapply(eset$BT, function(x) return(substr(x, 1, 1) == 'T'))) table(y)
###################################################
################################################### geneList <- getPvalues(exprs(eset), classlabel = y, alternative = "greater")
###################################################
################################################### topDiffGenes <- function(allScore) { return(allScore < 0.01) } x <- topDiffGenes(geneList) sum(x) ## the number of selected genes
###################################################
################################################### GOdata <- new("topGOdata", description = "GO analysis of ALL data; B-cell vs T-cell", ontology = "BP", allGenes = geneList, geneSel = topDiffGenes, annot = annFUN.db, nodeSize = 5, affyLib = affyLib)
###################################################
################################################### allProb <- featureNames(ALL) groupProb <- integer(length(allProb)) + 1 groupProb[allProb %in% genes(GOdata)] <- 0 groupProb[!selProbes] <- 2 groupProb <- factor(groupProb, labels = c("Used", "Not annotated", "Filtered"))
tt <- table(groupProb) tt
###################################################
###################################################
###################################################
################################################### pValue <- getPvalues(exprs(ALL), classlabel = y, alternative = "greater") geneVar <- apply(exprs(ALL), 1, var) dd <- data.frame(x = geneVar[allProb], y = log10(pValue[allProb]), groups = groupProb)
library(lattice) trellis.device(device = pdf, theme = col.whitebg(), file = "whichProbe.pdf", width = 9, height = 7) legendLab <- paste(names(table(groupProb)), " (#", table(groupProb), ")", sep = "") pP <- xyplot(y ~ x | groups, data = dd, groups = groups, xlab = "Variance", ylab = "Log of p-values", layout = c(2, 2), key = list(text = list(lab = legendLab), points = list(pch = 20, cex = 2, col = Rows(trellis.par.get("superpose.symbol"), 1:3)$col), size = 7, padding.text = 3, x = .65, y = .7, corner = c(0, 0), border = TRUE, cex = 1), panel = function(x, y, ...) { selY <- y <= -2 panel.xyplot(x[selY], y[selY], pch = 2, ...) panel.xyplot(x[!selY], y[!selY], pch = 20, ...) panel.abline(h = -2, lty = 2, col = "black") }) print(pP) dev.off()
###################################################
################################################### description(GOdata) description(GOdata) <- paste(description(GOdata), "Object modified on:", format(Sys.time(), "%d %b %Y"), sep = " ") description(GOdata)
###################################################
################################################### a <- genes(GOdata) ## obtain the list of genes head(a) numGenes(GOdata)
###################################################
################################################### selGenes <- sample(a, 10) gs <- geneScore(GOdata, whichGenes = selGenes) print(gs)
###################################################
################################################### gs <- geneScore(GOdata, whichGenes = selGenes, use.names = FALSE) print(gs)
gs <- geneScore(GOdata, use.names = FALSE) str(gs)
###################################################
################################################### sg <- sigGenes(GOdata) str(sg) numSigGenes(GOdata)
###################################################
################################################### .geneList <- geneScore(GOdata, use.names = TRUE) GOdata ## more available genes GOdata <- updateGenes(GOdata, .geneList, topDiffGenes) GOdata ## the available genes are now the feasible genes
###################################################
################################################### graph(GOdata) ## returns the GO graph
ug <- usedGO(GOdata) head(ug)
###################################################
################################################### sel.terms <- sample(usedGO(GOdata), 10)
num.ann.genes <- countGenesInTerm(GOdata, sel.terms) ## the number of annotated genes num.ann.genes
ann.genes <- genesInTerm(GOdata, sel.terms) ## get the annotations head(ann.genes)
###################################################
################################################### ann.score <- scoresInTerm(GOdata, sel.terms) head(ann.score) ann.score <- scoresInTerm(GOdata, sel.terms, use.names = TRUE) head(ann.score)
###################################################
################################################### termStat(GOdata, sel.terms)
###################################################
################################################### goID <- "GO:0044255" gene.universe <- genes(GOdata) go.genes <- genesInTerm(GOdata, goID)[[1]] sig.genes <- sigGenes(GOdata)
###################################################
################################################### my.group <- new("classicCount", testStatistic = GOFisherTest, name = "fisher", allMembers = gene.universe, groupMembers = go.genes, sigMembers = sig.genes)
contTable(my.group) runTest(my.group)
###################################################
################################################### set.seed(123) elim.genes <- sample(go.genes, length(go.genes) / 4) elim.group <- new("elimCount", testStatistic = GOFisherTest, name = "fisher", allMembers = gene.universe, groupMembers = go.genes, sigMembers = sig.genes, elim = elim.genes)
contTable(elim.group) runTest(elim.group)
###################################################
################################################### test.stat <- new("classicCount", testStatistic = GOFisherTest, name = "Fisher test") resultFisher <- getSigGroups(GOdata, test.stat)
###################################################
################################################### resultFisher
###################################################
################################################### test.stat <- new("classicScore", testStatistic = GOKSTest, name = "KS tests") resultKS <- getSigGroups(GOdata, test.stat)
###################################################
###################################################
###################################################
################################################### test.stat <- new("weightCount", testStatistic = GOFisherTest, name = "Fisher test", sigRatio = "ratio") resultWeight <- getSigGroups(GOdata, test.stat)
###################################################
################################################### resultFis <- runTest(GOdata, algorithm = "classic", statistic = "fisher")
###################################################
###################################################
###################################################
################################################### whichTests() whichAlgorithms()
###################################################
################################################### pvalFis <- score(resultFis) head(pvalFis) hist(pvalFis, 50, xlab = "p-values")
###################################################
################################################### head(score(resultWeight))
###################################################
################################################### pvalWeight <- score(resultWeight, whichGO = names(pvalFis)) head(pvalWeight) cor(pvalFis, pvalWeight)
###################################################
################################################### geneData(resultWeight)
###################################################
################################################### allRes <- GenTable(GOdata, classic = resultFis, KS = resultKS, weight = resultWeight, orderBy = "weight", ranksOf = "classic", topNodes = 20)
###################################################
################################################### if(require(xtable)) print(xtable(apply(allRes, 2, as.character)), floating = FALSE)
###################################################
################################################### goID <- allRes[1, "GO.ID"] print(showGroupDensity(GOdata, goID, ranks = TRUE))
###################################################
################################################### goID <- allRes[10, "GO.ID"] gt <- printGenes(GOdata, whichTerms = goID, chip = affyLib, numChar = 40)
###################################################
################################################### if(require(xtable)) print(xtable(gt), floating = FALSE)
###################################################
###################################################
###################################################
################################################### printGraph(GOdata, resultFis, firstSigNodes = 5, fn.prefix = "tGO", useInfo = "all", pdfSW = TRUE) printGraph(GOdata, resultWeight, firstSigNodes = 5, fn.prefix = "tGO", useInfo = "def", pdfSW = TRUE)
###################################################
###################################################
###################################################
################################################### toLatex(sessionInfo())