-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHORE: refactored converters to be more explicit
- Loading branch information
1 parent
26fc5b8
commit 0211d13
Showing
11 changed files
with
107 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,12 @@ | ||
use linreg::linear_regression; | ||
mod frame_to_rt; | ||
mod scan_to_im; | ||
mod tof_to_mz; | ||
|
||
/// Converting from an index domain (e.g. Time of Flight) to a continuous domain (m/z). | ||
pub trait ConvertableIndex { | ||
/// Convert any index (even fractional) to a continuous value. | ||
fn convert<T: Into<f64> + Copy>(&self, index: T) -> f64; | ||
} | ||
|
||
/// A converter from TOF -> m/z. | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct Tof2MzConverter { | ||
tof_intercept: f64, | ||
tof_slope: f64, | ||
} | ||
|
||
impl Tof2MzConverter { | ||
pub fn new(mz_min: f64, mz_max: f64, tof_max_index: u32) -> Self { | ||
let tof_intercept: f64 = mz_min.sqrt(); | ||
let tof_slope: f64 = | ||
(mz_max.sqrt() - tof_intercept) / tof_max_index as f64; | ||
Self { | ||
tof_intercept, | ||
tof_slope, | ||
} | ||
} | ||
|
||
pub fn from_unfragmented_precursors(data: &Vec<(f64, u32)>) -> Self { | ||
let x: Vec<u32> = data.iter().map(|(_, x_val)| *x_val).collect(); | ||
let y: Vec<f64> = | ||
data.iter().map(|(y_val, _)| (*y_val).sqrt()).collect(); | ||
let (tof_slope, tof_intercept) = linear_regression(&x, &y).unwrap(); | ||
Self { | ||
tof_intercept, | ||
tof_slope, | ||
} | ||
} | ||
} | ||
|
||
impl ConvertableIndex for Tof2MzConverter { | ||
fn convert<T: Into<f64> + Copy>(&self, index: T) -> f64 { | ||
let tof_index_f64: f64 = index.into(); | ||
(self.tof_intercept + self.tof_slope * tof_index_f64).powi(2) | ||
} | ||
} | ||
|
||
/// A converter from Scan -> ion mobility. | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct Scan2ImConverter { | ||
scan_intercept: f64, | ||
scan_slope: f64, | ||
} | ||
|
||
impl Scan2ImConverter { | ||
pub fn new(im_min: f64, im_max: f64, scan_max_index: u32) -> Self { | ||
let scan_intercept: f64 = im_max; | ||
let scan_slope: f64 = (im_min - scan_intercept) / scan_max_index as f64; | ||
Self { | ||
scan_intercept, | ||
scan_slope, | ||
} | ||
} | ||
} | ||
|
||
impl ConvertableIndex for Scan2ImConverter { | ||
fn convert<T: Into<f64> + Copy>(&self, index: T) -> f64 { | ||
let scan_index_f64: f64 = index.into(); | ||
self.scan_intercept + self.scan_slope * scan_index_f64 | ||
} | ||
} | ||
|
||
/// A converter from Frame -> retention time. | ||
#[derive(Debug, Clone)] | ||
pub struct Frame2RtConverter { | ||
rt_values: Vec<f64>, | ||
} | ||
|
||
impl Frame2RtConverter { | ||
pub fn new(rt_values: Vec<f64>) -> Self { | ||
Self { rt_values } | ||
} | ||
} | ||
pub use frame_to_rt::Frame2RtConverter; | ||
pub use scan_to_im::Scan2ImConverter; | ||
pub use tof_to_mz::Tof2MzConverter; | ||
|
||
impl ConvertableIndex for Frame2RtConverter { | ||
fn convert<T: Into<f64> + Copy>(&self, index: T) -> f64 { | ||
let lower_value: f64 = self.rt_values[index.into().floor() as usize]; | ||
let upper_value: f64 = self.rt_values[index.into().ceil() as usize]; | ||
(lower_value + upper_value) / 2. | ||
} | ||
/// Convert from one domain (e.g. Time of Flight) to a another (m/z). | ||
pub trait ConvertableDomain { | ||
fn convert<T: Into<f64> + Copy>(&self, value: T) -> f64; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/// A converter from Frame -> retention time. | ||
#[derive(Debug, Clone)] | ||
pub struct Frame2RtConverter { | ||
rt_values: Vec<f64>, | ||
} | ||
|
||
impl Frame2RtConverter { | ||
pub fn from_values(rt_values: Vec<f64>) -> Self { | ||
Self { rt_values } | ||
} | ||
} | ||
|
||
impl super::ConvertableDomain for Frame2RtConverter { | ||
fn convert<T: Into<f64> + Copy>(&self, value: T) -> f64 { | ||
let lower_value: f64 = self.rt_values[value.into().floor() as usize]; | ||
let upper_value: f64 = self.rt_values[value.into().ceil() as usize]; | ||
(lower_value + upper_value) / 2. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/// A converter from Scan -> (inversed) ion mobility. | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct Scan2ImConverter { | ||
scan_intercept: f64, | ||
scan_slope: f64, | ||
} | ||
|
||
impl Scan2ImConverter { | ||
pub fn from_boundaries( | ||
im_min: f64, | ||
im_max: f64, | ||
scan_max_index: u32, | ||
) -> Self { | ||
let scan_intercept: f64 = im_max; | ||
let scan_slope: f64 = (im_min - scan_intercept) / scan_max_index as f64; | ||
Self { | ||
scan_intercept, | ||
scan_slope, | ||
} | ||
} | ||
} | ||
|
||
impl super::ConvertableDomain for Scan2ImConverter { | ||
fn convert<T: Into<f64> + Copy>(&self, value: T) -> f64 { | ||
let scan_index_f64: f64 = value.into(); | ||
self.scan_intercept + self.scan_slope * scan_index_f64 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use linreg::linear_regression; | ||
|
||
/// A converter from TOF -> m/z. | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct Tof2MzConverter { | ||
tof_intercept: f64, | ||
tof_slope: f64, | ||
} | ||
|
||
impl Tof2MzConverter { | ||
pub fn from_boundaries( | ||
mz_min: f64, | ||
mz_max: f64, | ||
tof_max_index: u32, | ||
) -> Self { | ||
let tof_intercept: f64 = mz_min.sqrt(); | ||
let tof_slope: f64 = | ||
(mz_max.sqrt() - tof_intercept) / tof_max_index as f64; | ||
Self { | ||
tof_intercept, | ||
tof_slope, | ||
} | ||
} | ||
|
||
pub fn from_pairs(data: &Vec<(f64, u32)>) -> Self { | ||
let x: Vec<u32> = data.iter().map(|(_, x_val)| *x_val).collect(); | ||
let y: Vec<f64> = | ||
data.iter().map(|(y_val, _)| (*y_val).sqrt()).collect(); | ||
let (tof_slope, tof_intercept) = linear_regression(&x, &y).unwrap(); | ||
Self { | ||
tof_intercept, | ||
tof_slope, | ||
} | ||
} | ||
} | ||
|
||
impl super::ConvertableDomain for Tof2MzConverter { | ||
fn convert<T: Into<f64> + Copy>(&self, value: T) -> f64 { | ||
let tof_index_f64: f64 = value.into(); | ||
(self.tof_intercept + self.tof_slope * tof_index_f64).powi(2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters