ferro-hgvs 0.4.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
//! Tests for external HGVS pattern sources
//!
//! This module tests patterns collected from biocommons, mutalyzer, NCBI dbSNP,
//! and other external sources to verify coverage and identify unsupported patterns.

use ferro_hgvs::parse_hgvs;
use std::collections::HashMap;
use std::fs;

/// Extract HGVS patterns from biocommons grammar_test.tsv
fn extract_grammar_test_patterns() -> Vec<(String, bool)> {
    let content = fs::read_to_string("tests/fixtures/external/biocommons_grammar_test.tsv")
        .expect("Failed to read biocommons_grammar_test.tsv");

    let mut patterns = Vec::new();

    for line in content.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        let parts: Vec<&str> = line.split('\t').collect();
        if parts.len() < 3 {
            continue;
        }

        let func = parts[0];
        let test = parts[1];
        let valid = parts[2].to_lowercase() == "true";

        // Only extract variant tests (hgvs_variant, c_variant, g_variant, etc.)
        if func.ends_with("_variant") || func == "hgvs_variant" {
            // Handle pipe-separated test cases
            for pattern in test.split('|') {
                let pattern = pattern.trim();
                if !pattern.is_empty() {
                    patterns.push((pattern.to_string(), valid));
                }
            }
        }
    }

    patterns
}

/// Extract HGVS patterns from biocommons gauntlet file
fn extract_gauntlet_patterns() -> Vec<(String, bool)> {
    let content = fs::read_to_string("tests/fixtures/external/biocommons_gauntlet.txt")
        .expect("Failed to read biocommons_gauntlet.txt");

    let mut patterns = Vec::new();

    for line in content.lines() {
        let line = line.trim();

        // Skip empty lines and comments
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        // Skip section headers
        if line.contains("####") {
            continue;
        }

        // Check if it's marked as unsupported
        if line.starts_with("#!unsupported:") {
            let pattern = line.trim_start_matches("#!unsupported:").trim();
            patterns.push((pattern.to_string(), false));
        } else if line.starts_with("#-") {
            // Negative examples
            let pattern = line.trim_start_matches("#-").trim();
            patterns.push((pattern.to_string(), false));
        } else {
            // Regular pattern
            patterns.push((line.to_string(), true));
        }
    }

    patterns
}

/// Extract HGVS patterns from biocommons real.tsv
fn extract_real_tsv_patterns() -> Vec<(String, bool)> {
    let content = fs::read_to_string("tests/fixtures/external/biocommons_real.tsv")
        .expect("Failed to read biocommons_real.tsv");

    let mut patterns = Vec::new();

    for line in content.lines().skip(1) {
        // Skip header
        let line = line.trim();
        if line.is_empty() {
            continue;
        }

        let parts: Vec<&str> = line.split('\t').collect();
        if parts.len() >= 4 {
            // Extract HGVSg, HGVSc, HGVSp
            for i in 1..=3 {
                if let Some(pattern) = parts.get(i) {
                    let pattern = pattern.trim();
                    if !pattern.is_empty() {
                        patterns.push((pattern.to_string(), true));
                    }
                }
            }
        }
    }

    patterns
}

/// Extract HGVS patterns from mutalyzer test file
fn extract_mutalyzer_patterns() -> Vec<(String, bool)> {
    let content = fs::read_to_string("tests/fixtures/external/mutalyzer_patterns.txt")
        .expect("Failed to read mutalyzer_patterns.txt");

    let mut patterns = Vec::new();

    for line in content.lines() {
        let line = line.trim();

        // Skip empty lines and comments
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        // All mutalyzer patterns are valid (invalid ones in comments)
        patterns.push((line.to_string(), true));
    }

    patterns
}

/// Extract HGVS patterns from NCBI dbSNP file
fn extract_ncbi_patterns() -> Vec<(String, bool)> {
    let content = fs::read_to_string("tests/fixtures/external/ncbi_dbsnp.txt")
        .expect("Failed to read ncbi_dbsnp.txt");

    let mut patterns = Vec::new();

    for line in content.lines() {
        let line = line.trim();

        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        patterns.push((line.to_string(), true));
    }

    patterns
}

/// Categorize parsing results
#[derive(Debug, Default)]
struct CategoryStats {
    supported_valid: Vec<String>,   // Expected valid, parsed successfully
    supported_invalid: Vec<String>, // Expected invalid, failed to parse (correct)
    unsupported: Vec<String>,       // Expected valid, failed to parse
    false_positive: Vec<String>,    // Expected invalid, parsed successfully (bug)
}

impl CategoryStats {
    fn summary(&self) -> String {
        format!(
            "Supported valid: {}, Correctly rejected: {}, Unsupported: {}, False positives: {}",
            self.supported_valid.len(),
            self.supported_invalid.len(),
            self.unsupported.len(),
            self.false_positive.len()
        )
    }
}

fn categorize_patterns(patterns: Vec<(String, bool)>) -> CategoryStats {
    let mut stats = CategoryStats::default();

    for (pattern, expected_valid) in patterns {
        let result = parse_hgvs(&pattern);

        match (result.is_ok(), expected_valid) {
            (true, true) => stats.supported_valid.push(pattern),
            (false, false) => stats.supported_invalid.push(pattern),
            (false, true) => stats.unsupported.push(pattern),
            (true, false) => stats.false_positive.push(pattern),
        }
    }

    stats
}

#[test]
fn test_biocommons_grammar_patterns() {
    let patterns = extract_grammar_test_patterns();
    let stats = categorize_patterns(patterns);

    println!("\n=== Biocommons Grammar Test Patterns ===");
    println!("{}", stats.summary());

    if !stats.unsupported.is_empty() {
        println!("\nUnsupported patterns (first 20):");
        for p in stats.unsupported.iter().take(20) {
            println!("  - {}", p);
        }
    }

    if !stats.false_positive.is_empty() {
        println!("\nFalse positives (should have rejected):");
        for p in &stats.false_positive {
            println!("  - {}", p);
        }
    }

    // Don't fail the test, just report
    println!(
        "Total patterns tested: {}",
        stats.supported_valid.len()
            + stats.supported_invalid.len()
            + stats.unsupported.len()
            + stats.false_positive.len()
    );
}

#[test]
fn test_biocommons_gauntlet_patterns() {
    let patterns = extract_gauntlet_patterns();
    let stats = categorize_patterns(patterns);

    println!("\n=== Biocommons Gauntlet Patterns ===");
    println!("{}", stats.summary());

    if !stats.unsupported.is_empty() {
        println!("\nUnsupported patterns:");
        for p in &stats.unsupported {
            println!("  - {}", p);
        }
    }
}

#[test]
fn test_biocommons_real_patterns() {
    let patterns = extract_real_tsv_patterns();
    let stats = categorize_patterns(patterns);

    println!("\n=== Biocommons Real TSV Patterns ===");
    println!("{}", stats.summary());

    if !stats.unsupported.is_empty() {
        println!("\nUnsupported patterns (first 20):");
        for p in stats.unsupported.iter().take(20) {
            println!("  - {}", p);
        }
    }

    // These are real ClinVar variants, so we should support most of them
    let total = stats.supported_valid.len() + stats.unsupported.len();
    let support_rate = stats.supported_valid.len() as f64 / total as f64 * 100.0;
    println!("Support rate: {:.1}%", support_rate);
}

#[test]
fn test_mutalyzer_patterns() {
    let patterns = extract_mutalyzer_patterns();
    let stats = categorize_patterns(patterns);

    println!("\n=== Mutalyzer Patterns ===");
    println!("{}", stats.summary());

    if !stats.unsupported.is_empty() {
        println!("\nUnsupported patterns:");
        for p in &stats.unsupported {
            println!("  - {}", p);
        }
    }
}

#[test]
fn test_ncbi_dbsnp_patterns() {
    let patterns = extract_ncbi_patterns();
    let stats = categorize_patterns(patterns);

    println!("\n=== NCBI dbSNP Patterns ===");
    println!("{}", stats.summary());

    // All NCBI dbSNP patterns should be supported
    assert!(
        stats.unsupported.is_empty(),
        "NCBI dbSNP patterns should all be supported: {:?}",
        stats.unsupported
    );
}

#[test]
fn test_comprehensive_pattern_report() {
    println!("\n============================================================");
    println!("       COMPREHENSIVE HGVS PATTERN SUPPORT REPORT");
    println!("============================================================\n");

    let mut all_unsupported: HashMap<String, Vec<String>> = HashMap::new();
    let mut total_supported = 0;
    let mut total_unsupported = 0;

    // Grammar test
    let patterns = extract_grammar_test_patterns();
    let stats = categorize_patterns(patterns);
    println!("Biocommons Grammar:");
    println!("  {}", stats.summary());
    total_supported += stats.supported_valid.len();
    total_unsupported += stats.unsupported.len();
    all_unsupported.insert("grammar".to_string(), stats.unsupported);

    // Gauntlet
    let patterns = extract_gauntlet_patterns();
    let stats = categorize_patterns(patterns);
    println!("Biocommons Gauntlet:");
    println!("  {}", stats.summary());
    total_supported += stats.supported_valid.len();
    total_unsupported += stats.unsupported.len();
    all_unsupported.insert("gauntlet".to_string(), stats.unsupported);

    // Real TSV
    let patterns = extract_real_tsv_patterns();
    let stats = categorize_patterns(patterns);
    println!("Biocommons Real:");
    println!("  {}", stats.summary());
    total_supported += stats.supported_valid.len();
    total_unsupported += stats.unsupported.len();
    all_unsupported.insert("real".to_string(), stats.unsupported);

    // Mutalyzer
    let patterns = extract_mutalyzer_patterns();
    let stats = categorize_patterns(patterns);
    println!("Mutalyzer:");
    println!("  {}", stats.summary());
    total_supported += stats.supported_valid.len();
    total_unsupported += stats.unsupported.len();
    all_unsupported.insert("mutalyzer".to_string(), stats.unsupported);

    // NCBI
    let patterns = extract_ncbi_patterns();
    let stats = categorize_patterns(patterns);
    println!("NCBI dbSNP:");
    println!("  {}", stats.summary());
    total_supported += stats.supported_valid.len();
    total_unsupported += stats.unsupported.len();
    all_unsupported.insert("ncbi".to_string(), stats.unsupported);

    println!("\n------------------------------------------------------------");
    println!("TOTALS:");
    println!("  Supported patterns: {}", total_supported);
    println!("  Unsupported patterns: {}", total_unsupported);
    let overall_rate =
        total_supported as f64 / (total_supported + total_unsupported) as f64 * 100.0;
    println!("  Overall support rate: {:.1}%", overall_rate);

    println!("\n------------------------------------------------------------");
    println!("ALL UNSUPPORTED PATTERNS:");

    for (source, patterns) in &all_unsupported {
        if !patterns.is_empty() {
            println!("\n[{}]:", source);
            for p in patterns {
                println!("  {}", p);
            }
        }
    }

    println!("\n============================================================");
}

#[test]
fn test_specific_patterns_to_verify() {
    // Test patterns that were marked for verification in the research plan
    let patterns_to_check = vec![
        // Selenocysteine
        ("NP_000001.1:p.Sec25Cys", "Selenocysteine substitution"),
        // Stop codon asterisk vs Ter
        ("NP_000001.1:p.Arg342*", "Stop codon with asterisk"),
        ("NP_000001.1:p.Arg342Ter", "Stop codon with Ter"),
        // Gene symbol in accession
        ("NM_004006.1(DMD):c.22+1A>T", "Gene symbol in accession"),
        // RNA lowercase
        ("NM_000001.1:r.76a>c", "RNA lowercase nucleotides"),
        // Repeat genotypes
        ("NM_000001.1:c.100CAG[14]", "Repeat genotype single"),
    ];

    println!("\n=== Specific Pattern Verification ===\n");

    for (pattern, description) in patterns_to_check {
        let result = parse_hgvs(pattern);
        let status = if result.is_ok() {
            "✓ SUPPORTED"
        } else {
            "✗ UNSUPPORTED"
        };
        println!("{}: {} - {}", status, description, pattern);
        if let Err(e) = result {
            println!("   Error: {}", e);
        }
    }
}