-
Notifications
You must be signed in to change notification settings - Fork 1
/
variant_related.rs
280 lines (263 loc) · 9.54 KB
/
variant_related.rs
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
//! Variant-related information.
use crate::seqvars::query::{annonars::Annotator, schema::SequenceVariant};
/// Record for variant-related scores.
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize, derive_new::new)]
pub struct Record {
/// Precomputed scores.
#[serde(skip_serializing_if = "indexmap::IndexMap::is_empty")]
pub precomputed_scores: indexmap::IndexMap<String, f32>,
/// Database identifiers.
#[serde(skip_serializing_if = "DbIds::is_empty")]
pub db_ids: DbIds,
/// Clinvar information.
#[serde(skip_serializing_if = "Option::is_none")]
pub clinvar: Option<Clinvar>,
/// Frequency information.
#[serde(skip_serializing_if = "Frequency::is_empty")]
pub frequency: Frequency,
}
impl Record {
/// Construct given sequence variant and annonars annotator.
pub fn with_seqvar_and_annotator(
seqvar: &SequenceVariant,
annotator: &Annotator,
) -> Result<Self, anyhow::Error> {
Ok(Self {
precomputed_scores: Self::query_precomputed_scores(seqvar, annotator)?,
db_ids: DbIds::with_seqvar_and_annotator(seqvar, annotator)?,
clinvar: Clinvar::with_seqvar_and_annotator(seqvar, annotator)?,
frequency: Frequency::with_seqvar(seqvar)?,
})
}
/// Query precomputed scores for `seqvar` from annonars `annotator`.
pub fn query_precomputed_scores(
_seqvar: &SequenceVariant,
_annotator: &Annotator,
) -> Result<indexmap::IndexMap<String, f32>, anyhow::Error> {
// TODO: implement me!
Ok(indexmap::IndexMap::default())
}
/// Returns whether all fields are empty and would not be serialized.
pub fn is_empty(&self) -> bool {
self.precomputed_scores.is_empty()
&& self.db_ids.is_empty()
&& self.clinvar.is_none()
&& self.frequency.is_empty()
}
}
/// Database identifiers.
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize, derive_new::new)]
pub struct DbIds {
/// The variant's dbSNP identifier present.
#[serde(skip_serializing_if = "Option::is_none")]
pub dbsnp_rs: Option<String>,
}
impl DbIds {
/// Construct given sequence variant and annonars annotator.
pub fn with_seqvar_and_annotator(
seqvar: &SequenceVariant,
annotator: &Annotator,
) -> Result<Self, anyhow::Error> {
// TODO: need to properly separate error from no result in annonars.
Ok(Self {
dbsnp_rs: annotator
.query_dbsnp(seqvar)
.ok()
.map(|record| format!("rs{}", record.rs_id)),
})
}
/// Returns whether all database identifiers are unset.
pub fn is_empty(&self) -> bool {
self.dbsnp_rs.is_none()
}
}
/// ClinVar-related information.
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize, derive_new::new)]
pub struct Clinvar {
// TODO: switch to VCV summary or hierarchical?
/// The RCV accession.
pub rcv: String,
/// The clinical significance.
pub significance: String,
/// The review status.
pub review_status: String,
}
impl Clinvar {
/// Construct given sequence variant and annonars annotator.
pub fn with_seqvar_and_annotator(
seqvar: &SequenceVariant,
annotator: &Annotator,
) -> Result<Option<Self>, anyhow::Error> {
// TODO: need to properly separate error from no result in annonars.
annotator
.query_clinvar_minimal(seqvar)
.ok()
.map(|record| {
let annonars::clinvar_minimal::pbs::Record {
rcv,
clinical_significance,
review_status,
..
} = record;
Ok(Self {
rcv,
significance: match clinical_significance {
0 => "Pathogenic",
1 => "Likely pathogenic",
2 => "Uncertain significance",
3 => "Likely benign",
4 => "Benign",
_ => anyhow::bail!(
"invalid clinical significance enum: {}",
clinical_significance
),
}
.into(),
review_status: match review_status {
0 => "no assertion provided",
1 => "no assertion criteria provided",
2 => "criteria provided, conflicting interpretations",
3 => "criteria provided, single submitter",
4 => "criteria provided, multiple submitters, no conflicts",
5 => "reviewed by expert panel",
6 => "practice guideline",
_ => anyhow::bail!("invalid review status enum: {}", review_status),
}
.into(),
})
})
.transpose()
}
}
/// Frequency information.
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize, derive_builder::Builder)]
pub struct Frequency {
/// gnomAD-genomes frequency
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
pub gnomad_genomes: Option<NuclearFrequency>,
/// gnomAD-exomes frequency
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
pub gnomad_exomes: Option<NuclearFrequency>,
/// gnomad-mtDNA frequency
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
pub gnomad_mtdna: Option<MtdnaFrequency>,
/// HelixMtDb frequency
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
pub helixmtdb: Option<MtdnaFrequency>,
/// inhouse frequency
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
pub inhouse: Option<NuclearFrequency>,
}
impl Frequency {
/// Extract frequency information from `seqvar`
pub fn with_seqvar(seqvar: &SequenceVariant) -> Result<Frequency, anyhow::Error> {
// TODO: inhouse not supported at the moment
let chrom = annonars::common::cli::canonicalize(&seqvar.chrom);
let frequency = if chrom == "MT" {
FrequencyBuilder::default()
.gnomad_genomes(
NuclearFrequency::new(
seqvar.gnomad_genomes_af(),
seqvar.gnomad_genomes_an,
seqvar.gnomad_genomes_het,
seqvar.gnomad_genomes_hom,
seqvar.gnomad_genomes_hemi,
)
.some_unless_empty(),
)
.gnomad_exomes(
NuclearFrequency::new(
seqvar.gnomad_exomes_af(),
seqvar.gnomad_exomes_an,
seqvar.gnomad_exomes_het,
seqvar.gnomad_exomes_hom,
seqvar.gnomad_exomes_hemi,
)
.some_unless_empty(),
)
.build()
} else {
FrequencyBuilder::default()
.gnomad_mtdna(
MtdnaFrequency::new(
seqvar.gnomad_genomes_af(),
seqvar.gnomad_genomes_an,
seqvar.gnomad_genomes_het,
seqvar.gnomad_genomes_hom,
)
.some_unless_empty(),
)
.helixmtdb(
MtdnaFrequency::new(
seqvar.helixmtdb_af(),
seqvar.helix_an,
seqvar.helix_het,
seqvar.helix_hom,
)
.some_unless_empty(),
)
.build()
}
.map_err(|e| anyhow::anyhow!("could not build frequency information: {}", e))?;
Ok(frequency)
}
/// Returns whether all frequencies are empty.
pub fn is_empty(&self) -> bool {
self.gnomad_genomes.is_none()
&& self.gnomad_exomes.is_none()
&& self.gnomad_mtdna.is_none()
&& self.helixmtdb.is_none()
&& self.inhouse.is_none()
}
}
/// Nuclear frequency information.
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize, derive_new::new)]
pub struct NuclearFrequency {
/// Overall allele frequency.
pub allele_freq: f32,
/// Number of alleles.
pub allele_count: i32,
/// Number of heterozygous carriers.
pub het_carriers: i32,
/// Number of homozygous carriers.
pub hom_carriers: i32,
/// Number of hemizygous carriers.
pub hemi_carriers: i32,
}
impl NuclearFrequency {
/// Return self or None if empty.
pub fn some_unless_empty(self) -> Option<Self> {
if self.allele_count == 0 {
None
} else {
Some(self)
}
}
}
/// Mitochondrial frequency information.
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize, derive_new::new)]
pub struct MtdnaFrequency {
/// Overall allele frequency.
pub allele_freq: f32,
/// Number of alleles.
pub allele_count: i32,
/// Number of heterplasmic carriers.
pub het_carriers: i32,
/// Number of homoplasmic carriers.
pub hom_carriers: i32,
}
impl MtdnaFrequency {
/// Return self or None if empty.
pub fn some_unless_empty(self) -> Option<Self> {
if self.allele_count == 0 {
None
} else {
Some(self)
}
}
}