rm(list = ls())load(file = 'step4output.Rdata')library(clusterProfiler)library(ggthemes)library(org.Hs.eg.db)library(dplyr)library(ggplot2)library(stringr)library(enrichplot)
# 1.GO 富集分析----#(1)输入数据gene_up = deg$ENTREZID[deg$change == 'up']gene_down = deg$ENTREZID[deg$change == 'down']gene_diff = c(gene_up,gene_down)#(2)富集#以下步骤耗时很长,设置了存在即跳过if(!file.exists(paste0(gse_number,"_GO.Rdata"))){ego <- enrichGO(gene = gene_diff,OrgDb= org.Hs.eg.db,ont = "ALL",readable = TRUE)ego_BP <- enrichGO(gene = gene_diff,OrgDb= org.Hs.eg.db,ont = "BP",readable = TRUE)#ont参数:One of "BP", "MF", and "CC" subontologies, or "ALL" for all three.save(ego,ego_BP,file = paste0(gse_number,"_GO.Rdata"))}load(paste0(gse_number,"_GO.Rdata"))
可视化
#条带图barplot(ego)#气泡图dotplot(ego)dotplot(ego, split = "ONTOLOGY", font.size = 10,showCategory = 5) +facet_grid(ONTOLOGY ~ ., space = "free_y",scales = "free_y") +scale_y_discrete(labels = function(x) str_wrap(x, width = 45))#geneList 用于设置下面图的颜色geneList = deg$logFCnames(geneList)=deg$ENTREZID#(3)展示top通路的共同基因,要放大看。#Gene-Concept Networkcnetplot(ego,categorySize="pvalue", foldChange=geneList,colorEdge = TRUE)cnetplot(ego, showCategory = 3,foldChange=geneList, circular = TRUE, colorEdge = TRUE)#Enrichment Map,这个函数最近更新过,版本不同代码会不同Biobase::package.version("enrichplot")if(T){emapplot(pairwise_termsim(ego)) #新版本}else{emapplot(ego)#旧版本}#(4)展示通路关系 https://zhuanlan.zhihu.com/p/99789859#goplot(ego)goplot(ego_BP)#(5)Heatmap-like functional classificationheatplot(ego,foldChange = geneList,showCategory = 8)
KEGG pathway analysis
# 2.KEGG pathway analysis----#上调、下调、差异、所有基因#(1)输入数据gene_up = deg[deg$change == 'up','ENTREZID']gene_down = deg[deg$change == 'down','ENTREZID']gene_diff = c(gene_up,gene_down)#(2)对上调/下调/所有差异基因进行富集分析if(!file.exists(paste0(gse_number,"_KEGG.Rdata"))){kk.up <- enrichKEGG(gene = gene_up,organism = 'hsa')kk.down <- enrichKEGG(gene = gene_down,organism = 'hsa')kk.diff <- enrichKEGG(gene = gene_diff,organism = 'hsa')save(kk.diff,kk.down,kk.up,file = paste0(gse_number,"_KEGG.Rdata"))}load(paste0(gse_number,"_KEGG.Rdata"))#(3)看看富集到了吗?https://mp.weixin.qq.com/s/NglawJgVgrMJ0QfD-YRBQgtable(kk.diff@result$p.adjust<0.05)table(kk.up@result$p.adjust<0.05)table(kk.down@result$p.adjust<0.05)#(4)双向图# 富集分析所有图表默认都是用p.adjust,富集不到可以退而求其次用p值,在文中说明即可down_kegg <- kk.down@result %>%filter(pvalue<0.05) %>% #筛选行mutate(group=-1) #新增列up_kegg <- kk.up@result %>%filter(pvalue<0.05) %>%mutate(group=1)source("kegg_plot_function.R")g_kegg <- kegg_plot(up_kegg,down_kegg)g_kegg#g_kegg +scale_y_continuous(labels = c(2,0,2,4,6))ggsave(g_kegg,filename = 'kegg_up_down.png')# 3.GSEA作kegg和GO富集分析----#https://www.yuque.com/xiaojiewanglezenmofenshen/dbwkg1/ytawgg#(1)查看示例数据data(geneList, package="DOSE")#(2)将我们的数据转换成示例数据的格式geneList=deg$logFCnames(geneList)=deg$ENTREZIDgeneList=sort(geneList,decreasing = T)#(3)GSEA富集kk_gse <- gseKEGG(geneList = geneList,organism = 'hsa',verbose = FALSE)down_kegg<-kk_gse[kk_gse$pvalue<0.05 & kk_gse$enrichmentScore < 0,];down_kegg$group=-1up_kegg<-kk_gse[kk_gse$pvalue<0.05 & kk_gse$enrichmentScore > 0,];up_kegg$group=1#(4)可视化g2 = kegg_plot(up_kegg,down_kegg)g2
