How to remove zero-length edges in newick tree
1
0
Entering edit mode
3.8 years ago
PeterWu ▴ 20

I want to know whether there is quick way to remove zero-length edges in newick tree, like

input: ((a:1,b:1)Node_0:0,c:1);

output:(a:1,b:1,c:1);

Thanks for any help

phylogenetics algorithm • 1.8k views
ADD COMMENT
0
Entering edit mode

the input/output is a string?

ADD REPLY
0
Entering edit mode

yes, newick is a format to record a rooted tree

ADD REPLY
0
Entering edit mode
3.8 years ago

Probably there is a slicker way... but this seems to work.

#!/bin/env python3

import ete3 as ete

input = "((a:1,b:1)z:0,c:1);"

# http://etetoolkit.org/docs/latest/tutorial/tutorial_trees.html#reading-and-writing-newick-trees
tree = ete.Tree(newick=input, format=1)

print("Before:", tree.write(format=1))

for node in list(tree.search_nodes(dist=0)):
    parent: ete.Tree
    parent = node.up
    if parent:
        for c in list(parent.get_children()):
            c.detach()
            if (c == node):
                for d in c.get_children():
                    parent.add_child(d)
            else:
                parent.add_child(c)

output = tree.write(format=1)
print("After: ", output)
ADD COMMENT
0
Entering edit mode

Thanks very much! That works for me.

ADD REPLY

Login before adding your answer.

Traffic: 1945 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6