1. #多个单细胞亚群各自标记基因联合进行GO和KEGG注释
    2. rm(list=ls())
    3. options(stringsAsFactors = F)
    4. library(Seurat)
    5. library(gplots)
    6. library(ggplot2)
    7. library(clusterProfiler)
    8. pro='all'
    9. #加载差异基因
    10. load('sce.markers.all_10_celltype.Rdata')
    11. table(sce.markers$cluster) #每个单细胞亚群都有自己的marker基因
    12. library(org.Hs.eg.db)
    13. ids=bitr(sce.markers$gene,
    14. 'SYMBOL',
    15. 'ENTREZID',
    16. 'org.Hs.eg.db')
    17. sce.markers=merge(sce.markers,
    18. ids,
    19. by.x='gene',
    20. by.y='SYMBOL')
    21. gcSample=split(sce.markers$ENTREZID,
    22. sce.markers$cluster)
    23. gcSample #entrez id , compareCluster
    ##KEGG
    xx <- compareCluster(gcSample, 
                           fun="enrichKEGG",
                           organism="hsa", 
                           pvalueCutoff=0.05)
    dotplot(xx)
    ggsave(paste0(pro,'_kegg.pdf'))
    

    image.png
    图 KEGG

    ##BP 
      formula_res <- compareCluster(
        gcSample, 
        fun="enrichGO", 
        OrgDb="org.Hs.eg.db",
        ont = "BP",
        pAdjustMethod = "BH",
        pvalueCutoff  = 0.01,
        qvalueCutoff  = 0.05)
    
      #Run GO enrichment test and merge terms 
      # that are close to each other to remove result redundancy
      lineage1_ego <- simplify(
        formula_res, 
        cutoff=0.5, 
        by="p.adjust", 
        select_fun=min) 
    
      pdf(paste0(pro,'_GO_BP_cluster_simplified.pdf'),
          width = 15,
          height = 6)
    
      print(dotplot(lineage1_ego, 
                    showCategory=5))
      dev.off() 
      write.csv(lineage1_ego@compareClusterResult, 
                file=paste0(pro,'_GO_BP_cluster_simplified.csv'))
    

    image.png
    图 BP

      ##CC 
      formula_res <- compareCluster(
        gcSample, 
        fun="enrichGO", 
        OrgDb="org.Hs.eg.db",
        ont    = "CC",
        pAdjustMethod = "BH",
        pvalueCutoff  = 0.01,
        qvalueCutoff  = 0.05)
    
      # Run GO enrichment test and merge terms 
      # that are close to each other to remove result redundancy
      lineage1_ego <- simplify(
        formula_res, 
        cutoff=0.5, 
        by="p.adjust", 
        select_fun=min) 
      pdf(paste0(pro,'_GO_CC_cluster_simplified.pdf'),
          width = 15,
          height = 6)
      print(dotplot(lineage1_ego,
                    showCategory=5))
      dev.off() 
      write.csv(lineage1_ego@compareClusterResult, 
                file=paste0(pro,'_GO_CC_cluster_simplified.csv'))
    

    image.png
    图 CC

      ##MF 
      formula_res <- compareCluster(
        gcSample, 
        fun="enrichGO", 
        OrgDb="org.Hs.eg.db",
        ont    = "MF",
        pAdjustMethod = "BH",
        pvalueCutoff  = 0.01,
        qvalueCutoff  = 0.05)
    
      # Run GO enrichment test and merge terms 
      # that are close to each other to remove result redundancy
      lineage1_ego <- simplify(
        formula_res, 
        cutoff=0.5, 
        by="p.adjust", 
        select_fun=min
      ) 
      pdf(paste0(pro,'_GO_MF_cluster_simplified.pdf'),
          width = 15,
          height = 6)
      print(dotplot(lineage1_ego, 
                    showCategory=5))
      dev.off() 
      write.csv(lineage1_ego@compareClusterResult, 
                file=paste0(pro,'_GO_MF_cluster_simplified.csv'))
    

    image.png
    图 MF