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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use serde::{Deserialize, Serialize};

use crate::formula::{Chemical, MolecularFormula};
use crate::fragment::Position;
use crate::fragment::{Fragment, FragmentType};
use crate::model::*;
use crate::molecular_charge::MolecularCharge;
use crate::Element;

include!("shared/aminoacid.rs");

impl Chemical for AminoAcid {
    fn formula(&self) -> MolecularFormula {
        match self {
            Self::Alanine => molecular_formula!(H 5 C 3 O 1 N 1),
            Self::Arginine => molecular_formula!(H 12 C 6 O 1 N 4), // One of the H's counts as the charge carrier and is added later
            Self::AmbiguousAsparagine => {
                panic!("Molecular formula of Asx/B ambiguous asparagine is not defined.")
            }
            Self::Asparagine => molecular_formula!(H 6 C 4 O 2 N 2),
            Self::AsparticAcid => molecular_formula!(H 5 C 4 O 3 N 1),
            Self::Cysteine => molecular_formula!(H 5 C 3 O 1 N 1 S 1),
            Self::GlutamicAcid => molecular_formula!(H 7 C 5 O 3 N 1),
            Self::Glutamine => molecular_formula!(H 8 C 5 O 2 N 2),
            Self::AmbiguousGlutamine => {
                panic!("Molecular formula of Glx/Z ambiguous glutamine is not defined.")
            }
            Self::Glycine => molecular_formula!(H 3 C 2 O 1 N 1),
            Self::Histidine => molecular_formula!(H 7 C 6 O 1 N 3),
            Self::AmbiguousLeucine | Self::Isoleucine | Self::Leucine => {
                molecular_formula!(H 11 C 6 O 1 N 1)
            }
            Self::Lysine => molecular_formula!(H 12 C 6 O 1 N 2),
            Self::Methionine => molecular_formula!(H 9 C 5 O 1 N 1 S 1),
            Self::Phenylalanine => molecular_formula!(H 9 C 9 O 1 N 1),
            Self::Proline => molecular_formula!(H 7 C 5 O 1 N 1),
            Self::Pyrrolysine => molecular_formula!(H 19 C 11 O 2 N 3),
            Self::Selenocysteine => molecular_formula!(H 4 C 3 O 1 N 1 Se 1),
            Self::Serine => molecular_formula!(H 5 C 3 O 2 N 1),
            Self::Threonine => molecular_formula!(H 7 C 4 O 2 N 1),
            Self::Tryptophan => molecular_formula!(H 10 C 11 O 1 N 2),
            Self::Tyrosine => molecular_formula!(H 9 C 9 O 2 N 1),
            Self::Valine => molecular_formula!(H 9 C 5 O 1 N 1),
            Self::Unknown => molecular_formula!(),
        }
    }
}

#[allow(non_upper_case_globals, missing_docs)]
impl AminoAcid {
    pub const A: Self = Self::Alanine;
    pub const B: Self = Self::AmbiguousAsparagine;
    pub const C: Self = Self::Cysteine;
    pub const D: Self = Self::AsparticAcid;
    pub const E: Self = Self::GlutamicAcid;
    pub const F: Self = Self::Phenylalanine;
    pub const G: Self = Self::Glycine;
    pub const H: Self = Self::Histidine;
    pub const I: Self = Self::Isoleucine;
    pub const J: Self = Self::AmbiguousLeucine;
    pub const K: Self = Self::Lysine;
    pub const L: Self = Self::Leucine;
    pub const M: Self = Self::Methionine;
    pub const N: Self = Self::Asparagine;
    pub const O: Self = Self::Pyrrolysine;
    pub const P: Self = Self::Proline;
    pub const Q: Self = Self::Glutamine;
    pub const R: Self = Self::Arginine;
    pub const S: Self = Self::Serine;
    pub const T: Self = Self::Threonine;
    pub const U: Self = Self::Selenocysteine;
    pub const V: Self = Self::Valine;
    pub const W: Self = Self::Tryptophan;
    pub const X: Self = Self::Unknown;
    pub const Y: Self = Self::Tyrosine;
    pub const Z: Self = Self::AmbiguousGlutamine;
    pub const Ala: Self = Self::Alanine;
    pub const Cys: Self = Self::Cysteine;
    pub const Asn: Self = Self::Asparagine;
    pub const Asp: Self = Self::AsparticAcid;
    pub const Asx: Self = Self::AmbiguousAsparagine;
    pub const Glu: Self = Self::GlutamicAcid;
    pub const Phe: Self = Self::Phenylalanine;
    pub const Gly: Self = Self::Glycine;
    pub const His: Self = Self::Histidine;
    pub const Ile: Self = Self::Isoleucine;
    pub const Xle: Self = Self::AmbiguousLeucine;
    pub const Lys: Self = Self::Lysine;
    pub const Leu: Self = Self::Leucine;
    pub const Met: Self = Self::Methionine;
    pub const Pyl: Self = Self::Pyrrolysine;
    pub const Pro: Self = Self::Proline;
    pub const Gln: Self = Self::Glutamine;
    pub const Glx: Self = Self::AmbiguousGlutamine;
    pub const Arg: Self = Self::Arginine;
    pub const Ser: Self = Self::Serine;
    pub const Thr: Self = Self::Threonine;
    pub const Sec: Self = Self::Selenocysteine;
    pub const Val: Self = Self::Valine;
    pub const Trp: Self = Self::Tryptophan;
    pub const Tyr: Self = Self::Tyrosine;
    pub const Xaa: Self = Self::Unknown;

    // TODO: Take side chain mutations into account (maybe define pyrrolysine as a mutation)
    pub fn satellite_ion_fragments(&self) -> Vec<MolecularFormula> {
        match self {
            Self::Alanine
            | Self::AmbiguousLeucine
            | Self::Glycine
            | Self::Histidine
            | Self::Phenylalanine
            | Self::Proline
            | Self::Tryptophan
            | Self::Tyrosine
            | Self::AmbiguousAsparagine
            | Self::AmbiguousGlutamine
            | Self::Unknown => vec![],
            Self::Arginine => vec![molecular_formula!(H 9 C 2 N 2)],
            Self::Asparagine => vec![molecular_formula!(H 2 C 1 N 1 O 1)],
            Self::AsparticAcid => vec![molecular_formula!(H 1 C 1 O 2)],
            Self::Cysteine => vec![molecular_formula!(H 1 S 1)],
            Self::GlutamicAcid => vec![molecular_formula!(H 3 C 2 O 2)],
            Self::Glutamine => vec![molecular_formula!(H 4 C 2 N 1 O 1)],
            Self::Isoleucine => vec![molecular_formula!(H 3 C 1), molecular_formula!(H 5 C 2)],
            Self::Leucine => vec![molecular_formula!(H 7 C 3)],
            Self::Lysine => vec![molecular_formula!(H 8 C 3 N 1)],
            Self::Methionine => vec![molecular_formula!(H 5 C 2 S 1)],
            Self::Pyrrolysine => {
                vec![molecular_formula!(H 15 C 9 N 2 O 1)]
            } // Weird, TODO: figure out what to make of this
            Self::Selenocysteine => vec![molecular_formula!(Se 1)],
            Self::Serine => vec![molecular_formula!(H 1 O 1)],
            Self::Threonine => vec![molecular_formula!(H 1 O 1), molecular_formula!(H 3 C 1)],
            Self::Valine => vec![molecular_formula!(H 3 C 1)], // Technically two options, but both have the same mass
        }
    }

    #[allow(clippy::too_many_lines, clippy::too_many_arguments)]
    pub fn fragments(
        &self,
        n_term: &[(MolecularFormula, String)],
        c_term: &[(MolecularFormula, String)],
        modifications: &MolecularFormula,
        charge_carriers: &MolecularCharge,
        sequence_index: usize,
        sequence_length: usize,
        ions: &PossibleIons,
        peptide_index: usize,
    ) -> Vec<Fragment> {
        let mut base_fragments = Vec::with_capacity(ions.size_upper_bound());
        if ions.a.0 {
            base_fragments.extend(Fragment::generate_all(
                &(modifications + &self.formula() - molecular_formula!(H 1 C 1 O 1)),
                peptide_index,
                &FragmentType::a(Position::n(sequence_index, sequence_length)),
                n_term,
                ions.a.1,
            ));
        }
        if ions.b.0 {
            base_fragments.extend(Fragment::generate_all(
                &(modifications + &self.formula() - molecular_formula!(H 1)),
                peptide_index,
                &FragmentType::b(Position::n(sequence_index, sequence_length)),
                n_term,
                ions.b.1,
            ));
        }
        if ions.c.0 {
            base_fragments.extend(Fragment::generate_all(
                &(modifications + &self.formula() + molecular_formula!(H 2 N 1)),
                peptide_index,
                &FragmentType::c(Position::n(sequence_index, sequence_length)),
                n_term,
                ions.c.1,
            ));
        }
        if ions.d.0 {
            for satellite in self.satellite_ion_fragments() {
                base_fragments.extend(Fragment::generate_all(
                    &(modifications + &self.formula()
                        - satellite
                        - molecular_formula!(H 1 C 1 O 1)),
                    peptide_index,
                    &FragmentType::d(Position::n(sequence_index, sequence_length)),
                    n_term,
                    ions.d.1,
                ));
            }
        }
        if ions.v.0 {
            base_fragments.extend(Fragment::generate_all(
                &(modifications + &molecular_formula!(H 3 C 2 N 1 O 1)), // TODO: are the modifications needed here? Some are on the side chain but some are on the backbone as well
                peptide_index,
                &FragmentType::v(Position::n(sequence_index, sequence_length)),
                c_term,
                ions.v.1,
            ));
        }
        if ions.w.0 {
            for satellite in self.satellite_ion_fragments() {
                base_fragments.extend(Fragment::generate_all(
                    &(modifications + &self.formula() - satellite - molecular_formula!(H 2 N 1)),
                    peptide_index,
                    &FragmentType::w(Position::c(sequence_index, sequence_length)),
                    c_term,
                    ions.w.1,
                ));
            }
        }
        if ions.x.0 {
            base_fragments.extend(Fragment::generate_all(
                &(modifications + &self.formula() + molecular_formula!(C 1 O 1)
                    - molecular_formula!(H 1)),
                peptide_index,
                &FragmentType::x(Position::c(sequence_index, sequence_length)),
                c_term,
                ions.x.1,
            ));
        }
        if ions.y.0 {
            base_fragments.extend(Fragment::generate_all(
                &(modifications + &self.formula() + molecular_formula!(H 1)),
                peptide_index,
                &FragmentType::y(Position::c(sequence_index, sequence_length)),
                c_term,
                ions.y.1,
            ));
        }
        if ions.z.0 {
            base_fragments.extend(Fragment::generate_all(
                &(modifications + &self.formula() - molecular_formula!(H 2 N 1)),
                peptide_index,
                &FragmentType::z(Position::c(sequence_index, sequence_length)),
                c_term,
                ions.z.1,
            ));
            base_fragments.extend(Fragment::generate_all(
                &(modifications + &self.formula() - molecular_formula!(H 1 N 1)),
                peptide_index,
                &FragmentType::z·(Position::c(sequence_index, sequence_length)),
                c_term,
                ions.z.1,
            ));
        }
        let charge_options = charge_carriers.all_charge_options();
        let mut charged = Vec::with_capacity(base_fragments.len() * charge_options.len());
        for base in base_fragments {
            for charge in &charge_options {
                charged.push(base.with_charge(charge));
            }
        }
        charged
    }

    pub const fn char(&self) -> char {
        match self {
            Self::Alanine => 'A',
            Self::AmbiguousAsparagine => 'B',
            Self::Cysteine => 'C',
            Self::AsparticAcid => 'D',
            Self::GlutamicAcid => 'E',
            Self::Phenylalanine => 'F',
            Self::Glycine => 'G',
            Self::Histidine => 'H',
            Self::Isoleucine => 'I',
            Self::AmbiguousLeucine => 'J',
            Self::Lysine => 'K',
            Self::Leucine => 'L',
            Self::Methionine => 'M',
            Self::Asparagine => 'N',
            Self::Pyrrolysine => 'O',
            Self::Proline => 'P',
            Self::Glutamine => 'Q',
            Self::Arginine => 'R',
            Self::Serine => 'S',
            Self::Threonine => 'T',
            Self::Selenocysteine => 'U',
            Self::Valine => 'V',
            Self::Tryptophan => 'W',
            Self::Unknown => 'X',
            Self::Tyrosine => 'Y',
            Self::AmbiguousGlutamine => 'Z',
        }
    }
}

#[cfg(test)]
#[allow(clippy::unreadable_literal, clippy::float_cmp)]
mod tests {
    use super::*;

    #[test]
    fn mass() {
        let weight_ala = AminoAcid::A.formula().average_weight().unwrap();
        let mass_ala = AminoAcid::Ala.formula().monoisotopic_mass().unwrap();
        assert_ne!(weight_ala, mass_ala);
        assert!((weight_ala.value - 71.07793).abs() < 1e-5);
        assert!((mass_ala.value - 71.037113783).abs() < 1e-5);
    }

    #[test]
    fn mass_lysine() {
        let weight_lys = AminoAcid::K.formula().average_weight().unwrap();
        let mass_lys = AminoAcid::Lys.formula().monoisotopic_mass().unwrap();
        assert_ne!(weight_lys, mass_lys);
        assert!((weight_lys.value - 128.17240999999999).abs() < 1e-5);
        assert!((mass_lys.value - 128.094963010536).abs() < 1e-5);
    }

    #[test]
    fn masses() {
        let known = &[
            ('A', 71.03711, 71.08),
            ('R', 156.10111, 156.2),
            ('N', 114.04293, 114.1),
            ('D', 115.02694, 115.1),
            ('C', 103.00919, 103.1),
            ('E', 129.04259, 129.1),
            ('Q', 128.05858, 128.1),
            ('G', 57.02146, 57.05),
            ('H', 137.05891, 137.1),
            ('I', 113.08406, 113.2),
            ('L', 113.08406, 113.2),
            ('K', 128.09496, 128.2),
            ('M', 131.04049, 131.2),
            ('F', 147.06841, 147.2),
            ('P', 97.05276, 97.12),
            ('S', 87.03203, 87.08),
            ('T', 101.04768, 101.1),
            ('W', 186.07931, 186.2),
            ('Y', 163.06333, 163.2),
            ('V', 99.06841, 99.13),
        ];

        for (aa, mono_mass, average_weight) in known {
            let aa = AminoAcid::try_from(*aa).unwrap();
            let (mono, weight) = (
                aa.formula().monoisotopic_mass().unwrap().value,
                aa.formula().average_weight().unwrap().value,
            );
            println!(
                "{}: {} {} {} {}",
                aa.char(),
                mono,
                mono_mass,
                weight,
                average_weight
            );
            assert!((mono - *mono_mass).abs() < 1e-5);
            assert!((weight - *average_weight).abs() < 1e-1);
        }
    }

    #[test]
    fn read_aa() {
        assert_eq!(
            AminoAcid::try_from('B').unwrap(),
            AminoAcid::AmbiguousAsparagine
        );
        assert_eq!(
            AminoAcid::try_from(b'B').unwrap(),
            AminoAcid::AmbiguousAsparagine
        );
        assert_eq!(AminoAcid::try_from('c'), Ok(AminoAcid::Cysteine));
        assert_eq!(AminoAcid::try_from('🦀'), Err(()));
    }
}