-
Notifications
You must be signed in to change notification settings - Fork 0
/
phylo.py
251 lines (219 loc) · 7.75 KB
/
phylo.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""
This needs to be cleaned up. If I am going to use biopython, you might as well use it in your PhyloTree...instead make
PhyloTree an easier to use interface for BioPython trees.
"""
__author__ = 'julianzaugg'
from sequence import *
from Bio import Phylo
class PhyloTree:
"""Non-binary tree for representing phylogenetic relationships"""
def __init__(self, root, **kwargs):
self.nodes = dict()
self.root = root
self.label = None
self.alignment = None
def __getitem__(self, key):
return self.nodes.get(key, None)
def __len__(self):
return len(self.nodes)
def __iter__(self):
return iter(self.nodes.values())
def __str__(self):
def recurse(root):
if root.children:
children = [recurse(child) for child in root.children]
return '(%s)%s' % (','.join(children), root.label)
else:
return root.label
tree = recurse(self.nodes[self.root]) + ";"
return tree
def _setLabel(self, label):
self.label = label
def addAlignment(self, aln):
"""
Add an alignment to the tree.
"""
self.alignment = aln
def add(self, node):
"""
Add a PhyloNode to the tree
"""
self.nodes[node.label] = node
def getNode(self, label):
"""
Get a node object from the tree
@return PhyloNode
"""
return self.nodes[label]
def getDescendants(self, node, transitive = False):
"""
Get all descendants for a node. If not transitive, will return only immediate descendants
@return list of descendants nodes
"""
if not isinstance(node, PhyloNode):
node = self.getNode(node)
if node:
return node.getDescendants(transitive)
return []
def getAncestors(self, node, transitive = False):
"""
Get all ancestors for a node. If not transitive, will return only immediate ancestors
@return list of ancestor nodes
"""
if not isinstance(node, PhyloNode):
node = self.getNode(node)
if node:
myroot = self.getNode(self.root)
found = False
branching = []
if node == myroot:
found = True
while not found and myroot != None:
branching.append(myroot)
for child in myroot.children:
if child == node:
found = True
break
if child.isAncestorOf(node, transitive = True):
myroot = child
if found and transitive:
return branching
elif found and len(branching) > 0:
return [branching[len(branching)-1]]
return []
def getLeaves(self):
"""
Get the leaves of the tree
@return set of nodes without children
"""
return {node for node in self if not node.children}
def getTopologicalOrdering(self):
"""
Get the ordering of all nodes in the tree based on topological order
@return list of nodes in topological order
"""
ordered = []
leaves = self.getLeaves()
visited = set()
def visit(node):
visited.add(node)
for parent in self.getAncestors(node):
if parent not in visited:
visit(parent)
ordered.append(node)
for node in leaves:
visit(node)
return ordered
def getBranch(self, node):
"""
@return ordered list of nodes in the same branch for queried node (including query node)
"""
return self.getAncestors(node, transitive=True) +\
[node] +\
self.getDescendants(node, transitive=True)
def getSubBranch(self, ancestor_node, descendant_node):
"""
@return a ordered list of nodes between (and including) ancestor and descendant nodes
"""
node_branch = self.getBranch(descendant_node)
ancestor_index = node_branch.index(ancestor_node)
descendant_index = node_branch.index(descendant_node)
return node_branch[ancestor_index:descendant_index + 1]
def findPath(self, start_node, end_node, path = []):
"""
Find the path between two nodes in the tree
@return list of nodes in order of steps in path
"""
path = path + [start_node]
if start_node == end_node:
return path
immediates = self.getDescendants(start_node) + self.getAncestors(start_node)
for node in immediates:
if node not in path:
newpath = self.findPath(node, end_node, path)
if newpath: return newpath
return None
def getBranches(self):
"""
Get all complete branches in the tree
@return dictionary of branches for the tree
"""
leaves = self.getLeaves()
branch_node_sets = {}
for leaf in leaves:
branch_nodes = self.getAncestors(leaf, transitive = True) + [leaf]
branch_node_sets[leaf] = branch_nodes
return branch_node_sets
class PhyloNode:
def __init__(self, label, **kwargs):
self.label = label
self.children = []
self.info = dict()
self.parent = None
def getDescendants(self, transitive = False):
"""
Get all descendants for this node. If not transitive, will return only immediate descendants
@return list of descendant nodes
"""
children = list(self.children)
if not transitive:
return children
else:
grandchildren = []
for child in children:
grandchildren.extend(child.getDescendants(transitive))
children.extend(grandchildren)
return children
def assignInfo(self, **kwargs):
"""
Assign additional annotations to this node
"""
for k,v in kwargs.items():
self.info[k] = v
def addChild(self, childNode):
if not isinstance(childNode, PhyloNode):
raise TypeError("supplied child node %s is node of type PhyloNode" % childNode)
self.children.append(childNode)
def isAncestorOf(self, node, transitive = False):
"""
Determine if this node is the ancestor of the specified node.
If transitive is True (default), all descendants are included.
If transitive is False, only direct descendants are included.
@return boolean
"""
if node in self.children:
return True
elif transitive:
for child_node in self.children:
status = child_node.isAncestorOf(node, transitive)
if status:
return True
else:
return False
def __str__(self):
return self.label
#This is a VERY dirty Hack. Fix later.
def load_tree(filename):
"""
Use Biopython to load newick tree. This is then converted into
our own Phylo tree format.
"""
bio_tree = Phylo.read(filename, 'newick')
tree = PhyloTree(bio_tree.root.name)
def thing(root):
if len(root) == 0:
tree.add(PhyloNode(root.name))
else:
tree.add(PhyloNode(root.name))
# print root.name
tree[root.name].children = [child.name for child in root]
# if root.name == None: print tree[root.name].children
for rc in root:
thing(rc)
thing(bio_tree.root)
for node in tree.nodes.values():
new_children = []
for child in node.children:
new_children.append(tree.nodes[child])
node.children = new_children
return tree