In this chapter’s code alongs, we will implement UPGMA, the evolutionary tree algorithm from the core text, and use it to build trees from real biological data. Along the way, we will give our classes methods, see that everything in Python is an object, design a recursive data structure for trees, and use recursion to count a tree’s leaves and write it in Newick format. The code alongs are listed below in order.
- 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.