Skip to content

Commit d905841

Browse files
authored
Merge pull request #6625 from MaraBesemer/main
add: implement tool to add rank names to phyloseq object
2 parents 56658d5 + 6d85318 commit d905841

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env Rscript
2+
3+
suppressPackageStartupMessages(library("optparse"))
4+
suppressPackageStartupMessages(library("phyloseq"))
5+
suppressPackageStartupMessages(library("tidyverse"))
6+
7+
# Option parsing
8+
option_list <- list(
9+
make_option(c("--input"),
10+
action = "store", dest = "input",
11+
help = "Input file containing a phyloseq object"
12+
),
13+
make_option(c("--output"),
14+
action = "store", dest = "output",
15+
help = "Output file for the updated phyloseq object"
16+
),
17+
make_option(c("--ranks"),
18+
action = "store", dest = "ranks",
19+
help = "Comma-separated list of taxonomy ranks (default: Kingdom,Phylum,Class,Order,Family,Genus,Species)"
20+
)
21+
)
22+
23+
parser <- OptionParser(usage = "%prog [options] file", option_list = option_list)
24+
args <- parse_args(parser, positional_arguments = TRUE)
25+
opt <- args$options
26+
27+
cat("Input file: ", opt$input, "\n")
28+
cat("Output file: ", opt$output, "\n")
29+
cat("Ranks provided: ", opt$ranks, "\n")
30+
31+
if (is.null(opt$ranks)) {
32+
opt$ranks <- "Kingdom,Phylum,Class,Order,Family,Genus,Species"
33+
}
34+
35+
# Parse rank names
36+
rank_names <- unlist(strsplit(opt$ranks, ","))
37+
38+
# Load phyloseq object
39+
physeq <- readRDS(opt$input)
40+
41+
# Check if physeq object is loaded successfully
42+
if (is.null(physeq)) {
43+
stop("Error: Failed to load the Phyloseq object. Check the input file.")
44+
}
45+
46+
cat("Phyloseq object successfully loaded.\n")
47+
cat("Class of loaded object: ", class(physeq), "\n")
48+
49+
# Check the current tax_table
50+
cat("Current tax_table:\n")
51+
print(tax_table(physeq))
52+
53+
54+
# Strict check for taxonomy table and provided ranks
55+
if (ncol(tax_table(physeq)) != length(rank_names)) {
56+
stop(
57+
"Error: Number of columns in tax_table does not match the number of provided ranks. ",
58+
"Please ensure the taxonomy table matches the ranks exactly."
59+
)
60+
}
61+
62+
# Set column names to the provided ranks
63+
colnames(tax_table(physeq)) <- rank_names
64+
65+
# Confirm the changes
66+
cat("Updated tax_table:\n")
67+
print(tax_table(physeq))
68+
69+
# Save the updated phyloseq object
70+
saveRDS(physeq, file = opt$output, compress = TRUE)
71+
cat("Updated Phyloseq object saved to: ", opt$output, "\n")
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<tool id="phyloseq_add_rank_names" name="Add Rank Names to Phyloseq Object" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@">
2+
<description>Add taxonomy rank names to a phyloseq object</description>
3+
<macros>
4+
<import>macros.xml</import>
5+
</macros>
6+
<expand macro="bio_tools"/>
7+
<expand macro="requirements"/>
8+
<command detect_errors="exit_code"><![CDATA[
9+
Rscript '${__tool_directory__}/add_rank_names_to_phyloseq.R' --input '$input' --output '$output' --ranks '$ranks'
10+
]]></command>
11+
12+
<inputs>
13+
<expand macro="phyloseq_input"/>
14+
<param name="ranks" type="text" label="Comma-separated list of taxonomy ranks" value="Kingdom,Phylum,Class,Order,Family,Genus,Species"/>
15+
</inputs>
16+
17+
<outputs>
18+
<data name="output" format="phyloseq"/>
19+
</outputs>
20+
21+
<tests>
22+
<test>
23+
<param name="input" value="output.phyloseq" ftype="phyloseq"/>
24+
<param name="ranks" value="Kingdom,Phylum,Class,Order,Family,Genus"/>
25+
<output name="output" ftype="phyloseq">
26+
<assert_contents>
27+
<has_size value="87125" delta="1000"/>
28+
</assert_contents>
29+
</output>
30+
</test>
31+
</tests>
32+
33+
<help>
34+
This tool adds taxonomy rank names to a phyloseq object in the `tax_table` slot.
35+
</help>
36+
<expand macro="citations"/>
37+
</tool>

0 commit comments

Comments
 (0)