Skip to content

Commit a70bb61

Browse files
committed
Bit of cleanup
1 parent fa7fea3 commit a70bb61

5 files changed

Lines changed: 61 additions & 38 deletions

File tree

mzcore/src/chemistry/neutral_loss.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ impl ParseJson for DiagnosticIon {
144144
}
145145
}
146146

147+
impl From<MolecularFormula> for DiagnosticIon {
148+
fn from(value: MolecularFormula) -> Self {
149+
Self(value)
150+
}
151+
}
152+
147153
impl From<MolecularFormula> for NeutralLoss {
148154
fn from(value: MolecularFormula) -> Self {
149155
let (pos, neg, gcd) = value.elements.iter().fold(

mzcore/src/ontology/ontology_modification.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use crate::{
99
use context_error::{BoxedError, Context, CreateError, combine_error};
1010
use itertools::Itertools;
1111
use mzcv::{
12-
AccessionCode, AccessionCodeParseError, CVError, OboIdentifier, RelationType, SynonymScope,
12+
AccessionCode, AccessionCodeParseError, CVError, Comment, Modifier, OboIdentifier,
13+
RelationType, SynonymScope,
1314
};
1415
use thin_vec::ThinVec;
1516

@@ -178,12 +179,7 @@ impl OntologyModification {
178179

179180
pub(crate) fn add_relationships(
180181
&mut self,
181-
relationships: &[(
182-
RelationType,
183-
OboIdentifier,
184-
Vec<(Box<str>, Box<str>)>,
185-
Option<Box<str>>,
186-
)],
182+
relationships: &[(RelationType, OboIdentifier, Vec<Modifier>, Comment)],
187183
) -> Vec<BoxedError<'static, CVError>> {
188184
let mut errors = Vec::new();
189185

mzcore/src/ontology/xlmod.rs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::collections::HashMap;
77
use thin_vec::ThinVec;
88

99
use mzcv::{
10-
CVError, CVFile, CVSource, CVVersion, HashBufReader, OboIdentifier, OboOntology, OboStanzaType,
11-
OboValue, RelationType,
10+
CVError, CVFile, CVSource, CVVersion, Comment, HashBufReader, Modifier, OboIdentifier,
11+
OboOntology, OboStanzaType, OboValue, RelationType,
1212
};
1313

1414
use crate::{
@@ -148,7 +148,7 @@ impl CVSource for XlMod {
148148
}
149149

150150
if properties.sites == Some(2) {
151-
let mut m =
151+
let mut m =
152152
OntologyModification {
153153
formula: properties.formula.unwrap_or_default(),
154154
name,
@@ -186,7 +186,7 @@ impl CVSource for XlMod {
186186
m.add_relationships(&obj.relationship);
187187
mods.push(m);
188188
} else if properties.sites == Some(1) {
189-
let mut m =
189+
let mut m =
190190
OntologyModification {
191191
formula: properties.formula.unwrap_or_default(),
192192
name,
@@ -245,10 +245,7 @@ struct Properties {
245245
}
246246

247247
fn parse_property_values(
248-
property_values: &HashMap<
249-
Box<str>,
250-
Vec<(OboValue, Vec<(Box<str>, Box<str>)>, Option<Box<str>>)>,
251-
>,
248+
property_values: &HashMap<Box<str>, Vec<(OboValue, Vec<Modifier>, Comment)>>,
252249
properties: &mut Properties,
253250
id: OboIdentifier,
254251
) -> Vec<BoxedError<'static, CVError>> {
@@ -471,9 +468,16 @@ fn parse_property_values(
471468
}
472469
"neutralLossFormula" => {
473470
for (def, _, _) in value {
474-
properties
475-
.neutral_losses
476-
.push(MolecularFormula::xlmod(&def.to_string()).unwrap().into());
471+
match MolecularFormula::xlmod(&def.to_string()) {
472+
Ok(v) => properties.neutral_losses.push(v.into()),
473+
Err(err) => combine_error(
474+
&mut errors,
475+
err.to_owned()
476+
.convert::<CVError, BoxedError<'static, CVError>>(|_| {
477+
CVError::ItemError
478+
}),
479+
),
480+
}
477481
}
478482
}
479483
"bridgeFormula" => {
@@ -489,8 +493,11 @@ fn parse_property_values(
489493
);
490494
}
491495
properties.sites = properties.sites.or(Some(2));
492-
properties.formula =
493-
Some(MolecularFormula::xlmod(&value[0].0.to_string()).unwrap());
496+
properties.formula = MolecularFormula::xlmod(&value[0].0.to_string())
497+
.map_err(|e| {
498+
combine_error(&mut errors, e.to_owned().convert(|_| CVError::ItemError));
499+
})
500+
.ok();
494501
}
495502
"specificities" => {
496503
// specificities: "(C,U)" xsd:string
@@ -599,9 +606,16 @@ fn parse_property_values(
599606
}
600607
"reporterFormula" => {
601608
for (def, _, _) in value {
602-
properties.diagnostic_ions.push(DiagnosticIon(
603-
MolecularFormula::xlmod(&def.to_string()).unwrap(),
604-
));
609+
match MolecularFormula::xlmod(&def.to_string()) {
610+
Ok(v) => properties.diagnostic_ions.push(v.into()),
611+
Err(err) => combine_error(
612+
&mut errors,
613+
err.to_owned()
614+
.convert::<CVError, BoxedError<'static, CVError>>(|_| {
615+
CVError::ItemError
616+
}),
617+
),
618+
}
605619
}
606620
}
607621
"stubDefinition" | "stubFormula" => {
@@ -610,23 +624,24 @@ fn parse_property_values(
610624
// TODO: Extend the stub logic to handle the techniques and neutral losses
611625
for (def, _, _) in value {
612626
if let OboValue::String(definition) = &def {
613-
let (techniques, definition) = definition.split_once('=').unwrap();
627+
let (_techniques, definition) = definition.split_once('=').unwrap();
614628
let (first, second) = definition.split_once(':').unwrap();
615629
let mut split_first = first.split(',');
616630
let mut split_second = second.split(',');
617-
let formula_first =
618-
split_first.next().map_or(MolecularFormula::default(), |v| {
631+
let formula_first = split_first
632+
.next()
633+
.map_or_else(MolecularFormula::default, |v| {
619634
MolecularFormula::xlmod(v).unwrap()
620635
});
621-
let losses_first: Vec<NeutralLoss> = split_first
636+
let _losses_first: Vec<NeutralLoss> = split_first
622637
.map(|v| MolecularFormula::xlmod(v).unwrap().into())
623638
.collect();
624639
let formula_second = split_second
625640
.next()
626-
.map_or(MolecularFormula::default(), |v| {
641+
.map_or_else(MolecularFormula::default, |v| {
627642
MolecularFormula::xlmod(v).unwrap()
628643
});
629-
let losses_second: Vec<NeutralLoss> = split_second
644+
let _losses_second: Vec<NeutralLoss> = split_second
630645
.map(|v| MolecularFormula::xlmod(v).unwrap().into())
631646
.collect();
632647
properties.stubs.push((formula_first, formula_second));

mzcv/src/curie.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ impl std::fmt::Display for CURIEParsingError {
12751275

12761276
/// An accession code, Can either be a numeric code (u32 to 4 milion, so 9 fully utilised digits).
12771277
/// Or it can be an ASCII alphanumeric code (case-sensitive) of 1 to 8 characters.
1278-
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
1278+
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
12791279
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
12801280
pub enum AccessionCode {
12811281
/// A strictly numeric code

mzcv/src/obo.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub struct OboStanza {
4848
}
4949

5050
/// An Obo relation type.
51-
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
51+
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
5252
pub enum RelationType {
5353
/// A relationship set with an `is_a` line
5454
#[default]
@@ -150,9 +150,11 @@ impl From<OboIdentifier> for RawOboID {
150150
}
151151
}
152152

153-
type Modifier = (Box<str>, Box<str>);
153+
/// A modifier from an Obo line
154+
pub type Modifier = (Box<str>, Box<str>);
154155

155-
type Comment = Option<Box<str>>;
156+
/// A possible comment from an Obo line
157+
pub type Comment = Option<Box<str>>;
156158

157159
/// A synonym in an Obo stanza
158160
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
@@ -730,7 +732,7 @@ pub enum OboError {
730732
InvalidLine,
731733
/// If a synonym line is invalid
732734
InvalidSynonym,
733-
/// If an xref is invalid
735+
/// If a xref is invalid
734736
InvalidXref,
735737
/// If a relationship is invalid
736738
InvalidRelationship,
@@ -759,12 +761,14 @@ impl ErrorKind for OboError {
759761
}
760762
}
761763

762-
// Tokenise a value line, the `<value>` part is left untouched the trailing modifiers and comment are unescaped.
763-
// `<value> {<trailing modifiers>} ! <comment>`
764+
/// Tokenise a value line, the `<value>` part is left untouched the trailing modifiers and comment are not escaped.
765+
/// `<value> {<trailing modifiers>} ! <comment>`
766+
/// # Errors
767+
/// If a bracket was not closed, or if a modifier was malformed.
764768
fn tokenise_obo_value_line<'a>(
765769
text: &'a str,
766770
context: Context<'static>,
767-
) -> Result<(&'a str, Vec<(Box<str>, Box<str>)>, Option<Box<str>>), BoxedError<'static, OboError>> {
771+
) -> Result<(&'a str, Vec<Modifier>, Comment), BoxedError<'static, OboError>> {
768772
let mut trailing_start = None;
769773
let mut comment_start = None;
770774
let mut enclosed: Option<(char, usize)> = None;
@@ -845,6 +849,8 @@ fn tokenise_obo_value_line<'a>(
845849
}
846850

847851
/// Split into parts with the enclosing characters
852+
/// # Errors
853+
/// If an enclosing sequence was not closed. Returns the bracket that was not closed.
848854
fn tokenise(text: &str) -> Result<Vec<(Option<char>, &str)>, char> {
849855
let mut parts = Vec::new();
850856
let mut enclosed = None;
@@ -894,6 +900,6 @@ fn tokenise(text: &str) -> Result<Vec<(Option<char>, &str)>, char> {
894900
fn parse_dbxref(text: &str) -> Vec<RawOboID> {
895901
text.split(',')
896902
.filter(|s| !s.is_empty())
897-
.map(|id| OboIdentifier::from_str(id).unwrap().into())
903+
.map(|id| OboIdentifier::from(id).into())
898904
.collect()
899905
}

0 commit comments

Comments
 (0)