首先需要加载的包
library(AnnotationHub)
library(org.Hs.eg.db)
library(clusterProfiler)
library(DOSE) #GO分面用
library(ggplot2) #加大泡泡图圈圈
需要准备的文件
- 包含gene列表的txt或csv文件,如“genelist.txt”
- 格式为ENSEMBL
一、读取表格
f <- read.table("genelist.txt") # genelist.txt内容ENSEMBL格式
f <- f[c(1)]
二、ID转换
EG2Ensembl=toTable(org.Hs.egENSEMBL)
f=f$V1
geneLists=data.frame(ensembl_id=f)
results=merge(geneLists,EG2Ensembl,by='ensembl_id',all.x=T)
id=na.omit(results$gene_id)
三、富集分析
(1)GO富集分析(MF、BP、CC)
- 分析
#包含MF、BP和CC的GO分析
All <- enrichGO(OrgDb="org.Hs.eg.db", gene = id, ont = "ALL", readable= TRUE)
#GO-MF富集分析
MF <- enrichGO(OrgDb="org.Hs.eg.db", gene = id, ont = "MF", readable= TRUE)
#GO-BP富集分析
BP <- enrichGO(OrgDb="org.Hs.eg.db", gene = id, ont = "BP", readable= TRUE)
#GO-CC富集分析
CC <- enrichGO(OrgDb="org.Hs.eg.db", gene = id, ont = "CC", readable= TRUE)
- 作图
#泡泡图,显示前10项,标题为“Enrichment GO Top10”
dotplot(All,showCategory=10,title="Enrichment GO Top10")
#柱状图,显示前20项,标题为“Enrichment GO Top10”
barplot(All, showCategory=20,title="EnrichmentGO")
# BP,MF,CC分别显示(要加载DOSE包)
barplot(All, split="ONTOLOGY")+ facet_grid(ONTOLOGY~.,scale="free")
dotplot(All, split="ONTOLOGY")+ facet_grid(ONTOLOGY~.,scale="free")
(2) KEGG分析
KEGG <- enrichKEGG(gene= id, organism = 'hsa', pvalueCutoff = 0.05) #KEGG富集分析
barplot(KEGG,font.size=8) # 柱状图,字体大小8
dotplot(KEGG,font.size=8) # 气泡图,字体大小8
dotplot(KEGG,showCategory=10,title="Enrichment KEGG Top10") #泡泡图
browseKEGG(KEGG,'mmu01100') # 显示通路图
(3) 个性定制
#字体大小,显示项数,标题,泡泡大小,需加载ggplot2
dotplot(KEGG, font.size=8, showCategory=10, title="Enrichment KEGG Top10") + scale_size(rang=c(5.20))
#文本太长超出的解决方案
library(stringr)
library(ggplot2)
barplot(All)+scale_x_discrete(labels=function(x) str_wrap(x,width=10))
dotplot(All)+scale_y_discrete(labels=function(x) stringr::str_wrap(x, width=60))
ego2 <-
cnetplot(ego2, foldChange=geneList)
cnetplot(ego2, foldChange=geneList, circular =TRUE, colorEdge =TRUE) (只显示5项,如加项可输入showCategory=10等)
网友评论