annotate 16_fgsea/GSEA.R @ 1:91eed1b5c009 draft

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