mzcore 0.1.0

Core logic for handling massspectrometry in Rust.
Documentation
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Code to handle the GNOme ontology
use std::collections::HashMap;

use context_error::{
    BoxedError, CreateError, FullErrorContent, StaticErrorContent, combine_errors,
};
use thin_vec::ThinVec;

use mzcv::{CVError, CVFile, CVSource, CVVersion, HashBufReader, OboOntology, OboStanzaType};

use crate::{
    glycan::{GlycanStructure, MonoSaccharide},
    ontology::Ontology,
    sequence::{
        GnoComposition, GnoSubsumption, ModificationId, SimpleModification, SimpleModificationInner,
    },
};

/// GNOme modifications
///
/// These integrate the GNOme ontology with the structures from glycosmos.
#[allow(missing_copy_implementations, missing_debug_implementations)]
pub struct Gnome {}

impl CVSource for Gnome {
    type Data = SimpleModificationInner;
    type Structure = Vec<SimpleModification>;
    fn cv_name() -> &'static str {
        "GNOme"
    }

    fn files() -> &'static [CVFile] {
        &[
            CVFile {
                name: "GNOme",
                extension: "obo",
                url: Some("https://purl.obolibrary.org/obo/gno.obo"),
                compression: mzcv::CVCompression::None,
            },
            CVFile {
                name: "glycosmos_glycans_list",
                extension: "csv",
                url: Some("https://glycosmos.org/download/glycosmos_glycans_list.csv"),
                compression: mzcv::CVCompression::None,
            },
        ]
    }

    fn static_data() -> Option<(CVVersion, Self::Structure)> {
        #[cfg(not(feature = "internal-no-data"))]
        {
            use bincode::config::Configuration;
            let cache = bincode::decode_from_slice::<(CVVersion, Self::Structure), Configuration>(
                include_bytes!("../databases/gnome.dat"),
                Configuration::default(),
            )
            .unwrap()
            .0;
            Some(cache)
        }
        #[cfg(feature = "internal-no-data")]
        None
    }

    fn parse(
        mut readers: impl Iterator<Item = HashBufReader<Box<dyn std::io::Read>, impl sha2::Digest>>,
    ) -> Result<(CVVersion, Self::Structure), Vec<BoxedError<'static, CVError>>> {
        let reader = readers.next().unwrap();
        let (version, mut mods) = OboOntology::from_raw(reader)
            .map_err(|e| {
                vec![
                    BoxedError::small(
                        CVError::FileCouldNotBeParsed,
                        e.get_short_description(),
                        e.get_long_description(),
                    )
                    .add_contexts(e.get_contexts().iter().cloned()),
                ]
            })
            .map(|obo| (obo.version(), parse_gnome(obo)))?;
        let read_mods = mods.clone();
        let structures = parse_gnome_structures(readers.next().unwrap());

        // Fill all known info points
        for modification in mods.values_mut() {
            if modification.weight.is_none() {
                modification.weight = find_mass(&read_mods, modification.is_a.clone().into());
            }
            if let Some(structure) = structures.get(&modification.id.name) {
                modification.topology = structure.structure.clone();
                if let Some(chebi) = structure.chebi {
                    modification
                        .id
                        .cross_ids
                        .push((Some("ChEBI".into()), chebi.to_string().into_boxed_str()));
                }
                if let Some(pubchem) = structure.pubchem {
                    modification.id.cross_ids.push((
                        Some("PubChemCID".into()),
                        pubchem.to_string().into_boxed_str(),
                    ));
                }
                modification.motif = structure.motif.clone();
                modification.taxonomy = structure.taxonomy.clone().into();
                modification.glycomeatlas = structure.glycomeatlas.clone().into();
            } else if let Some(id) = &modification.topology_id {
                modification.topology = structures
                    .get(id.as_ref())
                    .and_then(|s| s.structure.clone());
            }
            if modification.composition.is_none()
                && let Some(composition_id) = &modification.composition_id
            {
                modification.composition = read_mods
                    .get(composition_id.as_ref())
                    .and_then(|g| g.composition.clone());
            }
        }

        Ok((
            version,
            mods.into_values()
                .filter_map(|m| m.try_into().ok())
                .map(std::sync::Arc::new)
                .collect(),
        ))
    }
}

fn find_mass(mods: &HashMap<Box<str>, GNOmeModification>, mut name: Box<str>) -> Option<f64> {
    let mut mass = None;
    while mass.is_none() {
        mass = mods.get(&name)?.weight;
        name.clone_from(&mods[&name].is_a);
    }
    mass
}

#[expect(dead_code)]
mod gnome_terms {
    pub(super) const SUBSUMPTION_MOLECULAR_WEIGHT: &str = "GNO:00000012";
    pub(super) const SUBSUMPTION_BASECOMPOSITION: &str = "GNO:00000013";
    pub(super) const SUBSUMPTION_COMPOSITION: &str = "GNO:00000014";
    pub(super) const SUBSUMPTION_TOPOLOGY: &str = "GNO:00000015";
    pub(super) const SUBSUMPTION_SACCHARIDE: &str = "GNO:00000016";

    pub(super) const HAS_SUBSUMPTION_CATEGORY: &str = "GNO:00000021";
    pub(super) const HAS_GLYTOUCAN_ID: &str = "GNO:00000022";
    pub(super) const HAS_GLYTOUCAN_LINK: &str = "GNO:00000023";
    pub(super) const IS_SUBSUMED_BY: &str = "GNO:00000024";
    pub(super) const IS_RESTRICTION_MEMBER: &str = "GNO:00000025";
    /// Is the basic composition the same, meaning the same monosaccharides (without isomeric information)
    pub(super) const HAS_BASECOMPOSITION: &str = "GNO:00000033";
    /// Is the composition the same, meaning the same monosaccharides
    pub(super) const HAS_COMPOSITION: &str = "GNO:00000034";
    /// Is the basic structure the same (if anomeric and linkage information are thrown overboard)
    pub(super) const HAS_TOPOLOGY: &str = "GNO:00000035";
    /// Is the linked structure the same (if anomeric and reducing end ring information are thrown overboard)
    pub(super) const HAS_ARCHETYPE: &str = "GNO:00000036";
    pub(super) const HAS_STRUCTURE_BROWSER_LINK: &str = "GNO:00000041";
    pub(super) const HAS_COMPOSITION_BROWSER_LINK: &str = "GNO:00000042";
    pub(super) const SHORTUCKB_COMPOSITION: &str = "GNO:00000101";
    /// Indicates the precision of the definition, lower is better, 0 is a fully defined glycan
    pub(super) const HAS_STRUCTURE_CHARACTERISATION_SCORE: &str = "GNO:00000102";
    pub(super) const HAS_BYONIC_NAME: &str = "GNO:00000202";
}

use gnome_terms::*;
use itertools::Itertools;

/// Get the GNO Subsumption level.
/// # Errors
/// If this is not a known level.
fn gno_subsumption_from_str(s: &str) -> Result<GnoSubsumption, ()> {
    match s {
        SUBSUMPTION_MOLECULAR_WEIGHT => Ok(GnoSubsumption::AverageWeight),
        SUBSUMPTION_BASECOMPOSITION => Ok(GnoSubsumption::BaseComposition),
        SUBSUMPTION_COMPOSITION => Ok(GnoSubsumption::Composition),
        SUBSUMPTION_TOPOLOGY => Ok(GnoSubsumption::Topology),
        SUBSUMPTION_SACCHARIDE => Ok(GnoSubsumption::Saccharide),
        _ => Err(()),
    }
}

/// Parse the GNOme ontology .obo file
/// # Errors
/// If the file is not valid.
fn parse_gnome(obo: OboOntology) -> HashMap<Box<str>, GNOmeModification> {
    let mut mods = HashMap::new();
    let mut errors = Vec::new();

    for obj in obo.objects {
        if obj.stanza_type != OboStanzaType::Term || !obj.lines.contains_key("is_a") {
            continue;
        }

        let modification = GNOmeModification {
            id: ModificationId::new(
                Ontology::Gnome,
                obj.id.1,
                None,
                Box::default(), // Description is never useful (either contains the mass or contains the ID never anything unique)
                obj.synonyms
                    .iter()
                    .map(|s| (s.scope, s.synonym.clone()))
                    .collect(),
                obj.property_values
                    .get(HAS_GLYTOUCAN_ID)
                    .map(|v| {
                        (
                            Some("GlyTouCan".into()),
                            v[0].0.to_string().into_boxed_str(),
                        )
                    })
                    .into_iter()
                    .chain(obj.property_values.get(HAS_GLYTOUCAN_LINK).map(|v| {
                        (
                            Some("GlyTouCanURL".into()),
                            v[0].0.to_string().into_boxed_str(),
                        )
                    }))
                    .chain(
                        obj.property_values
                            .get(HAS_COMPOSITION_BROWSER_LINK)
                            .map(|v| {
                                (
                                    Some("CompositionBrowser".into()),
                                    v[0].0.to_string().into_boxed_str(),
                                )
                            }),
                    )
                    .chain(
                        obj.property_values
                            .get(HAS_STRUCTURE_BROWSER_LINK)
                            .map(|v| {
                                (
                                    Some("StructureBrowser".into()),
                                    v[0].0.to_string().into_boxed_str(),
                                )
                            }),
                    )
                    .collect(),
                obj.obsolete,
            ),
            subsumption_level: obj
                .lines
                .get(HAS_SUBSUMPTION_CATEGORY)
                .map(|s| gno_subsumption_from_str(&s[0].0).unwrap())
                .unwrap_or_default(),
            structure_score: obj
                .lines
                .get(HAS_STRUCTURE_CHARACTERISATION_SCORE)
                .map_or(u16::MAX, |s| s[0].0.parse().unwrap()),
            is_a: obj.lines["is_a"][0].0.trim()[4..]
                .trim()
                .to_ascii_lowercase()
                .into_boxed_str(),
            composition_id: obj
                .property_values
                .get(HAS_COMPOSITION)
                .map(|lines| lines[0].0.to_string().into_boxed_str()),
            topology_id: obj
                .property_values
                .get(HAS_TOPOLOGY)
                .map(|lines| lines[0].0.to_string().into_boxed_str()),
            weight: obj
                .lines
                .get("name")
                .map(|e| &e[0])
                .filter(|(n, _, _)| n.len() > 30)
                .and_then(|(name, _, _)| name[27..name.len() - 3].parse::<f64>().ok()),
            composition: obj
                .property_values
                .get(SHORTUCKB_COMPOSITION)
                .and_then(|lines| {
                    match MonoSaccharide::pro_forma_composition::<false>(&lines[0].0.to_string()) {
                        Ok((v, _)) => Some(v),
                        Err(e) => {
                            combine_errors(
                                &mut errors,
                                e.into_iter().map(BoxedError::to_owned),
                                (),
                            );
                            None
                        }
                    }
                }),
            topology: None, // Will be looked up later
            motif: None,
            taxonomy: ThinVec::new(),
            glycomeatlas: ThinVec::new(),
        };

        mods.insert(modification.id.name.clone(), modification);
    }

    for error in errors {
        println!("{error}");
    }

    mods
}

#[derive(Debug)]
struct GlycosmosList {
    structure: Option<GlycanStructure>,
    motif: Option<(Box<str>, Box<str>)>,
    chebi: Option<usize>,
    pubchem: Option<usize>,
    taxonomy: Vec<(Box<str>, usize)>,
    glycomeatlas: ThinVec<(Box<str>, ThinVec<(Box<str>, Box<str>)>)>,
}

/// Parse the glycosmos glycan structures .csv file
/// # Panics
/// If the file is not valid.
fn parse_gnome_structures(
    file: HashBufReader<Box<dyn std::io::Read>, impl sha2::Digest>,
) -> HashMap<Box<str>, GlycosmosList> {
    let mut glycans = HashMap::new();
    let mut errors = 0;
    for line in crate::csv::parse_csv_raw(file, b',', None, None).unwrap() {
        let line = line.unwrap();

        glycans.insert(
            line.index_column("accession number").unwrap().0.into(),
            GlycosmosList {
                structure: line
                    .index_column("iupac condensed")
                    .ok()
                    .filter(|p| !p.0.is_empty())
                    .and_then(|(_, range)| {
                        match GlycanStructure::from_short_iupac(
                            line.line(),
                            range.clone(),
                            (line.line_index() + 1) as u32,
                        ) {
                            Ok(glycan) => Some(glycan),
                            Err(error) => {
                                if errors < 5 {
                                    println!("{error}");
                                }
                                errors += 1;
                                None
                            }
                        }
                    }),
                motif: line
                    .index_column("motif name(s)")
                    .ok()
                    .filter(|p| !p.0.is_empty())
                    .and_then(|p| p.0.split_once(':'))
                    .map(|(n, i)| (n.into(), i.to_ascii_lowercase().into_boxed_str())),
                chebi: line
                    .index_column("chebi")
                    .ok()
                    .map(|p| p.0.to_string())
                    .filter(|p| !p.is_empty())
                    .and_then(|p| p.parse::<usize>().ok()),
                pubchem: line
                    .index_column("pubchem cid")
                    .ok()
                    .map(|p| p.0.to_string())
                    .filter(|p| !p.is_empty())
                    .and_then(|p| p.parse::<usize>().ok()),
                taxonomy: line
                    .index_column("taxonomy")
                    .ok()
                    .filter(|p| !p.0.is_empty())
                    .into_iter()
                    .flat_map(|p| {
                        p.0.split(',').map(|s| {
                            s.rsplit_once(':')
                                .map(|(n, i)| (n.into(), i.parse::<usize>().unwrap()))
                                .unwrap()
                        })
                    })
                    .collect(),
                glycomeatlas: line
                    .index_column("glycomeatlas")
                    .ok()
                    .filter(|p| !p.0.is_empty())
                    .map(|p| p.0)
                    .into_iter()
                    .flat_map(|p| p.split(','))
                    .map(|p| p.split_once(':').unwrap())
                    .chunk_by(|(species, _)| (*species).to_string())
                    .into_iter()
                    .map(|(species, locations)| {
                        (
                            (*species).into(),
                            locations
                                .into_iter()
                                .map(|location| {
                                    location
                                        .1
                                        .trim_end_matches(')')
                                        .split_once('(')
                                        .map(|(l, o)| (l.into(), o.into()))
                                        .unwrap()
                                })
                                .collect(),
                        )
                    })
                    .collect(),
            },
        );
    }
    assert!(
        errors <= 0,
        "Total glycan structure reading errors: {errors} total read {}",
        glycans.len()
    );
    glycans
}

#[derive(Clone, Debug)]
struct GNOmeModification {
    id: ModificationId,
    /// subsumption level, indication to what extent this species is described
    subsumption_level: GnoSubsumption,
    /// id to prev in chain
    is_a: Box<str>,
    /// id of composition
    composition_id: Option<Box<str>>,
    /// id of topology
    topology_id: Option<Box<str>>,
    /// molecular weight if defined
    weight: Option<f64>,
    /// composition if defined
    composition: Option<Vec<(MonoSaccharide, isize)>>,
    /// structure if defined
    topology: Option<GlycanStructure>,
    /// The score for the structure (0 if fully defined, u16::MAX if not defined)
    structure_score: u16,
    /// The underlying glycan motifs
    motif: Option<(Box<str>, Box<str>)>,
    /// Taxonomy of where the glycan exists
    taxonomy: ThinVec<(Box<str>, usize)>,
    /// Locations of where the glycan exists
    glycomeatlas: ThinVec<(Box<str>, ThinVec<(Box<str>, Box<str>)>)>,
}

impl TryFrom<GNOmeModification> for SimpleModificationInner {
    type Error = ();
    fn try_from(value: GNOmeModification) -> Result<Self, ()> {
        Ok(Self::Gno {
            composition: if let Some(structure) = value.topology {
                GnoComposition::Topology(structure)
            } else if let Some(composition) = value.composition {
                GnoComposition::Composition(composition)
            } else if let Some(mass) = value.weight {
                GnoComposition::Weight(crate::system::f64::da(mass).into())
            } else {
                return Err(());
            },
            id: value.id,
            structure_score: value.structure_score,
            subsumption_level: value.subsumption_level,
            motif: value.motif,
            taxonomy: value.taxonomy,
            glycomeatlas: value.glycomeatlas,
        })
    }
}

impl PartialEq for GNOmeModification {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
            && self.subsumption_level == other.subsumption_level
            && self.is_a == other.is_a
            && self.composition_id == other.composition_id
            && self.topology_id == other.topology_id
            && (self.weight.is_none() && other.weight.is_none()
                || self
                    .weight
                    .is_some_and(|sw| other.weight.is_some_and(|ow| sw.total_cmp(&ow).is_eq())))
            && self.composition == other.composition
            && self.topology == other.topology
            && self.structure_score == other.structure_score
    }
}
impl Eq for GNOmeModification {}

impl PartialOrd for GNOmeModification {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for GNOmeModification {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.id.name.cmp(&other.id.name)
    }
}