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
281
282
283
284
285
286
287
288
289
use std::{collections::BTreeSet, fmt::Write};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use crate::{
chemistry::{MolecularCharge, MolecularFormula},
glycan::FullGlycan,
quantities::Multi,
sequence::{
CrossLinkName, CrossLinkSide, Linked, Modification, Peptidoform, RulePossible,
SequencePosition, SimpleModification, SimpleModificationInner,
},
};
/// A single peptidoform ion, can contain multiple peptidoforms
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct PeptidoformIon(pub(crate) Vec<Peptidoform<Linked>>);
impl PeptidoformIon {
/// Create a new [`PeptidoformIon`] from many [`Peptidoform`]s. This returns None if the
/// global isotope modifications or the charge carriers of all peptides are not identical.
pub fn new<Complexity>(
iter: impl IntoIterator<Item = Peptidoform<Complexity>>,
) -> Option<Self> {
let result = Self(iter.into_iter().map(Peptidoform::mark).collect());
let global_and_charge_equal = result.peptidoforms().iter().tuple_windows().all(|(a, b)| {
a.get_global() == b.get_global() && a.get_charge_carriers() == b.get_charge_carriers()
});
global_and_charge_equal.then_some(result)
}
/// Create a new [`PeptidoformIon`] from many [`Peptidoform`]s. This returns None if the
/// global isotope modifications or the charge carriers of all peptides are not identical.
pub fn from_vec(iter: Vec<Peptidoform<Linked>>) -> Option<Self> {
let result = Self(iter);
let global_and_charge_equal = result.peptidoforms().iter().tuple_windows().all(|(a, b)| {
a.get_global() == b.get_global() && a.get_charge_carriers() == b.get_charge_carriers()
});
global_and_charge_equal.then_some(result)
}
/// Gives all possible formulas for this peptidoform (including breakage of cross-links that can break).
/// Includes the full glycan, if there are any glycans.
/// Assumes all peptides in this peptidoform are connected.
/// If there are no peptides in this peptidoform it returns [`Multi::default`].
pub fn formulas(&self) -> Multi<MolecularFormula> {
self.formulas_inner(0)
}
/// Gives all possible formulas for this peptidoform (including breakage of cross-links that can break).
/// Includes the full glycan, if there are any glycans.
/// Assumes all peptides in this peptidoform are connected.
/// If there are no peptides in this peptidoform it returns [`Multi::default`].
pub(crate) fn formulas_inner(&self, peptidoform_ion_index: usize) -> Multi<MolecularFormula> {
self.0
.first()
.map(|p| {
p.formulas_inner(
0,
peptidoform_ion_index,
&self.0,
&[],
&mut Vec::new(),
true,
&FullGlycan {},
)
.0
})
.unwrap_or_default()
}
/// Assume there is exactly one peptide in this collection.
pub fn singular(mut self) -> Option<Peptidoform<Linked>> {
(self.0.len() == 1).then(|| self.0.pop()).flatten()
}
/// Assume there is exactly one peptide in this collection.
pub fn singular_ref(&self) -> Option<&Peptidoform<Linked>> {
(self.0.len() == 1).then(|| &self.0[0])
}
/// Get all peptides making up this peptidoform
pub fn peptidoforms(&self) -> &[Peptidoform<Linked>] {
&self.0
}
/// Get all peptides making up this peptidoform
pub fn peptidoforms_mut(&mut self) -> &mut [Peptidoform<Linked>] {
&mut self.0
}
/// Set the charge carriers
#[expect(clippy::needless_pass_by_value)]
pub fn set_charge_carriers(&mut self, charge_carriers: Option<MolecularCharge>) {
for peptide in &mut self.0 {
peptide.set_charge_carriers(charge_carriers.clone());
}
}
/// Get the charge carriers
pub fn get_charge_carriers(&self) -> Option<&MolecularCharge> {
// Take the charge of the first peptidoform, as all are required to have the same charge
self.0.first().and_then(|p| p.get_charge_carriers())
}
/// Add a cross-link to this peptidoform and check if it is placed according to its placement rules.
/// The positions are first the peptide index and second the sequence index.
pub fn add_cross_link(
&mut self,
position_1: (usize, SequencePosition),
position_2: (usize, SequencePosition),
linker: SimpleModification,
name: CrossLinkName,
) -> bool {
let pos_1 = self.0.get(position_1.0).map(|seq| &seq[position_1.1]);
let pos_2 = self.0.get(position_2.0).map(|seq| &seq[position_2.1]);
if let (Some(pos_1), Some(pos_2)) = (pos_1, pos_2) {
let left = linker.is_possible(pos_1, position_1.1);
let right = linker.is_possible(pos_2, position_1.1);
let (left, right, according_to_rules) = if matches!(
&*linker,
SimpleModificationInner::Formula(_)
| SimpleModificationInner::Glycan(_)
| SimpleModificationInner::GlycanStructure(_)
| SimpleModificationInner::Gno { .. }
| SimpleModificationInner::Mass(_, _, _)
) {
(
CrossLinkSide::Symmetric(BTreeSet::default()),
CrossLinkSide::Symmetric(BTreeSet::default()),
true,
)
} else {
match (left, right) {
(RulePossible::Symmetric(a), RulePossible::Symmetric(b)) => {
let intersection: BTreeSet<usize> = a.intersection(&b).copied().collect();
if intersection.is_empty() {
(
CrossLinkSide::Symmetric(BTreeSet::default()),
CrossLinkSide::Symmetric(BTreeSet::default()),
false,
)
} else {
(
CrossLinkSide::Symmetric(intersection.clone()),
CrossLinkSide::Symmetric(intersection),
true,
)
}
}
(
RulePossible::AsymmetricLeft(a),
RulePossible::AsymmetricRight(b) | RulePossible::Symmetric(b),
) => {
let intersection: BTreeSet<usize> = a.intersection(&b).copied().collect();
if intersection.is_empty() {
(
CrossLinkSide::Symmetric(BTreeSet::default()),
CrossLinkSide::Symmetric(BTreeSet::default()),
false,
)
} else {
(
CrossLinkSide::Left(intersection.clone()),
CrossLinkSide::Right(intersection),
true,
)
}
}
(
RulePossible::AsymmetricRight(a),
RulePossible::AsymmetricLeft(b) | RulePossible::Symmetric(b),
) => {
let intersection: BTreeSet<usize> = a.intersection(&b).copied().collect();
if intersection.is_empty() {
(
CrossLinkSide::Symmetric(BTreeSet::default()),
CrossLinkSide::Symmetric(BTreeSet::default()),
false,
)
} else {
(
CrossLinkSide::Right(intersection.clone()),
CrossLinkSide::Left(intersection),
true,
)
}
}
_ => (
CrossLinkSide::Symmetric(BTreeSet::default()),
CrossLinkSide::Symmetric(BTreeSet::default()),
false,
),
}
};
self.0[position_1.0].add_modification(
position_1.1,
Modification::CrossLink {
peptide: position_2.0,
sequence_index: position_2.1,
linker: linker.clone(),
name: name.clone(),
side: left,
},
);
self.0[position_2.0].add_modification(
position_2.1,
Modification::CrossLink {
peptide: position_1.0,
sequence_index: position_1.1,
linker,
name,
side: right,
},
);
according_to_rules
} else {
false // TODO: maybe generate better error on invalid positions
}
}
/// Display this peptidoform.
/// `specification_compliant` Displays this peptidoform either normalised to the internal representation or as fully spec compliant ProForma
/// (no glycan structure or custom modifications).
/// # Panics
/// When some peptides do not have the same global isotope modifications.
/// # Errors
/// If the underlying formatter errors.
pub fn display(
&self,
f: &mut impl Write,
show_global_mods: bool,
specification_compliant: bool,
) -> std::fmt::Result {
if show_global_mods {
let global_equal = self
.peptidoforms()
.iter()
.map(Peptidoform::get_global)
.tuple_windows()
.all(|(a, b)| a == b);
assert!(
global_equal,
"Not all global isotope modifications on all peptides on this peptidoform are identical"
);
let empty = Vec::new();
let global = self
.peptidoforms()
.first()
.map_or(empty.as_slice(), |p| p.get_global());
for (element, isotope) in global {
write!(
f,
"<{}{}>",
isotope.map(|i| i.to_string()).unwrap_or_default(),
element
)?;
}
}
let mut first = true;
for (index, p) in self.peptidoforms().iter().enumerate() {
if !first {
write!(f, "//")?;
}
p.display(
f,
false,
index == self.peptidoforms().len() - 1,
specification_compliant,
)?;
first = false;
}
Ok(())
}
}
impl std::fmt::Display for PeptidoformIon {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.display(f, true, true)
}
}
impl<Complexity> From<Peptidoform<Complexity>> for PeptidoformIon {
fn from(value: Peptidoform<Complexity>) -> Self {
Self(vec![value.mark()])
}
}