From 8613bc9ac68e83457eab6d64181f8e4eade7d40b Mon Sep 17 00:00:00 2001 From: theGreatHerrLebert Date: Tue, 19 Sep 2023 15:01:20 +0200 Subject: [PATCH] added print to Spectrum Classes --- mscore/Cargo.toml | 1 + mscore/src/main.rs | 3 ++ mscore/src/mz_spectrum.rs | 85 ++++++++++++++++++++++++++++++++++----- rustdf/src/main.rs | 6 ++- 4 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 mscore/src/main.rs diff --git a/mscore/Cargo.toml b/mscore/Cargo.toml index b06e1caa..989d26e0 100644 --- a/mscore/Cargo.toml +++ b/mscore/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +itertools = "0.11.0" [lib] name = "mscore" diff --git a/mscore/src/main.rs b/mscore/src/main.rs new file mode 100644 index 00000000..fbedd920 --- /dev/null +++ b/mscore/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} \ No newline at end of file diff --git a/mscore/src/mz_spectrum.rs b/mscore/src/mz_spectrum.rs index 20b99499..5f5b3c0f 100644 --- a/mscore/src/mz_spectrum.rs +++ b/mscore/src/mz_spectrum.rs @@ -1,6 +1,7 @@ use std::fmt; use std::collections::BTreeMap; use std::fmt::Formatter; +use itertools; /// Represents a mass spectrum with associated m/z values and intensities. #[derive(Clone)] @@ -78,7 +79,13 @@ impl MzSpectrum { /// Formats the `MzSpectrum` for display. impl fmt::Display for MzSpectrum { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "MzSpectrum(mz: {:?}, intensity: {:?})", self.mz, self.intensity) + + let (mz, i) = self.mz.iter() + .zip(&self.intensity) + .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal)) + .unwrap(); + + write!(f, "MzSpectrum(data points: {}, max value:({}, {}))", self.mz.len(), mz, i) } } @@ -181,15 +188,15 @@ impl TOFMzSpectrum { let mut mz_bins: BTreeMap)> = BTreeMap::new(); let factor = 10f64.powi(resolution as i32); - for ((mz, inten), tof_val) in self.mz.iter().zip(self.intensity.iter()).zip(&self.tof) { + for ((mz, intensity), tof_val) in self.mz.iter().zip(self.intensity.iter()).zip(&self.tof) { let key = (mz * factor).round() as i64; let entry = mz_bins.entry(key).or_insert((0.0, Vec::new())); - entry.0 += *inten; + entry.0 += *intensity; entry.1.push(*tof_val as i64); } let mz: Vec = mz_bins.keys().map(|&key| key as f64 / factor).collect(); - let intensity: Vec = mz_bins.values().map(|(inten, _)| *inten).collect(); + let intensity: Vec = mz_bins.values().map(|(intensity, _)| *intensity).collect(); let tof: Vec = mz_bins.values().map(|(_, tof_vals)| { let sum: i64 = tof_vals.iter().sum(); let count: i32 = tof_vals.len() as i32; @@ -200,6 +207,18 @@ impl TOFMzSpectrum { } } +impl fmt::Display for TOFMzSpectrum { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let (mz, i) = self.mz.iter() + .zip(&self.intensity) + .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal)) + .unwrap(); + + write!(f, "TOFMzSpectrum(data points: {}, max value:({}, {}))", self.mz.len(), mz, i) + } +} + + #[derive(Clone)] pub struct ImsSpectrum { pub retention_time: f64, @@ -207,6 +226,12 @@ pub struct ImsSpectrum { pub spectrum: MzSpectrum, } +impl fmt::Display for ImsSpectrum { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "ImsSpectrum(rt: {}, inv_mobility: {}, spectrum: {})", self.retention_time, self.inv_mobility, self.spectrum) + } +} + #[derive(Clone)] pub struct TimsSpectrum { pub frame_id: i32, @@ -222,6 +247,12 @@ impl TimsSpectrum { } } +impl fmt::Display for TimsSpectrum { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "TimsSpectrum(frame_id: {}, scan_id: {}, retention_time: {}, inv_mobility: {}, spectrum: {})", self.frame_id, self.scan_id, self.retention_time, self.inv_mobility, self.spectrum) + } +} + #[derive(Clone)] pub struct ImsFrame { pub retention_time: f64, @@ -252,7 +283,13 @@ impl ImsFrame { } } -#[derive(Clone, Debug)] +impl fmt::Display for ImsFrame { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "ImsFrame(rt: {}, data points: {})", self.retention_time, self.inv_mobility.len()) + } +} + +#[derive(Clone)] pub struct TimsFrame { pub frame_id: i32, pub retention_time: f64, @@ -290,17 +327,43 @@ impl TimsFrame { pub fn to_ims_frame(&self) -> ImsFrame { ImsFrame { retention_time: self.retention_time, inv_mobility: self.inv_mobility.clone(), mz: self.mz.clone(), intensity: self.intensity.clone() } } -} -impl fmt::Display for TimsFrame { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "TimsFrame(id: {}, rt: {}, number data points: {})", self.frame_id, self.retention_time, self.scan.len()) + pub fn to_tims_spectra(&self) -> Vec { + let mut spectra = BTreeMap::, Vec, Vec)>::new(); + + for (scan, inv_mobility, tof, mz, intensity) in itertools::multizip(( + &self.scan, + &self.inv_mobility, + &self.tof, + &self.mz, + &self.intensity, + )) { + let entry = spectra.entry(*scan).or_insert_with(|| (*inv_mobility, Vec::new(), Vec::new(), Vec::new())); + entry.1.push(*tof); + entry.2.push(*mz); + entry.3.push(*intensity); + } + + let mut tims_spectra: Vec = Vec::new(); + + for (scan, (inv_mobility, tof, mz, intensity)) in spectra { + let spectrum = TOFMzSpectrum::new(tof, mz, intensity); + tims_spectra.push(TimsSpectrum::new(self.frame_id, scan, self.retention_time, inv_mobility, spectrum)); + } + + tims_spectra } } -impl fmt::Display for ImsFrame { +impl fmt::Display for TimsFrame { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "ImsFrame(rt: {}, number data points: {})", self.retention_time, self.inv_mobility.len()) + + let (mz, i) = self.mz.iter() + .zip(&self.intensity) + .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal)) + .unwrap(); + + write!(f, "TimsFrame(id: {}, rt: {}, data points: {}, max value: (mz: {}, intensity: {}))", self.frame_id, self.retention_time, self.scan.len(), mz, i) } } diff --git a/rustdf/src/main.rs b/rustdf/src/main.rs index 1ab2cd01..d95083dc 100644 --- a/rustdf/src/main.rs +++ b/rustdf/src/main.rs @@ -6,10 +6,12 @@ fn main() { let tims_data = TimsDataset::new(bruker_lib_path, data_path); match tims_data { Ok(tims_data) => { - for i in 1..100 { + for i in 25_000..30_000 { let frame = tims_data.get_frame(i); match frame { - Ok(frame) => println!("{}", frame), + Ok(frame) => { + println!("{}", frame); + }, Err(e) => println!("error: {}", e), }; }