From 5ee7a14df97a0de8d36f7c63c8467fd7c4a95764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rayan=20Hassa=C3=AFne?= Date: Thu, 3 Dec 2026 13:46:16 +0100 Subject: [PATCH] fix(plots): stop axis labels repeating or collapsing to 0 Three plots truncated fractional tick values to whole numbers, so neighbouring ticks printed the same label: - RSeQC read duplication: plotters picked a 0.5 step, giving "0, 0, 1, 1, 2, 2". Ask for one tick per integer, as RSeQC's axis(side=2, at=0:ym) does. - RSeQC junction saturation: small junction counts (in thousands) all printed as "0". Show the decimals, as R's default axis does. - Qualimap coverage profile and coverage histogram: values >= 1 were rounded, giving "1, 2, 2, 3". Keep up to two decimals, with thousands separators. Only the y-axis labels change; plotted data is unchanged. Co-Authored-By: Claude Opus 5.5 --- src/rna/qualimap/plots.rs | 50 ++++++++++++++++++++++----------------- src/rna/rseqc/plots.rs | 35 ++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/rna/qualimap/plots.rs b/src/rna/qualimap/plots.rs index 4fd83b9d..a4a1a068 100644 --- a/src/rna/qualimap/plots.rs +++ b/src/rna/qualimap/plots.rs @@ -727,35 +727,36 @@ where // ============================= Helpers ========================================= /// Format Y-axis labels with comma separators (e.g., 675000 → "675,000"). +/// +/// Fractional ticks keep up to two decimals (2.5 → "2.5") instead of being +/// rounded, which used to print the same integer for neighbouring ticks. fn format_y_label(val: f64) -> String { - if val == 0.0 { + let s = format!("{:.2}", val); + let s = s.trim_end_matches('0').trim_end_matches('.'); + if s == "-0" || s.is_empty() { return "0".to_string(); } - // For small values (< 1), show decimal - if val.abs() < 1.0 { - let s = format!("{:.2}", val); - return s.trim_end_matches('0').trim_end_matches('.').to_string(); - } - - let n = val.round() as i64; - let s = n.to_string(); - let bytes = s.as_bytes(); - let len = bytes.len(); - let negative = bytes[0] == b'-'; - let start = if negative { 1 } else { 0 }; - let digit_len = len - start; - - if digit_len <= 3 { - return s; - } + let (sign, unsigned) = match s.strip_prefix('-') { + Some(rest) => ("-", rest), + None => ("", s), + }; + let (int_part, frac_part) = match unsigned.split_once('.') { + Some((i, f)) => (i, Some(f)), + None => (unsigned, None), + }; - let mut result = String::with_capacity(len + (digit_len - 1) / 3); - for (i, &b) in bytes.iter().enumerate() { - if i > start && (len - i).is_multiple_of(3) { + let mut result = String::with_capacity(s.len() + int_part.len() / 3); + result.push_str(sign); + for (i, c) in int_part.chars().enumerate() { + if i > 0 && (int_part.len() - i).is_multiple_of(3) { result.push(','); } - result.push(b as char); + result.push(c); + } + if let Some(f) = frac_part { + result.push('.'); + result.push_str(f); } result } @@ -831,6 +832,11 @@ mod tests { assert_eq!(format_y_label(1000.0), "1,000"); assert_eq!(format_y_label(675000.0), "675,000"); assert_eq!(format_y_label(1234567.0), "1,234,567"); + assert_eq!(format_y_label(0.25), "0.25"); + assert_eq!(format_y_label(1.5), "1.5"); + assert_eq!(format_y_label(2.0), "2"); + assert_eq!(format_y_label(2500.5), "2,500.5"); + assert_eq!(format_y_label(-1500.0), "-1,500"); } #[test] diff --git a/src/rna/rseqc/plots.rs b/src/rna/rseqc/plots.rs index 9cfd96c1..4eef1266 100644 --- a/src/rna/rseqc/plots.rs +++ b/src/rna/rseqc/plots.rs @@ -610,7 +610,7 @@ where /// - Y-axis values divided by 1000 /// - No gridlines, black border around plot area /// - X-axis labels every 20% (matching R's default tick selection for 0–100) -/// - Integer axis labels (no decimals) +/// - Axis labels without trailing zeros (fractional only when R's ticks are) /// /// # Arguments /// * `result` — junction saturation analysis results @@ -737,7 +737,9 @@ where .axis_desc_style(("sans-serif", ps(14.0))) .x_labels(6) // 0, 20, 40, 60, 80, 100 .x_label_formatter(&|v| format!("{}", *v as i64)) - .y_label_formatter(&|v| format!("{}", *v as i64)) + // R picks fractional ticks for small ranges, so keep the decimals + // rather than truncating 0.5 to "0". + .y_label_formatter(&|v| format_tick(*v)) .disable_mesh() // no gridlines (matching upstream R default) .draw()?; @@ -1002,7 +1004,10 @@ where .axis_desc_style(("sans-serif", ps(12.0))) .x_labels(6) // 0, 100, 200, 300, 400, 500 .x_label_formatter(&|v| format!("{}", *v as i64)) - .y_label_formatter(&|v| format!("{}", *v as i64)) + // axis(side=2, at=0:ym): one tick per integer. With the default tick + // count plotters picks a 0.5 step, which printed every integer twice. + .y_labels(ym as usize + 1) + .y_label_formatter(&|v| format_tick(*v)) .disable_mesh() // no gridlines (matching upstream R default) .draw()?; @@ -1191,6 +1196,20 @@ where Ok(()) } +/// Format an axis tick value without trailing zeros: 3.0 → "3", 2.5 → "2.5". +/// +/// Rounds to six decimals first, so floating-point noise in tick positions +/// (0.30000000000000004) doesn't leak into the label. +fn format_tick(v: f64) -> String { + let s = format!("{:.6}", v); + let s = s.trim_end_matches('0').trim_end_matches('.'); + if s == "-0" { + "0".to_string() + } else { + s.to_string() + } +} + // ============================================================================ // Tests // ============================================================================ @@ -1199,6 +1218,16 @@ where mod tests { use super::*; + #[test] + fn test_format_tick() { + assert_eq!(format_tick(0.0), "0"); + assert_eq!(format_tick(-0.0), "0"); + assert_eq!(format_tick(3.0), "3"); + assert_eq!(format_tick(2.5), "2.5"); + assert_eq!(format_tick(0.1 + 0.2), "0.3"); + assert_eq!(format_tick(150.0), "150"); + } + #[test] fn test_compute_weighted_mean_sd() { // Symmetric distribution centered at 0