Touring Our Evolutionary Trees

Video

Learning objectives

In this final lesson of the chapter, we will visualize the trees that UPGMA produced, and then we will take a tour of what they tell us. There is no new Python code in this lesson.

Code along summary

Setup: installing R and RStudio

To draw our trees, we will use R, a language with outstanding support for data visualization, along with the ape package for working with evolutionary trees. You will need to install three things.

  1. Install R from CRAN.
  2. Install RStudio Desktop from Posit.
  3. Open RStudio and run the following command in its console to install the two R packages that our script needs.
install.packages(c("ape", "rstudioapi"))

Visualizing our trees

In RStudio, open the file visualize_trees.R from your upgma directory. The script reads each .nwk file from Output and saves a picture of the corresponding tree, with leaves colored by group, as a .png file in Images. It uses rstudioapi to find the folder that it lives in, so it should be run from RStudio with the file open. Select all of the code and click Run. R will probably print some warning messages; this is normal and does not mean that anything is wrong.

We didn’t write this R script by hand. Typing out R plotting code is tedious, and these days, if you can clearly describe the visualization that you want (for example, “I have some Newick files of evolutionary trees, and I would like nice pictures of them with the leaves colored by group”), an AI assistant will write the R code for you, and it will revise the code when you ask for changes.

After running the script, your Images directory will contain the following files.

  • great_apes.png
  • giant_panda_bears.png
  • hemoglobin.png
  • hiv_subtypes.png
  • SARS_spike.png
  • mitochondrial_haplogroups.png
  • cetaceans.png
  • cetaceans_upgma_vs_nj.png

Let’s take a tour of these trees. The timestamps in each heading link to the corresponding part of the video.

Great apes (1:55:00)

UPGMA tree built from mitochondrial genomes of great apes and other primates, with a mouse and a zebra finch as outgroups
Figure: UPGMA tree for the great apes dataset.

Our first tree is built from the mitochondrial DNA of great apes and a few other species, including a mouse and a zebra finch. There may not be much selective pressure on mitochondrial DNA, and so differences accumulate through genetic drift at a roughly constant pace, like a molecular clock. As we discussed in this chapter’s main text, UPGMA assumes that the pace of evolution is the same on every branch of the tree, and so it works well on data for which this assumption is reasonable. The result is not earth-shattering: bonobos and chimpanzees are each other’s closest relatives, and their most recent common ancestor with humans lived earlier.

Giant pandas and bears (1:57:20)

UPGMA tree of bears, the giant panda, the red panda, a raccoon, and a ferret, with the giant panda among the bears
Figure: UPGMA tree for bears, pandas, a raccoon, and a ferret.

For a long time, biologists debated whether the giant panda belongs with the bears or with the red panda and the raccoon. Molecular data closed the case. On one side of our tree, we find the raccoon, the ferret, and the red panda; on the other side, the giant panda sits among the bears. Two animals that we call pandas belong on opposite sides of the tree.

Hemoglobin across animals (1:59:30)

UPGMA tree of hemoglobin subunit alpha in 108 animals, with leaves colored as mammals, birds, fish, reptiles, and amphibians
Figure: UPGMA tree for hemoglobin subunit alpha in 108 animals, colored by mammals, birds, fish, reptiles, and amphibians. (Click the image to view it at full size.)

Running UPGMA on hemoglobin from 108 animals shows the power of applying an algorithm at scale. (This dataset happens to be heavy on geese and swans.) For the most part, species cluster according to how we have long thought about taxonomy: the mammals cluster together, including chimpanzees, bonobos, and humans, and the fish appear least similar to everything else. A few birds cluster with a tortoise, which raises a question worth exploring: is this an artifact of our data or algorithm, or is something interesting going on? Hemoglobin carries oxygen in all of these animals, so mutations that preserve its function accumulate through drift, which is exactly the setting in which UPGMA performs well.

HIV (2:03:20)

UPGMA tree of HIV-1, HIV-2, and simian immunodeficiency viruses, with HIV-1 and HIV-2 on opposite sides of the tree
Figure: UPGMA tree for HIV-1, HIV-2, and simian immunodeficiency viruses.

It was once believed that HIV entered the human population through a single patient zero. Our tree tells a murkier story. HIV-1 and HIV-2 lie on completely different sides of the tree, and HIV-2 is far more similar to an SIV found in a mangabey than to anything on the other side of the tree. Some HIV-1 groups are also more similar to particular SIVs than to other HIV-1 groups. The only way for this to happen is if HIV jumped from other primates to humans at multiple points in time, which is part of why HIV has been so difficult to treat and vaccinate against.

SARS-CoV-2 in the UK (2:05:30)

UPGMA tree of SARS-CoV-2 spike proteins sampled in the UK from 2020 to 2024, with leaves colored by year
Figure: UPGMA tree for SARS-CoV-2 spike proteins sampled in the UK from 2020 to 2024, with leaves colored by year. (Click the image to view it at full size.)

Our next tree is built from spike proteins of viruses taken from UK patients over the course of the pandemic, with leaves colored by year. As we might expect, samples cluster by time. The samples from 2020 are very closely related to each other and occupy a small part of the tree, since the virus had not yet had much time to mutate, which made it easier to vaccinate against. Over time, the virus became more diverse. Yet we also see tight clusters, which likely correspond to variants: a virus with a particular collection of mutations was more fit than others and spread widely.

Human mitochondrial haplogroups (2:09:30)

UPGMA tree of human mitochondrial haplogroups, with leaves colored by region of the world
Figure: UPGMA tree for human mitochondrial haplogroups, colored by region. (Click the image to view it at full size.)

Mitochondrial DNA is inherited from your mother without paternal DNA mixed in, so unless a mutation occurs along the way, your mitochondrial DNA is essentially that of your mother’s mother’s mother. Mitochondrial DNA is therefore a record of human migration. In our small tree, individuals from Eurasia, East Asia and Oceania, and the Americas cluster together, but individuals of African ancestry appear across the entire tree. The genetic diversity within Africa is greater than the diversity in the rest of the world combined, which provided strong molecular evidence that all humans descend from ancestors in Africa, with the migration out of Africa occurring much more recently. Real studies use hundreds or thousands of individuals, but this small tree illustrates the idea.

Cetaceans and a word of caution (2:15:30)

UPGMA tree of whales, dolphins, and other mammals, placing the hippopotamus next to the cow and the giraffe
Figure: UPGMA tree for the cetaceans dataset.

Our last tree contains whales and dolphins along with hoofed mammals like the camel, the cow, and the giraffe, as well as the African elephant and rhinos. In the UPGMA tree, the hippopotamus is closest to the cow and the giraffe. The trouble is that this is not what biologists believe.

The UPGMA tree for the cetaceans dataset beside the neighbor-joining tree for the same matrix, which places the hippopotamus beside the whales
Figure: The UPGMA tree (left) beside the tree produced by neighbor-joining (right) on the same distance matrix.

The R script also produced one extra picture, which compares the UPGMA tree (left) with a tree produced on the same distance matrix by a different algorithm called neighbor-joining (right). In the neighbor-joining tree, the hippo sits alongside the whales and dolphins, which reflects the current consensus that the hippo is the closest living land relative of whales. UPGMA can reproduce a great deal of biology, but it is not the best tree-building algorithm, and in some cases it is outperformed by others.

Our tour raises plenty of questions. How were these distance matrices formed from DNA and protein sequences, and what makes a distance matrix good? Why does neighbor-joining succeed where UPGMA fails? These questions are the subject of computational biology, and you can explore them further in our Bioinformatics Algorithms book.

Time to practice!

After you have the chance to check your work below, you are ready to apply what you have learned to the chapter’s exercises.

Scroll to Top
Programming for Lovers banner no background
programming for lovers logo cropped

Join our community!