Skip to content

Commit

Permalink
add ggplot2 to macros and R linting fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
MaraBesemer committed Nov 27, 2024
1 parent 435232e commit 8ce744d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
1 change: 1 addition & 0 deletions tools/phyloseq/macros.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<requirement type="package" version="@TOOL_VERSION@">bioconductor-phyloseq</requirement>
<requirement type="package" version="1.7.3">r-optparse</requirement>
<requirement type="package" version="2.0.0">r-tidyverse</requirement>
<requirement type="package" version="3.3.0">r-ggplot2</requirement>
</requirements>
</xml>
<xml name="phyloseq_input">
Expand Down
54 changes: 27 additions & 27 deletions tools/phyloseq/phyloseq_plot_bar.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,23 +40,23 @@ 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
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
Expand Down

0 comments on commit 8ce744d

Please sign in to comment.