Skip to content

Commit

Permalink
CHORE: reorganize specrum reader code
Browse files Browse the repository at this point in the history
  • Loading branch information
sander-willems-bruker committed Dec 4, 2024
1 parent 3f49454 commit d4b71dd
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 111 deletions.
2 changes: 1 addition & 1 deletion benches/speed_performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
123 changes: 14 additions & 109 deletions src/io/readers/spectrum_reader.rs
Original file line number Diff line number Diff line change
@@ -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<dyn SpectrumReaderTrait>,
}

impl SpectrumReader {
pub fn build() -> SpectrumReaderBuilder {
SpectrumReaderBuilder::default()
}

pub fn new(
path: impl TimsTofPathLike,
) -> Result<Self, SpectrumReaderError> {
Self::build().with_path(path).finalize()
}

pub fn build() -> SpectrumReaderBuilder {
SpectrumReaderBuilder::default()
}

pub fn get(&self, index: usize) -> Result<Spectrum, SpectrumReaderError> {
self.spectrum_reader.get(index)
}
Expand Down Expand Up @@ -60,96 +58,3 @@ impl SpectrumReader {
self.spectrum_reader.calibrate();
}
}

#[derive(Debug, Default, Clone)]
pub struct SpectrumReaderBuilder {
path: Option<TimsTofPath>,
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<SpectrumReader, SpectrumReaderError> {
let path = match self.path {
None => return Err(SpectrumReaderError::NoPath),
Some(path) => path,
};
let spectrum_reader: Box<dyn SpectrumReaderTrait> =
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<Spectrum, SpectrumReaderError>;
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,
}
58 changes: 58 additions & 0 deletions src/io/readers/spectrum_reader/builder.rs
Original file line number Diff line number Diff line change
@@ -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<TimsTofPath>,
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<SpectrumReader, SpectrumReaderError> {
let path = match self.path {
None => return Err(SpectrumReaderError::NoPath),
Some(path) => path,
};
let spectrum_reader: Box<dyn SpectrumReaderTrait> =
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)
}
}
33 changes: 33 additions & 0 deletions src/io/readers/spectrum_reader/config.rs
Original file line number Diff line number Diff line change
@@ -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,
}
16 changes: 16 additions & 0 deletions src/io/readers/spectrum_reader/errors.rs
Original file line number Diff line number Diff line change
@@ -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,
}
9 changes: 9 additions & 0 deletions src/io/readers/spectrum_reader/spectrum_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::Spectrum;

use super::errors::SpectrumReaderError;

pub(crate) trait SpectrumReaderTrait: Sync + Send {
fn get(&self, index: usize) -> Result<Spectrum, SpectrumReaderError>;
fn len(&self) -> usize;
fn calibrate(&mut self);
}

0 comments on commit d4b71dd

Please sign in to comment.