-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate.go
328 lines (287 loc) · 6.95 KB
/
simulate.go
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
// Copyright © 2022 J. Salvador Arias <jsalarias@gmail.com>
// All rights reserved.
// Distributed under BSD2 license that can be found in the LICENSE file.
// Package simulate creates random trees.
package simulate
import (
"cmp"
"fmt"
"math/rand/v2"
"slices"
"github.com/js-arias/timetree"
"gonum.org/v1/gonum/stat/distuv"
)
// Rander is a distribution that returns
// a random number.
type Rander interface {
Rand() float64
}
// Uniform creates a random tree using a uniform prior
// based on the method described by
// Ronquist et al. (2012)
// "A total evidence approach to dating with fossils,
// applied to the early radiation of Hymenoptera"
// Syst. Biol. 61: 973-999.
// doi:10.1093/sysbio/sys058.
// Uniform panics if len(ages) < 2,
func Uniform(name string, max, min int64, ages []int64) *timetree.Tree {
if len(ages) < 2 {
panic("expecting more than two terminals")
}
for _, a := range ages[1:] {
if a > min {
min = a
}
}
rootAge := max
if max > min {
rootAge = rand.Int64N(max-min) + min
}
// shuffle terminals
rand.Shuffle(len(ages), func(i, j int) {
ages[i], ages[j] = ages[j], ages[i]
})
added := make([]string, 0, len(ages))
t := timetree.New(name, rootAge)
// first node
term := "term0"
t.Add(0, rootAge-ages[0], term)
added = append(added, term)
term = "term1"
t.Add(0, rootAge-ages[1], term)
added = append(added, term)
for i, a := range ages[2:] {
// pick sister
s := added[rand.IntN(i+2)]
sis, _ := t.TaxNode(s)
// pick age
oldest := a
if sa := t.Age(sis); sa > a {
oldest = sa
}
age := rootAge - rand.Int64N(rootAge-oldest) + 1
// search coalescent sister
for {
p := t.Parent(sis)
pa := t.Age(p)
if pa > age {
break
}
sis = p
}
term := fmt.Sprintf("term%d", i+2)
if _, err := t.AddSister(sis, a, age-a, term); err != nil {
panic(fmt.Sprintf("unexpected error: %v", err))
}
added = append(added, term)
}
return t
}
// Coalescent creates a random tree
// using the Kingman coalescence
// with a population size of n.
// see Felsenstein J. (2004)
// "Inferring Phylogenies", Sinauer, p.456.
// Coalescent panics if terms < 2.
func Coalescent(name string, n float64, max int64, terms int) *timetree.Tree {
if terms < 2 {
panic("expecting more than two terminals")
}
ages := make([]int64, terms-1)
for i := range ages {
rate := float64((i+2)*(i+1)) / (4 * n)
exp := distuv.Exponential{
Rate: rate,
}
a := int64(exp.Rand())
for a > max {
a = int64(exp.Rand())
}
ages[i] = a
}
slices.SortFunc(ages, func(a, b int64) int {
return cmp.Compare(b, a)
})
added := make([]string, 0, terms)
t := timetree.New(name, ages[0])
// first node
term := "term0"
t.Add(0, ages[0], term)
added = append(added, term)
term = "term1"
t.Add(0, ages[0], term)
added = append(added, term)
for i := 2; i < terms; i++ {
// pick sister
s := added[rand.IntN(i)]
sis, _ := t.TaxNode(s)
// pick age
age := ages[i-1]
// search coalescent sister
for {
p := t.Parent(sis)
pa := t.Age(p)
if pa > age {
break
}
sis = p
}
term := fmt.Sprintf("term%d", i)
if _, err := t.AddSister(sis, 0, age, term); err != nil {
panic(fmt.Sprintf("unexpected error: %v", err))
}
added = append(added, term)
}
return t
}
// Yule creates a Yule tree with the given speciation rate,
// in million years,
// stopping when the number of terminals is reached
// or when all proposed speciation events are in the future.
// It returns false if less than two terminals are included.
// Yule panics if terms < 2.
func Yule(name string, spRate float64, rootAge int64, terms int) (*timetree.Tree, bool) {
if terms < 2 {
panic("expecting more than two terminals")
}
exp := distuv.Exponential{
Rate: spRate,
}
t := timetree.New(name, rootAge)
added := 0
yuleNode(t, 0, terms-2, &added, exp)
if len(t.Terms()) < 2 {
return t, false
}
return t, true
}
func yuleNode(t *timetree.Tree, n, max int, added *int, exp distuv.Exponential) {
age := t.Age(n)
if t.NumInternal() >= max {
term := fmt.Sprintf("term%d", *added)
t.Add(n, age, term)
*added++
term = fmt.Sprintf("term%d", *added)
t.Add(n, age, term)
*added++
return
}
// left descendant
next := age - int64(exp.Rand()*1_000_000)
if next < 0 {
term := fmt.Sprintf("term%d", *added)
t.Add(n, age, term)
*added++
} else {
left, _ := t.Add(n, age-next, "")
yuleNode(t, left, max, added, exp)
}
// right descendant
if t.NumInternal() >= max {
term := fmt.Sprintf("term%d", *added)
t.Add(n, age, term)
*added++
return
}
next = age - int64(exp.Rand()*1_000_000)
if next < 0 {
term := fmt.Sprintf("term%d", *added)
t.Add(n, age, term)
*added++
return
}
left, _ := t.Add(n, age-next, "")
yuleNode(t, left, max, added, exp)
}
// BirthDeath create a birth-death tree
// with the given speciation and extinction rate,
// in million years,
// stopping when the number of terminals is reached
// of when all proposed events are in the future.
// It returns false if less than two terminals are included.
// BirthDeath panics if terms < 2.
func BirthDeath(name string, spRate, extRate float64, rootAge int64, terms int) (*timetree.Tree, bool) {
if terms < 2 {
panic("expecting more than two terminals")
}
if extRate == 0 {
return Yule(name, spRate, rootAge, terms)
}
sp := distuv.Exponential{
Rate: spRate,
}
e := distuv.Exponential{
Rate: extRate,
}
t := timetree.New(name, rootAge)
added := 0
bdNode(t, 0, terms-2, &added, sp, e)
if len(t.Terms()) < 2 {
return t, false
}
return t, true
}
func bdNode(t *timetree.Tree, n, max int, added *int, sp, ext distuv.Exponential) {
age := t.Age(n)
if t.NumInternal() >= max {
// left descendant
brLen := age
if e := age - int64(ext.Rand()*1_000_000); e > 0 {
brLen = age - e
}
term := fmt.Sprintf("term%d", *added)
t.Add(n, brLen, term)
*added++
// right descendant
brLen = age
if e := age - int64(ext.Rand()*1_000_000); e > 0 {
brLen = age - e
}
term = fmt.Sprintf("term%d", *added)
t.Add(n, brLen, term)
*added++
return
}
// left descendant
spNext := age - int64(sp.Rand()*1_000_000)
eNext := age - int64(ext.Rand()*1_000_000)
if spNext < 0 && eNext < 0 {
term := fmt.Sprintf("term%d", *added)
t.Add(n, age, term)
*added++
} else if eNext > spNext {
term := fmt.Sprintf("term%d", *added)
t.Add(n, age-eNext, term)
*added++
} else {
left, _ := t.Add(n, age-spNext, "")
bdNode(t, left, max, added, sp, ext)
}
// right descendant
eNext = age - int64(ext.Rand()*1_000_000)
if t.NumInternal() >= max {
brLen := age
if eNext > 0 {
brLen = age - eNext
}
term := fmt.Sprintf("term%d", *added)
t.Add(n, brLen, term)
*added++
return
}
spNext = age - int64(sp.Rand()*1_000_000)
if spNext < 0 && eNext < 0 {
term := fmt.Sprintf("term%d", *added)
t.Add(n, age, term)
*added++
return
}
if eNext > spNext {
term := fmt.Sprintf("term%d", *added)
t.Add(n, age-eNext, term)
*added++
return
}
right, _ := t.Add(n, age-spNext, "")
bdNode(t, right, max, added, sp, ext)
}