Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into timsrust_0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sander-willems-bruker committed Jul 8, 2024
2 parents 4379352 + a969ca7 commit bb539a8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion crates/sage-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,23 @@ impl Runner {
.sn
.then_some(self.parameters.quant.tmt_settings.level);

let (min_fragment_mz, min_deisotope_mz) = match &self.parameters.quant.tmt {
Some(i) => match self.parameters.quant.tmt_settings.level {
2 => (
i.reporter_masses().first().map(|x| x * (1.0 - 20E-6)),
i.reporter_masses().last().map(|x| x * (1.0 + 20E-6)),
),
_ => (None, None),
},
None => (None, None),
};

let sp = SpectrumProcessor::new(
self.parameters.max_peaks,
self.parameters.database.fragment_min_mz,
min_fragment_mz.unwrap_or(self.parameters.database.fragment_min_mz),
self.parameters.database.fragment_max_mz,
self.parameters.deisotope,
min_deisotope_mz.unwrap_or(0.0),
);

let bruker_extensions = [".d", ".tdf", ".tdf_bin", "ms2", "raw"];
Expand Down
4 changes: 1 addition & 3 deletions crates/sage-cli/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use sage_core::database::Builder;
use sage_core::enzyme::Digest;
use sage_core::mass::Tolerance;
use sage_core::peptide::Peptide;
use sage_core::scoring::Scorer;
use sage_core::spectrum::SpectrumProcessor;

Expand All @@ -15,7 +13,7 @@ fn integration() -> anyhow::Result<()> {
let spectra = sage_cloudpath::util::read_mzml("../../tests/LQSRPAAPPAPGPGQLTLR.mzML", 0, None)?;
assert_eq!(spectra.len(), 1);

let sp = SpectrumProcessor::new(100, 0.0, 1500.0, true);
let sp = SpectrumProcessor::new(100, 0.0, 1500.0, true, 0.0);
let processed = sp.process(spectra[0].clone());
assert!(processed.peaks.len() <= 300);

Expand Down
3 changes: 1 addition & 2 deletions crates/sage-cloudpath/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ log = "0.4.0"
once_cell = "1.0"
tokio = { version = "1.0", features = ["fs", "io-util", "rt", "macros"] }
quick-xml = { version = "0.31.0", features = ["async-tokio"] }
# timsrust = "0.2.0"
timsrust = {path="../../../timsrust"}
timsrust = "0.3.0"
rayon = "1.5"
reqwest = { version = "0.11", features = ["json", "rustls-tls"], default-features = false }
regex = "1.6"
Expand Down
2 changes: 1 addition & 1 deletion crates/sage-cloudpath/src/tdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl TdfReader {
total_ion_current: 0.0,
mz: dda_spectrum.mz_values.iter().map(|&x| x as f32).collect(),
ms_level: 2,
id: dda_precursor.index.to_string(),
id: dda_spectrum.index.to_string(),
// precursor_id: dda_precursor.index as u32,
// frame_id: dda_precursor.frame_index as u32,
intensity: dda_spectrum.intensities.iter().map(|&x| x as f32).collect(),
Expand Down
72 changes: 57 additions & 15 deletions crates/sage/src/spectrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct SpectrumProcessor {
pub take_top_n: usize,
pub max_fragment_mz: f32,
pub min_fragment_mz: f32,
pub min_deisotope_mz: f32,
pub deisotope: bool,
}

Expand Down Expand Up @@ -176,7 +177,13 @@ pub fn select_most_intense_peak(
// }

/// Deisotope a set of peaks by attempting to find C13 peaks under a given `ppm` tolerance
pub fn deisotope(mz: &[f32], int: &[f32], max_charge: u8, ppm: f32) -> Vec<Deisotoped> {
pub fn deisotope(
mz: &[f32],
int: &[f32],
max_charge: u8,
ppm: f32,
min_mz: f32,
) -> Vec<Deisotoped> {
let mut peaks = mz
.iter()
.zip(int.iter())
Expand All @@ -192,7 +199,8 @@ pub fn deisotope(mz: &[f32], int: &[f32], max_charge: u8, ppm: f32) -> Vec<Deiso
for i in (0..mz.len()).rev() {
// Two pointer approach, j is fast pointer
let mut j = i.saturating_sub(1);
while mz[i] - mz[j] <= NEUTRON + Tolerance::ppm_to_delta_mass(mz[i], ppm) {
while mz[i] - mz[j] <= NEUTRON + Tolerance::ppm_to_delta_mass(mz[i], ppm) && mz[j] >= min_mz
{
let delta = mz[i] - mz[j];
let tol = Tolerance::ppm_to_delta_mass(mz[i], ppm);
for charge in 1..=max_charge {
Expand Down Expand Up @@ -259,11 +267,13 @@ impl SpectrumProcessor {
min_fragment_mz: f32,
max_fragment_mz: f32,
deisotope: bool,
min_deisotope_mz: f32,
) -> Self {
Self {
take_top_n,
min_fragment_mz,
max_fragment_mz,
min_deisotope_mz,
deisotope,
}
}
Expand All @@ -285,7 +295,13 @@ impl SpectrumProcessor {
.unwrap_or(3);

if should_deisotope {
let mut peaks = deisotope(&spectrum.mz, &spectrum.intensity, charge, 10.0);
let mut peaks = deisotope(
&spectrum.mz,
&spectrum.intensity,
charge,
10.0,
self.min_deisotope_mz,
);
peaks.sort_unstable_by(|a, b| {
b.intensity
.total_cmp(&a.intensity)
Expand Down Expand Up @@ -364,6 +380,8 @@ mod test {
fn test_deisotope() {
let mut mz = [
800.9,
800.9 + NEUTRON * 1.0,
800.9 + NEUTRON * 2.0,
803.4080,
804.4108,
805.4106,
Expand All @@ -372,18 +390,30 @@ mod test {
812.0,
812.0 + NEUTRON / 2.0,
];
let mut int = [1., 4., 3., 2., 1., 1., 9.0, 4.5];
let mut peaks = deisotope(&mut mz, &mut int, 2, 5.0);
let mut int = [2., 1.5, 1., 4., 3., 2., 1., 1., 9.0, 4.5];
let mut peaks = deisotope(&mut mz, &mut int, 2, 5.0, 800.91);

assert_eq!(
peaks,
vec![
Deisotoped {
mz: 800.9,
intensity: 1.0,
intensity: 2.0,
charge: None,
envelope: None,
},
Deisotoped {
mz: 800.9 + NEUTRON * 1.0,
intensity: 2.5,
charge: Some(1),
envelope: None,
},
Deisotoped {
mz: 800.9 + NEUTRON * 2.0,
intensity: 1.0,
charge: Some(1),
envelope: Some(1),
},
Deisotoped {
mz: 803.4080,
intensity: 10.0,
Expand All @@ -394,19 +424,19 @@ mod test {
mz: 804.4108,
intensity: 6.0,
charge: Some(1),
envelope: Some(1),
envelope: Some(3),
},
Deisotoped {
mz: 805.4106,
intensity: 3.0,
charge: Some(1),
envelope: Some(2),
envelope: Some(4),
},
Deisotoped {
mz: 806.4116,
intensity: 1.0,
charge: Some(1),
envelope: Some(3),
envelope: Some(5),
},
Deisotoped {
mz: 810.0,
Expand All @@ -424,7 +454,7 @@ mod test {
mz: 812.0 + NEUTRON / 2.0,
intensity: 4.5,
charge: Some(2),
envelope: Some(6),
envelope: Some(8),
}
]
);
Expand All @@ -435,10 +465,22 @@ mod test {
vec![
Deisotoped {
mz: 800.9,
intensity: 1.0,
intensity: 2.0,
charge: None,
envelope: None,
},
Deisotoped {
mz: 800.9 + NEUTRON * 1.0,
intensity: 2.5,
charge: Some(1),
envelope: None,
},
Deisotoped {
mz: 800.9 + NEUTRON * 2.0,
intensity: 0.0,
charge: Some(1),
envelope: Some(1),
},
Deisotoped {
mz: 803.4080,
intensity: 10.0,
Expand All @@ -449,19 +491,19 @@ mod test {
mz: 804.4108,
intensity: 0.0,
charge: Some(1),
envelope: Some(1),
envelope: Some(3),
},
Deisotoped {
mz: 805.4106,
intensity: 0.0,
charge: Some(1),
envelope: Some(1),
envelope: Some(3),
},
Deisotoped {
mz: 806.4116,
intensity: 0.0,
charge: Some(1),
envelope: Some(1),
envelope: Some(3),
},
Deisotoped {
mz: 810.0,
Expand All @@ -479,7 +521,7 @@ mod test {
mz: 812.0 + NEUTRON / 2.0,
intensity: 0.0,
charge: Some(2),
envelope: Some(6),
envelope: Some(8),
}
]
);
Expand Down

0 comments on commit bb539a8

Please sign in to comment.