Mercurial > repos > iuc > phyloseq_plot_richness
annotate phyloseq_plot_bar.R @ 7:4faa9c663b38 draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 53ece29ec3f7e3c2c51d0ff9849e6193863cca71
| author | iuc | 
|---|---|
| date | Sat, 25 Jan 2025 17:12:03 +0000 | 
| parents | a20bc31f2821 | 
| children | d6cbeb48294d | 
| 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)" | 
| 6 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 29 ), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 30 make_option(c("--topX"), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 31 action = "store", dest = "topX", default = NULL, | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 32 help = "Show only the top X taxa based on abundance (e.g., '10') (optional)" | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 33 ), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 34 make_option(c("--keepOthers"), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 35 action = "store_true", dest = "keepOthers", default = FALSE, | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 36 help = "Keep taxa not in topX and label them as 'Others' (optional)" | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 37 ), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 38 make_option(c("--keepNonAssigned"), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 39 action = "store_true", dest = "keepNonAssigned", default = FALSE, | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 40 help = "Keep taxa labeled as 'Not Assigned' (optional)" | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 41 ), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 42 make_option(c("--normalize"), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 43 action = "store_true", dest = "normalize", default = FALSE, | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 44 help = "Normalize abundances to sum to 100% (optional)" | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 45 ), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 46 make_option(c("--width"), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 47 action = "store", dest = "width", default = 10, | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 48 type = "numeric", help = "Width of the output plot in inches" | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 49 ), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 50 make_option(c("--height"), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 51 action = "store", dest = "height", default = 8, | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 52 type = "numeric", help = "Height of the output plot in inches" | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 53 ), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 54 make_option(c("--device"), | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 55 action = "store", dest = "device", default = "pdf", | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 56 help = "Output format (e.g., 'pdf', 'png', 'jpeg')" | 
| 3 
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 ) | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 59 | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 60 # Parse arguments | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 61 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 | 62 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 | 63 opt <- args$options | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 64 | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 65 # Validate required options | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 66 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 | 67 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 | 68 } | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 69 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 | 70 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 | 71 } | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 72 | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 73 # Load phyloseq object | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 74 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 | 75 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 | 76 | 
| 6 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 77 # Normalize to relative abundances if requested | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 78 if (opt$normalize) { | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 79 print("Normalizing abundances to sum to 100%...") | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 80 physeq <- transform_sample_counts(physeq, function(x) 100 * x / sum(x)) | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 81 } | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 82 | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 83 if (opt$keepNonAssigned) { | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 84 # Add synthetic "Not Assigned" for missing/NA taxa | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 85 tax_table(physeq) <- apply(tax_table(physeq), c(1, 2), function(x) ifelse(is.na(x) | x == "", "Not Assigned", x)) | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 86 } | 
| 3 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 87 # 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 | 88 sample_vars <- colnames(sample_data(physeq)) | 
| 6 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 89 | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 90 # If topX is provided, filter the phyloseq object to show only top X taxa | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 91 if (!is.null(opt$topX) && opt$topX != "") { | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 92 topX <- as.numeric(opt$topX) | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 93 if (is.na(topX) || topX <= 0) { | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 94 stop("Error: topX should be a positive number.") | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 95 } | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 96 | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 97 # Aggregate the data at the selected rank (e.g., Phylum) | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 98 tax_rank <- opt$fill # Adjust as necessary | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 99 physeq_agg <- tax_glom(physeq, taxrank = tax_rank) | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 100 | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 101 # Get the abundance of each taxon at the selected rank | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 102 taxa_abundance <- taxa_sums(physeq_agg) | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 103 | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 104 # Summarize the abundance at each taxonomic rank (grouping by taxonomic name) | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 105 tax_table_agg <- tax_table(physeq_agg) | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 106 taxa_abundance_by_rank <- tapply(taxa_abundance, tax_table_agg[, tax_rank], sum) | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 107 | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 108 # Identify the top X taxa by summed abundance | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 109 top_taxa <- names(sort(taxa_abundance_by_rank, decreasing = TRUE))[1:topX] | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 110 | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 111 print("Only plotting taxa in TopX taxa group:") | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 112 print(top_taxa) | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 113 | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 114 # Get the OTUs corresponding to the top taxa | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 115 otus_in_top_taxa <- rownames(tax_table_agg)[tax_table_agg[, tax_rank] %in% top_taxa] | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 116 | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 117 if (opt$keepOthers) { | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 118 # Label taxa not in top_taxa as "Others" | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 119 tax_table(physeq_agg)[, tax_rank][!rownames(tax_table(physeq_agg)) %in% otus_in_top_taxa] <- "Others" | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 120 physeq <- physeq_agg | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 121 } else { | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 122 # Subset the phyloseq object to keep only the top X taxa | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 123 physeq_filtered <- prune_taxa(otus_in_top_taxa, physeq_agg) | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 124 physeq <- physeq_filtered | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 125 } | 
| 3 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 126 } | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 127 | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 128 # Generate bar plot | 
| 6 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 129 if (!is.null(opt$x) && opt$x != "") { | 
| 7 
4faa9c663b38
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 53ece29ec3f7e3c2c51d0ff9849e6193863cca71
 iuc parents: 
6diff
changeset | 130 p <- plot_bar(physeq, x = opt$x, fill = opt$fill) + | 
| 
4faa9c663b38
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 53ece29ec3f7e3c2c51d0ff9849e6193863cca71
 iuc parents: 
6diff
changeset | 131 geom_bar(aes(color = NULL, fill = !!sym(opt$fill)), stat = "identity", position = "stack") | 
| 6 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 132 } else { | 
| 7 
4faa9c663b38
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 53ece29ec3f7e3c2c51d0ff9849e6193863cca71
 iuc parents: 
6diff
changeset | 133 p <- plot_bar(physeq, fill = opt$fill) + | 
| 
4faa9c663b38
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 53ece29ec3f7e3c2c51d0ff9849e6193863cca71
 iuc parents: 
6diff
changeset | 134 geom_bar(aes(color = NULL, fill = !!sym(opt$fill)), stat = "identity", position = "stack") | 
| 6 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 135 } | 
| 3 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 136 | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 137 # 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 | 138 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 | 139 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 | 140 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 | 141 } else { | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 142 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 | 143 } | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 144 } | 
| 
1a6c2cc92c6e
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
 iuc parents: diff
changeset | 145 | 
| 7 
4faa9c663b38
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 53ece29ec3f7e3c2c51d0ff9849e6193863cca71
 iuc parents: 
6diff
changeset | 146 | 
| 6 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 147 # Save to output file | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 148 ggsave( | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 149 filename = opt$output, | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 150 plot = p, | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 151 width = opt$width, | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 152 height = opt$height, | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 153 device = opt$device | 
| 
a20bc31f2821
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
 iuc parents: 
3diff
changeset | 154 ) | 
