view test-data/out.rscript @ 5:44608d0193ed draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/volcanoplot commit 8464d1b013c316d88b37884be521c0ef50be5623"
author iuc
date Sun, 06 Jun 2021 09:12:22 +0000
parents
children 83c573f2e73c
line wrap: on
line source


# Galaxy settings start ---------------------------------------------------

# setup R error handling to go to stderr
options(show.error.messages = F, error = function() {cat(geterrmessage(), file = stderr()); q("no", 1, F)})

# we need that to not crash galaxy with an UTF8 error on German LC settings.
loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")


# Load packages -----------------------------------------------------------

suppressPackageStartupMessages({
    library(dplyr)
    library(ggplot2)
    library(ggrepel)
})


# Import data  ------------------------------------------------------------

# Check if header is present by checking if P value column is numeric or not

first_line <- read.delim('/private/var/folders/zn/m_qvr9zd7tq0wdtsbq255f8xypj_zg/T/tmpfpemuuun/files/8/3/7/dataset_8374ef6b-02c7-46f1-afc9-408a2a6cbde4.dat', header = FALSE, nrow = 1)

first_pvalue <- first_line[, 3]

if (is.numeric(first_pvalue)) {
  print("No header row detected")
  results <- read.delim('/private/var/folders/zn/m_qvr9zd7tq0wdtsbq255f8xypj_zg/T/tmpfpemuuun/files/8/3/7/dataset_8374ef6b-02c7-46f1-afc9-408a2a6cbde4.dat', header = FALSE)
} else {
  print("Header row detected")
  results <- read.delim('/private/var/folders/zn/m_qvr9zd7tq0wdtsbq255f8xypj_zg/T/tmpfpemuuun/files/8/3/7/dataset_8374ef6b-02c7-46f1-afc9-408a2a6cbde4.dat', header = TRUE)
}


# Format data  ------------------------------------------------------------

# Create columns from the column numbers specified
results <- results %>% mutate(fdr = .[[4]],
                              pvalue = .[[3]],
                              logfc = .[[2]],
                              labels = .[[1]])

# Get names for legend
down <- unlist(strsplit('Down,Not Sig,Up', split = ","))[1]
notsig <- unlist(strsplit('Down,Not Sig,Up', split = ","))[2]
up <- unlist(strsplit('Down,Not Sig,Up', split = ","))[3]

# Set colours
colours <- setNames(c("cornflowerblue", "grey", "firebrick"), c(down, notsig, up))

# Create significant (sig) column
results <- mutate(results, sig = case_when(
                                fdr < 0.05 & logfc > 0.0 ~ up, 
                                fdr < 0.05 & logfc < -0.0 ~ down, 
                                TRUE ~ notsig))


# Specify genes to label --------------------------------------------------
labelfile <- read.delim('/private/var/folders/zn/m_qvr9zd7tq0wdtsbq255f8xypj_zg/T/tmpfpemuuun/files/4/2/f/dataset_42fc8a63-f9cc-435b-9bb3-dd106b708cd9.dat')
results <- mutate(results, labels = ifelse(labels %in% labelfile[, 1], labels, ""))


# Create plot -------------------------------------------------------------

pdf("out.pdf")
p <- ggplot(results, aes(x = logfc, y = -log10(pvalue))) +
    geom_point(aes(colour = sig)) +
    scale_color_manual(values = colours) +
    scale_fill_manual(values = colours) +
    theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"),
        legend.key = element_blank())






# Set legend title
p <- p + labs(colour = "")

# Add gene labels in boxes
p <- p + geom_label_repel(aes(label = labels, fill = sig), 
                          segment.colour = "black", 
                          colour = "white", 
                          min.segment.length = 0, 
                          show.legend = FALSE)

print(p)
dev.off()


# Save RData -------------------------------------------------------------
save.image(file="volcanoplot.RData")


# R and Package versions -------------------------------------------------
sessionInfo()