mzcore 0.2.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
496
497
498
499
500
501
502
503
504
505
//! Code to handle the RESID ontology
use std::io::Read;

use context_error::{BoxedError, Context, CreateError, FullErrorContent, combine_error};
use mzcv::{CVError, CVFile, CVSource, CVVersion, HashBufReader};
use roxmltree::*;

use crate::{
    chemistry::MolecularFormula,
    ontology::{
        Ontology,
        ontology_modification::{ModData, OntologyModification},
    },
    sequence::{
        AminoAcid, LinkerSpecificity, PlacementRule, Position, SimpleModification,
        SimpleModificationInner,
    },
};

/// RESID modifications
///
/// Note that because RESID is stored on a ftp server the automatic downloading does not work.
#[allow(missing_copy_implementations, missing_debug_implementations)]
pub struct Resid {}

impl CVSource for Resid {
    type Data = SimpleModificationInner;
    type Structure = Vec<SimpleModification>;

    fn cv_name() -> &'static str {
        "RESID"
    }

    fn files() -> &'static [CVFile] {
        &[CVFile {
            name: "RESID",
            extension: "xml",
            url: None, /* FTP is not supported but for when it is: ftp://ftp.proteininformationresource.org/pir_databases/other_databases/resid/RESIDUES.XML */
            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/resid.dat"),
                Configuration::default(),
            )
            .unwrap()
            .0;
            Some(cache)
        }
        #[cfg(feature = "internal-no-data")]
        None
    }

    fn parse(
        mut reader: impl Iterator<Item = HashBufReader<Box<dyn Read>, impl sha2::Digest>>,
    ) -> Result<
        (
            CVVersion,
            Self::Structure,
            Vec<BoxedError<'static, CVError>>,
        ),
        Vec<BoxedError<'static, CVError>>,
    > {
        let mut reader = reader.next().unwrap();
        let mut buf = String::new();
        reader.read_to_string(&mut buf).map_err(|e| {
            vec![BoxedError::small(
                CVError::FileCouldNotBeOpenend,
                "Could not read file",
                e.to_string(),
            )]
        })?;
        let document = Document::parse_with_options(&buf, ParsingOptions {
            allow_dtd: true,
            ..Default::default()
        })
        .map_err(|err| {
            vec![BoxedError::new(
                CVError::FileCouldNotBeParsed,
                "Invalid XML",
                "The given RESID file does not contain valid XML",
                Context::default().lines(0, err.to_string()),
            )]
        })?;
        let mut errors = Vec::new();
        let mut modifications: Vec<OntologyModification> = Vec::new();
        let database = document.root().first_child().ok_or_else(|| {
            vec![BoxedError::new(
                CVError::FileCouldNotBeParsed,
                "No root node in RESID XML",
                "A Database node should be present as the root in a RESID XML file",
                Context::default(),
            )]
        })?;

        'entry: for entry in database.children() {
            if entry.has_tag_name("Entry") {
                let mut modification = OntologyModification::default();
                let mut rules = Vec::new();

                modification.ontology = Ontology::Resid;
                let Some(id) = entry.attribute("id").and_then(|id| id.parse().ok()) else {
                    combine_error(
                        &mut errors,
                        BoxedError::new(
                            CVError::ItemError,
                            "Invalid ID",
                            "A RESID ID should be present and of shape 'AA0000'",
                            Context::default().byte_range(entry.range()),
                        ),
                    );
                    continue;
                };
                modification.id = id;
                let mut formula_found = false;
                for data_block in entry.children() {
                    match data_block.tag_name().name() {
                        "Names" => {
                            for name_node in data_block.children() {
                                match name_node.tag_name().name() {
                                    "Name" => {
                                        modification.name =
                                            name_node.text().unwrap_or_default().into();
                                    }
                                    "AlternateName" | "SystematicName" => {
                                        modification.synonyms.push((
                                            mzcv::SynonymScope::Exact,
                                            name_node.text().unwrap_or_default().into(),
                                        ));
                                    }
                                    "Xref" => {
                                        if let Some((a, b)) =
                                            name_node.text().unwrap_or_default().split_once(':')
                                        {
                                            modification.cross_ids.push(
                                                (Some(a.into()), b.into())
                                                    .try_into()
                                                    .expect("Invalid cross-id"),
                                            );
                                        } else {
                                            combine_error(
                                                &mut errors,
                                                BoxedError::new(
                                                    CVError::ItemError,
                                                    "Invalid xref",
                                                    "A RESID xref should be of shape TYPE:VALUE",
                                                    Context::default()
                                                        .byte_range(name_node.range())
                                                        .lines(
                                                            0,
                                                            format!("RESID:{}", modification.id),
                                                        ),
                                                ),
                                            );
                                        }
                                    }
                                    tag if tag.trim().is_empty() => (),
                                    _ => combine_error(
                                        &mut errors,
                                        BoxedError::new(
                                            CVError::ItemError,
                                            "Invalid name",
                                            "A RESID name should be Name, AlternateName, SystemicName, or Xref",
                                            Context::default()
                                                .byte_range(name_node.range())
                                                .lines(0, format!("RESID:{}", modification.id)),
                                        ),
                                    ),
                                }
                            }
                        }
                        "CorrectionBlock" => {
                            if formula_found {
                                combine_error(
                                    &mut errors,
                                    BoxedError::new(
                                        CVError::ItemWarning,
                                        "Multiple formulas",
                                        "Modifications with formulas (CorrectionBlock) that differ based on the used amino acid are ignored for now",
                                        Context::default()
                                            .byte_range(data_block.range())
                                            .lines(0, format!("RESID:AA{:04}", modification.id)),
                                    ),
                                );
                                continue 'entry;
                            }
                            for formula_node in data_block.children() {
                                if formula_node.has_tag_name("Formula") {
                                    match MolecularFormula::resid_single(
                                        formula_node.text().unwrap_or_default(),
                                    ) {
                                        Ok(f) => {
                                            formula_found = true;
                                            modification.formula = f;
                                        }
                                        Err(err) => combine_error(
                                            &mut errors,
                                            err.convert::<CVError, BoxedError<'_, CVError>>(|_| {
                                                CVError::ItemError
                                            })
                                            .to_owned(),
                                        ),
                                    }
                                }
                            }
                        }
                        "ReferenceBlock" => {
                            for ref_node in data_block.children() {
                                if ref_node.has_tag_name("Xref") {
                                    if let Some((a, b)) =
                                        ref_node.text().unwrap_or_default().split_once(':')
                                    {
                                        modification.cross_ids.push(
                                            (Some(a.into()), b.into())
                                                .try_into()
                                                .expect("Invalid cross-id"),
                                        );
                                    } else {
                                        combine_error(
                                            &mut errors,
                                            BoxedError::new(
                                                CVError::ItemError,
                                                "Invalid xref",
                                                "A RESID xref should be of shape TYPE:VALUE",
                                                Context::default()
                                                    .byte_range(ref_node.range())
                                                    .lines(
                                                        0,
                                                        format!("RESID:AA{:04}", modification.id),
                                                    ),
                                            ),
                                        );
                                    }
                                }
                            }
                        }
                        "Comment" => {
                            modification.description = format!(
                                "{}{}",
                                modification.description,
                                data_block.text().unwrap_or_default()
                            )
                            .into_boxed_str();
                        }
                        "SequenceCode" => {
                            let mut rule =
                                (AminoAcid::Alanine, None, None, data_block.attribute("link"));
                            for rule_node in data_block.children() {
                                match rule_node.tag_name().name() {
                                    "SequenceSpec" => {
                                        let txt = rule_node.text().unwrap_or_default();
                                        if let Some((a, b)) = txt.split_once(", ") {
                                            if b.contains(',') {
                                                combine_error(
                                                    &mut errors,
                                                    BoxedError::new(
                                                        CVError::ItemWarning,
                                                        "Higher order cross-linker",
                                                        "Only cross-linkers with two sites are supported for now",
                                                        Context::default()
                                                            .byte_range(rule_node.range())
                                                            .lines(
                                                                0,
                                                                format!(
                                                                    "RESID:AA{:04} {txt}",
                                                                    modification.id
                                                                ),
                                                            ),
                                                    ),
                                                );
                                                continue 'entry; // Ignore any cross-link > 2
                                            }
                                            if let Ok(a) = AminoAcid::try_from(a.trim()) {
                                                rule.0 = a;
                                            } else {
                                                combine_error(
                                                    &mut errors,
                                                    BoxedError::new(
                                                        CVError::ItemWarning,
                                                        "Invalid amino acid",
                                                        "Placement rules can only contain amino acids",
                                                        Context::default()
                                                            .byte_range(rule_node.range())
                                                            .lines(0, a.to_string()),
                                                    ),
                                                );
                                                continue 'entry;
                                            }
                                            if let Ok(b) = AminoAcid::try_from(b.trim()) {
                                                rule.1 = Some(b);
                                            } else {
                                                combine_error(
                                                    &mut errors,
                                                    BoxedError::new(
                                                        CVError::ItemWarning,
                                                        "Invalid amino acid",
                                                        "Placement rules can only contain amino acids",
                                                        Context::default()
                                                            .byte_range(rule_node.range())
                                                            .lines(0, b.to_string()),
                                                    ),
                                                );
                                                continue 'entry;
                                            }
                                        } else if let Ok(a) = AminoAcid::try_from(txt.trim()) {
                                            rule.0 = a;
                                        } else {
                                            combine_error(
                                                &mut errors,
                                                BoxedError::new(
                                                    CVError::ItemWarning,
                                                    "Invalid amino acid",
                                                    "Placement rules can only contain amino acids",
                                                    Context::default()
                                                        .byte_range(rule_node.range())
                                                        .lines(0, txt.to_string()),
                                                ),
                                            );
                                            continue 'entry;
                                        }
                                    }
                                    "Condition" => match rule_node.text().unwrap_or_default() {
                                        "amino-terminal" => rule.2 = Some(Position::AnyNTerm),
                                        "carboxyl-terminal" => rule.2 = Some(Position::AnyCTerm),
                                        "carboxamidine" => (),
                                        text if text.starts_with("cross-link")
                                            || text.starts_with("incidental")
                                            || text.starts_with("secondary") => {} // Ignore
                                        _ => combine_error(
                                            &mut errors,
                                            BoxedError::new(
                                                CVError::ItemError,
                                                "Invalid condition position",
                                                "A RESID condition position should be amino-terminal, carboxy-terminal, carboxamidine, cross-link, incidental, or secondary",
                                                Context::default()
                                                    .byte_range(rule_node.range())
                                                    .lines(
                                                        0,
                                                        format!("RESID:AA{:04}", modification.id),
                                                    ),
                                            ),
                                        ),
                                    },
                                    "Xref" => {
                                        if let Some((a, b)) =
                                            rule_node.text().unwrap_or_default().split_once(':')
                                        {
                                            modification.cross_ids.push(
                                                (Some(a.into()), b.into())
                                                    .try_into()
                                                    .expect("Invalid cross-id"),
                                            );
                                        } else {
                                            combine_error(
                                                &mut errors,
                                                BoxedError::new(
                                                    CVError::ItemError,
                                                    "Invalid xref",
                                                    "A RESID xref should be of shape TYPE:VALUE",
                                                    Context::default()
                                                        .byte_range(rule_node.range())
                                                        .lines(
                                                            0,
                                                            format!(
                                                                "RESID:AA{:04}",
                                                                modification.id
                                                            ),
                                                        ),
                                                ),
                                            );
                                        }
                                    }
                                    _ => (),
                                }
                            }
                            rules.push(rule);
                        } // Placement rules
                        "Features" => {
                            for feature in data_block.children() {
                                if feature.tag_name().name() == "Feature"
                                    && feature.attribute("type") == Some("UniProt")
                                    && let Some(text) = feature.text()
                                    && let Some(m) = text.strip_prefix("MOD_RES ")
                                {
                                    modification.cross_ids.push(
                                        (Some("UniProt".into()), m.trim().into())
                                            .try_into()
                                            .expect("Invalid cross-id"),
                                    );
                                }
                            }
                        }
                        _ => (),
                    }
                }

                let mut data = None;
                for rule in rules {
                    if data.is_none() {
                        if rule.1.is_none() {
                            data = Some(ModData::Mod {
                                specificities: Vec::new(),
                            });
                        } else {
                            data = Some(ModData::Linker {
                                length: crate::sequence::LinkerLength::Unknown,
                                specificities: Vec::new(),
                            });
                        }
                    }
                    if let (Some(ModData::Linker { specificities, .. }), Some(aa)) =
                        (&mut data, rule.1)
                    {
                        if rule.0 == aa {
                            specificities.push(LinkerSpecificity::Symmetric {
                                rules: vec![PlacementRule::AminoAcid(
                                    vec![rule.0].into(),
                                    rule.2.unwrap_or(Position::Anywhere),
                                )],
                                stubs: Vec::new(),
                                neutral_losses: Vec::new(),
                                diagnostic: Vec::new(),
                            });
                        } else {
                            specificities.push(LinkerSpecificity::Asymmetric {
                                rules: (
                                    vec![PlacementRule::AminoAcid(
                                        vec![rule.0].into(),
                                        rule.2.unwrap_or(Position::Anywhere),
                                    )],
                                    vec![PlacementRule::AminoAcid(
                                        vec![aa].into(),
                                        rule.2.unwrap_or(Position::Anywhere),
                                    )],
                                ),
                                stubs: Vec::new(),
                                neutral_losses: Vec::new(),
                                diagnostic: Vec::new(),
                            });
                        }
                    } else if let (Some(ModData::Mod { specificities }), None) = (&mut data, rule.1)
                    {
                        specificities.push((
                            vec![PlacementRule::AminoAcid(
                                vec![rule.0].into(),
                                rule.2.unwrap_or(Position::Anywhere),
                            )],
                            Vec::new(),
                            Vec::new(),
                        ));
                    } else {
                        combine_error(
                            &mut errors,
                            BoxedError::new(
                                CVError::ItemError,
                                "Both cross-linker and normal modification",
                                "This modification contains information for a normal modification and for a cross-linker",
                                Context::default()
                                    .byte_range(entry.range())
                                    .lines(0, format!("RESID:AA{:04}", modification.id)),
                            ),
                        );
                        continue 'entry;
                    }
                }

                modification.data = data.unwrap_or_default();
                modifications.push(modification);
            }
        }

        Ok((
            CVVersion {
                last_updated: database.attribute("date").map(|date| {
                    let mut res = date.split('-');
                    let day = res.next().and_then(|d| d.parse::<u8>().ok()).unwrap();
                    let month = res
                        .next()
                        .and_then(|m| {
                            let lower = m.to_ascii_lowercase();
                            [
                                "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep",
                                "oct", "nov", "dec",
                            ]
                            .iter()
                            .position(|n| *n == lower)
                        })
                        .and_then(|p| u8::try_from(p).ok())
                        .unwrap();
                    let year = res.next().and_then(|d| d.parse::<u16>().ok()).unwrap();
                    (year, month, day, 0, 0)
                }),
                version: database.attribute("release").map(ToString::to_string),
                hash: reader.hash(),
            },
            OntologyModification::finish(modifications),
            errors,
        ))
    }
}