Skip to content

Instantly share code, notes, and snippets.

@jhcepas
Last active August 29, 2015 14:23
Show Gist options
  • Save jhcepas/fd76041149a92ac9ba40 to your computer and use it in GitHub Desktop.
Save jhcepas/fd76041149a92ac9ba40 to your computer and use it in GitHub Desktop.
Converts a tree in newick format into a FastTree-compatible constraint alignment. It allows for multifurcated nodes.
#!/usr/bin/env python
import sys
from collections import defaultdict
from ete2 import Tree
try:
t = Tree(sys.argv[1])
except IndexError:
print >>sys.stderr, 'you need to provide a newick tree file as first argument\n\n'
print >>sys.stderr, 'Usage: Newick2FastTreeConstraints.py tree.nw > constraints.fa'
sys.exit(1)
n2content = t.get_cached_content()
all_leaves = sorted(n2content[t])
alg = defaultdict(list)
for n in t.traverse("postorder"):
if len(n.children) > 1:
ones = n2content[n]
for leaf in all_leaves:
if leaf in ones:
alg[leaf].append("1")
else:
alg[leaf].append("0")
print >>sys.stderr, "Number of leafs:", len(alg)
print >>sys.stderr, "Number of constraints:", set([len(v) for v in alg.values()])
for name, values in alg.iteritems():
print ">%s\n%s" %(name.name, ''.join(values))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment