Skip to content

Bio.Phylo.write Newick default precision (%1.5f) causes silent data loss for small branch lengths #5184

@Wytamma

Description

@Wytamma

Hi! Thanks so much for Biopython, I use it in a lot of projects :) I just ran into a weird bug(?) for trees with small branches. By reading and writing a newick file it changes dramatically.

This can be resolved by setting format_branch_length to higher values e.g. `Phylo.write(tree, "output.nwk", "newick", format_branch_length="%1.10f"). However, it would be nice to default to a higher value or raise a warning before rounding down? This is an issue because sometimes we use biopython to open -> modify -> write trees and it seems like branch lengths are lost without warning.

See example below:

from Bio import Phylo

tree = Phylo.read("input.newick", "newick")
Phylo.draw_ascii(tree)
  _______ 10
 |
 |        , 01
 |        |
 |        | 08
 |        |
 |        | 06
 |        |
 |        | 04
 |        |
_|       ,| 09
 |       ||
 |     __|| 02
 |    |  |
 |    |  |   __________________________________________________________ 05
 |____|  |__|
 |    |     |___________ 13
 |    |
 |    | 03
 |
 |__ 07
 |
 | 11
Phylo.write(tree, "output.nwk", "newick")

tree = Phylo.read("output.nwk", "newick")
Phylo.draw_ascii(tree)
  _______ 10
 |
 |                                                             ________ 01
 |                                                    ________|
 |                                           ________|        |________ 08
 |                                          |        |
 |                                   _______|        |________ 06
 |                                  |       |
 |                          ________|       |________ 04
 |                         |        |
_|                 ________|        |_______ 09
 |                |        |
 |        ________|        |________ 02
 |       |        |
 |       |        |         ________ 05
 |_______|        |________|
 |       |                 |________ 13
 |       |
 |       |________ 03
 |
 |        ________ 07
 |_______|
         |________ 11

input.nwk

(10:0.0000004302,(((((((01:0.0000000000,08:0.0000000000):0.0000000000,06:0.0000000000):0.0000000000,04:0.0000000000):0.0000000000,09:0.0000000000):0.0000000143,02:0.0000000143)35:0.0000000143,(05:0.0000028682,13:0.0000005736)78:0.0000001434)65:0.0000001434,03:0.0000000143)80:0.0000002868,(07:0.0000001434,11:0.0000000143)80:0.0000000143);

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions