From a9d9c8a83fbb866eeef34390513ebe3c5909d708 Mon Sep 17 00:00:00 2001 From: Douwe Schulte Date: Wed, 1 Nov 2023 13:59:50 +0100 Subject: [PATCH] Refactorings + fixed wrong msrv --- Cargo.toml | 2 +- src/complex_peptide.rs | 69 +++++++++++++++++------------------------- src/modification.rs | 15 +++++++-- src/shared/ontology.rs | 14 +++++++++ src/system.rs | 2 +- 5 files changed, 56 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a8e5bdb..2e82100 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ keywords = ["spectra", "massspectrometry", "ms", "fragmentation", "proforma"] repository = "https://github.com/snijderlab/rustyms" readme = "README.md" include = ["src/**/*", "databases/**/*", "LICENSE", "README.md", "build.rs"] -rust-version = "1.62.1" +rust-version = "1.70.0" [dependencies] uom = { version = "0.35", features = ["use_serde"] } diff --git a/src/complex_peptide.rs b/src/complex_peptide.rs index 6431055..aa46be0 100644 --- a/src/complex_peptide.rs +++ b/src/complex_peptide.rs @@ -124,10 +124,8 @@ impl ComplexPeptide { &mut ambiguous_lookup, ) .map(|m| { - if let ReturnModification::Defined(m) = m { - Ok(m) - } else { - Err(CustomError::error( + m.defined().ok_or_else(|| { + CustomError::error( "Invalid modification", "A modification in the sloppy peptide format cannot be ambiguous", Context::line( @@ -136,8 +134,8 @@ impl ComplexPeptide { location.start + index + 1, end_index - 1 - index, ), - )) - } + ) + }) }) .flat_err() .map_err(|err| { @@ -217,15 +215,13 @@ impl ComplexPeptide { let modification = Modification::try_from(line, index + 2..at_index - 2, &mut ambiguous_lookup) .map(|m| { - if let ReturnModification::Defined(m) = m { - Ok(m) - } else { - Err(CustomError::error( + m.defined().ok_or_else(|| { + CustomError::error( "Invalid global modification", "A global modification cannot be ambiguous", Context::line(0, line, index + 2, at_index - index - 4), - )) - } + ) + }) }) .flat_err()?; for aa in line[at_index..end_index].split(',') { @@ -282,15 +278,13 @@ impl ComplexPeptide { unknown_position_modifications = mods .into_iter() .map(|m| { - if let ReturnModification::Defined(def) = m { - Ok(def) - } else { - Err(CustomError::error( + m.defined().ok_or_else(|| { + CustomError::error( "Invalid unknown position modification", "An invalid position modification cannot be ambiguous", Context::full_line(0, line), - )) - } + ) + }) }) .collect::>()?; ambiguous_lookup.extend(ambiguous_mods); @@ -308,18 +302,15 @@ impl ComplexPeptide { peptide.labile.push( Modification::try_from(line, index + 1..end_index, &mut ambiguous_lookup) - .map(|m| { - if let ReturnModification::Defined(m) = m { - Ok(m) - } else { - Err(CustomError::error( + .and_then(|m| { + m.defined().ok_or_else(|| { + CustomError::error( "Invalid labile modification", "A labile modification cannot be ambiguous", Context::line(0, line, index + 1, end_index - 1 - index), - )) - } - }) - .flat_err()?, + ) + }) + })?, ); index = end_index + 1; } @@ -342,15 +333,13 @@ impl ComplexPeptide { peptide.n_term = Some( Modification::try_from(line, index + 1..end_index - 1, &mut ambiguous_lookup) .map(|m| { - if let ReturnModification::Defined(m) = m { - Ok(m) - } else { - Err(CustomError::error( + m.defined().ok_or_else(|| { + CustomError::error( "Invalid N terminal modification", "An N terminal modification cannot be ambiguous", Context::line(0, line, index + 1, end_index - 2 - index), - )) - } + ) + }) }) .flat_err()?, ); @@ -466,15 +455,11 @@ impl ComplexPeptide { index = end_index + 1; if c_term { peptide.c_term = - Some(if let ReturnModification::Defined(m) = modification { - Ok(m) - } else { - Err(CustomError::error( - "Invalid C terminal modification", - "A C terminal modification cannot be ambiguous", - Context::line(0, line, start_index, start_index - index - 1), - )) - }?); + Some(modification.defined().ok_or_else(|| CustomError::error( + "Invalid C terminal modification", + "A C terminal modification cannot be ambiguous", + Context::line(0, line, start_index, start_index - index - 1), + ))?); if index < chars.len() && chars[index] == b'+' { index+=1; // If a peptide in a multimeric definition contains a C terminal modification } diff --git a/src/modification.rs b/src/modification.rs index 2bcc1a3..4bdf8ee 100644 --- a/src/modification.rs +++ b/src/modification.rs @@ -206,7 +206,7 @@ fn parse_single_modification( .map(Some) .map_err(|_| basic_error .with_long_description("This modification cannot be read as a PSI-MOD name or numerical modification")), - ("gno", tail) => gnome_ontology().find_name(tail) + ("gno" | "g", tail) => gnome_ontology().find_name(tail) .map(Some) .ok_or_else(|| basic_error .with_long_description("This modification cannot be read as a GNO name")), @@ -312,6 +312,17 @@ pub enum ReturnModification { Preferred(usize, Option), } +impl ReturnModification { + /// Force this modification to be defined + #[must_use] + pub fn defined(self) -> Option { + match self { + Self::Defined(modification) => Some(modification), + _ => None, + } + } +} + /// An ambiguous modification which could be placed on any of a set of locations #[derive(Debug, Clone, PartialEq)] pub struct AmbiguousModification { @@ -362,7 +373,7 @@ impl Display for Modification { Self::Predefined(_, _, context, name, _) => { write!(f, "{}:{name}", context.char())?; } - Self::Gno(_, name) => write!(f, "{name}")?, + Self::Gno(_, name) => write!(f, "{}:{name}", Ontology::Gnome.char())?, } Ok(()) } diff --git a/src/shared/ontology.rs b/src/shared/ontology.rs index c88f228..bb5f45c 100644 --- a/src/shared/ontology.rs +++ b/src/shared/ontology.rs @@ -6,6 +6,8 @@ pub enum Ontology { Unimod, /// PSI-MOD Psimod, + /// GNOme + Gnome, } impl Ontology { @@ -15,6 +17,17 @@ impl Ontology { match self { Self::Unimod => 'U', Self::Psimod => 'M', + Self::Gnome => 'G', + } + } + + /// Get the accession number name for the ontology + #[allow(dead_code)] + pub const fn name(self) -> &'static str { + match self { + Self::Unimod => "UNIMOD", + Self::Psimod => "MOD", + Self::Gnome => "GNO", } } } @@ -27,6 +40,7 @@ impl std::fmt::Display for Ontology { match self { Self::Unimod => "Unimod", Self::Psimod => "PSI-MOD", + Self::Gnome => "GNOme", }, ) } diff --git a/src/system.rs b/src/system.rs index 25c9d9b..e3a7674 100644 --- a/src/system.rs +++ b/src/system.rs @@ -5,7 +5,7 @@ #![allow(clippy::ignored_unit_patterns)] use uom::*; -pub use f64::*; +pub use self::f64::*; /// The mass quantity in dalton #[macro_use]