comparison fgsea.R @ 0:9bb7943b5263 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/fgsea commit 9a6eda48463d6c19e9c5f3f2f8109f33de74855d-dirty
author iuc
date Sat, 20 Oct 2018 05:47:18 -0400
parents
children 17eb1e0d711f
comparison
equal deleted inserted replaced
-1:000000000000 0:9bb7943b5263
1 options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
2
3 # we need that to not crash galaxy with an UTF8 error on German LC settings.
4 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
5
6 suppressPackageStartupMessages({
7 library(fgsea)
8 library(ggplot2)
9 library(optparse)
10 })
11
12 option_list <- list(
13 make_option(c("-rnk_file", "--rnk_file"), type="character", help="Path to ranked genes file"),
14 make_option(c("-header", "--header"), type="logical", help = "Does ranked genes file have a header"),
15 make_option(c("-sets_file", "--sets_file"), type="character", help = "Path to gene sets file"),
16 make_option(c("-gmt", "--gmt"), type="logical", help = "Is the sets file in GMT format"),
17 make_option(c("-out_tab","--out_tab"), type="character", help="Path to output file"),
18 make_option(c("-min_size", "--min_size"), type="integer", help="Minimal size of a gene set to test. All pathways below the threshold are excluded."),
19 make_option(c("-max_size", "--max_size"), type="integer", help="Maximal size of a gene set to test. All pathways above the threshold are excluded."),
20 make_option(c("-n_perm", "--n_perm"), type="integer", help="Number of permutations to do. Minimial possible nominal p-value is about 1/nperm"),
21 make_option(c("-rda_opt", "--rda_opt"), type="logical", help="Output RData file"),
22 make_option(c("-plot_opt", "--plot_opt"), type="logical", help="Output plot"),
23 make_option(c("-top_num", "--top_num"), type="integer", help="Top number of pathways to plot")
24 )
25
26 parser <- OptionParser(usage = "%prog [options] file", option_list=option_list)
27 args = parse_args(parser)
28
29 # Vars:
30 rnk_file = args$rnk_file
31 if (args$header) {
32 header = TRUE
33 } else {
34 header = FALSE
35 }
36 sets_file = args$sets_file
37 gmt = args$gmt
38 out_tab = args$out_tab
39 min_size = args$min_size
40 max_size = args$max_size
41 n_perm = args$n_perm
42 rda_opt = args$rda_opt
43 plot_opt = args$plot_opt
44 top_num = args$top_num
45
46 ## Basically using the steps from the fgsea vignette
47 rankTab <- read.table(rnk_file, header=header, colClasses = c("character", "numeric"))
48
49 ranks <-rankTab[,2]
50 names(ranks) <- rankTab[,1]
51
52 if (gmt) {
53 pathways <- gmtPathways(sets_file)
54 } else {
55 pathways <- load(sets_file)
56 pathways <- get(pathways)
57 }
58
59 fgseaRes <- fgsea(pathways, ranks, minSize=min_size, maxSize=max_size, nperm=n_perm)
60 fgseaRes <- fgseaRes[order(pval), ]
61 # Convert leadingEdge column from list to character to output
62 fgseaRes$leadingEdge <- sapply(fgseaRes$leadingEdge, toString)
63
64 write.table(fgseaRes, out_tab, sep="\t", row.names=FALSE, quote=FALSE)
65
66 if (plot_opt) {
67 pdf("fgsea_plots.pdf", width=8)
68
69 topPathways <- head(fgseaRes, n=top_num)
70 topPathways <- topPathways$pathway
71
72 ## Make summary table plot for top pathways
73 plotGseaTable(pathways[topPathways], ranks, fgseaRes, gseaParam = 0.5,
74 colwidths = c(5.3,3,0.7, 0.9, 0.9))
75
76 # Make enrichment plots for top pathways
77 for (i in topPathways) {
78 p <- plotEnrichment(pathways[[i]], ranks) + labs(title=i)
79 print(p)
80 }
81
82 dev.off()
83 }
84
85 ## Output RData file
86 if (rda_opt) {
87 save.image(file = "fgsea_analysis.RData")
88 }