annotate 16_fgsea/GSEA.R @ 2:d91ddc13f8a8 draft

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