参考:http://www.biotrainee.com/thread-558-1-1.html
http://bioconductor.org/packages/3.7/bioc/
http://www.bioconductor.org/packages/release/bioc/html/topGO.html
https://www.jianshu.com/p/9e21f2196178
https://rpubs.com/aemoore62/TopGo_colMap_Func_Troubleshoot
构建topGOdata对象的3个数据
- 基因某种ID的列表(可以有另一个对应的分数值,如p值或t统计量,或者是差异表达值)
- 基因的这种ID与GO的映射表,在ID为芯片的探针ID时,可以直接使用bioconductor的芯片注释包如
hgu95av2.db
包 - GO的层次关系数据,这个结果可以从GO.db包获得,topGO也只支持GO.db包定义的层次结
library("topGO")
geneID2GO<-readMappings(choose.files()) ##读取所有基因注释信息
geneNames<- names(geneID2GO)
data<-read.table(choose.files(), row.names = 1, header=TRUE,check.names =F) ##读取差异基因的ID
geneList<-data[,1]
names(geneList) <- rownames(data)
topDiffGenes<-function(allScore){return(allScore<0.05)}
1.1、###BP
sampleGOdata <- new("topGOdata",nodeSize = 6,ontology="BP", allGenes = geneList,annot = annFUN.gene2GO, gene2GO = geneID2GO,geneSel=topDiffGenes)
resultKS.elim <- runTest(sampleGOdata, algorithm = "elim", statistic = "ks")
allRes <- GenTable(sampleGOdata,KS = resultKS.elim,ranksOf = "classic", topNodes = attributes(resultKS.elim)$geneData[4])
write.table(allRes, file="T01_vs_T02.topGO_BP.xls", sep=" ", quote=FALSE, col.names=TRUE, row.names=FALSE)
pdf("T01_vs_T02.topGO_BP.pdf")
showSigOfNodes(sampleGOdata, score(resultKS.elim), firstSigNodes = 10, useInfo = "all") ##作图
dev.off()
png("T01_vs_T02.topGO_BP.png")
showSigOfNodes(sampleGOdata, score(resultKS.elim), firstSigNodes = 10, useInfo = "all")
dev.off()
1.2、##MF
sampleGOdata <- new("topGOdata",nodeSize = 6,ontology="MF", allGenes = geneList, annot = annFUN.gene2GO, gene2GO = geneID2GO,geneSel=topDiffGenes)
resultKS.elim <- runTest(sampleGOdata, algorithm = "elim", statistic = "ks")
allRes <- GenTable(sampleGOdata,KS = resultKS.elim,ranksOf = "classic", topNodes = attributes(resultKS.elim)$geneData[4])
write.table(allRes, file="T01_vs_T02.topGO_MF.xls", sep=" ", quote=FALSE, col.names=TRUE, row.names=FALSE)
pdf("T01_vs_T02.topGO_MF.pdf")
showSigOfNodes(sampleGOdata, score(resultKS.elim), firstSigNodes = 10, useInfo = "all")
dev.off()
png("T01_vs_T02.topGO_MF.png")
showSigOfNodes(sampleGOdata, score(resultKS.elim), firstSigNodes = 10, useInfo = "all")
dev.off()
1.3、##CC
sampleGOdata <- new("topGOdata",nodeSize = 6,ontology="CC", allGenes = geneList, annot = annFUN.gene2GO, gene2GO = geneID2GO,geneSel=topDiffGenes)
resultKS.elim <- runTest(sampleGOdata, algorithm = "elim", statistic = "ks")
allRes <- GenTable(sampleGOdata,KS = resultKS.elim,ranksOf = "classic", topNodes = attributes(resultKS.elim)$geneData[4])
write.table(allRes, file="T01_vs_T02.topGO_CC.xls", sep=" ", quote=FALSE, col.names=TRUE, row.names=FALSE)
pdf("T01_vs_T02.topGO_CC.pdf")
showSigOfNodes(sampleGOdata, score(resultKS.elim), firstSigNodes = 10, useInfo = "all")
dev.off()
png("T01_vs_T02.topGO_CC.png")
showSigOfNodes(sampleGOdata, score(resultKS.elim), firstSigNodes = 10, useInfo = "all")
dev.off()
1.4、##输出每个go的基因以及注释到这个go的差异基因[可以不做这个]
allGO =usedGO(object = sampleGOdata)
for (gos in allGO){
goID <-gos;
gene.universe <- genes(sampleGOdata);
go.genes <- genesInTerm(sampleGOdata,goID)[[1]];
sig.genes <- sigGenes(sampleGOdata);
file1=paste("GO-TMP_BP_sig_",gos,sep="");
write.table(sig.genes,file=file1);
file2=paste("GO-TMP_BP_go_",gos,sep="");
write.table(go.genes,file=file2);
}
2、来自生信技能树
###BP
sampleGOdata <- new("topGOdata",nodeSize = 6,ontology="BP", allGenes = geneList,annot = annFUN.gene2GO, gene2GO = geneID2GO,geneSel=topDiffGenes)
allGO =usedGO(object = sampleGOdata)
resultCFis<-runTest(sampleGOdata,algorithm="classic",statistic="fisher")
gtFis<-GenTable(sampleGOdata,classicFisher=resultCFis,orderBy="classic",ranksOf="classicFisher",topNodes=length(allGO))
fdr<-p.adjust(p=gtFis[,"classicFisher"],method="fdr")
r <-cbind(gtFis,fdr)
write.table(r,file="topGO_BP.xls",sep=" ")
showSigOfNodes(GOdata,score(resultCFis),firstSigNodes= 5, useInfo = "all")