From a1ab8f97e2f40936fd313e2a76a7d815c7d066fa Mon Sep 17 00:00:00 2001 From: Mara Besemer Date: Tue, 19 Nov 2024 16:04:19 +0100 Subject: [PATCH 01/14] add phyloseq bar chart --- tools/phyloseq/phyloseq_plot_bar.R | 33 +++++++++++++++ tools/phyloseq/phyloseq_plot_bar.xml | 61 ++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100755 tools/phyloseq/phyloseq_plot_bar.R create mode 100644 tools/phyloseq/phyloseq_plot_bar.xml diff --git a/tools/phyloseq/phyloseq_plot_bar.R b/tools/phyloseq/phyloseq_plot_bar.R new file mode 100755 index 00000000000..aaf71841156 --- /dev/null +++ b/tools/phyloseq/phyloseq_plot_bar.R @@ -0,0 +1,33 @@ +#!/usr/bin/env Rscript + +# Load libraries +suppressPackageStartupMessages(library("optparse")) +suppressPackageStartupMessages(library("phyloseq")) +suppressPackageStartupMessages(library("ggplot2")) + +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)") +) + +# Parse arguments +parser <- OptionParser(usage = "%prog [options] file", option_list = option_list) +args <- parse_args(parser) +opt <- args$options + +# Load phyloseq object +physeq <- readRDS(opt$input) + +# Generate bar plot +p <- plot_bar(physeq, x = opt$x, fill = opt$fill) + +# Add faceting if specified +if (!is.null(opt$facet)) { + p <- p + facet_wrap(as.formula(paste("~", opt$facet))) +} + +# Save to output file +ggsave(opt$output, plot = p, width = 10, height = 8) diff --git a/tools/phyloseq/phyloseq_plot_bar.xml b/tools/phyloseq/phyloseq_plot_bar.xml new file mode 100644 index 00000000000..79bdb86e8ee --- /dev/null +++ b/tools/phyloseq/phyloseq_plot_bar.xml @@ -0,0 +1,61 @@ + + Generate bar charts from a phyloseq object + + macros.xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + **Description** + + This tool generates bar charts from a phyloseq object using the `plot_bar` function. + + **Inputs** + + - **Input**: A phyloseq object in RDS format. + - **X-axis variable**: The variable to use for the x-axis (e.g., Sample, Phylum). + - **Fill variable**: The variable to use for the bar fill colors (e.g., Genus, Order). + - **Facet by variable**: (Optional) A variable to facet the bar chart (e.g., SampleType). + + **Outputs** + + - A PDF file containing the bar chart. + + **Usage Notes** + + Ensure that the input file is a valid phyloseq object in RDS format. + + + From 0d8ca83889853d90459d8e6dba941ecb13da9c0f Mon Sep 17 00:00:00 2001 From: Mara Besemer Date: Tue, 19 Nov 2024 16:12:02 +0100 Subject: [PATCH 02/14] small changes --- tools/phyloseq/phyloseq_plot_bar.R | 1 - tools/phyloseq/phyloseq_plot_bar.xml | 1 - 2 files changed, 2 deletions(-) diff --git a/tools/phyloseq/phyloseq_plot_bar.R b/tools/phyloseq/phyloseq_plot_bar.R index aaf71841156..4d60ea2ca89 100755 --- a/tools/phyloseq/phyloseq_plot_bar.R +++ b/tools/phyloseq/phyloseq_plot_bar.R @@ -24,7 +24,6 @@ physeq <- readRDS(opt$input) # Generate bar plot p <- plot_bar(physeq, x = opt$x, fill = opt$fill) -# Add faceting if specified if (!is.null(opt$facet)) { p <- p + facet_wrap(as.formula(paste("~", opt$facet))) } diff --git a/tools/phyloseq/phyloseq_plot_bar.xml b/tools/phyloseq/phyloseq_plot_bar.xml index 79bdb86e8ee..9c12f0dd0b0 100644 --- a/tools/phyloseq/phyloseq_plot_bar.xml +++ b/tools/phyloseq/phyloseq_plot_bar.xml @@ -23,7 +23,6 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R' - From b35e149a233ed41802042e6bf108bed5d4cb2bc6 Mon Sep 17 00:00:00 2001 From: Mara Besemer Date: Tue, 19 Nov 2024 17:30:23 +0100 Subject: [PATCH 03/14] fix R Script linting --- tools/phyloseq/phyloseq_plot_bar.R | 41 +++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/tools/phyloseq/phyloseq_plot_bar.R b/tools/phyloseq/phyloseq_plot_bar.R index 4d60ea2ca89..dc816409013 100755 --- a/tools/phyloseq/phyloseq_plot_bar.R +++ b/tools/phyloseq/phyloseq_plot_bar.R @@ -6,15 +6,44 @@ suppressPackageStartupMessages(library("phyloseq")) suppressPackageStartupMessages(library("ggplot2")) 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 -parser <- OptionParser(usage = "%prog [options] file", option_list = option_list) +parser <- OptionParser( + usage = "%prog [options] file", + option_list = option_list +) args <- parse_args(parser) opt <- args$options From 62005bc22bb0db9cd669644942344ca8e8975ac8 Mon Sep 17 00:00:00 2001 From: Mara Besemer Date: Wed, 20 Nov 2024 10:21:09 +0100 Subject: [PATCH 04/14] fix lintr --- tools/phyloseq/phyloseq_plot_bar.R | 41 ++++++++---------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/tools/phyloseq/phyloseq_plot_bar.R b/tools/phyloseq/phyloseq_plot_bar.R index dc816409013..a52ccfa9322 100755 --- a/tools/phyloseq/phyloseq_plot_bar.R +++ b/tools/phyloseq/phyloseq_plot_bar.R @@ -6,37 +6,16 @@ suppressPackageStartupMessages(library("phyloseq")) suppressPackageStartupMessages(library("ggplot2")) 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 From 199e4343dda9a79cee5ad20ab30c1357395ec5d1 Mon Sep 17 00:00:00 2001 From: Mara Besemer Date: Wed, 20 Nov 2024 10:31:12 +0100 Subject: [PATCH 05/14] fix lintr error --- tools/phyloseq/phyloseq_plot_bar.R | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tools/phyloseq/phyloseq_plot_bar.R b/tools/phyloseq/phyloseq_plot_bar.R index a52ccfa9322..ef159ab2553 100755 --- a/tools/phyloseq/phyloseq_plot_bar.R +++ b/tools/phyloseq/phyloseq_plot_bar.R @@ -6,15 +6,20 @@ suppressPackageStartupMessages(library("phyloseq")) suppressPackageStartupMessages(library("ggplot2")) option_list <- list( - make_option(c("--input"), action = "store", dest = "input", + make_option(c("--input"), + action = "store", dest = "input", help = "Input file containing a phyloseq object"), - make_option(c("--x"), action = "store", dest = "x", + 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", + 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, + make_option(c("--facet"), + action = "store", dest = "facet", default = NULL, help = "Facet by variable (optional)"), - make_option(c("--output"), action = "store", dest = "output", + make_option(c("--output"), + action = "store", dest = "output", help = "Output file (PDF)") ) From f3553ba916b9e1b9de3881d5a1a06bb3b44f3d63 Mon Sep 17 00:00:00 2001 From: Mara Besemer Date: Wed, 20 Nov 2024 13:45:24 +0100 Subject: [PATCH 06/14] fix linting with styler.R --- tools/phyloseq/phyloseq_plot_bar.R | 42 +++++++++++++++++------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/tools/phyloseq/phyloseq_plot_bar.R b/tools/phyloseq/phyloseq_plot_bar.R index ef159ab2553..847de4ffb96 100755 --- a/tools/phyloseq/phyloseq_plot_bar.R +++ b/tools/phyloseq/phyloseq_plot_bar.R @@ -5,28 +5,34 @@ suppressPackageStartupMessages(library("optparse")) suppressPackageStartupMessages(library("phyloseq")) 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 parser <- OptionParser( - usage = "%prog [options] file", - option_list = option_list + usage = "%prog [options] file", + option_list = option_list ) args <- parse_args(parser) opt <- args$options @@ -38,7 +44,7 @@ physeq <- readRDS(opt$input) p <- plot_bar(physeq, x = opt$x, fill = opt$fill) if (!is.null(opt$facet)) { - p <- p + facet_wrap(as.formula(paste("~", opt$facet))) + p <- p + facet_wrap(as.formula(paste("~", opt$facet))) } # Save to output file From 7aec94118dd44d8359c17501621ac94fdcd38a8b Mon Sep 17 00:00:00 2001 From: MaraBesemer Date: Tue, 26 Nov 2024 15:47:22 +0100 Subject: [PATCH 07/14] change testinputs --- tools/phyloseq/phyloseq_plot_bar.R | 24 ++++++++++++++++++------ tools/phyloseq/phyloseq_plot_bar.xml | 7 ++++--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/tools/phyloseq/phyloseq_plot_bar.R b/tools/phyloseq/phyloseq_plot_bar.R index 847de4ffb96..7bb107356e7 100755 --- a/tools/phyloseq/phyloseq_plot_bar.R +++ b/tools/phyloseq/phyloseq_plot_bar.R @@ -30,21 +30,33 @@ option_list <- list( ) # Parse arguments -parser <- OptionParser( - usage = "%prog [options] file", - option_list = option_list -) -args <- parse_args(parser) +parser <- OptionParser(usage = "%prog [options] file", option_list = option_list) +args <- parse_args(parser, positional_arguments = TRUE) opt <- args$options # Load phyloseq object +print(paste("Trying to read:", opt$input)) 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.")) +} + +if (!opt$fill %in% colnames(sample_data(physeq))) { + stop(paste("Error: fill variable", opt$fill, "does not exist in the sample data.")) +} + # Generate bar plot 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)) { - p <- p + facet_wrap(as.formula(paste("~", 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.")) + } } # Save to output file diff --git a/tools/phyloseq/phyloseq_plot_bar.xml b/tools/phyloseq/phyloseq_plot_bar.xml index 9c12f0dd0b0..75d96b1d06c 100644 --- a/tools/phyloseq/phyloseq_plot_bar.xml +++ b/tools/phyloseq/phyloseq_plot_bar.xml @@ -25,9 +25,9 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R' - - - + + + @@ -58,3 +58,4 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R' + From 435232ecbd1f72df85909114063915ad050c0467 Mon Sep 17 00:00:00 2001 From: MaraBesemer Date: Tue, 26 Nov 2024 16:03:56 +0100 Subject: [PATCH 08/14] fix R linting --- tools/phyloseq/phyloseq_plot_bar.R | 50 +++++++++++++++--------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/tools/phyloseq/phyloseq_plot_bar.R b/tools/phyloseq/phyloseq_plot_bar.R index 7bb107356e7..237abf2d658 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 @@ -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 From 8ce744dcc6f96c3eb7ba92980741b4832237bad0 Mon Sep 17 00:00:00 2001 From: MaraBesemer Date: Wed, 27 Nov 2024 09:24:27 +0100 Subject: [PATCH 09/14] 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 From 6aed8fb5f75d0c6d5a7ba317ebb27cd83bd8cc1b Mon Sep 17 00:00:00 2001 From: MaraBesemer Date: Sun, 1 Dec 2024 15:20:38 +0100 Subject: [PATCH 10/14] reset last commit --- 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 From d914de76069036aba427e3fe793c8699eaa70934 Mon Sep 17 00:00:00 2001 From: MaraBesemer Date: Sun, 1 Dec 2024 16:03:41 +0100 Subject: [PATCH 11/14] add more tests --- tools/phyloseq/macros.xml | 1 - tools/phyloseq/phyloseq_plot_bar.xml | 24 +++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/tools/phyloseq/macros.xml b/tools/phyloseq/macros.xml index 312bcccd9a3..ebe365840ac 100644 --- a/tools/phyloseq/macros.xml +++ b/tools/phyloseq/macros.xml @@ -12,7 +12,6 @@ bioconductor-phyloseq r-optparse r-tidyverse - r-ggplot2 diff --git a/tools/phyloseq/phyloseq_plot_bar.xml b/tools/phyloseq/phyloseq_plot_bar.xml index 75d96b1d06c..aeb2385ad8b 100644 --- a/tools/phyloseq/phyloseq_plot_bar.xml +++ b/tools/phyloseq/phyloseq_plot_bar.xml @@ -22,7 +22,8 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R' - + + @@ -35,6 +36,27 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R' + + + + + + + 1 + + + + + + + + + + + + + + **Description** From 805656a032001247681dce711df6f9bc71f39616 Mon Sep 17 00:00:00 2001 From: MaraBesemer Date: Sun, 1 Dec 2024 17:08:17 +0100 Subject: [PATCH 12/14] add improve plot saving in phyloseq_plot_bar --- tools/phyloseq/phyloseq_plot_bar.R | 35 ++++++++++++----- tools/phyloseq/phyloseq_plot_bar.xml | 56 ++++++++++++++-------------- 2 files changed, 54 insertions(+), 37 deletions(-) diff --git a/tools/phyloseq/phyloseq_plot_bar.R b/tools/phyloseq/phyloseq_plot_bar.R index 0a3aba58490..af86b57fd1a 100755 --- a/tools/phyloseq/phyloseq_plot_bar.R +++ b/tools/phyloseq/phyloseq_plot_bar.R @@ -34,30 +34,47 @@ parser <- OptionParser(usage = "%prog [options] file", option_list = option_list args <- parse_args(parser, positional_arguments = TRUE) opt <- args$options +# Validate required options +if (is.null(opt$input) || opt$input == "") { + stop("Error: Input file is required.") +} +if (is.null(opt$x) || opt$x == "") { + stop("Error: X-axis variable is required.") +} +if (is.null(opt$fill) || opt$fill == "") { + stop("Error: Fill variable is required.") +} +if (is.null(opt$output) || opt$output == "") { + stop("Error: Output file is required.") +} + # Load phyloseq object print(paste("Trying to read:", opt$input)) 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.")) +sample_vars <- colnames(sample_data(physeq)) +if (!opt$x %in% sample_vars) { + stop(paste("Error: X-axis 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.")) +if (!opt$fill %in% sample_vars) { + stop(paste("Error: Fill variable", opt$fill, "does not exist in the sample data.")) } # Generate bar plot 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))) { +if (!is.null(opt$facet) && opt$facet != "") { + if (opt$facet %in% sample_vars) { 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 -ggsave(opt$output, plot = p, width = 10, height = 8) +# Save to output file using PDF device +print(paste("Saving plot to:", opt$output)) +pdf(file = opt$output, width = 10, height = 8) +print(p) +dev.off() diff --git a/tools/phyloseq/phyloseq_plot_bar.xml b/tools/phyloseq/phyloseq_plot_bar.xml index aeb2385ad8b..914249e030e 100644 --- a/tools/phyloseq/phyloseq_plot_bar.xml +++ b/tools/phyloseq/phyloseq_plot_bar.xml @@ -25,39 +25,39 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R' - - - - - - - - - - + + + + + + + + + + - + - - - - - 1 + + + + 1 - + - - - - - - - - - - + + + + + + + + + + - + + **Description** From b89f35c8e7a016adebd496e30c8f39045986149b Mon Sep 17 00:00:00 2001 From: MaraBesemer Date: Sun, 1 Dec 2024 17:09:39 +0100 Subject: [PATCH 13/14] fix: update test parameters for phyloseq_plot_bar to ensure valid input --- tools/phyloseq/phyloseq_plot_bar.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/phyloseq/phyloseq_plot_bar.xml b/tools/phyloseq/phyloseq_plot_bar.xml index 914249e030e..e91dc9dfcaf 100644 --- a/tools/phyloseq/phyloseq_plot_bar.xml +++ b/tools/phyloseq/phyloseq_plot_bar.xml @@ -39,7 +39,7 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R' - + 1 From 79169e6b7f1f393107b0f4986d23e7deef9aa3de Mon Sep 17 00:00:00 2001 From: MaraBesemer Date: Sun, 1 Dec 2024 21:44:43 +0100 Subject: [PATCH 14/14] fix inputs --- tools/phyloseq/phyloseq_plot_bar.R | 10 ++-------- tools/phyloseq/phyloseq_plot_bar.xml | 11 ++--------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/tools/phyloseq/phyloseq_plot_bar.R b/tools/phyloseq/phyloseq_plot_bar.R index af86b57fd1a..d0d5830f39a 100755 --- a/tools/phyloseq/phyloseq_plot_bar.R +++ b/tools/phyloseq/phyloseq_plot_bar.R @@ -16,8 +16,8 @@ option_list <- list( 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')" + action = "store", dest = "fill", default = NULL, + help = "Variable for fill color (e.g., 'Genus', 'Order') (optional)" ), make_option(c("--facet"), action = "store", dest = "facet", default = NULL, @@ -41,9 +41,6 @@ if (is.null(opt$input) || opt$input == "") { if (is.null(opt$x) || opt$x == "") { stop("Error: X-axis variable is required.") } -if (is.null(opt$fill) || opt$fill == "") { - stop("Error: Fill variable is required.") -} if (is.null(opt$output) || opt$output == "") { stop("Error: Output file is required.") } @@ -57,9 +54,6 @@ sample_vars <- colnames(sample_data(physeq)) if (!opt$x %in% sample_vars) { stop(paste("Error: X-axis variable", opt$x, "does not exist in the sample data.")) } -if (!opt$fill %in% sample_vars) { - stop(paste("Error: Fill variable", opt$fill, "does not exist in the sample data.")) -} # Generate bar plot p <- plot_bar(physeq, x = opt$x, fill = opt$fill) diff --git a/tools/phyloseq/phyloseq_plot_bar.xml b/tools/phyloseq/phyloseq_plot_bar.xml index e91dc9dfcaf..9ef0713f6fe 100644 --- a/tools/phyloseq/phyloseq_plot_bar.xml +++ b/tools/phyloseq/phyloseq_plot_bar.xml @@ -36,14 +36,7 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R' - - - - - - 1 - - + @@ -67,7 +60,7 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R' - **Input**: A phyloseq object in RDS format. - **X-axis variable**: The variable to use for the x-axis (e.g., Sample, Phylum). - - **Fill variable**: The variable to use for the bar fill colors (e.g., Genus, Order). + - **Fill variable**: (Optional) The variable to use for the bar fill colors (e.g., Genus, Order). - **Facet by variable**: (Optional) A variable to facet the bar chart (e.g., SampleType). **Outputs**