Skip to contents

Sums the abundance of the genomes matching taxon in each sample, optionally as a centred log-ratio (CLR). Samples where the taxon was not detected get an abundance of 0. This is the table behind sylph_taxon_plot(), and is useful for modelling on its own.

Usage

sylph_taxon_abundance(
  data,
  taxon,
  rank = "species",
  abundance = "taxonomic_abundance",
  sample = "sample_id",
  fixed = FALSE,
  clr = FALSE,
  clr_rank = "accession",
  pseudocount = NULL,
  keep = NULL
)

Arguments

data

A data frame from sylph_aggregate(), or any data frame with sample, taxon and abundance columns, one row per sample and genome.

taxon

Text (a regular expression unless fixed = TRUE) to look for in rank, e.g. "Clostridioides difficile".

rank

Column to search for taxon. Defaults to "species". Any column works, e.g. "genus", "organism", "gtdb_taxonomy" or "contig_name".

abundance

Abundance column: "taxonomic_abundance" (the default) or "sequence_abundance".

sample

Column identifying the sample. Defaults to "sample_id".

fixed

If TRUE, match taxon exactly as written (case-sensitive, not a regular expression).

clr

If TRUE, also return the CLR-transformed abundance; see Details.

clr_rank

Column defining the other features of the composition for the CLR. Defaults to "accession" (each genome).

pseudocount

Value that replaces zeros before the CLR. Defaults to half the smallest non-zero abundance in data.

keep

Other columns to carry into the result, e.g. subject, group or date. They must have one value per sample.

Value

A tibble with one row per sample: the sample column, the keep columns, abundance (the summed abundance, %), n_genomes (the number of matching genomes detected), and with clr = TRUE, clr.

Details

Genomes match when taxon is found in their rank column, a regular expression match ignoring case unless fixed = TRUE. With rank = "species", taxon = "Clostridioides difficile" matches C. difficile; with rank = "gtdb_taxonomy" (after sylph_aggregate() with taxonomy), taxon = "g__Clostridioides" matches the whole genus.

CLR. With clr = TRUE, each sample's composition is first built from the matching genomes, added together into one part, and every other feature at clr_rank (by default each genome, "accession"); features not detected in a sample are 0 there. Zeros are replaced with pseudocount, and the CLR of the taxon is $$\log(x_{taxon}) - \frac{1}{D}\sum_{j=1}^{D} \log(x_j)$$ over the sample's \(D\) parts. Because sylph reports only genomes detected in at least one sample, \(D\) is the number of features detected anywhere in data, and the CLR depends on which samples are included and on the pseudocount; report both.

See also

sylph_taxon_plot() to plot it.

Examples

profiles <- sylph_aggregate(sylph_example("profiles"),
                            metadata = sylph_example("runs"))

sylph_taxon_abundance(profiles, "Staphylococcus", rank = "genus",
                      keep = c("host_subject_id", "isolation_source"),
                      clr = TRUE)
#> # A tibble: 18 × 6
#>    sample_id   host_subject_id isolation_source abundance n_genomes    clr
#>    <chr>       <chr>           <chr>                <dbl>     <int>  <dbl>
#>  1 SRR41520862 GOL_019         Stool Swab          0              0 -0.949
#>  2 SRR41520863 GOL_017         Stool Swab          0              0 -0.498
#>  3 SRR41520865 GOL_017         Perirectal Swab     0              0 -0.639
#>  4 SRR41520867 GOL_015         Stool Swab          0              0 -0.721
#>  5 SRR41520868 GOL_019         Stool Swab          0              0 -0.723
#>  6 SRR41520869 GOL_016         Perirectal Swab     0              0 -0.691
#>  7 SRR41520870 GOL_016         Perirectal Swab    79.4            2  9.15 
#>  8 SRR41520873 GOL_017         Groin Swab         94.1            2  9.50 
#>  9 SRR41520874 GOL_034         Groin Swab         89.9            1  9.49 
#> 10 SRR41520876 GOL_015         Stool Swab          0              0 -0.715
#> 11 SRR41520878 GOL_017         Perirectal Swab     0              0 -0.507
#> 12 SRR41520879 GOL_017         Stool Swab          0              0 -0.514
#> 13 SRR41520882 GOL_017         Perirectal Swab     0              0 -0.470
#> 14 SRR41520884 GOL_003         Stool Swab          0              0 -0.230
#> 15 SRR41520894 GOL_018         Stool Swab          0              0 -0.225
#> 16 SRR41520897 GOL_015         Axilla Swab        96.6            1  9.57 
#> 17 SRR41520902 GOL_003         Stool Swab          0.0801         1  1.31 
#> 18 SRR41520919 GOL_016         Axilla Swab        75.0            2  9.32