Skip to content

Commit

Permalink
Adding a format_branchlength parameter to NewickIO writers.
Browse files Browse the repository at this point in the history
(cherry picked from commit 533c5b0)
  • Loading branch information
habnabit authored and etal committed May 27, 2011
1 parent 183339a commit cc48ad2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
23 changes: 15 additions & 8 deletions Bio/Phylo/NewickIO.py
Expand Up @@ -171,13 +171,14 @@ def write(self, handle, **kwargs):
def to_strings(self, support_as_branchlengths=False,
branchlengths_only=False, plain=False,
plain_newick=True, ladderize=None,
max_support=1.0):
max_support=1.0, format_branchlength=None):
"""Return an iterable of PAUP-compatible tree lines."""
# If there's a conflict in the arguments, we override plain=True
if support_as_branchlengths or branchlengths_only:
plain = False
make_info_string = self._info_factory(plain, support_as_branchlengths,
branchlengths_only, max_support)
branchlengths_only, max_support,
format_branchlength)
def newickize(clade):
"""Convert a node tree to a Newick tree string, recursively."""
if clade.is_terminal(): #terminal
Expand Down Expand Up @@ -207,8 +208,14 @@ def newickize(clade):
yield ' '.join(treeline)

def _info_factory(self, plain, support_as_branchlengths,
branchlengths_only, max_support):
branchlengths_only, max_support, format_branchlength):
"""Return a function that creates a nicely formatted node tag."""
if format_branchlength is None:
def fmt_bl(bl):
return '%1.5f' % (bl,)
else:
fmt_bl = format_branchlength

if plain:
# Plain tree only. That's easy.
def make_info_string(clade, terminal=False):
Expand All @@ -226,23 +233,23 @@ def make_info_string(clade, terminal=False):
elif branchlengths_only:
# write only branchlengths, ignore support
def make_info_string(clade, terminal=False):
return ':%1.5f' % (clade.branch_length)
return ':%s' % (fmt_bl(clade.branch_length),)

else:
# write support and branchlengths (e.g. .con tree of mrbayes)
def make_info_string(clade, terminal=False):
if terminal:
return ':%1.5f' % (clade.branch_length or 1.0)
return ':%s' % (fmt_bl(clade.branch_length or 1.0),)
else:
if (clade.branch_length is not None and
hasattr(clade, 'confidence') and
clade.confidence is not None):
# we have blen and suppport
return '%1.2f:%1.5f' % (clade.confidence,
clade.branch_length)
return '%1.2f:%s' % (clade.confidence,
fmt_bl(clade.branch_length))
elif clade.branch_length is not None:
# we have only blen
return '0.00000:%1.5f' % clade.branch_length
return '0.00000:%s' % (fmt_bl(clade.branch_length),)
elif (hasattr(clade, 'confidence') and
clade.confidence is not None):
# we have only support
Expand Down
9 changes: 8 additions & 1 deletion Tests/test_Phylo.py
Expand Up @@ -10,7 +10,7 @@
from cStringIO import StringIO

from Bio import Phylo
from Bio.Phylo import PhyloXML
from Bio.Phylo import PhyloXML, NewickIO

#TODO - Remove this hack
#This will raise MissingPythonDependencyError if we don't have ElementTree
Expand Down Expand Up @@ -44,6 +44,13 @@ def test_newick_read_multiple(self):
for tree in trees:
self.assertEqual(len(tree.get_terminals()), 9)

def test_formatting_branch_length(self):
tree = Phylo.read(StringIO('A:0.1;'), 'newick')
mem_file = StringIO()
Phylo.write(tree, mem_file, 'newick',
format_branchlength=lambda bl: '%.0e' % (bl,))
self.assertEqual(mem_file.getvalue().strip(), 'A:1e-01;')

def test_convert(self):
"""Convert a tree between all supported formats."""
mem_file_1 = StringIO()
Expand Down

0 comments on commit cc48ad2

Please sign in to comment.