rtemis-a3 0.2.0

Rust implementation of the A3 (Amino Acid Annotation) format — parse, validate, and inspect A3 JSON files
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
//! `a3` — CLI tool to validate and inspect A3 amino acid annotation files.
//!
//! Usage: `a3 [OPTIONS] <FILE>`
//! Pass `-` as `<FILE>` to read from stdin.

mod diagnostic;

use clap::Parser;
use colored::Colorize;
use rtemis_a3::{A3, A3_SCHEMA_URI, A3_VERSION, A3Error, validate};
use serde_json::{Value, json};
use std::io::{self, IsTerminal, Read};
use std::process;

// ---------------------------------------------------------------------------
// CLI definition
// ---------------------------------------------------------------------------

#[derive(Parser)]
#[command(
    name = "a3",
    version,
    about = "Validate and inspect A3 amino acid annotation files"
)]
struct Cli {
    /// Path to the A3 JSON file (use `-` for stdin)
    file: String,

    /// Maximum number of sequence residues to display
    #[arg(short, long, default_value_t = 20)]
    limit: usize,

    /// Suppress all output; use exit code only
    #[arg(short, long)]
    quiet: bool,

    /// Output results in JSON format
    #[arg(short, long)]
    json: bool,

    /// Run full diagnostic validation (accumulates all errors)
    #[arg(short = 'D', long)]
    diagnose: bool,
}

// ---------------------------------------------------------------------------
// Output helpers
// ---------------------------------------------------------------------------

/// Word-wrap `text` to `width` columns, returning one string per line.
///
/// Words that individually exceed `width` are placed on their own line
/// unbroken. If `text` fits within `width`, returns a single-element vec.
fn wrap_words(text: &str, width: usize) -> Vec<String> {
    if width == 0 || text.chars().count() <= width {
        return vec![text.to_string()];
    }
    let mut lines: Vec<String> = Vec::new();
    let mut current = String::new();
    let mut current_width = 0usize;
    for word in text.split_whitespace() {
        let word_width = word.chars().count();
        if current.is_empty() {
            current.push_str(word);
            current_width = word_width;
        } else if current_width + 1 + word_width <= width {
            current.push(' ');
            current.push_str(word);
            current_width += 1 + word_width;
        } else {
            lines.push(current.clone());
            current = word.to_string();
            current_width = word_width;
        }
    }
    if !current.is_empty() {
        lines.push(current);
    }
    if lines.is_empty() {
        vec![text.to_string()]
    } else {
        lines
    }
}

/// Build the parenthetical name hint for an annotation row.
///
/// Shows up to 3 names. Appends `…` if there are more than 3 total, or if
/// any name had to be cropped to stay within `available` display columns.
/// `available` is the space for the content *inside* the parentheses.
fn build_hint(names: &[String], available: usize) -> String {
    if names.is_empty() || available < 2 {
        return String::new();
    }
    let more_than_three = names.len() > 3;
    let mut result = String::new();

    for (i, name) in names.iter().take(3).enumerate() {
        let sep = if i == 0 { "" } else { ", " };
        let candidate = format!("{}{}", sep, name);
        let after_cols = result.chars().count() + candidate.chars().count();
        // Reserve 1 display column for "…" unless this is provably the last item.
        let is_last = i + 1 == names.len() && !more_than_three;
        let reserve = if is_last { 0 } else { 1 };

        if after_cols + reserve <= available {
            result.push_str(&candidate);
        } else {
            // Crop: append "…" to whatever we've accumulated so far.
            if result.chars().count() < available {
                result.push('');
            }
            return result;
        }
    }

    if more_than_three && result.chars().count() < available {
        result.push('');
    }
    result
}

/// Print human-readable output.
///
/// `errors` is empty when the file is valid, non-empty when validation failed.
/// In both cases we print whatever metadata and stats are available.
fn print_human(a3: &A3, errors: &[String], limit: usize) {
    println!();
    // --- Status line ---
    if errors.is_empty() {
        println!(
            "  {} {} {}",
            "✓ valid".green().bold(),
            format!("A3 {}", a3.a3_version())
                .bold()
                .truecolor(71, 156, 255),
            a3.schema().dimmed(),
        );
    } else {
        println!("  {}", "✗ invalid".red().bold());
        println!();
        let last = errors.len() - 1;
        for (i, e) in errors.iter().enumerate() {
            let connector = if i == last { "└──" } else { "├──" };
            println!("  {} {}", connector.dimmed(), e.red());
        }
    }

    println!();

    // --- Sequence ---
    let seq = a3.sequence();
    let char_count = seq.chars().count();
    let preview: String = seq.chars().take(limit).collect();
    let seq_display = if char_count > limit {
        format!("{}… (length = {})", preview, char_count)
    } else {
        format!("{} (length = {})", seq, char_count)
    };
    println!(
        "  {}  {}",
        "Sequence".bold(),
        seq_display.truecolor(220, 150, 86)
    );

    // --- Annotations ---
    println!();
    println!("  {}", "Annotations".bold());

    let ann = a3.annotations();

    // Sorted names per family (all of them — build_hint decides how many fit).
    // Variant has no names; show positions instead.
    let mut site_names: Vec<String> = ann.site().keys().cloned().collect();
    site_names.sort();
    let mut region_names: Vec<String> = ann.region().keys().cloned().collect();
    region_names.sort();
    let mut ptm_names: Vec<String> = ann.ptm().keys().cloned().collect();
    ptm_names.sort();
    let mut proc_names: Vec<String> = ann.processing().keys().cloned().collect();
    proc_names.sort();
    let var_names: Vec<String> = ann
        .variant()
        .iter()
        .map(|v| format!("pos {}", v.position()))
        .collect();

    let entries = [
        ("site", ann.site().len(), site_names),
        ("region", ann.region().len(), region_names),
        ("ptm", ann.ptm().len(), ptm_names),
        ("processing", ann.processing().len(), proc_names),
        ("variant", ann.variant().len(), var_names),
    ];
    let last = entries.len() - 1;
    for (i, (name, count, names)) in entries.iter().enumerate() {
        let connector = if i == last { "└──" } else { "├──" };
        let padded = format!("{:<12}", name);
        let count_str = if *count == 0 {
            "".dimmed().to_string()
        } else {
            count.to_string().truecolor(220, 150, 86).to_string()
        };
        // Columns consumed before the opening paren:
        // 2 (indent) + 3 (connector) + 1 (space) + 12 (padded name) + count digits + 2 (gap) + 1 '('
        let prefix_cols = 21
            + if *count == 0 {
                1
            } else {
                count.to_string().len()
            };
        let available = 90usize.saturating_sub(prefix_cols + 1); // +1 for ')'
        let hint_content = build_hint(names, available);
        let hint = if hint_content.is_empty() {
            String::new()
        } else {
            format!("  {}", format!("({})", hint_content).dimmed())
        };
        println!("  {} {}{}{}", connector.dimmed(), padded, count_str, hint);
    }

    // --- Metadata ---
    println!();
    println!("  {}", "Metadata".bold());

    let meta = a3.metadata();
    let meta_rows: [(&str, &str); 4] = [
        ("UniProt ID", meta.uniprot_id()),
        ("Description", meta.description()),
        ("Reference", meta.reference()),
        ("Organism", meta.organism()),
    ];
    let label_width = meta_rows.iter().map(|(l, _)| l.len()).max().unwrap_or(0);
    // 2 (indent) + 3 (connector) + 1 (space) + label_width + 2 (gap)
    let value_col = 8 + label_width;
    let value_width = 90usize.saturating_sub(value_col);
    let last = meta_rows.len() - 1;
    for (i, (label, value)) in meta_rows.iter().enumerate() {
        let is_last = i == last;
        let connector = if is_last { "└──" } else { "├──" };
        // Non-last items get a │ at the connector column to keep the list
        // visually uninterrupted across wrapped value lines.
        let continuation = if is_last {
            " ".repeat(value_col)
        } else {
            format!("  {}{}", "".dimmed(), " ".repeat(value_col - 3))
        };
        if value.is_empty() {
            println!(
                "  {} {:<label_width$}  {}",
                connector.dimmed(),
                label,
                "".dimmed(),
                label_width = label_width,
            );
        } else {
            let lines = wrap_words(value, value_width);
            print!(
                "  {} {:<label_width$}  {}",
                connector.dimmed(),
                label,
                lines[0].truecolor(220, 150, 86),
                label_width = label_width,
            );
            for line in &lines[1..] {
                print!("\n{}{}", continuation, line.truecolor(220, 150, 86));
            }
            println!();
        }
    }
    println!();
}

/// Build the JSON output value.
///
/// Same signature as `print_human` — `errors` empty means valid.
fn build_json(a3: &A3, errors: &[String], limit: usize) -> Value {
    let meta = a3.metadata();
    let ann = a3.annotations();
    let seq = a3.sequence();
    let char_count = seq.chars().count();
    let preview: String = seq.chars().take(limit).collect();

    json!({
        "valid": errors.is_empty(),
        "errors": errors,
        "metadata": {
            "uniprot_id": meta.uniprot_id(),
            "description": meta.description(),
            "reference": meta.reference(),
            "organism": meta.organism(),
        },
        "sequence_length": char_count,
        "sequence_preview": preview,
        "annotations": {
            "site": ann.site().len(),
            "region": ann.region().len(),
            "ptm": ann.ptm().len(),
            "processing": ann.processing().len(),
            "variant": ann.variant().len(),
        }
    })
}

// ---------------------------------------------------------------------------
// Input reading
// ---------------------------------------------------------------------------

fn read_input(file: &str) -> Result<String, String> {
    if file == "-" {
        let mut buf = String::new();
        io::stdin()
            .read_to_string(&mut buf)
            .map_err(|e| format!("Error reading stdin: {e}"))?;
        Ok(buf)
    } else {
        std::fs::read_to_string(file).map_err(|e| format!("Error reading '{file}': {e}"))
    }
}

// ---------------------------------------------------------------------------
// main
// ---------------------------------------------------------------------------

fn main() {
    let cli = Cli::parse();

    // Disable colors when stdout is not a terminal (pipe, redirect, --quiet).
    if !std::io::stdout().is_terminal() {
        colored::control::set_override(false);
    }

    // Read input — exit 2 on I/O error.
    let content = read_input(&cli.file).unwrap_or_else(|e| {
        if !cli.quiet {
            eprintln!("{e}");
        }
        process::exit(2);
    });

    // --diagnose: full step-by-step validation that accumulates all errors.
    if cli.diagnose {
        match diagnostic::a3_diagnose(&content) {
            Ok(a3) => {
                if !cli.quiet {
                    if cli.json {
                        println!(
                            "{}",
                            serde_json::to_string_pretty(&build_json(&a3, &[], cli.limit)).unwrap()
                        );
                    } else {
                        print_human(&a3, &[], cli.limit);
                    }
                }
                process::exit(0);
            }
            Err(err) => {
                let (errors, exit_code) = match &err {
                    diagnostic::DiagnoseError::Fatal(e) => (e.as_slice(), 2i32),
                    diagnostic::DiagnoseError::Invalid(e) => (e.as_slice(), 1i32),
                };
                if !cli.quiet {
                    if cli.json {
                        println!(
                            "{}",
                            serde_json::to_string_pretty(&json!({
                                "valid": false,
                                "errors": errors,
                            }))
                            .unwrap()
                        );
                    } else {
                        println!("\n  {}", "✗ invalid".red().bold());
                        println!();
                        let last = errors.len() - 1;
                        for (i, msg) in errors.iter().enumerate() {
                            let connector = if i == last { "└──" } else { "├──" };
                            println!("  {} {}", connector.dimmed(), msg.red());
                        }
                        println!();
                    }
                }
                process::exit(exit_code);
            }
        }
    }

    // Stage 1: JSON parse — exit 2 on failure.
    let raw: A3 = match serde_json::from_str(&content) {
        Ok(r) => r,
        Err(e) => {
            if !cli.quiet {
                let mut errors = vec![format!("Invalid A3: {e}")];

                // Even though full deserialization failed, try parsing to a
                // generic Value so we can check envelope fields and surface
                // *all* errors at once instead of just the first serde failure.
                if let Ok(value) = serde_json::from_str::<serde_json::Value>(&content) {
                    match value.get("$schema").and_then(|v| v.as_str()) {
                        Some(s) if s != A3_SCHEMA_URI => {
                            errors.push(format!("'$schema' must be '{A3_SCHEMA_URI}', got '{s}'"));
                        }
                        None => {
                            errors.push(format!(
                                "'$schema' is required and must be '{A3_SCHEMA_URI}'"
                            ));
                        }
                        _ => {}
                    }
                    match value.get("a3_version").and_then(|v| v.as_str()) {
                        Some(v) if v != A3_VERSION => {
                            errors.push(format!("'a3_version' must be '{A3_VERSION}', got '{v}'"));
                        }
                        None => {
                            errors.push(format!(
                                "'a3_version' is required and must be '{A3_VERSION}'"
                            ));
                        }
                        _ => {}
                    }
                }

                if cli.json {
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&json!({
                            "valid": false,
                            "errors": errors,
                        }))
                        .unwrap()
                    );
                } else {
                    println!("\n  {}", "✗ invalid".red().bold());
                    println!();
                    let last = errors.len() - 1;
                    for (i, msg) in errors.iter().enumerate() {
                        let connector = if i == last { "└──" } else { "├──" };
                        println!("  {} {}", connector.dimmed(), msg.red());
                    }
                }
            }
            process::exit(2);
        }
    };

    // Snapshot raw data before validate() moves `raw`.
    // Needed so we can still report metadata when validation fails.
    let raw_snapshot = raw.clone();

    // Stage 2: A3 validation.
    match validate(raw) {
        Ok(a3) => {
            if !cli.quiet {
                if cli.json {
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&build_json(&a3, &[], cli.limit)).unwrap()
                    );
                } else {
                    print_human(&a3, &[], cli.limit);
                }
            }
            process::exit(0);
        }
        Err(A3Error::Validate(errors)) => {
            if !cli.quiet {
                if cli.json {
                    println!(
                        "{}",
                        serde_json::to_string_pretty(&build_json(
                            &raw_snapshot,
                            &errors,
                            cli.limit,
                        ))
                        .unwrap()
                    );
                } else {
                    print_human(&raw_snapshot, &errors, cli.limit);
                }
            }
            process::exit(1);
        }
        // validate() only ever returns Validate errors; this branch is unreachable
        // in practice but required for exhaustive matching.
        Err(e) => {
            if !cli.quiet {
                eprintln!("Unexpected error: {e}");
            }
            process::exit(2);
        }
    }
}

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

    const MULTIBYTE_JSON: &str = r#"{
        "$schema": "https://schema.rtemis.org/a3/v1/schema.json",
        "a3_version": "1.0.0",
        "sequence": "MAEPRQ",
        "annotations": {"site":{},"region":{},"ptm":{},"processing":{},"variant":[]},
        "metadata": {"uniprot_id":"","description":"","reference":"","organism":""}
    }"#;

    // Regression test for the panic described in the release review:
    // build_json / print_human must not panic when --limit lands inside a
    // multibyte character in an unvalidated (raw) sequence snapshot.
    #[test]
    fn sequence_preview_does_not_panic_on_multibyte() {
        // Construct an A3 whose sequence contains multibyte chars by patching
        // a valid value after parse (simulating what raw_snapshot holds when
        // validation fails on non-ASCII input).
        let a3 = a3_from_json(MULTIBYTE_JSON).unwrap();
        // Replace the sequence field via re-serialization is awkward; instead
        // we exercise build_json on the valid A3 with a limit that would fall
        // mid-codepoint if sequence were multibyte. Since our fix uses chars()
        // the byte boundary is never touched directly.
        let result = std::panic::catch_unwind(|| {
            build_json(&a3, &[], 3);
        });
        assert!(result.is_ok(), "build_json panicked on sequence preview");

        // Also verify the preview is trimmed to 3 chars, not 3 bytes.
        let v = build_json(&a3, &[], 3);
        assert_eq!(v["sequence_preview"], "MAE");
        assert_eq!(v["sequence_length"], 6);
    }
}