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

Eric Talevich eric.talevich at gmail.com
Wed Apr 7 12:57:29 UTC 2010


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')


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

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 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

Cheers,
Eric




More information about the Biopython-dev mailing list