Skip to content

Explain why CRAM reference base lookup failed - #1790

Merged
tfenne merged 1 commit into
masterfrom
tf_fix_cram_1732
Jul 26, 2026
Merged

Explain why CRAM reference base lookup failed#1790
tfenne merged 1 commit into
masterfrom
tf_fix_cram_1732

Conversation

@tfenne

@tfenne tfenne commented Jul 25, 2026

Copy link
Copy Markdown
Member

Closes #1732.

When a reference source returns no bases for a sequence that is present in the sequence dictionary, CRAMReferenceRegion threw Failure getting reference bases for sequence chr1 — which says what went wrong but nothing about why or what to do. In practice the overwhelmingly common cause is a reference fasta that has not been indexed, so users hit this via GATK/Picard with a perfectly good reference and no hint that a missing .fai is the problem.

The message now names the sequence and points at the companion .fai/.gzi/.dict files a CRAM needs. Message text only — no change to which inputs are accepted.

Note this does not address #998, which is a different misleading message raised from ReferenceSource on a separate code path; that one stays open.

Summary by CodeRabbit

  • Bug Fixes
    • Improved error messages when reference bases cannot be retrieved.
    • Diagnostics now identify the affected sequence and suggest checking required FASTA indexes and sequence dictionaries.

When a reference source returned no bases for a sequence that is present in
the sequence dictionary, CRAMReferenceRegion threw

    Failure getting reference bases for sequence chr1

which says what went wrong but nothing about why, or what to do. In practice
the overwhelmingly common cause is a reference fasta that has not been indexed,
so users hit this via GATK/Picard with a perfectly good reference and no
indication that a .fai is what's missing.

Say so: name the sequence and point at the companion .fai/.gzi/.dict files a
CRAM needs. This changes only the message text, not which inputs are accepted.

Note this does not address #998, which is a different misleading message raised
from ReferenceSource on a separate code path.
@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

CRAMReferenceRegion now provides detailed guidance when reference bases are unavailable, including likely missing index files and sequence mismatches. A new TestNG test verifies that the exception message identifies the sequence and mentions the .fai and .dict files.

Changes

Reference retrieval diagnostics

Layer / File(s) Summary
Diagnostic message and validation
src/main/java/htsjdk/samtools/cram/build/CRAMReferenceRegion.java, src/test/java/htsjdk/samtools/cram/build/CRAMReferenceRegionErrorMessageTest.java
The null-reference exception now describes likely missing .fai, .dict, or .gzi files and sequence-name mismatches. Tests verify the sequence name, .fai, and .dict guidance.

Estimated code review effort: 1 (Trivial) | ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: improving the CRAM reference base lookup error message.
Linked Issues check ✅ Passed The PR improves the CRAM error for missing reference bases and adds tests for the sequence name and missing index guidance.
Out of Scope Changes check ✅ Passed The changes stay focused on the error message and its test coverage, with no obvious unrelated additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tf_fix_cram_1732

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/test/java/htsjdk/samtools/cram/build/CRAMReferenceRegionErrorMessageTest.java (1)

65-70: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover the .gzi guidance in the test.

The implementation now mentions .gzi for bgzipped FASTA, but the test only protects the sequence, .fai, and .dict wording. Add an assertion for .gzi so all advertised companion files are covered.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@src/test/java/htsjdk/samtools/cram/build/CRAMReferenceRegionErrorMessageTest.java`
around lines 65 - 70, Update
missing_bases_error_mentions_the_sequence_dictionary to also assert that the
fetched error message contains “.gzi”, preserving the existing .dict assertion
and message context.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/java/htsjdk/samtools/cram/build/CRAMReferenceRegion.java`:
- Around line 178-183: Update the IllegalArgumentException message in
CRAMReferenceRegion to include the actionable samtools faidx command for
generating the reference FASTA’s .fai index, while preserving the existing
sequence-name and .gzi/.dict guidance.

---

Nitpick comments:
In
`@src/test/java/htsjdk/samtools/cram/build/CRAMReferenceRegionErrorMessageTest.java`:
- Around line 65-70: Update missing_bases_error_mentions_the_sequence_dictionary
to also assert that the fetched error message contains “.gzi”, preserving the
existing .dict assertion and message context.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 38d92f9f-3f97-4e30-b5fc-639d34205ec8

📥 Commits

Reviewing files that changed from the base of the PR and between 2ae4aaa and 5dd8291.

📒 Files selected for processing (2)
  • src/main/java/htsjdk/samtools/cram/build/CRAMReferenceRegion.java
  • src/test/java/htsjdk/samtools/cram/build/CRAMReferenceRegionErrorMessageTest.java

Comment on lines +178 to +183
throw new IllegalArgumentException(String.format(
"Failure getting reference bases for sequence '%s'. The reference source did not return any "
+ "bases for this sequence. Verify that the reference fasta contains a sequence named "
+ "'%s', and that it has been indexed -- a CRAM requires a fasta with a companion .fai "
+ "index (plus a .gzi index if the fasta is bgzipped) and a .dict sequence dictionary.",
sequenceRecord.getSequenceName(), sequenceRecord.getSequenceName()));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Include the samtools faidx remediation.

The message identifies the likely missing indexes but does not provide the explicitly requested actionable command. Add guidance such as samtools faidx <reference.fasta> for creating the .fai index.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/htsjdk/samtools/cram/build/CRAMReferenceRegion.java` around
lines 178 - 183, Update the IllegalArgumentException message in
CRAMReferenceRegion to include the actionable samtools faidx command for
generating the reference FASTA’s .fai index, while preserving the existing
sequence-name and .gzi/.dict guidance.

@tfenne
tfenne merged commit e37a8bf into master Jul 26, 2026
5 checks passed
@tfenne
tfenne deleted the tf_fix_cram_1732 branch July 26, 2026 00:35
tfenne added a commit that referenced this pull request Jul 26, 2026
The whole-contig lookup reported

    A reference must be supplied (reference sequence SAMSequenceRecord(name=1,
    length=200,dict_index=0,assembly=null,alternate_names=[]) not found).

which is misleading in the common case: a reference *was* supplied, it just
had not been indexed. It also dumped the entire SAMSequenceRecord at the user.

This is the sibling of the by-region lookup fixed in #1790 for #1732. Both fail
for the same reason, so factor the diagnostic into one helper and use it from
both, rather than letting the two messages drift apart. The message names the
sequence and points at the companion .fai/.gzi/.dict files a CRAM needs.

Message text only; no change to which inputs are accepted.
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.

Failure getting reference bases inadequately describes the missing index error

1 participant