rustqc 0.1.0

Fast RNA-seq QC in a single pass: dupRadar, featureCounts, 8 RSeQC tools, preseq, samtools stats, and Qualimap — reimplemented in Rust
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
//! Terminal UI: styled output, progress bars, and verbosity control.
//!
//! Centralises all user-facing terminal output for RustQC. Provides styled
//! headers, progress bars, summary boxes, and consistent formatting across
//! Normal, Verbose, and Quiet modes.

use console::Style;
use indicatif::{ProgressBar, ProgressStyle};
use std::time::Duration;

// ============================================================================
// Verbosity
// ============================================================================

/// Controls how much output is printed to the terminal.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Verbosity {
    /// Suppress all output except warnings and errors.
    Quiet,
    /// Default: header, config, progress, summary, output checklist.
    Normal,
    /// Show additional detail (per-file writes, intermediate stats, build steps).
    Verbose,
}

// ============================================================================
// Ui
// ============================================================================

/// Terminal UI handle. All user-facing output flows through this struct.
///
/// Methods are no-ops in `Quiet` mode (except `warn` and `error`, which always
/// print). `Verbose` mode shows additional detail beyond `Normal`.
///
/// **Note:** when processing multiple BAMs in parallel, output lines from
/// different files may interleave on stderr.
#[derive(Debug)]
pub struct Ui {
    /// Current verbosity level.
    verbosity: Verbosity,
    // Reusable styles
    /// Bold white for section headers.
    style_header: Style,
    /// Cyan for labels / keys.
    style_label: Style,
    /// Dim for paths and secondary info.
    style_dim: Style,
    /// Green for success markers.
    style_green: Style,
    /// Yellow for warnings.
    style_yellow: Style,
    /// Red for errors.
    style_red: Style,
    /// Bold for emphasis.
    style_bold: Style,
    /// Dim for box borders.
    style_box: Style,
}

impl Ui {
    /// Create a new UI handle with the given verbosity.
    pub fn new(verbosity: Verbosity) -> Self {
        Self {
            verbosity,
            style_header: Style::new().bold(),
            style_label: Style::new().cyan(),
            style_dim: Style::new().dim(),
            style_green: Style::new().green(),
            style_yellow: Style::new().yellow(),
            style_red: Style::new().red().bold(),
            style_bold: Style::new().bold(),
            style_box: Style::new().dim(),
        }
    }

    /// Whether we are in quiet mode.
    fn is_quiet(&self) -> bool {
        self.verbosity == Verbosity::Quiet
    }

    /// Whether we are in verbose mode.
    pub fn is_verbose(&self) -> bool {
        self.verbosity == Verbosity::Verbose
    }

    // ========================================================================
    // Header & config
    // ========================================================================

    /// Print the startup banner.
    pub fn header(&self, version: &str, commit: &str, build: &str) {
        if self.is_quiet() {
            return;
        }
        eprintln!();
        eprintln!(
            "  {} {}",
            self.style_header.apply_to(format!("RustQC v{version}")),
            self.style_dim
                .apply_to(format!("({commit}, built {build})")),
        );
        eprintln!();
    }

    /// Print a key-value config line (e.g. "  Input:      sample.bam").
    pub fn config(&self, key: &str, value: &str) {
        if self.is_quiet() {
            return;
        }
        eprintln!(
            "  {:<13}{}",
            self.style_label.apply_to(format!("{key}:")),
            value,
        );
    }

    /// Print a blank line separator.
    pub fn blank(&self) {
        if self.is_quiet() {
            return;
        }
        eprintln!();
    }

    // ========================================================================
    // Sections & steps
    // ========================================================================

    /// Print a section header (e.g. "  Processing sample.bam").
    pub fn section(&self, text: &str) {
        if self.is_quiet() {
            return;
        }
        eprintln!("  {}", self.style_header.apply_to(text));
    }

    /// Print an indented step line (visible in Normal + Verbose).
    pub fn step(&self, text: &str) {
        if self.is_quiet() {
            return;
        }
        eprintln!("    {text}");
    }

    /// Print an indented detail line (only visible in Verbose).
    pub fn detail(&self, text: &str) {
        if self.verbosity != Verbosity::Verbose {
            return;
        }
        eprintln!("    {}", self.style_dim.apply_to(text));
    }

    // ========================================================================
    // Progress bar
    // ========================================================================

    /// Create a progress bar for BAM read counting.
    ///
    /// Returns a spinner-style bar (total is unknown upfront) that shows the
    /// read count and elapsed time. In Quiet mode, returns a hidden bar.
    pub fn progress_bar(&self) -> ProgressBar {
        if self.is_quiet() {
            return ProgressBar::hidden();
        }
        let pb = ProgressBar::new_spinner();
        pb.set_style(
            ProgressStyle::with_template("    {spinner:.cyan} {msg}  {elapsed:.dim}")
                .expect("valid template")
                .tick_strings(&[
                    "\u{28fe}", "\u{28fd}", "\u{28fb}", "\u{28f7}", "\u{28ef}", "\u{28df}",
                    "\u{28bf}", "\u{287f}", "\u{28fe}",
                ]),
        );
        pb.enable_steady_tick(Duration::from_millis(100));
        pb
    }

    /// Finish the progress bar with a final styled line.
    pub fn finish_progress(&self, pb: &ProgressBar, reads: u64, duration: Duration) {
        if self.is_quiet() {
            pb.finish_and_clear();
            return;
        }
        pb.finish_and_clear();
        eprintln!(
            "    {} {} reads processed  {}",
            self.style_green.apply_to("\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}\u{2501}"),
            self.style_bold.apply_to(format_count(reads)),
            self.style_dim.apply_to(format_duration(duration)),
        );
    }

    // ========================================================================
    // Summary box
    // ========================================================================

    /// Print the per-BAM summary box with key metrics.
    ///
    /// Takes a title and a list of (label, bold_value, annotation) rows.
    #[allow(clippy::too_many_arguments)]
    pub fn summary_box(&self, title: &str, rows: &[(&str, String, String)]) {
        if self.is_quiet() {
            return;
        }
        // Adapt box width to the terminal, clamped to [40, 80].
        let inner_width = console::Term::stderr()
            .size()
            .1
            .saturating_sub(6) // 2 indent + 2 border chars + 2 padding
            as usize;
        let inner_width = inner_width.clamp(40, 80);
        let border_h = "\u{2500}".repeat(inner_width + 2);

        eprintln!();
        eprintln!(
            "  {}",
            self.style_box
                .apply_to(format!("\u{250c}{border_h}\u{2510}"))
        );
        // Title row
        let title_padded = format!(" {title:<width$} ", width = inner_width);
        eprintln!(
            "  {}{}{} ",
            self.style_box.apply_to("\u{2502}"),
            self.style_bold.apply_to(title_padded),
            self.style_box.apply_to("\u{2502}"),
        );
        // Blank separator
        eprintln!(
            "  {}",
            self.style_box.apply_to(format!(
                "\u{2502}{:width$}\u{2502}",
                "",
                width = inner_width + 2
            ))
        );
        // Data rows
        for (label, value, annotation) in rows {
            eprintln!(
                "  {}{}{}",
                self.style_box.apply_to("\u{2502}"),
                format_summary_row(
                    &self.style_label,
                    &self.style_bold,
                    label,
                    value,
                    annotation,
                    inner_width,
                ),
                self.style_box.apply_to("\u{2502}"),
            );
        }
        // Bottom border
        eprintln!(
            "  {}",
            self.style_box
                .apply_to(format!("\u{2514}{border_h}\u{2518}"))
        );
        eprintln!();
    }

    // ========================================================================
    // Output checklist
    // ========================================================================

    /// Print a tool output line: "  ✓ tool_name    path".
    pub fn output_item(&self, tool: &str, path: &str) {
        if self.is_quiet() {
            return;
        }
        eprintln!(
            "    {} {:<22}{}",
            self.style_green.apply_to("\u{2713}"),
            tool,
            self.style_dim.apply_to(path),
        );
    }

    /// Print a tool output sub-detail (verbose only).
    pub fn output_detail(&self, text: &str) {
        if self.verbosity != Verbosity::Verbose {
            return;
        }
        eprintln!("      {}", self.style_dim.apply_to(text));
    }

    /// Print a group header in the output checklist (e.g. "samtools", "RSeQC").
    pub fn output_group(&self, name: &str) {
        if self.is_quiet() {
            return;
        }
        eprintln!("    {}", self.style_label.apply_to(format!("{name}:")));
    }

    // ========================================================================
    // Multi-BAM summary
    // ========================================================================

    /// Print a per-BAM result in the multi-BAM summary.
    pub fn bam_result_ok(&self, name: &str, duration: Duration) {
        if self.is_quiet() {
            return;
        }
        eprintln!(
            "    {} {:<40}{}",
            self.style_green.apply_to("\u{2713}"),
            name,
            self.style_dim.apply_to(format_duration(duration)),
        );
    }

    /// Print a failed BAM result in the multi-BAM summary.
    pub fn bam_result_err(&self, name: &str, error: &str) {
        if self.is_quiet() {
            return;
        }
        eprintln!(
            "    {} {:<40}{}",
            self.style_red.apply_to("\u{2717}"),
            name,
            self.style_red.apply_to(format!("failed: {error}")),
        );
    }

    // ========================================================================
    // Finish
    // ========================================================================

    /// Print a completion line with a label (e.g. BAM stem or "RustQC run finished").
    pub fn finish(&self, label: &str, duration: Duration) {
        if self.is_quiet() {
            return;
        }
        eprintln!(
            "  {} {} {}",
            self.style_green.apply_to("\u{2713}"),
            self.style_header.apply_to(label),
            self.style_dim
                .apply_to(format!("finished in {}", format_duration(duration))),
        );
        eprintln!();
    }

    // ========================================================================
    // Warnings & errors
    // ========================================================================

    /// Print a styled warning (always visible, even in quiet mode).
    pub fn warn(&self, msg: &str) {
        eprintln!(
            "  {} {}",
            self.style_yellow.apply_to("\u{26a0}"),
            self.style_yellow.apply_to(msg),
        );
    }

    /// Print a multi-line warning inside a yellow box with Unicode border.
    ///
    /// The box is drawn with rounded corners and a "Warning" label in the top
    /// border. The first line is rendered **bold** as a heading. Remaining lines
    /// are padded to the box width. Always visible, even in quiet mode.
    pub fn warn_box(&self, lines: &[&str]) {
        // Find the widest line to size the box (minimum 40 chars)
        let content_width = lines.iter().map(|l| l.len()).max().unwrap_or(0).max(40);
        let y = &self.style_yellow;
        let yb = Style::new().yellow().bold();

        // Top border:  ╭─ Warning ─────╮
        let top_label = " Warning ";
        let remaining = content_width + 1 - top_label.len(); // +1 for inner padding
        eprintln!(
            "  {}{}{}",
            y.apply_to("╭─"),
            y.apply_to(top_label),
            y.apply_to(format!("{}", "".repeat(remaining))),
        );
        // Content lines: │ text │
        // First line is bold, rest are normal yellow
        for (i, line) in lines.iter().enumerate() {
            let styled = if i == 0 {
                format!("{}", yb.apply_to(line))
            } else {
                format!("{}", y.apply_to(line))
            };
            // Pad with spaces to fill the box width (use raw len for padding calc)
            let pad = content_width.saturating_sub(line.len());
            eprintln!(
                "  {} {}{} {}",
                y.apply_to(""),
                styled,
                " ".repeat(pad),
                y.apply_to(""),
            );
        }
        // Bottom border: ╰──────────╯
        eprintln!(
            "  {}",
            y.apply_to(format!("{}", "".repeat(content_width + 2))),
        );
    }

    /// Print a styled error (always visible, even in quiet mode).
    pub fn error(&self, msg: &str) {
        eprintln!(
            "  {} {}",
            self.style_red.apply_to("\u{2717} error:"),
            self.style_red.apply_to(msg),
        );
    }
}

// ============================================================================
// Formatting helpers
// ============================================================================

/// Format a summary box row with styled label, value, and annotation.
///
/// Returns the full inner content string (already padded to `inner_width + 2`).
fn format_summary_row(
    style_label: &Style,
    style_bold: &Style,
    label: &str,
    value: &str,
    annotation: &str,
    inner_width: usize,
) -> String {
    let styled_label = style_label.apply_to(format!("{label:<16}"));
    let styled_value = style_bold.apply_to(format!("{value:>6}"));
    let content = if annotation.is_empty() {
        format!("   {styled_label}{styled_value}")
    } else {
        format!("   {styled_label}{styled_value}  {annotation}")
    };
    // We need to pad based on visible length, not byte length (ANSI codes add bytes).
    // Use console::measure_text_width for accurate visible width.
    let visible_width = console::measure_text_width(&content);
    let target = inner_width + 2;
    if visible_width < target {
        format!("{content}{:width$}", "", width = target - visible_width)
    } else {
        content
    }
}

/// Format a count with SI prefix (e.g. 48200000 → "48.2M").
///
/// Values below 1000 are shown as-is. Values above use K/M/G/T suffixes
/// with one decimal place.
pub fn format_count(n: u64) -> String {
    use number_prefix::NumberPrefix;
    match NumberPrefix::decimal(n as f64) {
        NumberPrefix::Standalone(n) => format!("{n}"),
        NumberPrefix::Prefixed(prefix, n) => {
            // Map SI prefixes to short single-char suffixes
            let suffix = match prefix {
                number_prefix::Prefix::Kilo => "K",
                number_prefix::Prefix::Mega => "M",
                number_prefix::Prefix::Giga => "G",
                number_prefix::Prefix::Tera => "T",
                _ => return format!("{:.1}{prefix:?}", n),
            };
            format!("{n:.1}{suffix}")
        }
    }
}

/// Format a percentage string (e.g. "83.3%").
pub fn format_pct(n: u64, total: u64) -> String {
    if total == 0 {
        return "(0.0%)".to_string();
    }
    format!("({:.1}%)", n as f64 / total as f64 * 100.0)
}

/// Format a duration as human-friendly mm:ss or h:mm:ss.
///
/// - Under 60s: "45.2s"
/// - Under 1h: "1:23"
/// - Over 1h: "1:02:34"
pub fn format_duration(d: Duration) -> String {
    let total_secs = d.as_secs_f64();
    if total_secs < 60.0 {
        return format!("{total_secs:.1}s");
    }
    let total_secs = d.as_secs();
    let hours = total_secs / 3600;
    let minutes = (total_secs % 3600) / 60;
    let seconds = total_secs % 60;
    if hours > 0 {
        format!("{hours}:{minutes:02}:{seconds:02}")
    } else {
        format!("{minutes}:{seconds:02}")
    }
}

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

    #[test]
    fn test_format_count_small() {
        assert_eq!(format_count(0), "0");
        assert_eq!(format_count(42), "42");
        assert_eq!(format_count(999), "999");
    }

    #[test]
    fn test_format_count_thousands() {
        assert_eq!(format_count(1000), "1.0K");
        assert_eq!(format_count(1500), "1.5K");
        assert_eq!(format_count(50000), "50.0K");
    }

    #[test]
    fn test_format_count_millions() {
        assert_eq!(format_count(1_000_000), "1.0M");
        assert_eq!(format_count(48_200_000), "48.2M");
        assert_eq!(format_count(50_000_000), "50.0M");
    }

    #[test]
    fn test_format_count_billions() {
        assert_eq!(format_count(1_000_000_000), "1.0G");
        assert_eq!(format_count(5_000_000_000), "5.0G");
    }

    #[test]
    fn test_format_pct() {
        assert_eq!(format_pct(833, 1000), "(83.3%)");
        assert_eq!(format_pct(0, 0), "(0.0%)");
        assert_eq!(format_pct(1000, 1000), "(100.0%)");
    }

    #[test]
    fn test_format_duration_seconds() {
        assert_eq!(format_duration(Duration::from_secs_f64(0.5)), "0.5s");
        assert_eq!(format_duration(Duration::from_secs_f64(45.2)), "45.2s");
        assert_eq!(format_duration(Duration::from_secs_f64(59.9)), "59.9s");
    }

    #[test]
    fn test_format_duration_minutes() {
        assert_eq!(format_duration(Duration::from_secs(60)), "1:00");
        assert_eq!(format_duration(Duration::from_secs(83)), "1:23");
        assert_eq!(format_duration(Duration::from_secs(3599)), "59:59");
    }

    #[test]
    fn test_format_duration_hours() {
        assert_eq!(format_duration(Duration::from_secs(3600)), "1:00:00");
        assert_eq!(format_duration(Duration::from_secs(3754)), "1:02:34");
    }
}