Mercurial > repos > iuc > phyloseq_from_biom
comparison phyloseq_plot_bar.R @ 1:1feea247d08a draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
author | iuc |
---|---|
date | Tue, 03 Dec 2024 17:45:38 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:c0101c72b8af | 1:1feea247d08a |
---|---|
1 #!/usr/bin/env Rscript | |
2 | |
3 # Load libraries | |
4 suppressPackageStartupMessages(library("optparse")) | |
5 suppressPackageStartupMessages(library("phyloseq")) | |
6 suppressPackageStartupMessages(library("ggplot2")) | |
7 | |
8 # Define options | |
9 option_list <- list( | |
10 make_option(c("--input"), | |
11 action = "store", dest = "input", | |
12 help = "Input file containing a phyloseq object" | |
13 ), | |
14 make_option(c("--x"), | |
15 action = "store", dest = "x", | |
16 help = "Variable for x-axis (e.g., 'Sample', 'Phylum')" | |
17 ), | |
18 make_option(c("--fill"), | |
19 action = "store", dest = "fill", default = NULL, | |
20 help = "Variable for fill color (e.g., 'Genus', 'Order') (optional)" | |
21 ), | |
22 make_option(c("--facet"), | |
23 action = "store", dest = "facet", default = NULL, | |
24 help = "Facet by variable (optional)" | |
25 ), | |
26 make_option(c("--output"), | |
27 action = "store", dest = "output", | |
28 help = "Output file (PDF)" | |
29 ) | |
30 ) | |
31 | |
32 # Parse arguments | |
33 parser <- OptionParser(usage = "%prog [options] file", option_list = option_list) | |
34 args <- parse_args(parser, positional_arguments = TRUE) | |
35 opt <- args$options | |
36 | |
37 # Validate required options | |
38 if (is.null(opt$input) || opt$input == "") { | |
39 stop("Error: Input file is required.") | |
40 } | |
41 if (is.null(opt$x) || opt$x == "") { | |
42 stop("Error: X-axis variable is required.") | |
43 } | |
44 if (is.null(opt$output) || opt$output == "") { | |
45 stop("Error: Output file is required.") | |
46 } | |
47 | |
48 # Load phyloseq object | |
49 print(paste("Trying to read:", opt$input)) | |
50 physeq <- readRDS(opt$input) | |
51 | |
52 # Check if the 'x' and 'fill' variables are valid | |
53 sample_vars <- colnames(sample_data(physeq)) | |
54 if (!opt$x %in% sample_vars) { | |
55 stop(paste("Error: X-axis variable", opt$x, "does not exist in the sample data.")) | |
56 } | |
57 | |
58 # Generate bar plot | |
59 p <- plot_bar(physeq, x = opt$x, fill = opt$fill) | |
60 | |
61 # Only facet if the facet variable is provided and exists in the sample data | |
62 if (!is.null(opt$facet) && opt$facet != "") { | |
63 if (opt$facet %in% sample_vars) { | |
64 p <- p + facet_wrap(as.formula(paste("~", opt$facet))) | |
65 } else { | |
66 warning(paste("Facet variable", opt$facet, "does not exist in the sample data. Faceting will be skipped.")) | |
67 } | |
68 } | |
69 | |
70 # Save to output file using PDF device | |
71 print(paste("Saving plot to:", opt$output)) | |
72 pdf(file = opt$output, width = 10, height = 8) | |
73 print(p) | |
74 dev.off() |