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
use std::sync::LazyLock;
use itertools::Itertools;
use mzcore::{
chemistry::{MultiChemical, NeutralLoss},
quantities::Tolerance,
sequence::{BACKBONE, IsAminoAcid},
};
use crate::fragment::{BackboneCFragment, BackboneNFragment, Fragment, FragmentType};
/// Any data structure that can be written to mzPAF
pub trait ToMzPAF {
/// Create a new mzPAF string from this element
fn to_mz_paf_string(&self) -> String {
let mut output = String::new();
self.to_mz_paf(&mut output).unwrap(); // String writing cannot fail
output
}
/// Write the mzPAF encoding of this element to the writer.
/// # Errors
/// When the writer errors.
fn to_mz_paf(&self, w: impl std::fmt::Write) -> std::fmt::Result;
}
impl ToMzPAF for Fragment {
/// Write the fragment as a [mzPAF](https://www.psidev.info/mzPAF) string. Note that mzPAF
/// does not have support for all complexities that are supported by this crate. So fragments
/// of glycans and cross-links for example can not be encoded in a way to keeps the semantics
/// fully intact.
#[expect(clippy::cognitive_complexity)] // It is a very big function but breaking it up might not benefit readers
fn to_mz_paf(&self, mut w: impl std::fmt::Write) -> std::fmt::Result {
if self.auxiliary {
write!(w, "&")?;
}
if let Some(number) = self.peptidoform_ion_index {
write!(w, "{}@", number + 1)?;
} else {
write!(w, "0@")?;
}
// Push the ion type info (plus maybe some neutral losses if needed)
match &self.ion {
FragmentType::a(pos, variant)
| FragmentType::b(pos, variant)
| FragmentType::c(pos, variant)
| FragmentType::x(pos, variant)
| FragmentType::y(pos, variant) => write!(
w,
"{}{}{}",
self.ion.kind(),
pos.series_number,
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)?,
FragmentType::z(pos, variant) => write!(
w,
"{}{}{}",
self.ion.kind(),
pos.series_number,
if *variant == 1 {
String::new()
} else {
format!("{:+}H", variant - 1)
}
)?,
FragmentType::d(pos, aa, distance, variant, label) => {
if *distance == 0 {
write!(
w,
"d{label}{}{}",
pos.series_number,
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)?;
} else if let Some(loss) = aa
.satellite_ion_fragments(
pos.sequence_index,
self.peptidoform_index.unwrap_or_default(),
self.peptidoform_ion_index.unwrap_or_default(),
)
.and_then(|fragments| {
fragments
.iter()
.find(|f| f.0 == *label)
.map(|(_, loss)| loss.clone())
})
{
write!(
w,
"a{}-{loss}{}",
pos.series_number,
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)?;
} else {
write!(w, "?",)?;
}
}
FragmentType::v(pos, aa, distance, variant) => {
if *distance == 0 {
write!(
w,
"v{}{}",
pos.series_number,
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)?;
} else {
write!(
w,
"y{}-{}{}",
pos.series_number,
aa.formulas()
.first()
.map(|f| f - LazyLock::force(&BACKBONE))
.unwrap_or_default(),
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)?;
}
}
FragmentType::w(pos, aa, distance, variant, label) => {
if *distance == 0 {
write!(
w,
"w{label}{}{}",
pos.series_number,
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)?;
} else if let Some(loss) = aa
.satellite_ion_fragments(
pos.sequence_index,
self.peptidoform_index.unwrap_or_default(),
self.peptidoform_ion_index.unwrap_or_default(),
)
.and_then(|fragments| {
fragments
.iter()
.find(|f| f.0 == *label)
.map(|(_, loss)| loss.clone())
})
{
write!(
w,
"z{}-{loss}{}",
pos.series_number,
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)?;
} else {
write!(w, "?",)?;
}
}
FragmentType::Precursor => write!(w, "p")?,
FragmentType::PrecursorSideChainLoss(_, aa) => {
write!(w, "p-r[sidechain_{aa}]")?;
}
FragmentType::Immonium(_, seq) => write!(
w,
"I{}{}",
seq.aminoacid,
seq.modifications
.iter()
.filter_map(|m| m.unimod_name().map(|name| format!("[{name}]")))
.join("") // TODO: how to handle ambiguous mods? Maybe store somewhere which where applied for this fragment
)?,
FragmentType::Unknown(num) => {
if let Some(num) = num {
write!(w, "?{num}",)?;
} else if let Some(formula) = &self.formula {
write!(w, "f{{{formula}}}",)?;
} else {
write!(w, "?",)?;
}
}
FragmentType::Diagnostic(_)
| FragmentType::B { .. }
| FragmentType::BComposition(_, _)
| FragmentType::Y(_)
| FragmentType::YComposition(_, _) => {
if let Some(formula) = &self.formula
&& formula.charge().value != 0
{
// TODO: better way of storing?
write!(w, "f{{{}}}", formula.hill_notation_core())?;
if formula.additional_mass() != 0.0 {
write!(w, "{:+}", formula.additional_mass())?;
}
if formula.charge().value != 1 {
write!(w, "^{:}", formula.charge().value)?;
}
} else {
write!(w, "?",)?;
}
}
FragmentType::Internal(Some(name), a, b) => write!(
w,
"m{}:{}{}",
a.sequence_index + 1,
b.sequence_index + 1,
match name {
(BackboneNFragment::a, BackboneCFragment::x)
| (BackboneNFragment::b, BackboneCFragment::y)
| (BackboneNFragment::c, BackboneCFragment::z) => "",
(BackboneNFragment::a, BackboneCFragment::y) => "-CO",
(BackboneNFragment::a, BackboneCFragment::z) => "-CHNO",
(BackboneNFragment::b, BackboneCFragment::x) => "+CO",
(BackboneNFragment::b, BackboneCFragment::z) => "-NH",
(BackboneNFragment::c, BackboneCFragment::x) => "+CHNO",
(BackboneNFragment::c, BackboneCFragment::y) => "+NH",
}
)?,
FragmentType::Internal(None, a, b) => {
write!(w, "m{}:{}", a.sequence_index + 1, b.sequence_index + 1)?;
}
}
// More losses
for loss in &self.neutral_loss {
match loss {
NeutralLoss::SideChainLoss(_, aa) => {
write!(w, "-r[sidechain_{aa}]")?;
}
NeutralLoss::Gain(1, mol) => {
write!(w, "+{mol}")?;
}
NeutralLoss::Loss(1, mol) => {
write!(w, "-{mol}")?;
}
l => write!(w, "{l}")?,
}
}
// Isotopes: TODO: not handled
// Charge state
if self.charge.value != 1 {
write!(w, "^{}", self.charge.value)?;
}
// Deviation
match self.deviation {
Some(Tolerance::Absolute(abs)) => write!(w, "/{:.3}", abs.value)?,
Some(Tolerance::Relative(ppm)) => {
write!(w, "/{:.3}ppm", ppm.get::<mzcore::system::ratio::ppm>())?;
}
None => (),
}
// Confidence
if let Some(confidence) = self.confidence {
write!(w, "*{confidence}")?;
}
Ok(())
}
}