-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranked_gene_enrichment_test.R
executable file
·47 lines (40 loc) · 1.41 KB
/
ranked_gene_enrichment_test.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly = TRUE)
suppressPackageStartupMessages({
library(dplyr)
library(ggplot2)
library(data.table)
library(fgsea)})
magma_genes = fread(args[1], header = T)
magma_genes = magma_genes %>%
arrange(desc(ZSTAT)) # Sort by MAGMA Z to get ranked list
ranked_genes = magma_genes$ZSTAT
names(ranked_genes) = magma_genes$GENE
go_resource = gmtPathways(args[2])
gsea_result = fgseaMultilevel(pathways = go_resource,
stats = ranked_genes,
minSize = 15,
maxSize = 2000,
eps = 0,
nPermSimple = 100000) %>%
as.data.frame() %>%
arrange(desc(padj))
# Select independent pathways
concise_pathways = collapsePathways(as.data.table(gsea_result),
pathways = go_resource,
stats = ranked_genes)
concise_pathways = concise_pathways$mainPathways %>% as.data.frame()
colnames(concise_pathways) = c("pathway")
gsea_concise_result = inner_join(gsea_result,
concise_pathways,
by = c("pathway"))
fwrite(gsea_result,
paste0(args[3], "_GSEA_Results.txt"),
sep = "\t",
row.names = F,
quote = F)
fwrite(gsea_concise_result,
paste0(args[3], "_GSEA_Concise_Results.txt"),
sep = "\t",
row.names = F,
quote = F)