Skip to content

feat: regenerate NM/MD on CRAM input, like htslib - #210

Merged
ewels merged 1 commit into
seqeralabs:noodles-migration-redofrom
rhassaine:feat/cram-decode-md
Dec 2, 2026
Merged

ewels merged 1 commit into
seqeralabs:noodles-migration-redofrom
rhassaine:feat/cram-decode-md

Conversation

@rhassaine

Copy link
Copy Markdown
Contributor

htslib regenerates NM/MD when decoding CRAM; noodles leaves that to the caller by design (zaeleus/noodles#379). So with #118, CRAM input gave samtools stats 0 mismatches (main and samtools itself: 39,821 on the chr6 test data as CRAM) and the split_bam BAMs lost their MD/NM tags. BAM input is not affected.

This computes them in the shim, matching htslib:

  • src/rna/bam/md_nm.rs: NM/MD from CIGAR + read sequence + reference, following htslib's CRAM decoder rules (N against N is a mismatch, case-insensitive reference, ^ deletions, splices/clips skipped, no tags for unmapped/no-sequence records, existing tags never overwritten, tag order MD, NM then RG); 29 unit tests.
  • Computed only when something needs them, decided once per run: samtools stats needs NM, split_bam's written BAMs need NM+MD, otherwise nothing.
  • --cram-decode-md auto|always|never (config rna.cram.decode_md, env RUSTQC_CRAM_DECODE_MD), mirroring htslib's decode_md; default auto. Documented in the CLI and configuration docs.

On the chr6 CRAM at 1 and 6 threads: samtools stats is byte-identical to main (39,821 mismatches), split_bam's BAMs are identical including MD/NM, other outputs identical; the 8 BAM datasets are unchanged. Cost is ~0.03 s on that dataset. Tests pass on stable and 1.89.

Note: this and the shim-allocation PR both touch io.rs (a field in IndexedReader and an import); whichever goes in second needs a small rebase, which I'll do.

🤖 Generated with Claude Code

htslib rebuilds the NM and MD tags while decoding CRAM (CRAM_OPT_DECODE_MD
is set to auto on every CRAM it opens), noodles leaves that to the caller.
Without them samtools stats reports 0 mismatches for CRAM input and the
split_bam BAMs lose NM/MD.

The CRAM readers now add the missing tags from the reference, following
cram_decode_seq (mismatch rules, MD zero padding, deletions, splices, NM
width, tag order with RG last). Tags a record already has are kept.

It runs once per run only for what a tool needs (--cram-decode-md auto, the
default): NM for samtools stats, NM and MD for the split_bam BAMs. always
fills both on every mapped record, never turns it off. Also settable as
cram.decode_md in the config file. BAM/SAM input is untouched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rhassaine
rhassaine force-pushed the feat/cram-decode-md branch from f912e15 to 3c7b9d1 Compare December 2, 2026 13:07
@ewels
ewels merged commit e0a0c3e into seqeralabs:noodles-migration-redo Dec 2, 2026
7 checks passed
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.

2 participants