Skip to content

Commit ab2e633

Browse files
Mike LeeMike Lee
authored andcommitted
adding integration test for bit-get-cov-stats; updated changelog
1 parent bf30e51 commit ab2e633

File tree

6 files changed

+42
-7
lines changed

6 files changed

+42
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
1313
-->
1414

15-
## Not released yet
15+
## v1.13.4 (20-Nov-2025)
16+
1617
### Changed
1718
- updates to `bit-get-cov-stats`
1819
- can start from bam file now in addition to mosdepth per-base.bed.gz (will generate the mosdepth output if starting from bam)
19-
- modularized
20+
- modularized, integration test added
2021
- updates to `bit-check-for-fastq-dup-headers`
2122
- autodetect gzipped or not
2223
- modularized, test added

bit/modules/cov_analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def generate_sliding_bed_file(reference_fasta, sliding_window_size, step_size, o
123123

124124

125125
def run_mosdepth(bam_file, window_bed_path, output_dir):
126-
mosdepth_out_prefix = f"{output_dir}/mosdepth-files/{bam_file[:-4]}"
126+
mosdepth_out_prefix = str(Path(output_dir) / Path(bam_file).stem)
127127
cmd = f"mosdepth --by {window_bed_path} {mosdepth_out_prefix} {bam_file}"
128128
subprocess.run(cmd, shell=True)
129129
mosdepth_regions_file = f"{mosdepth_out_prefix}.regions.bed.gz"

bit/modules/get_cov_stats.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
def get_cov_stats(args):
1717

1818
preflight_checks(args)
19-
2019
refs = parse_refs(args.reference_fastas)
2120

22-
2321
if args.bed:
2422
refs = parse_bed_file(refs, args.bed)
2523
else:
@@ -95,7 +93,7 @@ def parse_bed_file(refs, bed_file):
9593
def run_mosdepth(bam_file, output_prefix):
9694
check_bam_file_is_indexed(bam_file)
9795
mosdepth_output_dir = f"{output_prefix}-mosdepth-files"
98-
mosdepth_prefix = f"{mosdepth_output_dir}/{bam_file[:-4]}"
96+
mosdepth_prefix = str(Path(mosdepth_output_dir) / Path(bam_file).stem)
9997
os.makedirs(mosdepth_output_dir, exist_ok=True)
10098
cmd = f"mosdepth {mosdepth_prefix} {bam_file}"
10199
print(f"\n {Fore.YELLOW}Running mosdepth to generate the required per-base coverage file...")
@@ -115,7 +113,7 @@ def check_bam_file_is_indexed(bam_file):
115113
def generate_output(refs, output_prefix):
116114
primary_out_path = f"{output_prefix}.tsv"
117115
with open(primary_out_path, "w") as f:
118-
f.write("Ref\tDetection\tDetection_at_10x\tAverage_coverage\n")
116+
f.write("ref\tdetection\tdetection_at_10x\taverage_coverage\n")
119117
for ref in refs:
120118
detection, detection_at_10x, average_coverage = ref.compute_metrics()
121119
f.write(f"{ref.path}\t{detection}\t{detection_at_10x}\t{average_coverage}\n")

bit/tests/data/ez-screen.bam

2.22 KB
Binary file not shown.

bit/tests/data/ez-screen.bam.bai

96 Bytes
Binary file not shown.

bit/tests/test_get_cov_stats.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
from pathlib import Path
3+
import pytest
4+
from bit.modules.general import get_package_path
5+
from bit.tests.utils import run_cli
6+
7+
test_fasta_path = get_package_path("tests/data/ez-screen-assembly.fasta")
8+
test_bam_path = get_package_path("tests/data/ez-screen.bam")
9+
10+
def test_get_cov_stats(tmp_path):
11+
out_prefix = tmp_path / "cov-stats"
12+
13+
cmd = [
14+
"bit-get-cov-stats",
15+
"-r", str(test_fasta_path),
16+
"-b", str(test_bam_path),
17+
"-o", str(out_prefix),
18+
]
19+
20+
run_cli(cmd)
21+
22+
summary_tsv = Path(f"{out_prefix}.tsv")
23+
assert summary_tsv.exists(), f"Coverage stats TSV not found at {summary_tsv}"
24+
25+
observed = summary_tsv.read_text().splitlines()
26+
expected_header = "ref\tdetection\tdetection_at_10x\taverage_coverage"
27+
assert observed[0] == expected_header, f"Header does not match expected:\n{observed[0]}\nExpected:\n{expected_header}"
28+
29+
fields = observed[1].split("\t")
30+
assert len(fields) == 4, f"Unexpected number of columns:\n{fields}"
31+
32+
ref, detection, detection10x, avg_cov = fields
33+
assert ref.endswith("ez-screen-assembly.fasta"), f"Unexpected ref path: {ref}"
34+
assert float(detection) == pytest.approx(0.8714, rel=1e-3)
35+
assert float(detection10x) == pytest.approx(0.0, rel=1e-1)
36+
assert float(avg_cov) == pytest.approx(1.9934, rel=1e-1)

0 commit comments

Comments
 (0)