Skip to content

Commit

Permalink
Merge pull request galaxyproject#6575 from MaraBesemer/main
Browse files Browse the repository at this point in the history
add phyloseq bar chart
  • Loading branch information
bgruening authored Dec 3, 2024
2 parents b6798b5 + 79169e6 commit 10dfb13
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tools/phyloseq/phyloseq_plot_bar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env Rscript

# Load libraries
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", default = NULL,
help = "Variable for fill color (e.g., 'Genus', 'Order') (optional)"
),
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, 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$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
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."))
}

# 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) && 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 using PDF device
print(paste("Saving plot to:", opt$output))
pdf(file = opt$output, width = 10, height = 8)
print(p)
dev.off()
76 changes: 76 additions & 0 deletions tools/phyloseq/phyloseq_plot_bar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<tool id="phyloseq_plot_bar" name="Phyloseq: Bar Chart" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@">
<description>Generate bar charts from a phyloseq object</description>
<macros>
<import>macros.xml</import>
</macros>
<expand macro="bio_tools"/>
<expand macro="requirements"/>
<command detect_errors="exit_code"><![CDATA[
Rscript '${__tool_directory__}/phyloseq_plot_bar.R'
--input '$input'
--x '$x'
--fill '$fill'
--facet '${facet}'
--output '$output'
]]></command>
<inputs>
<expand macro="phyloseq_input"/>
<param name="x" type="text" label="X-axis variable" help="Variable for the x-axis (e.g., Sample, Phylum)" />
<param name="fill" type="text" label="Fill variable" help="Variable to color the bars (e.g., Genus, Order)" />
<param name="facet" type="text" optional="true" label="Facet by variable" help="Optional: Variable to facet the chart by (e.g., SampleType)" />
</inputs>
<outputs>
<data name="output" format="pdf" label="Bar Chart (PDF)" />
</outputs>
<tests>
<!-- Test 1: Default parameters -->
<test>
<param name="input" value="output.phyloseq" ftype="phyloseq"/>
<param name="x" value="Property"/>
<param name="fill" value="Number"/>
<param name="facet" value="Property"/>
<output name="output" ftype="pdf">
<assert_contents>
<has_text text="%PDF"/>
<has_text text="%%EOF"/>
</assert_contents>
</output>
</test>
<!-- Test 2: Valid parameters without facet -->
<test>
<param name="input" value="output.phyloseq" ftype="phyloseq"/>
<param name="x" value="Property"/>
<param name="fill" value="Number"/>
<param name="facet" value=""/>
<output name="output" ftype="pdf">
<assert_contents>
<has_text text="%PDF"/>
<has_text text="%%EOF"/>
</assert_contents>
</output>
</test>
</tests>

<help>
**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**: (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**

- A PDF file containing the bar chart.

**Usage Notes**

Ensure that the input file is a valid phyloseq object in RDS format.
</help>
<expand macro="citations"/>
</tool>

0 comments on commit 10dfb13

Please sign in to comment.