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