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
use mzcore::{
chemistry::AmbiguousLabel,
prelude::{AminoAcid, MassMode, SequencePosition},
};
use crate::{
annotation::{Recovered, model::MatchingParameters},
fragment::Fragment,
mzspeclib::AnalyteTarget,
spectrum::AnnotatedSpectrum,
};
impl AnnotatedSpectrum {
/// Get the spectrum scores for this annotated spectrum.
/// The returned tuple has the scores for all peptides combined as first item
/// and as second item a vector with for each peptide its individual scores.
pub fn ambigous_statistics(
&self,
fragments: &[Fragment],
parameters: &MatchingParameters,
mass_mode: MassMode,
) -> AmbiguousStatistics {
let fragments = fragments
.iter()
.filter(|f| {
f.mz(mass_mode)
.is_some_and(|mz| parameters.mz_range.contains(&mz))
})
.collect::<Vec<_>>();
let total_intensity: f32 = self.peaks.iter().map(|p| p.intensity).sum();
let theoretical = |label: &AmbiguousLabel| {
fragments
.iter()
.filter(|f| {
f.formula
.as_ref()
.is_some_and(|f| f.labels().contains(label))
})
.count() as u32
};
let annotated = |label: &AmbiguousLabel| {
self.peaks.iter().fold((0_u32, 0.0), |acc, p| {
let count = p
.annotations
.iter()
.filter(|f| {
f.formula
.as_ref()
.is_some_and(|f| f.labels().contains(label))
})
.count();
if count == 0 {
acc
} else {
(acc.0 + count as u32, acc.1 + p.intensity)
}
})
};
let statistics = |label: &AmbiguousLabel| {
let th_count = theoretical(label);
let (an_count, an_intensity) = annotated(label);
(
Recovered {
found: an_count,
total: th_count,
},
Recovered {
found: an_intensity,
total: total_intensity,
},
)
};
let mut result = AmbiguousStatistics {
aminoacids: Vec::new(),
modifications: Vec::new(),
};
for (peptidoform_ion_index, analyte) in self.analytes.iter().enumerate() {
let AnalyteTarget::PeptidoformIon(peptidoform_ion) = &analyte.target else {
continue;
};
for (peptidoform_index, peptidoform) in
peptidoform_ion.peptidoforms().iter().enumerate()
{
for (sequence_index, aa) in peptidoform.sequence().iter().enumerate() {
match aa.aminoacid.aminoacid() {
AminoAcid::AmbiguousLeucine => {
let isoleucine = statistics(&AmbiguousLabel::AminoAcid {
option: AminoAcid::Isoleucine,
sequence_index,
peptidoform_index,
peptidoform_ion_index,
});
let leucine = statistics(&AmbiguousLabel::AminoAcid {
option: AminoAcid::Leucine,
sequence_index,
peptidoform_index,
peptidoform_ion_index,
});
result.aminoacids.push(AmbiguousAminoAcid {
sequence_index,
peptidoform_index,
peptidoform_ion_index,
optiona_a: (AminoAcid::Isoleucine, isoleucine.0, isoleucine.1),
optiona_b: (AminoAcid::Leucine, leucine.0, leucine.1),
});
}
AminoAcid::AmbiguousAsparagine => {
let aspartic_acid = statistics(&AmbiguousLabel::AminoAcid {
option: AminoAcid::AsparticAcid,
sequence_index,
peptidoform_index,
peptidoform_ion_index,
});
let asparagine = statistics(&AmbiguousLabel::AminoAcid {
option: AminoAcid::Asparagine,
sequence_index,
peptidoform_index,
peptidoform_ion_index,
});
result.aminoacids.push(AmbiguousAminoAcid {
sequence_index,
peptidoform_index,
peptidoform_ion_index,
optiona_a: (
AminoAcid::AsparticAcid,
aspartic_acid.0,
aspartic_acid.1,
),
optiona_b: (AminoAcid::Asparagine, asparagine.0, asparagine.1),
});
}
AminoAcid::AmbiguousGlutamine => {
let glutamic_acid = statistics(&AmbiguousLabel::AminoAcid {
option: AminoAcid::GlutamicAcid,
sequence_index,
peptidoform_index,
peptidoform_ion_index,
});
let glutamine = statistics(&AmbiguousLabel::AminoAcid {
option: AminoAcid::Glutamine,
sequence_index,
peptidoform_index,
peptidoform_ion_index,
});
result.aminoacids.push(AmbiguousAminoAcid {
sequence_index,
peptidoform_index,
peptidoform_ion_index,
optiona_a: (
AminoAcid::GlutamicAcid,
glutamic_acid.0,
glutamic_acid.1,
),
optiona_b: (AminoAcid::Glutamine, glutamine.0, glutamine.1),
});
}
_ => (),
}
}
for (id, locations) in peptidoform.get_ambiguous_modifications().iter().enumerate()
{
result.modifications.push(AmbiguousModification {
id,
peptidoform_index,
peptidoform_ion_index,
options: locations
.iter()
.copied()
.map(|sequence_index| {
let stats = statistics(&AmbiguousLabel::Modification {
id,
sequence_index,
peptidoform_index,
peptidoform_ion_index,
});
(sequence_index, stats.0, stats.1)
})
.collect(),
});
}
}
}
result
}
}
/// All statistics for ambiguous parts of the peptidoform definition
#[derive(Clone, Debug)]
pub struct AmbiguousStatistics {
/// All ambiguous amino acids
pub aminoacids: Vec<AmbiguousAminoAcid>,
/// All ambiguous modifications (modifications of unknown position)
pub modifications: Vec<AmbiguousModification>,
}
/// The statistics on an ambiguous amino acid and the support for both options
#[derive(Clone, Copy, Debug)]
pub struct AmbiguousAminoAcid {
/// What location in the sequence are we talking about
pub sequence_index: usize,
/// Peptidoform index
pub peptidoform_index: usize,
/// Peptidoform ion index
pub peptidoform_ion_index: usize,
/// First option, the amino acid, the fraction of theoretical fragments found, and the fraction of TIC that is annotated
pub optiona_a: (AminoAcid, Recovered<u32>, Recovered<f32>),
/// Second option, the amino acid, the fraction of theoretical fragments found, and the fraction of TIC that is annotated
pub optiona_b: (AminoAcid, Recovered<u32>, Recovered<f32>),
}
/// The statistics on an ambiguous modification and the support for each of the possible locations
#[derive(Clone, Debug)]
pub struct AmbiguousModification {
/// Which ambiguous modification
pub id: usize,
/// Peptidoform index
pub peptidoform_index: usize,
/// Peptidoform ion index
pub peptidoform_ion_index: usize,
/// All options, with the location, the fraction of theoretical fragments found, and the fraction of TIC that is annotated
pub options: Vec<(SequencePosition, Recovered<u32>, Recovered<f32>)>,
}