sylph profiles against a GTDB database report each detected genome by its NCBI accession. GTDB places all of those genomes on two reference trees, one for bacteria and one for archaea. This vignette gets those trees, reads them, and cuts them down to the genomes in your data, which every other gtdb3h function starts from. It also makes the phylogenetic correlation matrix a model needs.
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.
Read the trees
gtdb_tree_read() reads the tree files (gzip-compressed
or not). Given the bacterial and archaeal trees together, it joins them
into one tree:
library(gtdb3h)
tree_files <- gtdb_example("tree") # e.g. gtdb_tree_download(232, "~/projects/gtdb")
tree <- gtdb_tree_read(tree_files)
tree
#>
#> Phylogenetic tree with 560 tips and 560 internal nodes.
#>
#> Tip labels:
#> GCF_037023725.1, GCF_900105835.1, GCF_001042985.1, GCF_001457615.1, GCF_001598695.1, GCF_002925905.1, ...
#> Node labels:
#> , 7:d__Bacteria, 1, 0, 100:p__Pseudomonadota, 97:c__Gammaproteobacteria, ...
#>
#> Rooted; includes branch length(s).Reading the full release 232 trees this way takes about a second and gives a tree of about 200,000 tips.
Two things are tidied as the trees are read:
-
Tips. GTDB writes accessions with a prefix saying
where the genome came from,
RS_(RefSeq) orGB_(GenBank):RS_GCF_003697165.2. sylph andsylph2lab::sylph_aggregate()do not, so the prefix is removed:GCF_003697165.2. -
Node labels. GTDB labels each internal node with
its bootstrap support and, where a GTDB taxon begins, the taxon’s name:
'100:f__Enterobacteriaceae', or several names where ranks begin at the same node,'99.0:c__Bacilli; o__Lactobacillales'. The quotes are removed, andgtdb_node_labels()splits the labels:
nodes <- gtdb_node_labels(tree)
nodes[!is.na(nodes$taxa), ]
#> # A tibble: 174 × 6
#> node label support taxa taxon rank
#> <int> <chr> <dbl> <chr> <chr> <chr>
#> 1 562 7:d__Bacteria 7 d__Bacte… Bact… doma…
#> 2 565 100:p__Pseudomonadota 100 p__Pseud… Pseu… phyl…
#> 3 566 97:c__Gammaproteobacteria 97 c__Gamma… Gamm… class
#> 4 568 3:o__Pseudomonadales; f__Pseudomonadaceae 3 o__Pseud… Pseu… fami…
#> 5 569 96:g__Aquipseudomonas 96 g__Aquip… Aqui… genus
#> 6 571 1:o__Enterobacterales 1 o__Enter… Ente… order
#> 7 572 0:f__Enterobacteriaceae 0 f__Enter… Ente… fami…
#> 8 574 60:g__Klebsiella 60 g__Klebs… Kleb… genus
#> 9 582 17:g__Escherichia 17 g__Esche… Esch… genus
#> 10 583 58:g__Citrobacter_A 58 g__Citro… Citr… genus
#> # ℹ 164 more rows
table(nodes$rank)
#>
#> class domain family genus order phylum
#> 6 1 36 114 14 3The two domains are joined under a new root, each 1 unit of branch
length away (root_edge). GTDB infers their trees
separately, so this distance is not an estimate; it only sets how far
apart they are drawn. To keep them apart, use
gtdb_tree_read(tree_files, combine = FALSE), which returns
a list of two trees.
Prune to the genomes in your data
The full trees are far bigger than any study.
gtdb_tree_prune() keeps only the genomes you name, either
as a vector of accessions or as a data frame with an
accession column, such as the output of
sylph2lab::sylph_aggregate() or a table of model
estimates:
detections <- read.csv(gtdb_example("detections"))
head(detections[c("sample_id", "accession", "genus", "taxonomic_abundance")])
#> sample_id accession genus taxonomic_abundance
#> 1 SRR41520862 GCF_964248265.1 Phocaeicola 9.5146
#> 2 SRR41520862 GCF_051066175.1 Collinsella 6.4363
#> 3 SRR41520862 GCF_000020605.1 Roseburia 5.5968
#> 4 SRR41520862 GCA_958454115.1 Blautia_A 5.3170
#> 5 SRR41520862 GCF_020687435.1 Blautia_A 5.0371
#> 6 SRR41520862 GCF_001314995.1 Bacteroides 3.9510
stool <- detections[detections$isolation_source == "Stool Swab", ]
stool_tree <- gtdb_tree_prune(tree, stool)
#> Kept 457 of 560 tips.
stool_tree
#>
#> Phylogenetic tree with 457 tips and 456 internal nodes.
#>
#> Tip labels:
#> GCF_037023725.1, GCF_001457615.1, GCF_001598695.1, GCF_002925905.1, GCF_000742135.1, GCF_000828055.2, ...
#> Node labels:
#> , 7:d__Bacteria, 1, 0, 100:p__Pseudomonadota, 97:c__Gammaproteobacteria, ...
#>
#> Rooted; includes branch length(s).In a real analysis, prune the full tree read from the files you
downloaded; here the example tree is already pruned to all 560 detected
genomes, and stool_tree keeps the 457 found in stool
swabs.
If some accessions are not tips, gtdb_tree_prune()
warns. The usual causes are a tree from another GTDB release than the
sylph database, or reading only the bacterial tree when archaea were
detected.
Clade names are kept. Pruning leaves internal nodes
with a single child, which are removed. Those nodes often carry a GTDB
name: if one genus of a family remains, the family’s node is removed.
Before a node is removed, gtdb_tree_prune() moves its names
to its child, so the names stay on the tree: on the next internal node
("o__Lachnospirales; f__Lachnospiraceae"), or, when the
only descendant left is one genome, on that tip (in the tree’s
tip.taxa element).
The taxonomy of each tip
Because the tree carries the GTDB names, each tip’s lineage can be
read from its ancestors with gtdb_tip_taxonomy(), without
the taxonomy files:
tip_tax <- gtdb_tip_taxonomy(tree)
tip_tax
#> # A tibble: 560 × 9
#> accession domain phylum class order family genus species gtdb_lineage
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 GCF_037023725.1 Bacteria Pseud… Gamm… Pseu… Pseud… Aqui… NA d__Bacteria…
#> 2 GCF_900105835.1 Bacteria Pseud… Gamm… Pseu… Pseud… Aqui… NA d__Bacteria…
#> 3 GCF_001042985.1 Bacteria Pseud… Gamm… Pseu… Pseud… Aqui… NA d__Bacteria…
#> 4 GCF_001457615.1 Bacteria Pseud… Gamm… Pseu… Pseud… Pseu… NA d__Bacteria…
#> 5 GCF_001598695.1 Bacteria Pseud… Gamm… Ente… Enter… Kleb… NA d__Bacteria…
#> 6 GCF_002925905.1 Bacteria Pseud… Gamm… Ente… Enter… Kleb… NA d__Bacteria…
#> 7 GCF_020525925.1 Bacteria Pseud… Gamm… Ente… Enter… Kleb… NA d__Bacteria…
#> 8 GCF_000742135.1 Bacteria Pseud… Gamm… Ente… Enter… Kleb… NA d__Bacteria…
#> 9 GCF_000828055.2 Bacteria Pseud… Gamm… Ente… Enter… Kleb… NA d__Bacteria…
#> 10 GCF_000215745.1 Bacteria Pseud… Gamm… Ente… Enter… Kleb… NA d__Bacteria…
#> # ℹ 550 more rows
colSums(is.na(tip_tax))
#> accession domain phylum class order family
#> 0 0 0 0 0 2
#> genus species gtdb_lineage
#> 18 560 0It agrees with the GTDB taxonomy files. Species, and genera with a
single species in all of GTDB, are not named on the tree (they have no
internal node of their own), so those are NA.
gtdb_tree_plot() uses this lineage to label tips when your
model output has only accessions.
The phylogenetic correlation matrix
A phylogenetic random effect lets related taxa have similar effects. Its correlation matrix, from Brownian motion on the tree, has as its entry for two genomes the share of their root-to-tip branch length that they have in common:
A <- gtdb_tree_vcv(stool_tree)
dim(A)
#> [1] 457 457
round(A[1:4, 1:4], 2)
#> GCF_037023725.1 GCF_001457615.1 GCF_001598695.1 GCF_002925905.1
#> GCF_037023725.1 1.00 0.96 0.85 0.85
#> GCF_001457615.1 0.96 1.00 0.87 0.87
#> GCF_001598695.1 0.85 0.87 1.00 1.00
#> GCF_002925905.1 0.85 0.87 1.00 1.00Its row and column names are the accessions, so a model can match them to the data. For example, in brms:
brms::brm(
lost ~ condition + abundance_clr + (1 | gr(accession, cov = A)),
data = taxon_data, data2 = list(A = A), family = "bernoulli"
)gtdb_tree_vcv(tree, corr = FALSE) gives the covariance
matrix instead. The matrix has one entry per pair of genomes, so prune
the tree first.
See vignette("gtdb-workflow") for a full analysis, and
vignette("gtdb-tree-plot") for plotting the model’s output
on the tree.