Some pipelines report the taxonomy of each feature as a single string, such as "k__Bacteria; p__Firmicutes; c__Clostridia". parseTaxonomy splits these strings into one column per taxonomic rank. The same parser is used by the importers of mia, e.g. importQIIME2() and importMetaPhlAn().

Arguments

x

Character vector of taxonomy strings, or a data.frame or DataFrame (e.g. rowData(tse)) with the taxonomy strings in column col.name.

col.name

Character scalar. The column of x with the taxonomy strings. Ignored if x is a character vector. (Default: "Taxon")

sep

Character scalar. A regular expression that separates the ranks. The default fits Greengenes and QIIME 2 ("; " or ";"). Use, e.g., "\\|" for MetaPhlAn. (Default: "; |;")

remove.prefix

Logical scalar. Should the rank prefixes (e.g. "p__") be removed from the values? (Default: FALSE)

Value

A DataFrame with one column per rank, named in lower case ("kingdom", "phylum", ...) as in the importers. The row names are the names of x, or the row names if x is a table.

Details

The ranks are recognised by their prefixes: d__ (domain), sk__ (superkingdom), k__ (kingdom), p__ (phylum), c__ (class), o__ (order), f__ (family), g__ (genus), s__ (species) and t__ (strain). A rank that is missing from a string gets NA, and ranks that are missing from all strings are left out.

Examples

taxa <- c(
    ASV1 = "k__Bacteria; p__Firmicutes; c__Bacilli; g__Lactobacillus",
    ASV2 = "k__Bacteria; p__Bacteroidetes; c__Bacteroidia")
parseTaxonomy(taxa)
#> DataFrame with 2 rows and 4 columns
#>          kingdom           phylum          class            genus
#>      <character>      <character>    <character>      <character>
#> ASV1 k__Bacteria    p__Firmicutes     c__Bacilli g__Lactobacillus
#> ASV2 k__Bacteria p__Bacteroidetes c__Bacteroidia               NA
parseTaxonomy(taxa, remove.prefix = TRUE)
#> DataFrame with 2 rows and 4 columns
#>          kingdom        phylum       class         genus
#>      <character>   <character> <character>   <character>
#> ASV1    Bacteria    Firmicutes     Bacilli Lactobacillus
#> ASV2    Bacteria Bacteroidetes Bacteroidia            NA

# MetaPhlAn-style strings
parseTaxonomy(
    "k__Bacteria|p__Firmicutes|c__Clostridia", sep = "\\|",
    remove.prefix = TRUE)
#> DataFrame with 1 row and 3 columns
#>       kingdom      phylum       class
#>   <character> <character> <character>
#> 1    Bacteria  Firmicutes  Clostridia

# Taxonomy stored in one column of rowData
library(SummarizedExperiment)
tse <- SummarizedExperiment(
    assays = list(counts = matrix(1:4, 2L, dimnames = list(names(taxa),
        c("S1", "S2")))),
    rowData = DataFrame(Taxon = taxa))
rowData(tse) <- cbind(
    parseTaxonomy(rowData(tse), remove.prefix = TRUE), rowData(tse))
rowData(tse)
#> DataFrame with 2 rows and 5 columns
#>          kingdom        phylum       class         genus                  Taxon
#>      <character>   <character> <character>   <character>            <character>
#> ASV1    Bacteria    Firmicutes     Bacilli Lactobacillus k__Bacteria; p__Firm..
#> ASV2    Bacteria Bacteroidetes Bacteroidia            NA k__Bacteria; p__Bact..