|
| 1 | +""" |
| 2 | +Tests for the single-file filtering functions: |
| 3 | +filterSAMbyIdentity and filterSAMbyPercentMatched. |
| 4 | +""" |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from filtersam.filtersam import filterSAMbyIdentity, filterSAMbyPercentMatched |
| 11 | + |
| 12 | +from conftest import IDENTITY_KEPT, MATCHED_KEPT, detect_format, read_segment_names |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize("cutoff", sorted(IDENTITY_KEPT)) |
| 16 | +def test_filter_by_identity_keeps_expected(sam_path, tmp_path, cutoff): |
| 17 | + out = tmp_path / "out.sam" |
| 18 | + filterSAMbyIdentity(sam_path, out, identity_cutoff=cutoff) |
| 19 | + assert read_segment_names(out) == IDENTITY_KEPT[cutoff] |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize("cutoff", sorted(MATCHED_KEPT)) |
| 23 | +def test_filter_by_matched_keeps_expected(sam_path, tmp_path, cutoff): |
| 24 | + out = tmp_path / "out.sam" |
| 25 | + filterSAMbyPercentMatched(sam_path, out, matched_cutoff=cutoff) |
| 26 | + assert read_segment_names(out) == MATCHED_KEPT[cutoff] |
| 27 | + |
| 28 | + |
| 29 | +def test_segments_without_md_tag_are_always_dropped(sam_path, tmp_path): |
| 30 | + # Cutoff 0 keeps everything that has an MD tag, but never the MD-less read. |
| 31 | + out = tmp_path / "out.sam" |
| 32 | + filterSAMbyIdentity(sam_path, out, identity_cutoff=0.0) |
| 33 | + assert "read_nomd" not in read_segment_names(out) |
| 34 | + |
| 35 | + |
| 36 | +def test_works_on_bam_input(bam_path, tmp_path): |
| 37 | + out = tmp_path / "out.bam" |
| 38 | + filterSAMbyIdentity(bam_path, out, identity_cutoff=95.0) |
| 39 | + assert read_segment_names(out) == IDENTITY_KEPT[95.0] |
| 40 | + |
| 41 | + |
| 42 | +# --- Output format selection (regression guard for the single-process BAM fix) --- |
| 43 | + |
| 44 | +def test_output_sam_is_text(sam_path, tmp_path): |
| 45 | + out = tmp_path / "out.sam" |
| 46 | + filterSAMbyIdentity(sam_path, out, identity_cutoff=95.0) |
| 47 | + assert detect_format(out) == "sam" |
| 48 | + |
| 49 | + |
| 50 | +def test_output_bam_is_binary(sam_path, tmp_path): |
| 51 | + out = tmp_path / "out.bam" |
| 52 | + filterSAMbyIdentity(sam_path, out, identity_cutoff=95.0) |
| 53 | + assert detect_format(out) == "bam" |
| 54 | + |
| 55 | + |
| 56 | +def test_output_format_follows_output_extension_not_input(bam_path, tmp_path): |
| 57 | + # BAM input but a .sam output request must yield text SAM. |
| 58 | + out = tmp_path / "out.sam" |
| 59 | + filterSAMbyIdentity(bam_path, out, identity_cutoff=95.0) |
| 60 | + assert detect_format(out) == "sam" |
| 61 | + assert read_segment_names(out) == IDENTITY_KEPT[95.0] |
| 62 | + |
| 63 | + |
| 64 | +# --- Default output path naming (regression guard for the suffix fix) --- |
| 65 | + |
| 66 | +def test_default_output_path_naming(tmp_path): |
| 67 | + # A filename containing 'sam' before the extension used to break the old |
| 68 | + # regex-based extension detection; the suffix-based logic handles it. |
| 69 | + src = tmp_path / "mysample.bam" |
| 70 | + # Build a tiny BAM from the SAM fixture text. |
| 71 | + from conftest import SAM_TEXT |
| 72 | + import pysam |
| 73 | + sam = tmp_path / "_seed.sam" |
| 74 | + sam.write_text(SAM_TEXT) |
| 75 | + save = pysam.set_verbosity(0) |
| 76 | + with pysam.AlignmentFile(str(sam), "r") as s, \ |
| 77 | + pysam.AlignmentFile(str(src), "wb", template=s) as d: |
| 78 | + for seg in s: |
| 79 | + d.write(seg) |
| 80 | + pysam.set_verbosity(save) |
| 81 | + |
| 82 | + filterSAMbyIdentity(src, identity_cutoff=95.0) |
| 83 | + |
| 84 | + expected = tmp_path / "mysample.identity_filtered_at_95.0.bam" |
| 85 | + assert expected.is_file() |
| 86 | + assert detect_format(expected) == "bam" |
| 87 | + assert read_segment_names(expected) == IDENTITY_KEPT[95.0] |
0 commit comments