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
//! HGVS-RS tool service implementation
use crate::benchmark::types::ParseResult;
#[cfg(feature = "hgvs-rs")]
use std::sync::Arc;
#[cfg(feature = "hgvs-rs")]
use crate::benchmark::hgvs_rs::{
HgvsRsConfig as BenchmarkHgvsRsConfig, HgvsRsNormalizer, HgvsRsResult,
};
use crate::service::{
config::HgvsRsConfig,
tools::HgvsToolService,
types::{health_check::HealthCheckResult, ServiceError, ToolName},
};
/// HGVS-RS tool service
pub struct HgvsRsService {
/// Configuration (stored for future use)
_config: HgvsRsConfig,
/// HGVS-RS normalizer (only available with hgvs-rs feature)
#[cfg(feature = "hgvs-rs")]
normalizer: Arc<HgvsRsNormalizer>,
}
impl HgvsRsService {
/// Create a new HgvsRsService
pub fn new(config: &HgvsRsConfig) -> Result<Self, ServiceError> {
#[cfg(feature = "hgvs-rs")]
{
// Convert service config to benchmark config
let benchmark_config = BenchmarkHgvsRsConfig {
uta_db_url: config.uta_url.clone(),
uta_db_schema: config.uta_schema.clone(),
seqrepo_path: config.seqrepo_path.to_string_lossy().to_string(),
lrg_mapping_file: config
.lrg_mapping_file
.as_ref()
.map(|p| p.to_string_lossy().to_string()),
in_memory: false,
};
// Create the HGVS-RS normalizer
let normalizer = HgvsRsNormalizer::new(&benchmark_config).map_err(|e| {
ServiceError::ConfigError(format!("Failed to initialize HGVS-RS: {}", e))
})?;
Ok(Self {
_config: config.clone(),
normalizer: Arc::new(normalizer),
})
}
#[cfg(not(feature = "hgvs-rs"))]
{
let _ = config; // Suppress unused variable warning
Err(ServiceError::ConfigError(
"HGVS-RS integration requires the 'hgvs-rs' feature to be enabled".to_string(),
))
}
}
/// Run HGVS-RS normalization on a single variant
#[cfg(feature = "hgvs-rs")]
async fn run_hgvs_rs(
&self,
hgvs: &str,
_is_normalize: bool,
) -> Result<ParseResult, ServiceError> {
let hgvs = hgvs.to_string();
let normalizer = Arc::clone(&self.normalizer);
// Run HGVS-RS in a blocking task
let hgvs_result = tokio::task::spawn_blocking(move || normalizer.normalize(&hgvs))
.await
.map_err(|e| ServiceError::InternalError(format!("Task join error: {}", e)))?;
// Convert HgvsRsResult to ParseResult
Ok(convert_hgvs_rs_result_to_parse_result(hgvs_result))
}
/// Run HGVS-RS normalization on a single variant (fallback when feature disabled)
#[cfg(not(feature = "hgvs-rs"))]
async fn run_hgvs_rs(
&self,
hgvs: &str,
_is_normalize: bool,
) -> Result<ParseResult, ServiceError> {
Ok(ParseResult {
input: hgvs.to_string(),
success: false,
output: None,
error: Some("HGVS-RS feature not enabled".to_string()),
error_category: Some("feature_disabled".to_string()),
ref_mismatch: None,
details: None,
})
}
}
#[async_trait::async_trait]
impl HgvsToolService for HgvsRsService {
async fn parse(&self, hgvs: &str) -> Result<ParseResult, ServiceError> {
// HGVS-RS doesn't separate parsing from normalization like ferro does
// The normalize method includes parsing validation
self.run_hgvs_rs(hgvs, false).await
}
async fn normalize(&self, hgvs: &str) -> Result<ParseResult, ServiceError> {
self.run_hgvs_rs(hgvs, true).await
}
async fn health_check(&self) -> HealthCheckResult {
#[cfg(feature = "hgvs-rs")]
{
// Test with a variant known to be in the reference data
let test_variant = "NM_000088.4:c.589G>T";
match self.run_hgvs_rs(test_variant, false).await {
Ok(result) => {
if result.success {
HealthCheckResult::Healthy
} else if let Some(error) = &result.error {
// These are "expected" errors that indicate the service is working
if error.contains("not found")
|| error.contains("transcript")
|| error.contains("validation")
{
HealthCheckResult::Degraded {
reason: "Tool working but reference data may be incomplete"
.to_string(),
}
} else {
HealthCheckResult::Unhealthy {
reason: format!("HGVS-RS health check failed: {}", error),
}
}
} else {
HealthCheckResult::Unhealthy {
reason: "Unknown error".to_string(),
}
}
}
Err(e) => HealthCheckResult::Unhealthy {
reason: format!("HGVS-RS health check failed: {}", e),
},
}
}
#[cfg(not(feature = "hgvs-rs"))]
{
HealthCheckResult::Unhealthy {
reason: "HGVS-RS feature not enabled".to_string(),
}
}
}
fn tool_name(&self) -> ToolName {
ToolName::HgvsRs
}
}
/// Convert HgvsRsResult to ParseResult
#[cfg(feature = "hgvs-rs")]
fn convert_hgvs_rs_result_to_parse_result(result: HgvsRsResult) -> ParseResult {
ParseResult {
input: result.input,
success: result.success,
output: result.output,
error: result.error,
error_category: None, // HgvsRsResult doesn't have error categorization
ref_mismatch: None, // HgvsRsResult doesn't track reference mismatches
details: None, // Details not available from hgvs-rs
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_hgvs_rs_service_creation() {
// An empty `seqrepo_path` makes provider construction fail deterministically
// and *independently of any live UTA database*: with the `hgvs-rs` feature
// enabled, `HgvsRsService::new` builds a real UTA/SeqRepo provider, and the
// empty path aborts provider construction during path parsing — before any
// database connection is attempted (an empty `Path` has no `parent()`). So
// the assertions below hold whether or not a UTA server happens to be
// reachable from the test host. Without the feature, the value is ignored and
// the constructor returns its static feature-not-enabled error.
let config = HgvsRsConfig {
enabled: true,
uta_url: "postgresql://anonymous:anonymous@localhost:5432/uta/uta_20210129b"
.to_string(),
uta_schema: "uta_20210129b".to_string(),
seqrepo_path: PathBuf::new(),
lrg_mapping_file: None,
parallel_workers: Some(1),
};
let result = HgvsRsService::new(&config);
// In every feature configuration, construction with this config must fail with
// a `ConfigError` and must never panic. The error-variant assertion is
// unconditional: an `Ok`, or a differently-typed `ServiceError`, now fails the
// test instead of passing silently (the previous `if let` swallowed both).
match result {
Err(ServiceError::ConfigError(msg)) => {
// Without the feature the message is our own static string, which names
// the missing feature; assert on it. With the feature the message comes
// from the provider layer and is not contractually fixed, so only the
// variant is asserted there.
#[cfg(not(feature = "hgvs-rs"))]
assert!(
msg.contains("hgvs-rs"),
"config error should name the required feature, got: {msg}"
);
#[cfg(feature = "hgvs-rs")]
let _ = msg;
}
Err(other) => panic!("expected ServiceError::ConfigError, got {other:?}"),
Ok(_) => {
panic!("HgvsRsService::new unexpectedly succeeded with an empty seqrepo path")
}
}
}
#[tokio::test]
async fn test_hgvs_rs_tool_name() {
let _config = HgvsRsConfig {
enabled: true,
uta_url: "postgresql://test@localhost:5432/uta/uta_20210129b".to_string(),
uta_schema: "uta_20210129b".to_string(),
seqrepo_path: PathBuf::from("/tmp"),
lrg_mapping_file: None,
parallel_workers: Some(1),
};
// Even if creation fails, we can test the tool name if we had a service
// For now, just test that the tool name is correct by checking the constant
assert_eq!("hgvs-rs", "hgvs-rs"); // This ensures the tool name is consistent
}
}