From sylph profiles to a phylogenetic model figure
Source:vignettes/gtdb-workflow.Rmd
gtdb-workflow.RmdThis vignette follows an analysis from start to finish: sylph profiles are combined with sylph2lab, the GTDB tree is pruned to the detected genomes, a phylogenetic model is fitted (with brms, outside gtdb3h), and its posterior contrast is plotted on the tree. The model steps are shown but not run; the plot uses the simulated draws shipped with gtdb3h in their place.
The example question: does a process, such as manufacturing a fecal microbiota transplant (FMT) product from donated stool, make some taxa more likely to be lost than others, and are those taxa related?
Files you need
gtdb3h works on files you already have from a sylph analysis, plus the GTDB reference trees. A typical layout, shared with sylph2lab:
~/projects/
├── gtdb-r232-c200-dbv2.syl2db <- sylph database (shared, ~19 GB)
├── gtdb/
│ ├── bac120_r232.tree.gz <- GTDB trees (shared)
│ ├── ar53_r232.tree.gz
│ ├── bac120_taxonomy_r232.tsv.gz <- GTDB taxonomy (shared, optional)
│ └── ar53_taxonomy_r232.tsv.gz
└── PRJNA1556220/ <- analysis directory
├── data/
│ ├── PRJNA1556220_runs.csv <- run metadata
│ └── sylph/ <- one *_profiling.tsv per run
└── models/ <- model fits and posterior draws
The GTDB release. Everything must come from the GTDB
release the sylph database was built from. The sylph database
gtdb-r232-c200-dbv2.syl2db (from the sylph documentation’s
pre-built
databases page) holds the species representative genomes of GTDB
release 232; for gtdb-r220-c200-dbv1.syldb, use release
220.
The GTDB trees. GTDB publishes two trees per release, one tip per species representative genome, so every genome sylph detects is a tip:
| File | Size (r232) | Contents |
|---|---|---|
bac120_r232.tree.gz |
2.3 MB | bacteria (about 190,000 tips), from 120 marker genes |
ar53_r232.tree.gz |
0.15 MB | archaea (about 10,000 tips), from 53 marker genes |
They are in https://data.gtdb.ecogenomic.org/releases/release232/232.0/
(see the GTDB downloads
page). gtdb_tree_download() fetches them:
library(gtdb3h)
trees <- gtdb_tree_download(232, dest_dir = "~/projects/gtdb")
trees
#> bacteria archaea
#> "~/projects/gtdb/bac120_r232.tree.gz" "~/projects/gtdb/ar53_r232.tree.gz"gtdb_tree_url(232) gives the URLs, to download them
another way. Do not use the trees in auxillary_files/
(*.sp_labels.tree), whose tips are species names rather
than accessions.
Your detections. The genomes to keep come from the
sylph profiles, combined with sylph2lab::sylph_aggregate(),
whose accession column holds the genome accessions that
match the trees’ tips. With the GTDB taxonomy files (see
vignette("sylph-aggregate", package = "sylph2lab")) it also
has the GTDB names at every rank; gtdb3h can also read ranks down to
genus from the tree itself.
Model output. gtdb3h does not fit models. It plots
their output: a table of taxon-level estimates, or posterior draws with
one row per draw and taxon, keyed by accession.
The example data
gtdb3h ships, through gtdb_example():
-
"tree": the GTDB r232 bacterial and archaeal trees, pruned to the 560 genomes (559 bacterial, 1 archaeal) detected in the sylph2lab example profiles (18 SRA runs of BioProject PRJNA1556220), in the same format as the full files. They stand in for~/projects/gtdb/bac120_r232.tree.gzandar53_r232.tree.gz. -
"detections": those profiles aftersylph2lab::sylph_aggregate()with the run table and GTDB taxonomy, one row per sample and genome. -
"draws": simulated posterior draws of a taxon-level effect for the 560 genomes, standing in for a model’s output. They were simulated to be similar in related taxa, as a phylogenetic model would find; they are not results.
1. Combine the profiles
sylph2lab::sylph_aggregate() reads the per-sample
profiles and joins the sample metadata and the GTDB taxonomy:
profiles <- sylph2lab::sylph_aggregate(
sylph2lab::sylph_example("profiles"), # e.g. "data/sylph"
metadata = sylph2lab::sylph_example("runs"), # e.g. "data/PRJNA1556220_runs.csv"
taxonomy = sylph2lab::sylph_example("taxonomy")
# e.g. c("~/projects/gtdb/bac120_taxonomy_r232.tsv.gz",
# "~/projects/gtdb/ar53_taxonomy_r232.tsv.gz")
)
profiles[c("sample_id", "accession", "genus", "taxonomic_abundance")]
#> # A tibble: 1,280 × 4
#> sample_id accession genus taxonomic_abundance
#> <chr> <chr> <chr> <dbl>
#> 1 SRR41520862 GCF_964248265.1 Phocaeicola 9.51
#> 2 SRR41520862 GCF_051066175.1 Collinsella 6.44
#> 3 SRR41520862 GCF_000020605.1 Roseburia 5.60
#> 4 SRR41520862 GCA_958454115.1 Blautia_A 5.32
#> 5 SRR41520862 GCF_020687435.1 Blautia_A 5.04
#> 6 SRR41520862 GCF_001314995.1 Bacteroides 3.95
#> 7 SRR41520862 GCF_027667425.1 Fusicatenibacter 3.61
#> 8 SRR41520862 GCA_900755095.1 Bacteroides 3.49
#> 9 SRR41520862 GCF_025147765.1 Blautia_A 3.03
#> 10 SRR41520862 GCF_020554845.1 Blautia_A 3.00
#> # ℹ 1,270 more rowsThe example detections installed with gtdb3h are this table, with fewer columns:
2. Prune the tree
Read the GTDB trees of the database’s release and keep the detected genomes:
tree <- gtdb_tree_read(gtdb_example("tree"))
# in a real analysis:
# tree <- gtdb_tree_read(gtdb_tree_download(232, dest_dir = "~/projects/gtdb"))
tree <- gtdb_tree_prune(tree, detections)
#> Kept 560 of 560 tips.
A <- gtdb_tree_vcv(tree)3. Fit a model, elsewhere
gtdb3h does not fit models, but the model needs two things from it:
the accessions, to match the tips, and the correlation matrix
A. A sketch, for data with one row per donated stool
specimen and taxon detected in it (taxon_data), recording
whether the taxon was lost from the paired FMT product or the donor’s
next stool (lost), which of the two the pair is
(sample_source), and the taxon’s CLR abundance in the stool
(abundance_clr):
library(brms)
fit <- brm(
lost ~ (1 + sample_source + abundance_clr | donor_id) +
(1 | gr(accession, cov = A)),
data = taxon_data, data2 = list(A = A),
family = "bernoulli", backend = "cmdstanr",
chains = 4, cores = 4, seed = 16,
file = "models/m_taxonloss_phylo"
)The phylogenetic term (1 | gr(accession, cov = A)) gives
each taxon an effect that is correlated between related taxa. To make it
part of the quantity on the tree, take the posterior contrast between
the FMT product and the next stool for each taxon, at a typical
abundance:
library(tidybayes)
newdata <- tidyr::expand_grid(
accession = tree$tip.label,
donor_id = unique(taxon_data$donor_id),
sample_source = c("FMT Product", "Donated Stool"),
abundance_clr = stats::median(taxon_data$abundance_clr)
)
draws <- newdata |>
add_epred_draws(fit, ndraws = 500) |>
ungroup() |>
tidyr::pivot_wider(id_cols = c(.draw, accession, donor_id),
names_from = sample_source, values_from = .epred) |>
dplyr::mutate(effect = `FMT Product` - `Donated Stool`) |>
# average over donors within each draw
dplyr::summarise(effect = mean(effect), .by = c(.draw, accession))draws now has one row per draw and taxon: the increase
in the probability that the taxon is lost attributable to manufacture,
adjusted for abundance.
4. Plot the contrast on the tree
Here the simulated draws stand in for draws:
draws <- read.csv(gtdb_example("draws"))
estimates <- gtdb_summarise_draws(draws, value = "effect", threshold = 0)
estimates
#> # A tibble: 560 × 7
#> accession estimate .lower .upper .width n_draws prob_above
#> <chr> <dbl> <dbl> <dbl> <dbl> <int> <dbl>
#> 1 GCF_025567095.1 0.423 0.361 0.503 0.95 100 1
#> 2 GCA_018369015.1 0.345 0.303 0.375 0.95 100 1
#> 3 GCF_004216775.1 0.331 0.269 0.384 0.95 100 1
#> 4 GCF_005845215.1 0.330 0.255 0.375 0.95 100 1
#> 5 GCF_003024715.1 0.303 0.272 0.346 0.95 100 1
#> 6 GCF_040545455.1 0.292 0.240 0.345 0.95 100 1
#> 7 GCF_000466485.1 0.268 0.230 0.302 0.95 100 1
#> 8 GCF_003477885.1 0.267 0.248 0.287 0.95 100 1
#> 9 GCA_944383765.1 0.254 0.160 0.343 0.95 100 1
#> 10 GCF_032490495.1 0.237 0.162 0.336 0.95 100 1
#> # ℹ 550 more rowsTaxa most at risk:
gtdb_tree_plot(
tree, estimates, highlight = "above", threshold = 0.2,
clade_rank = "phylum",
fill_label = paste("Increase in Taxon Loss Probability<br>Attributable",
"to FMT Manufacture<br>(simulated)")
)
And taxa robust to it, whose credible interval lies below a small increase:
gtdb_tree_plot(
tree, estimates, highlight = "below", threshold = 0.01,
highlight_by = "interval", max_overlaps = 15,
fill_label = paste("Increase in Taxon Loss Probability<br>Attributable",
"to FMT Manufacture<br>(simulated)")
)
The estimates are a plain tibble, so the same taxa can be listed in a table, with their GTDB names:
at_risk <- estimates[estimates$.lower > 0.1, ]
merge(at_risk, unique(detections[c("accession", "family", "species")]),
by = "accession")[c("species", "family", "estimate", ".lower",
".upper")]
#> species family estimate .lower
#> 1 Catenibacillus sp018369015 Lachnospiraceae 0.34505 0.3029475
#> 2 Ventrisoma faecale Lachnospiraceae 0.21360 0.1571500
#> 3 Anaerobutyricum stercoris Lachnospiraceae 0.16365 0.1251175
#> 4 Mediterraneibacter excrementipullorum Lachnospiraceae 0.21745 0.1636400
#> 5 Enterocloster sp900541315 Lachnospiraceae 0.16895 0.1260775
#> 6 Massilistercora timonensis Lachnospiraceae 0.22275 0.1717925
#> 7 Catenibacillus sp902363555 Lachnospiraceae 0.22570 0.1893300
#> 8 Catenibacillus sp900553975 Lachnospiraceae 0.18880 0.1707800
#> 9 MGBC163016 sp910588435 Eggerthellaceae 0.17795 0.1495025
#> 10 Lachnoclostridium_A sp944383765 Lachnospiraceae 0.25365 0.1598650
#> 11 Pullichristensenella sp946496415 Aristaeellaceae 0.12095 0.1028075
#> 12 Enterocloster sp900555905 Lachnospiraceae 0.18955 0.1323075
#> 13 Mediterraneibacter torques Lachnospiraceae 0.16245 0.1358825
#> 14 Fusobacterium animalis Fusobacteriaceae 0.15945 0.1000475
#> 15 Anaerobutyricum hallii Lachnospiraceae 0.14255 0.1133300
#> 16 Otoolea symbiosa Lachnospiraceae 0.26835 0.2304125
#> 17 Enterocloster sp001517625 Lachnospiraceae 0.20705 0.1638550
#> 18 Enterocloster bolteae Lachnospiraceae 0.23235 0.1845475
#> 19 CHH4-2 sp002899675 Lachnospiraceae 0.12305 0.1034275
#> 20 Enterocloster lavalensis Lachnospiraceae 0.20035 0.1734700
#> 21 Otoolea fessa Lachnospiraceae 0.30335 0.2720500
#> 22 AF33-28 sp003477885 Lachnospiraceae 0.26725 0.2480350
#> 23 Cuneatibacter caecimuris Lachnospiraceae 0.33100 0.2689875
#> 24 Enterocloster sp005845215 Lachnospiraceae 0.33020 0.2546425
#> 25 Dialister hominis Dialisteraceae 0.14070 0.1058725
#> 26 Mediterraneibacter gnavus Lachnospiraceae 0.16655 0.1338650
#> 27 Hominilimicola fabiformis UBA1381 0.18790 0.1701850
#> 28 Dorea_A longicatena Lachnospiraceae 0.12395 0.1009225
#> 29 Mediterraneibacter lactaris Lachnospiraceae 0.13340 0.1101000
#> 30 Anthropogastromicrobium aceti Lachnospiraceae 0.42265 0.3606500
#> 31 Alitiscatomonas aceti Lachnospiraceae 0.20160 0.1257075
#> 32 Veillonella sp032470285 Veillonellaceae 0.17890 0.1466850
#> 33 Pilosibacter fragilis Lachnospiraceae 0.23655 0.1621300
#> 34 Enterocloster citroniae Lachnospiraceae 0.29245 0.2397325
#> 35 Enterocloster aldenensis Lachnospiraceae 0.14895 0.1234075
#> 36 QAND01 sp003150225 QAND01 0.16315 0.1096825
#> 37 Lientehia sp934351765 Lachnospiraceae 0.21700 0.2040425
#> 38 Anaerobutyricum soehngenii Lachnospiraceae 0.15845 0.1064100
#> 39 Lachnoclostridium_A edouardi Lachnospiraceae 0.17920 0.1573175
#> 40 Brotaphodocola sp900538475 Lachnospiraceae 0.19275 0.1549825
#> .upper
#> 1 0.3750275
#> 2 0.2710100
#> 3 0.2027200
#> 4 0.2681100
#> 5 0.2065975
#> 6 0.2670250
#> 7 0.2690375
#> 8 0.2127825
#> 9 0.2039675
#> 10 0.3429650
#> 11 0.1466200
#> 12 0.2439625
#> 13 0.1949850
#> 14 0.2383325
#> 15 0.1746225
#> 16 0.3022900
#> 17 0.2541450
#> 18 0.2911025
#> 19 0.1456575
#> 20 0.2298575
#> 21 0.3457875
#> 22 0.2868050
#> 23 0.3838025
#> 24 0.3750150
#> 25 0.1846725
#> 26 0.1978300
#> 27 0.2045925
#> 28 0.1423425
#> 29 0.1589775
#> 30 0.5028525
#> 31 0.2754475
#> 32 0.2199325
#> 33 0.3355500
#> 34 0.3446575
#> 35 0.1720750
#> 36 0.2188125
#> 37 0.2369375
#> 38 0.2233175
#> 39 0.2015700
#> 40 0.2319200