ferro-hgvs 0.4.1

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
//! Multi-transcript projection.
//!
//! Project genomic variants onto all overlapping transcripts to generate
//! corresponding c./n. variants.

use crate::data::cdot::CdotMapper;
use crate::data::mapping::{CoordinateMapper, MappingInfo};
use crate::error::FerroError;
use crate::hgvs::location::{CdsPos, GenomePos};
use std::cmp::Ordering;

/// Status of a transcript for clinical prioritization.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ManeStatus {
    /// MANE Select - primary clinical transcript
    Select,
    /// MANE Plus Clinical - additional clinical transcript
    PlusClinical,
    /// Not a MANE transcript
    None,
}

impl ManeStatus {
    /// Get priority order (lower is higher priority).
    fn priority(&self) -> u8 {
        match self {
            ManeStatus::Select => 0,
            ManeStatus::PlusClinical => 1,
            ManeStatus::None => 2,
        }
    }
}

/// Projection result for one transcript.
#[derive(Debug, Clone)]
pub struct TranscriptProjection {
    /// Transcript accession (e.g., "NM_000088.3").
    pub transcript_id: String,
    /// Gene symbol if available.
    pub gene_symbol: Option<String>,
    /// CDS position on this transcript.
    pub cds_position: CdsPos,
    /// MANE status.
    pub mane_status: ManeStatus,
    /// Whether this is the canonical transcript.
    pub is_canonical: bool,
    /// Mapping information (exon numbers, region, etc.).
    pub mapping_info: MappingInfo,
    /// CDS length for this transcript.
    pub cds_length: Option<u64>,
}

impl TranscriptProjection {
    /// Format as HGVS c./n. string.
    pub fn to_hgvs_string(&self) -> String {
        let coord_type =
            if self.mapping_info.in_cds || self.mapping_info.in_5utr || self.mapping_info.in_3utr {
                "c"
            } else {
                "n"
            };
        format!(
            "{}:{}.{}",
            self.transcript_id, coord_type, self.cds_position
        )
    }
}

/// All projections for a genomic position.
#[derive(Debug, Clone)]
pub struct ProjectionResult {
    /// Source contig.
    pub source_contig: String,
    /// Source position (1-based).
    pub source_pos: u64,
    /// All transcript projections, sorted by priority.
    pub projections: Vec<TranscriptProjection>,
}

impl ProjectionResult {
    /// Get the highest priority projection (MANE Select if available).
    pub fn best(&self) -> Option<&TranscriptProjection> {
        self.projections.first()
    }

    /// Get all MANE transcripts.
    pub fn mane_only(&self) -> Vec<&TranscriptProjection> {
        self.projections
            .iter()
            .filter(|p| p.mane_status != ManeStatus::None)
            .collect()
    }

    /// Get projections for a specific gene.
    pub fn for_gene(&self, gene: &str) -> Vec<&TranscriptProjection> {
        self.projections
            .iter()
            .filter(|p| p.gene_symbol.as_deref() == Some(gene))
            .collect()
    }

    /// Check if position maps to any coding region.
    pub fn is_coding(&self) -> bool {
        self.projections.iter().any(|p| p.mapping_info.in_cds)
    }
}

/// Projector for mapping genomic positions to transcripts.
#[derive(Debug, Clone)]
pub struct Projector {
    mapper: CoordinateMapper,
    mane_select: std::collections::HashSet<String>,
    mane_plus_clinical: std::collections::HashSet<String>,
    canonical: std::collections::HashSet<String>,
}

impl Projector {
    /// Create a new projector from a cdot mapper.
    pub fn new(cdot: CdotMapper) -> Self {
        Self {
            mapper: CoordinateMapper::new(cdot),
            mane_select: std::collections::HashSet::new(),
            mane_plus_clinical: std::collections::HashSet::new(),
            canonical: std::collections::HashSet::new(),
        }
    }

    /// Create with MANE transcript annotations.
    pub fn with_mane(mut self, select: Vec<String>, plus_clinical: Vec<String>) -> Self {
        self.mane_select = select.into_iter().collect();
        self.mane_plus_clinical = plus_clinical.into_iter().collect();
        self
    }

    /// Create with canonical transcript annotations.
    pub fn with_canonical(mut self, canonical: Vec<String>) -> Self {
        self.canonical = canonical.into_iter().collect();
        self
    }

    /// Get the underlying coordinate mapper.
    pub fn mapper(&self) -> &CoordinateMapper {
        &self.mapper
    }

    /// Project a genomic position to all overlapping transcripts.
    ///
    /// # Arguments
    ///
    /// * `contig` - Contig/chromosome name
    /// * `pos` - 1-based genomic position
    ///
    /// # Returns
    ///
    /// All transcript projections, sorted by clinical priority.
    pub fn project(&self, contig: &str, pos: u64) -> Result<ProjectionResult, FerroError> {
        // Find overlapping transcripts
        let transcripts = self.mapper.find_overlapping_transcripts(contig, pos);

        if transcripts.is_empty() {
            return Ok(ProjectionResult {
                source_contig: contig.to_string(),
                source_pos: pos,
                projections: Vec::new(),
            });
        }

        let genome_pos = GenomePos::new(pos);
        let mut projections = Vec::new();

        for (tx_id, tx) in transcripts {
            match self.mapper.genome_to_cds(tx_id, &genome_pos) {
                Ok(result) => {
                    let projection = TranscriptProjection {
                        transcript_id: tx_id.to_string(),
                        gene_symbol: tx.gene_name.clone(),
                        cds_position: result.variant,
                        mane_status: self.get_mane_status(tx_id),
                        is_canonical: self.canonical.contains(tx_id),
                        mapping_info: result.info,
                        cds_length: tx.cds_length(),
                    };
                    projections.push(projection);
                }
                Err(_) => {
                    // Position might be in intron or outside CDS
                    // Skip this transcript
                    continue;
                }
            }
        }

        // Sort by priority
        self.sort_projections(&mut projections);

        Ok(ProjectionResult {
            source_contig: contig.to_string(),
            source_pos: pos,
            projections,
        })
    }

    /// Project to a specific transcript only.
    pub fn project_to_transcript(
        &self,
        _contig: &str,
        pos: u64,
        transcript_id: &str,
    ) -> Result<TranscriptProjection, FerroError> {
        let tx = self
            .mapper
            .cdot()
            .get_transcript(transcript_id)
            .ok_or_else(|| FerroError::ReferenceNotFound {
                id: transcript_id.to_string(),
            })?;

        let genome_pos = GenomePos::new(pos);
        let result = self.mapper.genome_to_cds(transcript_id, &genome_pos)?;

        Ok(TranscriptProjection {
            transcript_id: transcript_id.to_string(),
            gene_symbol: tx.gene_name.clone(),
            cds_position: result.variant,
            mane_status: self.get_mane_status(transcript_id),
            is_canonical: self.canonical.contains(transcript_id),
            mapping_info: result.info,
            cds_length: tx.cds_length(),
        })
    }

    /// Get MANE status for a transcript.
    fn get_mane_status(&self, transcript_id: &str) -> ManeStatus {
        // Strip version for lookup
        let base_id = transcript_id.split('.').next().unwrap_or(transcript_id);

        if self.mane_select.contains(transcript_id) || self.mane_select.contains(base_id) {
            ManeStatus::Select
        } else if self.mane_plus_clinical.contains(transcript_id)
            || self.mane_plus_clinical.contains(base_id)
        {
            ManeStatus::PlusClinical
        } else {
            ManeStatus::None
        }
    }

    /// Sort projections by clinical priority.
    fn sort_projections(&self, projections: &mut [TranscriptProjection]) {
        projections.sort_by(|a, b| {
            // 1. MANE status (Select > PlusClinical > None)
            let mane_cmp = a.mane_status.priority().cmp(&b.mane_status.priority());
            if mane_cmp != Ordering::Equal {
                return mane_cmp;
            }

            // 2. Canonical status
            let canonical_cmp = b.is_canonical.cmp(&a.is_canonical);
            if canonical_cmp != Ordering::Equal {
                return canonical_cmp;
            }

            // 3. Longer CDS preferred
            let len_cmp = b.cds_length.cmp(&a.cds_length);
            if len_cmp != Ordering::Equal {
                return len_cmp;
            }

            // 4. Alphabetical by transcript ID
            a.transcript_id.cmp(&b.transcript_id)
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::cdot::CdotTranscript;
    use crate::reference::Strand;

    fn create_test_mapper() -> CdotMapper {
        let mut cdot = CdotMapper::new();

        // Transcript 1 - "MANE Select"
        let tx1 = CdotTranscript {
            gene_name: Some("TESTGENE".to_string()),
            contig: "chr1".to_string(),
            strand: Strand::Plus,
            exons: vec![
                [1000, 1100, 0, 100],
                [2000, 2200, 100, 300],
                [3000, 3150, 300, 450],
            ],
            cds_start: Some(50),
            cds_end: Some(400),
            gene_id: None,
            protein: None,
            exon_cigars: Vec::new(),
        };
        cdot.add_transcript("NM_000001.1".to_string(), tx1);

        // Transcript 2 - different isoform
        let tx2 = CdotTranscript {
            gene_name: Some("TESTGENE".to_string()),
            contig: "chr1".to_string(),
            strand: Strand::Plus,
            exons: vec![[1000, 1150, 0, 150], [2000, 2200, 150, 350]],
            cds_start: Some(50),
            cds_end: Some(300),
            gene_id: None,
            protein: None,
            exon_cigars: Vec::new(),
        };
        cdot.add_transcript("NM_000001.2".to_string(), tx2);

        cdot
    }

    #[test]
    fn test_project_single_position() {
        let cdot = create_test_mapper();
        let projector = Projector::new(cdot);

        let result = projector.project("chr1", 1050).unwrap();

        // Both transcripts should overlap position 1050
        assert!(!result.projections.is_empty());
        assert!(result.projections.len() <= 2);
    }

    #[test]
    fn test_project_with_mane() {
        let cdot = create_test_mapper();
        let projector = Projector::new(cdot).with_mane(vec!["NM_000001.1".to_string()], vec![]);

        let result = projector.project("chr1", 1050).unwrap();

        // MANE Select should be first
        if let Some(first) = result.best() {
            assert_eq!(first.mane_status, ManeStatus::Select);
        }
    }

    #[test]
    fn test_project_no_overlap() {
        let cdot = create_test_mapper();
        let projector = Projector::new(cdot);

        let result = projector.project("chr1", 5000).unwrap();

        assert!(result.projections.is_empty());
    }

    #[test]
    fn test_project_to_specific_transcript() {
        let cdot = create_test_mapper();
        let projector = Projector::new(cdot);

        let result = projector
            .project_to_transcript("chr1", 1050, "NM_000001.1")
            .unwrap();

        assert_eq!(result.transcript_id, "NM_000001.1");
        assert_eq!(result.gene_symbol, Some("TESTGENE".to_string()));
    }

    #[test]
    fn test_mane_status() {
        let cdot = create_test_mapper();
        let projector = Projector::new(cdot).with_mane(
            vec!["NM_000001.1".to_string()],
            vec!["NM_000001.2".to_string()],
        );

        assert_eq!(projector.get_mane_status("NM_000001.1"), ManeStatus::Select);
        assert_eq!(
            projector.get_mane_status("NM_000001.2"),
            ManeStatus::PlusClinical
        );
        assert_eq!(projector.get_mane_status("NM_000099.1"), ManeStatus::None);
    }

    #[test]
    fn test_projection_result_methods() {
        let cdot = create_test_mapper();
        let projector = Projector::new(cdot)
            .with_mane(vec!["NM_000001.1".to_string()], vec![])
            .with_canonical(vec!["NM_000001.1".to_string()]);

        let result = projector.project("chr1", 1050).unwrap();

        // Test is_coding
        assert!(result.is_coding());

        // Test for_gene
        let gene_results = result.for_gene("TESTGENE");
        assert!(!gene_results.is_empty());

        // Test mane_only
        let mane_results = result.mane_only();
        assert!(!mane_results.is_empty());
    }

    #[test]
    fn test_to_hgvs_string() {
        let cdot = create_test_mapper();
        let projector = Projector::new(cdot);

        let result = projector
            .project_to_transcript("chr1", 1050, "NM_000001.1")
            .unwrap();

        let hgvs = result.to_hgvs_string();
        assert!(hgvs.starts_with("NM_000001.1:"));
        assert!(hgvs.contains("."));
    }
}