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
use std::{fmt::Write, sync::LazyLock};
use itertools::Itertools;
use crate::{
chemistry::MultiChemical,
fragment::{BackboneCFragment, BackboneNFragment, Fragment, FragmentType, NeutralLoss},
quantities::Tolerance,
sequence::{BACKBONE, IsAminoAcid},
};
impl Fragment {
/// Write the fragment as a [mzPAF](https://www.psidev.info/mzPAF) string.
// TODO: figure out a way to handle the fallibility (when used on glycans/cross-linked stuff etc)
pub fn to_mz_paf(&self) -> String {
let mut output = String::new();
if self.auxiliary {
output.push('&');
}
if let Some(number) = self.peptidoform_ion_index {
write!(&mut output, "{}@", number + 1).unwrap();
} else {
write!(&mut output, "0@").unwrap();
}
// 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!(
&mut output,
"{}{}{}",
self.ion.kind(),
pos.series_number,
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)
.unwrap(),
FragmentType::z(pos, variant) => write!(
&mut output,
"{}{}{}",
self.ion.kind(),
pos.series_number,
if *variant == 1 {
String::new()
} else {
format!("{:+}H", variant - 1)
}
)
.unwrap(),
FragmentType::d(pos, aa, distance, variant, label) => {
if *distance == 0 {
write!(
&mut output,
"d{label}{}{}",
pos.series_number,
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)
.unwrap();
} 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!(
&mut output,
"a{}-{loss}{}",
pos.series_number,
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)
.unwrap();
} else {
write!(&mut output, "?",).unwrap();
}
}
FragmentType::v(pos, aa, distance, variant) => {
if *distance == 0 {
write!(
&mut output,
"v{}{}",
pos.series_number,
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)
.unwrap();
} else {
write!(
&mut output,
"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")
}
)
.unwrap();
}
}
FragmentType::w(pos, aa, distance, variant, label) => {
if *distance == 0 {
write!(
&mut output,
"w{label}{}{}",
pos.series_number,
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)
.unwrap();
} 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!(
&mut output,
"z{}-{loss}{}",
pos.series_number,
if *variant == 0 {
String::new()
} else {
format!("{variant:+}H")
}
)
.unwrap();
} else {
write!(&mut output, "?",).unwrap();
}
}
FragmentType::Precursor => write!(&mut output, "p").unwrap(),
FragmentType::PrecursorSideChainLoss(_, aa) => {
write!(&mut output, "p-r[sidechain_{aa}]").unwrap();
}
FragmentType::Immonium(_, seq) => write!(
&mut output,
"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
)
.unwrap(),
FragmentType::Unknown(num) => {
if let Some(num) = num {
write!(&mut output, "?{num}",).unwrap();
} else if let Some(formula) = &self.formula {
write!(&mut output, "f{{{formula}}}",).unwrap();
} else {
write!(&mut output, "?",).unwrap();
}
}
FragmentType::Diagnostic(_)
| FragmentType::B { .. }
| FragmentType::BComposition(_, _)
| FragmentType::Y(_)
| FragmentType::YComposition(_, _) => {
if let Some(formula) = &self.formula {
// TODO: better way of storing?
write!(&mut output, "f{{{formula}}}",).unwrap();
} else {
write!(&mut output, "?",).unwrap();
}
}
FragmentType::Internal(Some(name), a, b) => write!(
&mut output,
"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",
}
)
.unwrap(),
FragmentType::Internal(None, a, b) => write!(
&mut output,
"m{}:{}",
a.sequence_index + 1,
b.sequence_index + 1
)
.unwrap(),
}
// More losses
for loss in &self.neutral_loss {
match loss {
NeutralLoss::SideChainLoss(_, aa) => {
write!(&mut output, "-r[sidechain_{aa}]").unwrap();
}
NeutralLoss::Gain(1, mol) => {
write!(&mut output, "+{mol}").unwrap();
}
NeutralLoss::Loss(1, mol) => {
write!(&mut output, "-{mol}").unwrap();
}
l => write!(&mut output, "{l}").unwrap(),
}
}
// Isotopes: TODO: not handled
// Charge state
if self.charge.value != 1 {
write!(&mut output, "^{}", self.charge.value).unwrap();
}
// Deviation
match self.deviation {
Some(Tolerance::Absolute(abs)) => write!(&mut output, "/{:.3}", abs.value).unwrap(),
Some(Tolerance::Relative(ppm)) => write!(
&mut output,
"/{:.3}ppm",
ppm.get::<crate::system::ratio::ppm>()
)
.unwrap(),
None => (),
}
// Confidence
if let Some(confidence) = self.confidence {
write!(&mut output, "*{confidence}").unwrap();
}
output
}
}