annotate phyloseq_plot_bar.R @ 8:bbd9d1022144 draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
author iuc
date Tue, 04 Feb 2025 14:39:35 +0000
parents eab0b21652c4
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')"
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
57 ),
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
58 make_option(c("--nolines"),
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
59 type = "logical", default = FALSE,
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
60 help = "Remove borders (lines) around bars (TRUE/FALSE)"
3
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
61 )
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
62 )
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
63
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
64 # Parse arguments
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
65 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
66 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
67 opt <- args$options
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 # Validate required options
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
70 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
71 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
72 }
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
73 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
74 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
75 }
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
76
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
77 # Load phyloseq object
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
78 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
79 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
80
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
81 # 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
82 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
83 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
84 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
85 }
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
86
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
87 # Debug: Check available taxonomic ranks
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
88 print("Available taxonomic ranks:")
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
89 print(colnames(tax_table(physeq)))
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
90
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
91 # Handle missing or unassigned taxa for all ranks
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
92 if (opt$keepNonAssigned) {
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
93 # Replace NA or empty values with 'Not Assigned' for all ranks
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
94 tax_ranks <- colnames(tax_table(physeq))
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
95
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
96 for (rank in tax_ranks) {
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
97 if (rank %in% colnames(tax_table(physeq))) {
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
98 # replace NA or empty values with 'Not Assigned'
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
99 tax_table(physeq)[, rank][is.na(tax_table(physeq)[, rank])] <- "Not Assigned"
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
100 }
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
101 }
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
102 }
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
103
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
104 # Filter to top X taxa if requested
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
105 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
106 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
107 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
108 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
109 }
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
110
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
111 tax_rank <- opt$fill
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
112 if (!tax_rank %in% colnames(tax_table(physeq))) {
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
113 stop(paste("Error: Tax rank", tax_rank, "not found in tax_table."))
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
114 }
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
115
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
116 physeq_agg <- tax_glom(physeq, taxrank = tax_rank)
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
117 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
118 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
119 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
120 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
121
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
122 print("Top taxa:")
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
123 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
124
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
125 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
126
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
127 if (opt$keepOthers) {
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
128 tax_table(physeq_agg)[, tax_rank][!rownames(tax_table_agg) %in% otus_in_top_taxa] <- "Others"
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
129 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
130 } else {
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
131 physeq <- prune_taxa(otus_in_top_taxa, physeq_agg)
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
132 }
3
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
133 }
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 # 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
136 if (!is.null(opt$x) && opt$x != "") {
7
eab0b21652c4 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 53ece29ec3f7e3c2c51d0ff9849e6193863cca71
iuc
parents: 6
diff changeset
137 p <- plot_bar(physeq, x = opt$x, fill = opt$fill) +
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
138 geom_bar(aes(fill = !!sym(opt$fill)),
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
139 stat = "identity", position = "stack",
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
140 color = ifelse(opt$nolines, NA, "black")
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
141 )
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
142 } else {
7
eab0b21652c4 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 53ece29ec3f7e3c2c51d0ff9849e6193863cca71
iuc
parents: 6
diff changeset
143 p <- plot_bar(physeq, fill = opt$fill) +
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
144 geom_bar(aes(fill = !!sym(opt$fill)),
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
145 stat = "identity", position = "stack",
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
146 color = ifelse(opt$nolines, NA, "black")
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
147 )
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
148 }
3
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
149
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
150 # Optional: Add faceting if specified
3
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
151 if (!is.null(opt$facet) && opt$facet != "") {
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
152 sample_vars <- colnames(sample_data(physeq))
3
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
153 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
154 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
155 } else {
8
bbd9d1022144 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d6888da7aba38b97f6cb827355f2de436565684a
iuc
parents: 7
diff changeset
156 warning(paste("Facet variable", opt$facet, "not found in sample data. Skipping faceting."))
3
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
157 }
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
158 }
c35e0075c7a2 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 10dfb1308ff858c6623c7dd9215a3bdf518427f9
iuc
parents:
diff changeset
159
6
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
160 # 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
161 ggsave(
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
162 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
163 plot = p,
f9543efabd85 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit 26f87cc62468c9c329b33246af4f14e2659856f9
iuc
parents: 3
diff changeset
164 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
165 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
166 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
167 )