annotate phyloseq_plot_bar.R @ 6:f9543efabd85 draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
author iuc
date Fri, 10 Jan 2025 14:59:00 +0000
parents c35e0075c7a2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
c35e0075c7a2 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
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
2
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
3 # Load libraries
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
4 suppressPackageStartupMessages(library("optparse"))
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
5 suppressPackageStartupMessages(library("phyloseq"))
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
6 suppressPackageStartupMessages(library("ggplot2"))
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
7
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
8 # Define options
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
9 option_list <- list(
c35e0075c7a2 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"),
c35e0075c7a2 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",
c35e0075c7a2 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"
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
13 ),
c35e0075c7a2 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"),
c35e0075c7a2 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",
c35e0075c7a2 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')"
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
17 ),
c35e0075c7a2 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"),
c35e0075c7a2 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,
c35e0075c7a2 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)"
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
21 ),
c35e0075c7a2 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"),
c35e0075c7a2 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,
c35e0075c7a2 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)"
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
25 ),
c35e0075c7a2 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"),
c35e0075c7a2 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",
c35e0075c7a2 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
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
29 ),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
30 make_option(c("--topX"),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
31 action = "store", dest = "topX", default = NULL,
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
32 help = "Show only the top X taxa based on abundance (e.g., '10') (optional)"
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
33 ),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
34 make_option(c("--keepOthers"),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
35 action = "store_true", dest = "keepOthers", default = FALSE,
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
36 help = "Keep taxa not in topX and label them as 'Others' (optional)"
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
37 ),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
38 make_option(c("--keepNonAssigned"),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
39 action = "store_true", dest = "keepNonAssigned", default = FALSE,
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
40 help = "Keep taxa labeled as 'Not Assigned' (optional)"
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
41 ),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
42 make_option(c("--normalize"),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
43 action = "store_true", dest = "normalize", default = FALSE,
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
44 help = "Normalize abundances to sum to 100% (optional)"
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
45 ),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
46 make_option(c("--width"),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
47 action = "store", dest = "width", default = 10,
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
48 type = "numeric", help = "Width of the output plot in inches"
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
49 ),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
50 make_option(c("--height"),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
51 action = "store", dest = "height", default = 8,
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
52 type = "numeric", help = "Height of the output plot in inches"
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
53 ),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
54 make_option(c("--device"),
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
55 action = "store", dest = "device", default = "pdf",
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
56 help = "Output format (e.g., 'pdf', 'png', 'jpeg')"
3
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
57 )
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
58 )
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
59
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
60 # Parse arguments
c35e0075c7a2 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)
c35e0075c7a2 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)
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
63 opt <- args$options
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
64
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
65 # Validate required options
c35e0075c7a2 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 == "") {
c35e0075c7a2 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.")
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
68 }
c35e0075c7a2 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 == "") {
c35e0075c7a2 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.")
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
71 }
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
72
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
73 # Load phyloseq object
c35e0075c7a2 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))
c35e0075c7a2 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)
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
76
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
77 # Normalize to relative abundances if requested
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
78 if (opt$normalize) {
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
79 print("Normalizing abundances to sum to 100%...")
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
80 physeq <- transform_sample_counts(physeq, function(x) 100 * x / sum(x))
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
81 }
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
82
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
83 if (opt$keepNonAssigned) {
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
84 # Add synthetic "Not Assigned" for missing/NA taxa
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
85 tax_table(physeq) <- apply(tax_table(physeq), c(1, 2), function(x) ifelse(is.na(x) | x == "", "Not Assigned", x))
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
86 }
3
c35e0075c7a2 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
c35e0075c7a2 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
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
89
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
90 # If topX is provided, filter the phyloseq object to show only top X taxa
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
91 if (!is.null(opt$topX) && opt$topX != "") {
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
92 topX <- as.numeric(opt$topX)
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
93 if (is.na(topX) || topX <= 0) {
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
94 stop("Error: topX should be a positive number.")
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
95 }
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
96
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
97 # Aggregate the data at the selected rank (e.g., Phylum)
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
98 tax_rank <- opt$fill # Adjust as necessary
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
99 physeq_agg <- tax_glom(physeq, taxrank = tax_rank)
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
100
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
101 # Get the abundance of each taxon at the selected rank
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
102 taxa_abundance <- taxa_sums(physeq_agg)
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
103
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
104 # Summarize the abundance at each taxonomic rank (grouping by taxonomic name)
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
105 tax_table_agg <- tax_table(physeq_agg)
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
106 taxa_abundance_by_rank <- tapply(taxa_abundance, tax_table_agg[, tax_rank], sum)
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
107
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
108 # Identify the top X taxa by summed abundance
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
109 top_taxa <- names(sort(taxa_abundance_by_rank, decreasing = TRUE))[1:topX]
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
110
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
111 print("Only plotting taxa in TopX taxa group:")
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
112 print(top_taxa)
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
113
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
114 # Get the OTUs corresponding to the top taxa
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
115 otus_in_top_taxa <- rownames(tax_table_agg)[tax_table_agg[, tax_rank] %in% top_taxa]
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
116
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
117 if (opt$keepOthers) {
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
118 # Label taxa not in top_taxa as "Others"
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
119 tax_table(physeq_agg)[, tax_rank][!rownames(tax_table(physeq_agg)) %in% otus_in_top_taxa] <- "Others"
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
120 physeq <- physeq_agg
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
121 } else {
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
122 # Subset the phyloseq object to keep only the top X taxa
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
123 physeq_filtered <- prune_taxa(otus_in_top_taxa, physeq_agg)
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
124 physeq <- physeq_filtered
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
125 }
3
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
126 }
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
127
c35e0075c7a2 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
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
129 if (!is.null(opt$x) && opt$x != "") {
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
130 p <- plot_bar(physeq, x = opt$x, fill = opt$fill)
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
131 } else {
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
132 p <- plot_bar(physeq, fill = opt$fill) # If no x is provided, don't include x
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
133 }
3
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
134
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
135 # Only facet if the facet variable is provided and exists in the sample data
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
136 if (!is.null(opt$facet) && opt$facet != "") {
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
137 if (opt$facet %in% sample_vars) {
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
138 p <- p + facet_wrap(as.formula(paste("~", opt$facet)))
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
139 } else {
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
140 warning(paste("Facet variable", opt$facet, "does not exist in the sample data. Faceting will be skipped."))
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
141 }
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
142 }
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
143
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
144 # Save to output file
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
145 ggsave(
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
146 filename = opt$output,
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
147 plot = p,
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
148 width = opt$width,
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
149 height = opt$height,
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
150 device = opt$device
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
151 )