[Bug]: deeptools validate_files reports a correctly indexed BAM as unindexed when a parent directory name contains .bam
What happened
check_bam_index() in skills/deeptools/scripts/validate_files.py looks for the samtools sibling-index convention with:
bai_file2 = bam_file.replace(".bam", ".bai")str.replace is global, not suffix-anchored, so any path containing .bam before the filename produces a probe against a path that does not exist.
What you expected instead
The sibling index chr1.bai next to chr1.bam to be found.
Steps to reproduce
A stage directory named align.bam is enough — which is a realistic name for alignment output:
import importlib.util, tempfile
from pathlib import Path
spec = importlib.util.spec_from_file_location(
"validate_files", "skills/deeptools/scripts/validate_files.py")
vf = importlib.util.module_from_spec(spec); spec.loader.exec_module(vf)
stage = Path(tempfile.mkdtemp()) / "deeptools2" / "align.bam"
stage.mkdir(parents=True)
bam = stage / "chr1.bam"
bam.write_bytes(b"BAM\1")
(stage / "chr1.bai").write_bytes(b"BAI\1") # the real, correct index
print(vf.check_bam_index(str(bam)))Error output
(False, '\u2717 BAM index missing for: <tmp>/deeptools2/align.bam/chr1.bam
Run: samtools index <tmp>/deeptools2/align.bam/chr1.bam')The two probes it actually tries:
probe 1 (bam + ".bai") <tmp>/deeptools2/align.bam/chr1.bam.bai does not exist
probe 2 (.replace) <tmp>/deeptools2/align.bai/chr1.bai does not existThe BAM is fully indexed, yet the script exits 1 and tells the user to run samtools index on a file that is already indexed. Note the second probe rewrote the directory component, not the filename suffix.
A doubled suffix breaks the same way: x.bam.bam became x.bai.bam.
Why it was not caught
test_bam_index_is_found_under_either_naming_convention builds its fixture in a tempfile root whose own name never contains .bam, so the two conventions it does test both resolve correctly there.
A second, separate problem noticed while reproducing this
validate_files.py prints \u2713 / \u2717 and \u2014 status glyphs. On a stock Windows console (sys.stdout.encoding == 'cp1252') the first print of such a line raises UnicodeEncodeError, so the tool cannot report anything at all, including on the success path. I hit this while printing the message above:
UnicodeEncodeError: 'charmap' codec can't encode character '\u2717' in position 0It does not reproduce with PYTHONIOENCODING=utf-8 or on Linux. I am reporting it here rather than fixing it in the same change, because the suffix fix is independent of console encoding and I would rather not mix a portability change into a path-handling fix.
Environment
- Agent host: n/a (ran the script directly)
- Model: n/a
- Repository version or commit:
065b734(main) - Python version: 3.12.10
- Operating system: Windows 10
pathlib.Path is already imported in that file for the other checks, and Path.with_suffix() rewrites only the final suffix, which is the convention the function is trying to express. A fix is proposed alongside that adds regression tests for both the align.bam/ directory case and the doubled-suffix case.
Source: K-Dense-AI/scientific-agent-skills