Skip to content

Commit 40796ab

Browse files
committed
do not initialize predictors/annotators multiple times
1 parent 551d6cf commit 40796ab

File tree

3 files changed

+67
-78
lines changed

3 files changed

+67
-78
lines changed

src/annotate/seqvars/mod.rs

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,35 +1331,6 @@ pub(crate) struct Annotator {
13311331
annotators: Vec<AnnotatorEnum>,
13321332
}
13331333

1334-
impl Annotator {
1335-
pub(crate) fn consequence(&self) -> Option<&ConsequenceAnnotator> {
1336-
for annotator in &self.annotators {
1337-
if let AnnotatorEnum::Consequence(a) = annotator {
1338-
return Some(a);
1339-
}
1340-
}
1341-
None
1342-
}
1343-
1344-
pub(crate) fn frequencies(&self) -> Option<&FrequencyAnnotator> {
1345-
for annotator in &self.annotators {
1346-
if let AnnotatorEnum::Frequency(a) = annotator {
1347-
return Some(a);
1348-
}
1349-
}
1350-
None
1351-
}
1352-
1353-
pub(crate) fn clinvar(&self) -> Option<&ClinvarAnnotator> {
1354-
for annotator in &self.annotators {
1355-
if let AnnotatorEnum::Clinvar(a) = annotator {
1356-
return Some(a);
1357-
}
1358-
}
1359-
None
1360-
}
1361-
}
1362-
13631334
#[derive(Debug)]
13641335
pub struct FrequencyAnnotator {
13651336
db: DBWithThreadMode<MultiThreaded>,
@@ -1997,7 +1968,6 @@ pub(crate) fn setup_seqvars_annotator(
19971968
assembly: Option<Assembly>,
19981969
) -> Result<Annotator, Error> {
19991970
let mut annotators = vec![];
2000-
let pb_assembly = assembly.as_ref().and_then(proto_assembly_from);
20011971

20021972
// Add the frequency annotator if requested.
20031973
if let Some(rocksdb_path) = &sources.frequencies {
@@ -2037,29 +2007,7 @@ pub(crate) fn setup_seqvars_annotator(
20372007
if let Some(tx_sources) = &sources.transcripts {
20382008
tracing::info!("Opening transcript database(s)");
20392009

2040-
// Filter out any transcript databases that do not match the requested assembly.
2041-
let check_assembly = |db: &TxSeqDatabase, assembly: crate::pbs::txs::Assembly| {
2042-
db.source_version
2043-
.iter()
2044-
.map(|s| s.assembly)
2045-
.any(|a| a == i32::from(assembly))
2046-
};
2047-
let databases = tx_sources
2048-
.iter()
2049-
.enumerate()
2050-
.map(|(i, path)| (i, load_tx_db(path)))
2051-
.filter_map(|(i, txdb)| match txdb {
2052-
Ok(db) => match pb_assembly {
2053-
Some(assembly) if check_assembly(&db, assembly) => Some(Ok(db)),
2054-
Some(_) => {
2055-
tracing::info!("Skipping transcript database {} as its version {:?} does not support the requested assembly ({:?})", &tx_sources[i], &db.source_version, &assembly);
2056-
None
2057-
},
2058-
None => Some(Ok(db)),
2059-
},
2060-
Err(_) => Some(txdb),
2061-
})
2062-
.collect::<anyhow::Result<Vec<_>>>()?;
2010+
let databases = load_transcript_dbs_for_assembly(tx_sources, assembly)?;
20632011

20642012
if databases.is_empty() {
20652013
tracing::warn!("No suitable transcript databases found for requested assembly {:?}, therefore no consequence prediction will occur.", &assembly);
@@ -2080,6 +2028,38 @@ pub(crate) fn setup_seqvars_annotator(
20802028
Ok(annotator)
20812029
}
20822030

2031+
pub(crate) fn load_transcript_dbs_for_assembly(
2032+
tx_sources: &Vec<String>,
2033+
assembly: Option<Assembly>,
2034+
) -> Result<Vec<TxSeqDatabase>, Error> {
2035+
let pb_assembly = assembly.as_ref().and_then(proto_assembly_from);
2036+
2037+
// Filter out any transcript databases that do not match the requested assembly.
2038+
let check_assembly = |db: &TxSeqDatabase, assembly: crate::pbs::txs::Assembly| {
2039+
db.source_version
2040+
.iter()
2041+
.map(|s| s.assembly)
2042+
.any(|a| a == i32::from(assembly))
2043+
};
2044+
let databases = tx_sources
2045+
.iter()
2046+
.enumerate()
2047+
.map(|(i, path)| (i, load_tx_db(path)))
2048+
.filter_map(|(i, txdb)| match txdb {
2049+
Ok(db) => match pb_assembly {
2050+
Some(assembly) if check_assembly(&db, assembly) => Some(Ok(db)),
2051+
Some(_) => {
2052+
tracing::info!("Skipping transcript database {} as its version {:?} does not support the requested assembly ({:?})", &tx_sources[i], &db.source_version, &assembly);
2053+
None
2054+
},
2055+
None => Some(Ok(db)),
2056+
},
2057+
Err(_) => Some(txdb),
2058+
})
2059+
.collect::<anyhow::Result<Vec<_>>>()?;
2060+
Ok(databases)
2061+
}
2062+
20832063
/// Create for all alternate alleles from the given VCF record.
20842064
pub fn from_vcf_allele(value: &noodles::vcf::variant::RecordBuf, allele_no: usize) -> keys::Var {
20852065
let chrom = value.reference_sequence_name().to_string();

src/server/run/actix_server/frequencies.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ pub(crate) struct FrequencyQuery {
2828
pub reference: String,
2929
/// SPDI insertion.
3030
pub alternative: String,
31-
/// Optionally, the HGNC ID of the gene to limit to.
32-
pub hgnc_id: Option<String>,
3331
}
3432

3533
/// One entry in `FrequencyResponse`.
@@ -112,7 +110,6 @@ async fn handle_impl(
112110
position,
113111
reference,
114112
alternative,
115-
hgnc_id,
116113
} = query.clone().into_inner();
117114

118115
let annotator = data

src/server/run/mod.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::annotate::cli::{Sources, TranscriptSettings};
22
use crate::annotate::seqvars::csq::ConfigBuilder;
3-
use crate::annotate::seqvars::{setup_seqvars_annotator, FrequencyAnnotator};
3+
use crate::annotate::seqvars::{
4+
load_transcript_dbs_for_assembly, ConsequenceAnnotator, FrequencyAnnotator,
5+
};
6+
use crate::db::merge::merge_transcript_databases;
47
use crate::{
58
annotate::{
69
seqvars::csq::ConsequencePredictor as SeqvarConsequencePredictor,
@@ -182,28 +185,37 @@ pub async fn run(args_common: &crate::common::Args, args: &Args) -> Result<(), a
182185
tracing::info!(" - loading genome release {:?}", genome_release);
183186
let assembly = genome_release.into();
184187

185-
let annotator =
186-
setup_seqvars_annotator(&args.sources, &args.transcript_settings, Some(assembly))?;
187-
if let Some(seqvars_csq_predictor) = annotator.consequence().map(|a| &a.predictor) {
188-
let config = ConfigBuilder::default()
189-
.report_most_severe_consequence_by(
190-
args.transcript_settings.report_most_severe_consequence_by,
191-
)
192-
.transcript_source(args.transcript_settings.transcript_source)
193-
.build()?;
194-
195-
let provider = seqvars_csq_predictor.provider.clone();
196-
data.provider.insert(genome_release, provider.clone());
188+
if let Some(tx_db_paths) = args.sources.transcripts.as_ref() {
197189
tracing::info!(" - building seqvars predictors");
198-
data.seqvars_predictors.insert(
199-
genome_release,
200-
SeqvarConsequencePredictor::new(provider.clone(), config),
201-
);
202-
tracing::info!(" - building strucvars predictors");
203-
data.strucvars_predictors.insert(
204-
genome_release,
205-
StrucvarConsequencePredictor::new(provider.clone(), assembly),
206-
);
190+
let tx_dbs = load_transcript_dbs_for_assembly(tx_db_paths, Some(assembly))?;
191+
if tx_dbs.is_empty() {
192+
tracing::warn!(
193+
"No transcript databases loaded, respective endpoint will be unavailable."
194+
);
195+
} else {
196+
let tx_db = merge_transcript_databases(tx_dbs)?;
197+
let annotator =
198+
ConsequenceAnnotator::from_db_and_settings(tx_db, &args.transcript_settings)?;
199+
let config = ConfigBuilder::default()
200+
.report_most_severe_consequence_by(
201+
args.transcript_settings.report_most_severe_consequence_by,
202+
)
203+
.transcript_source(args.transcript_settings.transcript_source)
204+
.build()?;
205+
206+
let provider = annotator.predictor.provider.clone();
207+
data.provider.insert(genome_release, provider.clone());
208+
data.seqvars_predictors.insert(
209+
genome_release,
210+
SeqvarConsequencePredictor::new(provider.clone(), config),
211+
);
212+
213+
tracing::info!(" - building strucvars predictors");
214+
data.strucvars_predictors.insert(
215+
genome_release,
216+
StrucvarConsequencePredictor::new(provider.clone(), assembly),
217+
);
218+
}
207219
} else {
208220
tracing::warn!(
209221
"No predictors for genome release {:?}, respective endpoint will be unavailable.",

0 commit comments

Comments
 (0)