annotate add_rank_names_to_phyloseq.R @ 5:e8f58a93ff19 draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
author iuc
date Tue, 07 Jan 2025 17:58:14 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
1 #!/usr/bin/env Rscript
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
2
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
3 suppressPackageStartupMessages(library("optparse"))
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
4 suppressPackageStartupMessages(library("phyloseq"))
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
5 suppressPackageStartupMessages(library("tidyverse"))
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
6
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
7 # Option parsing
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
8 option_list <- list(
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
9 make_option(c("--input"),
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
10 action = "store", dest = "input",
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
11 help = "Input file containing a phyloseq object"
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
12 ),
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
13 make_option(c("--output"),
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
14 action = "store", dest = "output",
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
15 help = "Output file for the updated phyloseq object"
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
16 ),
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
17 make_option(c("--ranks"),
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
18 action = "store", dest = "ranks",
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
19 help = "Comma-separated list of taxonomy ranks (default: Kingdom,Phylum,Class,Order,Family,Genus,Species)"
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
20 )
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
21 )
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
22
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
23 parser <- OptionParser(usage = "%prog [options] file", option_list = option_list)
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
24 args <- parse_args(parser, positional_arguments = TRUE)
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
25 opt <- args$options
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
26
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
27 cat("Input file: ", opt$input, "\n")
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
28 cat("Output file: ", opt$output, "\n")
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
29 cat("Ranks provided: ", opt$ranks, "\n")
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
30
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
31 if (is.null(opt$ranks)) {
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
32 opt$ranks <- "Kingdom,Phylum,Class,Order,Family,Genus,Species"
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
33 }
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
34
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
35 # Parse rank names
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
36 rank_names <- unlist(strsplit(opt$ranks, ","))
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
37
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
38 # Load phyloseq object
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
39 physeq <- readRDS(opt$input)
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
40
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
41 # Check if physeq object is loaded successfully
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
42 if (is.null(physeq)) {
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
43 stop("Error: Failed to load the Phyloseq object. Check the input file.")
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
44 }
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
45
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
46 cat("Phyloseq object successfully loaded.\n")
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
47 cat("Class of loaded object: ", class(physeq), "\n")
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
48
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
49 # Check the current tax_table
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
50 cat("Current tax_table:\n")
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
51 print(tax_table(physeq))
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
52
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
53
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
54 # Strict check for taxonomy table and provided ranks
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
55 if (ncol(tax_table(physeq)) != length(rank_names)) {
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
56 stop(
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
57 "Error: Number of columns in tax_table does not match the number of provided ranks. ",
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
58 "Please ensure the taxonomy table matches the ranks exactly."
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
59 )
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
60 }
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
61
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
62 # Set column names to the provided ranks
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
63 colnames(tax_table(physeq)) <- rank_names
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
64
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
65 # Confirm the changes
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
66 cat("Updated tax_table:\n")
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
67 print(tax_table(physeq))
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
68
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
69 # Save the updated phyloseq object
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
70 saveRDS(physeq, file = opt$output, compress = TRUE)
e8f58a93ff19 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/phyloseq commit d905841a8c8eff6a2006492ef52b31f969e8206a
iuc
parents:
diff changeset
71 cat("Updated Phyloseq object saved to: ", opt$output, "\n")