-
Notifications
You must be signed in to change notification settings - Fork 2
/
capperTest.py
288 lines (241 loc) · 19.5 KB
/
capperTest.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
import logging
import math
import unittest
from capper import Read
class CapperTest(unittest.TestCase):
def test_overlap_interval_iterator(self):
reads = Read.parse_reads("minimizers_incorrect_1kg") #, maxReads=1000)
for read in reads:
#print(read)
i, j, k = 0, -1, 0
for left, right, bottom, top in read.overlap_interval_iterator(read.minimizers,
start_fn=lambda x: x.minimizer_start, length_fn=lambda x : 29):
#print(" Left {}, Right {}, Bottom {}, Top {}".format(left, right, bottom, top))
assert left < right
assert i <= left
assert i < right
i = right
assert j == bottom or j+1 == bottom
assert bottom < top
j = bottom
assert k == top or k + 1 == top
k = top
# Check each minimizer is part of the interval
for minimizer in read.minimizers[bottom:top]:
assert minimizer.minimizer_start <= left
assert minimizer.minimizer_start + len(minimizer.minimizer) >= right
# Check each preceding minimizer occurs before the interval
for minimizer in read.minimizers[:bottom]:
assert minimizer.minimizer_start + len(minimizer.minimizer) <= left
# Check each proceding minimizer occurs after the interval
for minimizer in read.minimizers[top:]:
assert minimizer.minimizer_start >= right
assert i <= len(read.read)
assert k == len(read.minimizers)
def test_recompute_adam_cap_nocap(self):
# Here's a version where no minimizers are located (last fields all 0)
parts = ["TTCTGCTGTTGTCAGAAGGCTACTTCTGATATTTATGCTTTCATTCTCCATTAATCATTTAATATTTTATTTTCCTTCATCAGCCAACACTGCAGGCAGTTGAGGCTTTTTCCATGATGAAGTAAATCCAACTATGATTTCTGAAGAATA",
"????????+??????????????????++?????????????????????????????????????+??????????????????&5??????5?????????????????????????????????????+5?????????5???????",
"1",
"AAAATAAAATATTAAATGATTAATGGAGA", "44", "41", "39", "1", "0",
"GAAATCATAGTTGGATTTACTTCATCATG", "112", "107", "43", "1", "0",
"ATAGTTGGATTTACTTCATCATGGAAAAA", "106", "96", "49", "1", "0",
"TGGATTTACTTCATCATGGAAAAAGCCTC", "101", "94", "40", "1", "0",
"AGCCTCAACTGCCTGCAGTGTTGGCTGAT", "78", "68", "49", "1", "0",
"TCAACTGCCTGCAGTGTTGGCTGATGAAG", "74", "64", "42", "1", "0",
"AGGCAGTTGAGGCTTTTTCCATGATGAAG", "93", "83", "49", "1", "0",
"CAGGCAGTTGAGGCTTTTTCCATGATGAA", "92", "82", "39", "1", "0",
"AACACTGCAGGCAGTTGAGGCTTTTTCCA", "85", "79", "41", "1", "0",
"GATGAAGGAAAATAAAATATTAAATGATT", "52", "42", "49", "1", "0",
"TTGTCAGAAGGCTACTTCTGATATTTATG", "8", "0", "47", "1", "0",
"TTTTATTTTCCTTCATCAGCCAACACTGC", "64", "54", "48", "1", "0",
"CATTTAATATTTTATTTTCCTTCATCAGC", "55", "53", "39", "1", "0",
"ATGAAAGCATAAATATCAGAAGTAGCCTT", "15", "9", "44", "1", "0",
"TCATTCTCCATTAATCATTTAATATTTTA", "40", "35", "44", "1", "0",
"ATGCTTTCATTCTCCATTAATCATTTAAT", "34", "30", "43", "1", "0",
"TATTTATGCTTTCATTCTCCATTAATCAT", "29", "27", "41", "1", "0",
"TGATATTTATGCTTTCATTCTCCATTAAT", "26", "16", "49", "1", "0",
"CTGATATTTATGCTTTCATTCTCCATTAA", "25", "15", "39", "1", "0",
"1.07374e+09", "134.858", "0", "winner"]
serialized = '\t'.join(parts)
read = Read(serialized, True)
# When no minimizers are explored we complain with inf
assert read.recompute_adam_cap() == float('inf')
def test_recompute_adam_cap_onemin(self):
# + = q10, ? = q30
# Here's a version where only one minimizer is located
# Best way to hit it will be at the one q10 base.
# --------TTGTCAGAAGGCTACTTCTGATATTTATG----------
parts = ["TTCTGCTGTTGTCAGAAGGCTACTTCTGATATTTATGCTTTCATTCTCCATTAATCATTTAATATTTTATTTTCCTTCATCAGCCAACACTGCAGGCAGTTGAGGCTTTTTCCATGATGAAGTAAATCCAACTATGATTTCTGAAGAATA",
"???????????????????????+??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????",
"1",
"AAAATAAAATATTAAATGATTAATGGAGA", "44", "41", "39", "1", "0",
"GAAATCATAGTTGGATTTACTTCATCATG", "112", "107", "43", "1", "0",
"ATAGTTGGATTTACTTCATCATGGAAAAA", "106", "96", "49", "1", "0",
"TGGATTTACTTCATCATGGAAAAAGCCTC", "101", "94", "40", "1", "0",
"AGCCTCAACTGCCTGCAGTGTTGGCTGAT", "78", "68", "49", "1", "0",
"TCAACTGCCTGCAGTGTTGGCTGATGAAG", "74", "64", "42", "1", "0",
"AGGCAGTTGAGGCTTTTTCCATGATGAAG", "93", "83", "49", "1", "0",
"CAGGCAGTTGAGGCTTTTTCCATGATGAA", "92", "82", "39", "1", "0",
"AACACTGCAGGCAGTTGAGGCTTTTTCCA", "85", "79", "41", "1", "0",
"GATGAAGGAAAATAAAATATTAAATGATT", "52", "42", "49", "1", "0",
"TTGTCAGAAGGCTACTTCTGATATTTATG", "8", "0", "47", "1", "1",
"TTTTATTTTCCTTCATCAGCCAACACTGC", "64", "54", "48", "1", "0",
"CATTTAATATTTTATTTTCCTTCATCAGC", "55", "53", "39", "1", "0",
"ATGAAAGCATAAATATCAGAAGTAGCCTT", "15", "9", "44", "1", "0",
"TCATTCTCCATTAATCATTTAATATTTTA", "40", "35", "44", "1", "0",
"ATGCTTTCATTCTCCATTAATCATTTAAT", "34", "30", "43", "1", "0",
"TATTTATGCTTTCATTCTCCATTAATCAT", "29", "27", "41", "1", "0",
"TGATATTTATGCTTTCATTCTCCATTAAT", "26", "16", "49", "1", "0",
"CTGATATTTATGCTTTCATTCTCCATTAA", "25", "15", "39", "1", "0",
"1.07374e+09", "134.858", "0", "winner"]
serialized = '\t'.join(parts)
read = Read(serialized, True)
recomputed_cap = read.recompute_adam_cap()
# We should be able to just pay the cost of the cheapest base.
assert recomputed_cap == 10, "Cap is {} when it shouldn't be".format(recomputed_cap)
def test_recompute_adam_cap_overlap(self):
# + = q10, 0 = q15, ? = q30,
# Here's a version where only one minimizer is located
# Best way to hit it will be at the shared q15 base.
# --------TTGTCAGAAGGCTACTTCTGATATTTATG----------
# ----------CTGATATTTATGCTTTCATTCTCCATTAA
parts = ["TTCTGCTGTTGTCAGAAGGCTACTTCTGATATTTATGCTTTCATTCTCCATTAATCATTTAATATTTTATTTTCCTTCATCAGCCAACACTGCAGGCAGTTGAGGCTTTTTCCATGATGAAGTAAATCCAACTATGATTTCTGAAGAATA",
"???????????+????????????????0????????????????????+????????????????????????????????????????????????????????????????????????????????????????????????????",
"1",
"AAAATAAAATATTAAATGATTAATGGAGA", "44", "41", "39", "1", "0",
"GAAATCATAGTTGGATTTACTTCATCATG", "112", "107", "43", "1", "0",
"ATAGTTGGATTTACTTCATCATGGAAAAA", "106", "96", "49", "1", "0",
"TGGATTTACTTCATCATGGAAAAAGCCTC", "101", "94", "40", "1", "0",
"AGCCTCAACTGCCTGCAGTGTTGGCTGAT", "78", "68", "49", "1", "0",
"TCAACTGCCTGCAGTGTTGGCTGATGAAG", "74", "64", "42", "1", "0",
"AGGCAGTTGAGGCTTTTTCCATGATGAAG", "93", "83", "49", "1", "0",
"CAGGCAGTTGAGGCTTTTTCCATGATGAA", "92", "82", "39", "1", "0",
"AACACTGCAGGCAGTTGAGGCTTTTTCCA", "85", "79", "41", "1", "0",
"GATGAAGGAAAATAAAATATTAAATGATT", "52", "42", "49", "1", "0",
"TTGTCAGAAGGCTACTTCTGATATTTATG", "8", "0", "47", "1", "1",
"TTTTATTTTCCTTCATCAGCCAACACTGC", "64", "54", "48", "1", "0",
"CATTTAATATTTTATTTTCCTTCATCAGC", "55", "53", "39", "1", "0",
"ATGAAAGCATAAATATCAGAAGTAGCCTT", "15", "9", "44", "1", "0",
"TCATTCTCCATTAATCATTTAATATTTTA", "40", "35", "44", "1", "0",
"ATGCTTTCATTCTCCATTAATCATTTAAT", "34", "30", "43", "1", "0",
"TATTTATGCTTTCATTCTCCATTAATCAT", "29", "27", "41", "1", "0",
"TGATATTTATGCTTTCATTCTCCATTAAT", "26", "16", "49", "1", "0",
"CTGATATTTATGCTTTCATTCTCCATTAA", "25", "15", "39", "1", "1",
"1.07374e+09", "134.858", "0", "winner"]
serialized = '\t'.join(parts)
read = Read(serialized, True)
recomputed_cap = read.recompute_adam_cap()
assert recomputed_cap == 15, "Cap is {} when it shouldn't be".format(recomputed_cap)
def test_recompute_adam_cap_flank(self):
# + = q10, 0 = q15, ? = q30,
# Here's a version where only one minimizer is located
# Best way to hit it will be at the shared q10 base and hope we break minimizer 2.
# Minimizer 2 is beat with probability 0.02730720856393431 by any of 7 candidates in the agglomeration
# --------TTGTCAGAAGGCTACTTCTGATATTTATG----------
# ----------CTGATATTTATGCTTTCATTCTCCATTAA
# 000000000011111111112222222222333333333344444444445555555555
# 012345678901234567890123456789012345678901234567890123456789
parts = ["TTCTGCTGTTGTCAGAAGGCTACTTCTGATATTTATGCTTTCATTCTCCATTAATCATTTAATATTTTATTTTCCTTCATCAGCCAACACTGCAGGCAGTTGAGGCTTTTTCCATGATGAAGTAAATCCAACTATGATTTCTGAAGAATA",
"?????????????????????+????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????",
"1",
"AAAATAAAATATTAAATGATTAATGGAGA", "44", "41", "39", "1", "0",
"GAAATCATAGTTGGATTTACTTCATCATG", "112", "107", "43", "1", "0",
"ATAGTTGGATTTACTTCATCATGGAAAAA", "106", "96", "49", "1", "0",
"TGGATTTACTTCATCATGGAAAAAGCCTC", "101", "94", "40", "1", "0",
"AGCCTCAACTGCCTGCAGTGTTGGCTGAT", "78", "68", "49", "1", "0",
"TCAACTGCCTGCAGTGTTGGCTGATGAAG", "74", "64", "42", "1", "0",
"AGGCAGTTGAGGCTTTTTCCATGATGAAG", "93", "83", "49", "1", "0",
"CAGGCAGTTGAGGCTTTTTCCATGATGAA", "92", "82", "39", "1", "0",
"AACACTGCAGGCAGTTGAGGCTTTTTCCA", "85", "79", "41", "1", "0",
"GATGAAGGAAAATAAAATATTAAATGATT", "52", "42", "49", "1", "0",
"TTGTCAGAAGGCTACTTCTGATATTTATG", "8", "0", "47", "1", "1",
"TTTTATTTTCCTTCATCAGCCAACACTGC", "64", "54", "48", "1", "0",
"CATTTAATATTTTATTTTCCTTCATCAGC", "55", "53", "39", "1", "0",
"ATGAAAGCATAAATATCAGAAGTAGCCTT", "15", "9", "44", "1", "0",
"TCATTCTCCATTAATCATTTAATATTTTA", "40", "35", "44", "1", "0",
"ATGCTTTCATTCTCCATTAATCATTTAAT", "34", "30", "43", "1", "0",
"TATTTATGCTTTCATTCTCCATTAATCAT", "29", "27", "41", "1", "0",
"TGATATTTATGCTTTCATTCTCCATTAAT", "26", "16", "49", "1", "0",
"CTGATATTTATGCTTTCATTCTCCATTAA", "25", "15", "39", "1", "1",
"1.07374e+09", "134.858", "0", "winner"]
serialized = '\t'.join(parts)
read = Read(serialized, True)
recomputed_cap = read.recompute_adam_cap()
right_answer = 10 + -10 * math.log10(1 - ((1 - 0.02730720856393431)**7))
assert recomputed_cap == right_answer, "Cap is {} when it should be {}".format(recomputed_cap, right_answer)
@unittest.skip("Cap is way off in this read due to minimizer probability quantization in vg")
def test_recompute_adam_cap_hard(self):
# This read gets the cap way off somehow
# TODO: work it out
# 11111111111111111111111111111111111111111111111111
# 11111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000000001111111111222222222233333333334444444444
#012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
#TGCGGGCTGGCAATCGGCTCGCCCTCCTTCCAAGACAAGCTGCTGCTCGGCCCCTCTGAGATCCGTGTGCCTTCGGTGGTGCCTGTGCAGACGGTGCGATAGCTAGTCGCGCTCCTGTACAGCGGTTCCCTCGTTGTGGCACAGAGTGAA
#?????????????????????????????????????+????????????????????????????????????????????????????????????'+5????????+???+++????????????+?????????????????????
# ! !
#
#------CTGGCAATCGGCTCGCCCTCCTTCCAAGA----------
# ------TCGGCTCGCCCTCCTTCCAAGACAAGCTG----------
# ----------TCCTTCCAAGACAAGCTGCTGCTCGGCCC---------
# ----------ACAAGCTGCTGCTCGGCCCCTCTGAGATC--
# ----------AGCTGCTGCTCGGCCCCTCTGAGATCCGT----------
# --------TCGGCCCCTCTGAGATCCGTGTGCCTTCG-------
# ----------TCTGAGATCCGTGTGCCTTCGGTGGTGCC---------
# ----------GTGTGCCTTCGGTGGTGCCTGTGCAGACG----------
# These aren't disrupted:
# ---------GGTGGTGCCTGTGCAGACGGTGCGATAGC----------
# --------TGTGCAGACGGTGCGATAGCTAGTCGCGC----------
# -TGCAGACGGTGCGATAGCTAGTCGCGCTC----------
# ----------CGATAGCTAGTCGCGCTCCTGTACAGCGG-----
# ----------CTAGTCGCGCTCCTGTACAGCGGTTCCCT----------
# ---TCGCGCTCCTGTACAGCGGTTCCCTCGTT----------
# -----TCCTGTACAGCGGTTCCCTCGTTGTGGCA-----
# ----------ACAGCGGTTCCCTCGTTGTGGCACAGAGT-
# ----------AGCGGTTCCCTCGTTGTGGCACAGAGTGA-
# Base 34 scores 39.209729
# Base 37:
#DEBUG:capper:At base 37
#DEBUG:capper:Cost to break base: 10.000000
#DEBUG:capper:Overlap agglomeration: window_start: 0 window_length: 7 minimizer_start: 6 minimizer_length: 29 minimizer: TCTTGGAAGGAGGGCGAGCCGATTGCCAG hits: 2 hits_in_extension: 2 hash: 000462e7b41b1caf beat_prob: 6.693035912203998e-05
#DEBUG:capper:Out of minimizer: need to beat with probability 0.000067 and 8 chances for 32.713886 points
#DEBUG:capper:Overlap agglomeration: window_start: 7 window_length: 7 minimizer_start: 13 minimizer_length: 29 minimizer: CAGCTTGTCTTGGAAGGAGGGCGAGCCGA hits: 2 hits_in_extension: 2 hash: 2b647149fc6d160d beat_prob: 0.16950138145732682
#DEBUG:capper:In minimizer: no additional cost
#DEBUG:capper:Overlap agglomeration: window_start: 14 window_length: 10 minimizer_start: 24 minimizer_length: 29 minimizer: GGGCCGAGCAGCAGCTTGTCTTGGAAGGA hits: 2 hits_in_extension: 2 hash: 118da2866b09a303 beat_prob: 0.06856742650692253
#DEBUG:capper:In minimizer: no additional cost
#DEBUG:capper:Overlap agglomeration: window_start: 24 window_length: 3 minimizer_start: 34 minimizer_length: 29 minimizer: GATCTCAGAGGGGCCGAGCAGCAGCTTGT hits: 2 hits_in_extension: 2 hash: 04cc30719faef05a beat_prob: 0.018740680446793116
#DEBUG:capper:In minimizer: no additional cost
#DEBUG:capper:Overlap agglomeration: window_start: 27 window_length: 11 minimizer_start: 37 minimizer_length: 29 minimizer: AGCTGCTGCTCGGCCCCTCTGAGATCCGT hits: 2 hits_in_extension: 2 hash: 040e7fa2a286d1f7 beat_prob: 0.015846230703142866
#DEBUG:capper:In minimizer: no additional cost
#DEBUG:capper:Total cost for best solution ending in this base: 42.713886
# 0.000067 << 1/255 so our quantizing minimizer beat probabilities to 8 bits in the approximation is probably to blame for the mismatch in caps here.
parts = ["TGCGGGCTGGCAATCGGCTCGCCCTCCTTCCAAGACAAGCTGCTGCTCGGCCCCTCTGAGATCCGTGTGCCTTCGGTGGTGCCTGTGCAGACGGTGCGATAGCTAGTCGCGCTCCTGTACAGCGGTTCCCTCGTTGTGGCACAGAGTGAA",
"?????????????????????????????????????+????????????????????????????????????????????????????????????'+5????????+???+++????????????+?????????????????????",
"2",
"AGCTGCTGCTCGGCCCCTCTGAGATCCGT", "37", "27", "49", "2", "2",
"GGCACCACCGAAGGCACACGGATCTCAGA", "54", "44", "48", "2", "2",
"CGAAGGCACACGGATCTCAGAGGGGCCGA", "46", "38", "44", "2", "2",
"GTGTGCCTTCGGTGGTGCCTGTGCAGACG", "64", "54", "49", "2", "2",
"GATCTCAGAGGGGCCGAGCAGCAGCTTGT", "34", "24", "41", "2", "2",
"GGGCCGAGCAGCAGCTTGTCTTGGAAGGA", "24", "14", "48", "2", "2",
"CAGCTTGTCTTGGAAGGAGGGCGAGCCGA", "13", "7", "45", "2", "2",
"TCTTGGAAGGAGGGCGAGCCGATTGCCAG", "6", "0", "45", "2", "2",
"GGTGGTGCCTGTGCAGACGGTGCGATAGC", "74", "65", "48", "0", "0",
"CGATAGCTAGTCGCGCTCCTGTACAGCGG", "96", "86", "44", "0", "0",
"TCGCGCTCCTGTACAGCGGTTCCCTCGTT", "106", "103", "42", "0", "0",
"GCGCGACTAGCTATCGCACCGTCTGCACA", "83", "75", "47", "0", "0",
"TCCTGTACAGCGGTTCCCTCGTTGTGGCA", "112", "107", "39", "0", "0",
"GAGCGCGACTAGCTATCGCACCGTCTGCA", "85", "84", "40", "0", "0",
"ACAGCGGTTCCCTCGTTGTGGCACAGAGT", "118", "108", "40", "0", "0",
"AGGGAACCGCTGTACAGGAGCGCGACTAG", "102", "92", "49", "0", "0",
"TCACTCTGTGCCACAACGAGGGAACCGCT", "120", "110", "40", "0", "0",
"1.50515", "58.0915", "0", "align"]
serialized = '\t'.join(parts)
read = Read(serialized, True)
read.visualize()
recomputed_cap = read.recompute_adam_cap()
right_answer = 58.0915
assert recomputed_cap == right_answer, "Cap is {} when it should be {}".format(recomputed_cap, right_answer)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
unittest.main()