-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeGenerators.py
More file actions
501 lines (401 loc) · 16.1 KB
/
TreeGenerators.py
File metadata and controls
501 lines (401 loc) · 16.1 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import math
import random
from dendropy.calculate import probability
import dendropy
from dendropy.simulate import treesim
import numpy as np
from Utilities import *
from DashUtilities import *
class TreeGenerator(Parameterizable, DashInterfacable):
def __init__(self):
Parameterizable.__init__(self)
DashInterfacable.__init__(self)
@abstractmethod
def generate(self, stopCriteria):
pass
#class NeutralTreeGenerator(TreeGenerator):
# def GetDefaultParams(self):
# return ParametersDescr({
# 'birth_rate' : (1.0,),
# 'death_rate' : (0.0,)
# })
#
# def generate(self, num_extant_tips = None, max_time = None):
# if num_extant_tips is not None:
# t = treesim.birth_death_tree(birth_rate=self.birth_rate, death_rate=self.death_rate, num_extant_tips=num_extant_tips, is_retain_extinct_tips = True)
# elif max_time is not None:
# t = treesim.birth_death_tree(birth_rate=self.birth_rate, death_rate=self.death_rate, max_time=max_time, is_retain_extinct_tips = True)
# else:
# raise ValueError('At least one ending condition must be specified.')
#
# # TODO Fix max_time here
#
# # TODO What to do about this?
# # Get last nodes generated (a cherry with branch lenghts = 0) and replace a cherry by one single node
# #lastleaf=[n for n in t.leaf_nodes() if n.edge_length == 0][0]
# #parent=lastleaf.parent_node
# #parent.remove_child(lastleaf, suppress_unifurcations=True)
# return t
# Utility class for tree generators
class StoppingCriteria(Parameterizable, DashInterfacable):
def __init__(self):
Parameterizable.__init__(self)
DashInterfacable.__init__(self)
@abstractmethod
def shouldStop(self, **kwargs):
pass
def isFinished(self, tree):
return True
def correctTree(self, **kwargs):
pass
class NumExtantStopCrit(StoppingCriteria):
def GetDefaultParams(self):
return ParametersDescr({
'num_extant_tips' : (20, int),
})
def shouldStop(self, extant_tips, **kwargs):
return len(extant_tips) >= self.num_extant_tips or len(extant_tips) == 0
def isFinished(self, tree):
return len([n for n in tree.leaf_nodes() if not hasattr(n, 'is_extinct') or not n.is_extinct]) >= self.num_extant_tips
class MaxTimeStopCrit(StoppingCriteria):
def GetDefaultParams(self):
return ParametersDescr({
'max_time' : (3.0, float),
})
def shouldStop(self, total_time, extant_tips, **kwargs):
return total_time >= self.max_time or len(extant_tips) == 0
def isFinished(self, tree):
return max(tree.calc_node_root_distances(return_leaf_distances_only=True)) + tree.seed_node.edge.length >= self.max_time
def correctTree(self, tree, c1, c2, total_time, isBirth, **kwargs):
if total_time > self.max_time:
if isBirth:
parent = c1.parent_node
parent.remove_child(c1)
parent.remove_child(c2)
for n in tree.leaf_nodes():
if not hasattr(n, 'is_extinct') or not n.is_extinct:
n.edge.length -= total_time - self.max_time
class NumLeavesStopCrit(StoppingCriteria):
def GetDefaultParams(self):
return ParametersDescr({
'num_leaves' : (20, int),
})
def shouldStop(self, extant_tips, extinct_tips, **kwargs):
return len(extant_tips) + len(extinct_tips) >= self.num_leaves or len(extant_tips) == 0
def isFinished(self, tree):
return len(tree.leaf_nodes()) >= self.num_leaves
##########################
# Tree Generator classes #
##########################
class RateFunctionTreeGenerator(TreeGenerator):
def __init__(self):
TreeGenerator.__init__(self)
def GetDefaultParams(self):
return ParametersDescr({
'birth_rf' : (TraitEvolLinearBrownian(), NonNeutralRateFunction),
'death_rf' : (ConstantRateFunction(), NonNeutralRateFunction)
})
def generate(self, stopCriteria):
self.birth_rf.updateValues()
self.death_rf.updateValues()
taxon_namespace = dendropy.TaxonNamespace()
tree = dendropy.Tree(taxon_namespace=taxon_namespace)
tree.is_rooted = True
tree.seed_node.edge.length = 0.0
extant_tips = [tree.seed_node]
extinct_tips = set()
total_time = 0
# Init Birth rates in edge
tree.seed_node.edge.birthRates = [(0, self.birth_rf.getRate(tree.seed_node, 0, total_time=0, extant_tips=extant_tips))]
tree.seed_node.edge.deathRates = [(0, self.death_rf.getRate(tree.seed_node, 0, total_time=0, extant_tips=extant_tips))]
#while len(extant_tips) < num_extant_tips and len(extant_tips) > 0:
while not stopCriteria.shouldStop(**{k:v for k, v in locals().items() if k!='self'}):
localTime = 0
noEvent = True
eventProb = 0
# Determine the time of the next event
while noEvent and not stopCriteria.shouldStop(**{k:v for k, v in locals().items() if k!='self'}):
allNextChange = [(self.birth_rf.getNextChange(n, n.edge.length + localTime, total_time=total_time+localTime, extant_tips=extant_tips), True) for n in extant_tips]
allNextChange += [(self.death_rf.getNextChange(n, n.edge.length + localTime, total_time=total_time+localTime, extant_tips=extant_tips), False) for n in extant_tips]
sortedNextChange = sorted(enumerate(allNextChange), key=lambda x:x[1][0])
IndNC, vNC = sortedNextChange[0]
minNextChange, nextChangeIsBirth = vNC
allProbs = [(self.birth_rf.getRate(n, n.edge.length + localTime, total_time=total_time+localTime, extant_tips=extant_tips), True) for n in extant_tips]
allProbs += [(self.death_rf.getRate(n, n.edge.length + localTime, total_time=total_time+localTime, extant_tips=extant_tips), False) for n in extant_tips]
eventProb = sum(prob for prob, tp in allProbs)
# Recompute epsilon according to the current event rate
epsilon = 0.00001 / max(1, eventProb)
waiting_time = random.expovariate(eventProb)
localTime += min(waiting_time, minNextChange + epsilon)
noEvent = waiting_time > minNextChange
# Build rate variations in edges
if noEvent:
for n, changeIsBirth in [(extant_tips[nc[0] - (len(extant_tips) if not nc[1][1] else 0)], nc[1][1]) for nc in sortedNextChange if nc[1][0] <= minNextChange + epsilon]:
if changeIsBirth:
n.edge.birthRates.append((total_time + localTime, self.birth_rf.getRate(n, n.edge.length+localTime, total_time=total_time+localTime, extant_tips=extant_tips)))
else:
n.edge.deathRates.append((total_time + localTime, self.death_rf.getRate(n, n.edge.length+localTime, total_time=total_time+localTime, extant_tips=extant_tips)))
# add waiting time to nodes
for nd in extant_tips:
try:
nd.edge.length += localTime
except TypeError:
nd.edge.length = localTime
total_time += localTime
# Determine in which branch will the event happen
event_nodes = [(n, True) for n in extant_tips] + [(n, False) for n in extant_tips]
event_rates = [self.birth_rf.getRate(n, n.edge.length, total_time=total_time, extant_tips=extant_tips) / eventProb for n in extant_tips]
event_rates += [self.death_rf.getRate(n, n.edge.length, total_time=total_time, extant_tips=extant_tips) / eventProb for n in extant_tips]
nd, isBirth = probability.weighted_choice(event_nodes, event_rates, rng=random)
# Update rates if they change on split or extinction events
if self.birth_rf.IsChangedOnSplitOrDeath():
for n in extant_tips:
n.edge.birthRates.append((total_time, self.birth_rf.getRate(n, n.edge.length, total_time=total_time, extant_tips=extant_tips)))
if self.death_rf.IsChangedOnSplitOrDeath():
for n in extant_tips:
n.edge.deathRates.append((total_time, self.death_rf.getRate(n, n.edge.length, total_time=total_time, extant_tips=extant_tips)))
if isBirth:
# Branch
extant_tips.remove(nd)
c1 = nd.new_child()
c2 = nd.new_child()
extant_tips.append(c1)
extant_tips.append(c2)
c1.edge.length = 0
c2.edge.length = 0
c1.edge.birthRates = [(total_time, self.birth_rf.getRate(c1, 0, total_time=total_time, extant_tips=extant_tips))]
c2.edge.birthRates = [(total_time, self.birth_rf.getRate(c2, 0, total_time=total_time, extant_tips=extant_tips))]
c1.edge.deathRates = [(total_time, self.death_rf.getRate(c1, 0, total_time=total_time, extant_tips=extant_tips))]
c2.edge.deathRates = [(total_time, self.death_rf.getRate(c2, 0, total_time=total_time, extant_tips=extant_tips))]
else:
extant_tips.remove(nd)
extinct_tips.add(nd)
setattr(nd, 'is_extinct', True)
# Correct the tree if the stopping criterion was not exactly respected (over time, etc)
stopCriteria.correctTree(**{k:v for k, v in locals().items() if k!='self'})
return tree
##################
# Rate Functions #
##################
class NonNeutralRateFunction(Parameterizable, DashInterfacable):
def __init__(self):
Parameterizable.__init__(self)
DashInterfacable.__init__(self)
@abstractmethod
def getRate(self, node, time, **kwargs):
pass
@abstractmethod
def getNextChange(self, node, time, **kwargs):
pass
@abstractmethod
def getHighestPosisbleRate(self):
pass
# Overload when some parameter dependent values are computed
def updateValues(self):
pass
def IsChangedOnSplitOrDeath(self):
return False
class ConstantRateFunction(NonNeutralRateFunction):
def GetDefaultParams(self):
return ParametersDescr({
'rate' : (0.0,),
})
def getRate(self, node, time, **kwargs):
return self.rate
def getNextChange(self, node, time, **kwargs):
return math.inf
def getHighestPosisbleRate(self):
return self.rate
class ExplosiveRadiationRateFunc(NonNeutralRateFunction):
def GetDefaultParams(self):
return ParametersDescr({
'timeDelay' : (0.1,),
'basalRate' : (1.0,),
'lowRate' : (0.01,)
})
def getRate(self, node, time, **kwargs):
return self.basalRate if time <= self.timeDelay else self.lowRate
def getNextChange(self, node, time, **kwargs):
return self.timeDelay - time if time <= self.timeDelay else math.inf
def getHighestPosisbleRate(self):
return self.basalRate
class TraitEvolLinearBrownian(NonNeutralRateFunction):
def __init__(self):
NonNeutralRateFunction.__init__(self)
self.traitValname = 'traitVal' + str(id(self))
def GetDefaultParams(self):
return ParametersDescr({
'basalRate' : (1.0,),
'sigma' : (0.8,),
'lowestRate' : (0.01,)
})
def getRate(self, node, time, **kwargs):
if hasattr(node, self.traitValname):
return getattr(node, self.traitValname)
else:
if node.parent_node is None:
setattr(node, self.traitValname, self.basalRate)
else:
setattr(node, self.traitValname, max(self.lowestRate, getattr(node.parent_node, self.traitValname) + np.random.normal(0, self.sigma)))
return getattr(node, self.traitValname)
def getNextChange(self, node, time, **kwargs):
return math.inf
def getHighestPosisbleRate(self):
return self.basalRate
class ExtendedExplRadRateFunc(NonNeutralRateFunction):
def __init__(self):
NonNeutralRateFunction.__init__(self)
self.stepTimes = []
def GetDefaultParams(self):
return ParametersDescr({
'endDelay' : (1.5,),
'nbSteps' : (10, int),
'basalRate' : (1.0,),
'lowRate' : (0.4,)
})
def getRate(self, node, time, **kwargs):
ind = 0
while ind < len(self.stepTimes) and time > self.stepTimes[ind]:
ind += 1
return (self.basalRate-self.lowRate)*(len(self.stepTimes) - ind)/(len(self.stepTimes)) + self.lowRate
def getNextChange(self, node, time, **kwargs):
ind = 0
while ind < len(self.stepTimes) and time > self.stepTimes[ind]:
ind += 1
return self.stepTimes[ind] - time if ind < len(self.stepTimes) else math.inf
def getHighestPosisbleRate(self):
return self.basalRate
def updateValues(self):
self.stepTimes = [self.endDelay * ((i+1) / self.nbSteps) for i in range(self.nbSteps)]
class PhaseRateFunc(NonNeutralRateFunction):
def __init__(self):
NonNeutralRateFunction.__init__(self)
self.stepVals = []
self.actualFunc = lambda x:x
def GetDefaultParams(self):
return ParametersDescr({
'period' : (10.0,float),
'nbSteps' : (10, int),
'maxRate' : (1.0,float),
'minRate' : (0.01,float),
'periodFunc' : ('lambda t:(1+math.sin(t*2*math.pi))/2',)
})
def updateValues(self):
self.actualFunc = eval(self.periodFunc)
self.stepVals = [self.actualFunc(i/self.nbSteps)*(self.maxRate-self.minRate) + self.minRate for i in range(self.nbSteps)]
def getRate(self, node, time, total_time = 0, **kwargs):
rateInd = int(total_time*self.nbSteps/self.period) % self.nbSteps
return self.stepVals[rateInd]
def getNextChange(self, node, time, total_time = 0, **kwargs):
return (int(total_time*self.nbSteps/self.period) + 1) * self.period / self.nbSteps - total_time
def getHighestPosisbleRate(self):
return self.maxRate
class ExtantSizeRateFunc(NonNeutralRateFunction):
def __init__(self):
NonNeutralRateFunction.__init__(self)
self.actualFunc = lambda x:x
self.lastNbExtant = 1
def GetDefaultParams(self):
return ParametersDescr({
'func' : ('lambda n:0.5*math.log((1+math.exp(n - 20)))',)
})
def updateValues(self):
self.actualFunc = eval(self.func)
self.lastNbExtant = 1
def getRate(self, node, time, extant_tips = set(), **kwargs):
self.lastNbExtant = len(extant_tips)
return self.actualFunc(len(extant_tips))
def getNextChange(self, node, time, extant_tips = set(), **kwargs):
return math.inf
def getHighestPosisbleRate(self):
return max(self.actualFunc(self.lastNbExtant + 1), self.actualFunc(self.lastNbExtant - 1))
def IsChangedOnSplitOrDeath(self):
return True
class ImmunizationRateFunc(NonNeutralRateFunction):
def __init__(self):
NonNeutralRateFunction.__init__(self)
self.traitValname = 'traitVal' + str(id(self))
self.immunization = {}
self.lastTime = 0
def GetDefaultParams(self):
return ParametersDescr({
'immunizationRate' : (1.0,float),
'forgetRate' : (0.1,float),
'sliceSize' : (2.0, float)
})
def updateValues(self):
self.immunization = {}
self.lastTime = 0
def getRate(self, node, time, total_time = 0, extant_tips = set(), **kwargs):
distrib = {}
for nd in extant_tips:
if not hasattr(nd, self.traitValname):
if nd.parent_node is None:
setattr(nd, self.traitValname, 0)
else:
setattr(nd, self.traitValname, getattr(nd.parent_node, self.traitValname) + np.random.normal(0, 1))
val = round(getattr(nd, self.traitValname) / self.sliceSize)
if val not in distrib:
distrib[val] = 0
if val not in self.immunization:
self.immunization[val] = 0
distrib[val] += 1 #TODO Should we normalize here?
deltaT = total_time - self.lastTime
if deltaT > 0:
for val, rate in self.immunization.items():
if val not in distrib:
distrib[val] = 0
expVal = np.exp(-self.forgetRate*deltaT)
self.immunization[val] = max(0, rate*expVal + self.immunizationRate * distrib[val] * (1 - expVal) / self.forgetRate)
self.lastTime = total_time
val = round(getattr(node, self.traitValname) / self.sliceSize)
return self.immunization[val]
def getNextChange(self, node, time, extant_tips = set(), **kwargs):
# TODO for now, only update when an event happens
return math.inf
def getHighestPosisbleRate(self):
return self.immunizationRate / self.forgetRate
def IsChangedOnSplitOrDeath(self):
return True
class SortRateFunc(NonNeutralRateFunction):
def __init__(self):
NonNeutralRateFunction.__init__(self)
self.actualFunc = lambda x:x
def GetDefaultParams(self):
return ParametersDescr({
'maxRate' : (1.0,float),
'minRate' : (0.01,float),
#'sortFunc' : ('lambda r:0.9**(r)',)
'sortFunc' : ('lambda r:math.exp(-r)',)
})
def updateValues(self):
self.actualFunc = eval(self.sortFunc)
def getRate(self, node, time, total_time = 0, extant_tips = set(), **kwargs):
# Sort nodes from the most recent to the oldest
sortedNodes = sorted(extant_tips, key=lambda x: x.edge_length)
# Find rank of the current node
rankNode = sortedNodes.index(node)
# Compute value directly without taking into account min and max rates
rateNode = self.actualFunc(rankNode)
# Re-scale according to min and max rates
x_beg = self.actualFunc(0)
x_end = self.actualFunc(len(sortedNodes)-1)
y_beg = self.minRate
y_end = self.maxRate
if x_beg > x_end:
y_beg = self.maxRate
y_end = self.minRate
if len(sortedNodes) > 1:
a = (y_beg-y_end)/(x_beg-x_end)
b = y_beg -a*x_beg
rateNode = a*rateNode + b
else:
rateNode = y_beg
return rateNode
def getNextChange(self, node, time, total_time = 0, **kwargs):
# Only update when an event happens
return math.inf
def getHighestPosisbleRate(self):
return self.maxRate
def IsChangedOnSplitOrDeath(self):
return True