Skip to content

Instantly share code, notes, and snippets.

@terrycojones
Created November 27, 2017 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrycojones/1655f864232136d8255a899e568032b8 to your computer and use it in GitHub Desktop.
Save terrycojones/1655f864232136d8255a899e568032b8 to your computer and use it in GitHub Desktop.
#!/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