forked from mjhubisz/argweaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_hmm.py
370 lines (283 loc) · 9.89 KB
/
test_hmm.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
"""
Tests for various HMM calculations
"""
from itertools import izip
from math import exp
from math import log
import argweaver
import argweaver.popsize
from argweaver import argweaverc
from compbio import arglib
from rasmus import treelib
from rasmus import util
from rasmus.testing import fequal
from rasmus.testing import make_clean_dir
def test_trans_two():
"""
Calculate transition probabilities for k=2
Only calculate a single matrix
"""
k = 2
n = 1e4
rho = 1.5e-8 * 20
length = 1000
times = argweaver.get_time_points(ntimes=5, maxtime=200000)
time_steps = [times[i] - times[i-1]
for i in range(1, len(times))]
time_steps.append(200000*10000.0)
popsizes = [n] * len(times)
arg = arglib.sample_arg(k, 2*n, rho, start=0, end=length)
argweaver.discretize_arg(arg, times)
print "recomb", arglib.get_recombs(arg)
arg = argweaver.make_trunk_arg(0, length, "n0")
pos = 10
tree = arg.get_marginal_tree(pos)
nlineages = argweaver.get_nlineages_recomb_coal(tree, times)
states = list(argweaver.iter_coal_states(tree, times))
mat = argweaver.calc_transition_probs(
tree, states, nlineages,
times, time_steps, popsizes, rho)
nstates = len(states)
def coal(j):
return 1.0 - exp(-time_steps[j]/(2.0 * n))
def recoal2(k, j):
p = coal(j)
for m in range(k, j):
p *= 1.0 - coal(m)
return p
def recoal(k, j):
if j == nstates-1:
return exp(- sum(time_steps[m] / (2.0 * n)
for m in range(k, j)))
else:
return ((1.0 - exp(-time_steps[j]/(2.0 * n))) *
exp(- sum(time_steps[m] / (2.0 * n)
for m in range(k, j))))
def isrecomb(i):
return 1.0 - exp(-max(rho * 2.0 * times[i], rho))
def recomb(i, k):
treelen = 2*times[i] + time_steps[i]
if k < i:
return 2.0 * time_steps[k] / treelen / 2.0
else:
return time_steps[k] / treelen / 2.0
def trans(i, j):
a = states[i][1]
b = states[j][1]
p = sum(recoal(k, b) * recomb(a, k)
for k in range(0, min(a, b)+1))
p += sum(recoal(k, b) * recomb(a, k)
for k in range(0, min(a, b)+1))
p *= isrecomb(a)
if i == j:
p += 1.0 - isrecomb(a)
return p
for i in range(len(states)):
for j in range(len(states)):
print isrecomb(states[i][1])
print states[i], states[j], mat[i][j], log(trans(i, j))
fequal(mat[i][j], log(trans(i, j)))
# recombs add up to 1
fequal(sum(recomb(i, k) for k in range(i+1)), 0.5)
# recoal add up to 1
fequal(sum(recoal(i, j) for j in range(i, nstates)), 1.0)
# recomb * recoal add up to .5
fequal(sum(sum(recoal(k, j) * recomb(i, k)
for k in range(0, min(i, j)+1))
for j in range(0, nstates)), 0.5)
fequal(sum(trans(i, j) for j in range(len(states))), 1.0)
def test_trans():
"""
Calculate transition probabilities
"""
create_data = False
if create_data:
make_clean_dir('test/data/test_trans')
k = 8
n = 1e4
rho = 1.5e-8 * 20
length = 1000
times = argweaver.get_time_points(ntimes=10, maxtime=200000)
popsizes = [n] * len(times)
ntests = 40
# generate test data
if create_data:
for i in range(ntests):
arg = arglib.sample_arg(k, 2*n, rho, start=0, end=length)
argweaver.discretize_arg(arg, times)
arg.write('test/data/test_trans/%d.arg' % i)
for i in range(ntests):
print 'arg', i
arg = arglib.read_arg('test/data/test_trans/%d.arg' % i)
argweaver.discretize_arg(arg, times)
pos = 10
tree = arg.get_marginal_tree(pos)
assert argweaverc.assert_transition_probs(tree, times, popsizes, rho)
def test_trans_switch():
"""
Calculate transition probabilities for switch matrix
Only calculate a single matrix
"""
create_data = False
if create_data:
make_clean_dir('test/data/test_trans_switch')
# model parameters
k = 12
n = 1e4
rho = 1.5e-8 * 20
length = 1000
times = argweaver.get_time_points(ntimes=20, maxtime=200000)
popsizes = [n] * len(times)
ntests = 100
# generate test data
if create_data:
for i in range(ntests):
# Sample ARG with at least one recombination.
while True:
arg = argweaver.sample_arg_dsmc(
k, 2*n, rho, start=0, end=length, times=times)
if any(x.event == "recomb" for x in arg):
break
arg.write('test/data/test_trans_switch/%d.arg' % i)
for i in range(ntests):
print 'arg', i
arg = arglib.read_arg('test/data/test_trans_switch/%d.arg' % i)
argweaver.discretize_arg(arg, times)
recombs = [x.pos for x in arg if x.event == "recomb"]
pos = recombs[0]
tree = arg.get_marginal_tree(pos-.5)
rpos, r, c = arglib.iter_arg_sprs(arg, start=pos-.5).next()
spr = (r, c)
if not argweaverc.assert_transition_switch_probs(
tree, spr, times, popsizes, rho):
tree2 = tree.get_tree()
treelib.remove_single_children(tree2)
treelib.draw_tree_names(tree2, maxlen=5, minlen=5)
assert False
def test_trans_internal():
"""
Calculate transition probabilities for internal branch re-sampling
Only calculate a single matrix
"""
k = 5
n = 1e4
rho = 1.5e-8 * 20
length = 1000
times = argweaver.get_time_points(ntimes=5, maxtime=200000)
popsizes = [n] * len(times)
arg = arglib.sample_arg(k, 2*n, rho, start=0, end=length)
argweaver.discretize_arg(arg, times)
pos = 10
tree = arg.get_marginal_tree(pos)
assert argweaverc.assert_transition_probs_internal(
tree, times, popsizes, rho)
def test_trans_switch_internal():
"""
Calculate transition probabilities for switch matrix and internal branches
Only calculate a single matrix
"""
k = 10
n = 1e4
rho = 1.5e-8 * 20
length = int(100e3) / 20
times = argweaver.get_time_points(ntimes=20, maxtime=200000)
popsizes = [n] * len(times)
arg = argweaver.sample_arg_dsmc(k, 2*n, rho, start=0, end=length,
times=times)
trees, names = argweaverc.arg2ctrees(arg, times)
assert argweaverc.assert_transition_probs_switch_internal(
trees, times, popsizes, rho)
def test_emit():
"""
Calculate emission probabilities
"""
k = 10
n = 1e4
rho = 1.5e-8 * 20
mu = 2.5e-8 * 20
length = int(1e3) / 20
times = argweaver.get_time_points(ntimes=20, maxtime=200000)
arg = argweaver.sample_arg_dsmc(k, 2*n, rho, start=0, end=length,
times=times)
muts = argweaver.sample_arg_mutations(arg, mu, times)
seqs = argweaver.make_alignment(arg, muts)
new_name = "n%d" % (k-1)
arg = argweaver.remove_arg_thread(arg, new_name)
trees, names = argweaverc.arg2ctrees(arg, times)
seqs2, nseqs, seqlen = argweaverc.seqs2cseqs(seqs, names + [new_name])
assert argweaverc.argweaver_assert_emit(trees, len(times), times, mu,
seqs2, nseqs, seqlen)
def test_emit_internal():
"""
Calculate emission probabilities for internal branches
"""
k = 10
n = 1e4
rho = 1.5e-8 * 20
mu = 2.5e-8 * 20
length = int(10e3) / 20
times = argweaver.get_time_points(ntimes=20, maxtime=200000)
arg = argweaver.sample_arg_dsmc(k, 2*n, rho, start=0, end=length,
times=times)
muts = argweaver.sample_arg_mutations(arg, mu, times)
seqs = argweaver.make_alignment(arg, muts)
trees, names = argweaverc.arg2ctrees(arg, times)
seqs2, nseqs, seqlen = argweaverc.seqs2cseqs(seqs, names)
assert argweaverc.argweaver_assert_emit_internal(
trees, len(times), times, mu, seqs2, nseqs, seqlen)
'''
def test_prior_counts():
"""
Calculate initial tree prior
"""
a = 10
n = 1e4
t = 1e3
for b in range(1, a):
x = argweaverc.prob_coal_counts_matrix(a, b, t, 2*n)
y = argweaver.popsize.prob_coal_counts(a, b, t, 2*n)
fequal(x, y)
'''
def test_forward():
k = 4
n = 1e4
rho = 1.5e-8 * 20
mu = 2.5e-8 * 20
length = int(100e3 / 20)
times = argweaver.get_time_points(ntimes=100)
arg = arglib.sample_arg_smc(k, 2*n, rho, start=0, end=length)
muts = arglib.sample_arg_mutations(arg, mu)
seqs = arglib.make_alignment(arg, muts)
print "muts", len(muts)
print "recomb", len(arglib.get_recombs(arg))
argweaver.discretize_arg(arg, times)
# remove chrom
new_name = "n%d" % (k - 1)
arg = argweaver.remove_arg_thread(arg, new_name)
carg = argweaverc.arg2ctrees(arg, times)
util.tic("C fast")
probs1 = argweaverc.argweaver_forward_algorithm(carg, seqs, times=times)
util.toc()
util.tic("C slow")
probs2 = argweaverc.argweaver_forward_algorithm(carg, seqs, times=times,
slow=True)
util.toc()
for i, (col1, col2) in enumerate(izip(probs1, probs2)):
for a, b in izip(col1, col2):
fequal(a, b, rel=.0001)
def test_arg_joint():
"""
Compute joint probability of an ARG
"""
k = 2
n = 1e4
rho = 1.5e-8 * 20
mu = 2.5e-8 * 20
length = 10000
times = argweaver.get_time_points(ntimes=20, maxtime=200000)
arg = argweaver.sample_arg_dsmc(k, 2*n, rho, start=0, end=length,
times=times)
muts = argweaver.sample_arg_mutations(arg, mu, times=times)
seqs = arglib.make_alignment(arg, muts)
lk = argweaver.calc_joint_prob(arg, seqs, mu=mu, rho=rho, times=times)
print lk