Skip to contents

A model with a taxon-level term, such as a phylogenetic random effect, gives an estimate for every taxon: how much more likely each taxon is to be lost, how much its abundance changes, and so on. gtdb_tree_plot() puts those estimates on the GTDB tree, so you can see whether whole clades behave alike, and highlights and names the taxa at either end.

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.gz and ar53_r232.tree.gz.
  • "detections": those profiles after sylph2lab::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.

From draws to one row per taxon

A Bayesian model’s output usually comes as posterior draws: one row per draw and taxon, e.g. from tidybayes::add_epred_draws() and a contrast between conditions. The example draws are of that shape (and simulated):

library(gtdb3h)

draws <- read.csv(gtdb_example("draws"))
head(draws)
#>   .draw       accession  effect
#> 1     1 GCF_037023725.1  0.0241
#> 2     2 GCF_037023725.1 -0.0185
#> 3     3 GCF_037023725.1  0.0844
#> 4     4 GCF_037023725.1 -0.0753
#> 5     5 GCF_037023725.1 -0.0106
#> 6     6 GCF_037023725.1 -0.0752

gtdb_summarise_draws() reduces them to one row per taxon: the posterior median (estimate), a 95% credible interval (.lower, .upper), and with threshold, the posterior probability of being above it:

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 rows

If your draws hold several values per draw and taxon, e.g. predictions over a grid of abundances or per donor, either average them first, or keep them apart with by and plot one group at a time.

Estimates from any other source work as well, as long as there is one row per taxon with an accession column (or name another with taxon).

The tree

Read the tree and prune it to the taxa with estimates (see vignette("gtdb-tree")):

tree <- gtdb_tree_read(gtdb_example("tree"))
tree <- gtdb_tree_prune(tree, estimates)
#> Kept 560 of 560 tips.

Plot the estimates

gtdb_tree_plot(tree, estimates)

Unrooted GTDB tree of 560 genomes with tips coloured by a simulated effect, highest in part of the Bacillota.

Each point is a genome, filled by its estimate, and more opaque the higher it is. The layout is unrooted: the tree spreads out from its centre, which keeps hundreds of tips from many phyla apart. Branch lengths are to scale, so the long branch on the right is the one archaeon, attached to the bacteria by the arbitrary root_edge.

Highlight the taxa above a threshold

highlight = "above" outlines the tips at or above threshold and labels them, here with their genus and accession. clade_rank names the phyla with at least 10 tips (clade_min_tips):

gtdb_tree_plot(tree, estimates, highlight = "above", threshold = 0.2,
               clade_rank = "phylum",
               fill_label = "Increase in taxon<br>loss probability")

The same tree with tips of effect 0.2 or more outlined and labelled with genus and accession, and phyla named.

The highlighted taxa cluster in one clade of Bacillota, as a phylogenetic effect would make them.

The legend title is Markdown (via ggtext), so <br> breaks lines and *...* sets italics.

Certainty rather than size

A high median with a wide interval is weak evidence. With highlight_by = "interval", a tip is highlighted only when its whole credible interval is above the threshold:

gtdb_tree_plot(tree, estimates, highlight = "above", threshold = 0.1,
               highlight_by = "interval")

Tree with tips whose 95% credible interval lies entirely above 0.1 outlined and labelled.

Highlight the taxa below a threshold

highlight = "below" finds the other end, e.g. taxa robust to a process, whose effect is near zero or negative. The transparency is reversed so that these tips are opaque:

gtdb_tree_plot(tree, estimates, highlight = "below", threshold = 0,
               highlight_by = "interval", max_overlaps = 15)

Tree with tips whose 95% credible interval lies entirely below zero outlined and labelled; low values are opaque.

When many tips are highlighted, not every label fits; labels that would overlap more than max_overlaps others are dropped. Raise it to keep more, or use label_tips = "none" and list the taxa in a table instead.

Other layouts

layout = "rectangular" draws the familiar rooted tree, root on the left, and layout = "circular" the same tree around a circle. Both name clades at the node where they start.

gtdb_tree_plot(tree, estimates, layout = "rectangular",
               highlight = "above", threshold = 0.25, clade_rank = "phylum",
               legend_position = "right")

Rectangular rooted tree with tips coloured by the effect, high-effect tips labelled, and phyla named at their root nodes.

gtdb_tree_plot(tree, estimates, layout = "circular", clade_rank = "phylum",
               legend_position = "right")

Circular tree with tips coloured by the effect and phyla named.

With branch_lengths = FALSE, only the branching order is drawn; in the rectangular and circular layouts, all tips then line up.

Labels

label is a template: {column} is replaced by the tip’s value of that column of your estimates or, for domain to genus, of the lineage read from the tree. Tips with a missing value (e.g. a genus GTDB has not named on the tree) are labelled by their accession. For example, with the GTDB species from the detections:

detections <- read.csv(gtdb_example("detections"))
species <- unique(detections[c("accession", "species")])
estimates_sp <- merge(estimates, species, by = "accession")

gtdb_tree_plot(tree, estimates_sp, highlight = "above", threshold = 0.25,
               label = "{species}", label_size = 2.5)

Tree with high-effect tips labelled with their GTDB species names.

Customising and saving

The plot is a ggplot object, so it can be changed with +. A diverging fill scale suits effects that can go either way (ggplot2 says it replaces the existing scale):

gtdb_tree_plot(tree, estimates, alpha = FALSE) +
  ggplot2::scale_fill_distiller(palette = "RdBu", limits = c(-0.45, 0.45),
                                name = "Effect")
#> Scale for fill is already present.
#> Adding another scale for fill, which will replace the existing scale.

Tree with a red-blue diverging fill scale centred on zero.

Other options: palette (any viridis option, e.g. "viridis" or "turbo"), transform (e.g. "sqrt"), breaks and limits for the fill scale; point_size, edge_colour and edge_width; show_missing = TRUE to draw tips without an estimate; legend_position; and layout_args for the ggraph layout (list(daylight = FALSE) makes the unrooted layout faster for very large trees).

Save with ggsave(), giving the unrooted and circular layouts a square canvas:

p <- gtdb_tree_plot(tree, estimates, highlight = "above", threshold = 0.2)
ggplot2::ggsave("figs/tree_effect.pdf", p, width = 8, height = 8)

Your own figure

gtdb_tree_graph() returns the tree as an igraph graph, with your estimates and the GTDB lineage attached to the tips, for figures gtdb_tree_plot() does not make:

library(ggraph)
#> Loading required package: ggplot2

g <- gtdb_tree_graph(tree, estimates)
ggraph(g, layout = "dendrogram", circular = TRUE, length = branch_length) +
  geom_edge_elbow(colour = "grey70", edge_width = 0.3) +
  geom_node_point(aes(filter = is_tip, colour = phylum), size = 1) +
  coord_fixed() +
  theme_void()

Circular tree with tips coloured by phylum.