1use crate::formula;
12
13#[derive(Debug, Clone, Copy, PartialEq)]
16pub struct Modification {
17 pub unimod_id: u32,
18 pub name: &'static str,
19 pub targets: &'static str,
21 pub mass_delta: f64,
22 pub composition: &'static str,
24}
25
26impl Modification {
27 pub fn validate(&self) -> Result<(), String> {
31 let from_comp = formula::monoisotopic_mass(self.composition)
32 .map_err(|e| format!("{} (id {}): composition {:?}: {e}", self.name, self.unimod_id, self.composition))?;
33 let d = (from_comp - self.mass_delta).abs();
34 if d > 1e-3 {
35 return Err(format!(
36 "{} (id {}): composition {} weighs {from_comp:.5} but mass_delta = {:.5} (Δ{d:.2e})",
37 self.name, self.unimod_id, self.composition, self.mass_delta
38 ));
39 }
40 Ok(())
41 }
42}
43
44pub const BUILTIN: &[Modification] = &[
49 Modification { unimod_id: 1, name: "Acetyl", targets: "K", mass_delta: 42.01057, composition: "C2H2O" },
50 Modification { unimod_id: 4, name: "Carbamidomethyl", targets: "C", mass_delta: 57.02146, composition: "C2H3NO" },
51 Modification { unimod_id: 21, name: "Phospho", targets: "STY", mass_delta: 79.96633, composition: "HO3P" },
52 Modification { unimod_id: 35, name: "Oxidation", targets: "M", mass_delta: 15.99491, composition: "O" },
53 Modification { unimod_id: 37, name: "Trimethyl", targets: "K", mass_delta: 42.04695, composition: "C3H6" },
54 Modification { unimod_id: 121, name: "GG", targets: "K", mass_delta: 114.04293, composition: "C4H6N2O2" },
55];
56
57pub fn by_id(id: u32) -> Option<Modification> {
59 BUILTIN.iter().copied().find(|m| m.unimod_id == id)
60}
61
62
63pub const MODIFICATION_COMPOSITION: &[(u32, &'static [(&'static str, i32)])] = &[
68 (1, &[("C", 2), ("H", 2), ("O", 1)]), (3, &[("N", 2), ("C", 10), ("H", 14), ("O", 2), ("S", 1)]), (4, &[("C", 2), ("H", 3), ("O", 1), ("N", 1)]),
71 (7, &[("H", -1), ("N", -1), ("O", 1)]), (21, &[("H", 1), ("O", 3), ("P", 1)]), (34, &[("H", 2), ("C", 1)]), (35, &[("O", 1)]), (58, &[("C", 3), ("H", 4), ("O", 1)]), (121, &[("C", 4), ("H", 6), ("O", 2), ("N", 2)]), (122, &[("C", 1), ("O", 1)]), (312, &[("C", 3), ("H", 5), ("O", 2), ("N", 1), ("S", 1)]), (354, &[("H", -1), ("O", 2), ("N", 1)]), (747, &[("C", 3), ("H", 2), ("O", 3)]), (1289, &[("C", 4), ("H", 6), ("O", 1)]), (1363, &[("C", 4), ("H", 4), ("O", 1)]), ];
84
85pub fn atomic_composition(id: u32) -> Option<&'static [(&'static str, i32)]> {
87 MODIFICATION_COMPOSITION.iter().find(|&&(i, _)| i == id).map(|&(_, c)| c)
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 #[test]
95 fn every_builtin_cross_checks() {
96 for m in BUILTIN {
98 m.validate().unwrap_or_else(|e| panic!("{e}"));
99 }
100 }
101
102 #[test]
103 fn compositions_cross_check_mass_table() {
104 use crate::elements::monoisotopic_mass;
105 let mut overlap = 0;
106 for &(id, comp) in MODIFICATION_COMPOSITION {
107 let mass: f64 = comp
108 .iter()
109 .map(|&(e, c)| monoisotopic_mass(e).expect("element") * c as f64)
110 .sum();
111 if let Some(tab) = crate::unimod::mass_delta(id) {
112 assert!((mass - tab).abs() < 1e-3, "UNIMOD:{id} composition {mass} vs table {tab}");
113 overlap += 1;
114 }
115 }
116 assert_eq!(overlap, 10, "expected 10 of 15 compositions to overlap the mass table");
118 assert!(atomic_composition(21).is_some() && atomic_composition(9_999_999).is_none());
119 }
120
121 #[test]
122 fn ids_are_unique_and_lookup_works() {
123 let mut ids: Vec<u32> = BUILTIN.iter().map(|m| m.unimod_id).collect();
124 let n = ids.len();
125 ids.sort_unstable();
126 ids.dedup();
127 assert_eq!(ids.len(), n, "duplicate unimod ids");
128 assert_eq!(by_id(21).unwrap().name, "Phospho");
129 assert_eq!(by_id(999999), None);
130 }
131}