Skip to content

samtools flagstat: reads with both SECONDARY and SUPPLEMENTARY flags are double-counted in the supplementary total #125

Description

@seqinfomics

Summary: When a BAM contains alignment records with both the SECONDARY (0x100) and SUPPLEMENTARY (0x800) flags set, RustQC's flagstat module counts those records in both the secondary and supplementary totals, inflating the supplementary count and breaking primary + secondary + supplementary == total. Real samtools flagstat gives the SECONDARY flag priority and counts such dual-flagged records only as secondary.

Environment:

  • RustQC 0.2.1 (c868466, built 2026-04-09), Docker image seqinfomics/rustqc:0.2.1, run via Apptainer 1.4.5
  • Input: paired-end STAR-aligned BAM (--paired), single contig, 156,370 total records

Steps to reproduce:

rustqc rna sample.bam --gtf annotation.gtf --paired --stranded reverse --skip-dup-check --outdir out/

Flag breakdown of the input BAM (via samtools view | awk bitwise check):

category count
primary (neither flag) 154,662
secondary-only 309
supplementary-only 587
both secondary+supplementary 812
total 156,370

Expected (matches real samtools flagstat 1.21):

154662 + 0 primary
1121 + 0 secondary        (309 + 812)
587 + 0 supplementary      (587-only)

Actual (RustQC's flagstat output):

154662 + 0 primary
1121 + 0 secondary        (matches, 309 + 812)
1399 + 0 supplementary     (587 + 812  <- the 812 dual-flagged reads are counted again here)

Note 154662 + 1121 + 1399 = 157182, which exceeds the true total of 156370 — the sum invariant that samtools flagstat maintains is violated.

Fix suggestion: In the flagstat counting logic, categorize each record by flag priority (secondary > supplementary > primary) rather than checking the supplementary bit independently of the secondary bit.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions