Skip to main content

timsrust_core/
coordinates.rs

1#[macro_use]
2mod macros;
3mod charge;
4mod converters;
5
6use timsrust_utils::custom_error;
7
8pub use charge::*;
9pub use converters::*;
10
11pub const PROTON_MASS: Mass = Mass(1.007276466812);
12
13custom_error!(pub CoordinateError);
14
15index_dimension!(FrameIndex);
16value_dimension!(Rt);
17impl ConvertibleTo<Rt> for FrameIndex {}
18impl ConvertibleTo<FrameIndex> for Rt {}
19
20index_dimension!(ScanIndex);
21value_dimension!(Im);
22impl ConvertibleTo<Im> for ScanIndex {}
23impl ConvertibleTo<ScanIndex> for Im {}
24
25index_dimension!(IntensityIndex);
26value_dimension!(Intensity);
27impl ConvertibleTo<Intensity> for IntensityIndex {}
28impl ConvertibleTo<IntensityIndex> for Intensity {}
29
30index_dimension!(TofIndex);
31value_dimension!(Mz);
32value_dimension!(Mass);
33value_dimension!(Mh);
34impl ConvertibleTo<Mz> for TofIndex {}
35impl ConvertibleTo<TofIndex> for Mz {}
36
37impl Mass {
38    pub fn to_mh(&self) -> Mh {
39        Mh(self.0 + PROTON_MASS.0)
40    }
41
42    pub fn to_mz(&self, charge: Charge) -> Mz {
43        Mz(self.0 / i8::from(charge) as f64 + PROTON_MASS.0)
44    }
45}
46
47impl Mh {
48    pub fn to_mass(&self) -> Mass {
49        Mass(self.0 - PROTON_MASS.0)
50    }
51}
52
53impl Mz {
54    pub fn to_mass(&self, charge: Charge) -> Mass {
55        Mass((self.0 - PROTON_MASS.0) * (i8::from(charge) as f64))
56    }
57}
58
59macro_rules! bit_conversion {
60    (
61        $index:ident, $value:ident
62    ) => {
63        impl Converter<$index, $value> for BitConverter {
64            fn convert(&self, value: $index) -> $value {
65                let bits = u32::from(value);
66                $value::from(f32::from_bits(bits))
67            }
68        }
69
70        impl Converter<$value, $index> for BitConverter {
71            fn convert(&self, value: $value) -> $index {
72                let bits = (f64::from(value) as f32).to_bits();
73                $index::try_from(bits)
74                    .expect("TofIndex conversion out of bounds")
75            }
76        }
77    };
78}
79
80bit_conversion!(TofIndex, Mz);
81bit_conversion!(ScanIndex, Im);
82bit_conversion!(FrameIndex, Rt);