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

Eric Talevich eric.talevich at gmail.com
Wed Apr 7 15:55:01 UTC 2010


On Wed, Apr 7, 2010 at 9:30 AM, Peter <biopython at maubp.freeserve.co.uk> wrote:
> 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")

You're right, that's better.

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

The object hierarchy is a fairly literal translation of the PhyloXML
spec. That's why I used TreeMixin to plaster over the differences --
most of the methods that operate on a whole tree or subclade make
sense either way, and we can still separate global from local
information somewhat.


> 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 use find_all and find_clades (mostly find_clades) in loops, and find
in if statements.

I'm open to renaming any of the TreeMixin methods -- e.g.

  find_all --> find_elements
  find --> find_any, or get_any, or just any
  find_clades --> find_all, or find, or stay the same

Would those names be more intuitive?


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

You can append to it, but the thing you append needs to be a
PhyloXML.Property object, or else the serializer harfs when it can't
find the expected attributes.

Mitigation: Since the phyloXML spec requires some attributes in
Property instances, and the serializer assumes they'll be satisfied,
Property.__init__ should do some additional checks too and fail early
if necessary. Adding type checks all over Bio.Phylo seems un-Pythonic,
but checking attribute existence should be easy.




More information about the Biopython-dev mailing list