ferro-hgvs 0.2.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//! Mutalyzer API client for normalization benchmarks.

use crate::benchmark::types::ParseResult;
use crate::FerroError;
use reqwest::blocking::Client;
use serde::Deserialize;
use std::time::Duration;

/// HTTP client for the Mutalyzer API.
pub struct MutalyzerClient {
    client: Client,
    base_url: String,
}

/// Response from Mutalyzer normalize endpoint.
#[derive(Debug, Deserialize)]
pub struct NormalizeResponse {
    /// The normalized HGVS description
    pub normalized_description: Option<String>,

    /// Original input
    pub input_description: Option<String>,

    /// Errors if any
    #[serde(default)]
    pub errors: Vec<MutalyzerError>,

    /// Warnings if any
    #[serde(default)]
    pub warnings: Vec<MutalyzerWarning>,
}

#[derive(Debug, Deserialize)]
pub struct MutalyzerError {
    pub code: Option<String>,
    pub message: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct MutalyzerWarning {
    pub code: Option<String>,
    pub message: Option<String>,
}

impl MutalyzerClient {
    /// Create a new Mutalyzer client.
    pub fn new(base_url: &str) -> Result<Self, FerroError> {
        let client = Client::builder()
            .timeout(Duration::from_secs(30))
            .build()
            .map_err(|e| FerroError::Io {
                msg: format!("Failed to create HTTP client: {}", e),
            })?;

        Ok(Self {
            client,
            base_url: base_url.trim_end_matches('/').to_string(),
        })
    }

    /// Check if the Mutalyzer API is reachable.
    pub fn health_check(&self) -> Result<bool, FerroError> {
        let url = format!("{}/", self.base_url);
        match self.client.get(&url).send() {
            Ok(resp) => Ok(resp.status().is_success()),
            Err(_) => Ok(false),
        }
    }

    /// Wait for the Mutalyzer API to become available.
    pub fn wait_for_ready(&self, timeout: Duration) -> Result<(), FerroError> {
        let start = std::time::Instant::now();
        let check_interval = Duration::from_secs(5);

        while start.elapsed() < timeout {
            if self.health_check()? {
                eprintln!("Mutalyzer API is ready at {}", self.base_url);
                return Ok(());
            }
            eprintln!("Waiting for Mutalyzer API...");
            std::thread::sleep(check_interval);
        }

        Err(FerroError::Io {
            msg: format!(
                "Mutalyzer API not ready after {:?} at {}",
                timeout, self.base_url
            ),
        })
    }

    /// Normalize a single HGVS expression.
    pub fn normalize(&self, hgvs: &str) -> Result<ParseResult, FerroError> {
        let encoded = urlencoding::encode(hgvs);
        let url = format!("{}/normalize/{}", self.base_url, encoded);

        let response = self.client.get(&url).send().map_err(|e| FerroError::Io {
            msg: format!("HTTP request failed: {}", e),
        })?;

        if !response.status().is_success() {
            return Ok(ParseResult {
                input: hgvs.to_string(),
                success: false,
                output: None,
                error: Some(format!("HTTP {}", response.status())),
                error_category: Some("http_error".to_string()),
                ref_mismatch: None,
                details: None,
            });
        }

        let body: NormalizeResponse = response.json().map_err(|e| FerroError::Io {
            msg: format!("Failed to parse response: {}", e),
        })?;

        if !body.errors.is_empty() {
            let error_msg = body
                .errors
                .iter()
                .filter_map(|e| e.message.as_ref())
                .cloned()
                .collect::<Vec<_>>()
                .join("; ");
            return Ok(ParseResult {
                input: hgvs.to_string(),
                success: false,
                output: None,
                error: Some(error_msg),
                error_category: Some("mutalyzer_error".to_string()),
                ref_mismatch: None,
                details: None,
            });
        }

        match body.normalized_description {
            Some(normalized) => Ok(ParseResult {
                input: hgvs.to_string(),
                success: true,
                output: Some(normalized),
                error: None,
                error_category: None,
                ref_mismatch: None,
                details: None,
            }),
            None => Ok(ParseResult {
                input: hgvs.to_string(),
                success: false,
                output: None,
                error: Some("No normalized description returned".to_string()),
                error_category: Some("empty_response".to_string()),
                ref_mismatch: None,
                details: None,
            }),
        }
    }

    /// Normalize a batch of HGVS expressions with rate limiting.
    pub fn normalize_batch(
        &self,
        patterns: &[String],
        rate_limit_ms: Option<u64>,
    ) -> Vec<ParseResult> {
        let delay = rate_limit_ms.map(Duration::from_millis);

        patterns
            .iter()
            .map(|pattern| {
                let result = self.normalize(pattern).unwrap_or_else(|e| ParseResult {
                    input: pattern.clone(),
                    success: false,
                    output: None,
                    error: Some(format!("{}", e)),
                    error_category: Some("client_error".to_string()),
                    ref_mismatch: None,
                    details: None,
                });

                if let Some(d) = delay {
                    std::thread::sleep(d);
                }

                result
            })
            .collect()
    }
}

/// Run Mutalyzer parsing via Python subprocess.
///
/// This calls the mutalyzer-hgvs-parser Python package which is separate
/// from the web API.
pub fn run_mutalyzer_parser_subprocess(
    input_file: &str,
    output_file: &str,
) -> Result<(), FerroError> {
    use std::process::Command;

    // Python script to run - outputs unified ToolParseOutput format
    let python_code = r#"
import sys
import json
import time

try:
    from mutalyzer_hgvs_parser import to_model
except ImportError:
    print("ERROR: mutalyzer-hgvs-parser not installed", file=sys.stderr)
    sys.exit(1)

input_file = sys.argv[1]
output_file = sys.argv[2]

results = []
successful = 0

start = time.perf_counter()

with open(input_file, 'r') as f:
    patterns = [line.strip() for line in f if line.strip()]

for pattern in patterns:
    try:
        model = to_model(pattern)
        # Output the original pattern on success - mutalyzer-hgvs-parser doesn't have
        # a built-in serializer, so we return the input if it parses successfully
        results.append({
            "input": pattern,
            "success": True,
            "output": pattern,
            "error": None
        })
        successful += 1
    except Exception as e:
        results.append({
            "input": pattern,
            "success": False,
            "output": None,
            "error": str(e)[:200]
        })

elapsed = time.perf_counter() - start

# Unified output format matching ToolParseOutput
output = {
    "tool": "mutalyzer",
    "total_patterns": len(patterns),
    "successful": successful,
    "failed": len(patterns) - successful,
    "elapsed_seconds": elapsed,
    "throughput": len(patterns) / elapsed if elapsed > 0 else 0,
    "results": results
}

with open(output_file, 'w') as f:
    json.dump(output, f, indent=2)
"#;

    let output = Command::new("python3")
        .args(["-c", python_code, input_file, output_file])
        .output()
        .map_err(|e| FerroError::Io {
            msg: format!("Failed to run Python: {}", e),
        })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(FerroError::Io {
            msg: format!("Mutalyzer parser failed: {}", stderr),
        });
    }

    Ok(())
}

/// Check if the mutalyzer-hgvs-parser Python package is available.
pub fn has_mutalyzer_parser() -> bool {
    use std::process::Command;

    Command::new("python3")
        .args(["-c", "from mutalyzer_hgvs_parser import to_model"])
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

/// Check if the mutalyzer package (for local normalization) is available.
pub fn has_mutalyzer_normalizer() -> bool {
    use std::process::Command;

    Command::new("python3")
        .args(["-c", "from mutalyzer.description import Description"])
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

/// Run local mutalyzer normalization via Python subprocess.
///
/// This calls the mutalyzer package directly for local normalization,
/// avoiding HTTP API calls for fair performance comparison.
pub fn run_mutalyzer_normalizer_subprocess(
    input_file: &str,
    output_file: &str,
    settings_file: Option<&str>,
) -> Result<(), FerroError> {
    use std::process::Command;

    // Python script for local normalization
    let python_code = r#"
import sys
import json
import time
import os

# Network call tracking - must be set up before any imports that use urllib
network_calls = []
_original_urlopen = None

def _tracking_urlopen(url, *args, **kwargs):
    """Track network calls made by urllib."""
    url_str = url if isinstance(url, str) else url.full_url if hasattr(url, 'full_url') else str(url)
    start = time.perf_counter()
    try:
        result = _original_urlopen(url, *args, **kwargs)
        elapsed = time.perf_counter() - start
        network_calls.append({"url": url_str[:200], "elapsed_seconds": elapsed, "success": True})
        return result
    except Exception as e:
        elapsed = time.perf_counter() - start
        network_calls.append({"url": url_str[:200], "elapsed_seconds": elapsed, "success": False, "error": str(e)[:100]})
        raise

# Install network tracking
import urllib.request
_original_urlopen = urllib.request.urlopen
urllib.request.urlopen = _tracking_urlopen

# Set up mutalyzer settings if provided
# We must configure the cache BEFORE importing mutalyzer, because
# mutalyzer_retriever.configuration loads settings at import time.
cache_dir = None
cache_add = False
if len(sys.argv) > 3 and sys.argv[3]:
    settings_file = sys.argv[3]
    try:
        with open(settings_file, 'r') as f:
            for line in f:
                line = line.strip()
                if line.startswith('MUTALYZER_CACHE_DIR'):
                    parts = line.split('=', 1)
                    if len(parts) == 2:
                        cache_dir = parts[1].strip()
                elif line.startswith('MUTALYZER_FILE_CACHE_ADD'):
                    parts = line.split('=', 1)
                    if len(parts) == 2:
                        cache_add = parts[1].strip().lower() in ('true', '1', 'yes')
    except Exception as e:
        print(f"WARNING: Failed to parse settings file: {e}", file=sys.stderr)

# Import mutalyzer_retriever first and configure the cache directory
# directly in its settings dict (env vars don't work after import)
from mutalyzer_retriever import configuration
if cache_dir:
    configuration.settings['MUTALYZER_CACHE_DIR'] = cache_dir
if cache_add:
    configuration.settings['MUTALYZER_FILE_CACHE_ADD'] = True

try:
    from mutalyzer.description import Description
except ImportError:
    print("ERROR: mutalyzer not installed", file=sys.stderr)
    print("Install with: pip install mutalyzer", file=sys.stderr)
    sys.exit(1)

input_file = sys.argv[1]
output_file = sys.argv[2]

results = []
successful = 0
failed = 0

with open(input_file, 'r') as f:
    patterns = [line.strip() for line in f if line.strip()]

start = time.perf_counter()

for pattern in patterns:
    try:
        d = Description(description=pattern)
        d.normalize()
        if not d.errors:
            output = d.output()
            results.append({
                "input": pattern,
                "success": True,
                "output": output.get("normalized_description", pattern)
            })
            successful += 1
        else:
            error_msg = str(d.errors[0]) if d.errors else "Unknown error"
            results.append({
                "input": pattern,
                "success": False,
                "error": error_msg[:200]
            })
            failed += 1
    except Exception as e:
        results.append({
            "input": pattern,
            "success": False,
            "error": str(e)[:200]
        })
        failed += 1

elapsed = time.perf_counter() - start

output_data = {
    "tool": "mutalyzer",
    "total_patterns": len(patterns),
    "successful": successful,
    "failed": failed,
    "elapsed_seconds": elapsed,
    "patterns_per_second": len(patterns) / elapsed if elapsed > 0 else 0,
    "network_calls": len(network_calls),
    "network_stats": network_calls[:50] if network_calls else [],  # First 50 for debugging
    "results": results
}

with open(output_file, 'w') as f:
    json.dump(output_data, f, indent=2)

# Report network statistics
if network_calls:
    total_network_time = sum(c.get('elapsed_seconds', 0) for c in network_calls)
    print(f"\n=== Network Statistics ===", file=sys.stderr)
    print(f"Total network calls: {len(network_calls)}", file=sys.stderr)
    print(f"Network calls per pattern: {len(network_calls) / len(patterns):.2f}", file=sys.stderr)
    print(f"Total network time: {total_network_time:.2f}s", file=sys.stderr)
    print(f"Average time per call: {total_network_time / len(network_calls):.2f}s", file=sys.stderr)
    # Show unique URLs called
    unique_urls = set()
    for c in network_calls:
        url = c.get('url', '')
        # Extract domain/path pattern
        if 'ncbi.nlm.nih.gov' in url:
            unique_urls.add('NCBI')
        elif 'ebi.ac.uk' in url:
            unique_urls.add('EBI')
        else:
            unique_urls.add(url[:60])
    print(f"Endpoints hit: {', '.join(sorted(unique_urls))}", file=sys.stderr)
else:
    print(f"\n=== Network Statistics ===", file=sys.stderr)
    print(f"Total network calls: 0 (all cached)", file=sys.stderr)
"#;

    let mut cmd = Command::new("python3");
    cmd.args(["-c", python_code, input_file, output_file]);

    if let Some(settings) = settings_file {
        cmd.arg(settings);
    } else {
        cmd.arg("");
    }

    let output = cmd.output().map_err(|e| FerroError::Io {
        msg: format!("Failed to run Python: {}", e),
    })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(FerroError::Io {
            msg: format!("Mutalyzer normalizer failed: {}", stderr),
        });
    }

    Ok(())
}

/// Normalize a single HGVS variant using local mutalyzer subprocess.
///
/// This is a convenience function for normalizing a single variant,
/// following the pattern used by biocommons::normalize_single().
pub fn normalize_single(
    hgvs: &str,
    settings_file: Option<&str>,
    allow_network: bool,
) -> Result<ParseResult, FerroError> {
    use std::io::Write;
    use tempfile::NamedTempFile;

    // Create temp files for input and output
    let mut input_file = NamedTempFile::new().map_err(|e| FerroError::Io {
        msg: format!("Failed to create temp file: {}", e),
    })?;
    let output_file = NamedTempFile::new().map_err(|e| FerroError::Io {
        msg: format!("Failed to create temp file: {}", e),
    })?;

    // Write input pattern
    writeln!(input_file, "{}", hgvs).map_err(|e| FerroError::Io {
        msg: format!("Failed to write to temp file: {}", e),
    })?;

    // Run normalization
    if allow_network {
        run_mutalyzer_normalizer_subprocess(
            input_file.path().to_str().unwrap(),
            output_file.path().to_str().unwrap(),
            settings_file,
        )?;
    } else {
        run_mutalyzer_normalizer_subprocess_offline(
            input_file.path().to_str().unwrap(),
            output_file.path().to_str().unwrap(),
            settings_file,
        )?;
    }

    // Read output
    let output_content =
        std::fs::read_to_string(output_file.path()).map_err(|e| FerroError::Io {
            msg: format!("Failed to read output file: {}", e),
        })?;

    // Parse JSON output
    let output_data: serde_json::Value =
        serde_json::from_str(&output_content).map_err(|e| FerroError::Io {
            msg: format!("Failed to parse JSON output: {}", e),
        })?;

    // Extract result
    let results = output_data["results"]
        .as_array()
        .ok_or_else(|| FerroError::Io {
            msg: "No results in output".to_string(),
        })?;

    if let Some(result) = results.first() {
        let success = result["success"].as_bool().unwrap_or(false);
        if success {
            Ok(ParseResult {
                input: hgvs.to_string(),
                success: true,
                output: result["output"].as_str().map(|s| s.to_string()),
                error: None,
                error_category: None,
                ref_mismatch: None,
                details: None,
            })
        } else {
            Ok(ParseResult {
                input: hgvs.to_string(),
                success: false,
                output: None,
                error: result["error"].as_str().map(|s| s.to_string()),
                error_category: Some("mutalyzer_error".to_string()),
                ref_mismatch: None,
                details: None,
            })
        }
    } else {
        Err(FerroError::Io {
            msg: "No results returned from mutalyzer".to_string(),
        })
    }
}

/// Run local mutalyzer normalization via Python subprocess (offline mode).
///
/// This version disables network access to force cache-only operation.
pub fn run_mutalyzer_normalizer_subprocess_offline(
    input_file: &str,
    output_file: &str,
    settings_file: Option<&str>,
) -> Result<(), FerroError> {
    use std::process::Command;

    // Python script for local normalization with network disabled
    let python_code = r#"
import sys
import json
import time
import os

# Block network access
def _blocked_urlopen(*args, **kwargs):
    raise ConnectionError("Network access disabled in offline mode")

import urllib.request
urllib.request.urlopen = _blocked_urlopen

# Set up mutalyzer settings if provided
cache_dir = None
cache_add = False
if len(sys.argv) > 3 and sys.argv[3]:
    settings_file = sys.argv[3]
    try:
        with open(settings_file, 'r') as f:
            for line in f:
                line = line.strip()
                if line.startswith('MUTALYZER_CACHE_DIR'):
                    parts = line.split('=', 1)
                    if len(parts) == 2:
                        cache_dir = parts[1].strip()
                elif line.startswith('MUTALYZER_FILE_CACHE_ADD'):
                    parts = line.split('=', 1)
                    if len(parts) == 2:
                        cache_add = parts[1].strip().lower() in ('true', '1', 'yes')
    except Exception as e:
        print(f"WARNING: Failed to parse settings file: {e}", file=sys.stderr)

# Import mutalyzer_retriever first and configure the cache directory
from mutalyzer_retriever import configuration
if cache_dir:
    configuration.settings['MUTALYZER_CACHE_DIR'] = cache_dir
if cache_add:
    configuration.settings['MUTALYZER_FILE_CACHE_ADD'] = True

try:
    from mutalyzer.description import Description
except ImportError:
    print("ERROR: mutalyzer not installed", file=sys.stderr)
    print("Install with: pip install mutalyzer", file=sys.stderr)
    sys.exit(1)

input_file = sys.argv[1]
output_file = sys.argv[2]

results = []
successful = 0
failed = 0

with open(input_file, 'r') as f:
    patterns = [line.strip() for line in f if line.strip()]

start = time.perf_counter()

for pattern in patterns:
    try:
        d = Description(description=pattern)
        d.normalize()
        if not d.errors:
            output = d.output()
            results.append({
                "input": pattern,
                "success": True,
                "output": output.get("normalized_description", pattern)
            })
            successful += 1
        else:
            error_msg = str(d.errors[0]) if d.errors else "Unknown error"
            results.append({
                "input": pattern,
                "success": False,
                "error": error_msg[:200]
            })
            failed += 1
    except ConnectionError as e:
        results.append({
            "input": pattern,
            "success": False,
            "error": f"Network access required but disabled: {str(e)[:150]}"
        })
        failed += 1
    except Exception as e:
        results.append({
            "input": pattern,
            "success": False,
            "error": str(e)[:200]
        })
        failed += 1

elapsed = time.perf_counter() - start

output_data = {
    "tool": "mutalyzer",
    "total_patterns": len(patterns),
    "successful": successful,
    "failed": failed,
    "elapsed_seconds": elapsed,
    "patterns_per_second": len(patterns) / elapsed if elapsed > 0 else 0,
    "network_calls": 0,
    "results": results
}

with open(output_file, 'w') as f:
    json.dump(output_data, f, indent=2)
"#;

    let mut cmd = Command::new("python3");
    cmd.args(["-c", python_code, input_file, output_file]);

    if let Some(settings) = settings_file {
        cmd.arg(settings);
    } else {
        cmd.arg("");
    }

    let output = cmd.output().map_err(|e| FerroError::Io {
        msg: format!("Failed to run Python: {}", e),
    })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(FerroError::Io {
            msg: format!("Mutalyzer normalizer (offline) failed: {}", stderr),
        });
    }

    Ok(())
}

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

    #[test]
    fn test_check_mutalyzer_available() {
        // This test just verifies the function doesn't panic
        let _ = has_mutalyzer_parser();
    }
}