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

Uploaded
author dktanwar
date Mon, 11 Dec 2017 09:52:26 -0500
parents
children 9e75d29c2409
line wrap: on
line source

## How to execute this tool
# $Rscript GSEA.R --input ranked_genes_list.rnk --input Mus_musculus_GSEA_GO_sets_all_symbols_highquality_April_2015.gmt 
# --output GSEA_results.txt --output 

# Send R errors to stderr
options(show.error.messages = F, error = function(){cat(geterrmessage(), file = stderr()); q("no", 1, F)})

# Avoid crashing Galaxy with an UTF8 error on German LC settings
loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")

# Import library
library("getopt")
library("fgsea")
library("Rcpp")

options(stringAsfactors = FALSE, useFancyQuotes = FALSE)

# Take in trailing command line arguments
args <- commandArgs(trailingOnly = TRUE)

# Get options using the spec as defined by the enclosed list
# Options are read from the default: commandArgs(TRUE)
option_specification = matrix(c(
  'input1', 'i1', 2, 'character',
  'input2', 'i2', 2, 'character',
  'output', 'o', 2, 'character'
), byrow = TRUE, ncol = 4);

# Parse options
options = getopt(option_specification);

# Print options to stderr for debugging
# cat("\n input: ", options$input1)
# cat("\n input: ", options$input2)
# cat("\n output: ", options$output)

# Rank file
ranks <- read.table(options$input1, header=F, colClasses = c("character", "numeric"))
ranks <- setNames(ranks[,2], ranks[,1])

# Pathways database
pathways <- gmtPathways(options$input2)

# running analysis
fgseaRes <- fgsea(pathways, ranks, minSize=10, maxSize=500, nperm=1000)
res <- as.data.frame(fgseaRes[order(pval), ], stringsAsFactors = F)

# save results
write.table(x = res[,1:7], file = options$output, quote = F, row.names = F, sep = "\t")
# 
# 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))
# 
# pdf(paste0(options$output, ".pdf"), width = 8.5, height = 11)
# plotGseaTable(pathways[topPathways], ranks, fgseaRes,gseaParam = 0.5)
# z <- dev.off()