ferro-hgvs 0.2.0

HGVS variant normalizer - part of the ferro bioinformatics toolkit
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! Validation levels for HGVS parsing
//!
//! This module provides configurable validation strictness for parsing
//! HGVS variant strings. Different levels allow for varying degrees of
//! flexibility vs. spec compliance.

use std::sync::atomic::{AtomicU8, Ordering};

/// Validation level for HGVS parsing
///
/// Controls how strictly the parser adheres to the HGVS specification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u8)]
pub enum ValidationLevel {
    /// Lenient validation - accepts common typos and variations
    ///
    /// - Accepts lowercase accession prefixes (nm_, nc_)
    /// - Accepts missing version numbers (NM_000088)
    /// - Accepts common typos in amino acid names
    /// - Accepts c: instead of c.
    Lenient = 0,

    /// Standard validation - common clinical usage
    ///
    /// - Requires proper case for accession prefixes
    /// - Allows missing version numbers
    /// - Requires valid amino acid codes
    /// - Requires proper separator (c.)
    #[default]
    Standard = 1,

    /// Strict validation - full HGVS spec compliance
    ///
    /// - Requires proper case for accession prefixes
    /// - Requires version numbers for RefSeq accessions
    /// - Requires valid amino acid codes
    /// - Requires proper separator (c.)
    /// - Validates position ranges
    Strict = 2,
}

impl ValidationLevel {
    /// Check if this level is at least as strict as another
    pub fn at_least(&self, other: ValidationLevel) -> bool {
        (*self as u8) >= (other as u8)
    }

    /// Check if this level allows lenient parsing
    pub fn is_lenient(&self) -> bool {
        *self == ValidationLevel::Lenient
    }

    /// Check if this level requires strict validation
    pub fn is_strict(&self) -> bool {
        *self == ValidationLevel::Strict
    }
}

// Global validation level for the parser
static GLOBAL_VALIDATION_LEVEL: AtomicU8 = AtomicU8::new(ValidationLevel::Standard as u8);

/// Get the current global validation level
pub fn get_validation_level() -> ValidationLevel {
    match GLOBAL_VALIDATION_LEVEL.load(Ordering::Relaxed) {
        0 => ValidationLevel::Lenient,
        1 => ValidationLevel::Standard,
        _ => ValidationLevel::Strict,
    }
}

/// Set the global validation level
///
/// Note: This is a global setting and affects all parsers in the process.
/// For thread-local validation, use `ParseConfig` instead.
pub fn set_validation_level(level: ValidationLevel) {
    GLOBAL_VALIDATION_LEVEL.store(level as u8, Ordering::Relaxed);
}

/// Run a block with a specific validation level, restoring the original level after
pub fn with_validation_level<T, F: FnOnce() -> T>(level: ValidationLevel, f: F) -> T {
    let old_level = get_validation_level();
    set_validation_level(level);
    let result = f();
    set_validation_level(old_level);
    result
}

/// Parse configuration with validation settings
#[derive(Debug, Clone, Default)]
pub struct ParseConfig {
    /// Validation level
    pub level: ValidationLevel,
    /// Whether to allow missing version numbers
    pub allow_missing_version: bool,
    /// Whether to allow lowercase accession prefixes
    pub allow_lowercase_prefix: bool,
    /// Whether to allow c: instead of c.
    pub allow_colon_separator: bool,
}

impl ParseConfig {
    /// Create a new parse configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a lenient configuration
    pub fn lenient() -> Self {
        Self {
            level: ValidationLevel::Lenient,
            allow_missing_version: true,
            allow_lowercase_prefix: true,
            allow_colon_separator: true,
        }
    }

    /// Create a standard configuration
    pub fn standard() -> Self {
        Self {
            level: ValidationLevel::Standard,
            allow_missing_version: true,
            allow_lowercase_prefix: false,
            allow_colon_separator: false,
        }
    }

    /// Create a strict configuration
    pub fn strict() -> Self {
        Self {
            level: ValidationLevel::Strict,
            allow_missing_version: false,
            allow_lowercase_prefix: false,
            allow_colon_separator: false,
        }
    }

    /// Set the validation level
    pub fn with_level(mut self, level: ValidationLevel) -> Self {
        self.level = level;
        self
    }

    /// Allow missing version numbers
    pub fn with_allow_missing_version(mut self, allow: bool) -> Self {
        self.allow_missing_version = allow;
        self
    }
}

/// Validation result with optional warnings
#[derive(Debug, Clone)]
pub struct ValidationResult {
    /// Whether the variant is valid
    pub valid: bool,
    /// Warnings (non-fatal issues)
    pub warnings: Vec<ValidationWarning>,
    /// Errors (fatal issues)
    pub errors: Vec<ValidationError>,
}

impl ValidationResult {
    /// Create a valid result with no issues
    pub fn ok() -> Self {
        Self {
            valid: true,
            warnings: Vec::new(),
            errors: Vec::new(),
        }
    }

    /// Add a warning
    pub fn with_warning(mut self, warning: ValidationWarning) -> Self {
        self.warnings.push(warning);
        self
    }

    /// Add an error
    pub fn with_error(mut self, error: ValidationError) -> Self {
        self.valid = false;
        self.errors.push(error);
        self
    }

    /// Check if there are any warnings
    pub fn has_warnings(&self) -> bool {
        !self.warnings.is_empty()
    }

    /// Check if there are any errors
    pub fn has_errors(&self) -> bool {
        !self.errors.is_empty()
    }
}

/// Validation warning
#[derive(Debug, Clone)]
pub struct ValidationWarning {
    /// Warning code
    pub code: &'static str,
    /// Warning message
    pub message: String,
    /// Position in input (if applicable)
    pub position: Option<usize>,
}

impl ValidationWarning {
    /// Create a new warning
    pub fn new(code: &'static str, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
            position: None,
        }
    }

    /// Set the position
    pub fn at_position(mut self, pos: usize) -> Self {
        self.position = Some(pos);
        self
    }
}

/// Validation error
#[derive(Debug, Clone)]
pub struct ValidationError {
    /// Error code
    pub code: &'static str,
    /// Error message
    pub message: String,
    /// Position in input (if applicable)
    pub position: Option<usize>,
}

impl ValidationError {
    /// Create a new error
    pub fn new(code: &'static str, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
            position: None,
        }
    }

    /// Set the position
    pub fn at_position(mut self, pos: usize) -> Self {
        self.position = Some(pos);
        self
    }
}

// ============================================================================
// Semantic Validation Rules
// ============================================================================

/// Validate a parsed HGVS variant for semantic correctness
pub mod rules {
    use crate::hgvs::edit::{Base, NaEdit, ProteinEdit};
    use crate::hgvs::variant::HgvsVariant;

    use super::{ValidationError, ValidationResult, ValidationWarning};

    /// Validate that an interval's start position is not greater than end
    pub fn validate_position_order(start: i64, end: i64) -> ValidationResult {
        if start > end {
            ValidationResult::ok().with_error(ValidationError::new(
                "E001",
                format!(
                    "Start position ({}) is greater than end position ({})",
                    start, end
                ),
            ))
        } else {
            ValidationResult::ok()
        }
    }

    /// Validate that a nucleotide edit has valid bases
    pub fn validate_na_edit(edit: &NaEdit) -> ValidationResult {
        let mut result = ValidationResult::ok();

        match edit {
            NaEdit::Substitution {
                reference,
                alternative,
            } => {
                if reference == alternative {
                    result = result.with_warning(ValidationWarning::new(
                        "W001",
                        format!(
                            "Reference and alternative are the same: {} = {}",
                            reference, alternative
                        ),
                    ));
                }
            }
            NaEdit::Deletion {
                sequence: Some(seq),
                ..
            } => {
                if seq.is_empty() {
                    result = result.with_warning(ValidationWarning::new(
                        "W002",
                        "Deletion has empty sequence specified",
                    ));
                }
            }
            NaEdit::Deletion { sequence: None, .. } => {}
            NaEdit::Insertion { sequence } => {
                if sequence.is_empty() {
                    result = result.with_error(ValidationError::new(
                        "E002",
                        "Insertion must have at least one base",
                    ));
                }
            }
            NaEdit::Delins { sequence } => {
                if sequence.is_empty() {
                    result = result.with_error(ValidationError::new(
                        "E003",
                        "Deletion-insertion must have at least one inserted base",
                    ));
                }
            }
            _ => {}
        }

        result
    }

    /// Validate that a protein edit has valid amino acids
    pub fn validate_protein_edit(_edit: &ProteinEdit) -> ValidationResult {
        // Protein edits are mostly self-validating through the AminoAcid enum
        // Additional validation could check for biologically impossible changes
        ValidationResult::ok()
    }

    /// Check if a base is a valid nucleotide code
    ///
    /// Currently supports: A, C, G, T, U (RNA), N (any)
    pub fn is_valid_base(base: &Base) -> bool {
        matches!(
            base,
            Base::A | Base::C | Base::G | Base::T | Base::U | Base::N
        )
    }

    /// Validate that a variant doesn't have conflicting attributes
    pub fn validate_variant_consistency(variant: &HgvsVariant) -> ValidationResult {
        let mut result = ValidationResult::ok();

        match variant {
            HgvsVariant::Cds(v) => {
                // Check for intronic positions in exon-only contexts
                if let Some(start) = v.loc_edit.location.start.inner() {
                    if let Some(end) = v.loc_edit.location.end.inner() {
                        // Only one can be in 3' UTR
                        if start.utr3 != end.utr3 && !start.utr3 && end.utr3 {
                            // This is valid - spanning CDS to 3' UTR
                        }
                    }
                }
            }
            HgvsVariant::Protein(v) => {
                // Check for frameshift without position change
                if let Some(ProteinEdit::Frameshift { .. }) = v.loc_edit.edit.inner() {
                    // Frameshifts are inherently complex - no additional validation needed
                }
            }
            HgvsVariant::Allele(a) => {
                // Validate each variant in the allele
                for v in &a.variants {
                    let sub_result = validate_variant_consistency(v);
                    for err in sub_result.errors {
                        result = result.with_error(err);
                    }
                    for warn in sub_result.warnings {
                        result = result.with_warning(warn);
                    }
                }
            }
            _ => {}
        }

        result
    }

    /// Comprehensive validation of a variant
    pub fn validate_variant(variant: &HgvsVariant) -> ValidationResult {
        let mut result = ValidationResult::ok();

        // Check consistency
        let consistency = validate_variant_consistency(variant);
        for err in consistency.errors {
            result = result.with_error(err);
        }
        for warn in consistency.warnings {
            result = result.with_warning(warn);
        }

        result
    }
}

/// Variant validator that uses a reference provider
pub struct Validator<P: crate::reference::ReferenceProvider> {
    #[allow(dead_code)] // Will be used for reference base validation
    provider: P,
    config: ParseConfig,
}

impl<P: crate::reference::ReferenceProvider> Validator<P> {
    /// Create a new validator with a reference provider
    pub fn new(provider: P) -> Self {
        Self {
            provider,
            config: ParseConfig::default(),
        }
    }

    /// Create a validator with custom config
    pub fn with_config(provider: P, config: ParseConfig) -> Self {
        Self { provider, config }
    }

    /// Validate a variant against the reference
    pub fn validate(&self, variant: &crate::hgvs::variant::HgvsVariant) -> ValidationResult {
        let mut result = rules::validate_variant(variant);

        // If we have a provider, validate reference bases
        if self.config.level.at_least(ValidationLevel::Standard) {
            if let Some(ref_error) = self.validate_reference_base(variant) {
                result = result.with_error(ref_error);
            }
        }

        result
    }

    /// Validate that reference base(s) in the variant match the actual reference
    fn validate_reference_base(
        &self,
        _variant: &crate::hgvs::variant::HgvsVariant,
    ) -> Option<ValidationError> {
        // This would need transcript/sequence access to verify
        // For now, return None (no error)
        // A full implementation would:
        // 1. Get the transcript/sequence via self.provider
        // 2. Extract the position(s)
        // 3. Compare the reference base in the variant to the actual sequence
        None
    }
}

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

    #[test]
    fn test_validation_level_default() {
        assert_eq!(ValidationLevel::default(), ValidationLevel::Standard);
    }

    #[test]
    fn test_validation_level_at_least() {
        assert!(ValidationLevel::Strict.at_least(ValidationLevel::Standard));
        assert!(ValidationLevel::Standard.at_least(ValidationLevel::Lenient));
        assert!(!ValidationLevel::Lenient.at_least(ValidationLevel::Strict));
    }

    #[test]
    fn test_parse_config_lenient() {
        let config = ParseConfig::lenient();
        assert_eq!(config.level, ValidationLevel::Lenient);
        assert!(config.allow_missing_version);
        assert!(config.allow_lowercase_prefix);
    }

    #[test]
    fn test_parse_config_strict() {
        let config = ParseConfig::strict();
        assert_eq!(config.level, ValidationLevel::Strict);
        assert!(!config.allow_missing_version);
        assert!(!config.allow_lowercase_prefix);
    }

    #[test]
    fn test_validation_result() {
        let result = ValidationResult::ok();
        assert!(result.valid);
        assert!(!result.has_warnings());
        assert!(!result.has_errors());

        let result = result.with_warning(ValidationWarning::new("W001", "test warning"));
        assert!(result.valid);
        assert!(result.has_warnings());
    }

    #[test]
    fn test_global_validation_level() {
        let original = get_validation_level();

        set_validation_level(ValidationLevel::Lenient);
        assert_eq!(get_validation_level(), ValidationLevel::Lenient);

        set_validation_level(ValidationLevel::Strict);
        assert_eq!(get_validation_level(), ValidationLevel::Strict);

        set_validation_level(original);
    }

    #[test]
    fn test_with_validation_level() {
        let original = get_validation_level();
        set_validation_level(ValidationLevel::Standard);

        let result = with_validation_level(ValidationLevel::Strict, || {
            assert_eq!(get_validation_level(), ValidationLevel::Strict);
            42
        });

        assert_eq!(result, 42);
        assert_eq!(get_validation_level(), ValidationLevel::Standard);

        set_validation_level(original);
    }

    // Tests for semantic validation rules
    mod rules_tests {
        use super::*;
        use crate::hgvs::edit::{Base, InsertedSequence, NaEdit, Sequence};

        #[test]
        fn test_validate_position_order_valid() {
            let result = rules::validate_position_order(10, 20);
            assert!(result.valid);
            assert!(!result.has_errors());
        }

        #[test]
        fn test_validate_position_order_equal() {
            let result = rules::validate_position_order(10, 10);
            assert!(result.valid);
        }

        #[test]
        fn test_validate_position_order_invalid() {
            let result = rules::validate_position_order(20, 10);
            assert!(!result.valid);
            assert!(result.has_errors());
            assert_eq!(result.errors[0].code, "E001");
        }

        #[test]
        fn test_validate_substitution_same_base_warning() {
            let edit = NaEdit::Substitution {
                reference: Base::A,
                alternative: Base::A,
            };
            let result = rules::validate_na_edit(&edit);
            assert!(result.valid); // Still valid, just a warning
            assert!(result.has_warnings());
            assert_eq!(result.warnings[0].code, "W001");
        }

        #[test]
        fn test_validate_substitution_different_bases() {
            let edit = NaEdit::Substitution {
                reference: Base::A,
                alternative: Base::G,
            };
            let result = rules::validate_na_edit(&edit);
            assert!(result.valid);
            assert!(!result.has_warnings());
        }

        #[test]
        fn test_validate_insertion_empty_error() {
            let edit = NaEdit::Insertion {
                sequence: InsertedSequence::Literal(Sequence::new(vec![])),
            };
            let result = rules::validate_na_edit(&edit);
            assert!(!result.valid);
            assert!(result.has_errors());
            assert_eq!(result.errors[0].code, "E002");
        }

        #[test]
        fn test_validate_delins_empty_error() {
            let edit = NaEdit::Delins {
                sequence: InsertedSequence::Literal(Sequence::new(vec![])),
            };
            let result = rules::validate_na_edit(&edit);
            assert!(!result.valid);
            assert!(result.has_errors());
            assert_eq!(result.errors[0].code, "E003");
        }

        #[test]
        fn test_is_valid_base() {
            assert!(rules::is_valid_base(&Base::A));
            assert!(rules::is_valid_base(&Base::C));
            assert!(rules::is_valid_base(&Base::G));
            assert!(rules::is_valid_base(&Base::T));
            assert!(rules::is_valid_base(&Base::U));
            assert!(rules::is_valid_base(&Base::N));
        }

        #[test]
        fn test_validate_variant_consistency_cds() {
            use crate::hgvs::parser::parse_hgvs;

            let variant = parse_hgvs("NM_000088.3:c.100A>G").unwrap();
            let result = rules::validate_variant(&variant);
            assert!(result.valid);
        }

        #[test]
        fn test_validate_variant_consistency_allele() {
            use crate::hgvs::parser::parse_hgvs;

            let variant = parse_hgvs("[NM_000088.3:c.100A>G;NM_000088.3:c.200C>T]").unwrap();
            let result = rules::validate_variant(&variant);
            assert!(result.valid);
        }
    }

    // Tests for Validator struct
    mod validator_tests {
        use super::*;
        use crate::hgvs::parser::parse_hgvs;
        use crate::reference::MockProvider;

        #[test]
        fn test_validator_creation() {
            let provider = MockProvider::with_test_data();
            let _validator = Validator::new(provider);
        }

        #[test]
        fn test_validator_with_config() {
            let provider = MockProvider::with_test_data();
            let config = ParseConfig::strict();
            let validator = Validator::with_config(provider, config);
            assert_eq!(validator.config.level, ValidationLevel::Strict);
        }

        #[test]
        fn test_validate_simple_variant() {
            let provider = MockProvider::with_test_data();
            let validator = Validator::new(provider);

            let variant = parse_hgvs("NM_000088.3:c.10A>G").unwrap();
            let result = validator.validate(&variant);
            assert!(result.valid);
        }

        #[test]
        fn test_validate_protein_variant() {
            let provider = MockProvider::with_test_data();
            let validator = Validator::new(provider);

            let variant = parse_hgvs("NP_000079.2:p.Val600Glu").unwrap();
            let result = validator.validate(&variant);
            assert!(result.valid);
        }
    }
}