?詳情請聯(lián)系作者:
?
library(Seurat)
library(msigdbr)
library(fgsea)
library(clusterProfiler)
library(ggplot2)
#加載函數(shù)
source('./KS_GSEA.R')
source('./KS_GSEA_plot.R')
#差異基因-----------------------------------------------------------------------
load("D:/KS項目/公眾號文章/GSEA分析及可視化函數(shù)/sce_mar.RData")
df <- FindMarkers(Macrophage, min.pct = 0, logfc.threshold = 0,
group.by = "group",ident.1 ="BM",ident.2="GM")
df$gene = rownames(df)
#GSEA分析-----------------------------------------------------------------------
GSEA_CP <- KS_GSEA(gene = df$gene,
LogFC = df$avg_log2FC,
analysis = "KEGG",
package = 'clusterProfiler',
OrgDb = 'org.Hs.eg.db')
class(GSEA_CP)
# [1] "gseaResult"
# attr(,"package")
# [1] "DOSE"
GSEA_F <- KS_GSEA(gene = df$gene,
LogFC = df$avg_log2FC,
analysis = "KEGG",
package = 'fgsea',
OrgDb = 'org.Hs.eg.db')
class(GSEA_F)
# [1] "data.table" "data.frame"
可以看到,clusterProfiler包返回的是一個gseaResult,fgsea包返回的是一個"data.table","data.frame"。這些結(jié)果就是我們下一步可視化的輸入文件。運行結(jié)束后,分析結(jié)果已txt或者csv的形式直接保存到當(dāng)前環(huán)境路徑下!
我們對可視化函數(shù)進行了設(shè)置,NES>0的結(jié)果點用紅色顯示。NES<0的結(jié)果用藍(lán)色點顯示。這里我們挑選自己感興趣的通路進行可視化。
#NES>0
p1=KS_GSEA_plot(inputType = "clusterProfiler",
analysis = "KEGG",
data = GSEA_CP,
term = 'Oxidative phosphorylation',
gene = df$gene,
LogFC = df$avg_log2FC,
OrgDb = 'org.Hs.eg.db')
p2=KS_GSEA_plot(inputType = "fgsea",
analysis = "KEGG",
data = GSEA_F,
term = 'Huntingtons disease',
gene = df$gene,
LogFC = df$avg_log2FC,
OrgDb = 'org.Hs.eg.db')
p1+p2
NES<0,這里需要注意一個問題,clusterProfiler和fgsea雖然都是GSEA分析,但是兩者得到的結(jié)果并不是一摸一樣完全相同的,總是有一些出入,這是因為數(shù)據(jù)庫,分析方式不一樣。選擇需要的包使用一個即可。
#NES<0
p3=KS_GSEA_plot(inputType = "clusterProfiler",
analysis = "KEGG",
data = GSEA_CP,
term = 'JAK-STAT signaling pathway',
gene = df$gene,
LogFC = df$avg_log2FC,
OrgDb = 'org.Hs.eg.db')
p4=KS_GSEA_plot(inputType = "fgsea",
analysis = "KEGG",
data = GSEA_F,
term = 'Jak stat signaling pathway',
gene = df$gene,
LogFC = df$avg_log2FC,
OrgDb = 'org.Hs.eg.db')
p3+p4
當(dāng)然了,我們可以利用循環(huán)一次性可視化通路。
#批量循環(huán)
#NES>0
pathway <- c('Oxidative phosphorylation',
"Parkinson disease",
"Biosynthesis of amino acids",
"Cardiac muscle contraction")
pathway_list <- list()
for (i in 1:length(pathway)) {
p = KS_GSEA_plot(inputType = "clusterProfiler",
analysis = "KEGG",
data = GSEA_CP,
term = pathway[i],
gene = df$gene,
LogFC = df$avg_log2FC,
OrgDb = 'org.Hs.eg.db')
pathway_list[[i]] <- p
}
#組合圖
CombinePlots(pathway_list, ncol = 2)
用另一個數(shù)據(jù)批量做一下下調(diào)的。
#NES<0
pathway1 <- c('Melanoma',
'Prostate cancer',
'Ecm receptor interaction',
'Tgf beta signaling pathway')
pathway_list1 <- list()
for (i in 1:length(pathway)) {
p = KS_GSEA_plot(inputType = "fgsea",
analysis = "KEGG",
data = GSEA_F,
term = pathway1[i],
gene = df$gene,
LogFC = df$avg_log2FC,
OrgDb = 'org.Hs.eg.db')
pathway_list1[[i]] <- p
}
#組合圖
CombinePlots(pathway_list1, ncol = 2)
這就是所有內(nèi)容了,希望對你有所啟發(fā)。這個函數(shù)其實并不是很完美,首先是物種只支持小鼠和人,其次是分析參數(shù)我沒有再設(shè)置,用的是我常用的。當(dāng)然了,這個函數(shù)框架我提供了,需要更加個性化的可以自行修改。覺得分享有用的,點個贊唄!
聯(lián)系客服