Skip to content

Commit

Permalink
Refactorings + fixed wrong msrv
Browse files Browse the repository at this point in the history
  • Loading branch information
douweschulte committed Nov 1, 2023
1 parent 0d8bac7 commit a9d9c8a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
69 changes: 27 additions & 42 deletions src/complex_peptide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -136,8 +134,8 @@ impl ComplexPeptide {
location.start + index + 1,
end_index - 1 - index,
),
))
}
)
})
})
.flat_err()
.map_err(|err| {
Expand Down Expand Up @@ -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(',') {
Expand Down Expand Up @@ -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::<Result<_, CustomError>>()?;
ambiguous_lookup.extend(ambiguous_mods);
Expand All @@ -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;
}
Expand All @@ -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()?,
);
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 13 additions & 2 deletions src/modification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down Expand Up @@ -312,6 +312,17 @@ pub enum ReturnModification {
Preferred(usize, Option<f64>),
}

impl ReturnModification {
/// Force this modification to be defined
#[must_use]
pub fn defined(self) -> Option<Modification> {
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 {
Expand Down Expand Up @@ -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(())
}
Expand Down
14 changes: 14 additions & 0 deletions src/shared/ontology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub enum Ontology {
Unimod,
/// PSI-MOD
Psimod,
/// GNOme
Gnome,
}

impl Ontology {
Expand All @@ -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",
}
}
}
Expand All @@ -27,6 +40,7 @@ impl std::fmt::Display for Ontology {
match self {
Self::Unimod => "Unimod",
Self::Psimod => "PSI-MOD",
Self::Gnome => "GNOme",
},
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit a9d9c8a

Please sign in to comment.