Focusing on one taxon, with or without CLR
Source:vignettes/sylph-taxon-plot.Rmd
sylph-taxon-plot.RmdOften the question is about one organism: is Clostridioides
difficile cleared after treatment, does Escherichia coli
expand in the gut, how much Staphylococcus is on the skin?
sylph_taxon_plot() pulls out one taxon’s abundance in every
sample, including the samples where it was not detected, and plots it by
group, as per-subject trajectories, or as the change from each subject’s
first sample to their last. sylph_taxon_abundance() returns
the values behind the plot, for modelling.
Files you need
sylph2lab works on files you already have, made by sylph (usually through sra2lab) and downloaded from NCBI and GTDB. A typical analysis directory looks like this:
~/projects/
├── gtdb-r232-c200-dbv2.syl2db <- sylph database (shared, ~19 GB)
├── gtdb/
│ ├── bac120_taxonomy_r232.tsv.gz <- GTDB taxonomy (shared, optional)
│ └── ar53_taxonomy_r232.tsv.gz
└── PRJNA1556220/ <- analysis directory (working directory)
└── data/
├── PRJNA1556220_runs.csv <- run metadata
└── sylph/ <- one *_profiling.tsv per run
The sylph database. The profiles are made by sylph
against a reference database. Pre-built databases are listed on the
sylph documentation’s pre-built
databases page. The example data in this vignette were profiled
against the GTDB r232 two-stage database,
gtdb-r232-c200-dbv2.syl2db (about 19 GB, needs sylph 1.0.0
or later), which sra2lab::sylph_runs() uses by default:
cd ~/projects
wget http://faust.compbio.cs.cmu.edu/sylph-stuff/gtdb-r232-c200-dbv2.syl2db
# mirror: https://storage.googleapis.com/sylph-stuff/gtdb-r232-c200-dbv2.syl2dbsylph2lab itself never reads the database, but it needs to know which GTDB release it came from, to download the matching taxonomy.
GTDB taxonomy (optional). sylph reports each genome by its file name (which holds the NCBI assembly accession) and the name of its first contig. For GTDB names at every rank, download the taxonomy tables of the same GTDB release as the database from the GTDB downloads page. For release 232, they are in https://data.gtdb.ecogenomic.org/releases/release232/232.0/:
| File | Size | Contents |
|---|---|---|
bac120_taxonomy_r232.tsv.gz |
9.4 MB | bacterial genomes |
ar53_taxonomy_r232.tsv.gz |
0.3 MB | archaeal genomes |
gtdb_url <- "https://data.gtdb.ecogenomic.org/releases/release232/232.0"
dir.create("~/projects/gtdb", showWarnings = FALSE)
for (f in c("bac120_taxonomy_r232.tsv.gz", "ar53_taxonomy_r232.tsv.gz")) {
download.file(file.path(gtdb_url, f), file.path("~/projects/gtdb", f),
mode = "wb")
}The much larger GTDB metadata tables in the same directory
(bac120_metadata_r232.tsv.gz, 276 MB, and
ar53_metadata_r232.tsv.gz, 6.7 MB) hold genome size,
completeness, NCBI names and more for each accession. sylph2lab does not
need them, but they can be joined to the sylph_aggregate()
output by accession in the same way (remove the
RS_ or GB_ prefix from their accessions
first). For an older database, use the same release’s files,
e.g. release220/220.0/bac120_taxonomy_r220.tsv.gz for
gtdb-r220-c200-dbv1.syldb.
Run metadata.
sra2lab::get_bioproject_runs() returns one row per SRA run,
with the BioSample attributes the submitters gave (subject, collection
date, body site, …). Save it next to the profiles:
runs <- sra2lab::get_bioproject_runs("PRJNA1556220")
write.csv(runs, "data/PRJNA1556220_runs.csv", row.names = FALSE)For local reads profiled with sra2lab::sylph_local(),
use your own specimen table instead, with one row per specimen ID.
The sylph profiles come from
sra2lab::sylph_runs(), sylph_stream(),
sylph_local() or multiprocess_runs(), one
<id>_profiling.tsv per sample; see
vignette("sylph-workflow", package = "sra2lab").
The example data
sylph2lab ships the profiles of 18 runs from BioProject PRJNA1556220,
“Microbiome changes in the peri-heart transplant setting”, profiled
against gtdb-r232-c200-dbv2.syl2db: the 8 runs of the
sra2lab sylph vignette, plus repeat stool and perirectal swabs from the
same subjects, so that five subjects have two to six samples each. It
also ships the run table for all 67 runs of the BioProject, and the rows
of the two GTDB r232 taxonomy files for the 560 genomes detected in the
example profiles (559 bacterial, 1 archaeal).
sylph_example() gives their paths, which stand in for
data/sylph, data/PRJNA1556220_runs.csv and the
full taxonomy files below. The taxonomy subsets only cover the example
genomes; for your own data, download the full files.
Pick out a taxon
Combine the profiles with the run table, as in
vignette("sylph-aggregate"):
library(sylph2lab)
profiles <- sylph_aggregate(sylph_example("profiles"),
metadata = sylph_example("runs"))sylph_taxon_abundance() finds the genomes whose
rank column matches taxon and adds up their
abundance in each sample. Samples where none were detected get 0, which
matters: the profiles only list genomes that were detected, so
simply filtering them would leave out the zeros. keep
carries sample-level columns along:
ecoli <- sylph_taxon_abundance(
profiles, "Escherichia coli",
keep = c("host_subject_id", "isolation_source", "collection_date")
)
ecoli
#> # A tibble: 18 × 6
#> sample_id host_subject_id isolation_source collection_date abundance
#> <chr> <chr> <chr> <date> <dbl>
#> 1 SRR41520862 GOL_019 Stool Swab 2026-04-16 3.50
#> 2 SRR41520863 GOL_017 Stool Swab 2026-04-03 10.7
#> 3 SRR41520865 GOL_017 Perirectal Swab 2026-03-24 0.0717
#> 4 SRR41520867 GOL_015 Stool Swab 2026-05-12 0.230
#> 5 SRR41520868 GOL_019 Stool Swab 2026-05-09 28.2
#> 6 SRR41520869 GOL_016 Perirectal Swab 2026-04-09 10.5
#> 7 SRR41520870 GOL_016 Perirectal Swab 2026-04-03 0.368
#> 8 SRR41520873 GOL_017 Groin Swab 2026-03-24 0
#> 9 SRR41520874 GOL_034 Groin Swab 2026-08-18 0
#> 10 SRR41520876 GOL_015 Stool Swab 2026-05-17 0
#> 11 SRR41520878 GOL_017 Perirectal Swab 2026-04-03 14.7
#> 12 SRR41520879 GOL_017 Stool Swab 2026-03-27 8.13
#> 13 SRR41520882 GOL_017 Perirectal Swab 2026-03-27 22.5
#> 14 SRR41520884 GOL_003 Stool Swab 2025-09-16 0
#> 15 SRR41520894 GOL_018 Stool Swab 2026-03-02 0
#> 16 SRR41520897 GOL_015 Axilla Swab 2026-05-11 0
#> 17 SRR41520902 GOL_003 Stool Swab 2025-09-09 2.93
#> 18 SRR41520919 GOL_016 Axilla Swab 2026-03-30 0
#> # ℹ 1 more variable: n_genomes <int>n_genomes counts the matching genomes in each sample.
E. coli was detected in most stool and perirectal swabs, at up
to 28%, and in none of the skin swabs.
taxon is a regular expression, matched ignoring case, so
"Staphylococcus" with rank = "genus" covers
the whole genus, and "Escherichia|Klebsiella" two genera at
once. Use fixed = TRUE to match the text exactly. With GTDB
taxonomy (below), rank = "gtdb_taxonomy" lets you match a
lineage at any rank, e.g. "f__Enterobacteriaceae".
Compare groups
With no subject, sylph_taxon_plot() draws a
box plot per group, with each sample as a point:
sylph_taxon_plot(profiles, "Staphylococcus epidermidis",
group = "isolation_source")
S. epidermidis makes up most of every skin swab and is absent from the gut swabs.
CLR-transformed abundance
Relative abundances are compositional: when one taxon grows, all the
others shrink as a share of the total, even if their absolute amounts
are unchanged. The centred log-ratio (CLR) compares a taxon with the
geometric mean of the whole sample instead, and is the usual scale for
comparing a taxon across samples. Set clr = TRUE:
sylph_taxon_plot(profiles, "Escherichia coli", group = "isolation_source",
clr = TRUE)
How the CLR is computed:
- The matching genomes are added together into one part, so a taxon with several genomes is one part, not several.
- Every other feature (by default each genome,
clr_rank = "accession") is a part of its own. A feature not detected in a sample counts as 0 there. - Zeros are replaced with
pseudocount, by default half the smallest non-zero abundance in the data. - The taxon’s CLR is its log abundance minus the mean log abundance of all parts in the sample.
Samples where the taxon was not detected therefore get a low CLR
value determined by the pseudocount, not a missing value. Because sylph
only reports genomes detected somewhere in the data, the CLR depends on
which samples are analysed together and on the pseudocount; report both,
and check that conclusions hold for another pseudocount
(pseudocount = 0.001, say).
Follow subjects over time
With subject, each subject’s samples are joined by a
line. time (a date or number) orders the samples and places
them on the x axis; without it, the x axis shows the samples’ order
(“Sample 1”, “Sample 2”, …). Five subjects gave repeat stool or
perirectal swabs, days to weeks apart. They were enrolled months apart,
so rather than calendar dates, use the days since each subject’s first
sample:
profiles$study_day <- as.numeric(
profiles$collection_date -
ave(profiles$collection_date, profiles$host_subject_id, FUN = min)
)
perirectal_and_stool <- profiles[profiles$isolation_source %in%
c("Perirectal Swab", "Stool Swab"), ]
sylph_taxon_plot(perirectal_and_stool, "Escherichia coli",
subject = "host_subject_id", time = "study_day",
group = "isolation_source")
#> Warning: Some subjects have samples in more than one `group`; the change plot
#> uses each subject's first sample's group.
With group, each group gets its own panel and colour,
and a subject sampled at two sites (GOL_017) gets a line in each.
Subjects with one sample in a group appear as single points. E.
coli rose sharply in some subjects (GOL_016, GOL_017 and GOL_019)
and disappeared in others (GOL_003 and GOL_015).
type = "change" plots each subject’s change from first
to last sample (percentage points, or CLR units with
clr = TRUE), and type = "both" puts the two
plots side by side. Subjects with a single sample are left out of the
change plot, with a message. Here, for the stool swabs alone:
stool <- profiles[profiles$isolation_source == "Stool Swab", ]
sylph_taxon_plot(stool, "Escherichia coli",
subject = "host_subject_id", time = "study_day",
clr = TRUE, type = "both")
#> 1 subject(s) with a single sample were left out of the change plot.
On the CLR scale, the losses in GOL_003 and GOL_015 are larger than the gain in GOL_019, although in percentage points they are small (3% and 0.2% to undetected) and the gain large (4% to 28%): the CLR measures fold-changes relative to the rest of the community, not shares of it.
With group, the change plot compares groups. In a cohort
with paired samples, for example two C. difficile tests per
patient in patients with recurrent or cleared infection, the same call
compares the groups’ changes:
sylph_taxon_plot(profiles, "Clostridioides difficile",
subject = "patient_id", group = "outcome",
time = "collection_date", clr = TRUE, type = "both")Modelling
sylph_taxon_abundance() gives one row per sample, ready
for a model. For paired samples, compute each subject’s change and
compare groups:
cd <- sylph_taxon_abundance(
profiles, "Clostridioides difficile", clr = TRUE,
keep = c("patient_id", "outcome", "collection_date")
)
cd <- cd[order(cd$patient_id, cd$collection_date), ]
change <- aggregate(clr ~ patient_id + outcome, data = cd,
FUN = function(x) x[length(x)] - x[1])
summary(lm(clr ~ outcome, data = change))With GTDB taxonomy
Without GTDB taxonomy, as above, species and
genus come from NCBI contig names (see
vignette("sylph-aggregate")). With the GTDB taxonomy (see
Files you need), names are consistent
across genomes, but they can differ from NCBI’s. GTDB r232 splits the
genomes NCBI calls Escherichia coli into several genera, so the
GTDB species "Escherichia coli" covers fewer samples:
profiles_gtdb <- sylph_aggregate(sylph_example("profiles"),
metadata = sylph_example("runs"),
taxonomy = sylph_example("taxonomy"))
unique(profiles_gtdb[profiles_gtdb$organism == "Escherichia coli",
c("accession", "genus", "species")])
#> # A tibble: 3 × 3
#> accession genus species
#> <chr> <chr> <chr>
#> 1 GCA_047199115.1 G047199115 G047199115 sp047199115
#> 2 GCF_003697165.2 Escherichia Escherichia coli
#> 3 GCA_047199055.1 ECMA0423 ECMA0423 sp047199055
ecoli_gtdb <- sylph_taxon_abundance(profiles_gtdb, "Escherichia coli")
sum(ecoli_gtdb$abundance > 0) # samples with GTDB E. coli
#> [1] 5
sum(ecoli$abundance > 0) # samples with NCBI E. coli
#> [1] 11Neither is wrong, but they answer different questions, so check how
the genomes of your taxon are named before plotting.
rank = "gtdb_taxonomy" matches the whole lineage, so a
higher rank, such as the family, covers all of them:
sylph_taxon_plot(profiles_gtdb[profiles_gtdb$isolation_source == "Stool Swab", ],
"f__Enterobacteriaceae", rank = "gtdb_taxonomy",
taxon_label = "Enterobacteriaceae", italic = FALSE,
subject = "host_subject_id", time = "collection_date",
type = "change")
#> 1 subject(s) with a single sample were left out of the change plot.
The archaeon Methanocatella smithii (NCBI: Methanobrevibacter smithii), from the archaeal taxonomy file, is plotted the same way:
sylph_taxon_plot(profiles_gtdb, "Methanocatella smithii",
group = "isolation_source")
taxon_label sets the name in the axis title (in italics;
italic = FALSE for names that are not genus or species,
such as a family).
Customising and saving
The plots are ggplot objects (type = "both" gives a
patchwork object), so they can be changed with + and saved
with ggsave(). The per-sample values are in the plot’s
data element.
p <- sylph_taxon_plot(profiles, "Escherichia coli",
subject = "host_subject_id", time = "collection_date",
clr = TRUE, type = "both")
ggplot2::ggsave("figs/ecoli_clr.pdf", p, width = 8, height = 6)