-
Notifications
You must be signed in to change notification settings - Fork 0
/
DP.py
401 lines (364 loc) · 18 KB
/
DP.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# DP.py
# Ran Libeskind-Hadas, June 2015
# The basic DP algorithm for reconciling pairs of trees
# Altered and expanded by Carter Slocum and Annalise Schweickart
# A tree is represented as a dictionary of key-value pairs where a key is an
# edge name and the value is a tuple of the form
# (start vertex, end vertex, left child edge name, right child edge name)
# An edge name may be None. The "dummy" edge leading to the root of the
# parasite tree, denoted e^P in the technical report, must be named "pTop".
# Edited by Annalise Schweickart and Carter Slocum, July 2015 to return
# the DTL reconciliation graph that uses frequency scoring, as well as the
# number of reconciliations of the host and parasite trees
import newickFormatReader
import Greedy
import copy
Infinity = float('inf')
def preorder(tree, rootEdgeName):
""" Takes a tree as input (see format description above) and returns a
list of the edges in that tree in preorder (high edges to low edges)"""
value = tree[rootEdgeName]
_,_,leftChildEdgeName,rightChildEdgeName = value
# base case
if leftChildEdgeName == None: # then rightChildEdgeName == None also
return [rootEdgeName]
else:
return [rootEdgeName] + \
preorder(tree, leftChildEdgeName) + \
preorder(tree, rightChildEdgeName)
def postorder(tree, rootEdgeName):
""" Takes a tree as input (see format description above) and returns a
list of the edges in that tree in postorder (low edges to high edges)"""
value = tree[rootEdgeName]
_,_,leftChildEdgeName,rightChildEdgeName = value
# base case
if leftChildEdgeName == None: # then rightChildEdgeName == None also
return [rootEdgeName]
else:
return postorder(tree, leftChildEdgeName) + \
postorder(tree, rightChildEdgeName) + \
[rootEdgeName]
def DP(hostTree, parasiteTree, phi, D, T, L):
""" Takes a hostTree, parasiteTree, tip mapping function phi, and
duplication cost (D), transfer cost (T), and loss cost (L) and
returns the DTL graph in the form of a dictionary, as well as a
the number of maximum parsimony reconciliations. The notation and
dynamic programming algorithm are explained in the tech report.
Cospeciation is assumed to cost 0. """
A = {} # A, C, O, and bestSwitch are all defined in tech report
C = {}
O = {}
eventsDict = {} # Dictionary to keep track of events, children, and scores
bestSwitch = {}
Minimums = {} # Dictionary to keep track of minimum reconciliation costs
oBest = {} # Dictionary to keep track of the lowest costing events in O
bestSwitchLocations = {} # Dictionary to keep track of switch locations
Score = {} # Dictionary to calculate the frequency scoring of each event
# Following logic taken from tech report
for ep in postorder(parasiteTree, "pTop"):
for eh in postorder(hostTree, "hTop"):
_,vp,ep1,ep2 = parasiteTree[ep]
_,vh,eh1,eh2 = hostTree[eh]
eventsDict[(vp, vh)] = []
oBest[(vp, vh)] = []
# is vp a tip?
if ep1 == None: # then ep2 == None too and vp is a tip!
vpIsATip = True
pChild1 = None
pChild2 = None
else:
vpIsATip = False
pChild1 = parasiteTree[ep][2][1]
pChild2 = parasiteTree[ep][3][1]
# is vh a tip?
if eh1 == None: # then eh2 == None too and vh is a tip!
vhIsATip = True
hChild1 = None
hChild2 = None
else:
vhIsATip = False
hChild1 = hostTree[eh][2][1]
hChild2 = hostTree[eh][3][1]
# Compute A(ep, eh)
if vhIsATip:
if vpIsATip and phi[vp] == vh:
A[(ep, eh)] = 0
# Contemporary event to be added to eventsDict
Amin = [["C", (None, None), (None, None), 1]]
Score[(vp, vh)] = 1.0
else:
Score[(vp, vh)] = Infinity
A[(ep, eh)] = Infinity
Amin = [Infinity]
else: #vh is not a tip
# Compute S and create event list to add to eventsDict
if not vpIsATip:
COepeh = min(C[(ep1, eh1)] + C[(ep2, eh2)], \
C[(ep1, eh2)] + C[(ep2, eh1)])
coMin = [] # List to keep track lowest cost speciation
if COepeh ==C[(ep2, eh1)] + C[(ep1, eh2)]:
coMin.append(["S", (pChild2, hChild1), \
(pChild1, hChild2), (Score[(pChild2, hChild1)] * \
Score[(pChild1, hChild2)])])
if COepeh == C[(ep1, eh1)] + C[(ep2, eh2)]:
coMin.append(["S", (pChild1, hChild1), \
(pChild2, hChild2),(Score[(pChild1, hChild1)]\
* Score[(pChild2, hChild2)])])
else:
COepeh = Infinity
coMin = [Infinity]
Score[(vp, vh)] = Infinity
# Compute L and create event list to add to eventsDict
LOSSepeh = L + min(C[(ep, eh1)], C[(ep, eh2)])
lossMin = [] # List to keep track of lowest cost loss
if LOSSepeh == L + C[(ep, eh1)]: lossMin.append(\
["L", (vp, hChild1), (None, None), \
Score[(vp, hChild1)]])
if LOSSepeh == L + C[(ep, eh2)]: lossMin.append(\
["L", (vp, hChild2), (None, None), Score[(vp, hChild2)]])
# Determine which event occurs for A[(ep, eh)]
A[(ep, eh)] = min(COepeh, LOSSepeh)
# Record event occuring for A[(ep, eh)] as Amin
if COepeh < LOSSepeh:
Amin = coMin
elif LOSSepeh < COepeh:
Amin = lossMin
else:
Amin = lossMin + coMin
# Compute C(ep, eh)
# First, compute D
if not vpIsATip:
DUPepeh = D + C[(ep1, eh)] + C[(ep2, eh)]
# List to keep track of lowest cost duplication event
dupList = ["D", (pChild1, vh), (pChild2, vh), \
(Score[(pChild1, vh)] * Score[(pChild2, vh)])]
else:
DUPepeh = Infinity
dupList = [Infinity]
# Next, Compute T and create event list to add
# to eventsDict using bestSwitchLocations
if not vpIsATip:
switchList = [] # List to keep track of lowest cost switch
SWITCHepeh = T + min(C[(ep1, eh)] + bestSwitch[(ep2, eh)], \
C[(ep2, eh)] + bestSwitch[(ep1, eh)])
# if ep2 switching has the lowest cost
if (C[(ep1, eh)] + bestSwitch[(ep2, eh)]) < (C[(ep2, eh)] + \
bestSwitch[(ep1, eh)]):
for location in bestSwitchLocations[(pChild2,vh)]:
currentLoc = location[1] # Switch landing site
if currentLoc == None: # Switches to a leaf
Score[(pChild1, currentLoc)] = Infinity
Score[(pChild2, currentLoc)] = Infinity
switchList.append(["T", (pChild1, vh), (pChild2, \
currentLoc), (Score[(pChild1, vh)] * \
Score[(pChild2, currentLoc)])])
# if ep1 switching has the lowest cost
elif (C[(ep2, eh)] + bestSwitch[(ep1, eh)]) < (C[(ep1, eh)] +\
bestSwitch[(ep2, eh)]):
for location in bestSwitchLocations[(pChild1,vh)]:
currentLoc = location[1]
if currentLoc == None:
Score[(pChild1, currentLoc)] = Infinity
Score[(pChild2, currentLoc)] = Infinity
switchList.append(["T", (pChild2, vh), \
(pChild1, currentLoc), (Score[(pChild2, vh)] * \
Score[(pChild1, currentLoc)])])
# if ep1 switching has the same cost as ep2 switching
else:
for location in bestSwitchLocations[(pChild2, vh)]:
currentLoc = location[1]
if currentLoc != None:
switchList.append(["T", (pChild1, vh), \
(pChild2, currentLoc), (Score[(pChild1, vh)] * \
Score[(pChild2, currentLoc)])])
else:
switchList.append(["T", (pChild1, vh), \
(pChild2, currentLoc), Infinity])
for location in bestSwitchLocations[(pChild1,vh)]:
currentLoc = location[1]
if currentLoc != None:
switchList.append(["T", (pChild2, vh), \
(pChild1, currentLoc), (Score[(pChild2, vh)] * \
Score[(pChild1, currentLoc)])])
else:
switchList.append(["T", (pChild1, vh), \
(pChild2, currentLoc), Infinity])
else:
SWITCHepeh = Infinity
switchList = [Infinity]
# Compute C[(ep, eh)] and add the event or events with that cost
# to the dictionary eventsDict
C[(ep, eh)] = min(A[(ep, eh)], DUPepeh, SWITCHepeh)
Minimums[(vp, vh)] = C[(ep, eh)]
if min(A[(ep, eh)], DUPepeh, SWITCHepeh) == DUPepeh:
eventsDict[(vp, vh)].append(dupList)
if min(A[(ep, eh)], DUPepeh, SWITCHepeh) == SWITCHepeh:
eventsDict[(vp, vh)].extend(switchList)
if min(A[(ep, eh)], DUPepeh, SWITCHepeh) == A[(ep, eh)]:
eventsDict[(vp, vh)].extend(Amin)
for key in eventsDict:
mapScore = 0 # initialize frequency scoring for each event
for event in eventsDict[key]:
if type(event) is list:
mapScore += event[-1]
Score[key] = mapScore
if Minimums[(vp, vh)] == Infinity:
del Minimums[(vp, vh)]
del eventsDict[(vp, vh)]
# Compute O(ep, eh)
# Compute oBest[(vp, vh)], the source of O(ep, eh)
if vhIsATip:
O[(ep, eh)] = C[(ep, eh)]
oBest[(vp, vh)] = [(vp, vh)]
else:
#finds the minimum switch locations for O
oMin = [C[(ep, eh)], O[(ep, eh1)], O[(ep, eh2)]].index\
(min(C[(ep, eh)], O[(ep, eh1)], O[(ep, eh2)]))
if oMin == 0:
oBest[(vp,vh)].append((vp, vh))
if oMin == 1:
oBest[(vp,vh)].extend(oBest[(vp, hChild1)])
if oMin == 2:
oBest[(vp,vh)].extend(oBest[(vp, hChild2)])
#finds Minimum Cost for O
O[(ep, eh)] = min(C[(ep, eh)], O[(ep, eh1)], O[(ep, eh2)])
# Compute bestSwitch values
bestSwitch[(ep, "hTop")] = Infinity
bestSwitchLocations[(vp, hostTree["hTop"][1])] = [(None,None)]
for eh in preorder(hostTree, "hTop"):
_, vp, ep1, ep2 = parasiteTree[ep]
_, vh, eh1, eh2 = hostTree[eh]
#is vp a tip?
if ep1 == None:
vpIsATip = True
pChild1 = None
pChild2 = None
else:
vpIsATip = False
pChild1 = parasiteTree[ep][2][1]
pChild2 = parasiteTree[ep][3][1]
# is vh a tip?
if eh1 == None: # then eh2 == None too and vh is a tip!
vhIsATip = True
hChild1 = None
hChild2 = None
else:
vhIsATip = False
hChild1 = hostTree[eh][2][1]
hChild2 = hostTree[eh][3][1]
# find best place for a switch to occur (bestSwitch)
# and the location to which the edge switches (bestSwitchLocations)
if eh1 != None and eh2 != None: # not a tip
bestSwitchLocations[(vp, hChild1)] = []
bestSwitchLocations[(vp, hChild2)] = []
bestSwitch[(ep, eh1)] = min(bestSwitch[(ep, eh)], O[(ep, eh2)])
bestSwitch[(ep, eh2)] = min(bestSwitch[(ep, eh)], O[(ep, eh1)])
if bestSwitch[(ep, eh1)] == bestSwitch[(ep, eh)] and \
bestSwitchLocations[(vp, vh)] != [(None, None)]:
bestSwitchLocations[(vp, hChild1)].extend\
(bestSwitchLocations[(vp, vh)])
if bestSwitch[(ep, eh1)] == O[(ep, eh2)] and \
oBest[(vp, hChild2)]!= [(None, None)]:
bestSwitchLocations[(vp, hChild1)].extend\
(oBest[(vp, hChild2)])
if bestSwitch[(ep, eh2)] == bestSwitch[(ep, eh)] and \
bestSwitchLocations[(vp, vh)] != [(None, None)]:
bestSwitchLocations[(vp, hChild2)].extend\
(bestSwitchLocations[(vp, vh)])
if bestSwitch[(ep, eh2)] == O[(ep, eh1)] and \
oBest[(vp, hChild1)]!=[(None, None)]:
bestSwitchLocations[(vp, hChild2)].extend\
(oBest[(vp, hChild1)])
for key in bestSwitchLocations:
if bestSwitchLocations[key][0] == (None, None):
bestSwitchLocations[key] = bestSwitchLocations[key][1:]
# Add the costs of each event to the corresponding eventsDict entry
for key in eventsDict:
eventsDict[key].append(Minimums[key])
# Use findPath and findBestRoots to construct the DTL graph dictionary
treeMin = findBestRoots(parasiteTree, Minimums)
DTL = findPath(treeMin, eventsDict, {})
for key in Score.keys():
if not key in DTL:
del Score[key]
DTL, numRecon = addScores(treeMin, DTL, Score)
return DTL, numRecon
def preorderDTLsort(DTL, ParasiteRoot):
"""This takes in a DTL reconciliation graph and parasite root and returns
a sorted list, orderedKeysL, that is ordered by level from largest to
smallest, where level 0 is the root and the highest level has tips."""
keysL = Greedy.orderDTL(DTL, ParasiteRoot)
uniqueKeysL = Greedy.sortHelper(DTL, keysL)
orderedKeysL = []
levelCounter = 0
while len(orderedKeysL) < len(keysL):
for mapping in keysL:
if mapping[-1] == levelCounter:
orderedKeysL = orderedKeysL + [mapping]
levelCounter += 1
lastLevel = orderedKeysL[-1][1]
return orderedKeysL
def addScores(treeMin, DTLDict, ScoreDict):
"""Takes the list of reconciliation roots, the DTL reconciliation graph,
a dictionary of parent nodes, and a dictionary of score values, and
returns the DTL with the normalized frequency scores calculated."""
newDTL = copy.deepcopy(DTLDict)
parentsDict = {}
preOrder = preorderDTLsort(DTLDict, treeMin[0][0])
for root in preOrder:
if root != (None, None):
vertices = root[0]
if root[1] == 0:
parentsDict[vertices] = ScoreDict[vertices]
for n in range(len(DTLDict[vertices])-1):
_,child1,child2,oldScore = DTLDict[vertices][n]
newDTL[vertices][n][3] = parentsDict[vertices] * \
(1.0 * oldScore / ScoreDict[vertices])
if child1!= (None, None):
if child1 in parentsDict:
parentsDict[DTLDict[vertices][n][1]] += \
newDTL[vertices][n][3]
else:
parentsDict[child1] = newDTL[vertices][n][3]
if child2!=(None, None):
if child2 in parentsDict:
parentsDict[child2] += newDTL[vertices][n][3]
else:
parentsDict[child2] = newDTL[vertices][n][3]
normalize = newDTL[preOrder[-1][0]][0][-1]
for key in newDTL:
for event in newDTL[key][:-1]:
event[-1] = event[-1]/normalize
return newDTL, normalize
def findBestRoots(Parasite, MinimumDict):
"""Takes Parasite Tree and a dictionary of minimum reconciliation costs
and returns a list of the minimum cost reconciliation tree roots"""
treeTops = []
for key in MinimumDict:
if key[0] == Parasite['pTop'][1]:
treeTops.append(key)
treeMin = []
for pair in treeTops:
if MinimumDict[pair] == min([MinimumDict[root] for root in treeTops]):
treeMin.append(pair)
return treeMin
def findPath(tupleList, eventDict, uniqueDict):
"""Takes as input tupleList, a list of minimum reconciliation cost roots,
eventDict, the dictionary of events and children for each node, and
uniqueDict, the dictionary of unique vertex mappings. This returns the
completed DTL graph as a Dictionary"""
for vertexPair in tupleList:
if not vertexPair in uniqueDict:
uniqueDict[vertexPair] = eventDict[vertexPair]
for event in eventDict[vertexPair][:-1]:
for location in event:
if type(location) is tuple and location != (None, None):
findPath([location], eventDict, uniqueDict)
return uniqueDict
def reconcile(fileName, D, T, L):
"""Takes as input a newick file, FileName, a dupliction cost, a transfer
cost, and a loss cost. This uses newickFormatReader to extract the host
tree, parasite tree and tip mapping from the file and then calls DP to
return the DTL reconciliation graph of the provided newick file"""
host, paras, phi = newickFormatReader.getInput(fileName)
return DP(host, paras, phi, D, T, L)