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

Peter biopython at maubp.freeserve.co.uk
Wed Apr 7 07:36:20 UTC 2010


Hi Eric,

Following discussion on Bug 3046 and 3047, I wrote the following
example using the current API to try to set all the branches to red,
except the branches of the terminal nodes which I set to blue:

from Bio import Phylo
tree = Phylo.read("apaf.xml", "phyloxml")
#This implicitly applies to all the children:
tree.properties.append(Phylo.PhyloXML.BranchColor(255,0,0))
#Now set the terminal nodes to blue:
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?:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/pjcock/repositories/biopython/build/lib.macosx-10.6-universal-2.6/Bio/Phylo/_io.py",
line 82, in write
    n = getattr(supported_formats[format], 'write')(trees, file, **kwargs)
  File "/Library/Python/2.6/site-packages/Bio/Phylo/PhyloXMLIO.py",
line 142, in write
    return Writer(obj).write(file, encoding=encoding, indent=indent)
  File "/Library/Python/2.6/site-packages/Bio/Phylo/PhyloXMLIO.py",
line 684, in __init__
    self._tree = ElementTree.ElementTree(self.phyloxml(phyloxml))
  File "/Library/Python/2.6/site-packages/Bio/Phylo/PhyloXMLIO.py",
line 705, in phyloxml
    elem.append(self.phylogeny(tree))
  File "/Library/Python/2.6/site-packages/Bio/Phylo/PhyloXMLIO.py",
line 661, in wrapped
    elem.append(getattr(self, method)(item))
  File "/Library/Python/2.6/site-packages/Bio/Phylo/PhyloXMLIO.py",
line 651, in wrapped
    elem = ElementTree.Element(tag, _clean_attrib(obj, attribs))
  File "/Library/Python/2.6/site-packages/Bio/Phylo/PhyloXMLIO.py",
line 643, in _clean_attrib
    val = getattr(obj, key)
AttributeError: 'BranchColor' object has no attribute 'ref'

Thanks,

Peter



More information about the Biopython-dev mailing list