[Biopython] Adding phyloxml colors to newick trees
Eric Talevich
eric.talevich at gmail.com
Wed Apr 20 01:56:21 UTC 2011
Hi Aaron,
On Tue, Apr 19, 2011 at 6:20 PM, Aaron Gallagher <agallagh at fhcrc.org> wrote:
> So, I'm trying to add colors to a phylogenetic tree when writing it out in
> phyloxml format. It's being originally loaded from Newick format, which
> seems
> to be causing problems. This doesn't raise any errors, but the output
> doesn't
> contain any coloring:
>
> tree = Phylo.read(..., 'newick')
> tree.root.color = BranchColor(255, 0, 0)
> Phylo.write(tree, ..., 'phyloxml')
>
Convert it to a PhyloXML tree first:
>>> tree2 = tree.as_phyloxml()
Details:
http://biopython.org/DIST/docs/tutorial/Tutorial.html#htoc164
The basic tree object has only the attributes that makes sense for a Newick
or Nexus tree, but since Python is a flexible language, it lets you add new
attributes to an object on the fly. The PhyloXML tree object is subclassed
from the basic tree, and supports more attributes including BranchColor.
Also, if you'd like to avoid another import, there are shortcuts for adding
colors to a tree:
>>> tree2.root.color = 'red'
>>> tree2.root.color = '#FF0000'
>>> tree2.root.color = (255, 0, 0)
> This, however, does include coloring in the output:
>
> sio = StringIO()
> Phylo.convert(..., 'newick', sio, 'phyloxml')
> sio.seek(0)
> tree = Phylo.read(sio, 'phyloxml')
> tree.root.color = BranchColor(255, 0, 0)
> Phylo.write(tree, ..., 'phyloxml')
>
> Is there any way to do this conversion on-the-fly with an already-loaded
> tree
> instead of having to convert it via a StringIO (or temporary file) ?
>
>
The .as_phyloxml() method is easiest.
I suppose I could make it friendlier by (a) issuing a warning when assigning
to the .color or .width attributes on the basic tree object, or (b) doing
more magic when writing a tree to phyloXML format. There are a lot of
phyloXML-specific attributes, though, and I wouldn't want to add special
checks for all of them.
Cheers,
Eric
More information about the Biopython
mailing list