[Biopython-dev] PhyloXML.BranchColor methods

Eric Talevich eric.talevich at gmail.com
Wed Apr 7 18:18:10 UTC 2010


Hi all,

There's been some discussion in Bugzilla about using the
PhyloXML.BranchColor class to assign colors to Bio.Phylo tree
branches.
http://bugzilla.open-bio.org/show_bug.cgi?id=3047

Currently, one sets the color for a clade by assigning a BranchColor
instance to the clade's color attribute:

from Bio import Phylo
from Bio.Phylo import PhyloXML as PX

tree = Phylo.read(..., 'phyloxml')
critters = tree.find(name='Rattus')
critters.color = PX.BranchColor(0, 128, 0)
# or, using HTML/matplotlib color names:
critters.color = PX.BranchColor.from_name('green')


The BranchColor class has these methods:

  from_name -- a class method that looks up RGB values in the
hard-coded dictionary BranchColor.color_names
  to_hex -- a 24-bit hex string, e.g. '#00A000', suitable for HTML/CSS
and matplotlib
  to_rgb -- a tuple of float RGB values, scaled 0 to 1.0

(See http://github.com/biopython/biopython/blob/master/Bio/Phylo/PhyloXML.py)


Here are some proposals. Please let me know which of these you like or hate.

1. Add a function Bio.Phylo.PhyloXML.color(...) which behaves like the
clade.color property Peter suggested earlier:

def color(thing):
    if isinstance(thing, BranchColor):
        return thing
    elif isinstance(thing, basestring):
        if thing in BranchColor.color_names:
            return BranchColor.from_name(thing)
        elif len(thing) == 7 and thing[0] == '#':
            # CSS/HTML/matplotlib-style hex string
            return BranchColor.from_hex_string(thing)
        raise ValueError("Fail!")
    elif hasattr(thing, '__iter__') and len(thing) == 3:
        # RGB triple -- an abstract base class would be nice here
        # (or, take *args instead of thing)
        return BranchColor(*thing)
    raise ValueError("Fail!")

Then the last line of the above example would be:
critters.color = PX.color('green')


2. Add a class method from_hex_string for constructing BranchColor
objects from a hex string like '#FF00AA'

This complements the to_hex function (to be renamed to_hex_string,
unless someone has a better name for it). The color function given
above assumes this method exists.


3. Drop the to_rgb method; it's confusing and floating-point
conversions lead to bugs.


4. New __repr__ and __str__ methods:

>>> critters.color
BranchColor(red=0, green=128, blue=0)
>>> print critters.color
(0, 128, 0)


I'm don't think any of the other PhyloXML classes warrant a similar
treatment -- except possibly PhyloXML.Sequence, which can be built
from at SeqRecord using the from_seqrecord class method. Any other
suggestions along these lines?

Thanks,
Eric



More information about the Biopython-dev mailing list