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.
- Install R from CRAN.
- Install RStudio Desktop from Posit.
- 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.pnggiant_panda_bears.pnghemoglobin.pnghiv_subtypes.pngSARS_spike.pngmitochondrial_haplogroups.pngcetaceans.pngcetaceans_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)

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)

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)

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)

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)

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)

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)

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 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.