Add taxonomic prefixes to tax_table columns
Source:R/modify_taxonomy_prefix.R
add_taxonomy_prefix.RdAdds QIIME-style two-underscore prefixes (e.g., "k__", "p__") to values in a taxonomy matrix or data.frame if they are not already present. The function is idempotent, meaning it will not add duplicate prefixes when reapplied.
Usage
add_taxonomy_prefix(
me,
prefix_map = c(Kingdom = "k__", Phylum = "p__", Class = "c__", Order = "o__", Family =
"f__", Genus = "g__", Species = "s__", Strain = "t__")
)Arguments
- me
A
charactermatrix,data.frame,microEDAorphyloseq-class object containing atax_table. If amicroEDAorphyloseqobject is passed, its internal taxonomyTable is modified.- prefix_map
A named
charactervector mapping taxonomic rank names (e.g., "Kingdom", "Phylum") to their corresponding prefixes (e.g., "k__", "p__"). Defaults to standard QIIME-style prefixes for common ranks including "Strain" ("t__").
Value
Returns the input object with prefixes added to appropriate cells:
For matrices/data.frames: returns the modified matrix or data.frame.
For
microEDA/phyloseqobjects: returns the updatedmicroEDA/phyloseqobject with prefixedtax_table.
Details
This function supports both character matrices/data.frames and microEDA or
phyloseq objects.
It only modifies columns matching known taxonomic ranks (as defined in prefix_map),
leaving other columns and missing/empty values (NA, "", " ") unchanged.
Only columns in
methat match keys inprefix_mapare processed.Values already starting with any valid prefix (e.g., "k__Bacteria") are skipped.
Empty strings (
""),NA, and whitespace-only entries are preserved without modification.
See also
trim_taxonomy_prefix() to remove prefixes.
tax_table() to access taxonomy data.
Examples
# Example 1: Basic usage with data.frame
tax_data <- data.frame(
Kingdom = c("Bacteria", "Archaea"),
Phylum = c("Proteobacteria", "Euryarchaeota"),
Genus = c("Escherichia", "Methanobrevibacter"),
Species = c("Escherichia coli", "Methanobrevibacter smithii"),
stringsAsFactors = FALSE
)
prefixed <- add_taxonomy_prefix(tax_data)
prefixed
#> Kingdom Phylum Genus
#> 1 k__Bacteria p__Proteobacteria g__Escherichia
#> 2 k__Archaea p__Euryarchaeota g__Methanobrevibacter
#> Species
#> 1 s__Escherichia coli
#> 2 s__Methanobrevibacter smithii
# Example 2: Idempotent (applying again changes nothing)
add_taxonomy_prefix(prefixed)
#> Kingdom Phylum Genus
#> 1 k__Bacteria p__Proteobacteria g__Escherichia
#> 2 k__Archaea p__Euryarchaeota g__Methanobrevibacter
#> Species
#> 1 s__Escherichia coli
#> 2 s__Methanobrevibacter smithii
# Example 3: Basic usage with phyloseq objects
data("GlobalPatterns", package = "phyloseq")
GlobalPatterns <- add_taxonomy_prefix(GlobalPatterns)
head(phyloseq::tax_table(GlobalPatterns)[, c("Kingdom", "Phylum", "Order")])
#> Taxonomy Table: [6 taxa by 3 taxonomic ranks]:
#> Kingdom Phylum Order
#> 549322 "k__Archaea" "p__Crenarchaeota" NA
#> 522457 "k__Archaea" "p__Crenarchaeota" NA
#> 951 "k__Archaea" "p__Crenarchaeota" "o__Sulfolobales"
#> 244423 "k__Archaea" "p__Crenarchaeota" NA
#> 586076 "k__Archaea" "p__Crenarchaeota" NA
#> 246140 "k__Archaea" "p__Crenarchaeota" NA