forked from framazan/bwtandem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_imperfect_repeats.py
More file actions
286 lines (235 loc) · 9.07 KB
/
test_imperfect_repeats.py
File metadata and controls
286 lines (235 loc) · 9.07 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
#!/usr/bin/env python3
"""
Test script for imperfect repeat detection.
Creates synthetic test cases and validates the implementation.
"""
import tempfile
import os
from bwt import TandemRepeatFinder, MotifUtils
def create_test_fasta(sequences, filename):
"""Create a test FASTA file."""
with open(filename, 'w') as f:
for name, seq in sequences.items():
f.write(f">{name}\n")
# Write in 80-char lines
for i in range(0, len(seq), 80):
f.write(seq[i:i+80] + "\n")
def test_perfect_repeat():
"""Test 1: Perfect tandem repeat (exact matches)."""
print("Test 1: Perfect tandem repeat")
print("-" * 60)
# Create perfect repeat: ATCG repeated 10 times
seq = "ATCG" * 10 + "$"
with tempfile.NamedTemporaryFile(mode='w', suffix='.fa', delete=False) as f:
f.write(">test_perfect\n")
f.write(seq + "\n")
temp_fa = f.name
with tempfile.NamedTemporaryFile(mode='w', suffix='.bed', delete=False) as f:
temp_out = f.name
try:
finder = TandemRepeatFinder(
temp_fa,
allow_mismatches=True,
max_motif_length=6,
min_copies=3
)
sequences = finder.load_reference()
finder.build_indices(sequences)
repeats = finder.find_tandem_repeats(enable_tier1=True, enable_tier2=False)
print(f" Input: {'ATCG' * 10}")
print(f" Found {len(repeats)} repeat(s)")
if repeats:
r = repeats[0]
print(f" Motif: {r.motif}")
print(f" Consensus: {r.consensus_motif}")
print(f" Copies: {r.copies}")
print(f" Mismatch rate: {r.mismatch_rate:.3f}")
print(f" Strand: {r.strand}")
assert r.copies >= 9, f"Expected ≥9 copies, got {r.copies}"
assert r.mismatch_rate == 0.0, f"Expected 0 mismatches, got {r.mismatch_rate}"
print(" ✓ PASSED")
else:
print(" ✗ FAILED: No repeats found")
finally:
os.unlink(temp_fa)
os.unlink(temp_out)
print()
def test_imperfect_repeat_single_mismatch():
"""Test 2: Imperfect repeat with single mismatch per copy."""
print("Test 2: Imperfect repeat (single mismatch)")
print("-" * 60)
# Create imperfect repeat: ATCG with one base changed in middle copies
seq = "ATCG" + "ATCG" + "ATGG" + "ATCG" + "ATCG" + "$"
with tempfile.NamedTemporaryFile(mode='w', suffix='.fa', delete=False) as f:
f.write(">test_imperfect\n")
f.write(seq + "\n")
temp_fa = f.name
with tempfile.NamedTemporaryFile(mode='w', suffix='.bed', delete=False) as f:
temp_out = f.name
try:
finder = TandemRepeatFinder(
temp_fa,
allow_mismatches=True,
max_motif_length=6,
min_copies=3
)
sequences = finder.load_reference()
finder.build_indices(sequences)
repeats = finder.find_tandem_repeats(enable_tier1=True, enable_tier2=False)
print(f" Input: ATCG + ATCG + ATGG + ATCG + ATCG (1 mismatch in copy 3)")
print(f" Found {len(repeats)} repeat(s)")
if repeats:
r = repeats[0]
print(f" Motif: {r.motif}")
print(f" Consensus: {r.consensus_motif}")
print(f" Copies: {r.copies}")
print(f" Mismatch rate: {r.mismatch_rate:.3f}")
print(f" Max MM per copy: {r.max_mismatches_per_copy}")
assert r.copies >= 4, f"Expected ≥4 copies, got {r.copies}"
assert r.mismatch_rate > 0, f"Expected mismatches, got {r.mismatch_rate}"
assert r.max_mismatches_per_copy == 1, f"Expected max 1 MM, got {r.max_mismatches_per_copy}"
print(" ✓ PASSED")
else:
print(" ✗ FAILED: No repeats found")
finally:
os.unlink(temp_fa)
os.unlink(temp_out)
print()
def test_low_complexity_filter():
"""Test 3: Low-complexity sequence should be filtered."""
print("Test 3: Low-complexity filter")
print("-" * 60)
# Create homopolymer: AAAA repeated
seq = "A" * 40 + "$"
with tempfile.NamedTemporaryFile(mode='w', suffix='.fa', delete=False) as f:
f.write(">test_lowcomp\n")
f.write(seq + "\n")
temp_fa = f.name
with tempfile.NamedTemporaryFile(mode='w', suffix='.bed', delete=False) as f:
temp_out = f.name
try:
finder = TandemRepeatFinder(
temp_fa,
allow_mismatches=True,
max_motif_length=6,
min_copies=3,
min_entropy=1.0 # Should filter homopolymer
)
sequences = finder.load_reference()
finder.build_indices(sequences)
repeats = finder.find_tandem_repeats(enable_tier1=True, enable_tier2=False)
print(f" Input: {'A' * 40} (homopolymer)")
print(f" Entropy: {MotifUtils.calculate_entropy('A'):.2f} bits")
print(f" Found {len(repeats)} repeat(s)")
# Should find very few or no repeats due to entropy filter
if len(repeats) == 0:
print(" ✓ PASSED: Low-complexity sequence filtered")
else:
print(f" ⚠ WARNING: Found {len(repeats)} repeats (may be expected for some motifs)")
finally:
os.unlink(temp_fa)
os.unlink(temp_out)
print()
def test_strand_canonicalization():
"""Test 4: Strand-aware canonicalization."""
print("Test 4: Strand canonicalization")
print("-" * 60)
# Test reverse complement
seq = "ATCG"
rc = MotifUtils.reverse_complement(seq)
print(f" {seq} → RC: {rc}")
assert rc == "CGAT", f"Expected CGAT, got {rc}"
# Test canonical motif selection
motif1 = "ATCG"
canonical1, strand1 = MotifUtils.get_canonical_motif_stranded(motif1)
print(f" {motif1} → Canonical: {canonical1} (strand {strand1})")
motif2 = "CGAT" # Reverse complement of ATCG
canonical2, strand2 = MotifUtils.get_canonical_motif_stranded(motif2)
print(f" {motif2} → Canonical: {canonical2} (strand {strand2})")
# Both should yield the same canonical motif
assert canonical1 == canonical2, f"Expected same canonical: {canonical1} vs {canonical2}"
print(" ✓ PASSED")
print()
def test_hamming_distance():
"""Test 5: Hamming distance calculation."""
print("Test 5: Hamming distance")
print("-" * 60)
tests = [
("ATCG", "ATCG", 0),
("ATCG", "ATGG", 1),
("ATCG", "GGGG", 3),
("AAAA", "TTTT", 4),
]
for s1, s2, expected in tests:
dist = MotifUtils.hamming_distance(s1, s2)
print(f" {s1} vs {s2}: {dist} (expected {expected})")
assert dist == expected, f"Expected {expected}, got {dist}"
print(" ✓ PASSED")
print()
def test_consensus_building():
"""Test 6: Consensus motif building."""
print("Test 6: Consensus building")
print("-" * 60)
# Test with perfect agreement
copies = ["ATCG", "ATCG", "ATCG"]
consensus, mm_rate = MotifUtils.build_consensus_motif(copies)
print(f" Perfect: {copies}")
print(f" Consensus: {consensus}, MM rate: {mm_rate:.3f}")
assert consensus == "ATCG", f"Expected ATCG, got {consensus}"
assert mm_rate == 0.0, f"Expected 0 MM rate, got {mm_rate}"
# Test with majority vote
copies = ["ATCG", "ATCG", "ATGG"]
consensus, mm_rate = MotifUtils.build_consensus_motif(copies)
print(f" Majority: {copies}")
print(f" Consensus: {consensus}, MM rate: {mm_rate:.3f}")
assert consensus == "ATCG", f"Expected ATCG, got {consensus}"
assert mm_rate > 0, f"Expected >0 MM rate, got {mm_rate}"
print(" ✓ PASSED")
print()
def test_entropy_calculation():
"""Test 7: Shannon entropy calculation."""
print("Test 7: Entropy calculation")
print("-" * 60)
tests = [
("AAAA", 0.0, "homopolymer"),
("ATAT", 1.0, "dinucleotide"),
("ATCG", 2.0, "all different"),
("AAAAAAAAAT", None, "low complexity"),
]
for seq, expected, desc in tests:
entropy = MotifUtils.calculate_entropy(seq)
if expected is not None:
print(f" {seq:15} entropy: {entropy:.2f} bits (expected ~{expected:.1f}) - {desc}")
assert abs(entropy - expected) < 0.1, f"Expected ~{expected}, got {entropy}"
else:
print(f" {seq:15} entropy: {entropy:.2f} bits - {desc}")
print(" ✓ PASSED")
print()
def main():
"""Run all tests."""
print("=" * 60)
print("Testing Imperfect Repeat Detection")
print("=" * 60)
print()
try:
test_hamming_distance()
test_entropy_calculation()
test_consensus_building()
test_strand_canonicalization()
test_perfect_repeat()
test_imperfect_repeat_single_mismatch()
test_low_complexity_filter()
print("=" * 60)
print("All tests completed successfully! ✓")
print("=" * 60)
except AssertionError as e:
print(f"\n✗ TEST FAILED: {e}")
return 1
except Exception as e:
print(f"\n✗ ERROR: {e}")
import traceback
traceback.print_exc()
return 1
return 0
if __name__ == "__main__":
exit(main())