Skip to content

Commit

Permalink
chore: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clintval committed Oct 4, 2024
1 parent c45dc09 commit dbcd145
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions prymer/offtarget/bwa.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def map_all(self, queries: list[Query]) -> list[BwaResult]:
for query in queries:
# get the next alignment and convert to a result
line: str = next(self._subprocess.stdout).strip()
assert not line.startswith("@"), f"SAM record must not start with '@'! {line}"
alignment = AlignedSegment.fromstring(line, self.header)
results.append(self._to_result(query=query, rec=alignment))

Expand Down
18 changes: 18 additions & 0 deletions tests/offtarget/test_bwa.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from dataclasses import replace
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import TypeAlias
from typing import cast

import pytest
from fgpyo.sam import Cigar
Expand All @@ -12,6 +14,9 @@
from prymer.offtarget.bwa import BwaHit
from prymer.offtarget.bwa import Query

SamHeaderType: TypeAlias = dict[str, dict[str, str] | list[dict[str, str]]]
"""A type alias for a SAM sequence header dictionary."""


@pytest.mark.parametrize("bases", [None, ""])
def test_query_no_bases(bases: str | None) -> None:
Expand Down Expand Up @@ -68,6 +73,19 @@ def test_hit_build_rc() -> None:
assert hit_r.negative is True


def test_header_is_properly_constructed(ref_fasta: Path) -> None:
"""Tests that bwa will return a properly constructed header."""
with BwaAlnInteractive(ref=ref_fasta, max_hits=1) as bwa:
header: SamHeaderType = bwa.header.to_dict()
assert set(header.keys()) == {"HD", "SQ", "PG"}
assert header["HD"] == {"GO": "query", "SO": "unsorted", "VN": "1.5"}
assert header["SQ"] == [{"LN": 10001, "SN": "chr1"}]
assert len(header["PG"]) == 1
program_group: dict[str, str] = cast(list[dict[str, str]], header["PG"])[0]
program_group["ID"] = "bwa"
program_group["PN"] = "bwa"


def test_map_one_uniquely_mapped(ref_fasta: Path) -> None:
"""Tests that bwa maps one hit when a query uniquely maps."""
query = Query(bases="TCTACTAAAAATACAAAAAATTAGCTGGGCATGATGGCATGCACCTGTAATCCCGCTACT", id="NA")
Expand Down

0 comments on commit dbcd145

Please sign in to comment.