1. rm(list = ls())
    2. load(file = 'step4output.Rdata')
    3. library(clusterProfiler)
    4. library(ggthemes)
    5. library(org.Hs.eg.db)
    6. library(dplyr)
    7. library(ggplot2)
    8. library(stringr)
    9. library(enrichplot)
    1. # 1.GO 富集分析----
    2. #(1)输入数据
    3. gene_up = deg$ENTREZID[deg$change == 'up']
    4. gene_down = deg$ENTREZID[deg$change == 'down']
    5. gene_diff = c(gene_up,gene_down)
    6. #(2)富集
    7. #以下步骤耗时很长,设置了存在即跳过
    8. if(!file.exists(paste0(gse_number,"_GO.Rdata"))){
    9. ego <- enrichGO(gene = gene_diff,
    10. OrgDb= org.Hs.eg.db,
    11. ont = "ALL",
    12. readable = TRUE)
    13. ego_BP <- enrichGO(gene = gene_diff,
    14. OrgDb= org.Hs.eg.db,
    15. ont = "BP",
    16. readable = TRUE)
    17. #ont参数:One of "BP", "MF", and "CC" subontologies, or "ALL" for all three.
    18. save(ego,ego_BP,file = paste0(gse_number,"_GO.Rdata"))
    19. }
    20. load(paste0(gse_number,"_GO.Rdata"))

    可视化

    1. #条带图
    2. barplot(ego)
    3. #气泡图
    4. dotplot(ego)
    5. dotplot(ego, split = "ONTOLOGY", font.size = 10,
    6. showCategory = 5) +
    7. facet_grid(ONTOLOGY ~ ., space = "free_y",scales = "free_y") +
    8. scale_y_discrete(labels = function(x) str_wrap(x, width = 45))
    9. #geneList 用于设置下面图的颜色
    10. geneList = deg$logFC
    11. names(geneList)=deg$ENTREZID
    12. #(3)展示top通路的共同基因,要放大看。
    13. #Gene-Concept Network
    14. cnetplot(ego,categorySize="pvalue", foldChange=geneList,colorEdge = TRUE)
    15. cnetplot(ego, showCategory = 3,foldChange=geneList, circular = TRUE, colorEdge = TRUE)
    16. #Enrichment Map,这个函数最近更新过,版本不同代码会不同
    17. Biobase::package.version("enrichplot")
    18. if(T){
    19. emapplot(pairwise_termsim(ego)) #新版本
    20. }else{
    21. emapplot(ego)#旧版本
    22. }
    23. #(4)展示通路关系 https://zhuanlan.zhihu.com/p/99789859
    24. #goplot(ego)
    25. goplot(ego_BP)
    26. #(5)Heatmap-like functional classification
    27. heatplot(ego,foldChange = geneList,showCategory = 8)

    KEGG pathway analysis

    1. # 2.KEGG pathway analysis----
    2. #上调、下调、差异、所有基因
    3. #(1)输入数据
    4. gene_up = deg[deg$change == 'up','ENTREZID']
    5. gene_down = deg[deg$change == 'down','ENTREZID']
    6. gene_diff = c(gene_up,gene_down)
    7. #(2)对上调/下调/所有差异基因进行富集分析
    8. if(!file.exists(paste0(gse_number,"_KEGG.Rdata"))){
    9. kk.up <- enrichKEGG(gene = gene_up,
    10. organism = 'hsa')
    11. kk.down <- enrichKEGG(gene = gene_down,
    12. organism = 'hsa')
    13. kk.diff <- enrichKEGG(gene = gene_diff,
    14. organism = 'hsa')
    15. save(kk.diff,kk.down,kk.up,file = paste0(gse_number,"_KEGG.Rdata"))
    16. }
    17. load(paste0(gse_number,"_KEGG.Rdata"))
    18. #(3)看看富集到了吗?https://mp.weixin.qq.com/s/NglawJgVgrMJ0QfD-YRBQg
    19. table(kk.diff@result$p.adjust<0.05)
    20. table(kk.up@result$p.adjust<0.05)
    21. table(kk.down@result$p.adjust<0.05)
    22. #(4)双向图
    23. # 富集分析所有图表默认都是用p.adjust,富集不到可以退而求其次用p值,在文中说明即可
    24. down_kegg <- kk.down@result %>%
    25. filter(pvalue<0.05) %>% #筛选行
    26. mutate(group=-1) #新增列
    27. up_kegg <- kk.up@result %>%
    28. filter(pvalue<0.05) %>%
    29. mutate(group=1)
    30. source("kegg_plot_function.R")
    31. g_kegg <- kegg_plot(up_kegg,down_kegg)
    32. g_kegg
    33. #g_kegg +scale_y_continuous(labels = c(2,0,2,4,6))
    34. ggsave(g_kegg,filename = 'kegg_up_down.png')
    35. # 3.GSEA作kegg和GO富集分析----
    36. #https://www.yuque.com/xiaojiewanglezenmofenshen/dbwkg1/ytawgg
    37. #(1)查看示例数据
    38. data(geneList, package="DOSE")
    39. #(2)将我们的数据转换成示例数据的格式
    40. geneList=deg$logFC
    41. names(geneList)=deg$ENTREZID
    42. geneList=sort(geneList,decreasing = T)
    43. #(3)GSEA富集
    44. kk_gse <- gseKEGG(geneList = geneList,
    45. organism = 'hsa',
    46. verbose = FALSE)
    47. down_kegg<-kk_gse[kk_gse$pvalue<0.05 & kk_gse$enrichmentScore < 0,];down_kegg$group=-1
    48. up_kegg<-kk_gse[kk_gse$pvalue<0.05 & kk_gse$enrichmentScore > 0,];up_kegg$group=1
    49. #(4)可视化
    50. g2 = kegg_plot(up_kegg,down_kegg)
    51. g2