forked from ComparativeGenomicsToolkit/hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treeCommon.py
205 lines (180 loc) · 6.36 KB
/
treeCommon.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
#Copyright (C) 2013 by Ngan Nguyen
#
#Released under the MIT license, see LICENSE.txt
'''
Objects & functions to parse, manipulate, draw, or get info from, trees
'''
from Bio import Phylo
from Bio.Phylo.Newick import Clade
import os, copy, sys
from sonLib.bioio import system
def isBinaryTree(tree):
for clade in tree.get_nonterminals():
children = clade.clades
if len(children) != 2:
#sys.stderr.write("NODE %s has %d children!!\n" %(clade.name, len(children)))
#print children
#sys.stderr.write("Children: %s\n" %(",".join([c.name for c in children])))
#sys.exit(1)
return False
return True
def getLeftRight(children):
#Left child has less leaves than right child
count0 = children[0].count_terminals()
count1 = children[1].count_terminals()
if count0 <= count1:
return children[0], children[1]
else:
return children[1], children[0]
def inorder(tree, reverse=False):
#Return a middle-sorted (left, root, right) list of node names
if tree.is_terminal():
if tree.name:
return [tree.name]
else:
return []
children = tree.clades
if len(children) != 2:
sys.stderr.write("Tree %s is not a binary tree format! Binary tree is required. Please check.\n" %tree.name)
left, right = getLeftRight(children)
leftNames = inorder(left)
rootName = []
if tree.name:
rootName = [tree.name]
rightNames = inorder(right)
if not reverse:
return leftNames + rootName + rightNames
else:
return rightNames + rootName + leftNames
def getNode(tree, name):
for n in tree.find_clades():
if n.name == name:
return n
return None
def inorder_relative(tree, name):
newtree = copy.deepcopy(tree)
#node = newtree.find_any(name)
node = getNode(newtree, name)
if node:
try:
newtree.root_with_outgroup(node)
except TypeError:
print "Node %s" %node.name
print "Ancestor %s" % getParent(newtree, node).name
sys.exit(1)
return inorder(newtree.root)
else:
sys.stderr.write("Cannot find node *%s* in tree using find_any\n" %name)
return inorder(tree.root)
def alignInternalNodes(tree):
newtree = copy.deepcopy(tree)
for clade in newtree.get_nonterminals():
#continue #HACK
#if clade.name != 'reference':#HACK
# continue#HACK
children = clade.clades
if len(children) != 2:
sys.stderr.write("Tree %s is not a binary tree format! Binary tree is required. Please check.\n" %clade.name)
selfleaf = Clade(branch_length=0, name=clade.name)
newchildren=[children[0], selfleaf, children[1]]
clade.clades = newchildren
return newtree
def getLeaves(tree):
return [ leaf.name for leaf in tree.get_terminals() ]
def getNodes(tree):
return [ n.name for n in tree.find_clades() ]
def iterAllClades(root):
allClades = []
names = getNodes(root)
children = []
for name in names:
if name:
children.append(name)
allClades.append(children)
for clade in root.clades:
allClades.extend( iterAllClades(clade) )
return allClades
#=== get neighboring nodes ===
def getParent(tree, clade):
for c in tree.find_clades():
if clade in c.clades:
return c
return None
def getNeighbors(tree, name):
#Return: 1 parent (if not root), 3 closest leaves, 1 leave that is a little further away
neighbors = []
allSortedNames = inorder_relative(tree, name)
leaves =[c.name for c in tree.get_terminals()]
sortedNames = []#leaves only
for n in allSortedNames:
if n in leaves:
sortedNames.append(n)
node = tree.find_any(name)
parent = getParent(tree, node)
if parent:
neighbors.append(parent.name)
currindex = 0
for i, n in enumerate(sortedNames):
if len(neighbors) >= 5:
currindex = i
break
if n != name or (parent and n != parent.name):
neighbors.append(n)
if currindex < len(sortedNames):
middleindex = (len(sortedNames) + currindex)/2
neighbors.append(sortedNames[middleindex])
return neighbors
###### ETE2 #####
def my_layout(node):
from ete2 import AttrFace, faces
if node.is_leaf():
faces.add_face_to_node(AttrFace("name"), node, column=0, position="aligned")
else:
node.img_style["size"] = 0
node.img_style["fgcolor"] = "#000000"
#node.img_style["shape"] = "circle"
def drawTree(nwfile, outfile):
from ete2 import Tree, TreeStyle, TextFace
ts = TreeStyle()
ts.show_leaf_name = False
ts.layout_fn = my_layout
ts.branch_vertical_margin = 12.75
ts.orientation = 1
titleFace = TextFace("Phylogenetic Tree", fsize=18, fgcolor="white")
titleFace.margin_top = 15
ts.title.add_face(titleFace, column=1)
t = Tree(nwfile)
t.render(outfile, tree_style=ts)
#t.render(outfile, w=183, units="mm", tree_style=ts)
#============ HAL RELATED ==========
def checkHalTree(halfile, outdir, options):
treefile = os.path.join(outdir, "haltree.nw")
system("halStats --tree %s > %s" %(halfile, treefile))
tree = Phylo.read(treefile, "newick")
options.treeFile = treefile
options.tree = tree
#def getOrderFromTree(options):
# if options.tree:
# orders = inorder(options.tree, reverse=True)
# if not options.genomes:
# options.genomes = orders
def getProperName_tree(tree, properName):
from hal.assemblyHub.assemblyHubCommon import getProperName
for node in tree.find_clades():
if node.name:
node.name = getProperName(node.name, properName)
return
def drawTreeWtInternalNodesAligned(tree, outdir, properName):
modifiedTree = alignInternalNodes(tree)
leaves = getLeaves(modifiedTree)
getProperName_tree(modifiedTree, properName)
modifiedTreeFile = os.path.join(outdir, "hubTreeModified.nw")
Phylo.write(modifiedTree, modifiedTreeFile, 'newick')
treeFig = os.path.join(outdir, "hubTree.png")
try:
#drawTree(modifiedTreeFile, treeFig) #HACK
return treeFig, leaves
except RuntimeError:
sys.stderr.write("Cannot draw the tree, so the tree will be missing on the config page. Perhaps ETE2 was not installed?\n")
return None, None