annotate phyloseq_plot_bar.R @ 6:bda00cb136ee 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:58:44 +0000
parents 40ebae5bbe51
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
40ebae5bbe51 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
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
2
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
3 # Load libraries
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
4 suppressPackageStartupMessages(library("optparse"))
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
5 suppressPackageStartupMessages(library("phyloseq"))
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
6 suppressPackageStartupMessages(library("ggplot2"))
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
7
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
8 # Define options
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
9 option_list <- list(
40ebae5bbe51 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"),
40ebae5bbe51 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",
40ebae5bbe51 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"
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
13 ),
40ebae5bbe51 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"),
40ebae5bbe51 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",
40ebae5bbe51 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')"
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
17 ),
40ebae5bbe51 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"),
40ebae5bbe51 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,
40ebae5bbe51 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)"
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
21 ),
40ebae5bbe51 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"),
40ebae5bbe51 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,
40ebae5bbe51 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)"
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
25 ),
40ebae5bbe51 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"),
40ebae5bbe51 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",
40ebae5bbe51 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
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
29 ),
bda00cb136ee 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"),
bda00cb136ee 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,
bda00cb136ee 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)"
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
33 ),
bda00cb136ee 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"),
bda00cb136ee 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,
bda00cb136ee 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)"
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
37 ),
bda00cb136ee 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"),
bda00cb136ee 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,
bda00cb136ee 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)"
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
41 ),
bda00cb136ee 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"),
bda00cb136ee 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,
bda00cb136ee 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)"
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
45 ),
bda00cb136ee 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"),
bda00cb136ee 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,
bda00cb136ee 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"
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
49 ),
bda00cb136ee 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"),
bda00cb136ee 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,
bda00cb136ee 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"
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
53 ),
bda00cb136ee 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"),
bda00cb136ee 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",
bda00cb136ee 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
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
57 )
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
58 )
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
59
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
60 # Parse arguments
40ebae5bbe51 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)
40ebae5bbe51 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)
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
63 opt <- args$options
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
64
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
65 # Validate required options
40ebae5bbe51 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 == "") {
40ebae5bbe51 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.")
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
68 }
40ebae5bbe51 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 == "") {
40ebae5bbe51 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.")
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
71 }
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
72
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
73 # Load phyloseq object
40ebae5bbe51 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))
40ebae5bbe51 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)
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
76
6
bda00cb136ee 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
bda00cb136ee 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) {
bda00cb136ee 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%...")
bda00cb136ee 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))
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
81 }
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
82
bda00cb136ee 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) {
bda00cb136ee 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
bda00cb136ee 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))
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
86 }
3
40ebae5bbe51 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
40ebae5bbe51 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
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
89
bda00cb136ee 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
bda00cb136ee 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 != "") {
bda00cb136ee 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)
bda00cb136ee 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) {
bda00cb136ee 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.")
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
95 }
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
96
bda00cb136ee 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)
bda00cb136ee 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
bda00cb136ee 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)
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
100
bda00cb136ee 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
bda00cb136ee 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)
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
103
bda00cb136ee 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)
bda00cb136ee 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)
bda00cb136ee 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)
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
107
bda00cb136ee 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
bda00cb136ee 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]
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
110
bda00cb136ee 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:")
bda00cb136ee 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)
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
113
bda00cb136ee 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
bda00cb136ee 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]
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
116
bda00cb136ee 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) {
bda00cb136ee 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"
bda00cb136ee 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"
bda00cb136ee 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
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
121 } else {
bda00cb136ee 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
bda00cb136ee 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)
bda00cb136ee 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
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
125 }
3
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
126 }
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
127
40ebae5bbe51 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
bda00cb136ee 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 != "") {
bda00cb136ee 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)
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
131 } else {
bda00cb136ee 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
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
133 }
3
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
134
40ebae5bbe51 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
40ebae5bbe51 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 != "") {
40ebae5bbe51 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) {
40ebae5bbe51 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)))
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
139 } else {
40ebae5bbe51 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."))
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
141 }
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
142 }
40ebae5bbe51 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
143
6
bda00cb136ee 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
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
145 ggsave(
bda00cb136ee 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,
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
147 plot = p,
bda00cb136ee 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,
bda00cb136ee 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,
bda00cb136ee 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
bda00cb136ee planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
151 )