Skip to content

Demonstrates that max-hits can be specified with values greater than a short size #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pybwa/libbwaindex.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ cdef extern from "bntseq.h":
void bns_destroy(bntseq_t *bns)

cdef bytes force_bytes(object s)
cdef bytes force_bytes_with(object s, encoding: str | None = *, errors: str | None = *)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why this doesn't work anymore, but fixing to get it to compile for this PR

cpdef bint _set_bwa_idx_verbosity(int level)

cdef class BwaIndex:
Expand Down
8 changes: 1 addition & 7 deletions pybwa/libbwaindex.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,13 @@ cdef str TEXT_ENCODING = 'utf-8'


cdef bytes force_bytes(object s):
return force_bytes_with(s, None, None)


cdef bytes force_bytes_with(
object s, encoding: str | None = None, errors: str | None = None
): # pragma: no cover
"""convert string or unicode object to bytes, assuming utf8 encoding."""
if s is None:
return None
elif PyBytes_Check(s):
return s
elif PyUnicode_Check(s):
return s.encode(encoding or TEXT_ENCODING, errors or ERROR_HANDLER)
return s.encode(TEXT_ENCODING, ERROR_HANDLER)
else:
raise TypeError("Argument must be a string, bytes or unicode.")

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ sphinx-rtd-theme = ">=3.0.2"
sphinx-autodoc-typehints = "2"

[build-system]
requires = ["poetry-core>=2.1.1", "cython>=3.0.11", "setuptools>=75.1.0", "pysam >=0.22.1"]
requires = ["poetry-core>=2.1.1", "cython>=3.0.11", "setuptools>=75.1.0", "pysam ==0.22.1"]
build-backend = "poetry.core.masonry.api"

[tool.poe]
Expand Down
12 changes: 6 additions & 6 deletions tests/_test_libbwaindex.pyx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# cython: language_level=3

from pybwa.libbwaindex cimport force_bytes_with
from pybwa.libbwaindex cimport force_bytes
import pytest


def test_force_bytes_with() -> None:
assert force_bytes_with(None) is None
assert force_bytes_with(b"foo-bar") == b"foo-bar"
assert force_bytes_with("foo-bar") == b"foo-bar"
def test_force_bytes() -> None:
assert force_bytes(None) is None
assert force_bytes(b"foo-bar") == b"foo-bar"
assert force_bytes("foo-bar") == b"foo-bar"
with pytest.raises(TypeError, match="Argument must be a string, bytes or unicode"):
force_bytes_with(42)
force_bytes(42)
4 changes: 2 additions & 2 deletions tests/test_libbwaindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import_test_lib(libname="libbwaindex")


def test_force_bytes_with() -> None:
def test_force_bytes() -> None:
import _test_libbwaindex

_test_libbwaindex.test_force_bytes_with()
_test_libbwaindex.test_force_bytes()


@pytest.mark.parametrize("in_place", [True, False])
Expand Down
24 changes: 24 additions & 0 deletions tests/test_libbwaln.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,27 @@ def test_bwa_aln_ambiguous_bases(num_amb: int, tmp_path_factory: pytest.TempPath
else:
assert rec.has_tag("XN"), str(rec)
assert rec.get_tag("XN") == num_amb


@pytest.mark.parametrize("limit", [1, 32767, 32768, 100000])
def test_bwa_aln_max_hits(limit: int, tmp_path_factory: pytest.TempPathFactory) -> None:
query = "G" * 25
target = ("A" * 25) + query + ("T" * 25)

src_dir = Path(str(tmp_path_factory.mktemp("test_bwa_aln_max_hits")))
fasta = src_dir / "ref.fasta"
with fasta.open("w") as writer:
writer.write(">ref\n")
for _ in range(limit):
writer.write(f"{target}\n")
BwaIndex.index(fasta=fasta)

bwa = BwaAln(prefix=fasta)
queries = [FastxRecord(name="NA", sequence=query)]
options = BwaAlnOptions(seed_length=len(query), max_mismatches=0, max_hits=limit)
recs = bwa.align(queries=queries, opt=options)
assert len(recs) == 1
rec = recs[0]
print(rec)
assert rec.is_unmapped is False
assert rec.get_tag("HN") == limit, f"limit was {limit}"
Loading