annotate GSEA.R @ 0:3a53b8e91ede draft

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