Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions src/rna/qualimap/plots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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]
Expand Down
35 changes: 32 additions & 3 deletions src/rna/rseqc/plots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()?;

Expand Down Expand Up @@ -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()?;

Expand Down Expand Up @@ -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
// ============================================================================
Expand All @@ -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
Expand Down
Loading