comparison volcanoplot.R @ 0:59ebf2c42c0e draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/volcanoplot commit 7cc1a05f2868f270bf5bdbbd4820ef6f69c9fc8d
author iuc
date Tue, 23 Oct 2018 06:57:26 -0400
parents
children 7b7303fa20e3
comparison
equal deleted inserted replaced
-1:000000000000 0:59ebf2c42c0e
1 # setup R error handling to go to stderr
2 options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
3
4 # we need that to not crash galaxy with an UTF8 error on German LC settings.
5 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
6
7 suppressPackageStartupMessages({
8 library(dplyr)
9 library(getopt)
10 library(ggplot2)
11 library(ggrepel)
12 })
13
14 options(stringAsFactors = FALSE, useFancyQuotes = FALSE)
15 args <- commandArgs(trailingOnly = TRUE)
16
17 spec <- matrix(c(
18 "input", "i", 1, "character",
19 "fdr_col", "a", 1, "integer",
20 "pval_col", "p", 1, "integer",
21 "lfc_col", "c", 1, "integer",
22 "label_col", "l", 1, "integer",
23 "signif_thresh", "s", 1, "double",
24 "lfc_thresh", "x", 1, "double",
25 "label_file", "f", 1, "character",
26 "top_num", "t", 1, "integer",
27 "title", "T", 1, "character",
28 "xlab", "X", 1, "character",
29 "ylab", "Y", 1, "character",
30 "legend", "L", 1, "character",
31 "llabs", "z", 1, "character"),
32 byrow=TRUE, ncol=4)
33 opt <- getopt(spec)
34
35 # Below modified from http://www.gettinggeneticsdone.com/2016/01/repel-overlapping-text-labels-in-ggplot2.html
36
37 results <- read.delim(opt$input)
38 results$fdr <- results[, opt$fdr_col]
39 results$Pvalue <- results[, opt$pval_col]
40 results$logFC <- results[, opt$lfc_col]
41 results$labels <- results[, opt$label_col]
42 label_down <- unlist(strsplit(opt$llabs, split=","))[1]
43 label_notsig <- unlist(strsplit(opt$llabs, split=","))[2]
44 label_up <- unlist(strsplit(opt$llabs, split=","))[3]
45 colours <- setNames(c("cornflowerblue","grey","firebrick"),c(label_down,label_notsig,label_up))
46
47 results <- mutate(results, sig=ifelse((fdr<opt$signif_thresh & logFC>opt$lfc_thresh), label_up, ifelse((fdr<opt$signif_thresh & logFC < -opt$lfc_thresh),label_down, label_notsig)))
48 results <- results[order(results$Pvalue),]
49 if (!is.null(opt$label_file)) {
50 labelfile <- read.delim(opt$label_file)
51 tolabel <- filter(results, labels %in% labelfile[, 1])
52 } else if (!is.null(opt$top_num)) {
53 tolabel <- filter(results, fdr<opt$signif_thresh) %>% top_n(opt$top_num)
54 } else {
55 tolabel <- filter(results, fdr<opt$signif_thresh)
56 }
57
58 pdf("out.pdf")
59 p <- ggplot(results, aes(logFC, -log10(Pvalue))) +
60 geom_point(aes(col=sig)) +
61 geom_label_repel(data=tolabel, aes(label=labels, fill=factor(sig)), colour="white", segment.colour="black", show.legend=FALSE) +
62 scale_color_manual(values=colours) +
63 scale_fill_manual(values=colours) +
64 theme(panel.grid.major = element_blank(),
65 panel.grid.minor = element_blank(),
66 panel.background = element_blank(),
67 axis.line = element_line(colour = "black"),
68 legend.key=element_blank())
69 if (!is.null(opt$title)) {
70 p <- p + ggtitle(opt$title)
71 }
72 if (!is.null(opt$xlab)) {
73 p <- p + xlab(opt$xlab)
74 }
75 if (!is.null(opt$ylab)) {
76 p <- p + ylab(opt$ylab)
77 }
78 if (!is.null(opt$legend)) {
79 p <- p + labs(colour=opt$legend)
80 } else {
81 p <- p + labs(colour="")
82 }
83
84 print(p)
85 dev.off()
86
87 cat("Session information:\n\n")
88 sessionInfo()