-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtreeToConstraints.py
56 lines (39 loc) · 1.68 KB
/
treeToConstraints.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/env python
# Takes an input tree and returns a list of constraints (one per bipartition)
from dendropy import Tree, TaxonSet
from cStringIO import StringIO
import sys
def main(argv):
# Instantiates taxon set object for tree list
taxa = TaxonSet()
# Reads in tree string from the command line
focalTree = Tree(stream=StringIO(argv[0]),schema="newick",rooted=False,taxon_set=taxa)
# Iterates over all internal nodes in the focal tree (generating one constraint each)
for i in focalTree.internal_nodes():
# Defines a list that initially contains all the leaf nodes from focal tree
fullTaxonSet = focalTree.leaf_nodes()
# Iterates over all internal nodes that are not the root
if i is not focalTree.seed_node:
# Instantiates string (conTree) to hold the constraint tree string
conTree = "(("
# Iterates over leaf nodes that are descendants of the current internal node
for j in i.leaf_nodes():
# Appropriately adds the taxon name to the constraint tree string
if j is i.leaf_nodes()[0]:
conTree = conTree + str(j.taxon)
else:
conTree = conTree + "," + str(j.taxon)
# Closes out the part of the constraint for taxa descended from the focal node
conTree = conTree + ")"
# Takes all leaves and removes those descended from the focal node
for j in i.leaf_nodes():
fullTaxonSet.remove(j)
# Adds all leaves not descended from the focal node to the constraint tree string
for j in fullTaxonSet:
conTree = conTree + "," + str(j.taxon)
# Closes constraint tree string
conTree = conTree + ")"
# Prints constraint tree string to the screen
print conTree
if __name__ == "__main__":
main(sys.argv[1:])