Created
November 27, 2017 16:04
-
-
Save terrycojones/1655f864232136d8255a899e568032b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
from ete3 import Tree | |
import sys | |
import re | |
numeric_prefix = re.compile('^\d+_') | |
numeric = re.compile('^\d+$') | |
t = Tree(sys.argv[1], format=1) | |
for node in t.traverse(): | |
match = numeric_prefix.match(node.name) | |
if match: | |
# print('%s has a numeric prefix' % node.name, file=sys.stderr) | |
# print('%s -> %s' % (node.name, node.name[match.end():]), | |
# file=sys.stderr) | |
node.name = node.name[match.end():] | |
else: | |
match = numeric.match(node.name) | |
if match: | |
# print('%s is all digits' % node.name, file=sys.stderr) | |
node.name = 'ancestral-node-' + node.name | |
else: | |
raise ValueError( | |
"Tree node label '%s' has an unrecognized format." % node.name) | |
# Print the tree, including the name of the root. | |
print('%s%s;' % (t.write(format=1)[:-1], t.name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment