fastqc-rust 1.0.1

A Rust rewrite of FastQC - a quality control tool for high throughput sequence data
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
pub mod line_graph;
pub mod quality_boxplot;
pub mod tile_graph;

/// A simple RGB color struct used across all chart types.
#[derive(Debug, Clone, Copy)]
pub struct ChartColor {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

impl ChartColor {
    pub const fn new(r: u8, g: u8, b: u8) -> Self {
        ChartColor { r, g, b }
    }

    pub fn to_rgb_string(&self) -> String {
        format!("rgb({},{},{})", self.r, self.g, self.b)
    }
}

// Default chart dimensions match Java's JPanel.getPreferredSize() = 800x600
pub const CHART_WIDTH: f64 = 800.0;
pub const CHART_HEIGHT: f64 = 600.0;

// Tol colorblind-safe palette from LineGraph.java
// Note: Java FastQC updated these colours from the original bright palette
// to the Tol scheme at https://davidmathlogic.com/colorblind/
pub const LINE_COLOURS: [ChartColor; 8] = [
    ChartColor::new(136, 34, 85),   // Purple-red
    ChartColor::new(51, 34, 136),   // Indigo
    ChartColor::new(17, 119, 51),   // Green
    ChartColor::new(221, 204, 119), // Yellow-green
    ChartColor::new(68, 170, 153),  // Teal
    ChartColor::new(170, 68, 153),  // Magenta
    ChartColor::new(204, 102, 119), // Pink
    ChartColor::new(136, 204, 238), // Light blue
];

// Java uses Font("Default", Font.PLAIN, 12) for all chart text.
// The SVGGenerator writes font-size="12" but Java AWT renders 12pt at screen DPI
// (typically 96dpi) where it appears slightly larger than SVG's 12px.
const FONT_SIZE: f64 = 12.0;
/// Bold text in Java AWT is approximately 13% wider than plain text.
/// Used when measuring text that will be rendered with font-weight="bold".
pub const BOLD_WIDTH_SCALE: f64 = 1.13;
// Java uses SansSerif which maps to Arial/Helvetica. We specify Liberation Sans
// first (bundled, metric-compatible with Arial) with web-safe fallbacks.
const FONT_FAMILY: &str = "'Liberation Sans', Arial, Helvetica, sans-serif";

/// Approximate the width of a string in pixels at 12pt Arial.
/// Java uses FontMetrics.stringWidth() which measures actual glyph widths.
/// We approximate with 7px per character (roughly correct for 12pt Arial),
/// which gives close-enough layout to match Java output visually.
pub fn approx_text_width(s: &str) -> f64 {
    // Approximate Java's FontMetrics.stringWidth() for Arial 12pt.
    // Digits and uppercase are ~7px, lowercase ~5.5px, spaces ~3px.
    // This produces correct overlap prevention for axis labels (mostly digits)
    // AND correct centering for titles (mixed case text).
    s.chars()
        .map(|c| match c {
            ' ' => 3.4,
            '.' | ',' | ':' | ';' | '!' | 'i' | 'l' | '|' | '(' | ')' => 3.5,
            'm' | 'w' | 'M' | 'W' => 9.0,
            'A'..='Z' | '0'..='9' | '%' | '+' | '>' | '#' => 8.2,
            _ => 5.7, // lowercase and other chars
        })
        .sum()
}

/// Generate the SVG header.
pub fn svg_header(width: f64, height: f64) -> String {
    // Match the SVG header format from SVGGenerator.java
    format!(
        "<?xml version=\"1.0\" standalone=\"no\"?>\n\
         <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n\
         <svg width=\"{}\" height=\"{}\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n",
        width as i32, height as i32
    )
}

/// Generate the SVG footer.
pub fn svg_footer() -> &'static str {
    "</svg>\n"
}

/// Escape text for safe embedding in XML (SVG, XSL-FO, etc.).
pub fn xml_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

/// Emit an SVG text element at the default label font size.
pub fn svg_text(x: f64, y: f64, text: &str, color: &ChartColor, bold: bool) -> String {
    svg_text_sized(x, y, text, color, bold, FONT_SIZE)
}

/// Emit an SVG text element at a specific font size.
fn svg_text_sized(x: f64, y: f64, text: &str, color: &ChartColor, bold: bool, size: f64) -> String {
    let weight = if bold { " font-weight=\"bold\"" } else { "" };
    format!(
        "<text x=\"{}\" y=\"{}\" fill=\"{}\" font-family=\"{}\" font-size=\"{}\"{}>{}</text>\n",
        x as i32,
        y as i32,
        color.to_rgb_string(),
        FONT_FAMILY,
        size as i32,
        weight,
        xml_escape(text)
    )
}

/// Strip `shape-rendering="crispEdges"` from SVG output.
///
/// crispEdges is included in the SVG so that resvg renders pixel-sharp lines
/// and rectangles in PNGs, but it is stripped from saved SVG files to minimise
/// the diff from upstream Java output (which doesn't include it).
pub fn strip_crisp_edges(svg: &str) -> String {
    svg.replace(" shape-rendering=\"crispEdges\"", "")
}

/// Emit an SVG line element.
pub fn svg_line(
    x1: f64,
    y1: f64,
    x2: f64,
    y2: f64,
    color: &ChartColor,
    stroke_width: f64,
) -> String {
    format!(
        "<line x1=\"{}\" y1=\"{}\" x2=\"{}\" y2=\"{}\" stroke=\"{}\" stroke-width=\"{}\" shape-rendering=\"crispEdges\"/>\n",
        x1 as i32,
        y1 as i32,
        x2 as i32,
        y2 as i32,
        color.to_rgb_string(),
        stroke_width as i32
    )
}

/// Emit an SVG filled rectangle.
pub fn svg_rect_filled(x: f64, y: f64, width: f64, height: f64, color: &ChartColor) -> String {
    format!(
        "<rect width=\"{}\" height=\"{}\" x=\"{}\" y=\"{}\" style=\"fill:{};stroke:none\" shape-rendering=\"crispEdges\"/>\n",
        width as i32,
        height as i32,
        x as i32,
        y as i32,
        color.to_rgb_string()
    )
}

/// Emit an SVG stroked rectangle (no fill).
pub fn svg_rect_stroked(x: f64, y: f64, width: f64, height: f64, color: &ChartColor) -> String {
    format!(
        "<rect width=\"{}\" height=\"{}\" x=\"{}\" y=\"{}\" rx=\"0\" ry=\"0\" style=\"fill:none;stroke-width:1;stroke:{}\" shape-rendering=\"crispEdges\"/>\n",
        width as i32,
        height as i32,
        x as i32,
        y as i32,
        color.to_rgb_string()
    )
}

/// Replicates findOptimalYInterval() from LineGraph.java.
/// Finds a nice round interval so there are at most 10 gridlines.
pub fn find_optimal_y_interval(max: f64) -> f64 {
    let mut base = 1.0_f64;
    let divisions = [1.0, 2.0, 2.5, 5.0];

    loop {
        for &d in &divisions {
            let tester = base * d;
            if max / tester <= 10.0 {
                return tester;
            }
        }
        base *= 10.0;
    }
}

/// Format a Y-axis label, stripping trailing ".0" to match Java's behaviour.
/// Java uses `(""+i).replaceAll(".0$", "")`.
pub fn format_y_label(value: f64) -> String {
    let s = format!("{}", value);
    if s.ends_with(".0") {
        s[..s.len() - 2].to_string()
    } else {
        s
    }
}

/// Render a bold, centered title in the plot area.
/// Title is bold and centered between x_offset and the right edge (width - 10).
pub fn render_centered_title(svg: &mut String, title: &str, x_offset: f64, width: f64) {
    let black = ChartColor::new(0, 0, 0);
    let title_w = approx_text_width(title) * BOLD_WIDTH_SCALE;
    let plot_area_center = x_offset + (width - x_offset - 10.0) / 2.0;
    let title_x = plot_area_center - title_w / 2.0;
    svg.push_str(&svg_text(title_x, 30.0, title, &black, true));
}

/// Shared layout state for charts with numeric Y-axis and categorical X-axis.
///
/// Encapsulates the common chart setup pattern used by LineGraph.java
/// and QualityBoxPlot.java: computing xOffset from Y-axis label widths, y_start
/// from the Y interval, and the get_y() mapping function.
pub struct ChartLayout {
    pub width: f64,
    pub height: f64,
    pub x_offset: f64,
    pub y_start: f64,
    pub y_interval: f64,
    pub min_y: f64,
    pub max_y: f64,
}

impl ChartLayout {
    /// Create a chart layout by computing x_offset from Y-axis label widths.
    pub fn new(min_y: f64, max_y: f64, y_interval: f64) -> Self {
        let width = CHART_WIDTH;
        let height = CHART_HEIGHT;

        // yStart calculation matches Java
        let y_start = if min_y % y_interval == 0.0 {
            min_y
        } else {
            y_interval * ((min_y / y_interval) as i64 + 1) as f64
        };

        // Calculate xOffset from widest Y-axis label
        let mut x_offset: f64 = 0.0;
        let mut y_val = y_start;
        while y_val <= max_y + y_interval * 0.001 {
            let label = format_y_label(y_val);
            let w = approx_text_width(&label);
            if w > x_offset {
                x_offset = w;
            }
            y_val += y_interval;
        }
        // Add 5px breathing space, then truncate to int to match Java's `int xOffset` // JAVA COMPAT
        x_offset = (x_offset + 5.0).trunc();

        ChartLayout {
            width,
            height,
            x_offset,
            y_start,
            y_interval,
            min_y,
            max_y,
        }
    }

    /// getY() maps a data value to a pixel Y coordinate.
    /// Matches Java: `(getHeight()-40) - (int)(((getHeight()-80)/(maxY-minY))*(y-minY))`
    /// The inner multiplication result is truncated to int before subtraction. // JAVA COMPAT
    pub fn get_y(&self, value: f64) -> f64 {
        let plot_height = self.height - 80.0;
        let y_range = self.max_y - self.min_y;
        // Java: (int)(NaN) == 0, so NaN values map to height-40 (bottom of plot). // JAVA COMPAT
        let scaled = (plot_height / y_range) * (value - self.min_y);
        (self.height - 40.0) - if scaled.is_nan() { 0.0 } else { scaled.trunc() }
    }

    /// Calculate the width of each data column in the plot area.
    /// Uses floor() to match Java's integer division truncation:
    /// `int baseWidth = (getWidth()-(xOffset+10))/xLabels.length`
    pub fn base_width(&self, num_points: usize) -> f64 {
        ((self.width - self.x_offset - 10.0) / num_points.max(1) as f64)
            .floor()
            .max(1.0)
    }

    /// Half of base_width, truncated to match Java's `baseWidth/2` int division. // JAVA COMPAT
    pub fn half_base_width(&self, num_points: usize) -> f64 {
        (self.base_width(num_points) / 2.0).trunc()
    }

    /// Render gray + white background rectangles matching Java's JPanel default paint.
    pub fn render_background(&self, svg: &mut String) {
        // Gray background then white overlay — matches Java's JPanel default
        // paint which fills with the panel background (238,238,238) first
        svg.push_str(&svg_rect_filled(
            0.0,
            0.0,
            self.width,
            self.height,
            &ChartColor::new(238, 238, 238),
        ));
        svg.push_str(&svg_rect_filled(
            0.0,
            0.0,
            self.width,
            self.height,
            &ChartColor::new(255, 255, 255),
        ));
    }

    /// Render Y-axis labels at each tick interval.
    pub fn render_y_labels(&self, svg: &mut String) {
        let black = ChartColor::new(0, 0, 0);
        let mut y_val = self.y_start;
        while y_val <= self.max_y + self.y_interval * 0.001 {
            let label = format_y_label(y_val);
            let y_pos = self.get_y(y_val);
            // Y-axis labels are left-aligned at x=2, matching Java's
            // g.drawString(label, 2, getY(i)+(ascent/2))
            let label_x = 2.0;
            // Vertically center on gridline by adding ascent/2 (FONT_SIZE/2 ~ 6px)
            svg.push_str(&svg_text(
                label_x,
                y_pos + FONT_SIZE / 2.0,
                &label,
                &black,
                false,
            ));
            y_val += self.y_interval;
        }
    }

    /// Render the X and Y axis lines.
    pub fn render_axes(&self, svg: &mut String) {
        let black = ChartColor::new(0, 0, 0);
        svg.push_str(&svg_line(
            self.x_offset,
            self.height - 40.0,
            self.width - 10.0,
            self.height - 40.0,
            &black,
            1.0,
        ));
        svg.push_str(&svg_line(
            self.x_offset,
            self.height - 40.0,
            self.x_offset,
            40.0,
            &black,
            1.0,
        ));
    }

    /// Render the X-axis label centered below the axis.
    pub fn render_x_axis_label(&self, svg: &mut String, label: &str) {
        let black = ChartColor::new(0, 0, 0);
        let x_label_w = approx_text_width(label);
        let x_label_x = self.width / 2.0 - x_label_w / 2.0;
        svg.push_str(&svg_text(
            x_label_x,
            self.height - 5.0,
            label,
            &black,
            false,
        ));
    }

    /// Render a single X-axis category label at position `i`, if it doesn't overlap
    /// the previous label. Returns the updated `last_x_label_end` value.
    pub fn render_x_category_label_at(
        &self,
        svg: &mut String,
        label: &str,
        i: usize,
        base_width: f64,
        last_x_label_end: f64,
    ) -> f64 {
        let half_bw = (base_width / 2.0).trunc();
        let label_w = approx_text_width(label).trunc();
        let label_x = half_bw + self.x_offset + (base_width * i as f64) - (label_w / 2.0).trunc();
        if label_x > last_x_label_end {
            let black = ChartColor::new(0, 0, 0);
            svg.push_str(&svg_text(label_x, self.height - 25.0, label, &black, false));
            label_x + label_w + 5.0
        } else {
            last_x_label_end
        }
    }

    /// Render horizontal gridlines at each Y-axis tick.
    pub fn render_gridlines(&self, svg: &mut String) {
        let grid_color = ChartColor::new(180, 180, 180);
        let mut y_val = self.y_start;
        while y_val <= self.max_y + self.y_interval * 0.001 {
            let y_pos = self.get_y(y_val);
            svg.push_str(&svg_line(
                self.x_offset,
                y_pos,
                self.width - 10.0,
                y_pos,
                &grid_color,
                1.0,
            ));
            y_val += self.y_interval;
        }
    }
}

/// Convert raw PNG bytes to a `data:image/png;base64,...` URI.
/// Matches ImageToBase64.imageToBase64() which produces
/// "data:image/png;base64,..." encoding for BufferedImage rendered charts.
pub fn png_to_data_uri(png_bytes: &[u8]) -> String {
    use base64::engine::general_purpose::STANDARD as BASE64;
    use base64::Engine;
    format!("data:image/png;base64,{}", BASE64.encode(png_bytes))
}

/// Convert an SVG string to PNG bytes.
///
/// In Java, writeDefaultImage() renders the Swing JPanel to a
/// BufferedImage at the specified dimensions, then encodes via ImageIO.write("PNG").
/// We replicate this by parsing the SVG with resvg and rasterizing via tiny-skia.
// Bundled fonts — no system font dependency.
// Liberation Sans is metric-compatible with Arial (SIL Open Font License).
const FONT_REGULAR: &[u8] = include_bytes!("../../../assets/fonts/LiberationSans-Regular.ttf");
const FONT_BOLD: &[u8] = include_bytes!("../../../assets/fonts/LiberationSans-Bold.ttf");

pub fn svg_to_png(svg: &str, width: u32, height: u32) -> Result<Vec<u8>, String> {
    use resvg::usvg;
    use tiny_skia::Pixmap;

    // Load bundled fonts — no system font dependency
    let mut fontdb = usvg::fontdb::Database::new();
    fontdb.load_font_data(FONT_REGULAR.to_vec());
    fontdb.load_font_data(FONT_BOLD.to_vec());

    let options = usvg::Options {
        fontdb: std::sync::Arc::new(fontdb),
        ..Default::default()
    };

    let tree =
        usvg::Tree::from_str(svg, &options).map_err(|e| format!("Failed to parse SVG: {}", e))?;

    // Create pixel buffer at target dimensions
    let mut pixmap =
        Pixmap::new(width, height).ok_or_else(|| "Failed to create pixel buffer".to_string())?;

    pixmap.fill(tiny_skia::Color::WHITE);

    // Render at identity transform (1:1 pixel mapping) since our SVG dimensions
    // match the target pixmap exactly. Any non-identity scale causes antialiased
    // sub-pixel interpolation that blurs lines and rectangles.
    resvg::render(
        &tree,
        tiny_skia::Transform::identity(),
        &mut pixmap.as_mut(),
    );

    // Encode to PNG
    let mut png_buf = Vec::new();
    {
        let mut encoder = png::Encoder::new(std::io::Cursor::new(&mut png_buf), width, height);
        encoder.set_color(png::ColorType::Rgba);
        encoder.set_depth(png::BitDepth::Eight);
        let mut writer = encoder
            .write_header()
            .map_err(|e| format!("PNG header error: {}", e))?;
        writer
            .write_image_data(pixmap.data())
            .map_err(|e| format!("PNG write error: {}", e))?;
    }

    Ok(png_buf)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_find_optimal_y_interval() {
        // Verify interval calculation matches Java's findOptimalYInterval
        assert_eq!(find_optimal_y_interval(10.0), 1.0);
        assert_eq!(find_optimal_y_interval(20.0), 2.0);
        assert_eq!(find_optimal_y_interval(25.0), 2.5);
        assert_eq!(find_optimal_y_interval(50.0), 5.0);
        assert_eq!(find_optimal_y_interval(100.0), 10.0);
        assert_eq!(find_optimal_y_interval(200.0), 20.0);
    }

    #[test]
    fn test_format_y_label() {
        // Trailing .0 should be stripped
        assert_eq!(format_y_label(10.0), "10");
        assert_eq!(format_y_label(2.5), "2.5");
        assert_eq!(format_y_label(0.0), "0");
    }

    #[test]
    fn test_chart_color_rgb_string() {
        let c = ChartColor::new(255, 128, 0);
        assert_eq!(c.to_rgb_string(), "rgb(255,128,0)");
    }

    #[test]
    fn test_line_graph_renders_valid_svg() {
        use crate::report::charts::line_graph::{render_line_graph, LineGraphData};

        let svg = render_line_graph(&LineGraphData {
            data: vec![vec![1.0, 5.0, 3.0]],
            min_y: 0.0,
            max_y: 10.0,
            x_label: "X".to_string(),
            series_names: vec!["Series 1".to_string()],
            x_categories: vec!["A".to_string(), "B".to_string(), "C".to_string()],
            title: "Test Graph".to_string(),
        });

        assert!(svg.starts_with("<?xml version"));
        assert!(svg.contains("<svg "));
        assert!(svg.contains("</svg>"));
        assert!(svg.contains("Test Graph"));
        assert!(svg.contains("Series 1"));
    }

    #[test]
    fn test_quality_boxplot_renders_valid_svg() {
        use crate::report::charts::quality_boxplot::{render_quality_boxplot, QualityBoxPlotData};

        let svg = render_quality_boxplot(&QualityBoxPlotData {
            means: vec![30.0, 28.0],
            medians: vec![31.0, 29.0],
            lower_quartile: vec![25.0, 24.0],
            upper_quartile: vec![35.0, 33.0],
            lowest: vec![20.0, 18.0],
            highest: vec![38.0, 36.0],
            min_y: 0.0,
            max_y: 40.0,
            y_interval: 2.0,
            x_labels: vec!["1".to_string(), "2".to_string()],
            title: "Test Quality".to_string(),
        });

        assert!(svg.starts_with("<?xml version"));
        assert!(svg.contains("</svg>"));
        assert!(svg.contains("Test Quality"));
        // Check for quality zone colors
        assert!(svg.contains("rgb(195,230,195)")); // GOOD color
        assert!(svg.contains("rgb(240,240,0)")); // BOX_FILL color
    }

    #[test]
    fn test_tile_graph_renders_valid_svg() {
        use crate::report::charts::tile_graph::{render_tile_graph, TileGraphData};

        let svg = render_tile_graph(&TileGraphData {
            x_labels: vec!["1".to_string(), "2".to_string()],
            tiles: vec![1101, 1102],
            tile_base_means: vec![vec![0.5, -0.3], vec![-1.0, 0.2]],
            color_scale_max: 5.0,
        });

        assert!(svg.starts_with("<?xml version"));
        assert!(svg.contains("</svg>"));
        assert!(svg.contains("Quality per tile"));
    }
}