Skip to content

Commit

Permalink
add: variables for width, height and device and fix R linting
Browse files Browse the repository at this point in the history
  • Loading branch information
MaraBesemer committed Jan 10, 2025
1 parent 3749464 commit 8282568
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 61 deletions.
125 changes: 69 additions & 56 deletions tools/phyloseq/phyloseq_plot_bar.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,54 @@ 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)"
),
make_option(c("--topX"),
action = "store", dest = "topX", default = NULL,
help = "Show only the top X taxa based on abundance (e.g., '10') (optional)"
),
make_option(c("--keepOthers"),
action = "store_true", dest = "keepOthers", default = FALSE,
help = "Keep taxa not in topX and label them as 'Others' (optional)"
),
make_option(c("--keepNonAssigned"),
action = "store_true", dest = "keepNonAssigned", default = FALSE,
help = "Keep taxa labeled as 'Not Assigned' (optional)"
),
make_option(c("--normalize"),
action = "store_true", dest = "normalize", default = FALSE,
help = "Normalize abundances to sum to 100% (optional)"
)
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)"
),
make_option(c("--topX"),
action = "store", dest = "topX", default = NULL,
help = "Show only the top X taxa based on abundance (e.g., '10') (optional)"
),
make_option(c("--keepOthers"),
action = "store_true", dest = "keepOthers", default = FALSE,
help = "Keep taxa not in topX and label them as 'Others' (optional)"
),
make_option(c("--keepNonAssigned"),
action = "store_true", dest = "keepNonAssigned", default = FALSE,
help = "Keep taxa labeled as 'Not Assigned' (optional)"
),
make_option(c("--normalize"),
action = "store_true", dest = "normalize", default = FALSE,
help = "Normalize abundances to sum to 100% (optional)"
),
make_option(c("--width"),
action = "store", dest = "width", default = 10,
type = "numeric", help = "Width of the output plot in inches"
),
make_option(c("--height"),
action = "store", dest = "height", default = 8,
type = "numeric", help = "Height of the output plot in inches"
),
make_option(c("--device"),
action = "store", dest = "device", default = "pdf",
help = "Output format (e.g., 'pdf', 'png', 'jpeg')"
)
)

# Parse arguments
Expand All @@ -52,10 +64,10 @@ opt <- args$options

# Validate required options
if (is.null(opt$input) || opt$input == "") {
stop("Error: Input file is required.")
stop("Error: Input file is required.")
}
if (is.null(opt$output) || opt$output == "") {
stop("Error: Output file is required.")
stop("Error: Output file is required.")
}

# Load phyloseq object
Expand All @@ -64,13 +76,13 @@ physeq <- readRDS(opt$input)

# Normalize to relative abundances if requested
if (opt$normalize) {
print("Normalizing abundances to sum to 100%...")
physeq <- transform_sample_counts(physeq, function(x) 100 * x / sum(x))
print("Normalizing abundances to sum to 100%...")
physeq <- transform_sample_counts(physeq, function(x) 100 * x / sum(x))
}

if (opt$keepNonAssigned) {
# Add synthetic "Not Assigned" for missing/NA taxa
tax_table(physeq) <- apply(tax_table(physeq), c(1, 2), function(x) ifelse(is.na(x) | x == "", "Not Assigned", x))
# Add synthetic "Not Assigned" for missing/NA taxa
tax_table(physeq) <- apply(tax_table(physeq), c(1, 2), function(x) ifelse(is.na(x) | x == "", "Not Assigned", x))
}
# Check if the 'x' and 'fill' variables are valid
sample_vars <- colnames(sample_data(physeq))
Expand All @@ -83,12 +95,12 @@ if (!is.null(opt$topX) && opt$topX != "") {
}

# Aggregate the data at the selected rank (e.g., Phylum)
tax_rank <- opt$fill # Adjust as necessary
tax_rank <- opt$fill # Adjust as necessary
physeq_agg <- tax_glom(physeq, taxrank = tax_rank)

# Get the abundance of each taxon at the selected rank
taxa_abundance <- taxa_sums(physeq_agg)

# Summarize the abundance at each taxonomic rank (grouping by taxonomic name)
tax_table_agg <- tax_table(physeq_agg)
taxa_abundance_by_rank <- tapply(taxa_abundance, tax_table_agg[, tax_rank], sum)
Expand All @@ -115,24 +127,25 @@ if (!is.null(opt$topX) && opt$topX != "") {

# Generate bar plot
if (!is.null(opt$x) && opt$x != "") {
p <- plot_bar(physeq, x = opt$x, fill = opt$fill)
p <- plot_bar(physeq, x = opt$x, fill = opt$fill)
} else {
p <- plot_bar(physeq, fill = opt$fill) # If no x is provided, don't include x
p <- plot_bar(physeq, fill = opt$fill) # If no x is provided, don't include x
}

# 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."))
}
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
# Save to output file
ggsave(
opt$output,
width = 10,
height = 8,
device="png"
filename = opt$output,
plot = p,
width = opt$width,
height = opt$height,
device = opt$device
)
23 changes: 18 additions & 5 deletions tools/phyloseq/phyloseq_plot_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,27 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R'
--keepOthers '${keepOthers}'
--keepNonAssigned '${keepNonAssigned}'
--normalize '${normalize}'
--width '${width}'
--height '${height}'
--device '${device}'
]]></command>
<inputs>
<expand macro="phyloseq_input"/>
<param name="x" type="text" optional="true" label="X-axis variable" help="Variable for the x-axis (e.g., Sample, Phylum). If not specified the Samples are taken." />
<param name="x" type="text" optional="true" label="X-axis variable" help="Variable for the x-axis (e.g., Sample, Phylum). If not specified, the Samples are taken." />
<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="Variable to facet the chart by (e.g., SampleType)." />
<param name="topX" value="10" type="integer" optional="true" label="Top X" help="Only show the ranks with the top X abundance." />
<param name="keepOthers" type="boolean" label="Keep 'Others'" help="Keep OTUs which are not in top X as 'Others'." />
<param name="keepNonAssigned" type="boolean" label="Keep Non Assigned" help="Keep OTUs that are not assigned at this rank and lable as 'Not Assigned'." />
<param name="keepOthers" type="boolean" label="Keep 'Others'" help="Keep OTUs which are not in top X as 'Others'." />
<param name="keepNonAssigned" type="boolean" label="Keep Non Assigned" help="Keep OTUs that are not assigned at this rank and label as 'Not Assigned'." />
<param name="normalize" type="boolean" label="Normalize" help="Normalize abundances to sum to 100%. Normalization is performed before Top X selection." />
<param name="width" type="float" value="10" optional="true" label="Plot Width" help="Width of the output plot in inches." />
<param name="height" type="float" value="8" optional="true" label="Plot Height" help="Height of the output plot in inches." />
<param name="device" type="select" value="pdf" label="Output Device" help="Device to use for the output file. Options include pdf, png, and others.">
<option value="pdf">PDF</option>
<option value="png">PNG</option>
<option value="jpeg">JPEG</option>
<option value="tiff">TIFF</option>
</param>
</inputs>
<outputs>
<data name="output" format="pdf" label="Bar Chart (PDF)" />
Expand All @@ -36,6 +47,7 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R'
<param name="input" value="output.phyloseq" ftype="phyloseq"/>
<param name="x" value="Property"/>
<param name="fill" value="Phylum"/>
<param name="device" value="pdf"/>
<output name="output" ftype="pdf">
<assert_contents>
<has_text text="%PDF"/>
Expand All @@ -61,7 +73,6 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R'
</test>
</tests>


<help>
**Description**

Expand All @@ -73,10 +84,12 @@ Rscript '${__tool_directory__}/phyloseq_plot_bar.R'
- **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).
- **Width and Height**: Dimensions of the plot in inches (default: 10x8).
- **Device**: Output format (e.g., pdf, png, jpeg, tiff).

**Outputs**

- A PDF file containing the bar chart.
- A file containing the bar chart in the specified format.

**Usage Notes**

Expand Down

0 comments on commit 8282568

Please sign in to comment.