fgsea做GSEA
2020-07-19 本文已影响0人
生信编程日常
library(fgsea)
1.导入测试数据,fgesa的examplePathways,exampleRanks测试数据分别是通路的list和经过fold change排序的基因。
data(examplePathways)
data(exampleRanks)
![](https://img.haomeiwen.com/i20297934/9e1a3358341d0ddf.png)
![](https://img.haomeiwen.com/i20297934/d0f5d71168e54d82.png)
2.运行 fgsea
The resulting table contains enrichment scores and p-values:
fgseaRes <- fgsea(pathways = examplePathways,
stats = exampleRanks,
minSize=15,
maxSize=500,
nperm=10000)
head(fgseaRes[order(pval), ])
![](https://img.haomeiwen.com/i20297934/46b3da6b74369011.png)
3.计算padj<0.01的个数
sum(fgseaRes[, padj < 0.01])
4.画特定的通路
plotEnrichment(examplePathways[["5991130_Programmed_Cell_Death"]],
exampleRanks) + labs(title="Programmed Cell Death")
![](https://img.haomeiwen.com/i20297934/e7899afb18669971.png)
5.选择上调下调的top10的pathtway
topPathwaysUp <- fgseaRes[ES > 0][head(order(pval), n=10), pathway]
topPathwaysDown <- fgseaRes[ES < 0][head(order(pval), n=10), pathway]
topPathways <- c(topPathwaysUp, rev(topPathwaysDown))
plotGseaTable(examplePathways[topPathways], exampleRanks, fgseaRes,
gseaParam = 0.5)
![](https://img.haomeiwen.com/i20297934/d868b4f2092f499c.png)
From the plot above one can see that there are very similar pathways in the table (for example 5991502_Mitotic_Metaphase_and_Anaphase and 5991600_Mitotic_Anaphase). To select only independent pathways one can use collapsePathways function:
collapsedPathways <- collapsePathways(fgseaRes[order(pval)][padj < 0.01],
examplePathways, exampleRanks)
mainPathways <- fgseaRes[pathway %in% collapsedPathways$mainPathways][
order(-NES), pathway]
plotGseaTable(examplePathways[mainPathways], exampleRanks, fgseaRes,
gseaParam = 0.5)
![](https://img.haomeiwen.com/i20297934/6fbfc66298e6dfb9.png)
最后保存结果。
library(data.table)
fwrite(fgseaRes, file="fgseaRes.txt", sep="\t", sep2=c("", " ", ""))