diff --git a/benches/speed_performance.rs b/benches/speed_performance.rs index d9ff504..1944962 100644 --- a/benches/speed_performance.rs +++ b/benches/speed_performance.rs @@ -2,7 +2,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; use rayon::iter::ParallelIterator; #[cfg(feature = "tdf")] use timsrust::readers::FrameReader; -use timsrust::readers::{SpectrumReader, SpectrumReaderConfig}; +use timsrust::readers::SpectrumReader; const DDA_TEST: &str = "/mnt/d/data/mpib/tims05_300SPD/20230505_TIMS05_PaSk_MA_HeLa_6min_ddaP_S1-C10_1_2323.d/"; diff --git a/src/errors.rs b/src/errors.rs index 7758b0b..dfa4b0b 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -2,7 +2,7 @@ use crate::io::readers::{ FrameReaderError, MetadataReaderError, QuadrupoleSettingsReaderError, }; -use crate::io::readers::{PrecursorReaderError, SpectrumReaderError}; +use crate::{io::readers::PrecursorReaderError, readers::SpectrumReaderError}; /// An error that is produced by timsrust (uses [thiserror]). #[derive(thiserror::Error, Debug)] diff --git a/src/io/readers/spectrum_reader.rs b/src/io/readers/spectrum_reader.rs index e97b724..ce80658 100644 --- a/src/io/readers/spectrum_reader.rs +++ b/src/io/readers/spectrum_reader.rs @@ -1,37 +1,35 @@ +mod builder; +mod config; +mod errors; #[cfg(feature = "minitdf")] mod minitdf; +mod spectrum_trait; #[cfg(feature = "tdf")] mod tdf; -#[cfg(feature = "minitdf")] -use minitdf::{MiniTDFSpectrumReader, MiniTDFSpectrumReaderError}; -use rayon::prelude::*; -#[cfg(feature = "serialize")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "tdf")] -use tdf::{TDFSpectrumReader, TDFSpectrumReaderError}; - +use super::TimsTofPathLike; use crate::ms_data::Spectrum; - -#[cfg(feature = "tdf")] -use super::FrameWindowSplittingConfiguration; -use super::{TimsTofFileType, TimsTofPath, TimsTofPathLike}; +pub use builder::SpectrumReaderBuilder; +pub use config::{SpectrumProcessingParams, SpectrumReaderConfig}; +pub use errors::SpectrumReaderError; +use rayon::prelude::*; +use spectrum_trait::SpectrumReaderTrait; pub struct SpectrumReader { spectrum_reader: Box, } impl SpectrumReader { - pub fn build() -> SpectrumReaderBuilder { - SpectrumReaderBuilder::default() - } - pub fn new( path: impl TimsTofPathLike, ) -> Result { Self::build().with_path(path).finalize() } + pub fn build() -> SpectrumReaderBuilder { + SpectrumReaderBuilder::default() + } + pub fn get(&self, index: usize) -> Result { self.spectrum_reader.get(index) } @@ -60,96 +58,3 @@ impl SpectrumReader { self.spectrum_reader.calibrate(); } } - -#[derive(Debug, Default, Clone)] -pub struct SpectrumReaderBuilder { - path: Option, - config: SpectrumReaderConfig, -} - -impl SpectrumReaderBuilder { - pub fn with_path(&self, path: impl TimsTofPathLike) -> Self { - // TODO - let path = Some(path.to_timstof_path().unwrap()); - Self { - path, - ..self.clone() - } - } - - pub fn with_config(&self, config: SpectrumReaderConfig) -> Self { - Self { - config: config, - ..self.clone() - } - } - - pub fn finalize(self) -> Result { - let path = match self.path { - None => return Err(SpectrumReaderError::NoPath), - Some(path) => path, - }; - let spectrum_reader: Box = - match path.file_type() { - #[cfg(feature = "minitdf")] - TimsTofFileType::MiniTDF => { - Box::new(MiniTDFSpectrumReader::new(path)?) - }, - #[cfg(feature = "tdf")] - TimsTofFileType::TDF => { - Box::new(TDFSpectrumReader::new(path, self.config)?) - }, - }; - let mut reader = SpectrumReader { spectrum_reader }; - if self.config.spectrum_processing_params.calibrate { - reader.calibrate(); - } - Ok(reader) - } -} - -trait SpectrumReaderTrait: Sync + Send { - fn get(&self, index: usize) -> Result; - fn len(&self) -> usize; - fn calibrate(&mut self); -} - -#[derive(Debug, thiserror::Error)] -pub enum SpectrumReaderError { - #[cfg(feature = "minitdf")] - #[error("{0}")] - MiniTDFSpectrumReaderError(#[from] MiniTDFSpectrumReaderError), - #[cfg(feature = "tdf")] - #[error("{0}")] - TDFSpectrumReaderError(#[from] TDFSpectrumReaderError), - #[error("No path provided")] - NoPath, -} - -#[derive(Debug, Clone, Copy)] -#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))] -pub struct SpectrumProcessingParams { - pub smoothing_window: u32, - pub centroiding_window: u32, - pub calibration_tolerance: f64, - pub calibrate: bool, -} - -impl Default for SpectrumProcessingParams { - fn default() -> Self { - Self { - smoothing_window: 1, - centroiding_window: 1, - calibration_tolerance: 0.1, - calibrate: false, - } - } -} - -#[derive(Debug, Default, Clone, Copy)] -#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))] -pub struct SpectrumReaderConfig { - pub spectrum_processing_params: SpectrumProcessingParams, - #[cfg(feature = "tdf")] - pub frame_splitting_params: FrameWindowSplittingConfiguration, -} diff --git a/src/io/readers/spectrum_reader/builder.rs b/src/io/readers/spectrum_reader/builder.rs new file mode 100644 index 0000000..b191af6 --- /dev/null +++ b/src/io/readers/spectrum_reader/builder.rs @@ -0,0 +1,58 @@ +use crate::readers::{TimsTofFileType, TimsTofPath, TimsTofPathLike}; + +use super::{ + errors::SpectrumReaderError, SpectrumReader, SpectrumReaderConfig, + SpectrumReaderTrait, +}; + +#[cfg(feature = "minitdf")] +use super::minitdf::MiniTDFSpectrumReader; +#[cfg(feature = "tdf")] +use super::tdf::TDFSpectrumReader; + +#[derive(Debug, Default, Clone)] +pub struct SpectrumReaderBuilder { + path: Option, + config: SpectrumReaderConfig, +} + +impl SpectrumReaderBuilder { + pub fn with_path(&self, path: impl TimsTofPathLike) -> Self { + // TODO + let path = Some(path.to_timstof_path().unwrap()); + Self { + path, + ..self.clone() + } + } + + pub fn with_config(&self, config: SpectrumReaderConfig) -> Self { + Self { + config: config, + ..self.clone() + } + } + + pub fn finalize(self) -> Result { + let path = match self.path { + None => return Err(SpectrumReaderError::NoPath), + Some(path) => path, + }; + let spectrum_reader: Box = + match path.file_type() { + #[cfg(feature = "minitdf")] + TimsTofFileType::MiniTDF => { + Box::new(MiniTDFSpectrumReader::new(path)?) + }, + #[cfg(feature = "tdf")] + TimsTofFileType::TDF => { + Box::new(TDFSpectrumReader::new(path, self.config)?) + }, + }; + let mut reader = SpectrumReader { spectrum_reader }; + if self.config.spectrum_processing_params.calibrate { + reader.calibrate(); + } + Ok(reader) + } +} diff --git a/src/io/readers/spectrum_reader/config.rs b/src/io/readers/spectrum_reader/config.rs new file mode 100644 index 0000000..d387b49 --- /dev/null +++ b/src/io/readers/spectrum_reader/config.rs @@ -0,0 +1,33 @@ +#[cfg(feature = "tdf")] +use super::super::FrameWindowSplittingConfiguration; + +#[cfg(feature = "serialize")] +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Copy)] +#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))] +pub struct SpectrumProcessingParams { + pub smoothing_window: u32, + pub centroiding_window: u32, + pub calibration_tolerance: f64, + pub calibrate: bool, +} + +impl Default for SpectrumProcessingParams { + fn default() -> Self { + Self { + smoothing_window: 1, + centroiding_window: 1, + calibration_tolerance: 0.1, + calibrate: false, + } + } +} + +#[derive(Debug, Default, Clone, Copy)] +#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))] +pub struct SpectrumReaderConfig { + pub spectrum_processing_params: SpectrumProcessingParams, + #[cfg(feature = "tdf")] + pub frame_splitting_params: FrameWindowSplittingConfiguration, +} diff --git a/src/io/readers/spectrum_reader/errors.rs b/src/io/readers/spectrum_reader/errors.rs new file mode 100644 index 0000000..1782645 --- /dev/null +++ b/src/io/readers/spectrum_reader/errors.rs @@ -0,0 +1,16 @@ +#[cfg(feature = "minitdf")] +use super::minitdf::MiniTDFSpectrumReaderError; +#[cfg(feature = "tdf")] +use super::tdf::TDFSpectrumReaderError; + +#[derive(Debug, thiserror::Error)] +pub enum SpectrumReaderError { + #[cfg(feature = "minitdf")] + #[error("{0}")] + MiniTDFSpectrumReaderError(#[from] MiniTDFSpectrumReaderError), + #[cfg(feature = "tdf")] + #[error("{0}")] + TDFSpectrumReaderError(#[from] TDFSpectrumReaderError), + #[error("No path provided")] + NoPath, +} diff --git a/src/io/readers/spectrum_reader/spectrum_trait.rs b/src/io/readers/spectrum_reader/spectrum_trait.rs new file mode 100644 index 0000000..c05b8ea --- /dev/null +++ b/src/io/readers/spectrum_reader/spectrum_trait.rs @@ -0,0 +1,9 @@ +use crate::Spectrum; + +use super::errors::SpectrumReaderError; + +pub(crate) trait SpectrumReaderTrait: Sync + Send { + fn get(&self, index: usize) -> Result; + fn len(&self) -> usize; + fn calibrate(&mut self); +}