rustdf/data/
meta.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
extern crate rusqlite;

use rusqlite::{Connection, Result};
use std::path::Path;

#[derive(Debug, Clone)]
pub struct DiaMsMisInfo {
    pub frame_id: u32,
    pub window_group: u32,
}

#[derive(Debug, Clone)]
pub struct DiaMsMsWindow {
    pub window_group: u32,
    pub scan_num_begin: u32,
    pub scan_num_end: u32,
    pub isolation_mz: f64,
    pub isolation_width: f64,
    pub collision_energy: f64,
}

#[derive(Debug, Clone)]
pub struct PasefMsMsMeta {
    pub frame_id: i64,
    pub scan_num_begin: i64,
    pub scan_num_end: i64,
    pub isolation_mz: f64,
    pub isolation_width: f64,
    pub collision_energy: f64,
    pub precursor_id: i64,
}

#[derive(Debug, Clone)]
pub struct DDAPrecursorMeta {
    pub precursor_id: i64,
    pub precursor_mz_highest_intensity: f64,
    pub precursor_mz_average: f64,
    pub precursor_mz_monoisotopic: Option<f64>,
    pub precursor_charge: Option<i64>,
    pub precursor_average_scan_number: f64,
    pub precursor_total_intensity: f64,
    pub precursor_frame_id: i64,
}

#[derive(Debug, Clone)]
pub struct DDAPrecursor {
    pub frame_id: i64,
    pub precursor_id: i64,
    pub mono_mz: Option<f64>,
    pub highest_intensity_mz: f64,
    pub average_mz: f64,
    pub charge: Option<i64>,
    pub inverse_ion_mobility: f64,
    pub collision_energy: f64,
    pub precuror_total_intensity: f64,
    pub isolation_mz: f64,
    pub isolation_width: f64,
}

#[derive(Debug, Clone)]
pub struct DDAFragmentInfo {
    pub frame_id: i64,
    pub scan_begin: i64,
    pub scan_end: i64,
    pub isolation_mz: f64,
    pub isolation_width: f64,
    pub collision_energy: f64,
    pub precursor_id: i64,
}

pub struct DIAFragmentFrameInfo {}

pub struct DIAWindowGroupInfo {}

#[derive(Debug)]
pub struct GlobalMetaData {
    pub schema_type: String,
    pub schema_version_major: i64,
    pub schema_version_minor: i64,
    pub acquisition_software_vendor: String,
    pub instrument_vendor: String,
    pub closed_property: i64,
    pub tims_compression_type: i64,
    pub max_num_peaks_per_scan: i64,
    pub mz_acquisition_range_lower: f64,
    pub mz_acquisition_range_upper: f64,
    pub one_over_k0_range_lower: f64,
    pub one_over_k0_range_upper: f64,
    pub tof_max_index: u32,
}

#[derive(Debug)]
pub struct FrameMeta {
    pub id: i64,
    pub time: f64,
    pub polarity: String,
    pub scan_mode: i64,
    pub ms_ms_type: i64,
    pub tims_id: i64,
    pub max_intensity: f64,
    pub sum_intensity: f64,
    pub num_scans: i64,
    pub num_peaks: i64,
    pub mz_calibration: i64,
    pub t_1: f64,
    pub t_2: f64,
    pub tims_calibration: i64,
    pub property_group: i64,
    pub accumulation_time: f64,
    pub ramp_time: f64,
}

struct GlobalMetaInternal {
    key: String,
    value: String,
}

pub fn read_dda_precursor_meta(
    bruker_d_folder_name: &str,
) -> Result<Vec<DDAPrecursorMeta>, Box<dyn std::error::Error>> {
    // Connect to the database
    let db_path = Path::new(bruker_d_folder_name).join("analysis.tdf");
    let conn = Connection::open(db_path)?;

    // prepare the query
    let rows: Vec<&str> = vec![
        "Id",
        "LargestPeakMz",
        "AverageMz",
        "MonoisotopicMz",
        "Charge",
        "ScanNumber",
        "Intensity",
        "Parent",
    ];
    let query = format!("SELECT {} FROM Precursors", rows.join(", "));

    // execute the query
    let frames_rows: Result<Vec<DDAPrecursorMeta>, _> = conn
        .prepare(&query)?
        .query_map([], |row| {
            Ok(DDAPrecursorMeta {
                precursor_id: row.get(0)?,
                precursor_mz_highest_intensity: row.get(1)?,
                precursor_mz_average: row.get(2)?,
                precursor_mz_monoisotopic: row.get(3)?, // Now using Option<f64>
                precursor_charge: row.get(4)?,          // Now using Option<i64>
                precursor_average_scan_number: row.get(5)?,
                precursor_total_intensity: row.get(6)?,
                precursor_frame_id: row.get(7)?,
            })
        })?
        .collect();

    // return the frames
    Ok(frames_rows?)
}

pub fn read_pasef_frame_ms_ms_info(
    bruker_d_folder_name: &str,
) -> Result<Vec<PasefMsMsMeta>, Box<dyn std::error::Error>> {
    // Connect to the database
    let db_path = Path::new(bruker_d_folder_name).join("analysis.tdf");
    let conn = Connection::open(db_path)?;

    // prepare the query
    let rows: Vec<&str> = vec![
        "Frame",
        "ScanNumBegin",
        "ScanNumEnd",
        "IsolationMz",
        "IsolationWidth",
        "CollisionEnergy",
        "Precursor",
    ];
    let query = format!("SELECT {} FROM PasefFrameMsMsInfo", rows.join(", "));

    // execute the query
    let frames_rows: Result<Vec<PasefMsMsMeta>, _> = conn
        .prepare(&query)?
        .query_map([], |row| {
            Ok(PasefMsMsMeta {
                frame_id: row.get(0)?,
                scan_num_begin: row.get(1)?,
                scan_num_end: row.get(2)?,
                isolation_mz: row.get(3)?,
                isolation_width: row.get(4)?,
                collision_energy: row.get(5)?,
                precursor_id: row.get(6)?,
            })
        })?
        .collect();

    // return the frames
    Ok(frames_rows?)
}

// Read the global meta data from the analysis.tdf file
pub fn read_global_meta_sql(
    bruker_d_folder_name: &str,
) -> Result<GlobalMetaData, Box<dyn std::error::Error>> {
    // Connect to the database
    let db_path = Path::new(bruker_d_folder_name).join("analysis.tdf");
    let conn = Connection::open(db_path)?;

    // execute the query
    let frames_rows: Result<Vec<GlobalMetaInternal>, _> = conn
        .prepare("SELECT * FROM GlobalMetadata")?
        .query_map([], |row| {
            Ok(GlobalMetaInternal {
                key: row.get(0)?,
                value: row.get(1)?,
            })
        })?
        .collect();

    let mut global_meta = GlobalMetaData {
        schema_type: String::new(),
        schema_version_major: -1,
        schema_version_minor: -1,
        acquisition_software_vendor: String::new(),
        instrument_vendor: String::new(),
        closed_property: -1,
        tims_compression_type: -1,
        max_num_peaks_per_scan: -1,
        mz_acquisition_range_lower: -1.0,
        mz_acquisition_range_upper: -1.0,
        one_over_k0_range_lower: -1.0,
        one_over_k0_range_upper: -1.0,
        tof_max_index: 0,
    };

    // go over the keys and parse values for the global meta data
    for row in frames_rows? {
        match row.key.as_str() {
            "SchemaType" => global_meta.schema_type = row.value,
            "SchemaVersionMajor" => {
                global_meta.schema_version_major = row.value.parse::<i64>().unwrap()
            }
            "SchemaVersionMinor" => {
                global_meta.schema_version_minor = row.value.parse::<i64>().unwrap()
            }
            "AcquisitionSoftwareVendor" => global_meta.acquisition_software_vendor = row.value,
            "InstrumentVendor" => global_meta.instrument_vendor = row.value,
            "ClosedProperly" => global_meta.closed_property = row.value.parse::<i64>().unwrap(),
            "TimsCompressionType" => {
                global_meta.tims_compression_type = row.value.parse::<i64>().unwrap()
            }
            "MaxNumPeaksPerScan" => {
                global_meta.max_num_peaks_per_scan = row.value.parse::<i64>().unwrap()
            }
            "MzAcqRangeLower" => {
                global_meta.mz_acquisition_range_lower = row.value.parse::<f64>().unwrap()
            }
            "MzAcqRangeUpper" => {
                global_meta.mz_acquisition_range_upper = row.value.parse::<f64>().unwrap()
            }
            "OneOverK0AcqRangeLower" => {
                global_meta.one_over_k0_range_lower = row.value.parse::<f64>().unwrap()
            }
            "OneOverK0AcqRangeUpper" => {
                global_meta.one_over_k0_range_upper = row.value.parse::<f64>().unwrap()
            }
            "DigitizerNumSamples" => {
                global_meta.tof_max_index = (row.value.parse::<i64>().unwrap() + 1) as u32
            }
            _ => (),
        }
    }
    // return global_meta
    Ok(global_meta)
}

// Read the frame meta data from the analysis.tdf file
pub fn read_meta_data_sql(
    bruker_d_folder_name: &str,
) -> Result<Vec<FrameMeta>, Box<dyn std::error::Error>> {
    // Connect to the database
    let db_path = Path::new(bruker_d_folder_name).join("analysis.tdf");
    let conn = Connection::open(db_path)?;

    // prepare the query
    let rows: Vec<&str> = vec![
        "Id",
        "Time",
        "ScanMode",
        "Polarity",
        "MsMsType",
        "TimsId",
        "MaxIntensity",
        "SummedIntensities",
        "NumScans",
        "NumPeaks",
        "MzCalibration",
        "T1",
        "T2",
        "TimsCalibration",
        "PropertyGroup",
        "AccumulationTime",
        "RampTime",
    ];
    let query = format!("SELECT {} FROM Frames", rows.join(", "));

    // execute the query
    let frames_rows: Result<Vec<FrameMeta>, _> = conn
        .prepare(&query)?
        .query_map([], |row| {
            Ok(FrameMeta {
                id: row.get(0)?,
                time: row.get(1)?,
                scan_mode: row.get(2)?,
                polarity: row.get(3)?,
                ms_ms_type: row.get(4)?,
                tims_id: row.get(5)?,
                max_intensity: row.get(6)?,
                sum_intensity: row.get(7)?,
                num_scans: row.get(8)?,
                num_peaks: row.get(9)?,
                mz_calibration: row.get(10)?,
                t_1: row.get(11)?,
                t_2: row.get(12)?,
                tims_calibration: row.get(13)?,
                property_group: row.get(14)?,
                accumulation_time: row.get(15)?,
                ramp_time: row.get(16)?,
            })
        })?
        .collect();

    // return the frames
    Ok(frames_rows?)
}

pub fn read_dia_ms_ms_info(
    bruker_d_folder_name: &str,
) -> Result<Vec<DiaMsMisInfo>, Box<dyn std::error::Error>> {
    // Connect to the database
    let db_path = Path::new(bruker_d_folder_name).join("analysis.tdf");
    let conn = Connection::open(db_path)?;

    // prepare the query
    let rows: Vec<&str> = vec!["Frame", "WindowGroup"];
    let query = format!("SELECT {} FROM DiaFrameMsMsInfo", rows.join(", "));

    // execute the query
    let frames_rows: Result<Vec<DiaMsMisInfo>, _> = conn
        .prepare(&query)?
        .query_map([], |row| {
            Ok(DiaMsMisInfo {
                frame_id: row.get(0)?,
                window_group: row.get(1)?,
            })
        })?
        .collect();

    // return the frames
    Ok(frames_rows?)
}

pub fn read_dia_ms_ms_windows(
    bruker_d_folder_name: &str,
) -> Result<Vec<DiaMsMsWindow>, Box<dyn std::error::Error>> {
    // Connect to the database
    let db_path = Path::new(bruker_d_folder_name).join("analysis.tdf");
    let conn = Connection::open(db_path)?;

    // prepare the query
    let rows: Vec<&str> = vec![
        "WindowGroup",
        "ScanNumBegin",
        "ScanNumEnd",
        "IsolationMz",
        "IsolationWidth",
        "CollisionEnergy",
    ];
    let query = format!("SELECT {} FROM DiaFrameMsMsWindows", rows.join(", "));

    // execute the query
    let frames_rows: Result<Vec<DiaMsMsWindow>, _> = conn
        .prepare(&query)?
        .query_map([], |row| {
            Ok(DiaMsMsWindow {
                window_group: row.get(0)?,
                scan_num_begin: row.get(1)?,
                scan_num_end: row.get(2)?,
                isolation_mz: row.get(3)?,
                isolation_width: row.get(4)?,
                collision_energy: row.get(5)?,
            })
        })?
        .collect();

    // return the frames
    Ok(frames_rows?)
}