Skip to content

Commit

Permalink
CHORE: moved spectrum reader config serialization to timsrust
Browse files Browse the repository at this point in the history
  • Loading branch information
sander-willems-bruker committed Sep 4, 2024
1 parent 3ced081 commit 492acdb
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 107 deletions.
3 changes: 1 addition & 2 deletions crates/sage-cli/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use anyhow::{ensure, Context};
use clap::ArgMatches;
use sage_cloudpath::CloudPath;
use sage_cloudpath::{tdf::BrukerSpectrumProcessor, CloudPath};
use sage_core::{
database::{Builder, Parameters},
lfq::LfqSettings,
mass::Tolerance,
spectrum::BrukerSpectrumProcessor,
tmt::Isobaric,
};
use serde::{Deserialize, Serialize};
Expand Down
7 changes: 5 additions & 2 deletions crates/sage-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ impl Runner {
path_lower.ends_with(ext)
}
}) {
sage_cloudpath::util::read_tdf(path, file_id, scorer.bruker_spectrum_processor)
sage_cloudpath::util::read_tdf(
path,
file_id,
self.parameters.bruker_spectrum_processor,
)
} else {
sage_cloudpath::util::read_mzml(path, file_id, sn)
};
Expand Down Expand Up @@ -268,7 +272,6 @@ impl Runner {
report_psms: self.parameters.report_psms,
wide_window: self.parameters.wide_window,
annotate_matches: self.parameters.annotate_matches,
bruker_spectrum_processor: self.parameters.bruker_spectrum_processor,
};

//Collect all results into a single container
Expand Down
3 changes: 1 addition & 2 deletions crates/sage-cli/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use sage_core::database::Builder;
use sage_core::mass::Tolerance;
use sage_core::scoring::Scorer;
use sage_core::spectrum::{BrukerSpectrumProcessor, SpectrumProcessor};
use sage_core::spectrum::SpectrumProcessor;

#[test]
fn integration() -> anyhow::Result<()> {
Expand Down Expand Up @@ -32,7 +32,6 @@ fn integration() -> anyhow::Result<()> {
report_psms: 1,
wide_window: false,
annotate_matches: false,
bruker_spectrum_processor: BrukerSpectrumProcessor::default(),
};

let psm = scorer.score(&processed);
Expand Down
2 changes: 1 addition & 1 deletion crates/sage-cloudpath/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +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 = { version = "0.4.0"}
timsrust = { version = "0.4.1"}
rayon = "1.5"
reqwest = { version = "0.11", features = ["json", "rustls-tls"], default-features = false }
regex = "1.6"
Expand Down
75 changes: 7 additions & 68 deletions crates/sage-cloudpath/src/tdf.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use rayon::prelude::*;
use sage_core::{
mass::Tolerance,
spectrum::{
BrukerSpectrumProcessor, FrameWindowSplittingStrategy, QuadWindowExpansionStrategy,
},
spectrum::{Precursor, RawSpectrum, Representation},
};
use serde::{Deserialize, Serialize};
use timsrust::readers::SpectrumReaderConfig;

pub struct TdfReader;

Expand All @@ -18,9 +17,7 @@ impl TdfReader {
) -> Result<Vec<RawSpectrum>, timsrust::TimsRustError> {
let spectrum_reader = timsrust::readers::SpectrumReader::build()
.with_path(path_name.as_ref())
.with_config(Self::parse_bruker_spectrum_config(
bruker_spectrum_processor,
))
.with_config(bruker_spectrum_processor.spectrum_reader_config)
.finalize()?;
let spectra: Vec<RawSpectrum> = (0..spectrum_reader.len())
.into_par_iter()
Expand Down Expand Up @@ -69,67 +66,9 @@ impl TdfReader {
precursor.inverse_ion_mobility = Option::from(dda_precursor.im as f32);
precursor
}
}

fn parse_bruker_spectrum_config(
config: BrukerSpectrumProcessor,
) -> timsrust::readers::SpectrumReaderConfig {
let mut spectrum_processing_params = timsrust::readers::SpectrumProcessingParams::default();
if let Some(smoothing_window) = config.smoothing_window {
spectrum_processing_params.smoothing_window = smoothing_window;
}
if let Some(centroiding_window) = config.centroiding_window {
spectrum_processing_params.centroiding_window = centroiding_window;
}
if let Some(calibration_tolerance) = config.calibration_tolerance {
spectrum_processing_params.calibration_tolerance = calibration_tolerance;
}
if let Some(calibrate) = config.calibrate {
spectrum_processing_params.calibrate = calibrate;
}
let frame_splitting_params: timsrust::readers::FrameWindowSplittingConfiguration;
if let Some(dia_strategy) = config.dia_strategy {
frame_splitting_params = match dia_strategy {
FrameWindowSplittingStrategy::Quadrupole(q) => {
timsrust::readers::FrameWindowSplittingConfiguration::Quadrupole(match q {
QuadWindowExpansionStrategy::None => {
timsrust::readers::QuadWindowExpansionStrategy::None
}
QuadWindowExpansionStrategy::Even(n) => {
timsrust::readers::QuadWindowExpansionStrategy::Even(n)
}
QuadWindowExpansionStrategy::UniformScan(x) => {
timsrust::readers::QuadWindowExpansionStrategy::UniformScan(x)
}
QuadWindowExpansionStrategy::UniformMobility(x) => {
timsrust::readers::QuadWindowExpansionStrategy::UniformMobility(x, None)
}
})
}
FrameWindowSplittingStrategy::Window(q) => {
timsrust::readers::FrameWindowSplittingConfiguration::Window(match q {
QuadWindowExpansionStrategy::None => {
timsrust::readers::QuadWindowExpansionStrategy::None
}

QuadWindowExpansionStrategy::Even(n) => {
timsrust::readers::QuadWindowExpansionStrategy::Even(n)
}

QuadWindowExpansionStrategy::UniformScan(x) => {
timsrust::readers::QuadWindowExpansionStrategy::UniformScan(x)
}
QuadWindowExpansionStrategy::UniformMobility(x) => {
timsrust::readers::QuadWindowExpansionStrategy::UniformMobility(x, None)
}
})
}
};
} else {
frame_splitting_params = timsrust::readers::FrameWindowSplittingConfiguration::default()
}
timsrust::readers::SpectrumReaderConfig {
spectrum_processing_params,
frame_splitting_params,
}
}
#[derive(Clone, Copy, Serialize, Deserialize, Default)]
pub struct BrukerSpectrumProcessor {
pub spectrum_reader_config: SpectrumReaderConfig,
}
4 changes: 2 additions & 2 deletions crates/sage-cloudpath/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{read_and_execute, Error};
use sage_core::spectrum::{BrukerSpectrumProcessor, RawSpectrum};
use crate::{read_and_execute, tdf::BrukerSpectrumProcessor, Error};
use sage_core::spectrum::RawSpectrum;
use serde::Serialize;
use tokio::io::AsyncReadExt;

Expand Down
3 changes: 1 addition & 2 deletions crates/sage/src/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::database::{IndexedDatabase, PeptideIx};
use crate::heap::bounded_min_heapify;
use crate::ion_series::{IonSeries, Kind};
use crate::mass::{Tolerance, NEUTRON, PROTON};
use crate::spectrum::{BrukerSpectrumProcessor, Precursor, ProcessedSpectrum};
use crate::spectrum::{Precursor, ProcessedSpectrum};
use serde::Serialize;
use std::ops::AddAssign;
use std::sync::atomic::{AtomicUsize, Ordering};
Expand Down Expand Up @@ -191,7 +191,6 @@ pub struct Scorer<'db> {
// the precursor tolerance window based on MS2 isolation window and charge
pub wide_window: bool,
pub annotate_matches: bool,
pub bruker_spectrum_processor: BrukerSpectrumProcessor,
}

#[inline(always)]
Expand Down
28 changes: 0 additions & 28 deletions crates/sage/src/spectrum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use serde::{Deserialize, Serialize};

use crate::database::binary_search_slice;
use crate::mass::{Tolerance, NEUTRON, PROTON};

Expand Down Expand Up @@ -359,32 +357,6 @@ impl SpectrumProcessor {
}
}

#[derive(Clone, Copy, Serialize, Deserialize, Default)]
pub struct BrukerSpectrumProcessor {
pub smoothing_window: Option<u32>,
pub centroiding_window: Option<u32>,
pub calibration_tolerance: Option<f64>,
pub calibrate: Option<bool>,
pub dia_strategy: Option<FrameWindowSplittingStrategy>,
}

type ScanSpanStep = (usize, usize);
type MobilitySpanStep = (f64, f64);

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum QuadWindowExpansionStrategy {
None,
Even(usize),
UniformMobility(MobilitySpanStep),
UniformScan(ScanSpanStep),
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum FrameWindowSplittingStrategy {
Quadrupole(QuadWindowExpansionStrategy),
Window(QuadWindowExpansionStrategy),
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 492acdb

Please sign in to comment.