[Biopython-dev] PhyloXML.BranchColor methods

Peter biopython at maubp.freeserve.co.uk
Wed Apr 7 21:31:42 UTC 2010


On Wed, Apr 7, 2010 at 10:06 PM, Eric Talevich <eric.talevich at gmail.com> wrote:
> On Wed, Apr 7, 2010 at 4:29 PM, Peter <biopython at maubp.freeserve.co.uk>wrote:
>>
>> 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).
>>
>
> It's pretty magical, but the convenience of "critters.color = 'green'" wins.
> I'll implement the property to accept a BranchColor, RGB triple, color name,
> or hex string, and raise a ValueError otherwise.

Yeah - it feels right to me ;)

>>  > 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.
>>
>
> My only concern: the builtin hex() returns a string formatted a little
> differently. Matching that format would be useless here, but I was worried
> about people being confused. But if you're OK with to/from_hex, then I am
> too.

See below.

>  > 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.
>>
>
> Strawmen:
> - Should to_rgb be renamed to_tuple, then?

There are other things a color tuple could be, although RGB is the
most common (see also CMYK, HSV, ...).

> - if we defined BranchColor.__iter__ as "return (self.red, self.green,
> self.blue)", then "tuple(clade.color)" would work
> - if we defined BranchColor.__hex__, then similarly, "hex(clade.color)"
> would work
> - ... but those magic methods would hurt discoverability

Huh - that is neat, but it hadn't occurred to me to think about
supporting those. It isn't helpful to support hex(...) since that
usually returns a string which starts "0x..." which isn't what we
want for HTML, CSS or XML. Maybe instead of to_hex() we
should have to_css_color() or something like that?

Peter




More information about the Biopython-dev mailing list