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
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
//! Batch commands for parsing and normalizing HGVS variants.
//!
//! This module provides batch processing functionality for the ferro CLI,
//! including support for timing output and parallel processing.

use crate::reference::ReferenceProvider;
use crate::{parse_hgvs, FerroError, MockProvider, MultiFastaProvider, Normalizer};
use indicatif::{ProgressBar, ProgressStyle};
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::Path;
use std::time::{Duration, Instant};

/// Result of processing a single variant.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariantResult {
    /// Input HGVS string
    pub input: String,

    /// Whether processing succeeded
    pub success: bool,

    /// Output (parsed or normalized) if successful
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output: Option<String>,

    /// Error message if failed
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

/// Timing information for a batch operation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimingInfo {
    /// Total variants processed
    pub total: usize,

    /// Successfully processed
    pub successful: usize,

    /// Failed
    pub failed: usize,

    /// Total elapsed time in seconds
    pub elapsed_seconds: f64,

    /// Throughput (variants per second)
    pub variants_per_second: f64,
}

impl TimingInfo {
    /// Create timing info from measurements.
    pub fn new(total: usize, successful: usize, elapsed: Duration) -> Self {
        let elapsed_secs = elapsed.as_secs_f64();
        let throughput = if elapsed_secs > f64::EPSILON {
            total as f64 / elapsed_secs
        } else {
            0.0
        };

        Self {
            total,
            successful,
            failed: total - successful,
            elapsed_seconds: elapsed_secs,
            variants_per_second: throughput,
        }
    }
}

/// Results from a batch operation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchResults {
    /// All results
    pub results: Vec<VariantResult>,

    /// Timing information
    pub timing: TimingInfo,
}

/// Configuration for batch normalization.
#[derive(Debug, Clone)]
pub struct NormalizeConfig {
    /// Reference directory (with manifest.json)
    pub reference_dir: Option<std::path::PathBuf>,

    /// Show progress bar
    pub show_progress: bool,

    /// Number of workers for parallel processing
    pub workers: usize,
}

impl Default for NormalizeConfig {
    fn default() -> Self {
        Self {
            reference_dir: None,
            show_progress: true,
            workers: 1,
        }
    }
}

/// Create a reference provider from a directory.
///
/// Handles:
/// - Directory with manifest.json → MultiFastaProvider::from_manifest
/// - Directory without manifest → MultiFastaProvider::from_directory
/// - Falls back to MockProvider with test data
pub fn create_reference_provider(
    reference_dir: Option<&Path>,
) -> Result<Box<dyn ReferenceProvider>, FerroError> {
    match reference_dir {
        Some(ref_path) => {
            let manifest_path = ref_path.join("manifest.json");
            if manifest_path.exists() {
                eprintln!("Using reference data from {}", ref_path.display());
                let provider = MultiFastaProvider::from_manifest(&manifest_path)?;
                Ok(Box::new(provider))
            } else if ref_path.is_dir() {
                eprintln!("Using reference data from directory {}", ref_path.display());
                let provider = MultiFastaProvider::from_directory(ref_path)?;
                Ok(Box::new(provider))
            } else {
                eprintln!("Reference path not recognized, using test data");
                Ok(Box::new(MockProvider::with_test_data()))
            }
        }
        None => {
            eprintln!("No reference data provided, using mock provider");
            Ok(Box::new(MockProvider::with_test_data()))
        }
    }
}

/// Normalize variants from a file.
///
/// # Arguments
/// * `input` - Path to input file (one variant per line)
/// * `config` - Normalization configuration
///
/// # Returns
/// Batch results with all normalize results and timing info.
pub fn normalize_batch<P: AsRef<Path>>(
    input: P,
    config: &NormalizeConfig,
) -> Result<BatchResults, FerroError> {
    let input = input.as_ref();

    // Create normalizer with appropriate reference provider
    let provider = create_reference_provider(config.reference_dir.as_deref())?;
    let normalizer = Normalizer::new(provider);

    // Read variants
    let variants = read_variants(input)?;

    if variants.is_empty() {
        return Ok(BatchResults {
            results: Vec::new(),
            timing: TimingInfo::new(0, 0, Duration::ZERO),
        });
    }

    // Progress bar
    let pb = if config.show_progress {
        let pb = ProgressBar::new(variants.len() as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}")
                .unwrap()
                .progress_chars("##-"),
        );
        Some(pb)
    } else {
        None
    };

    // Normalize all variants
    let start = Instant::now();

    let results: Vec<VariantResult> = variants
        .iter()
        .map(|variant| {
            if let Some(ref pb) = pb {
                pb.inc(1);
            }

            match parse_hgvs(variant) {
                Ok(parsed) => {
                    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                        normalizer.normalize(&parsed)
                    })) {
                        Ok(Ok(normalized)) => VariantResult {
                            input: variant.clone(),
                            success: true,
                            output: Some(normalized.to_string()),
                            error: None,
                        },
                        Ok(Err(e)) => VariantResult {
                            input: variant.clone(),
                            success: false,
                            output: None,
                            error: Some(format!("{}", e)),
                        },
                        Err(payload) => {
                            let msg = payload
                                .downcast_ref::<String>()
                                .map(|s| s.as_str())
                                .or_else(|| payload.downcast_ref::<&str>().copied())
                                .unwrap_or("unknown panic");
                            VariantResult {
                                input: variant.clone(),
                                success: false,
                                output: None,
                                error: Some(format!(
                                    "internal error: panic during normalization: {}",
                                    msg
                                )),
                            }
                        }
                    }
                }
                Err(e) => VariantResult {
                    input: variant.clone(),
                    success: false,
                    output: None,
                    error: Some(format!("{}", e)),
                },
            }
        })
        .collect();

    let elapsed = start.elapsed();

    if let Some(pb) = pb {
        pb.finish_with_message(format!(
            "Normalized {} variants in {:.2}s",
            variants.len(),
            elapsed.as_secs_f64()
        ));
    }

    let successful = results.iter().filter(|r| r.success).count();
    let timing = TimingInfo::new(variants.len(), successful, elapsed);

    Ok(BatchResults { results, timing })
}

/// Parse variants from a file (validation only, no normalization).
///
/// # Arguments
/// * `input` - Path to input file (one variant per line)
/// * `show_progress` - Whether to show a progress bar
///
/// # Returns
/// Batch results with all parse results and timing info.
pub fn parse_batch<P: AsRef<Path>>(
    input: P,
    show_progress: bool,
) -> Result<BatchResults, FerroError> {
    let input = input.as_ref();

    // Read variants
    let variants = read_variants(input)?;

    if variants.is_empty() {
        return Ok(BatchResults {
            results: Vec::new(),
            timing: TimingInfo::new(0, 0, Duration::ZERO),
        });
    }

    // Progress bar
    let pb = if show_progress {
        let pb = ProgressBar::new(variants.len() as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}")
                .unwrap()
                .progress_chars("##-"),
        );
        Some(pb)
    } else {
        None
    };

    // Parse all variants
    let start = Instant::now();

    let results: Vec<VariantResult> = variants
        .iter()
        .map(|variant| {
            if let Some(ref pb) = pb {
                pb.inc(1);
            }

            match parse_hgvs(variant) {
                Ok(parsed) => VariantResult {
                    input: variant.clone(),
                    success: true,
                    output: Some(parsed.to_string()),
                    error: None,
                },
                Err(e) => VariantResult {
                    input: variant.clone(),
                    success: false,
                    output: None,
                    error: Some(format!("{}", e)),
                },
            }
        })
        .collect();

    let elapsed = start.elapsed();

    if let Some(pb) = pb {
        pb.finish_with_message(format!(
            "Parsed {} variants in {:.2}s",
            variants.len(),
            elapsed.as_secs_f64()
        ));
    }

    let successful = results.iter().filter(|r| r.success).count();
    let timing = TimingInfo::new(variants.len(), successful, elapsed);

    Ok(BatchResults { results, timing })
}

/// Read variants from a file (one per line).
fn read_variants<P: AsRef<Path>>(path: P) -> Result<Vec<String>, FerroError> {
    let path = path.as_ref();
    let file = File::open(path).map_err(|e| FerroError::Io {
        msg: format!("Failed to open {}: {}", path.display(), e),
    })?;

    let reader = BufReader::new(file);
    let variants: Vec<String> = reader
        .lines()
        .map_while(Result::ok)
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .collect();

    Ok(variants)
}

/// Write batch results to a JSON file.
pub fn write_results<P: AsRef<Path>>(results: &BatchResults, path: P) -> Result<(), FerroError> {
    let path = path.as_ref();

    // Create parent directory if needed
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(|e| FerroError::Io {
            msg: format!("Failed to create directory {}: {}", parent.display(), e),
        })?;
    }

    let file = File::create(path).map_err(|e| FerroError::Io {
        msg: format!("Failed to create {}: {}", path.display(), e),
    })?;

    let writer = BufWriter::new(file);
    serde_json::to_writer_pretty(writer, results).map_err(|e| FerroError::Io {
        msg: format!("Failed to write JSON: {}", e),
    })?;

    Ok(())
}

/// Write timing info to a JSON file.
pub fn write_timing<P: AsRef<Path>>(timing: &TimingInfo, path: P) -> Result<(), FerroError> {
    let path = path.as_ref();

    // Create parent directory if needed
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(|e| FerroError::Io {
            msg: format!("Failed to create directory {}: {}", parent.display(), e),
        })?;
    }

    let file = File::create(path).map_err(|e| FerroError::Io {
        msg: format!("Failed to create {}: {}", path.display(), e),
    })?;

    let writer = BufWriter::new(file);
    serde_json::to_writer_pretty(writer, timing).map_err(|e| FerroError::Io {
        msg: format!("Failed to write JSON: {}", e),
    })?;

    Ok(())
}

/// Write results in text format (one result per line).
pub fn write_results_text<W: Write>(
    results: &BatchResults,
    mut writer: W,
) -> Result<(), FerroError> {
    for result in &results.results {
        if result.success {
            if let Some(ref output) = result.output {
                writeln!(writer, "{}", output).map_err(|e| FerroError::Io {
                    msg: format!("Failed to write output: {}", e),
                })?;
            }
        } else {
            let error = result.error.as_deref().unwrap_or("unknown error");
            writeln!(writer, "ERROR: {} - {}", result.input, error).map_err(|e| {
                FerroError::Io {
                    msg: format!("Failed to write output: {}", e),
                }
            })?;
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::TempDir;

    #[test]
    fn test_parse_batch_empty() {
        let dir = TempDir::new().unwrap();
        let input = dir.path().join("input.txt");
        File::create(&input).unwrap();

        let result = parse_batch(&input, false).unwrap();
        assert!(result.results.is_empty());
        assert_eq!(result.timing.total, 0);
    }

    #[test]
    fn test_parse_batch_valid() {
        let dir = TempDir::new().unwrap();
        let input = dir.path().join("input.txt");
        let mut f = File::create(&input).unwrap();
        writeln!(f, "NM_000088.3:c.100A>G").unwrap();
        writeln!(f, "NM_000088.3:c.200del").unwrap();

        let result = parse_batch(&input, false).unwrap();
        assert_eq!(result.results.len(), 2);
        assert_eq!(result.timing.total, 2);
        assert!(result.results[0].success);
        assert!(result.results[1].success);
    }

    #[test]
    fn test_parse_batch_invalid() {
        let dir = TempDir::new().unwrap();
        let input = dir.path().join("input.txt");
        let mut f = File::create(&input).unwrap();
        writeln!(f, "not a valid variant").unwrap();

        let result = parse_batch(&input, false).unwrap();
        assert_eq!(result.results.len(), 1);
        assert!(!result.results[0].success);
        assert!(result.results[0].error.is_some());
    }

    #[test]
    fn test_timing_info() {
        let timing = TimingInfo::new(100, 90, Duration::from_secs(2));
        assert_eq!(timing.total, 100);
        assert_eq!(timing.successful, 90);
        assert_eq!(timing.failed, 10);
        assert!((timing.elapsed_seconds - 2.0).abs() < 0.001);
        assert!((timing.variants_per_second - 50.0).abs() < 0.001);
    }
}