Skip to content

fix(qualimap): match Qualimap 2.3 number formatting, read selection and 5'-3' bias - #204

Merged
ewels merged 2 commits into
seqeralabs:mainfrom
rhassaine:fix/qualimap-parity-v2
Nov 26, 2026
Merged

ewels merged 2 commits into
seqeralabs:mainfrom
rhassaine:fix/qualimap-parity-v2

Conversation

@rhassaine

@rhassaine rhassaine commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Brings RustQC's Qualimap rnaseq output (rnaseq_qc_results.txt, coverage profiles, qualimapReport.html tables) in line with Qualimap 2.3:

  • the 5'-3' bias is computed the way Qualimap does (e.g. 1.3 rather than 1.19 on the benchmark data);
  • numbers are printed as Qualimap's Java DecimalFormat prints them: at most two decimals, trailing zeros dropped, half-even rounding, so 60% rather than 60.0%;
  • reads are selected as Qualimap selects them: secondary, supplementary and QC-fail records are not skipped, pairs are grouped by read name, and reads on a contig without annotated exons are reported as "Missing chromosome in annotation";
  • junction motifs with equal counts are listed by count, then name, which is deterministic across runs and --threads (before, ties came out in a different order on every run).

Slimmed down after review: the earlier version also emulated Java HashMap iteration to reproduce Qualimap's own order for tied motifs. That's gone; tied motifs are sorted by name instead.

🤖 Generated with Claude Code

rhassaine and others added 2 commits November 26, 2026 16:57
RustQC reported a 5'-3' bias of 1.19 on the benchmark data where
Qualimap 2.3 reports 1.3 (5' and 3' bias and the coverage profiles
already matched).

Root cause: Qualimap takes the median with Commons Math 2.0
StatUtils.percentile(values, 50), which sorts with Arrays.sort and keeps
NaN (sorted last). A transcript with no coverage in either 100-base end
gives a 5'/3' ratio of 0/0 = NaN; RustQC dropped these. On the benchmark
data 21 of the 320 selected transcripts are NaN, and dropping them moves
the median from 1.3 to 1.19 (checked by toggling only that filter).

compute_bias now follows TranscriptDataHandler.calculateCoverageBias()
and pickTranscripts():
- best transcript per gene: >= 500 bp and mean >= 1, visited in Java
  HashMap order, replaced only by a strictly higher mean;
- keep those at or above coverages[max(0, n - 1001)], i.e. the top
  1001 (plus ties), not 1000;
- no filtering of NaN/Infinity ratios;
- Commons Math 2.0 percentile: pos = p(n+1)/100, interpolated as
  lower + dif * (upper - lower), on a Java-ordered sort (NaN last).

Evidence (pod, Qualimap 2.3 on Java 21, run as nf-core does: name-sorted
BAM, --sorted, -p strand-specific-reverse -pe): rnaseq_qc_results.txt
from RustQC is identical to Qualimap's and to the benchmark snapshot
apart from the bam/gff path lines. The coverage profiles stay
byte-identical, and output is unchanged with --threads 1, 2 and 4.

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

Make rnaseq_qc_results.txt, the raw coverage profiles and the summary
tables of qualimapReport.html match Qualimap 2.3 byte for byte.

Junction motif order. Motifs are counted in a HashMap and were sorted by
count only, so motifs with the same count came out in a different order on
every run (Rust HashMap iteration order is random per process). They are now
ordered deterministically, by count and then by motif name, in one shared
helper used by both outputs. Qualimap's own tie order (reverse Java HashMap iteration
order) is not reproduced.

Number formatting. Qualimap formats through StringUtilsSwing, which calls
bioinfo-commons StringUtils.decimalFormat(v, pattern), i.e.
new java.text.DecimalFormat(pattern).format(v):
- formatLong / formatDecimal ("###,...,###.##"): grouped by three, at
  most two decimals, trailing zeros dropped;
- formatPercentage ("###.##" + "%"): same without grouping, so a whole
  percentage is "60%" (RustQC printed "60.0%" in the text report and
  "37.50%" in the HTML report).
Rounding is HALF_EVEN on the double's exact value (0.125 -> "0.12").
The new qualimap::format module reproduces this; it was checked against
64,070 values formatted by Qualimap's own StringUtilsSwing on Java 21.

Other differences fixed:
- the coverage profile files use Double.toString ("0.0", not "0";
  "1.0E-4" style below 1e-3);
- SSP estimation is computed in single precision (fwd / (float)total)
  and printed with formatDecimal ("0.5 / 0.5"); it is printed for any
  unstranded run, as in Qualimap;
- genomic-origin percentages are (100. * n) / (exonic + no feature);
- SE "reads aligned" / "Number of mapped reads:" is the primary
  alignment count (the HTML row was missing for SE);
- 5'/3'/5'-3' bias is 0 when no transcript received coverage and Infinity
  prints as Java does;
- input table values had a leading space Qualimap does not print;
- reads on a sequence with no annotated exon are dropped before the NH
  check, as in ComputeCountsTask.checkRead, and reported in the HTML
  report as "Missing chromosome in annotation" (seqNotFoundCount).

Read selection. Follow ComputeCountsTask (checkRead, computeReadWeight, run,
processFragment) for every record:
- secondary alignments are not skipped: Qualimap's skipSecondaryAlignments
  is false, so a secondary record with NH <= 1 (or no NH tag, which Qualimap
  reads as 1) is counted like a primary one, and only NH > 1 drops it;
- supplementary and QC-fail records are not skipped either;
- the order of checks matches checkRead: unmapped, then the
  secondary/primary and left/right/proper counters, then the
  annotated-sequence check, then NH;
- proper pairs are grouped by read name, as Qualimap groups consecutive
  records of its name-sorted input. A group of exactly two records is a
  fragment; any other size is skipped. The per-chromosome workers merge
  their open groups;
- junction statistics are collected only for counted reads;
- a record without M operations adds no interval to its fragment, and each
  record's strand mark and intron lookup use its own flags and sequence.

Verified on the 8 harness datasets plus derived missing-chromosome, QC-fail,
secondary and supplementary inputs against Qualimap 2.3 run as the nf-core
module does (samtools sort -n, --sorted, -pe for paired-end): identical
apart from the order of tied junction motifs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rhassaine
rhassaine force-pushed the fix/qualimap-parity-v2 branch from bab3fd5 to c881713 Compare November 26, 2026 16:01
@rhassaine rhassaine changed the title fix(qualimap): make the output identical to Qualimap 2.3 fix(qualimap): match Qualimap 2.3 number formatting, read selection and 5'-3' bias Nov 26, 2026

@ewels ewels left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I still don't love the stuff to do with matching Java's number formatting, as it's quite a lot of code that has effectively zero utility. But fair enough, it's not too bad.

Thanks!

@ewels
ewels merged commit afc6e6f into seqeralabs:main Nov 26, 2026
7 checks passed
@rhassaine

Copy link
Copy Markdown
Contributor Author

Thanks! I'm not a fan of it either, it's a lot of code for very little. I think there's a simpler way to get most of the value. What actually matters is that MultiQC's Qualimap module parses rnaseq_qc_results.txt, so the numbers need to look like Qualimap's (60%, 1,234, at most two decimals). The exact Java edge cases on top of that (Double.toString for the profiles, half-even corner cases, NaN/∞ glyphs) aren't worth carrying.

I could do a small follow-up that replaces it with a short formatter on top of Rust's standard formatting (up to two decimals, trailing zeros trimmed, thousands separators) and checks MultiQC still parses the output on the benchmark data. Happy to do that if you'd like.

@rhassaine
rhassaine deleted the fix/qualimap-parity-v2 branch November 27, 2026 15:52
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