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
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
//! VCF-based rsID lookup.
//!
//! Provides rsID lookup from dbSNP or other VCF files containing variant IDs.
//!
//! # Example
//!
//! ```no_run
//! use ferro_hgvs::rsid::vcf::VcfRsIdLookup;
//! use ferro_hgvs::rsid::RsIdLookup;  // Import trait to use lookup method
//! use ferro_hgvs::reference::transcript::GenomeBuild;
//!
//! // Load from a dbSNP VCF file
//! let lookup = VcfRsIdLookup::from_vcf("dbsnp.vcf.gz", GenomeBuild::GRCh38).unwrap();
//!
//! // Look up an rsID
//! let results = lookup.lookup("rs121913529").unwrap();
//! for result in results {
//!     println!("{}: {}:{} {}>{}",
//!         result.rsid, result.contig, result.position,
//!         result.reference, result.alternate
//!     );
//! }
//! ```

use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;

use flate2::read::GzDecoder;

use crate::error::FerroError;
use crate::reference::transcript::GenomeBuild;

use super::{parse_rsid, RsIdLookup, RsIdMetadata, RsIdResult};

/// VCF-based rsID lookup.
///
/// Loads variant data from VCF files (like dbSNP) and provides rsID lookup.
/// Supports both plain text and gzip-compressed VCF files.
#[derive(Debug, Clone)]
pub struct VcfRsIdLookup {
    /// Indexed entries: rsID numeric value -> list of results
    entries: HashMap<u64, Vec<RsIdResult>>,
    /// Metadata about the data source
    metadata: RsIdMetadata,
}

impl VcfRsIdLookup {
    /// Create a new empty VCF lookup.
    pub fn new(build: GenomeBuild) -> Self {
        Self {
            entries: HashMap::new(),
            metadata: RsIdMetadata {
                source: "VCF".to_string(),
                build,
                version: "unknown".to_string(),
                variant_count: 0,
            },
        }
    }

    /// Load rsID lookup from a VCF file.
    ///
    /// Automatically detects gzip compression based on `.gz` extension.
    pub fn from_vcf<P: AsRef<Path>>(path: P, build: GenomeBuild) -> Result<Self, FerroError> {
        let path = path.as_ref();
        let file = File::open(path).map_err(|e| FerroError::Io {
            msg: format!("Failed to open VCF file '{}': {}", path.display(), e),
        })?;

        let filename = path
            .file_name()
            .and_then(|s| s.to_str())
            .unwrap_or("unknown");

        if path.extension().is_some_and(|ext| ext == "gz") {
            let decoder = GzDecoder::new(file);
            let reader = BufReader::new(decoder);
            Self::from_reader(reader, build, filename)
        } else {
            let reader = BufReader::new(file);
            Self::from_reader(reader, build, filename)
        }
    }

    /// Load rsID lookup from a buffered reader.
    pub fn from_reader<R: BufRead>(
        reader: R,
        build: GenomeBuild,
        source_name: &str,
    ) -> Result<Self, FerroError> {
        let mut lookup = Self::new(build);
        lookup.metadata.source = format!("VCF: {}", source_name);

        let mut version = None;

        for line in reader.lines() {
            let line = line.map_err(|e| FerroError::Io {
                msg: format!("Failed to read VCF line: {}", e),
            })?;

            // Skip empty lines
            if line.is_empty() {
                continue;
            }

            // Handle header lines
            if line.starts_with('#') {
                // Extract version from ##fileformat line
                if let Some(ver) = line.strip_prefix("##fileformat=") {
                    version = Some(ver.to_string());
                }
                // Extract dbSNP version if present
                if let Some(build_id) = line.strip_prefix("##dbSNP_BUILD_ID=") {
                    version = Some(format!("dbSNP {}", build_id));
                }
                continue;
            }

            // Parse data line
            if let Some(results) = parse_vcf_line(&line, build) {
                for result in results {
                    if let Ok(rsid_num) = parse_rsid(&result.rsid) {
                        lookup.entries.entry(rsid_num).or_default().push(result);
                    }
                }
            }
        }

        lookup.metadata.version = version.unwrap_or_else(|| "unknown".to_string());
        lookup.metadata.variant_count = lookup.entries.values().map(|v| v.len() as u64).sum();

        Ok(lookup)
    }

    /// Add a single rsID entry.
    pub fn add(&mut self, result: RsIdResult) -> Result<(), FerroError> {
        let rsid_num = parse_rsid(&result.rsid)?;
        self.entries.entry(rsid_num).or_default().push(result);
        self.metadata.variant_count = self.entries.values().map(|v| v.len() as u64).sum();
        Ok(())
    }

    /// Get the number of unique rsIDs indexed.
    pub fn rsid_count(&self) -> usize {
        self.entries.len()
    }

    /// Get the total number of variant entries (may be more than rsID count
    /// for multi-allelic variants).
    pub fn variant_count(&self) -> u64 {
        self.metadata.variant_count
    }
}

impl RsIdLookup for VcfRsIdLookup {
    fn lookup(&self, rsid: &str) -> Result<Vec<RsIdResult>, FerroError> {
        let rsid_num = parse_rsid(rsid)?;
        self.entries
            .get(&rsid_num)
            .cloned()
            .ok_or_else(|| FerroError::ReferenceNotFound {
                id: rsid.to_string(),
            })
    }

    fn contains(&self, rsid: &str) -> bool {
        parse_rsid(rsid)
            .map(|n| self.entries.contains_key(&n))
            .unwrap_or(false)
    }

    fn metadata(&self) -> &RsIdMetadata {
        &self.metadata
    }
}

/// Parse a VCF data line and extract rsID results.
fn parse_vcf_line(line: &str, build: GenomeBuild) -> Option<Vec<RsIdResult>> {
    let fields: Vec<&str> = line.split('\t').collect();
    if fields.len() < 5 {
        return None;
    }

    let chrom = fields[0];
    let pos: u64 = fields[1].parse().ok()?;
    let id_field = fields[2];
    let reference = fields[3];
    let alt_field = fields[4];

    // Skip if no ID or ID is "."
    if id_field.is_empty() || id_field == "." {
        return None;
    }

    // VCF field indices: CHROM(0), POS(1), ID(2), REF(3), ALT(4), QUAL(5), FILTER(6), INFO(7)
    // Parse INFO field for allele frequency if present
    let allele_frequency = if fields.len() > 7 {
        parse_allele_frequency(fields[7]) // INFO field
    } else {
        None
    };

    // Parse clinical significance from INFO if present
    let clinical_significance = if fields.len() > 7 {
        parse_clinical_significance(fields[7]) // INFO field
    } else {
        None
    };

    // Handle multiple IDs (semicolon-separated)
    let ids: Vec<&str> = id_field.split(';').collect();

    // Handle multiple alternate alleles (comma-separated)
    let alts: Vec<&str> = alt_field.split(',').collect();

    let mut results = Vec::new();

    // Create result for each rsID + alt allele combination
    for id in &ids {
        // Only process rsIDs
        if !id.starts_with("rs") {
            continue;
        }

        for alt in &alts {
            // Skip symbolic alleles like <DEL>, <INS>, etc.
            if alt.starts_with('<') && alt.ends_with('>') {
                continue;
            }

            let result = RsIdResult {
                rsid: (*id).to_string(),
                contig: chrom.to_string(),
                position: pos,
                reference: reference.to_string(),
                alternate: (*alt).to_string(),
                build,
                hgvs: None,
                allele_frequency,
                clinical_significance: clinical_significance.clone(),
            };
            results.push(result);
        }
    }

    if results.is_empty() {
        None
    } else {
        Some(results)
    }
}

/// Parse allele frequency from INFO field.
fn parse_allele_frequency(info: &str) -> Option<f64> {
    // Look for AF= or CAF= field
    for field in info.split(';') {
        if let Some(af_str) = field.strip_prefix("AF=") {
            // Take first value if comma-separated
            let first = af_str.split(',').next()?;
            return first.parse().ok();
        }
        if let Some(caf_str) = field.strip_prefix("CAF=") {
            // CAF is ref,alt1,alt2,... - take second value (first alt)
            let mut parts = caf_str.split(',');
            parts.next(); // Skip ref frequency
            let alt_freq = parts.next()?;
            if alt_freq != "." {
                return alt_freq.parse().ok();
            }
        }
    }
    None
}

/// Parse clinical significance from INFO field.
fn parse_clinical_significance(info: &str) -> Option<String> {
    for field in info.split(';') {
        // dbSNP uses CLNSIG
        if let Some(clnsig) = field.strip_prefix("CLNSIG=") {
            return Some(clnsig.to_string());
        }
        // ClinVar uses CLNSIG as well
        if let Some(clinical) = field.strip_prefix("CLINICAL_SIGNIFICANCE=") {
            return Some(clinical.to_string());
        }
    }
    None
}

/// Streaming VCF rsID lookup for large files.
///
/// Instead of loading the entire VCF into memory, this searches the file
/// on each lookup. Suitable for very large files where memory is constrained.
#[derive(Debug)]
pub struct StreamingVcfRsIdLookup {
    path: std::path::PathBuf,
    metadata: RsIdMetadata,
}

impl StreamingVcfRsIdLookup {
    /// Create a new streaming lookup from a VCF file.
    pub fn new<P: AsRef<Path>>(path: P, build: GenomeBuild) -> Result<Self, FerroError> {
        let path = path.as_ref();

        // Verify file exists
        if !path.exists() {
            return Err(FerroError::Io {
                msg: format!("VCF file not found: {}", path.display()),
            });
        }

        let filename = path
            .file_name()
            .and_then(|s| s.to_str())
            .unwrap_or("unknown");

        Ok(Self {
            path: path.to_path_buf(),
            metadata: RsIdMetadata {
                source: format!("VCF (streaming): {}", filename),
                build,
                version: "unknown".to_string(),
                variant_count: 0, // Unknown for streaming
            },
        })
    }

    /// Search for an rsID in the VCF file.
    fn search_file(&self, rsid_num: u64) -> Result<Vec<RsIdResult>, FerroError> {
        let file = File::open(&self.path).map_err(|e| FerroError::Io {
            msg: format!("Failed to open VCF file: {}", e),
        })?;

        let reader: Box<dyn BufRead> = if self.path.extension().is_some_and(|ext| ext == "gz") {
            Box::new(BufReader::new(GzDecoder::new(file)))
        } else {
            Box::new(BufReader::new(file))
        };

        let rsid_str = format!("rs{}", rsid_num);
        let mut results = Vec::new();

        for line in reader.lines() {
            let line = line.map_err(|e| FerroError::Io {
                msg: format!("Failed to read VCF line: {}", e),
            })?;

            // Skip headers
            if line.starts_with('#') {
                continue;
            }

            // Quick check if this line might contain our rsID
            if !line.contains(&rsid_str) {
                continue;
            }

            // Parse the line
            if let Some(line_results) = parse_vcf_line(&line, self.metadata.build) {
                for result in line_results {
                    if result.rsid == rsid_str {
                        results.push(result);
                    }
                }
            }
        }

        if results.is_empty() {
            Err(FerroError::ReferenceNotFound { id: rsid_str })
        } else {
            Ok(results)
        }
    }
}

impl RsIdLookup for StreamingVcfRsIdLookup {
    fn lookup(&self, rsid: &str) -> Result<Vec<RsIdResult>, FerroError> {
        let rsid_num = parse_rsid(rsid)?;
        self.search_file(rsid_num)
    }

    fn contains(&self, rsid: &str) -> bool {
        self.lookup(rsid).is_ok()
    }

    fn metadata(&self) -> &RsIdMetadata {
        &self.metadata
    }
}

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

    const TEST_VCF: &str = r#"##fileformat=VCFv4.3
##dbSNP_BUILD_ID=156
##INFO=<ID=AF,Number=A,Type=Float,Description="Allele Frequency">
##INFO=<ID=CLNSIG,Number=.,Type=String,Description="Clinical significance">
#CHROM	POS	ID	REF	ALT	QUAL	FILTER	INFO
chr7	140753336	rs121913529	A	T	.	.	AF=0.0001;CLNSIG=pathogenic
chr17	43124027	rs80357906	GAG	G	.	.	AF=0.0001
chr1	12345	rs12345;rs67890	A	G,T	.	.	AF=0.01,0.02
chr1	100	.	A	G	.	.	.
chr1	200	rs999	C	<DEL>	.	.	.
"#;

    #[test]
    fn test_from_reader() {
        let reader = BufReader::new(TEST_VCF.as_bytes());
        let lookup = VcfRsIdLookup::from_reader(reader, GenomeBuild::GRCh38, "test.vcf").unwrap();

        assert_eq!(lookup.metadata.version, "dbSNP 156");
        assert!(lookup.rsid_count() >= 3); // rs121913529, rs80357906, rs12345, rs67890
    }

    #[test]
    fn test_lookup_snv() {
        let reader = BufReader::new(TEST_VCF.as_bytes());
        let lookup = VcfRsIdLookup::from_reader(reader, GenomeBuild::GRCh38, "test.vcf").unwrap();

        let results = lookup.lookup("rs121913529").unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].contig, "chr7");
        assert_eq!(results[0].position, 140753336);
        assert_eq!(results[0].reference, "A");
        assert_eq!(results[0].alternate, "T");
        assert!(results[0].is_snv());
        assert_eq!(results[0].allele_frequency, Some(0.0001));
        assert_eq!(
            results[0].clinical_significance,
            Some("pathogenic".to_string())
        );
    }

    #[test]
    fn test_lookup_deletion() {
        let reader = BufReader::new(TEST_VCF.as_bytes());
        let lookup = VcfRsIdLookup::from_reader(reader, GenomeBuild::GRCh38, "test.vcf").unwrap();

        let results = lookup.lookup("rs80357906").unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].contig, "chr17");
        assert!(results[0].is_deletion());
    }

    #[test]
    fn test_lookup_multiallelic() {
        let reader = BufReader::new(TEST_VCF.as_bytes());
        let lookup = VcfRsIdLookup::from_reader(reader, GenomeBuild::GRCh38, "test.vcf").unwrap();

        // rs12345 should have two entries (one for each alt allele)
        let results = lookup.lookup("rs12345").unwrap();
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].alternate, "G");
        assert_eq!(results[1].alternate, "T");
    }

    #[test]
    fn test_lookup_multiple_ids() {
        let reader = BufReader::new(TEST_VCF.as_bytes());
        let lookup = VcfRsIdLookup::from_reader(reader, GenomeBuild::GRCh38, "test.vcf").unwrap();

        // Both rs12345 and rs67890 should be found
        assert!(lookup.contains("rs12345"));
        assert!(lookup.contains("rs67890"));
    }

    #[test]
    fn test_lookup_not_found() {
        let reader = BufReader::new(TEST_VCF.as_bytes());
        let lookup = VcfRsIdLookup::from_reader(reader, GenomeBuild::GRCh38, "test.vcf").unwrap();

        let result = lookup.lookup("rs99999999");
        assert!(result.is_err());
    }

    #[test]
    fn test_contains() {
        let reader = BufReader::new(TEST_VCF.as_bytes());
        let lookup = VcfRsIdLookup::from_reader(reader, GenomeBuild::GRCh38, "test.vcf").unwrap();

        assert!(lookup.contains("rs121913529"));
        assert!(lookup.contains("121913529")); // Without prefix
        assert!(!lookup.contains("rs99999999"));
    }

    #[test]
    fn test_skips_missing_id() {
        let reader = BufReader::new(TEST_VCF.as_bytes());
        let lookup = VcfRsIdLookup::from_reader(reader, GenomeBuild::GRCh38, "test.vcf").unwrap();

        // The line with "." as ID should not create any entries
        // So we just verify other lookups work
        assert!(lookup.rsid_count() >= 3);
    }

    #[test]
    fn test_skips_symbolic_alleles() {
        let reader = BufReader::new(TEST_VCF.as_bytes());
        let lookup = VcfRsIdLookup::from_reader(reader, GenomeBuild::GRCh38, "test.vcf").unwrap();

        // rs999 has <DEL> as alt, should not have any results
        assert!(!lookup.contains("rs999"));
    }

    #[test]
    fn test_to_hgvs() {
        let reader = BufReader::new(TEST_VCF.as_bytes());
        let lookup = VcfRsIdLookup::from_reader(reader, GenomeBuild::GRCh38, "test.vcf").unwrap();

        let results = lookup.lookup("rs121913529").unwrap();
        assert_eq!(results[0].to_hgvs(), "g.140753336A>T");
    }

    #[test]
    fn test_add_entry() {
        let mut lookup = VcfRsIdLookup::new(GenomeBuild::GRCh38);

        let result = RsIdResult::new(
            "rs12345".to_string(),
            "chr1".to_string(),
            100,
            "A".to_string(),
            "G".to_string(),
            GenomeBuild::GRCh38,
        );

        lookup.add(result).unwrap();
        assert!(lookup.contains("rs12345"));
        assert_eq!(lookup.rsid_count(), 1);
    }

    #[test]
    fn test_parse_allele_frequency() {
        assert_eq!(parse_allele_frequency("AF=0.01"), Some(0.01));
        assert_eq!(parse_allele_frequency("DP=100;AF=0.05"), Some(0.05));
        assert_eq!(parse_allele_frequency("AF=0.1,0.2,0.3"), Some(0.1));
        assert_eq!(parse_allele_frequency("CAF=0.99,0.01"), Some(0.01));
        assert_eq!(parse_allele_frequency("DP=100"), None);
    }

    #[test]
    fn test_parse_clinical_significance() {
        assert_eq!(
            parse_clinical_significance("CLNSIG=pathogenic"),
            Some("pathogenic".to_string())
        );
        assert_eq!(
            parse_clinical_significance("DP=100;CLNSIG=benign"),
            Some("benign".to_string())
        );
        assert_eq!(parse_clinical_significance("DP=100"), None);
    }

    #[test]
    fn test_metadata() {
        let reader = BufReader::new(TEST_VCF.as_bytes());
        let lookup = VcfRsIdLookup::from_reader(reader, GenomeBuild::GRCh38, "test.vcf").unwrap();

        let meta = lookup.metadata();
        assert!(meta.source.contains("test.vcf"));
        assert_eq!(meta.build, GenomeBuild::GRCh38);
        assert!(meta.variant_count > 0);
    }
}