view test-data/out.rscript @ 9:ab01e379d29e draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/volcanoplot commit 060f6734402e7d2eced6227663cc61d2cb865ae5
author iuc
date Mon, 09 Dec 2024 16:04:24 +0000
parents 83c573f2e73c
children 99ace6c1ff57
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")

# Galaxy settings end -----------------------------------------------------

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

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


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

results <- read.delim('/tmp/tmpl4o1f_bf/files/5/2/5/dataset_52538741-d085-42da-817b-263bd4f7cf98.dat', header = TRUE)


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

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

# Check if shape_col is provided 

# 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 --------------------------------------------------

# Import file with genes of interest
labelfile <- read.delim('/tmp/tmpl4o1f_bf/files/5/d/4/dataset_5d401b02-f6af-4ed9-b853-992fd4a4d044.dat', header = TRUE)

# Label the genes of interest in results table
results <- mutate(results, labels = ifelse(labels %in% labelfile[, 1], labels, ""))



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

# Open file to save plot as PDF
pdf("volcano_plot.pdf")

# Set up base plot with faceting by category_symbol instead of shapes
p <- ggplot(data = results, aes(x = logfc, y = -log10(pvalue))) +
    scale_color_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()) 

# Conditional logic to use either shape or facet based on user selection
p <- p + geom_point(aes(colour = sig)) #only add color

# Add gene labels
p <- p + geom_text_repel(data = filter(results, labels != ""), aes(label = labels),
                         min.segment.length = 0,
                         max.overlaps = Inf,
                         show.legend = FALSE)






# Set legend title
p <- p + theme(legend.title = element_blank())

# Print plot
print(p)

# Close PDF graphics device
dev.off()


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