some weeks ago I sent a similar code example to a friend. It uses ETE to check for the monophyly of a given set of values in unrooted trees. You could easly customize the code to test for the monophyly of your two partitions, check if the are direct sister branches, etc.
A more complete version of the check_monophyly function is also in the current development version of ETE (i.e. to detect polyphyletic or paraphyletic relationships): https://github.com/jhcepas/ete/blob/master/ete_dev/coretype/tree.py#L1841
from ete2 import PhyloTree
def check_monophyly(t, values, attr):
''' Returns True if all attributes (attr) in a tree (t) are monophyletic for the
provided set of values. The most monophyletic partition is also returned. '''
content = t.get_cached_content()
alltips = content[t]
targets = set([n for n in alltips if getattr(n, attr) in values])
smallest = None
for n, leaves in content.iteritems():
if targets.issubset(leaves) and (not smallest or len(leaves) < len(smallest)):
smallest = leaves
else:
# if the node itself is not defining the monophyly, break on through
# to the other side... and check if the monophyly is there
other_side = alltips - leaves
if targets.issubset(other_side) and (not smallest or len(other_side) < len(smallest)):
smallest = other_side
return len(smallest) == len(targets), smallest
# Test examples
t = PhyloTree('(aaa1, (aaa3, (aaa4, (bbb1, bbb2))));')
print check_monophyly(t, values=set(['aaa']), attr='species')
t = PhyloTree('(aaa1, (bbb3, (aaa4, (bbb1, bbb2))));')
print check_monophyly(t, values=set(['aaa']), attr='species')
t = PhyloTree('(aaa1, (aaa3, (aaa4, (bbb1, bbb2))));')
print check_monophyly(t, values=set(['bbb']), attr='species')
t = PhyloTree('(aaa1, (aaa3, (aaa4, (bbb1, ccc2))));')
print check_monophyly(t, values=set(['bbb', 'ccc']), attr='species')
t = PhyloTree('(bbb1, (aaa3, (aaa4, (bbb1, bbb2))));')
print check_monophyly(t, values=set(['bbb', 'ccc']), attr='species')
t = PhyloTree('(aaa1, (aaa3, (bbb4, (bbb1, bbb2))));')
print check_monophyly(t, values=set(['bbb4', 'bbb2']), attr='name')
t = PhyloTree('(aaa1, (aaa3, (bbb4, (bbb1, bbb2))));')
print check_monophyly(t, values=set(['bbb1', 'bbb2']), attr='name')
Thanks for that - I've added a Perl script that probably does something very similar below. I'm interested how ETE detects monophyletic relationships in unrooted trees though? I thought that the definition of monophyly, paraphyly etc depends on knowing where the root of the phylogeny lies. Does it look for subclusters of leaves within unrooted trees that is not interrupted by another 'outgroup' taxa?
exactly. If the target tree is unrooted, it will search for "potential" monophyly, meaning "could these nodes form a monophyletic clade? "