In this chapter, we will see how biologists build evolutionary trees from genetic data. Start with the core text, which introduces distance matrices, the UPGMA algorithm, and recursion. Then join us for the Python code alongs, where we will implement UPGMA with object-oriented programming and run it on real biological datasets. A recorded lecture and a Go code along accompany the chapter below.
Core Text
Python Code Alongs
- Methods on Shapes in Python: Give our shape classes their own methods, so that every shape can compute its area and translate itself, and see why calling a method can change an object.
- Everything Is an Object in Python: Use
id()to see that Python passes every value by object reference, and learn why integers and lists still seem to behave differently. - Designing Data for Trees in Python: Weigh two ways of representing an evolutionary tree, and build a recursive
Nodeclass whose children may beNone. - How UPGMA Works (optional): Walk through UPGMA by hand on small distance matrices. If you are comfortable with UPGMA from the core text, feel free to skip ahead.
- Planning UPGMA Top-Down in Python: Download the starter code, decide what data UPGMA needs to track, and write the highest-level
upgma()function. - Validating Input, Initializing the Tree, and What Slices Copy: Check the input distance matrix, build the leaves of the tree, and learn what a slice copies when a list holds mutable objects.
- Shrinking the Matrix and Clusters in Python: Find the minimum element of the distance matrix, add a weighted row and column for each new cluster, and delete the rows, columns, and clusters that we no longer need.
- An Introduction to Recursion in Python: Write recursive functions for sums, factorials, and Fibonacci numbers, and see why naive recursive Fibonacci is so slow.
- Counting Leaves Recursively in Python: Apply recursion to trees with a
count_leaves()method, and test it on a small sample tree. - Newick Format in Python: Convert a tree into a Newick format string with a short recursive method, so that other software can draw our trees.
- Running UPGMA on Real Data in Python: Finish a command-line program that runs UPGMA on eight real biological datasets and writes each tree to file.
- Touring Our Evolutionary Trees: Visualize our trees in R, and take a tour of what they tell us about great apes, pandas, hemoglobin, HIV, SARS-CoV-2, human mitochondria, and whales.
Lecture
Go Code-Along: Methods, Pointers, and Implementing UPGMA
Practice Exercises
When you have finished the code alongs, test your skills with this chapter’s practice exercises on the Barnes-Hut algorithm.