Skip to content

Auto-detect BAM lists and directories; fix CRAM decoding; clearer input errors (2.1.2) - #5

Merged
ishinder merged 9 commits into
mainfrom
fix-bam-list-warning
Nov 8, 2026
Merged

ishinder merged 9 commits into
mainfrom
fix-bam-list-warning

Conversation

@ishinder

@ishinder ishinder commented Nov 8, 2026

Copy link
Copy Markdown
Owner

Fixes #4.

The issue

When --bam was given a text file of BAM paths, 2.1.0 printed:

Warning: 'bam_list.txt' was auto-detected as a list of file paths. This is deprecated; pass --bam_list to declare a list file explicitly.

--bam_list is a flag (--bam bam_list.txt --bam_list), but the message reads like --bam_list bam_list.txt. That form fails with The following argument was not expected, so the flag looked like it didn't exist.

Changes

List files no longer warn. A text file of paths is detected from its contents (each line must be an existing file), whatever it is named. That is the supported way to pass several BAMs. --bam_list/--bed_list keep their 2.1.x behavior (skip auto-detection and read the argument as a list) but are optional; their help text now says so.

No breaking changes. Commands written for 2.1.0/2.1.1 run unchanged. Single BAM, BAM list with and without --bam_list, BED, BED list with and without --bed_list, and GTF all give byte-identical removed/kept junction BEDs to v2.1.0. Output changes only where 2.1.x output was wrong: CRAM inputs that gave 0 junctions or crashed, and inputs with colliding sample names, which now error instead of silently overwriting.

--bam accepts a directory. Every BAM/CRAM/SAM file directly inside it is processed in one run, sorted by filename. Files are chosen by htslib's format detection (hts_detect_format2), not by extension. Index files (.bai, .csi), gzipped FASTA and other files can sit alongside them; hidden files and subdirectories are ignored. Before, a directory was silently read as an empty path list: EASTR wrote empty output and exited 0. --bed <dir> gives a clear error.

Input/output collisions are rejected up front:

  • If two inputs share a filename stem (a/S1.bam, b/S1.cram), EASTR gives both the same sample name and their outputs overwrite each other. This is now an error, for lists and directories.
  • --bam <dir> with --out_filtered_bam pointing at that same directory is now an error. Before, a re-run picked up EASTR's own *_EASTR_filtered.bam files as extra samples and silently changed the pooled read counts.

CRAM is decoded with -r. The reference was never passed to htslib. If the reference path stored in a CRAM's header (UR:) wasn't reachable, for example because the CRAM was made on another machine, every slice failed to decode with Unable to fetch reference. EASTR then reported zero junctions while exiting 0. A new open_alignment() helper sets the reference (hts_set_fai_filename) at all 7 places that open an input file for reading.

CRAM 3.1 crash on arm64 macOS fixed. With 2+ CRAM 3.1 inputs and -p > 1, EASTR crashed (SIGSEGV/SIGBUS) or a sample silently got 0 junctions. This also happens in 2.1.0. htscodecs' NEON rANS-Nx16 order-1 decoder keeps ~1.25 MiB of tables on the stack, more than the 512 KiB macOS gives std::thread/std::async threads. open_alignment() now always gives CRAM inputs an htslib thread pool, so decoding runs on htslib's own threads (stacks of at least 3 MiB). x86_64 and CRAM 3.0 were not affected.

Clearer errors for unrecognized input. These replace File not found: <entire first line> and, for lines longer than the filesystem's name limit, an uncaught filesystem error ... File name too long:

Input Message (abridged)
SAM without a header (.sam, .dat, …) looks like a SAM file without a header … samtools view -h …
.bam/.cram without a valid signature has a .bam extension but is not a valid BAM file … truncated, corrupt …
--bam_list on an actual BAM, or on a directory is an alignment file, not a list of paths. Drop --bam_list …
a list whose first path is wrong is not a BAM/CRAM/SAM file, and its first line is not an existing file path … First line: /data/missing.bam

The headerless-SAM case is detected from the file's contents (11+ tab-separated fields with integer FLAG/POS/MAPQ), so it works for Galaxy-style names. The extension is only used to pick the right wording, never to decide what a file is.

Refactor and tests:

  • List and directory handling moved from main.cpp into input_detect.cpp so it can be unit-tested.
  • The unit tests were tidied:
    • shared TempDir helper, safe under parallel CTest
    • test_main.cpp dropped, since Catch2WithMain provides main
    • the manual test_alignment_score harness removed
  • New tests/test_alignment_io.cpp: builds a CRAM, deletes its reference, and checks it decodes with -r and fails without it.
  • New scripts/make_test_crams.sh builds CRAM copies of the chr4 test BAMs, for end-to-end checks. The output is git-ignored, about 190 MB.

Version 2.1.2. CMake and conda/meta.yaml are bumped. The conda sha256 still refers to the v2.1.1 tarball and must be updated after v2.1.2 is tagged, as was done for 2.1.0 in 19132fe. CI only checks that the two version numbers match.

Testing

  • ctest: 45/45 pass locally (macOS arm64). CI covers Linux x86_64/aarch64 and macOS.
  • End to end on test_data/chr4_subset (3 BAMs, 1202 removed junctions as the baseline):
    • The list file runs with no warning and matches the baseline.
    • --bam test_data/chr4_subset (directory) gives byte-identical removed junctions and identical filtered BAMs to the list run.
    • CRAM 3.1 copies (scripts/make_test_crams.sh, reference removed, REF_PATH/REF_CACHE disabled) at -p 1, 2, 4 and 8: byte-identical removed junctions. Filtered reads match (1,973,511 for one sample), except TLEN, which CRAM recomputes when a read and its mate start at the same position.
    • On those CRAMs, v2.1.0 gives 0 junctions with exit 0 when it can't find the reference. At -p 2-4 it crashes even when it can.
    • Output into the input directory and duplicate sample names each give the new error before any alignment work.
  • The command from Misleading warning? #4 (eastr --bam bam_list.txt -r $REF -i $INDEX --out_filtered_bam .) exits 0 and writes the same three *_EASTR_filtered.bam files as v2.1.0, with identical contents and no warning.
  • Compatibility with v2.1.0, same commands, same inputs: single BAM, BAM list (with and without --bam_list), BED, BED list (with and without --bed_list) and GTF all give byte-identical removed and kept junction BEDs. A list file can still write filtered BAMs next to its input BAMs.

Not fixed here (follow-up)

The read loops treat any sam_read1() error (return value < -1) as end of file. A corrupt BAM, or a CRAM that fails to decode for some other reason, can still finish with exit 0 and no junctions. That needs its own change to error handling.

🤖 Generated with Claude Code

ishinder and others added 9 commits November 7, 2026 15:33
…input errors

- Drop the 2.1.0 deprecation warning for auto-detected path lists. It said
  "pass --bam_list", which reads as `--bam_list FILE` and fails because the
  option is a flag (#4). Content-based list detection is the supported path;
  --bam_list/--bed_list stay as optional overrides.
- Decode CRAM against the -r reference via a shared open_alignment() helper.
  Without it, a CRAM whose UR: reference was unreachable failed every slice
  and EASTR reported zero junctions while exiting 0.
- Move list resolution into input_detect so it is unit-tested, and replace
  "File not found: <first line>" with a diagnosis: headerless SAM (by content
  or .sam extension), .bam/.cram without a valid signature, or an unrecognized
  file. Use non-throwing fs::exists so long data lines no longer raise
  ENAMETOOLONG.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
--bam <dir> now processes every BAM/CRAM/SAM directly inside the directory in
one run, sorted by filename. Files are selected with htslib format detection
(hts_detect_format2), not by extension, so .bai/.csi indexes, gzipped FASTA
and other files can sit alongside; hidden files and subdirectories are
ignored. Previously a directory was silently read as an empty path list and
EASTR exited 0 with no junctions. --bed with a directory and --bam_list with
a directory now give clear errors.

Bump the version to 2.1.2 (CMake + conda/meta.yaml). The conda source sha256
still refers to the v2.1.1 tarball and must be updated once v2.1.2 is tagged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
They were on/off flags rather than options taking a file, and list files are
detected automatically, so they had no practical effect; their help text was
also the source of the confusion in #4. Dropping them removes the force_list
path in resolve_input_files() and its two error branches.

Breaking: commands that still pass --bam_list/--bed_list now fail with
"The following argument was not expected"; drop the flag.

tests/test_input_detect.cpp also moves to the shared test TempDir helper
(tests/test_temp.hpp), which this commit adds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Drop tests/test_main.cpp: Catch2::Catch2WithMain already provides main().
- Drop the standalone test_alignment_score executable (a manual comparison
  harness against the Python implementation, not run by CTest).
- Use the shared eastr_test::TempDir helper so tests do not collide when
  CTest runs them in parallel.
- Consolidate and tighten several bed_parser, junction, path_utils and
  strand-handling tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… macOS

htscodecs' NEON rANS-Nx16 order-1 decoder (rans_uncompress_O1_32x16_neon)
keeps s3[256][1024] (1 MiB) and fb[256][256] (256 KiB) on the stack. macOS
gives secondary threads a 512 KiB stack, so decoding CRAM 3.1 on EASTR's
std::thread/std::async workers overflowed into adjacent thread memory: with
2+ CRAM inputs and -p > 1 EASTR crashed (SIGSEGV/SIGBUS in
htscodecs_tls_free) or reported 0 junctions for a sample.

open_alignment() now takes the htslib thread count and always attaches an
htslib thread pool (>= 1 thread) to CRAM inputs, so slices are decoded on
htslib's worker threads, which get >= 3 MiB stacks (HTS_MIN_THREAD_STACK).
The input-side hts_set_threads() calls move into open_alignment(), since
htslib ignores a second pool on a CRAM handle.

Verified on arm64 macOS: 3 CRAM 3.1 inputs at -p 1/2/3/4/6/8 give
byte-identical removed junctions to the BAM inputs (previously -p 2..4
crashed or lost samples); BAM output unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…irectory

- Two inputs whose filenames share a stem (a/S1.bam, b/S1.cram) got the same
  sample name, so their per-sample outputs overwrote each other. This is now
  an error, for lists and directories alike.
- With --bam <dir> and --out_filtered_bam pointing at that same directory, a
  re-run picked up EASTR's own *_EASTR_filtered.bam files as extra samples and
  silently changed the pooled junction counts. This is now an error.

Both checks run before any alignment work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t CRAMs

- tests/test_alignment_io.cpp writes a small CRAM against a reference, deletes
  that reference (so the header UR: path is gone), and checks that
  open_alignment() decodes every record with the -r reference and fails
  without it. Runs in CI without the LFS test data.
- scripts/make_test_crams.sh builds CRAM copies of test_data/chr4_subset/*.bam
  in test_data/chr4_subset_cram/ (git-ignored; ~190 MB) for end-to-end CRAM
  vs BAM comparisons, encoded against a temporary reference that is deleted
  afterwards. Usage is documented in test_data/README.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Restore the two flags removed in ba6bad6 with their 2.1.x behavior (skip
auto-detection and read the argument as a list), so commands written for
2.1.0/2.1.1 keep working with identical output. They stay in --help, worded
as optional since list files are detected automatically.

Verified against v2.1.0 with the same commands (single BAM, BAM list with and
without --bam_list, BED, BED list with and without --bed_list, GTF): removed
and kept junction BEDs are byte-identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ishinder
ishinder merged commit d86d510 into main Nov 8, 2026
4 checks passed
@ishinder ishinder mentioned this pull request Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misleading warning?

1 participant