Skip to content

Commit

Permalink
refactor: PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
msto committed Sep 24, 2024
1 parent 5d4cde5 commit 43e4e04
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
21 changes: 11 additions & 10 deletions fgpyo/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,35 +267,36 @@ def suppress_stderr() -> Generator[None, None, None]:

def assert_fasta_is_indexed(
fasta: Path,
faidx: bool = True,
dictionary: bool = True,
bwa: bool = True,
/,
dictionary: bool = False,
bwa: bool = False,
) -> None:
"""
Verify that a FASTA is readable and has the expected index files.
The existence of the FASTA index generated by `samtools faidx` will always be verified. The
existence of the index files generated by `samtools dict` and `bwa index` may be optionally
verified.
Args:
fasta: Path to the FASTA file.
faidx: If True, check for the index file generated by `samtools faidx` (`{fasta}.fai`).
dictionary: If True, check for the index file generated by `samtools dict` (`{fasta}.dict`).
bwa: If True, check for the index files generated by `bwa index` (`{fasta}.{suffix}`, for
all suffixes in [".amb", ".ann", ".bwt", ".pac", ".sa"]).
all suffixes in ["amb", "ann", "bwt", "pac", "sa"]).
Raises:
AssertionError: If the FASTA or any of the expected index files are missing or not readable.
"""
assert_path_is_readable(fasta)

if faidx:
fai_index = Path(f"{fasta}.fai")
assert_path_is_readable(fai_index)
fai_index = Path(f"{fasta}.fai")
assert_path_is_readable(fai_index)

if dictionary:
dict_index = Path(f"{fasta}.dict")
assert_path_is_readable(dict_index)

if bwa:
suffixes = [".amb", ".ann", ".bwt", ".pac", ".sa"]
suffixes = ["amb", "ann", "bwt", "pac", "sa"]
for suffix in suffixes:
bwa_index = Path(f"{fasta}.{suffix}")
assert_path_is_readable(bwa_index)
16 changes: 9 additions & 7 deletions tests/fgpyo/io/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,22 @@ def test_read_and_write_lines(
assert int(next(read_back)) == list_to_write[1]


@pytest.mark.parametrize("faidx", [True, False])
@pytest.mark.parametrize("dictionary", [True, False])
@pytest.mark.parametrize("bwa", [True, False])
def test_assert_fasta_is_indexed(tmp_path: Path, faidx: bool, dictionary: bool, bwa: bool) -> None:
def test_assert_fasta_is_indexed(tmp_path: Path, dictionary: bool, bwa: bool) -> None:
"""assert_fasta_is_indexed should verify the presence of the expected index files."""
fasta = tmp_path / "test.fa"
fasta.touch()

if faidx:
(tmp_path / "test.fa.fai").touch()
(tmp_path / "test.fa.fai").touch()

if dictionary:
(tmp_path / "test.fa.dict").touch()
if bwa:
for suffix in [".amb", ".ann", ".bwt", ".pac", ".sa"]:
(tmp_path / f"test.fa.{suffix}").touch()
(tmp_path / "test.fa.amb").touch()
(tmp_path / "test.fa.ann").touch()
(tmp_path / "test.fa.bwt").touch()
(tmp_path / "test.fa.pac").touch()
(tmp_path / "test.fa.sa").touch()

assert_fasta_is_indexed(fasta, faidx=faidx, dictionary=dictionary, bwa=bwa)
assert_fasta_is_indexed(fasta, dictionary=dictionary, bwa=bwa)

0 comments on commit 43e4e04

Please sign in to comment.