From 8ce744dcc6f96c3eb7ba92980741b4832237bad0 Mon Sep 17 00:00:00 2001 From: MaraBesemer Date: Wed, 27 Nov 2024 09:24:27 +0100 Subject: [PATCH] add ggplot2 to macros and R linting fixing --- tools/phyloseq/macros.xml | 1 + tools/phyloseq/phyloseq_plot_bar.R | 54 +++++++++++++++--------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/tools/phyloseq/macros.xml b/tools/phyloseq/macros.xml index ebe365840ac..312bcccd9a3 100644 --- a/tools/phyloseq/macros.xml +++ b/tools/phyloseq/macros.xml @@ -12,6 +12,7 @@ bioconductor-phyloseq r-optparse r-tidyverse + r-ggplot2 diff --git a/tools/phyloseq/phyloseq_plot_bar.R b/tools/phyloseq/phyloseq_plot_bar.R index 237abf2d658..0a3aba58490 100755 --- a/tools/phyloseq/phyloseq_plot_bar.R +++ b/tools/phyloseq/phyloseq_plot_bar.R @@ -7,26 +7,26 @@ suppressPackageStartupMessages(library("ggplot2")) # Define options option_list <- list( - make_option(c("--input"), - action = "store", dest = "input", - help = "Input file containing a phyloseq object" - ), - make_option(c("--x"), - action = "store", dest = "x", - help = "Variable for x-axis (e.g., 'Sample', 'Phylum')" - ), - make_option(c("--fill"), - action = "store", dest = "fill", - help = "Variable for fill color (e.g., 'Genus', 'Order')" - ), - make_option(c("--facet"), - action = "store", dest = "facet", default = NULL, - help = "Facet by variable (optional)" - ), - make_option(c("--output"), - action = "store", dest = "output", - help = "Output file (PDF)" - ) + make_option(c("--input"), + action = "store", dest = "input", + help = "Input file containing a phyloseq object" + ), + make_option(c("--x"), + action = "store", dest = "x", + help = "Variable for x-axis (e.g., 'Sample', 'Phylum')" + ), + make_option(c("--fill"), + action = "store", dest = "fill", + help = "Variable for fill color (e.g., 'Genus', 'Order')" + ), + make_option(c("--facet"), + action = "store", dest = "facet", default = NULL, + help = "Facet by variable (optional)" + ), + make_option(c("--output"), + action = "store", dest = "output", + help = "Output file (PDF)" + ) ) # Parse arguments @@ -40,11 +40,11 @@ physeq <- readRDS(opt$input) # Check if the 'x' and 'fill' variables are valid if (!opt$x %in% colnames(sample_data(physeq))) { - stop(paste("Error: x variable", opt$x, "does not exist in the sample data.")) + stop(paste("Error: x variable", opt$x, "does not exist in the sample data.")) } if (!opt$fill %in% colnames(sample_data(physeq))) { - stop(paste("Error: fill variable", opt$fill, "does not exist in the sample data.")) + stop(paste("Error: fill variable", opt$fill, "does not exist in the sample data.")) } # Generate bar plot @@ -52,11 +52,11 @@ p <- plot_bar(physeq, x = opt$x, fill = opt$fill) # Only facet if the facet variable is provided and exists in the sample data if (!is.null(opt$facet)) { - if (opt$facet %in% colnames(sample_data(physeq))) { - p <- p + facet_wrap(as.formula(paste("~", opt$facet))) - } else { - warning(paste("Facet variable", opt$facet, "does not exist in the sample data. Faceting will be skipped.")) - } + if (opt$facet %in% colnames(sample_data(physeq))) { + p <- p + facet_wrap(as.formula(paste("~", opt$facet))) + } else { + warning(paste("Facet variable", opt$facet, "does not exist in the sample data. Faceting will be skipped.")) + } } # Save to output file