Question: Problem 3 : Drawing better trees Place your code for this problem in a file called DrawTrees 2 . py . The drawPhyloTree you have

Problem 3: Drawing better trees
Place your code for this problem in a file called DrawTrees2.py.
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.
drawPhyloTree2(Tree, scale)
We will now write a function drawPhyloTree2(Tree, 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 =30
CORRECTION =1.155 # corrects for fact turtle is going at 30 deg angle
drawPhyloTree2 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 11). Here's an example:
myTree =(5,
(3,
("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 3 is drawn at a horizontal distance of 3 units from the leaves. And, the root node with a height of 5 is drawn 5 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 5, whereas the leaf "C" has a height of 0. This says that the segment from the root to the leaf labeled C should traverse 5 horizontal units. But the turtle itself is moving at an angle of 30 degrees. To find how far the turtle must go at this heading to move 5 horizontal units, we multiply by a correction factor (that variable we told you to paste in at the top):
>>> CORRECTION
1.155
>>>5*CORRECTION
5.775
This procedure gives you the right distance to travel assuming you're going at a 30-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 3. Because the root is at 5, we need to draw a segment leading to this subtree, which has a horizontal distance of 5-3=2 units. To get the value the turtle must travel, we again multiply by the correction factor.
>>>2*CORRECTION
2.31
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 drawPhyloTree2 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[0] is the thing you want to write, you can write it with the command turtle.write(str(Tree[0])). Finally, you can use different fonts and font sizes when writing. For example:
turtle.write(Tree[0], font=("Arial",12, "normal"))
This writes the root of the given Tree (that is, Tree[0]) in Arial 12-point font.
Here's an example of the output of drawPhyloTree2 on our test dataset.
>>> drawPhyloTree2(myTree,20)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!