Skip to content

Commit

Permalink
Merge pull request #6625 from MaraBesemer/main
Browse files Browse the repository at this point in the history
add: implement tool to add rank names to phyloseq object
  • Loading branch information
bgruening authored Jan 7, 2025
2 parents 56658d5 + 6d85318 commit d905841
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
71 changes: 71 additions & 0 deletions tools/phyloseq/add_rank_names_to_phyloseq.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env Rscript

suppressPackageStartupMessages(library("optparse"))
suppressPackageStartupMessages(library("phyloseq"))
suppressPackageStartupMessages(library("tidyverse"))

# Option parsing
option_list <- list(
make_option(c("--input"),
action = "store", dest = "input",
help = "Input file containing a phyloseq object"
),
make_option(c("--output"),
action = "store", dest = "output",
help = "Output file for the updated phyloseq object"
),
make_option(c("--ranks"),
action = "store", dest = "ranks",
help = "Comma-separated list of taxonomy ranks (default: Kingdom,Phylum,Class,Order,Family,Genus,Species)"
)
)

parser <- OptionParser(usage = "%prog [options] file", option_list = option_list)
args <- parse_args(parser, positional_arguments = TRUE)
opt <- args$options

cat("Input file: ", opt$input, "\n")
cat("Output file: ", opt$output, "\n")
cat("Ranks provided: ", opt$ranks, "\n")

if (is.null(opt$ranks)) {
opt$ranks <- "Kingdom,Phylum,Class,Order,Family,Genus,Species"
}

# Parse rank names
rank_names <- unlist(strsplit(opt$ranks, ","))

# Load phyloseq object
physeq <- readRDS(opt$input)

# Check if physeq object is loaded successfully
if (is.null(physeq)) {
stop("Error: Failed to load the Phyloseq object. Check the input file.")
}

cat("Phyloseq object successfully loaded.\n")
cat("Class of loaded object: ", class(physeq), "\n")

# Check the current tax_table
cat("Current tax_table:\n")
print(tax_table(physeq))


# Strict check for taxonomy table and provided ranks
if (ncol(tax_table(physeq)) != length(rank_names)) {
stop(
"Error: Number of columns in tax_table does not match the number of provided ranks. ",
"Please ensure the taxonomy table matches the ranks exactly."
)
}

# Set column names to the provided ranks
colnames(tax_table(physeq)) <- rank_names

# Confirm the changes
cat("Updated tax_table:\n")
print(tax_table(physeq))

# Save the updated phyloseq object
saveRDS(physeq, file = opt$output, compress = TRUE)
cat("Updated Phyloseq object saved to: ", opt$output, "\n")
37 changes: 37 additions & 0 deletions tools/phyloseq/add_rank_names_to_phyloseq.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<tool id="phyloseq_add_rank_names" name="Add Rank Names to Phyloseq Object" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@">
<description>Add taxonomy rank names to a phyloseq object</description>
<macros>
<import>macros.xml</import>
</macros>
<expand macro="bio_tools"/>
<expand macro="requirements"/>
<command detect_errors="exit_code"><![CDATA[
Rscript '${__tool_directory__}/add_rank_names_to_phyloseq.R' --input '$input' --output '$output' --ranks '$ranks'
]]></command>

<inputs>
<expand macro="phyloseq_input"/>
<param name="ranks" type="text" label="Comma-separated list of taxonomy ranks" value="Kingdom,Phylum,Class,Order,Family,Genus,Species"/>
</inputs>

<outputs>
<data name="output" format="phyloseq"/>
</outputs>

<tests>
<test>
<param name="input" value="output.phyloseq" ftype="phyloseq"/>
<param name="ranks" value="Kingdom,Phylum,Class,Order,Family,Genus"/>
<output name="output" ftype="phyloseq">
<assert_contents>
<has_size value="87125" delta="1000"/>
</assert_contents>
</output>
</test>
</tests>

<help>
This tool adds taxonomy rank names to a phyloseq object in the `tax_table` slot.
</help>
<expand macro="citations"/>
</tool>

0 comments on commit d905841

Please sign in to comment.