Skip to content

Commit

Permalink
Allowed alignment mass mode setting and tolernance conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
douweschulte committed Oct 22, 2024
1 parent ba83f70 commit 4098b6b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
12 changes: 6 additions & 6 deletions rustyms/src/align/mass_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::fmt::Debug;
use crate::{
peptide::{AtMax, SimpleLinear},
system::Mass,
LinearPeptide, MolecularFormula, Multi, SequenceElement, SequencePosition, WithinTolerance,
LinearPeptide, MassMode, MolecularFormula, Multi, SequenceElement, SequencePosition,
WithinTolerance,
};

use super::{
Expand All @@ -30,8 +31,8 @@ pub fn align<'lifetime, const STEPS: u16, A: AtMax<SimpleLinear>, B: AtMax<Simpl

let mut matrix = Matrix::new(seq_a.len(), seq_b.len());
let mut global_highest = (0, 0, 0);
let masses_a: DiagonalArray<Multi<Mass>> = calculate_masses::<STEPS>(seq_a);
let masses_b: DiagonalArray<Multi<Mass>> = calculate_masses::<STEPS>(seq_b);
let masses_a: DiagonalArray<Multi<Mass>> = calculate_masses::<STEPS>(seq_a, scoring.mass_mode);
let masses_b: DiagonalArray<Multi<Mass>> = calculate_masses::<STEPS>(seq_b, scoring.mass_mode);
let zero: Multi<Mass> = Multi::default();

if align_type.left.global_a() {
Expand Down Expand Up @@ -287,11 +288,10 @@ fn score<A: AtMax<SimpleLinear>, B: AtMax<SimpleLinear>>(
/// Get the masses of all sequence elements
fn calculate_masses<const STEPS: u16>(
sequence: &LinearPeptide<impl AtMax<SimpleLinear>>,
mass_mode: MassMode,
) -> DiagonalArray<Multi<Mass>> {
let mut array = DiagonalArray::new(sequence.len(), STEPS);
// dbg!(&array, format!("{sequence}"));
for i in 0..sequence.len() {
// dbg!(i, 0..=i.min(max_depth));
for j in 0..=i.min(STEPS as usize) {
array[[i, j]] = sequence.sequence()[i - j..=i]
.iter()
Expand All @@ -308,7 +308,7 @@ fn calculate_masses<const STEPS: u16>(
})
.sum::<Multi<MolecularFormula>>()
.iter()
.map(MolecularFormula::monoisotopic_mass)
.map(|f| f.mass(mass_mode))
.collect();
}
}
Expand Down
7 changes: 6 additions & 1 deletion rustyms/src/align/scoring.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::{system::OrderedMass, AminoAcid, Tolerance};
use crate::{system::OrderedMass, AminoAcid, MassMode, Tolerance};

/// The type of a single match step
#[derive(
Expand Down Expand Up @@ -68,6 +68,10 @@ pub struct AlignScoring<'a> {
///
/// Default: 10ppm.
pub tolerance: Tolerance<OrderedMass>,
/// The mass mode for the alignment.
///
/// Default: Monoisotopic.
pub mass_mode: MassMode,
}

impl Default for AlignScoring<'static> {
Expand All @@ -82,6 +86,7 @@ impl Default for AlignScoring<'static> {
gap_extend: -1,
matrix: matrices::BLOSUM62,
tolerance: crate::Tolerance::new_ppm(10.0),
mass_mode: MassMode::Monoisotopic,
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions rustyms/src/tolerance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ impl<T> Tolerance<T> {
pub fn new_absolute(value: impl Into<T>) -> Self {
Self::Absolute(value.into())
}

/// Convert this tolerance into another absolute type.
pub fn convert<O: From<T>>(self) -> Tolerance<O> {
match self {
Self::Relative(r) => Tolerance::Relative(r),
Self::Absolute(a) => Tolerance::Absolute(a.into()),
}
}
}

impl<T> Tolerance<T>
Expand Down

0 comments on commit 4098b6b

Please sign in to comment.