Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tree.prune incorrectly sets the branch length #4460

Open
diljotgrewal opened this issue Oct 18, 2023 · 1 comment · May be fixed by #4601
Open

tree.prune incorrectly sets the branch length #4460

diljotgrewal opened this issue Oct 18, 2023 · 1 comment · May be fixed by #4601

Comments

@diljotgrewal
Copy link

Setup

I am reporting a problem with Biopython version, Python version, and operating
system as follows:

>>> import sys; print(sys.version)
3.7.16 (default, Jan 17 2023, 22:20:44)
[GCC 11.2.0]
>>> import platform; print(platform.python_implementation()); print(platform.platform())
CPython
Linux-3.10.0-1160.45.1.el7.x86_64-x86_64-with-centos-7.9.2009-Core
>>> import Bio; print(Bio.__version__)
1.81

Expected behaviour

running prune function on diploid node of a tree that looks like this

Tree(rooted=False, weight=1.0)
    Clade(branch_length=0.0)
        Clade(branch_length=27.0, name='internal_3')
            Clade(branch_length=6.0, name='internal_6')
                Clade(branch_length=2.0, name='internal_4')
                    Clade(branch_length=1.0, name='cluster_13')
                    Clade(branch_length=3.0, name='cluster_39')
                Clade(branch_length=1.0, name='internal_5')
                    Clade(branch_length=2.0, name='cluster_7')
                    Clade(branch_length=2.0, name='cluster_8')
            Clade(branch_length=3.0, name='internal_2')
                Clade(branch_length=5.0, name='cluster_77')
                Clade(branch_length=1.0, name='internal_1')
                    Clade(branch_length=3.0, name='cluster_18')
                    Clade(branch_length=3.0, name='cluster_25')
        Clade(branch_length=0.0, name='diploid')

should result in

Tree(rooted=False, weight=1.0)
    Clade(branch_length=27.0, name='internal_3')
        Clade(branch_length=6.0, name='internal_6')
            Clade(branch_length=2.0, name='internal_4')
                Clade(branch_length=1.0, name='cluster_13')
                Clade(branch_length=3.0, name='cluster_39')
            Clade(branch_length=1.0, name='internal_5')
                Clade(branch_length=2.0, name='cluster_7')
                Clade(branch_length=2.0, name='cluster_8')
        Clade(branch_length=3.0, name='internal_2')
            Clade(branch_length=5.0, name='cluster_77')
            Clade(branch_length=1.0, name='internal_1')
                Clade(branch_length=3.0, name='cluster_18')
                Clade(branch_length=3.0, name='cluster_25')

Actual behaviour

the pruned tree looks like this

Tree(rooted=False, weight=1.0)
    Clade(name='internal_3')
        Clade(branch_length=6.0, name='internal_6')
            Clade(branch_length=2.0, name='internal_4')
                Clade(branch_length=1.0, name='cluster_13')
                Clade(branch_length=3.0, name='cluster_39')
            Clade(branch_length=1.0, name='internal_5')
                Clade(branch_length=2.0, name='cluster_7')
                Clade(branch_length=2.0, name='cluster_8')
        Clade(branch_length=3.0, name='internal_2')
            Clade(branch_length=5.0, name='cluster_77')
            Clade(branch_length=1.0, name='internal_1')
                Clade(branch_length=3.0, name='cluster_18')
                Clade(branch_length=3.0, name='cluster_25')

The branch length for node internal_3 is set to None in the pruned tree

Steps to reproduce

the python code is as follows:

while tree.count_terminals() > 0:
    altered = False
    for a in tree.get_terminals():
        if a.name == 'diploid':
            tree.prune(a)
            altered = True
    if not altered:
        break
@michaelfm1211
Copy link
Contributor

michaelfm1211 commented Jan 23, 2024

I know I'm about 3 months late to this, but it looks like this behavior is intentional:

if parent == self.root:
# If we're at the root, move the root upwards
# NB: This loses the length of the original branch
newroot = parent.clades[0]
newroot.branch_length = None
parent = self.root = newroot

If this really is a problem, then maybe we can add a new keyword argument to the function to disable the newroot.branch_length = None part, however, I'm not sure if any other code in Bio.Phylo relies on the root having no length. Any thoughts on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants