Skip to content

Commit

Permalink
Updated API based on align-cli and changed (multi)chemical to have bo…
Browse files Browse the repository at this point in the history
…th 'formula(s)' and 'formula(s)_inner' for ease of use
  • Loading branch information
douweschulte committed Sep 10, 2024
1 parent 8887981 commit 292f7a4
Show file tree
Hide file tree
Showing 25 changed files with 258 additions and 215 deletions.
30 changes: 7 additions & 23 deletions rustyms-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ impl AminoAcid {
///
fn formulas(&self) -> Vec<MolecularFormula> {
self.0
.formulas(SequencePosition::default(), 0)
.formulas()
.iter()
.map(|f| MolecularFormula(f.clone()))
.collect()
Expand All @@ -474,13 +474,7 @@ impl AminoAcid {
/// List[MolecularFormula]
///
fn formula(&self) -> MolecularFormula {
MolecularFormula(
self.0
.formulas(SequencePosition::default(), 0)
.first()
.unwrap()
.clone(),
)
MolecularFormula(self.0.formulas().first().unwrap().clone())
}

/// Monoisotopic mass(es) of the amino acid.
Expand All @@ -493,7 +487,7 @@ impl AminoAcid {
///
fn monoisotopic_masses(&self) -> Vec<f64> {
self.0
.formulas(SequencePosition::default(), 0)
.formulas()
.iter()
.map(|f| f.monoisotopic_mass().value)
.collect()
Expand All @@ -508,12 +502,7 @@ impl AminoAcid {
/// float
///
fn monoisotopic_mass(&self) -> f64 {
self.0
.formulas(SequencePosition::default(), 0)
.first()
.unwrap()
.monoisotopic_mass()
.value
self.0.formulas().first().unwrap().monoisotopic_mass().value
}
}

Expand Down Expand Up @@ -565,7 +554,7 @@ impl SimpleModification {
/// MolecularFormula
///
fn formula(&self) -> MolecularFormula {
MolecularFormula(self.0.formula(SequencePosition::default(), 0))
MolecularFormula(self.0.formula())
}

/// Monoisotopic mass of the modification.
Expand All @@ -575,10 +564,7 @@ impl SimpleModification {
/// float
///
fn monoisotopic_mass(&self) -> f64 {
self.0
.formula(SequencePosition::default(), 0)
.monoisotopic_mass()
.value
self.0.formula().monoisotopic_mass().value
}
}

Expand Down Expand Up @@ -1170,9 +1156,7 @@ impl LinearPeptide {
///
#[getter]
fn charge(&self) -> Option<isize> {
self.0
.get_charge_carriers()
.map(|c| c.formula(SequencePosition::default(), 0).charge().value)
self.0.get_charge_carriers().map(|c| c.charge().value)
}

/// The adduct ions, if specified.
Expand Down
3 changes: 1 addition & 2 deletions rustyms/benches/iai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use iai_callgrind::{

#[inline(never)]
fn setup(a: &str, b: &str) -> (LinearPeptide<SimpleLinear>, LinearPeptide<SimpleLinear>) {
let _force_elements_init =
black_box(AminoAcid::Alanine.formulas(SequencePosition::default(), 0));
let _force_elements_init = black_box(AminoAcid::Alanine.formulas());
(
LinearPeptide::pro_forma(a, None)
.unwrap()
Expand Down
8 changes: 3 additions & 5 deletions rustyms/src/align/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ mod tests {
align::{align, matrix::BLOSUM62, AlignType},
peptide::SimpleLinear,
system::da,
AminoAcid, LinearPeptide, MultiChemical, SequencePosition,
AminoAcid, LinearPeptide, MultiChemical,
};

#[test]
Expand Down Expand Up @@ -470,10 +470,8 @@ mod tests {
.abs()
< f64::EPSILON
);
let mass_diff_nd = (AminoAcid::Asparagine.formulas(SequencePosition::default(), 0)[0]
.monoisotopic_mass()
- AminoAcid::AsparticAcid.formulas(SequencePosition::default(), 0)[0]
.monoisotopic_mass())
let mass_diff_nd = (AminoAcid::Asparagine.formulas()[0].monoisotopic_mass()
- AminoAcid::AsparticAcid.formulas()[0].monoisotopic_mass())
.value
.abs();
let mass_diff_bc = align::<1, SimpleLinear, SimpleLinear>(
Expand Down
40 changes: 16 additions & 24 deletions rustyms/src/aminoacids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl AminoAcid {

if ions.a.0 && allow_terminal.0 {
base_fragments.extend(Fragment::generate_all(
&(self.formulas(sequence_index, peptide_index)
&(self.formulas_inner(sequence_index, peptide_index)
* (modifications - molecular_formula!(H 1 C 1 O 1))),
peptidoform_index,
peptide_index,
Expand All @@ -304,7 +304,7 @@ impl AminoAcid {
}
if ions.b.0 && allow_terminal.0 {
base_fragments.extend(Fragment::generate_all(
&(self.formulas(sequence_index, peptide_index)
&(self.formulas_inner(sequence_index, peptide_index)
* (modifications - molecular_formula!(H 1))),
peptidoform_index,
peptide_index,
Expand All @@ -317,7 +317,7 @@ impl AminoAcid {
}
if ions.c.0 && allow_terminal.0 {
base_fragments.extend(Fragment::generate_all(
&(self.formulas(sequence_index, peptide_index)
&(self.formulas_inner(sequence_index, peptide_index)
* (modifications + molecular_formula!(H 2 N 1))),
peptidoform_index,
peptide_index,
Expand All @@ -332,7 +332,7 @@ impl AminoAcid {
base_fragments.extend(Fragment::generate_all(
&(-self.satellite_ion_fragments(sequence_index, peptide_index)
* modifications
* self.formulas(sequence_index, peptide_index)
* self.formulas_inner(sequence_index, peptide_index)
+ molecular_formula!(H 1 C 1 O 1)),
peptidoform_index,
peptide_index,
Expand All @@ -359,7 +359,7 @@ impl AminoAcid {
base_fragments.extend(Fragment::generate_all(
&(-self.satellite_ion_fragments(sequence_index, peptide_index)
* modifications
* self.formulas(sequence_index, peptide_index)
* self.formulas_inner(sequence_index, peptide_index)
+ molecular_formula!(H 2 N 1)),
peptidoform_index,
peptide_index,
Expand All @@ -372,7 +372,7 @@ impl AminoAcid {
}
if ions.x.0 && allow_terminal.1 {
base_fragments.extend(Fragment::generate_all(
&(self.formulas(sequence_index, peptide_index)
&(self.formulas_inner(sequence_index, peptide_index)
* (modifications + molecular_formula!(C 1 O 1) - molecular_formula!(H 1))),
peptidoform_index,
peptide_index,
Expand All @@ -385,7 +385,7 @@ impl AminoAcid {
}
if ions.y.0 && allow_terminal.1 {
base_fragments.extend(Fragment::generate_all(
&(self.formulas(sequence_index, peptide_index)
&(self.formulas_inner(sequence_index, peptide_index)
* (modifications + molecular_formula!(H 1))),
peptidoform_index,
peptide_index,
Expand All @@ -398,7 +398,7 @@ impl AminoAcid {
}
if ions.z.0 && allow_terminal.1 {
base_fragments.extend(Fragment::generate_all(
&(self.formulas(sequence_index, peptide_index)
&(self.formulas_inner(sequence_index, peptide_index)
* (modifications - molecular_formula!(H 2 N 1))),
peptidoform_index,
peptide_index,
Expand All @@ -409,7 +409,7 @@ impl AminoAcid {
ions.z.2,
));
base_fragments.extend(Fragment::generate_all(
&(self.formulas(sequence_index, peptide_index)
&(self.formulas_inner(sequence_index, peptide_index)
* (modifications - molecular_formula!(H 1 N 1))),
peptidoform_index,
peptide_index,
Expand All @@ -423,7 +423,7 @@ impl AminoAcid {

if ions.immonium.0 && allow_terminal.0 && allow_terminal.1 {
base_fragments.extend(Fragment::generate_all(
&(self.formulas(sequence_index, peptide_index)
&(self.formulas_inner(sequence_index, peptide_index)
* (modifications - molecular_formula!(C 1 O 1))),
peptidoform_index,
peptide_index,
Expand Down Expand Up @@ -567,21 +567,17 @@ mod tests {

#[test]
fn mass() {
let weight_ala =
AminoAcid::Alanine.formulas(SequencePosition::default(), 0)[0].average_weight();
let mass_ala =
AminoAcid::Alanine.formulas(SequencePosition::default(), 0)[0].monoisotopic_mass();
let weight_ala = AminoAcid::Alanine.formulas()[0].average_weight();
let mass_ala = AminoAcid::Alanine.formulas()[0].monoisotopic_mass();
assert_ne!(weight_ala, mass_ala);
assert!((weight_ala.value - 71.07793).abs() < 1e-5);
assert!((mass_ala.value - 71.037113783).abs() < 1e-5);
}

#[test]
fn mass_lysine() {
let weight_lys =
AminoAcid::Lysine.formulas(SequencePosition::default(), 0)[0].average_weight();
let mass_lys =
AminoAcid::Lysine.formulas(SequencePosition::default(), 0)[0].monoisotopic_mass();
let weight_lys = AminoAcid::Lysine.formulas()[0].average_weight();
let mass_lys = AminoAcid::Lysine.formulas()[0].monoisotopic_mass();
assert_ne!(weight_lys, mass_lys);
assert!((weight_lys.value - 128.17240999999999).abs() < 1e-5);
assert!((mass_lys.value - 128.094963010536).abs() < 1e-5);
Expand Down Expand Up @@ -615,12 +611,8 @@ mod tests {
for (aa, mono_mass, average_weight) in known {
let aa = AminoAcid::try_from(*aa).unwrap();
let (mono, weight) = (
aa.formulas(SequencePosition::default(), 0)[0]
.monoisotopic_mass()
.value,
aa.formulas(SequencePosition::default(), 0)[0]
.average_weight()
.value,
aa.formulas()[0].monoisotopic_mass().value,
aa.formulas()[0].average_weight().value,
);
println!(
"{}: {} {} {} {}",
Expand Down
12 changes: 3 additions & 9 deletions rustyms/src/build/resid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{
ontology_modification::{OntologyModification, OntologyModificationList},
AminoAcid, ModData, Ontology, PlacementRule, Position,
};
use crate::{formula::MultiChemical, print, MolecularFormula, SequencePosition};
use crate::{formula::MultiChemical, print, MolecularFormula};

use roxmltree::*;

Expand Down Expand Up @@ -194,16 +194,10 @@ fn parse_resid(debug: bool) -> Vec<OntologyModification> {
continue 'entry;
}
let diff_formula = modification.formula.clone()
- rule
.0
.single_formula(SequencePosition::default(), 0)
.expect("B or Z used as target")
- rule.0.single_formula().expect("B or Z used as target")
- rule
.1
.map(|a| {
a.single_formula(SequencePosition::default(), 0)
.expect("B or Z used as target")
})
.map(|a| a.single_formula().expect("B or Z used as target"))
.unwrap_or_default();
if shared_formula.is_some_and(|s| s != diff_formula) {
print(
Expand Down
6 changes: 3 additions & 3 deletions rustyms/src/checked_aminoacid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl<T> CheckedAminoAcid<T> {

impl Chemical for CheckedAminoAcid<UnAmbiguous> {
/// Get all possible formula for an unambiguous amino acid (X is defined to be an empty formula)
fn formula(
fn formula_inner(
&self,
_sequence_index: crate::SequencePosition,
_peptide_index: usize,
Expand Down Expand Up @@ -340,7 +340,7 @@ impl Chemical for CheckedAminoAcid<UnAmbiguous> {
impl<T> MultiChemical for CheckedAminoAcid<T> {
/// # Panics
/// Is the sequence index is a terminal index
fn formulas(
fn formulas_inner(
&self,
sequence_index: crate::SequencePosition,
peptide_index: usize,
Expand All @@ -361,7 +361,7 @@ impl<T> MultiChemical for CheckedAminoAcid<T> {
]
.into(),
_ => unreachable!(), }
}, |unambiguous| unambiguous.formula(sequence_index, peptide_index).into())
}, |unambiguous| unambiguous.formula_inner(sequence_index, peptide_index).into())
}
}

Expand Down
4 changes: 2 additions & 2 deletions rustyms/src/formula.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl std::fmt::Display for MolecularFormula {
mod tests {
use crate::{
model::ChargeRange, molecular_formula, AminoAcid, Fragment, MolecularCharge,
MolecularFormula, MultiChemical, SequencePosition,
MolecularFormula, MultiChemical,
};

#[test]
Expand Down Expand Up @@ -295,7 +295,7 @@ mod tests {
#[test]
#[allow(clippy::similar_names)]
fn labels() {
let labelled = AminoAcid::AmbiguousAsparagine.formulas(SequencePosition::default(), 0);
let labelled = AminoAcid::AmbiguousAsparagine.formulas();
let unlabelled: crate::Multi<MolecularFormula> =
vec![molecular_formula!(C 1), molecular_formula!(H 1)].into();
let mut mul_assign_l = labelled.clone();
Expand Down
10 changes: 4 additions & 6 deletions rustyms/src/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Fragment {
.map(|(((term, mass), charge), loss)| Self {
formula: term
+ mass
+ charge.formula(SequencePosition::default(), peptide_index)
+ charge.formula_inner(SequencePosition::default(), peptide_index)
+ loss.unwrap_or(&NeutralLoss::Gain(MolecularFormula::default())),
charge: Charge::new::<crate::system::e>(charge.charge().value.try_into().unwrap()),
ion: annotation.clone(),
Expand All @@ -109,10 +109,8 @@ impl Fragment {
#[must_use]
fn with_charge(&self, charge: &MolecularCharge) -> Self {
let formula = charge
.formula(SequencePosition::default(), 0)
.with_labels(&[AmbiguousLabel::ChargeCarrier(
charge.formula(SequencePosition::default(), 0),
)]);
.formula()
.with_labels(&[AmbiguousLabel::ChargeCarrier(charge.formula())]);
let c = Charge::new::<crate::system::charge::e>(
usize::try_from(formula.charge().value).unwrap(),
);
Expand Down Expand Up @@ -605,7 +603,7 @@ mod tests {
#[test]
fn neutral_loss() {
let a = Fragment::new(
AminoAcid::AsparticAcid.formulas(SequencePosition::default(), 0)[0].clone(),
AminoAcid::AsparticAcid.formulas()[0].clone(),
Charge::new::<crate::system::charge::e>(1),
0,
0,
Expand Down
Loading

0 comments on commit 292f7a4

Please sign in to comment.