rustdf/data/
dataset.rs

1use crate::data::acquisition::AcquisitionMode;
2use crate::data::handle::{IndexConverter, TimsData, TimsDataLoader};
3use crate::data::meta::{read_global_meta_sql, read_meta_data_sql};
4use mscore::timstof::frame::{RawTimsFrame, TimsFrame};
5use mscore::timstof::slice::TimsSlice;
6
7pub struct TimsDataset {
8    pub loader: TimsDataLoader,
9}
10
11impl TimsDataset {
12    pub fn new(
13        bruker_lib_path: &str,
14        data_path: &str,
15        in_memory: bool,
16        use_bruker_sdk: bool,
17    ) -> Self {
18        // TODO: error handling
19        let global_meta_data = read_global_meta_sql(data_path).unwrap();
20        let meta_data = read_meta_data_sql(data_path).unwrap();
21
22        let scan_max_index = meta_data.iter().map(|x| x.num_scans).max().unwrap() as u32;
23        let im_lower = global_meta_data.one_over_k0_range_lower;
24        let im_upper = global_meta_data.one_over_k0_range_upper;
25
26        let tof_max_index = global_meta_data.tof_max_index;
27        let mz_lower = global_meta_data.mz_acquisition_range_lower;
28        let mz_upper = global_meta_data.mz_acquisition_range_upper;
29
30        let loader = match in_memory {
31            true => TimsDataLoader::new_in_memory(
32                bruker_lib_path,
33                data_path,
34                use_bruker_sdk,
35                scan_max_index,
36                im_lower,
37                im_upper,
38                tof_max_index,
39                mz_lower,
40                mz_upper,
41            ),
42            false => TimsDataLoader::new_lazy(
43                bruker_lib_path,
44                data_path,
45                use_bruker_sdk,
46                scan_max_index,
47                im_lower,
48                im_upper,
49                tof_max_index,
50                mz_lower,
51                mz_upper,
52            ),
53        };
54
55        TimsDataset { loader }
56    }
57
58    /// Create a new dataset with pre-computed ion mobility calibration lookup table.
59    ///
60    /// This enables accurate ion mobility calibration with fast parallel extraction.
61    /// The im_lookup table should be pre-computed using the Bruker SDK.
62    ///
63    /// # Arguments
64    /// * `data_path` - Path to the .d folder
65    /// * `in_memory` - Whether to load all data into memory
66    /// * `im_lookup` - Pre-computed scan→1/K0 lookup table from Bruker SDK
67    ///
68    /// # Returns
69    /// A new TimsDataset with LookupIndexConverter
70    pub fn new_with_calibration(
71        data_path: &str,
72        in_memory: bool,
73        im_lookup: Vec<f64>,
74    ) -> Self {
75        let global_meta_data = read_global_meta_sql(data_path).unwrap();
76
77        let tof_max_index = global_meta_data.tof_max_index;
78        let mz_lower = global_meta_data.mz_acquisition_range_lower;
79        let mz_upper = global_meta_data.mz_acquisition_range_upper;
80
81        let loader = match in_memory {
82            true => TimsDataLoader::new_in_memory_with_calibration(
83                data_path,
84                tof_max_index,
85                mz_lower,
86                mz_upper,
87                im_lookup,
88            ),
89            false => TimsDataLoader::new_lazy_with_calibration(
90                data_path,
91                tof_max_index,
92                mz_lower,
93                mz_upper,
94                im_lookup,
95            ),
96        };
97
98        TimsDataset { loader }
99    }
100
101    /// Check if the Bruker SDK is being used for index conversion.
102    /// Returns false for both Simple and Lookup converters (which are thread-safe).
103    pub fn uses_bruker_sdk(&self) -> bool {
104        self.loader.uses_bruker_sdk()
105    }
106}
107
108impl TimsData for TimsDataset {
109    // Get a frame by its id
110    fn get_frame(&self, frame_id: u32) -> TimsFrame {
111        self.loader.get_frame(frame_id)
112    }
113    // Get a raw frame by its id
114    fn get_raw_frame(&self, frame_id: u32) -> RawTimsFrame {
115        self.loader.get_raw_frame(frame_id)
116    }
117    // Get a collection of frames by their ids
118    fn get_slice(&self, frame_ids: Vec<u32>, num_threads: usize) -> TimsSlice {
119        self.loader.get_slice(frame_ids, num_threads)
120    }
121    // Get the acquisition mode, DDA or DIA
122    fn get_acquisition_mode(&self) -> AcquisitionMode {
123        self.loader.get_acquisition_mode().clone()
124    }
125    // Get total number of frames in the dataset
126    fn get_frame_count(&self) -> i32 {
127        self.loader.get_frame_count()
128    }
129    // Get the path to the data
130    fn get_data_path(&self) -> &str {
131        &self.loader.get_data_path()
132    }
133}
134
135impl IndexConverter for TimsDataset {
136    fn tof_to_mz(&self, frame_id: u32, tof_values: &Vec<u32>) -> Vec<f64> {
137        self.loader
138            .get_index_converter()
139            .tof_to_mz(frame_id, tof_values)
140    }
141    // convert m/z values to TOF values given a valid data handle and frame id
142    fn mz_to_tof(&self, frame_id: u32, mz_values: &Vec<f64>) -> Vec<u32> {
143        self.loader
144            .get_index_converter()
145            .mz_to_tof(frame_id, mz_values)
146    }
147    // convert inverse mobility values to scan values given a valid data handle and frame id
148    fn scan_to_inverse_mobility(&self, frame_id: u32, scan_values: &Vec<u32>) -> Vec<f64> {
149        self.loader
150            .get_index_converter()
151            .scan_to_inverse_mobility(frame_id, scan_values)
152    }
153    // convert scan values to inverse mobility values given a valid data handle and frame id
154    fn inverse_mobility_to_scan(
155        &self,
156        frame_id: u32,
157        inverse_mobility_values: &Vec<f64>,
158    ) -> Vec<u32> {
159        self.loader
160            .get_index_converter()
161            .inverse_mobility_to_scan(frame_id, inverse_mobility_values)
162    }
163}