|
| 1 | +""" |
| 2 | +Regression test for fix/contig-variants-check-if-ins. |
| 3 | +
|
| 4 | +check_if_ins was passing the variant object to Insertion.contains() which |
| 5 | +expects an int (a position). This caused the method to always return None |
| 6 | +even when a variant's position falls within an insertion's span. |
| 7 | +""" |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from neat.variants.contig_variants import ContigVariants |
| 11 | +from neat.variants import Insertion, SingleNucleotideVariant |
| 12 | + |
| 13 | + |
| 14 | +_GT = np.array([0, 1]) |
| 15 | + |
| 16 | + |
| 17 | +def _ins(pos, alt="ACGTT", length=4, gt=None): |
| 18 | + return Insertion(pos, length, alt, gt if gt is not None else _GT.copy(), "42") |
| 19 | + |
| 20 | + |
| 21 | +def _snv(pos, alt="T", gt=None): |
| 22 | + return SingleNucleotideVariant(pos, alt, gt if gt is not None else _GT.copy(), "42") |
| 23 | + |
| 24 | + |
| 25 | +def test_check_if_ins_finds_containing_insertion(): |
| 26 | + """SNV inside insertion span is detected.""" |
| 27 | + cv = ContigVariants() |
| 28 | + ins = _ins(10, "ACGTT", 4) |
| 29 | + cv.add_variant(ins) |
| 30 | + snv = _snv(11) # position1=11 is within [10, 14) |
| 31 | + result = cv.check_if_ins(snv) |
| 32 | + assert result is ins |
| 33 | + |
| 34 | + |
| 35 | +def test_check_if_ins_at_insertion_start(): |
| 36 | + """SNV at the insertion's own position is detected.""" |
| 37 | + cv = ContigVariants() |
| 38 | + ins = _ins(10, "ACGTT", 4) |
| 39 | + cv.add_variant(ins) |
| 40 | + snv = _snv(10) |
| 41 | + assert cv.check_if_ins(snv) is ins |
| 42 | + |
| 43 | + |
| 44 | +def test_check_if_ins_at_insertion_end_exclusive(): |
| 45 | + """Position at insertion start + length is outside the span.""" |
| 46 | + cv = ContigVariants() |
| 47 | + ins = _ins(10, "ACGTT", 4) # spans [10, 14) |
| 48 | + cv.add_variant(ins) |
| 49 | + snv = _snv(14) |
| 50 | + assert cv.check_if_ins(snv) is None |
| 51 | + |
| 52 | + |
| 53 | +def test_check_if_ins_outside_span_returns_none(): |
| 54 | + cv = ContigVariants() |
| 55 | + ins = _ins(10, "ACGTT", 4) |
| 56 | + cv.add_variant(ins) |
| 57 | + snv = _snv(50) |
| 58 | + assert cv.check_if_ins(snv) is None |
| 59 | + |
| 60 | + |
| 61 | +def test_check_if_ins_empty_returns_none(): |
| 62 | + cv = ContigVariants() |
| 63 | + assert cv.check_if_ins(_snv(10)) is None |
| 64 | + |
| 65 | + |
| 66 | +def test_check_if_ins_genotype_mismatch_returns_none(): |
| 67 | + """Even if position matches, different genotype means no match.""" |
| 68 | + cv = ContigVariants() |
| 69 | + ins = _ins(10, "ACGTT", 4, gt=np.array([1, 0])) |
| 70 | + cv.add_variant(ins) |
| 71 | + snv = _snv(11, gt=np.array([0, 1])) # different genotype |
| 72 | + assert cv.check_if_ins(snv) is None |
0 commit comments