Question: Problem 3 : Drawing better trees Place your code for this problem in a file called DrawTrees 2 . py . The drawPhyloTree you have
Problem : Drawing better trees
Place your code for this problem in a file called DrawTreespy
The drawPhyloTree you have already written is a start, but because it draws all branch segments to the same length, it produces trees that can be difficult to read.
drawPhyloTreeTree scale
We will now write a function drawPhyloTreeTree scale which draws segments of varying lengths. You should include import turtle at the top of this file, as well as the following two lines which we will discuss more below:
ANGLE
CORRECTION # corrects for fact turtle is going at deg angle
drawPhyloTree expects input trees whose leaves all have the same distance from the root, which happens to be the kind of tree produced by the UPGMA algorithm which is introduced in Chapter Here's an example:
myTree
A
B
C
Here the leaves have strings in the root position, which give the species name. However, instead of naming internal nodes, we have included another kind of information in the root position. Internal nodes have a "height," which tells us how far they are from the leaves. This "height" might be measured in years, or by units of sequence distance. To draw this tree properly, we need to make sure that the internal nodes are drawn according to this "height" that is the proper horizontal distance away from the leaves Here's our example:
In this figure, we have made sure that the internal node with height is drawn at a horizontal distance of units from the leaves. And, the root node with a height of is drawn units from the leaves.
Let's consider how to draw this now, imagining the turtle is starting at the root of the tree. There are two subtrees at the root. The left subtree contains the leaves A and B And the right subtree is itself a leaf, labeled C
We'll first draw the right subtree. The root has a height of whereas the leaf C has a height of This says that the segment from the root to the leaf labeled C should traverse horizontal units. But the turtle itself is moving at an angle of degrees. To find how far the turtle must go at this heading to move horizontal units, we multiply by a correction factor that variable we told you to paste in at the top:
CORRECTION
CORRECTION
This procedure gives you the right distance to travel assuming you're going at a degree angle. If you used another angle, you'd need to use trigonometry to calculate a different correction factor.
The other subtree in our example has a height of Because the root is at we need to draw a segment leading to this subtree, which has a horizontal distance of units. To get the value the turtle must travel, we again multiply by the correction factor.
CORRECTION
With a tree like our example tree, the values of the units are rather small. This will result in a ridiculously compact and unreadable tree. The scale parameter taken by drawPhyloTree allows us to multiply the heights by some amount for the sake of drawing to make the final diagram larger or smaller and thus easier to read
As we discussed in the last problem, if you wish to write things on your phylogenetic tree, you can use the turtle write command. Here we'll be writing the element found in the root position of a tree, which is sometimes a number and sometimes a string. This presents a problem because the write command expects a string. To deal with this, we can use the str function to convert the element found in this root position into a string str takes whatever it is given, whether it be a number or a string, and returns it as a string So if Tree is the thing you want to write, you can write it with the command turtle.writestrTree Finally, you can use different fonts and font sizes when writing. For example:
turtle.writeTree fontArial "normal"
This writes the root of the given Tree that is Tree in Arial point font.
Here's an example of the output of drawPhyloTree on our test dataset.
drawPhyloTreemyTree
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
