单细胞 (小鼠)| 差异分析+GSEA通路富集

教程:
FindMarkers做差异分析默认就不会返回全部基因 - 腾讯云开发者社区-腾讯云 (tencent.com)
Seurat包的findmarkers函数只能根据划分好的亚群进行差异分析吗 (qq.com)
Seurat中FindMarker寻找两个cell type差异基因的若干方法 - (jianshu.com)
用clusterProfiler做GSEA(二) - 腾讯云开发者社区-腾讯云 (tencent.com)
(125条消息) 使用clusterProfiler进行GO富集分析_生信修炼手册的博客-CSDN博客_clusterprofiler
(125条消息) GSEA_yssxswl的博客-CSDN博客

#寻找marker基因(第一种)
tumor.markers <- FindAllMarkers(tumor,  assay = 'RNA',slot = 'counts',
                                logfc.threshold =0,min.pct = 0)
library(dplyr,lib.loc = "/home/yifan/R/4.0")
tumor.markers = tumor.markers %>% group_by(cluster)
write.csv (tumor.markers,"~/project/liuji/raw/Seurat/all/tumor/tumor.all.cluster.marker.csv")

#####整群差异分析----
markers <- FindMarkers(tumor, ident.1 = "test",
                       group.by = 'tissue_type',
                       assay = 'RNA',slot = 'counts',
                       logfc.threshold =0,min.pct = 0 )

###############7群特有marker
markers <- FindMarkers(ILC, ident.1 = 7,
                       assay = 'RNA',slot = 'counts',
                       logfc.threshold =0,min.pct = 0 )

###############富集分析
tumor.markers<-fread("~/project/liuji/raw/Seurat/all/tumor/tumor.all.cluster.marker.csv",data.table = F)
n<-length(tumor.markers$cluster)
n<-n-1

library(clusterProfiler)
library(DOSE)
library(org.Mm.eg.db)
library(clusterProfiler)
library(dplyr)
library(enrichplot)
library(GSVA)
library(GSEABase)
library(msigdbr)
library(limma)
library(tinyarray)
#install.packages("tinyarray")

for (i in 0:n){
  #i=0
  d<-tumor.markers[tumor.markers$cluster==i,c(8,3)]
  names(d) <- c("SYMBOL","FC")
  #SYMBOL转为ENTREZID才能进行后续富集分析
  entrezid <- bitr(d$SYMBOL,fromType = "SYMBOL",toType = "ENTREZID",OrgDb = 'org.Mm.eg.db', drop = T)
  final <- merge(d,entrezid,by="SYMBOL")
  ID_FC <- dplyr::select(final,FC,ENTREZID)

#转为数值型
data=apply(ID_FC,2,as.numeric)
data<-as.data.frame(data)
data<-data[!(data$FC==0),]#去除0的,不然gseGO报错
ID_FC<-data


  #class(ID_FC[1,1])
  final_sort <- arrange(ID_FC, desc(FC))
  genelist <- final_sort$FC
  names(genelist) <- final_sort$ENTREZID
  
  #gsea分析KEGG
  gse_kegg <- gseKEGG(genelist,organism = "mmu",pvalueCutoff=1,pAdjustMethod = "BH")
  head(gse_kegg)
  gse_kegg <- setReadable(gse_kegg, 'org.Mm.eg.db', 'ENTREZID') %>% as.data.frame()

  #-gsea分析GO_BP
  gse_BP <- gseGO(genelist,OrgDb=org.Mm.eg.db,ont = "BP",pvalueCutoff = 1, pAdjustMethod = "BH")
  gse_BP <- setReadable(gse_BP, 'org.Mm.eg.db', 'ENTREZID') %>% as.data.frame()
  
  results<-rbind(gse_kegg,gse_BP)
  out<-paste(i,".cluster.GSEA.results.csv",sep="")
  setwd("/home/yifan/project/liuji/raw/Seurat/all/tumor")
  write.csv(results,out)
  
}

``````



``````
#################    msigdbr GSEA
#################获取GSEA基因集
library(clusterProfiler)
library(org.Mm.eg.db)
library(msigdbr)
library(dplyr)

##################################################
#colnames(mouse)
#H: hallmark gene sets;C1: positional gene sets; C2: curated gene sets.KEGG ;C3: motif gene sets;C4: computational gene sets
#C5: GO gene sets;C6: oncogenic signatures;C7: immunologic signatures
###################H
H <-msigdbr(species = "Mus musculus",category = "H") %>% as.data.frame %>% dplyr::select(gs_name,entrez_gene,gene_symbol) %>% as.data.frame
###################C1
#C1 <-msigdbr(species = "Mus musculus",category = "C1") %>% select(gs_name,entrez_gene,gene_symbol) %>% as.data.frame
###################C2
C2 <-msigdbr(species = "Mus musculus",category = "C2") %>% as.data.frame %>% dplyr::select(gs_name,entrez_gene,gene_symbol) %>% as.data.frame
###################C3
#C3 <-msigdbr(species = "Mus musculus",category = "C3") %>% select(gs_name,entrez_gene,gene_symbol) %>% as.data.frame
###################C4
#C4 <-msigdbr(species = "Mus musculus",category = "C4") %>% select(gs_name,entrez_gene,gene_symbol) %>% as.data.frame
###################C5
C5 <-msigdbr(species = "Mus musculus",category = "C5") %>% as.data.frame %>% dplyr::select(gs_name,entrez_gene,gene_symbol) %>% as.data.frame
###################C6
#C6 <-msigdbr(species = "Mus musculus",category = "C6") %>% select(gs_name,entrez_gene,gene_symbol) %>% as.data.frame
###################C7
#C7 <-msigdbr(species = "Mus musculus",category = "C7") %>% select(gs_name,entrez_gene,gene_symbol) %>% as.data.frame

#输入所有基因的"SYMBOL","foldChange"
df<-resMvsC_order[,c(1,3)]
colnames(df)[1]<-"SYMBOL"
colnames(df)[2]<-"foldChange"
#####id转换
df.id<-bitr(df$SYMBOL, fromType = "SYMBOL", toType = "ENTREZID",OrgDb = "org.Mm.eg.db")
#让基因名、ENTREZID、foldchange对应起来
easy.df<-merge(df,df.id,by="SYMBOL",all=F)
#按照foldchange排序
sortdf<-easy.df[order(easy.df$foldChange, decreasing = T),] %>%
  filter(is.na(foldChange)==FALSE)
gene.expr = sortdf$foldChange
names(gene.expr) <- sortdf$ENTREZID
################开始GSEA
################################1.H
em_msig <- GSEA(gene.expr,TERM2GENE=H[,c(1,2)])
kkx.H <- setReadable(em_msig, 'org.Mm.eg.db', 'ENTREZID') %>% as.data.frame()
################################C2
em_msig <- GSEA(gene.expr,TERM2GENE=C2[,c(1,2)])
kkx.C2 <- setReadable(em_msig, 'org.Mm.eg.db', 'ENTREZID') %>% as.data.frame()
################################C5
em_msig <- GSEA(gene.expr,TERM2GENE=C5[,c(1,2)])
kkx.C5 <- setReadable(em_msig, 'org.Mm.eg.db', 'ENTREZID') %>% as.data.frame()
#合并
results<-rbind(kkx.H,kkx.C2,kkx.C5)
write.csv(results,file = 'a5.GSEA.result.csv',quote = F,row.names = T,col.names = T)




你可能感兴趣的:(单细胞 (小鼠)| 差异分析+GSEA通路富集)