Skip to content

Commit

Permalink
Error msg for mismatched read counts, update log output for stdout, test
Browse files Browse the repository at this point in the history
  • Loading branch information
bede committed Dec 16, 2024
1 parent b5ccc77 commit 73c1cf7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
26 changes: 20 additions & 6 deletions src/hostile/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def gather_stats(
aligner: str,
invert: bool,
index: str,
stdout: bool,
) -> list[dict[str, str | int | float | list[str]]]:
stats = []
for fastq1 in fastqs:
Expand All @@ -64,7 +65,12 @@ def gather_stats(
proportion_removed = float(0)
options = [
k
for k, v in {"rename": rename, "reorder": reorder, "invert": invert}.items()
for k, v in {
"invert": invert,
"rename": rename,
"reorder": reorder,
"stdout": stdout,
}.items()
if v
]
report = SampleReport(
Expand Down Expand Up @@ -93,6 +99,7 @@ def gather_stats_paired(
aligner: str,
index: str,
invert: bool,
stdout: bool,
) -> list[dict[str, str | int | float]]:
stats = []
for fastq1, fastq2 in fastqs:
Expand All @@ -113,7 +120,12 @@ def gather_stats_paired(
proportion_removed = float(0)
options = [
k
for k, v in {"rename": rename, "reorder": reorder, "invert": invert}.items()
for k, v in {
"invert": invert,
"rename": rename,
"reorder": reorder,
"stdout": stdout,
}.items()
if v
]
stats.append(
Expand All @@ -126,10 +138,10 @@ def gather_stats_paired(
fastq2_in_name=fastq2.name,
fastq1_in_path=str(fastq1),
fastq2_in_path=str(fastq2),
fastq1_out_name=fastq1_out_path.name,
fastq2_out_name=fastq2_out_path.name,
fastq1_out_path=str(fastq1_out_path),
fastq2_out_path=str(fastq2_out_path),
fastq1_out_name=fastq1_out_path.name if not stdout else None,
fastq2_out_name=fastq2_out_path.name if not stdout else None,
fastq1_out_path=str(fastq1_out_path) if not stdout else None,
fastq2_out_path=str(fastq2_out_path) if not stdout else None,
reads_in=n_reads_in,
reads_out=n_reads_out,
reads_removed=n_reads_removed,
Expand Down Expand Up @@ -191,6 +203,7 @@ def clean_fastqs(
aligner=aligner.name,
index=index,
invert=invert,
stdout=stdout,
)
util.fix_empty_fastqs(stats)
logging.info("Cleaning complete")
Expand Down Expand Up @@ -256,6 +269,7 @@ def clean_paired_fastqs(
aligner=aligner.name,
index=index,
invert=invert,
stdout=stdout,
)
util.fix_empty_fastqs(stats)
logging.info("Cleaning complete")
Expand Down
4 changes: 3 additions & 1 deletion src/hostile/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def handle_alignment_exceptions(exception: subprocess.CalledProcessError) -> Non
logging.debug(f"stderr: {exception.stderr}")
alignment_successful = False
stream_empty = False
if "Error, fewer reads in file specified" in exception.stderr: # Bowtie2
raise RuntimeError("fastq1 and fastq2 contain different numbers of reads")
if 'Failed to read header for "-"' in exception.stderr:
stream_empty = True
if "overall alignment rate" in exception.stderr: # Bowtie2
Expand All @@ -83,7 +85,7 @@ def handle_alignment_exceptions(exception: subprocess.CalledProcessError) -> Non
pass
else:
logging.error(
f"Hostile encountered a problem. Check available RAM and storage\n"
f"Hostile encountered a problem. Details below\n"
f"pipeline stdout:\n{exception.stdout}\n"
f"pipeline stderr:\n{exception.stderr}\n"
)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,9 @@ def test_stats_options(tmp_path):
rename=True,
reorder=True,
force=True,
stdout=True,
)
assert ["rename", "reorder", "invert"] == stats[0]["options"]
assert {"rename", "reorder", "invert", "stdout"} == set(stats[0]["options"])


def test_fixing_empty_fastqs_single(tmp_path):
Expand Down Expand Up @@ -690,18 +691,17 @@ def test_fixing_empty_fastqs_paired(tmp_path):

def test_mismatched_number_of_reads_bowtie2(tmp_path):
"""This has caused sinister errors in the wild, yet is handled gracefully here"""
with pytest.raises(subprocess.CalledProcessError):
with pytest.raises(RuntimeError):
lib.clean_paired_fastqs(
fastqs=[
(
data_dir / "sars-cov-2_100_1.fastq.gz",
data_dir / "sars-cov-2_50_2.fastq.gz",
data_dir / "sars-cov-2_1_2.fastq",
),
],
aligner=lib.ALIGNER.bowtie2,
index=data_dir / "sars-cov-2/sars-cov-2",
out_dir=tmp_path,
force=True,
stdout=True,
)


Expand Down

0 comments on commit 73c1cf7

Please sign in to comment.