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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! ClinVar data types.

use serde::{Deserialize, Serialize};
use std::str::FromStr;

/// Clinical significance classification from ClinVar.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum ClinicalSignificance {
    /// Benign - variant does not cause disease
    Benign,
    /// Likely benign - variant probably does not cause disease
    LikelyBenign,
    /// Uncertain significance - insufficient evidence
    UncertainSignificance,
    /// Likely pathogenic - variant probably causes disease
    LikelyPathogenic,
    /// Pathogenic - variant causes disease
    Pathogenic,
    /// Conflicting interpretations from different submitters
    Conflicting,
    /// Drug response
    DrugResponse,
    /// Association - variant is associated with phenotype
    Association,
    /// Risk factor
    RiskFactor,
    /// Protective factor
    Protective,
    /// Affects (affects gene function)
    Affects,
    /// Not provided
    #[default]
    NotProvided,
    /// Other
    Other,
}

impl ClinicalSignificance {
    /// Convert to ClinVar string representation.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Benign => "Benign",
            Self::LikelyBenign => "Likely benign",
            Self::UncertainSignificance => "Uncertain significance",
            Self::LikelyPathogenic => "Likely pathogenic",
            Self::Pathogenic => "Pathogenic",
            Self::Conflicting => "Conflicting interpretations of pathogenicity",
            Self::DrugResponse => "drug response",
            Self::Association => "association",
            Self::RiskFactor => "risk factor",
            Self::Protective => "Protective",
            Self::Affects => "Affects",
            Self::NotProvided => "not provided",
            Self::Other => "other",
        }
    }

    /// Check if this is a pathogenic classification.
    pub fn is_pathogenic(&self) -> bool {
        matches!(self, Self::Pathogenic | Self::LikelyPathogenic)
    }

    /// Check if this is a benign classification.
    pub fn is_benign(&self) -> bool {
        matches!(self, Self::Benign | Self::LikelyBenign)
    }

    /// Check if this is uncertain or conflicting.
    pub fn is_uncertain(&self) -> bool {
        matches!(self, Self::UncertainSignificance | Self::Conflicting)
    }
}

impl std::fmt::Display for ClinicalSignificance {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl FromStr for ClinicalSignificance {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s_lower = s.to_lowercase();
        Ok(match s_lower.as_str() {
            "benign" => Self::Benign,
            "likely benign" | "likely_benign" => Self::LikelyBenign,
            "uncertain significance" | "uncertain_significance" | "vus" => {
                Self::UncertainSignificance
            }
            "likely pathogenic" | "likely_pathogenic" => Self::LikelyPathogenic,
            "pathogenic" => Self::Pathogenic,
            "conflicting interpretations of pathogenicity"
            | "conflicting_interpretations_of_pathogenicity"
            | "conflicting" => Self::Conflicting,
            "drug response" | "drug_response" => Self::DrugResponse,
            "association" => Self::Association,
            "risk factor" | "risk_factor" => Self::RiskFactor,
            "protective" => Self::Protective,
            "affects" => Self::Affects,
            "not provided" | "not_provided" => Self::NotProvided,
            _ => Self::Other,
        })
    }
}

/// ClinVar review status (star rating).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum ReviewStatus {
    /// No assertion criteria provided (0 stars)
    #[default]
    NoAssertion,
    /// No assertion provided (0 stars)
    NoAssertionProvided,
    /// Criteria provided, conflicting interpretations (1 star)
    ConflictingInterpretations,
    /// Criteria provided, single submitter (1 star)
    SingleSubmitter,
    /// Criteria provided, multiple submitters (2 stars)
    MultipleSubmitters,
    /// Reviewed by expert panel (3 stars)
    ExpertPanel,
    /// Practice guideline (4 stars)
    PracticeGuideline,
}

impl ReviewStatus {
    /// Get the star rating (0-4).
    pub fn stars(&self) -> u8 {
        match self {
            Self::NoAssertion | Self::NoAssertionProvided => 0,
            Self::ConflictingInterpretations | Self::SingleSubmitter => 1,
            Self::MultipleSubmitters => 2,
            Self::ExpertPanel => 3,
            Self::PracticeGuideline => 4,
        }
    }

    /// Convert to ClinVar string representation.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::NoAssertion => "no assertion criteria provided",
            Self::NoAssertionProvided => "no assertion provided",
            Self::ConflictingInterpretations => "criteria provided, conflicting interpretations",
            Self::SingleSubmitter => "criteria provided, single submitter",
            Self::MultipleSubmitters => "criteria provided, multiple submitters, no conflicts",
            Self::ExpertPanel => "reviewed by expert panel",
            Self::PracticeGuideline => "practice guideline",
        }
    }
}

impl std::fmt::Display for ReviewStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl FromStr for ReviewStatus {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s_lower = s.to_lowercase();
        Ok(if s_lower.contains("practice guideline") {
            Self::PracticeGuideline
        } else if s_lower.contains("expert panel") {
            Self::ExpertPanel
        } else if s_lower.contains("multiple submitters") {
            Self::MultipleSubmitters
        } else if s_lower.contains("single submitter") {
            Self::SingleSubmitter
        } else if s_lower.contains("conflicting") {
            Self::ConflictingInterpretations
        } else if s_lower.contains("no assertion provided") {
            Self::NoAssertionProvided
        } else {
            Self::NoAssertion
        })
    }
}

/// Information about a disease/condition.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub struct ConditionInfo {
    /// Condition name.
    pub name: String,
    /// MedGen concept ID (e.g., "C0001080").
    pub medgen_id: Option<String>,
    /// OMIM ID.
    pub omim_id: Option<String>,
    /// Orphanet ID.
    pub orphanet_id: Option<String>,
}

impl ConditionInfo {
    /// Create a new condition with just a name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            ..Default::default()
        }
    }

    /// Set the MedGen ID.
    pub fn with_medgen(mut self, id: impl Into<String>) -> Self {
        self.medgen_id = Some(id.into());
        self
    }

    /// Set the OMIM ID.
    pub fn with_omim(mut self, id: impl Into<String>) -> Self {
        self.omim_id = Some(id.into());
        self
    }
}

/// Information about a submitter organization.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub struct SubmitterInfo {
    /// Organization name.
    pub name: String,
    /// Organization ID in ClinVar.
    pub org_id: Option<String>,
    /// Submission date.
    pub date: Option<String>,
}

impl SubmitterInfo {
    /// Create a new submitter info.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            ..Default::default()
        }
    }
}

/// A ClinVar variant record.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct ClinVarRecord {
    /// ClinVar Variation ID.
    pub variation_id: String,
    /// ClinVar Accession (e.g., "VCV000012345").
    pub accession: Option<String>,
    /// HGVS expression (typically the canonical expression).
    pub hgvs: String,
    /// Additional HGVS expressions for this variant.
    pub hgvs_aliases: Vec<String>,
    /// Gene symbol.
    pub gene: Option<String>,
    /// Clinical significance.
    pub significance: ClinicalSignificance,
    /// Review status (raw string from ClinVar).
    pub review_status: String,
    /// Parsed review status.
    pub parsed_review_status: Option<ReviewStatus>,
    /// Associated conditions.
    pub conditions: Vec<ConditionInfo>,
    /// Submitter information.
    pub submitters: Vec<SubmitterInfo>,
    /// rsID if available.
    pub rsid: Option<String>,
    /// Last evaluated date.
    pub last_evaluated: Option<String>,
    /// Last updated date in ClinVar.
    pub last_updated: Option<String>,
}

impl ClinVarRecord {
    /// Create a new ClinVar record.
    pub fn new(variation_id: impl Into<String>, hgvs: impl Into<String>) -> Self {
        Self {
            variation_id: variation_id.into(),
            hgvs: hgvs.into(),
            ..Default::default()
        }
    }

    /// Get the review status stars (0-4).
    pub fn stars(&self) -> u8 {
        self.parsed_review_status
            .map(|r| r.stars())
            .unwrap_or_else(|| {
                self.review_status
                    .parse::<ReviewStatus>()
                    .unwrap_or_default()
                    .stars()
            })
    }

    /// Check if this is a high-confidence pathogenic variant (2+ stars).
    pub fn is_high_confidence_pathogenic(&self) -> bool {
        self.significance.is_pathogenic() && self.stars() >= 2
    }

    /// Check if this is a high-confidence benign variant (2+ stars).
    pub fn is_high_confidence_benign(&self) -> bool {
        self.significance.is_benign() && self.stars() >= 2
    }
}

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

    #[test]
    fn test_clinical_significance_from_str() {
        assert_eq!(
            "Pathogenic".parse::<ClinicalSignificance>().unwrap(),
            ClinicalSignificance::Pathogenic
        );
        assert_eq!(
            "likely benign".parse::<ClinicalSignificance>().unwrap(),
            ClinicalSignificance::LikelyBenign
        );
        assert_eq!(
            "VUS".parse::<ClinicalSignificance>().unwrap(),
            ClinicalSignificance::UncertainSignificance
        );
        assert_eq!(
            "unknown".parse::<ClinicalSignificance>().unwrap(),
            ClinicalSignificance::Other
        );
    }

    #[test]
    fn test_clinical_significance_is_pathogenic() {
        assert!(ClinicalSignificance::Pathogenic.is_pathogenic());
        assert!(ClinicalSignificance::LikelyPathogenic.is_pathogenic());
        assert!(!ClinicalSignificance::Benign.is_pathogenic());
        assert!(!ClinicalSignificance::UncertainSignificance.is_pathogenic());
    }

    #[test]
    fn test_clinical_significance_is_benign() {
        assert!(ClinicalSignificance::Benign.is_benign());
        assert!(ClinicalSignificance::LikelyBenign.is_benign());
        assert!(!ClinicalSignificance::Pathogenic.is_benign());
    }

    #[test]
    fn test_clinical_significance_is_uncertain() {
        assert!(ClinicalSignificance::UncertainSignificance.is_uncertain());
        assert!(ClinicalSignificance::Conflicting.is_uncertain());
        assert!(!ClinicalSignificance::Pathogenic.is_uncertain());
    }

    #[test]
    fn test_review_status_from_str() {
        assert_eq!(
            "criteria provided, single submitter"
                .parse::<ReviewStatus>()
                .unwrap(),
            ReviewStatus::SingleSubmitter
        );
        assert_eq!(
            "reviewed by expert panel".parse::<ReviewStatus>().unwrap(),
            ReviewStatus::ExpertPanel
        );
        assert_eq!(
            "practice guideline".parse::<ReviewStatus>().unwrap(),
            ReviewStatus::PracticeGuideline
        );
    }

    #[test]
    fn test_review_status_stars() {
        assert_eq!(ReviewStatus::NoAssertion.stars(), 0);
        assert_eq!(ReviewStatus::SingleSubmitter.stars(), 1);
        assert_eq!(ReviewStatus::MultipleSubmitters.stars(), 2);
        assert_eq!(ReviewStatus::ExpertPanel.stars(), 3);
        assert_eq!(ReviewStatus::PracticeGuideline.stars(), 4);
    }

    #[test]
    fn test_clinvar_record_new() {
        let record = ClinVarRecord::new("12345", "NM_000088.3:c.10A>G");
        assert_eq!(record.variation_id, "12345");
        assert_eq!(record.hgvs, "NM_000088.3:c.10A>G");
    }

    #[test]
    fn test_clinvar_record_stars() {
        let record = ClinVarRecord {
            variation_id: "12345".to_string(),
            hgvs: "NM_000088.3:c.10A>G".to_string(),
            review_status: "criteria provided, single submitter".to_string(),
            ..Default::default()
        };
        assert_eq!(record.stars(), 1);
    }

    #[test]
    fn test_clinvar_record_high_confidence_pathogenic() {
        let record = ClinVarRecord {
            variation_id: "12345".to_string(),
            hgvs: "NM_000088.3:c.10A>G".to_string(),
            significance: ClinicalSignificance::Pathogenic,
            review_status: "criteria provided, multiple submitters".to_string(),
            ..Default::default()
        };
        assert!(record.is_high_confidence_pathogenic());

        let low_confidence = ClinVarRecord {
            review_status: "no assertion criteria provided".to_string(),
            ..record.clone()
        };
        assert!(!low_confidence.is_high_confidence_pathogenic());
    }

    #[test]
    fn test_condition_info_builder() {
        let condition = ConditionInfo::new("Hereditary cancer syndrome")
            .with_medgen("C1234567")
            .with_omim("123456");

        assert_eq!(condition.name, "Hereditary cancer syndrome");
        assert_eq!(condition.medgen_id, Some("C1234567".to_string()));
        assert_eq!(condition.omim_id, Some("123456".to_string()));
    }
}