Lesson 7 of 9
Module 4 · Comparing sequences
A tree turns an alignment into a branching history. Learn how distance trees are built, how to root and read them, and how the bootstrap tells you which branches to trust.
What a tree shows
A phylogenetic tree is a diagram of how sequences are related by descent. In Multiple alignment and conserved regions you lined up sequences in a multiple alignment; a tree summarises that alignment as a branching history. Trees are used to place an unknown sequence among known ones, to trace how a virus spread, to tell genes copied within one genome (paralogues) from the same gene in different species (orthologues), and to judge which relatives to compare when you ask whether a position is conserved.
A few terms cover every tree you will read:
- A tip, or leaf, is one of the sequences you started with.
- An internal node is a point where branches split. It stands for an inferred common ancestor that no one has sequenced.
- A branch joins two nodes. Its branch length, when drawn to scale, is the amount of change along it, often in substitutions per site.
- A clade is a node with everything descended from it: a group that shares one common ancestor.
- The topology is the branching pattern alone, ignoring the lengths.
A tree built from one gene is a gene tree. It describes the history of that gene, which usually, but not always, matches the history of the species, the species tree. You will see a mismatch in this lesson.
The globin family from Why compare sequences: homology, similarity and identity shows why the difference matters. Build a tree from human and mouse alpha and beta globin, and the first split is not between the species but between the alpha genes and the beta genes, because the duplication that made them is older than the split between humans and mice. Each species then appears twice, once in each half. Reading such a tree as a species tree would be a mistake; reading it as a gene tree tells you at once which pairs are orthologues and which are paralogues.
Programs store trees as plain text in Newick format, which writes each clade in brackets with its members separated by commas and ends with a semicolon. ((human,cow),horse); says that human and cow form a clade and horse joins them. Branch lengths can be added after a colon, as in human:0.08. Tree viewers, papers' supplementary files and most tree-building programs all use this format.
Ways to build a tree
There are two broad families of method. This lesson uses the first; the second is described so that you recognise it in papers.
- Distance methods first turn the alignment into a table of distances, one number for each pair of sequences, and then build the tree from the table. They are fast. UPGMA repeatedly joins the closest pair and assumes every lineage changes at the same rate, a so-called molecular clock; this gives a rooted tree with all tips the same distance from the root. Neighbour joining (NJ) does not assume a clock, so lineages can change at different rates; it gives an unrooted tree.
- Character methods use every column of the alignment directly. Maximum parsimony looks for the tree that needs the fewest changes. Maximum likelihood and Bayesian methods use a statistical model of how sequences change and look for the tree that best explains the data. They are slower but generally more accurate, and are what most published trees use, with tools such as IQ-TREE, RAxML-NG and MrBayes.
Two points apply to distance methods in particular. First, a simple distance such as the share of differing positions underestimates the true amount of change once sequences are far apart, because a position can change more than once, or change and change back, and the alignment shows only one difference. Corrected distances, such as the Jukes–Cantor correction for DNA, estimate the hidden changes and stretch long distances accordingly. Second, UPGMA's clock assumption is often untrue. When one lineage changes much faster than the others, as mouse beta globin does in this lesson, UPGMA can join the wrong pairs, while neighbour joining copes better. For this reason neighbour joining is the usual choice among distance methods.
The model-based methods make similar choices explicit. A substitution model describes how often each kind of change happens, in the same spirit as the BLOSUM62 table of Scoring matrices: PAM and BLOSUM. For proteins you will see names such as JTT, WAG and LG in methods sections; programs such as IQ-TREE can test several and pick the one that fits the alignment best.
Go deeper: why tree methods cannot try every tree
The number of possible trees grows astonishingly fast. For n sequences, the number of different unrooted trees is 3 × 5 × 7 × … up to (2n − 5). Five sequences give just 15 trees, so a computer can score them all. Ten sequences give 2,027,025. Twenty give about 2 × 10 to the power 20, far more than could ever be checked one by one.
This is why maximum likelihood and parsimony programs search rather than check everything. They build a reasonable starting tree, often by neighbour joining, then try rearranging branches and keep any change that scores better. Like progressive alignment, the search can settle on a good tree that is not the best one, which is why careful studies repeat it from several starting trees.
Rooting, reading and support
Most methods produce an unrooted tree: it shows who is next to whom, but not which way time runs. To give it a direction you root it. The usual way is an outgroup: a sequence known from other evidence to have branched off before all the others. The root goes on the outgroup's branch. Here chicken is the outgroup, because birds split from mammals long before the mammals split from each other. Without a known outgroup, midpoint rooting puts the root halfway along the longest path between two tips, which is reasonable only if rates are roughly even.
Choosing an outgroup is a balance. It must be truly outside the group you study, or the root lands in the wrong place. But an outgroup that is very distant has a long branch and shares little with the others, so where it attaches can be uncertain, and it can be drawn towards other fast-changing sequences. A close but clearly separate relative, or two or three outgroups together, gives a steadier root.
To read a tree, follow the branches, not the order of the tips. Two tips are close relatives if they share a recent common ancestor, a node not far back. You can swing any node round, swapping the two groups below it, without changing the tree at all; tips that sit side by side on the page are not necessarily close relatives.
How sure can you be of a clade? The standard check is the bootstrap. The program builds many new alignments by sampling columns from the real one at random, with replacement, so some columns appear twice and others not at all. It builds a tree from each, and counts how often each clade appears. A clade found in 95 of 100 trees has bootstrap support of 95%. Values of about 70% or more are often treated as reasonable support; values near 50% mean the data barely favour that group over others.
How to read the figure
Time runs from the root on the left to the tips on the right. Only horizontal distance counts: the scale bar shows a length of 0.05, meaning about 5 differences per 100 positions. Human and cow meet at the node marked with a dot, so in this tree they are each other's closest relatives; the 58 above that node says the grouping appeared in 58 of 100 bootstrap trees. Horse joins that pair with support 70. Chicken, the outgroup in vermilion, branches off at the root. Notice that mouse has the longest branch among the mammals: its beta globin has changed the most.
Neighbour joining was published by Saitou and Nei (1987), 'The neighbor-joining method: a new method for reconstructing phylogenetic trees', Molecular Biology and Evolution 4:406–425. The bootstrap for trees comes from Felsenstein (1985), 'Confidence limits on phylogenies: an approach using the bootstrap', Evolution 39:783–791. A bootstrap value is not the probability that a clade is true. It measures how consistently the data support the clade under the chosen method; Hillis and Bull (1993, Systematic Biology 42:182–192) found it tends to be cautious when support is high. It cannot reveal errors a method makes every time, such as long-branch attraction, where fast-changing lineages are wrongly drawn together.
Building trees in Python
This lesson needs Biopython and an internet connection, both available in Colab. The first block downloads the five beta-globin proteins from Multiple alignment and conserved regions from UniProt, this time the whole chains. Pairwise alignments show that, after a few residues at the very start, the five align with no gaps, so cutting those first residues lines them up.
import urllib.request
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Align import MultipleSeqAlignment
# UniProt accession, and how many residues to skip so all start at the same Leu/Trp
proteins = {"human": ("P68871", 3), "mouse": ("P02088", 3), "cow": ("P02070", 1),
"horse": ("P02062", 2), "chicken": ("P02112", 3)}
records = []
for name, (acc, skip) in proteins.items():
url = f"https://rest.uniprot.org/uniprotkb/{acc}.fasta"
fasta = urllib.request.urlopen(url).read().decode()
seq = "".join(fasta.splitlines()[1:]) # drop the header line
records.append(SeqRecord(Seq(seq[skip:]), id=name))
aln = MultipleSeqAlignment(records)
print(len(aln), "sequences,", aln.get_alignment_length(), "columns")What this does
proteinslists each species with its UniProt accession and how many starting residues to skip. The chains differ in their first few residues: cow lacks two of them, and the horse entry starts without the first methionine. Skipping these lines everyone up at residue 4 of the human protein.urllib.request.urlopen(url)downloads the FASTA text.splitlines()[1:]drops the header line and"".join(...)glues the sequence lines together.SeqRecordwraps each sequence with a name, andMultipleSeqAlignmentstacks them into an alignment. It refuses sequences of different lengths, which is a useful check.
Reading the output
Five sequences of 144 columns each. All five came out the same length after trimming, which confirms the line-up.
Only these five chains could be lined up by trimming because they happen to have no internal gaps relative to each other. In general you would run them through a multiple alignment tool first, as in Multiple alignment and conserved regions, and read the result with AlignIO. Trees are only as good as the alignment they come from: a misaligned region adds differences that never happened.
from Bio import Phylo
from Bio.Phylo.TreeConstruction import DistanceCalculator, DistanceTreeConstructor
dm = DistanceCalculator("identity").get_distance(aln)
print(dm)
builder = DistanceTreeConstructor()
upgma = builder.upgma(dm)
nj = builder.nj(dm)
nj.root_with_outgroup("chicken") # chicken is the outgroup
a, b = nj.root.clades # the two branches from the root
a.branch_length = b.branch_length = (a.branch_length + b.branch_length) / 2
Phylo.draw_ascii(upgma)
Phylo.draw_ascii(nj)What this does
DistanceCalculator("identity")sets the distance to the share of columns where two sequences differ. Other choices, such as"blosum62", weigh similar amino acids as less different.get_distance(aln)returns the distance table;builder.upgmaandbuilder.njbuild one tree each from it.root_with_outgroup("chicken")roots the NJ tree on chicken's branch. Biopython puts the root right next to chicken, so the next two lines share that branch equally between the two sides. This only makes the drawing clearer; the distances between tips are unchanged.Phylo.draw_asciidraws a tree with text characters.
Reading the output
The table is triangular, because the distance from human to mouse equals the distance from mouse to human; the zeros are each sequence against itself. Human and cow are the closest pair, at 0.153. Chicken is far from everything, at 0.31 to 0.36. The first drawing is UPGMA, the second NJ. Both have the same topology: chicken first, then mouse, then horse, and human with cow. In the UPGMA tree all tips line up at the right, as a clock assumes; in the NJ tree branch lengths differ, and mouse's is visibly the longest.
Now compare with what is known about these animals. Humans are more closely related to mice than to cows or horses, and cows and horses are closer to each other. This gene tree says otherwise. A short protein, 144 positions, holds few informative differences, and the mouse lineage has changed faster, which pulls it outwards. The bootstrap shows how weak the evidence is.
import random
from Bio.Phylo.Consensus import bootstrap_trees
random.seed(1) # same resamples every run
nj_builder = DistanceTreeConstructor(DistanceCalculator("identity"), "nj")
trees = list(bootstrap_trees(aln, 100, nj_builder)) # 100 resampled alignments
def grouped(tree, *names):
"""True if these names form a group of their own in this tree."""
tree.root_with_outgroup("chicken")
return tree.common_ancestor(*names).count_terminals() == len(names)
for group in [("human", "cow"), ("human", "cow", "horse"),
("cow", "horse"), ("human", "mouse")]:
print(" + ".join(group), ":", sum(grouped(t, *group) for t in trees), "of 100")What this does
random.seed(1)fixes the random sampling so you get the same numbers.bootstrap_trees(aln, 100, nj_builder)makes 100 resampled alignments, each with 144 columns drawn at random with replacement, and builds an NJ tree from each.groupedroots a tree on chicken and checks whether the named sequences, and nothing else, descend from their common ancestor.- The loop counts, for four possible groups, how many of the 100 trees contain them.
Reading the output
Human with cow appears in 58 of 100 trees: barely more than half. Human, cow and horse together appear in 70. Cow with horse, the grouping the species tree expects, appears in 34, and human with mouse in only 13. None of these groupings is strongly supported, so the honest summary is that this protein alone cannot resolve how these four mammals are related. A longer alignment, many genes, or a model-based method would be needed.
To look at a tree in a dedicated viewer, save it in Newick format with Phylo.write(nj, "beta.nwk", "newick") and open the file in a free viewer such as FigTree or iTOL. Viewers let you show support values when a tree file carries them, swing nodes round and colour clades, which is easier than reading text drawings once trees grow beyond a handful of tips.
Worked example · the first UPGMA join
smallest distance in the table → join human and cow first
each branch = 0.153 ÷ 2 = 0.076
Step by step. The identity distance is the number of columns where two sequences differ, divided by the number of columns. For human and cow it is 0.152778, and 0.152778 × 144 = 22, so the two proteins differ at 22 of 144 positions. Scan the table: no other pair is closer (human with horse, 0.160, is next). UPGMA therefore joins human and cow first. Because it assumes a clock, it places their common ancestor halfway between them, so each branch is 0.153 ÷ 2 = 0.076. The pair is then treated as one unit, its distance to each other sequence is the average of the two, and the process repeats.
What the answer means. The whole UPGMA tree follows from the table by repeating that one step. Its first decision, human with cow, rests on a gap of only one difference between 22 and the human–horse count of 23. That is why the bootstrap support is so low: resampling the columns easily tips the balance.
Key point
Read a tree by its branching, not by the order of the tips. Root it with an outgroup, and check the bootstrap before you believe a clade. A gene tree from one short sequence can disagree with the species tree; low support tells you when that is likely.
For a quick look, a distance tree like this one is fine. For anything you publish, use a maximum likelihood method with a model of sequence change and at least several hundred bootstrap replicates.
Further reading · Module 4Biopython tutorial: phylogenetics with Bio.Phylo. · Bio.Phylo wiki page: reading, drawing and editing trees. · All reading for this course
Check your understanding
1. Two tips are drawn next to each other on a tree. What does that tell you on its own?
2. A clade has bootstrap support of 34%. What does that mean?
3. Why was chicken used to root the tree of mammal beta globins?
Up next
Lesson 8
Variants: what a change in sequence does, and how it is annotated