comparison 16_fgsea/GSEA.R @ 0:b75ceb945966 draft

Uploaded
author dktanwar
date Mon, 11 Dec 2017 09:52:26 -0500
parents
children 9e75d29c2409
comparison
equal deleted inserted replaced
-1:000000000000 0:b75ceb945966
1 ## How to execute this tool
2 # $Rscript GSEA.R --input ranked_genes_list.rnk --input Mus_musculus_GSEA_GO_sets_all_symbols_highquality_April_2015.gmt
3 # --output GSEA_results.txt --output
4
5 # Send R errors to stderr
6 options(show.error.messages = F, error = function(){cat(geterrmessage(), file = stderr()); q("no", 1, F)})
7
8 # Avoid crashing Galaxy with an UTF8 error on German LC settings
9 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
10
11 # Import library
12 library("getopt")
13 library("fgsea")
14 library("Rcpp")
15
16 options(stringAsfactors = FALSE, useFancyQuotes = FALSE)
17
18 # Take in trailing command line arguments
19 args <- commandArgs(trailingOnly = TRUE)
20
21 # Get options using the spec as defined by the enclosed list
22 # Options are read from the default: commandArgs(TRUE)
23 option_specification = matrix(c(
24 'input1', 'i1', 2, 'character',
25 'input2', 'i2', 2, 'character',
26 'output', 'o', 2, 'character'
27 ), byrow = TRUE, ncol = 4);
28
29 # Parse options
30 options = getopt(option_specification);
31
32 # Print options to stderr for debugging
33 # cat("\n input: ", options$input1)
34 # cat("\n input: ", options$input2)
35 # cat("\n output: ", options$output)
36
37 # Rank file
38 ranks <- read.table(options$input1, header=F, colClasses = c("character", "numeric"))
39 ranks <- setNames(ranks[,2], ranks[,1])
40
41 # Pathways database
42 pathways <- gmtPathways(options$input2)
43
44 # running analysis
45 fgseaRes <- fgsea(pathways, ranks, minSize=10, maxSize=500, nperm=1000)
46 res <- as.data.frame(fgseaRes[order(pval), ], stringsAsFactors = F)
47
48 # save results
49 write.table(x = res[,1:7], file = options$output, quote = F, row.names = F, sep = "\t")
50 #
51 # topPathwaysUp <- fgseaRes[ES > 0][head(order(pval), n=10), pathway]
52 # topPathwaysDown <- fgseaRes[ES < 0][head(order(pval), n=10), pathway]
53 # topPathways <- c(topPathwaysUp, rev(topPathwaysDown))
54 #
55 # pdf(paste0(options$output, ".pdf"), width = 8.5, height = 11)
56 # plotGseaTable(pathways[topPathways], ranks, fgseaRes,gseaParam = 0.5)
57 # z <- dev.off()