diff --git a/docs/src/content/docs/rna/qualimap.mdx b/docs/src/content/docs/rna/qualimap.mdx index 1ef5149..88c345b 100644 --- a/docs/src/content/docs/rna/qualimap.mdx +++ b/docs/src/content/docs/rna/qualimap.mdx @@ -330,11 +330,10 @@ eliminating the separate name-sort and Qualimap run required by the original Jav -Every number is printed the way Qualimap prints it. Qualimap formats counts, -ratios and percentages with Java's `DecimalFormat` (at most two decimals, -trailing zeros dropped, half-even rounding), so a whole percentage is `60%`, -not `60.0%`, and a ratio of exactly one half is `0.5`. The coverage profile -files use Java's `Double.toString` (`0.0`, `1.0E-4`). Reads on a sequence with +Counts, ratios and percentages are printed with at most two decimals and +trailing zeros dropped, so a whole percentage is `60%` and a ratio of exactly +one half is `0.5`. Counts are grouped with commas. The coverage profile +files hold plain decimal numbers (`0`, `0.0001`). Reads on a sequence with no annotated exon are left out of the counts, as in Qualimap, and reported in the HTML report as "Missing chromosome in annotation". diff --git a/src/rna/qualimap/format.rs b/src/rna/qualimap/format.rs index c3f3cf2..694be54 100644 --- a/src/rna/qualimap/format.rs +++ b/src/rna/qualimap/format.rs @@ -1,122 +1,29 @@ -//! Number formatting that reproduces Qualimap's Java output. +//! Plain number formatting for Qualimap's text and HTML output. //! -//! Qualimap 2.3 formats every number in `rnaseq_qc_results.txt` and in the -//! summary tables of `qualimapReport.html` through `StringUtilsSwing` -//! (`org.bioinfo.ngs.qc.qualimap.gui.utils`), which calls -//! `org.bioinfo.commons.utils.StringUtils.decimalFormat(value, pattern)`, i.e. -//! `new java.text.DecimalFormat(pattern).format(value)`: -//! -//! | method | pattern | example | -//! |----------------------|-------------------------------------|--------------| -//! | `formatLong` | `###,###,####,###,…,###.##` | `1,234,567` | -//! | `formatDecimal` | `###,###,###,###,###,###,###.##` | `1,234.5` | -//! | `formatPercentage` | `###.##` followed by `%` | `60%` | -//! -//! `DecimalFormat` with these patterns prints at most two fraction digits, -//! drops trailing zeros (so whole numbers have no decimal point), always prints -//! at least one integer digit, groups the integer part by three (grouping size -//! is the last group in the pattern; `###.##` has no grouping) and rounds -//! with `RoundingMode.HALF_EVEN`. - -/// Doubles at or above 2^53 are whole numbers whose exact decimal expansion -/// can have more digits than Java prints. -const EXACT_INTEGER_LIMIT: f64 = 9_007_199_254_740_992.0; - -/// Java `DecimalFormat` output for NaN (CLDR locale data, Java 9+). -/// -/// The nf-core Qualimap image (`biocontainers/qualimap:2.3--hdfd78af_0`) -/// ships OpenJDK 21.0.1 with `LANG=C.UTF-8`, so this and [`JAVA_INFINITY`] -/// are what it prints; Java 18+ also writes files as UTF-8 by default. -const JAVA_NAN: &str = "NaN"; - -/// Java `DecimalFormat` output for infinity. -const JAVA_INFINITY: &str = "\u{221e}"; +//! Numbers have at most two decimals, trailing zeros (and a trailing `.`) are +//! dropped, and counts are grouped with `,` every three digits. Percentages are +//! not grouped and end in `%`. -/// Qualimap `StringUtilsSwing.formatLong`: an integer with `,` every three digits. -/// -/// `formatLong` passes the `long` to `decimalFormat(double, String)`, so counts -/// above 2^53 lose precision exactly as a double does. +/// An integer with `,` every three digits. pub(crate) fn format_long(n: u64) -> String { - decimal_format(n as f64, true) + group_thousands(&n.to_string()) } -/// Qualimap `StringUtilsSwing.formatDecimal`: up to two decimals, grouped by three. +/// At most two decimals, with `,` every three integer digits. pub(crate) fn format_decimal(v: f64) -> String { - decimal_format(v, true) + format_number(v, true) } -/// Qualimap `StringUtilsSwing.formatPercentage`: up to two decimals and a `%` sign. +/// At most two decimals and a `%` sign, without grouping. pub(crate) fn format_percentage(v: f64) -> String { - let mut s = decimal_format(v, false); - s.push('%'); - s -} - -/// A percentage computed the way Qualimap does it: `(100. * part) / total`. -/// -/// The operand order matters for the last bit of the result, and therefore for -/// how a value close to a rounding boundary is printed. -pub(crate) fn percentage_of(part: u64, total: u64) -> f64 { - (100.0 * part as f64) / total as f64 -} - -/// Java's `Double.toString(v)` (Java 19+), which Qualimap uses when it writes -/// chart data such as the coverage profiles in `raw_data_qualimapReport/`. -/// -/// The digits are the shortest that round-trip, as Rust prints them. Values -/// in `[1e-3, 1e7)` are plain decimals with at least one fraction digit -/// (`0.0`, `12.0`, `0.0041`); others use computerized scientific notation -/// (`1.0E-4`, `1.2345678E7`). -pub(crate) fn java_double_to_string(v: f64) -> String { - if v.is_nan() { - return "NaN".to_string(); - } - let sign = if v.is_sign_negative() { "-" } else { "" }; - if v.is_infinite() { - return format!("{sign}Infinity"); - } - let abs = v.abs(); - let (mantissa, exponent) = if abs == 0.0 || (1e-3..1e7).contains(&abs) { - (abs.to_string(), None) - } else { - let sci = format!("{abs:e}"); - let (m, e) = sci.split_once('e').expect("exponent in {:e} output"); - (m.to_string(), Some(e.to_string())) - }; - let point = if mantissa.contains('.') { "" } else { ".0" }; - match exponent { - None => format!("{sign}{mantissa}{point}"), - Some(e) => format!("{sign}{mantissa}{point}E{e}"), - } + format!("{}%", format_number(v, false)) } -/// `new DecimalFormat(pattern).format(v)` for Qualimap's two decimal patterns: -/// at most two fraction digits, at least one integer digit, HALF_EVEN rounding, -/// optional grouping by three. -/// -/// `DecimalFormat` rounds the double's `FloatingDecimal` digits (17 significant -/// digits at most) and corrects ties for whether those digits are above or -/// below the exact value (`java.text.DigitList.shouldRoundUp`). While a -/// hundredth is far larger than the spacing between doubles, as for every -/// percentage, ratio and bias Qualimap prints, that equals rounding the exact -/// binary value half to even, which is what Rust's `{:.2}` does. Whole numbers -/// from 2^53 up are printed from their shortest digits, as Java does. -fn decimal_format(v: f64, grouping: bool) -> String { - if v.is_nan() { - return JAVA_NAN.to_string(); - } - // Java prints a minus sign for any negative value, including -0.0 and - // values that round to zero. - let sign = if v.is_sign_negative() { "-" } else { "" }; - if v.is_infinite() { - return format!("{sign}{JAVA_INFINITY}"); +fn format_number(v: f64, grouping: bool) -> String { + if !v.is_finite() { + return v.to_string(); } - let abs = v.abs(); - let text = if abs >= EXACT_INTEGER_LIMIT { - format!("{abs}") - } else { - format!("{abs:.2}") - }; + let text = format!("{:.2}", v.abs()); let (int_part, frac_part) = text.split_once('.').unwrap_or((&text, "")); let frac_part = frac_part.trim_end_matches('0'); let int_part = if grouping { @@ -124,6 +31,7 @@ fn decimal_format(v: f64, grouping: bool) -> String { } else { int_part.to_string() }; + let sign = if v.is_sign_negative() { "-" } else { "" }; if frac_part.is_empty() { format!("{sign}{int_part}") } else { @@ -148,8 +56,6 @@ fn group_thousands(digits: &str) -> String { mod tests { use super::*; - /// Expected strings below were produced by Qualimap 2.3's own - /// `StringUtilsSwing` (bioinfo-commons 0.10.1) on Java 21. #[test] fn format_long_groups_by_three() { assert_eq!(format_long(0), "0"); @@ -157,9 +63,7 @@ mod tests { assert_eq!(format_long(1234), "1,234"); assert_eq!(format_long(1_234_567), "1,234,567"); assert_eq!(format_long(1_000_000_000), "1,000,000,000"); - // Above 2^53 Java formats the count as a double. - assert_eq!(format_long(9_007_199_254_740_993), "9,007,199,254,740,992"); - assert_eq!(format_long(u64::MAX >> 1), "9,223,372,036,854,776,000"); + assert_eq!(format_long(u64::MAX), "18,446,744,073,709,551,615"); } #[test] @@ -171,6 +75,7 @@ mod tests { assert_eq!(format_percentage(86.77), "86.77%"); assert_eq!(format_percentage(0.05), "0.05%"); assert_eq!(format_percentage(1.0 / 3.0 * 100.0), "33.33%"); + assert_eq!(format_percentage(99.995), "100%"); } #[test] @@ -183,88 +88,26 @@ mod tests { assert_eq!(format_decimal(0.5), "0.5"); assert_eq!(format_decimal(1.0), "1"); assert_eq!(format_decimal(1.3), "1.3"); - assert_eq!(format_decimal(0.44), "0.44"); assert_eq!(format_decimal(2.0 / 3.0), "0.67"); assert_eq!(format_decimal(1234.5), "1,234.5"); - assert_eq!(format_decimal(1e6 + 0.125), "1,000,000.12"); assert_eq!(format_decimal(1e15), "1,000,000,000,000,000"); - assert_eq!(format_decimal(1e20), "100,000,000,000,000,000,000"); - assert_eq!( - format_decimal(123_456_789_012_345_678.0), - "123,456,789,012,345,680" - ); - } - - #[test] - fn rounding_is_half_even_on_the_exact_binary_value() { - // Exact binary ties go to the even neighbour. - assert_eq!(format_decimal(0.125), "0.12"); - assert_eq!(format_decimal(0.375), "0.38"); - assert_eq!(format_decimal(0.625), "0.62"); - assert_eq!(format_decimal(0.875), "0.88"); - assert_eq!(format_percentage(3.125), "3.12%"); - // Not ties: the double is just above or below the decimal literal. - assert_eq!(format_decimal(0.015), "0.01"); // 0.01499999... - assert_eq!(format_decimal(0.035), "0.04"); // 0.03500000...04 - assert_eq!(format_decimal(1.005), "1"); // 1.00499999... - assert_eq!(format_decimal(2.675), "2.67"); // 2.67499999... - assert_eq!(format_decimal(0.995), "0.99"); // 0.99499999... - assert_eq!(format_decimal(9.995), "9.99"); // 9.99499999... - assert_eq!(format_percentage(99.995), "100%"); // 99.99500000...03 - // Carry into the integer part and a new group. assert_eq!(format_decimal(999.999), "1,000"); - } - - #[test] - fn special_values_match_java() { - assert_eq!(format_decimal(f64::NAN), "NaN"); - assert_eq!(format_percentage(f64::NAN), "NaN%"); - assert_eq!(format_decimal(f64::INFINITY), "\u{221e}"); - assert_eq!(format_decimal(f64::NEG_INFINITY), "-\u{221e}"); - assert_eq!(format_decimal(-0.0), "-0"); - assert_eq!(format_decimal(-0.004), "-0"); - assert_eq!(format_decimal(-1.5), "-1.5"); assert_eq!(format_decimal(-1_234_567.891), "-1,234,567.89"); - assert_eq!(format_decimal(f64::from_bits(1)), "0"); - } - - #[test] - fn strand_estimation_is_formatted_as_a_float() { - // Qualimap divides in single precision: fwd / (float)(fwd + rev). - let ssp = |fwd: u64, rev: u64| f64::from(fwd as f32 / (fwd + rev) as f32); - assert_eq!(format_decimal(ssp(1, 1)), "0.5"); - assert_eq!(format_decimal(ssp(1, 7)), "0.12"); // 0.125f is a tie - assert_eq!(format_decimal(ssp(3, 0)), "1"); } #[test] - fn double_to_string_matches_java() { - assert_eq!(java_double_to_string(0.0), "0.0"); - assert_eq!(java_double_to_string(-0.0), "-0.0"); - assert_eq!(java_double_to_string(1.0), "1.0"); - assert_eq!(java_double_to_string(99.0), "99.0"); - assert_eq!(java_double_to_string(0.1), "0.1"); - assert_eq!( - java_double_to_string(0.004131012097964001), - "0.004131012097964001" - ); - assert_eq!(java_double_to_string(1234.5), "1234.5"); - assert_eq!(java_double_to_string(0.001), "0.001"); - assert_eq!(java_double_to_string(0.000_999), "9.99E-4"); - assert_eq!(java_double_to_string(1e-4), "1.0E-4"); - assert_eq!(java_double_to_string(9_999_999.0), "9999999.0"); - assert_eq!(java_double_to_string(1e7), "1.0E7"); - assert_eq!(java_double_to_string(12_345_678.9), "1.23456789E7"); - assert_eq!(java_double_to_string(-2.5e-5), "-2.5E-5"); - assert_eq!(java_double_to_string(f64::NAN), "NaN"); - assert_eq!(java_double_to_string(f64::INFINITY), "Infinity"); - assert_eq!(java_double_to_string(f64::NEG_INFINITY), "-Infinity"); + fn rounds_to_two_decimals() { + assert_eq!(format_decimal(0.015), "0.01"); + assert_eq!(format_decimal(0.035), "0.04"); + assert_eq!(format_decimal(1.005), "1"); + assert_eq!(format_decimal(2.675), "2.67"); + assert_eq!(format_decimal(-0.004), "-0"); } #[test] - fn percentage_of_uses_qualimaps_operand_order() { - assert_eq!(percentage_of(1, 3), (100.0 * 1.0) / 3.0); - assert_eq!(format_percentage(percentage_of(6, 10)), "60%"); - assert_eq!(format_percentage(percentage_of(1639, 1889)), "86.77%"); + fn non_finite_values_are_parseable() { + assert_eq!(format_decimal(f64::NAN), "NaN"); + assert_eq!(format_percentage(f64::NAN), "NaN%"); + assert_eq!(format_decimal(f64::INFINITY), "inf"); } } diff --git a/src/rna/qualimap/mod.rs b/src/rna/qualimap/mod.rs index bb567d9..fb740a6 100644 --- a/src/rna/qualimap/mod.rs +++ b/src/rna/qualimap/mod.rs @@ -131,20 +131,24 @@ pub(crate) fn junction_motif_rows( .collect() } -/// Qualimap's strand-specificity estimation (fwd, rev). -/// -/// `ComputeCountsTask.getFwdStrandEstimation` divides in single precision, -/// `fwd / (float)(fwd + rev)`, so the value is computed as an `f32` and only -/// then widened for formatting. Both are NaN when no read overlapped a feature. +/// Strand-specificity estimation (fwd, rev): each strand's share of the reads +/// that overlapped a feature. Both are NaN when there were none. pub(crate) fn strand_estimation(fwd: u64, rev: u64) -> (f64, f64) { - let total = (fwd + rev) as f32; - (f64::from(fwd as f32 / total), f64::from(rev as f32 / total)) + let total = (fwd + rev) as f64; + (fwd as f64 / total, rev as f64 / total) } #[cfg(test)] mod tests { use super::*; + #[test] + fn strand_estimation_is_a_plain_fraction() { + assert_eq!(strand_estimation(1, 3), (0.25, 0.75)); + let (fwd, rev) = strand_estimation(0, 0); + assert!(fwd.is_nan() && rev.is_nan()); + } + fn motif_map(entries: &[(&str, u64)]) -> HashMap { entries.iter().map(|&(m, c)| (m.to_string(), c)).collect() } diff --git a/src/rna/qualimap/output.rs b/src/rna/qualimap/output.rs index a4f9a18..6dee44f 100644 --- a/src/rna/qualimap/output.rs +++ b/src/rna/qualimap/output.rs @@ -10,9 +10,7 @@ use anyhow::{Context, Result}; use log::debug; use super::coverage::TranscriptCoverage; -use super::format::{ - format_decimal, format_long, format_percentage, java_double_to_string, percentage_of, -}; +use super::format::{format_decimal, format_long, format_percentage}; use super::index::QualimapIndex; use super::plots; use super::QualimapResult; @@ -144,7 +142,7 @@ fn compute_bias(entries: &[TranscriptCoverageEntry], order: &[usize]) -> (f64, f .values() .map(|&i| entries[i].mean_coverage) .collect(); - means.sort_by(java_double_cmp); + means.sort_by(nan_last_cmp); let min_mean = means[means.len().saturating_sub(NUM_TRANSCRIPTS_FOR_BIAS + 1)]; let mut five_prime_biases = Vec::new(); @@ -195,13 +193,10 @@ fn compute_bias(entries: &[TranscriptCoverageEntry], order: &[usize]) -> (f64, f ) } -/// Order doubles as Java's `Arrays.sort(double[])` does: NaN sorts after -/// +Infinity (whatever its sign bit) and -0.0 before 0.0. -fn java_double_cmp(a: &f64, b: &f64) -> std::cmp::Ordering { - match (a.is_nan(), b.is_nan()) { - (false, false) => a.total_cmp(b), - (a_nan, b_nan) => a_nan.cmp(&b_nan), - } +/// Order doubles with NaN last (whatever its sign bit). +fn nan_last_cmp(a: &f64, b: &f64) -> std::cmp::Ordering { + a.partial_cmp(b) + .unwrap_or_else(|| a.is_nan().cmp(&b.is_nan())) } /// Commons Math 2.0 `StatUtils.percentile(values, 50)`, as Qualimap uses it. @@ -222,7 +217,7 @@ fn commons_math_median(values: &mut [f64]) -> f64 { let fpos = pos.floor(); let int_pos = fpos as usize; let dif = pos - fpos; - values.sort_by(java_double_cmp); + values.sort_by(nan_last_cmp); if pos < 1.0 { return values[0]; } @@ -267,8 +262,7 @@ fn compute_mean_coverage_histogram( /// Write the coverage profile TSV file. /// /// Format: tab-separated, header line starting with `#`, 100 rows (0.0–99.0). -/// Qualimap writes both columns by string concatenation, i.e. with -/// `Double.toString` (`BamQCChart.exportData`). +/// Values are written with Rust's `Debug` format (shortest round-trip digits, `0.0` for whole numbers). fn write_coverage_profile(profile: &[f64; NUM_BINS], path: &Path) -> Result<()> { use std::fs::File; @@ -278,12 +272,8 @@ fn write_coverage_profile(profile: &[f64; NUM_BINS], path: &Path) -> Result<()> writeln!(f, "#Transcript position\tTranscript coverage profile")?; for (i, &val) in profile.iter().enumerate() { - writeln!( - f, - "{}\t{}", - java_double_to_string(i as f64), - java_double_to_string(val) - )?; + // Positions are whole numbers, written as `0.0` to keep the files' existing format. + writeln!(f, "{:.1}\t{:?}", i as f64, val)?; } Ok(()) @@ -679,7 +669,7 @@ fn write_results_file( f, " {label} {} ({})", format_long(count), - format_percentage(percentage_of(count, total_reads)) + format_percentage(100.0 * count as f64 / total_reads as f64) )?; } writeln!(f)?; diff --git a/src/rna/qualimap/report.rs b/src/rna/qualimap/report.rs index f30e632..c5e42f4 100644 --- a/src/rna/qualimap/report.rs +++ b/src/rna/qualimap/report.rs @@ -9,7 +9,7 @@ use std::path::Path; use anyhow::{Context, Result}; -use super::format::{format_decimal, format_long, format_percentage, percentage_of}; +use super::format::{format_decimal, format_long, format_percentage}; use super::output::{NUM_PRIME_BASES, NUM_TRANSCRIPTS_FOR_BIAS}; use crate::Strandedness; use log::debug; @@ -392,7 +392,7 @@ fn write_summary_section(html: &mut String, data: &ReportData) { format!( "{} / {}", format_long(count), - format_percentage(percentage_of(count, total_reads)) + format_percentage(100.0 * count as f64 / total_reads as f64) ) }; summary_table(