[Biopython-dev] Setting branch colors in Bio.Phylo

Peter biopython at maubp.freeserve.co.uk
Wed Apr 7 13:30:12 UTC 2010


On Wed, Apr 7, 2010 at 1:57 PM, Eric Talevich <eric.talevich at gmail.com> wrote:
> On Wed, Apr 7, 2010 at 3:36 AM, Peter <biopython at maubp.freeserve.co.uk> wrote:
>> Hi Eric,
>>
>> Following discussion on Bug 3046 and 3047, I wrote the following
>> example using the current API to try to set all the branches to red,
>> except the branches of the terminal nodes which I set to blue:
>
> My aproach would be:
>
> from Bio import Phylo
> from Bio.Phylo import PhyloXML as PX
>
> tree = Phylo.read("apaf.xml", "phyloxml")
> for clade in tree.find_clades():
>    if clade.is_terminal():
>        clade.color = PX.BranchColor.from_name('blue')
>    else:
>        clade.color = PX.BranchColor.from_name('red')

Very helpful.

> Strictly according the phyloXML spec, with colors cascading down
> branches, this should display the same way (but doesn't in
> Phylo.draw_graphviz):

So for Bug 3047 you'll check fix Phylo.draw_graphviz to do that
(assuming the Archaeopteryx tool also does this)?

> for child in tree.root.clades:
>    child.color = PX.BranchColor.from_name('red')
> for term in tree.get_terminals()
>    child.color = PX.BranchColor.from_name('blue')

I'm assuming you made a typo with the variable names (term vs child).

Why not just apply the red to the root node itself? This seems to
work:

from Bio import Phylo
#This implicitly applies to all the children:
tree.root.color = Phylo.PhyloXML.BranchColor(255,0,0)
#Now set the terminal nodes to blue:
for clade in tree.find_clades(terminal=True):
   clade.color = Phylo.PhyloXML.BranchColor(0,0,255)
Phylo.write(tree, "colored.xml", "phyloxml")

This is based on my original example but now using find_clades
which is more specific than find_all as I now see, and also if
hadn't appreciated the difference between tree and tree.root
(a Tree object and a Clade object - in other libraries a tree
is also a clade).

As an aside, I don't like the find method - it seems dangerous
is the case where find_all returns multiple hits. I can see it
could be useful *if* it returns a single hit, None for no hits, or
an exception for multiple hits.

> I haven't confirmed that Archaeopteryx follows the spec here, but
> that's how the GUI behaves when colorizing branches, so I assume it
> does.
>
>
>> from Bio import Phylo
>> tree = Phylo.read("apaf.xml", "phyloxml")
>> #This implicitly applies to all the children:
>> tree.properties.append(Phylo.PhyloXML.BranchColor(255,0,0))
>> #Now set the terminal nodes to blue:
>> for node in tree.find(terminal=True):
>>    node.properties.append(Phylo.PhyloXML.BranchColor(0,0,255))
>> Phylo.write(tree, "colored.xml", "phyloxml")
>>
>> It fails in the call to write - what am I doing wrong?:
>
> The clade.properties attribute isn't a container for Python
> properties, it's a phyloXML-specific thing:
> http://www.phyloxml.org/documentation/version_1.10/phyloxml.xsd.html#h158033242

But it is still a list of objects, so I would expect to be able to add
(suitable)
things to it. If you regard this as an implementation detail, then maybe
rename the list to _properties instead?

Regards,

Peter




More information about the Biopython-dev mailing list