mehari 0.42.0

Variant effect prediction all in Rust
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
use crate::annotate::cli::{PredictorSettings, Sources};
use crate::annotate::seqvars::csq::ConfigBuilder;
use crate::annotate::seqvars::{
    ConsequenceAnnotator, initialize_clinvar_annotators_for_assembly,
    initialize_frequency_annotators_for_assembly, load_transcript_dbs_for_assembly,
};
use crate::annotate::{
    seqvars::csq::ConsequencePredictor as SeqvarConsequencePredictor,
    strucvars::csq::ConsequencePredictor as StrucvarConsequencePredictor,
};
use crate::common::contig::ContigManager;
use crate::db::merge::merge_transcript_databases;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use strum::EnumString;

/// Implementation of Actix server.
pub mod actix_server;

/// Module with OpenAPI documentation.
pub mod openapi {
    use crate::annotate::seqvars::ann::{
        Consequence, FeatureBiotype, FeatureType, Message, Pos, PutativeImpact, Rank, SoFeature,
    };
    use crate::annotate::strucvars::csq::interface::StrucvarsSvType;
    use crate::annotate::strucvars::csq::{
        StrucvarsGeneTranscriptEffects, StrucvarsTranscriptEffect,
    };
    use crate::server::run::actix_server::gene_txs::{
        ExonAlignment, GenesTranscriptsListQuery, GenesTranscriptsListResponse, GenomeAlignment,
        Strand, Transcript, TranscriptBiotype, TranscriptTag,
    };
    use crate::server::run::actix_server::seqvars_clinvar::{
        ClinvarQuery, ClinvarResponse, ClinvarResultEntry,
    };
    use crate::server::run::actix_server::seqvars_csq::{
        SeqvarsCsqQuery, SeqvarsCsqResponse, SeqvarsCsqResultEntry,
    };
    use crate::server::run::actix_server::seqvars_frequencies::{
        AutosomalResultEntry, FrequencyQuery, FrequencyResponse, FrequencyResultEntry,
        GonosomalResultEntry, MitochondrialResultEntry,
    };
    use crate::server::run::actix_server::strucvars_csq::{
        StrucvarsCsqQuery, StrucvarsCsqResponse,
    };
    use crate::server::run::actix_server::versions::{
        Assembly, DataVersionEntry, SoftwareVersions, VersionsInfoResponse,
    };

    use super::actix_server::{
        CustomError, gene_txs, seqvars_clinvar, seqvars_csq, seqvars_frequencies, strucvars_csq,
        versions,
    };

    /// Utoipa-based `OpenAPI` generation helper.
    #[derive(utoipa::OpenApi)]
    #[openapi(
        paths(
            versions::handle,
            gene_txs::handle_with_openapi,
            seqvars_csq::handle_with_openapi,
            strucvars_csq::handle_with_openapi,
            seqvars_frequencies::handle_with_openapi,
            seqvars_clinvar::handle_with_openapi,
        ),
        components(schemas(
            Assembly,
            CustomError,
            VersionsInfoResponse,
            SoftwareVersions,
            DataVersionEntry,
            StrucvarsCsqResponse,
            StrucvarsCsqQuery,
            StrucvarsGeneTranscriptEffects,
            StrucvarsSvType,
            StrucvarsTranscriptEffect,
            SeqvarsCsqQuery,
            SeqvarsCsqResponse,
            SeqvarsCsqResultEntry,
            Consequence,
            PutativeImpact,
            FeatureType,
            FeatureBiotype,
            Rank,
            Pos,
            Message,
            SoFeature,
            ExonAlignment,
            GenesTranscriptsListQuery,
            GenesTranscriptsListResponse,
            GenomeAlignment,
            Strand,
            Transcript,
            TranscriptBiotype,
            TranscriptTag,
            FrequencyQuery,
            FrequencyResponse,
            FrequencyResultEntry,
            AutosomalResultEntry,
            GonosomalResultEntry,
            MitochondrialResultEntry,
            ClinvarQuery,
            ClinvarResponse,
            ClinvarResultEntry,
        ))
    )]
    pub struct ApiDoc;
}

#[derive(Debug, Clone)]
pub struct AssemblyReference {
    pub assembly: String,
    pub path: PathBuf,
}

impl FromStr for AssemblyReference {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.split_once('=') {
            Some((assembly, path)) if !assembly.is_empty() && !path.is_empty() => {
                Ok(AssemblyReference {
                    assembly: assembly.to_lowercase(),
                    path: PathBuf::from(path),
                })
            }
            _ => Err(format!(
                "Invalid reference format `{}`. Expected `ASSEMBLY=PATH`",
                s
            )),
        }
    }
}

/// Command line arguments for "server run` command.
#[derive(clap::Parser, Debug)]
#[command(about = "Run Mehari REST API server", long_about = None)]
pub struct Args {
    /// Path to the reference genome(s), with accompanying index.
    ///
    /// Provide one reference per assembly you want to serve (e.g., GRCh37 and/or GRCh38).
    /// Example: --reference grch37=/path/to/grch37.fa
    #[arg(long, value_name = "ASSEMBLY=PATH")]
    pub reference: Vec<AssemblyReference>,

    /// Read the reference genome into memory.
    #[arg(long, requires = "reference")]
    pub in_memory_reference: bool,

    /// What to annotate and which source to use.
    #[command(flatten)]
    pub sources: Sources,

    /// Transcript related settings.
    #[command(flatten)]
    pub predictor_settings: PredictorSettings,

    /// Whether to suppress printing hints.
    #[arg(long, default_value_t = false)]
    pub suppress_hints: bool,

    /// IP to listen on.
    #[arg(long, default_value = "127.0.0.1")]
    pub listen_host: String,

    /// Port to listen on.
    #[arg(long, default_value_t = 8080)]
    pub listen_port: u16,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EnumString)]
enum Endpoint {
    Transcripts,
    Frequency,
    Clinvar,
}

/// Print some hints via `tracing::info!`.
fn print_hints(args: &Args, enabled_sources: &[(String, Endpoint)]) {
    tracing::info!(
        "Launching server main on http://{}:{} ...",
        args.listen_host.as_str(),
        args.listen_port
    );

    // Short-circuit if no hints are to be
    if args.suppress_hints {
        return;
    }

    use Endpoint::*;

    let prefix = format!(
        "try: http://{host}:{port}/api/v1/",
        host = args.listen_host,
        port = args.listen_port
    );
    let examples: HashMap<(String, Endpoint), Vec<&str>> = HashMap::from([
        (
            ("grch37".into(), Transcripts),
            vec![
                r#"genes/transcripts?hgnc_id=HGNC:1100&genome_build=grch37"#,
                r#"seqvars/csq?assembly=grch37&chromosome=17&position=48275363&reference=C&alternative=A"#,
                r#"seqvars/csq?assembly=grch37&chromosome=17&position=48275363&reference=C&alternative=A&hgnc_id=HGNC:2197"#,
                r#"strucvars/csq?assembly=grch37&chromosome=17&start=48275360&&stop=48275370&sv_type=DEL""#,
            ],
        ),
        (
            ("grch37".into(), Frequency),
            vec![
                r#"seqvars/frequency?assembly=grch37&chromosome=17&position=48275363&reference=C&alternative=A"#,
            ],
        ),
        (
            ("grch37".into(), Clinvar),
            vec![
                r#"seqvars/clinvar?assembly=grch37&chromosome=17&position=48275363&reference=C&alternative=A"#,
            ],
        ),
        (
            ("grch38".into(), Transcripts),
            vec![
                r#"genes/transcripts?hgnc_id=HGNC:1100&genome_build=grch38"#,
                r#"seqvars/csq?assembly=grch38&chromosome=2&position=26364839&reference=C&alternative=T"#,
            ],
        ),
        (
            ("grch38".into(), Frequency),
            vec![
                r#"seqvars/frequency?assembly=grch38&chromosome=2&position=26364839&reference=C&alternative=T"#,
            ],
        ),
        (
            ("grch38".into(), Clinvar),
            vec![
                r#"seqvars/clinvar?assembly=grch38&chromosome=2&position=26364839&reference=C&alternative=T"#,
            ],
        ),
    ]);
    for (genome_release, endpoint) in enabled_sources {
        if let Some(examples) = examples.get(&(genome_release.clone(), *endpoint)) {
            for example in examples {
                tracing::info!("{}{}", prefix, example);
            }
        }
    }
}

/// Main entry point for `server run` sub command.
///
/// # Errors
///
/// In the case that there is an error running the server.
pub async fn run(args_common: &crate::common::Args, args: &Args) -> Result<(), anyhow::Error> {
    tracing::info!("args_common = {:?}", &args_common);
    tracing::info!("args = {:?}", &args);

    if let Some(log::Level::Trace | log::Level::Debug) = args_common.verbose.log_level() {
        // SAFETY: This environment variable is set during server initialization,
        // before any worker threads are spawned. At this point, only the main thread
        // is running, making this operation thread-safe.
        unsafe { std::env::set_var("RUST_LOG", "debug") };
        env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
    }

    // Load data that we need for running the server.
    tracing::info!("Loading data...");
    let before_loading = std::time::Instant::now();
    let mut data = actix_server::WebServerData::default();

    let reference_paths: HashMap<String, PathBuf> = args
        .reference
        .iter()
        .map(|r| (r.assembly.clone(), r.path.clone()))
        .collect();

    let associate_db_path = |path: &str| -> Option<String> {
        let path_str = path.to_lowercase();
        if path_str.contains("grch37") {
            Some("grch37".into())
        } else if path_str.contains("grch38") {
            Some("grch38".into())
        } else {
            None
        }
    };

    let mut transcript_paths: HashMap<String, Vec<String>> = HashMap::new();
    if let Some(paths) = args.sources.transcripts.as_ref() {
        for path in paths {
            if let Some(release) = associate_db_path(path) {
                transcript_paths
                    .entry(release)
                    .or_default()
                    .push(path.clone());
            } else {
                tracing::warn!(
                    "Could not determine assembly for transcript db: {}. Skipping.",
                    path
                );
            }
        }
    }

    let mut frequency_paths = HashMap::new();
    if let Some(paths) = args.sources.frequencies.as_ref() {
        for path in paths {
            if let Some(assembly) = associate_db_path(path) {
                if let Some(prev) = frequency_paths.insert(assembly.clone(), path.clone()) {
                    tracing::warn!(
                        "Duplicate frequency DB for {:?}: {} overwritten by {}",
                        assembly,
                        prev,
                        path
                    );
                }
            } else {
                tracing::warn!(
                    "Could not determine assembly for frequency db: {}. Skipping.",
                    path
                );
            }
        }
    }

    let mut clinvar_paths = HashMap::new();
    if let Some(paths) = args.sources.clinvar.as_ref() {
        for path in paths {
            if let Some(assembly) = associate_db_path(path) {
                if let Some(prev) = clinvar_paths.insert(assembly.clone(), path.clone()) {
                    tracing::warn!(
                        "Duplicate ClinVar DB for {:?}: {} overwritten by {}",
                        assembly,
                        prev,
                        path
                    );
                }
            } else {
                tracing::warn!(
                    "Could not determine assembly for clinvar db: {}. Skipping.",
                    path
                );
            }
        }
    }

    let mut all_releases: HashSet<String> = HashSet::new();
    all_releases.extend(transcript_paths.keys().cloned());
    all_releases.extend(frequency_paths.keys().cloned());
    all_releases.extend(clinvar_paths.keys().cloned());

    let mut enabled_sources = vec![];
    for assembly in all_releases.iter() {
        let contig_manager = Arc::new(ContigManager::new(assembly));
        let reference_path = reference_paths.get(&assembly.clone());

        tracing::info!("Loading data for assembly {:?}...", &assembly);

        // Load transcript data if available for this release.
        // At the moment, this is the only type of data that can benefit from a reference FASTA.
        if let Some(tx_db_paths) = transcript_paths.get(&assembly.clone()) {
            if reference_path.is_none() {
                tracing::warn!(
                    "No reference FASTA provided for {}. Some features like HGVSg normalization will be unavailable.",
                    &assembly
                );
            }

            tracing::info!("Building seqvars predictors for {}...", &assembly);
            let tx_dbs = load_transcript_dbs_for_assembly(tx_db_paths, assembly)?;
            if tx_dbs.is_empty() {
                tracing::warn!(
                    "No transcript databases loaded for {}, respective endpoints will be unavailable.",
                    &assembly
                );
            } else {
                let tx_db = merge_transcript_databases(tx_dbs)?;
                let annotator = ConsequenceAnnotator::from_db_and_settings(
                    tx_db,
                    reference_path.cloned(),
                    args.in_memory_reference,
                    &args.predictor_settings,
                )?;
                let config = ConfigBuilder::default()
                    .report_most_severe_consequence_by(
                        args.predictor_settings
                            .transcript_settings
                            .report_most_severe_consequence_by,
                    )
                    .build()?;

                let provider = annotator.predictor.provider.clone();
                data.provider.insert(assembly.clone(), provider.clone());
                data.seqvars_predictors.insert(
                    assembly.clone(),
                    SeqvarConsequencePredictor::new(provider.clone(), config),
                );
                tracing::info!("Finished building seqvars predictors.");

                tracing::info!("Building strucvars predictors...");
                data.strucvars_predictors.insert(
                    assembly.clone(),
                    StrucvarConsequencePredictor::new(provider.clone()),
                );
                enabled_sources.push((assembly.clone(), Endpoint::Transcripts));
                tracing::info!(
                    "Finished building seqvars predictors for {:?}.",
                    assembly.clone()
                );
            }
        }

        if let Some(freq_path) = frequency_paths.get(&assembly.clone()) {
            tracing::info!("Loading frequency data for {:?}...", assembly);
            let annotators = initialize_frequency_annotators_for_assembly(
                std::slice::from_ref(freq_path),
                assembly,
                contig_manager.clone(),
            )?;
            if let Some(frequency_db) = annotators.into_iter().next() {
                data.frequency_annotators
                    .insert(assembly.clone(), frequency_db);
                enabled_sources.push((assembly.clone(), Endpoint::Frequency));
                tracing::info!(
                    "... done loading frequency data for {:?}.",
                    assembly.clone()
                );
            } else {
                tracing::warn!("Could not load frequency data for {:?}.", assembly.clone());
            }
        }

        if let Some(clinvar_path) = clinvar_paths.get(&assembly.clone()) {
            tracing::info!("Loading ClinVar data for {:?}...", assembly.clone());
            let annotators = initialize_clinvar_annotators_for_assembly(
                std::slice::from_ref(clinvar_path),
                assembly,
                contig_manager.clone(),
            )?;
            if let Some(annotator) = annotators.into_iter().next() {
                data.clinvar_annotators.insert(assembly.clone(), annotator);
                enabled_sources.push((assembly.clone(), Endpoint::Clinvar));
                tracing::info!("... done loading ClinVar data for {:?}.", assembly.clone());
            } else {
                tracing::warn!("Could not load ClinVar data for {:?}.", assembly.clone());
            }
        }
    }
    let data = actix_web::web::Data::new(data);
    tracing::info!("... done loading data {:?}", before_loading.elapsed());

    if enabled_sources.is_empty() {
        tracing::error!("No data sources loaded. Exiting.");
        return Err(anyhow::anyhow!("No data sources loaded."));
    }

    tracing::info!("Loaded the following sources: {:?}", &enabled_sources);

    // Print the server URL and some hints (the latter: unless suppressed).
    print_hints(args, &enabled_sources);
    // Launch the Actix web server.
    actix_server::main(args, data).await?;

    tracing::info!("All done. Have a nice day!");
    Ok(())
}