[Biopython-dev] PhyloXML.BranchColor methods

Peter biopython at maubp.freeserve.co.uk
Wed Apr 7 20:29:47 UTC 2010


On Wed, Apr 7, 2010 at 7:18 PM, Eric Talevich <eric.talevich at gmail.com> wrote:
> 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

We could probably move the lookup table under Bio.Data,
where it might also be useful for Bio.Graphics. I assume you
are using standard HTML/CSS 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:

I was suggesting adding a property to the clade (which could for
example map color names or RBG triples to the BranchColor
objects automatically). It would still be:

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)

BUT, you could choose to allow:

critters.color = (0, 128, 0)

Or a named color,

critters.color = "green"

Or a hex string.

critters.color = "#008000"

and have the property set method convert these into
the same result, BranchColor(0, 128, 0).

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

Hmm, to_hex seems OK to me.

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

I had assumed to_rgb would give a tuple of ints in the range
0 to 255 (following HTML/CSS color conventions). That would
avoid the rounding issue.

> 4. New __repr__ and __str__ methods:
>
>>>> critters.color
> BranchColor(red=0, green=128, blue=0)
>>>> print critters.color
> (0, 128, 0)

Personally I would like an HTML style output, hash then a six character
hex number. Anyone planning to look at the XML should know these
from HTML and CSS. However, I recognise this isn't universally
understood.

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

Colors are special enough to warrant special attention. Possibly also
width would to.

Peter




More information about the Biopython-dev mailing list