Skip to content

Add sam-stats command (port of ngsutils bam-stats)#27

Merged
mbreese merged 1 commit into
mainfrom
sam-stats
Jun 18, 2026
Merged

Add sam-stats command (port of ngsutils bam-stats)#27
mbreese merged 1 commit into
mainfrom
sam-stats

Conversation

@mbreese

@mbreese mbreese commented Jun 18, 2026

Copy link
Copy Markdown
Member

Summary

Adds a sam-stats command that prints summary statistics for SAM/BAM/CRAM files, porting the per-read logic from ngsutils bam-stats. Phase 1 — the --gtf gene-model and --bed on-target stats are intentionally deferred (they need an interval/region model that doesn't exist in cgkit yet).

Statistics reported

  • Core counts: total / mapped / unmapped / multiple-mapped / uniquely-mapped
  • Q30 pct, total bases, reference length, total depth (suppressed when gapped N alignments are present, matching the RNA-seq guard)
  • [Flags] breakdown (non-zero bits only) and [References] per-chrom counts
  • --tags NH:i,RG:Z,MAPQ value distributions with a missing bucket
  • --calc-insert median, --rgid filter, --unique, --show-unmapped-ref, --cram-ref

Faithful to the Java semantics: unique detection is NH→IH→MAPQ, and only the first read of each pair is profiled (so total is intentionally lower than samtools flagstat's raw record count).

Also: usage-on-error fix (root)

SilenceUsage was set globally, so a missing argument printed only the bare error with no usage. Moved it into PersistentPreRun (which runs after Cobra validates args/flags but before RunE), so:

  • Argument/flag errors → error + usage shown
  • Execution errors (e.g. file not found) → error only

Also stopped double-printing the error in Execute() (Cobra already prints it). This applies uniformly to all commands.

Test plan

  • go test ./... passes; go vet and gofmt clean
  • New sam_stats_test.go covers counts, second-of-pair/unmapped handling, Q30, a --tags tally, --rgid filtering, and tag-spec parsing
  • Cross-checked counts against samtools flagstat
  • Manually verified missing-arg shows usage while runtime errors do not

🤖 Generated with Claude Code

Add a sam-stats command that prints summary statistics for SAM/BAM/CRAM
files: read counts, mapping rates, Q30, depth, SAM flag breakdown,
per-reference counts, optional --tags value distributions and
--calc-insert median. Ports the per-read logic from ngsutils bam-stats
(unique = NH/IH/MAPQ; profiles the first read of each pair). Phase 1
omits the --gtf gene-model and --bed on-target stats.

Also fix usage-on-error handling at the root: move SilenceUsage into
PersistentPreRun so argument/flag errors still print usage while
execution errors do not, and stop double-printing the error in Execute.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@mbreese mbreese merged commit 8e0824b into main Jun 18, 2026
1 check 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.

1 participant