ferro-hgvs 0.3.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
//! Helper functions for Python bindings
//!
//! These functions are separated from the PyO3 code so they can be unit tested
//! without requiring the Python runtime.

use crate::hgvs::edit::NaEdit;
use crate::hgvs::uncertainty::Mu;
use crate::hgvs::variant::HgvsVariant;
use crate::normalize::ShuffleDirection;

/// Get the variant type as a string
///
/// Returns a human-readable string describing the type of HGVS variant.
///
/// # Examples
///
/// ```
/// use ferro_hgvs::python_helpers::variant_type_str;
/// use ferro_hgvs::parse_hgvs;
///
/// let variant = parse_hgvs("NC_000001.11:g.12345A>G").unwrap();
/// assert_eq!(variant_type_str(&variant), "genomic");
///
/// let variant = parse_hgvs("NM_000088.3:c.100A>G").unwrap();
/// assert_eq!(variant_type_str(&variant), "coding");
/// ```
pub fn variant_type_str(variant: &HgvsVariant) -> &'static str {
    match variant {
        HgvsVariant::Genome(_) => "genomic",
        HgvsVariant::Cds(_) => "coding",
        HgvsVariant::Tx(_) => "non_coding",
        HgvsVariant::Protein(_) => "protein",
        HgvsVariant::Rna(_) => "rna",
        HgvsVariant::Mt(_) => "mitochondrial",
        HgvsVariant::Circular(_) => "circular",
        HgvsVariant::RnaFusion(_) => "rna_fusion",
        HgvsVariant::Allele(_) => "allele",
        HgvsVariant::NullAllele => "null_allele",
        HgvsVariant::UnknownAllele => "unknown_allele",
    }
}

/// Get the edit type as a string from an NaEdit
///
/// Returns a human-readable string describing the type of nucleic acid edit.
///
/// # Examples
///
/// ```
/// use ferro_hgvs::python_helpers::na_edit_type_str;
/// use ferro_hgvs::hgvs::edit::{NaEdit, Base};
///
/// let edit = NaEdit::Substitution { reference: Base::A, alternative: Base::G };
/// assert_eq!(na_edit_type_str(&edit), "substitution");
/// ```
pub fn na_edit_type_str(edit: &NaEdit) -> &'static str {
    match edit {
        NaEdit::Substitution { .. } => "substitution",
        NaEdit::SubstitutionNoRef { .. } => "substitution",
        NaEdit::Deletion { .. } => "deletion",
        NaEdit::Duplication { .. } => "duplication",
        NaEdit::DupIns { .. } => "dupins",
        NaEdit::Insertion { .. } => "insertion",
        NaEdit::Delins { .. } => "delins",
        NaEdit::Inversion { .. } => "inversion",
        NaEdit::Repeat { .. } => "repeat",
        NaEdit::MultiRepeat { .. } => "multi_repeat",
        NaEdit::Identity { .. } => "identity",
        NaEdit::Conversion { .. } => "conversion",
        NaEdit::Unknown { .. } => "unknown",
        NaEdit::Methylation { .. } => "methylation",
        NaEdit::CopyNumber { .. } => "copy_number",
        NaEdit::Splice => "splice",
        NaEdit::NoProduct => "no_product",
        NaEdit::PositionOnly => "position_only",
    }
}

/// Get the edit type from debug string representation
///
/// This is a fallback function that determines the edit type by inspecting
/// the Debug representation of an edit. Use `na_edit_type_str` when possible.
pub fn edit_type_from_debug<T: std::fmt::Debug>(edit: &T) -> &'static str {
    let debug = format!("{:?}", edit);
    if debug.contains("Substitution") {
        "substitution"
    } else if debug.contains("Deletion") {
        "deletion"
    } else if debug.contains("DupIns") {
        "dupins"
    } else if debug.contains("Duplication") {
        "duplication"
    } else if debug.contains("Insertion") {
        "insertion"
    } else if debug.contains("Delins") {
        "delins"
    } else if debug.contains("Inversion") {
        "inversion"
    } else if debug.contains("Repeat") {
        "repeat"
    } else if debug.contains("Identity") {
        "identity"
    } else if debug.contains("Conversion") {
        "conversion"
    } else if debug.contains("Methylation") {
        "methylation"
    } else if debug.contains("CopyNumber") {
        "copy_number"
    } else {
        "unknown"
    }
}

/// Parse a direction string into ShuffleDirection
///
/// Accepts various common formats:
/// - 3prime, 3', 3 -> ThreePrime (default)
/// - 5prime, 5', 5 -> FivePrime
///
/// # Examples
///
/// ```
/// use ferro_hgvs::python_helpers::parse_direction;
/// use ferro_hgvs::ShuffleDirection;
///
/// assert!(matches!(parse_direction("3prime"), ShuffleDirection::ThreePrime));
/// assert!(matches!(parse_direction("5'"), ShuffleDirection::FivePrime));
/// ```
pub fn parse_direction(direction: &str) -> ShuffleDirection {
    match direction.to_lowercase().as_str() {
        "5prime" | "5'" | "5" => ShuffleDirection::FivePrime,
        _ => ShuffleDirection::ThreePrime,
    }
}

/// Get the reference accession from a variant
///
/// Returns the accession string for variants that have one, or an error message
/// for variants that don't support accessions.
///
/// # Examples
///
/// ```
/// use ferro_hgvs::python_helpers::get_variant_reference;
/// use ferro_hgvs::parse_hgvs;
///
/// let variant = parse_hgvs("NC_000001.11:g.12345A>G").unwrap();
/// assert_eq!(get_variant_reference(&variant).unwrap(), "NC_000001.11");
/// ```
pub fn get_variant_reference(variant: &HgvsVariant) -> Result<String, &'static str> {
    match variant {
        HgvsVariant::Genome(v) => Ok(v.accession.to_string()),
        HgvsVariant::Cds(v) => Ok(v.accession.to_string()),
        HgvsVariant::Tx(v) => Ok(v.accession.to_string()),
        HgvsVariant::Protein(v) => Ok(v.accession.to_string()),
        HgvsVariant::Rna(v) => Ok(v.accession.to_string()),
        HgvsVariant::Mt(v) => Ok(v.accession.to_string()),
        HgvsVariant::Circular(v) => Ok(v.accession.to_string()),
        HgvsVariant::RnaFusion(v) => Ok(v.five_prime.accession.to_string()),
        HgvsVariant::Allele(a) => {
            if let Some(first) = a.variants.first() {
                get_variant_reference(first)
            } else {
                Err("Empty allele")
            }
        }
        HgvsVariant::NullAllele | HgvsVariant::UnknownAllele => {
            Err("No reference for null/unknown allele")
        }
    }
}

/// Get the edit type from a Mu-wrapped NaEdit
///
/// Handles the uncertainty wrapper, returning "unknown" for Mu::Unknown.
pub fn mu_na_edit_type_str(edit: &Mu<NaEdit>) -> &'static str {
    match edit.inner() {
        Some(e) => na_edit_type_str(e),
        None => "unknown",
    }
}

/// Get the edit type string for any HgvsVariant
///
/// Returns a string describing the type of edit in the variant.
pub fn get_variant_edit_type(variant: &HgvsVariant) -> &'static str {
    match variant {
        HgvsVariant::Genome(v) => mu_na_edit_type_str(&v.loc_edit.edit),
        HgvsVariant::Cds(v) => mu_na_edit_type_str(&v.loc_edit.edit),
        HgvsVariant::Tx(v) => mu_na_edit_type_str(&v.loc_edit.edit),
        HgvsVariant::Rna(v) => mu_na_edit_type_str(&v.loc_edit.edit),
        HgvsVariant::Mt(v) => mu_na_edit_type_str(&v.loc_edit.edit),
        HgvsVariant::Circular(v) => mu_na_edit_type_str(&v.loc_edit.edit),
        HgvsVariant::Protein(_) => "protein",
        HgvsVariant::RnaFusion(_) => "fusion",
        HgvsVariant::Allele(_) => "allele",
        HgvsVariant::NullAllele => "null",
        HgvsVariant::UnknownAllele => "unknown",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hgvs::edit::{Base, MethylationStatus};
    use crate::parse_hgvs;

    // ===== variant_type_str Tests =====

    #[test]
    fn test_variant_type_str_genomic() {
        let variant = parse_hgvs("NC_000001.11:g.12345A>G").unwrap();
        assert_eq!(variant_type_str(&variant), "genomic");
    }

    #[test]
    fn test_variant_type_str_coding() {
        let variant = parse_hgvs("NM_000088.3:c.100A>G").unwrap();
        assert_eq!(variant_type_str(&variant), "coding");
    }

    #[test]
    fn test_variant_type_str_non_coding() {
        let variant = parse_hgvs("NR_000001.1:n.100A>G").unwrap();
        assert_eq!(variant_type_str(&variant), "non_coding");
    }

    #[test]
    fn test_variant_type_str_protein() {
        let variant = parse_hgvs("NP_000001.1:p.Val100Glu").unwrap();
        assert_eq!(variant_type_str(&variant), "protein");
    }

    #[test]
    fn test_variant_type_str_rna() {
        let variant = parse_hgvs("NM_000088.3:r.100a>g").unwrap();
        assert_eq!(variant_type_str(&variant), "rna");
    }

    #[test]
    fn test_variant_type_str_mitochondrial() {
        let variant = parse_hgvs("NC_012920.1:m.100A>G").unwrap();
        assert_eq!(variant_type_str(&variant), "mitochondrial");
    }

    #[test]
    fn test_variant_type_str_circular() {
        let variant = parse_hgvs("NC_001416.1:o.100A>G").unwrap();
        assert_eq!(variant_type_str(&variant), "circular");
    }

    #[test]
    fn test_variant_type_str_null_allele() {
        let variant = HgvsVariant::NullAllele;
        assert_eq!(variant_type_str(&variant), "null_allele");
    }

    #[test]
    fn test_variant_type_str_unknown_allele() {
        let variant = HgvsVariant::UnknownAllele;
        assert_eq!(variant_type_str(&variant), "unknown_allele");
    }

    // ===== na_edit_type_str Tests =====

    #[test]
    fn test_na_edit_type_str_substitution() {
        let edit = NaEdit::Substitution {
            reference: Base::A,
            alternative: Base::G,
        };
        assert_eq!(na_edit_type_str(&edit), "substitution");
    }

    #[test]
    fn test_na_edit_type_str_deletion() {
        let edit = NaEdit::Deletion {
            sequence: None,
            length: None,
        };
        assert_eq!(na_edit_type_str(&edit), "deletion");
    }

    #[test]
    fn test_na_edit_type_str_duplication() {
        let edit = NaEdit::Duplication {
            sequence: None,
            length: None,
            uncertain_extent: None,
        };
        assert_eq!(na_edit_type_str(&edit), "duplication");
    }

    #[test]
    fn test_na_edit_type_str_insertion() {
        use crate::hgvs::edit::{InsertedSequence, Sequence};
        use std::str::FromStr;
        let edit = NaEdit::Insertion {
            sequence: InsertedSequence::Literal(Sequence::from_str("ATG").unwrap()),
        };
        assert_eq!(na_edit_type_str(&edit), "insertion");
    }

    #[test]
    fn test_na_edit_type_str_delins() {
        use crate::hgvs::edit::{InsertedSequence, Sequence};
        use std::str::FromStr;
        let edit = NaEdit::Delins {
            sequence: InsertedSequence::Literal(Sequence::from_str("ATG").unwrap()),
        };
        assert_eq!(na_edit_type_str(&edit), "delins");
    }

    #[test]
    fn test_na_edit_type_str_inversion() {
        let edit = NaEdit::Inversion {
            sequence: None,
            length: None,
        };
        assert_eq!(na_edit_type_str(&edit), "inversion");
    }

    #[test]
    fn test_na_edit_type_str_repeat() {
        use crate::hgvs::edit::RepeatCount;
        let edit = NaEdit::Repeat {
            sequence: None,
            count: RepeatCount::Exact(10),
            additional_counts: Vec::new(),
            trailing: None,
        };
        assert_eq!(na_edit_type_str(&edit), "repeat");
    }

    #[test]
    fn test_na_edit_type_str_identity() {
        let edit = NaEdit::Identity {
            sequence: None,
            whole_entity: false,
        };
        assert_eq!(na_edit_type_str(&edit), "identity");
    }

    #[test]
    fn test_na_edit_type_str_unknown() {
        let edit = NaEdit::Unknown {
            whole_entity: false,
        };
        assert_eq!(na_edit_type_str(&edit), "unknown");
    }

    #[test]
    fn test_na_edit_type_str_methylation() {
        let edit = NaEdit::Methylation {
            status: MethylationStatus::GainOfMethylation,
        };
        assert_eq!(na_edit_type_str(&edit), "methylation");
    }

    // ===== edit_type_from_debug Tests =====

    #[test]
    fn test_edit_type_from_debug_substitution() {
        #[derive(Debug)]
        struct TestSubstitution;
        assert_eq!(edit_type_from_debug(&TestSubstitution), "substitution");
    }

    #[test]
    fn test_edit_type_from_debug_deletion() {
        #[derive(Debug)]
        struct TestDeletion;
        assert_eq!(edit_type_from_debug(&TestDeletion), "deletion");
    }

    #[test]
    fn test_edit_type_from_debug_unknown() {
        #[derive(Debug)]
        struct TestOther;
        assert_eq!(edit_type_from_debug(&TestOther), "unknown");
    }

    // ===== parse_direction Tests =====

    #[test]
    fn test_parse_direction_three_prime() {
        assert!(matches!(
            parse_direction("3prime"),
            ShuffleDirection::ThreePrime
        ));
        assert!(matches!(
            parse_direction("3'"),
            ShuffleDirection::ThreePrime
        ));
        assert!(matches!(parse_direction("3"), ShuffleDirection::ThreePrime));
    }

    #[test]
    fn test_parse_direction_five_prime() {
        assert!(matches!(
            parse_direction("5prime"),
            ShuffleDirection::FivePrime
        ));
        assert!(matches!(parse_direction("5'"), ShuffleDirection::FivePrime));
        assert!(matches!(parse_direction("5"), ShuffleDirection::FivePrime));
    }

    #[test]
    fn test_parse_direction_default() {
        assert!(matches!(
            parse_direction("unknown"),
            ShuffleDirection::ThreePrime
        ));
        assert!(matches!(parse_direction(""), ShuffleDirection::ThreePrime));
    }

    #[test]
    fn test_parse_direction_case_insensitive() {
        assert!(matches!(
            parse_direction("5PRIME"),
            ShuffleDirection::FivePrime
        ));
        assert!(matches!(
            parse_direction("5Prime"),
            ShuffleDirection::FivePrime
        ));
    }

    // ===== get_variant_reference Tests =====

    #[test]
    fn test_get_variant_reference_genomic() {
        let variant = parse_hgvs("NC_000001.11:g.12345A>G").unwrap();
        assert_eq!(get_variant_reference(&variant).unwrap(), "NC_000001.11");
    }

    #[test]
    fn test_get_variant_reference_coding() {
        let variant = parse_hgvs("NM_000088.3:c.100A>G").unwrap();
        assert_eq!(get_variant_reference(&variant).unwrap(), "NM_000088.3");
    }

    #[test]
    fn test_get_variant_reference_protein() {
        let variant = parse_hgvs("NP_000001.1:p.Val100Glu").unwrap();
        assert_eq!(get_variant_reference(&variant).unwrap(), "NP_000001.1");
    }

    #[test]
    fn test_get_variant_reference_null_allele() {
        let variant = HgvsVariant::NullAllele;
        assert!(get_variant_reference(&variant).is_err());
    }

    #[test]
    fn test_get_variant_reference_unknown_allele() {
        let variant = HgvsVariant::UnknownAllele;
        assert!(get_variant_reference(&variant).is_err());
    }

    // ===== get_variant_edit_type Tests =====

    #[test]
    fn test_get_variant_edit_type_substitution() {
        let variant = parse_hgvs("NC_000001.11:g.12345A>G").unwrap();
        assert_eq!(get_variant_edit_type(&variant), "substitution");
    }

    #[test]
    fn test_get_variant_edit_type_deletion() {
        let variant = parse_hgvs("NC_000001.11:g.12345del").unwrap();
        assert_eq!(get_variant_edit_type(&variant), "deletion");
    }

    #[test]
    fn test_get_variant_edit_type_duplication() {
        let variant = parse_hgvs("NC_000001.11:g.12345dup").unwrap();
        assert_eq!(get_variant_edit_type(&variant), "duplication");
    }

    #[test]
    fn test_get_variant_edit_type_insertion() {
        let variant = parse_hgvs("NC_000001.11:g.12345_12346insATG").unwrap();
        assert_eq!(get_variant_edit_type(&variant), "insertion");
    }

    #[test]
    fn test_get_variant_edit_type_delins() {
        let variant = parse_hgvs("NC_000001.11:g.12345_12350delinsATG").unwrap();
        assert_eq!(get_variant_edit_type(&variant), "delins");
    }

    #[test]
    fn test_get_variant_edit_type_inversion() {
        let variant = parse_hgvs("NC_000001.11:g.12345_12350inv").unwrap();
        assert_eq!(get_variant_edit_type(&variant), "inversion");
    }

    #[test]
    fn test_get_variant_edit_type_protein() {
        let variant = parse_hgvs("NP_000001.1:p.Val100Glu").unwrap();
        assert_eq!(get_variant_edit_type(&variant), "protein");
    }

    #[test]
    fn test_get_variant_edit_type_null() {
        let variant = HgvsVariant::NullAllele;
        assert_eq!(get_variant_edit_type(&variant), "null");
    }

    #[test]
    fn test_get_variant_edit_type_unknown() {
        let variant = HgvsVariant::UnknownAllele;
        assert_eq!(get_variant_edit_type(&variant), "unknown");
    }
}