mzdata 0.63.4

A library to read mass spectrometry data formats and a data model for mass spectra
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
use std::{
    collections::HashMap, io, iter::FusedIterator, marker::PhantomData, ops::Range, path::Path,
    sync::Arc,
};

use chrono::DateTime;

#[allow(unused)]
use crate::io::checksum_file;

use crate::{
    curie,
    io::{DetailLevel, IntoIonMobilityFrameSource, IonMobilityFrameAccessError, OffsetIndex},
    meta::{
        Component, ComponentType, DataProcessing, DetectorTypeTerm,
        DissociationMethodTerm::CollisionInducedDissociation, FileDescription,
        InstrumentConfiguration, MassAnalyzerTerm, MassSpectrometerFileFormatTerm,
        MassSpectrometryRun, NativeSpectrumIdentifierFormatTerm, Sample, Software, SoftwareTerm,
        SourceFile,
    },
    mzpeaks::{
        feature::{ChargedFeature, Feature},
        CentroidPeak, DeconvolutedPeak, IonMobility, Mass, MZ,
    },
    params::{ControlledVocabulary, Unit, Value},
    prelude::*,
    spectrum::{
        bindata::{ArrayRetrievalError, BinaryArrayMap3D},
        Activation, ArrayType, BinaryArrayMap, BinaryDataArrayType, Chromatogram,
        ChromatogramDescription, ChromatogramType, DataArray, IonMobilityFrameDescription,
        IsolationWindow, IsolationWindowState, MultiLayerIonMobilityFrame, MultiLayerSpectrum,
        Precursor, ScanCombination, ScanEvent, ScanWindow, SelectedIon, SignalContinuity,
    },
    Param,
};
use identity_hash::BuildIdentityHasher;
use rusqlite::Error;

use timsrust::{
    converters::ConvertableDomain,
    readers::{FrameReader, FrameReaderError, MetadataReader},
    Metadata, TimsRustError,
};

pub use super::arrays::FrameToArraysMapper;
use super::{
    arrays::consolidate_peaks,
    constants::{InstrumentSource, MsMsType},
    sql::{
        ChromatographyData, FromSQL, PasefPrecursor, RawTDFSQLReader, SQLDIAFrameMsMsWindow,
        SQLFrame, SQLPasefFrameMsMs, SQLPrecursor, TDFMSnFacet,
    },
};

const PEAK_MERGE_TOLERANCE: Tolerance = Tolerance::PPM(10.0);

fn inverse_reduce_ion_mobility_param(value: f64) -> Param {
    ControlledVocabulary::MS
        .param_val(1002815, "inverse reduced ion mobility", value)
        .with_unit_t(&Unit::VoltSecondPerSquareCentimeter)
}

#[derive(Debug, Clone)]
pub struct IndexExtry {
    pub frame: Arc<SQLFrame>,
    pub index: usize,
    pub parent_index: Option<usize>,
    pub tdf_facet: TDFMSnFacet,
}

impl IndexExtry {
    pub fn new(
        frame: Arc<SQLFrame>,
        index: usize,
        parent_index: Option<usize>,
        tdf_facet: TDFMSnFacet,
    ) -> Self {
        Self {
            frame,
            index,
            parent_index,
            tdf_facet,
        }
    }

    pub fn scan_range(&self) -> (usize, usize) {
        match self.ms_type() {
            MsMsType::MS1 => (0, self.frame.num_scans),
            MsMsType::DDAPASEF => {
                if let Some(pasef) = self.tdf_facet.pasef_msms() {
                    (pasef.scan_start, pasef.scan_end)
                } else {
                    (0, self.frame.num_scans)
                }
            }
            MsMsType::DIAPASEF => {
                if let Some(pasef) = self.tdf_facet.dia_window() {
                    (pasef.scan_start, pasef.scan_end)
                } else {
                    (0, self.frame.num_scans)
                }
            }
            MsMsType::MRM | MsMsType::PRMPASEF | MsMsType::Unknown => todo!(),
        }
    }

    pub fn format_native_id(&self) -> String {
        let (start_scan, end_scan) = self.scan_range();
        let start_scan = start_scan + 1;
        format!(
            "merged={} frame={} startScan={start_scan} endScan={end_scan}",
            self.index, self.frame.id,
        )
    }

    pub fn ms_type(&self) -> MsMsType {
        MsMsType::from(self.frame.msms_type)
    }

    pub fn ms_level(&self) -> u8 {
        self.ms_type().ms_level()
    }

    pub fn pasef_msms(&self) -> Option<&SQLPasefFrameMsMs> {
        self.tdf_facet.pasef_msms()
    }

    pub fn precursor(&self) -> Option<&SQLPrecursor> {
        self.tdf_facet.precursor()
    }

    pub fn dia_window(&self) -> Option<&SQLDIAFrameMsMsWindow> {
        self.tdf_facet.dia_window()
    }
}

/// An ion mobility frame reader for Bruker TDF file format. It supports the same
/// kind of PASEF frame traversal that is supported by [ProteoWizard](https://github.com/ProteoWizard/pwiz).
///
/// It implements the full range of [`IonMobilityFrameSource`]-derived traits. To view
/// these frames as flat peak lists, this type can be wrapped in a [`TDFSpectrumReaderType`]
/// using [`TDFFrameReaderType::into_spectrum_reader`].
#[derive(Debug)]
pub struct TDFFrameReaderType<
    C: FeatureLike<MZ, IonMobility> = Feature<MZ, IonMobility>,
    D: FeatureLike<Mass, IonMobility> + KnownCharge = ChargedFeature<Mass, IonMobility>,
> {
    metadata: timsrust::Metadata,
    frame_reader: timsrust::readers::FrameReader,
    tdf_reader: RawTDFSQLReader,
    entry_index: Vec<IndexExtry>,
    index: usize,
    offset_index: OffsetIndex,
    /// The description of the file's contents and the previous data files that were
    /// consumed to produce it.
    pub(crate) file_description: FileDescription,
    /// A mapping of different instrument configurations (source, analyzer, detector) components
    /// by ID string.
    pub(crate) instrument_configurations: HashMap<u32, InstrumentConfiguration>,
    /// The different software components that were involved in the processing and creation of this
    /// file.
    pub(crate) softwares: Vec<Software>,
    pub(crate) samples: Vec<Sample>,
    /// The data processing and signal transformation operations performed on the raw data in previous
    /// source files to produce this file's contents.
    pub(crate) data_processings: Vec<DataProcessing>,
    pub detail_level: DetailLevel,

    // SpectrumList attributes
    pub run: MassSpectrometryRun,
    _c: PhantomData<C>,
    _d: PhantomData<D>,
}

type DIAFrameWindowMap = HashMap<u32, Vec<Arc<SQLDIAFrameMsMsWindow>>, BuildIdentityHasher<u32>>;

impl<C: FeatureLike<MZ, IonMobility>, D: FeatureLike<Mass, IonMobility> + KnownCharge>
    TDFFrameReaderType<C, D>
{
    /// Construct a new reader from the specified file system path.
    ///
    /// # Errors
    /// This may fail if any of the component files fails to match the expected schema
    /// or layout.
    pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, timsrust::TimsRustError> {
        Self::new_with_detail_level(path, DetailLevel::Full)
    }

    /// Construct a new reader from the specified file system path with the specified
    /// [`DetailLevel`].
    ///
    /// # Errors
    /// This may fail if any of the component files fails to match the expected schema
    /// or layout.
    pub fn new_with_detail_level<P: AsRef<Path>>(
        path: P,
        detail_level: DetailLevel,
    ) -> Result<Self, timsrust::TimsRustError> {
        let path = path.as_ref();
        let tdf_path = path.join("analysis.tdf");
        if !tdf_path.exists() {
            return Err(timsrust::TimsRustError::FrameReaderError(
                FrameReaderError::FileNotFound(tdf_path.display().to_string()),
            ));
        }

        let metadata = MetadataReader::new(&tdf_path)?;
        let frame_reader = FrameReader::new(path)?;
        let tdf_reader = RawTDFSQLReader::new(&tdf_path)
            .map_err(|e| TimsRustError::FrameReaderError(FrameReaderError::SqlError(e.into())))?;

        let mut this = Self {
            metadata,
            frame_reader,
            tdf_reader,
            entry_index: Vec::new(),
            index: 0,
            offset_index: OffsetIndex::new("spectrum".into()),

            instrument_configurations: HashMap::default(),
            file_description: FileDescription::default(),
            softwares: Vec::new(),
            data_processings: Vec::new(),
            samples: Vec::new(),

            run: MassSpectrometryRun::default(),
            detail_level,

            _c: PhantomData,
            _d: PhantomData,
        };

        this.build_index()
            .map_err(|e| TimsRustError::FrameReaderError(FrameReaderError::SqlError(e.into())))?;
        this.build_metadata().unwrap();

        Ok(this)
    }

    /// Consume this reader, wrapping it in a [`TDFSpectrumReaderType`] with the default
    /// peak merging tolerance.
    pub fn into_spectrum_reader<CP: CentroidLike, DP: DeconvolutedCentroidLike>(
        self,
    ) -> TDFSpectrumReaderType<C, D, CP, DP> {
        self.into_spectrum_reader_with_peak_merging_tolerance(PEAK_MERGE_TOLERANCE, false)
    }

    /// Consume this reader, wrapping it in a [`TDFSpectrumReaderType`] with the
    /// specified peak merging error tolerance.
    pub fn into_spectrum_reader_with_peak_merging_tolerance<
        CP: CentroidLike,
        DP: DeconvolutedCentroidLike,
    >(
        self,
        peak_merging_tolerance: Tolerance,
        do_consolidate_peaks: bool,
    ) -> TDFSpectrumReaderType<C, D, CP, DP> {
        TDFSpectrumReaderType {
            frame_reader: self,
            peak_merging_tolerance,
            do_consolidate_peaks,
            _cp: PhantomData,
            _dp: PhantomData,
        }
    }

    fn guess_number_of_entries(&self) -> Result<(usize, Vec<MsMsType>), Error> {
        let conn = self.tdf_reader.connection();

        let mut stmt = conn
            .prepare("SELECT MsMsType, COUNT(1) FROM Frames GROUP BY MsMsType")
            .unwrap_or_else(|e| panic!("Failed to count scan types: {}", e));

        let counts_per_msms_type: Vec<(u8, usize)> = stmt
            .query([])?
            .mapped(|row| -> Result<(u8, usize), Error> { Ok((row.get(0)?, row.get(1)?)) })
            .flatten()
            .collect();

        let mut entry_count_guess = 0;
        let mut frame_types = Vec::new();
        for (msms_type, count_of) in counts_per_msms_type.iter().copied() {
            let ft = MsMsType::from(msms_type);
            frame_types.push(ft);
            let entry_count = match ft {
                MsMsType::MS1 | MsMsType::MRM | MsMsType::Unknown => count_of,
                MsMsType::DDAPASEF => conn.query_row(
                    "SELECT COUNT(1) FROM PasefFrameMsMsInfo pasef JOIN Precursors prec ON pasef.Precursor = prec.Id",
                    [], |row| {
                    row.get::<usize, usize>(0)
                }).unwrap_or_else(|e| panic!("Failed to count precursors: {}", e)),
                MsMsType::DIAPASEF => {
                    let mut q = conn.prepare(r#"SELECT frames_of.WindowGroup, FrameCount * GroupSize FROM
                    (SELECT info.WindowGroup, COUNT(1) AS FrameCount FROM Frames f JOIN DiaFrameMsMsInfo info ON f.Id = info.Frame GROUP BY info.WindowGroup) frames_of
                    JOIN
                    (SELECT WindowGroup AS WindowGroup, COUNT(1) AS GroupSize FROM DiaFrameMsMsWindows GROUP BY WindowGroup) as window_group_size
                    ON frames_of.WindowGroup = window_group_size.WindowGroup GROUP BY frames_of.WindowGroup"#)?;

                    let total = q.query_map([], |row| {
                        let count: usize = row.get(1)?;
                        Ok(count)
                    })?.flatten().sum::<usize>();
                    #[allow(clippy::let_and_return)]
                    total
                },
                MsMsType::PRMPASEF => todo!(),
            };
            entry_count_guess += entry_count;
        }

        Ok((entry_count_guess, frame_types))
    }

    fn build_window_index(&self) -> Result<DIAFrameWindowMap, Error> {
        let conn = self.tdf_reader.connection();
        let dia_windows = SQLDIAFrameMsMsWindow::read_from(&conn, []).unwrap_or_default();
        let mut window_groups: HashMap<u32, Vec<Arc<SQLDIAFrameMsMsWindow>>, _> =
            HashMap::default();
        for window in dia_windows {
            window_groups
                .entry(window.window_group)
                .or_default()
                .push(Arc::new(window));
        }
        Ok(window_groups)
    }

    fn build_index(&mut self) -> Result<(), Error> {
        let pasef_window_groups = self.build_window_index()?;

        let (entry_guess, _frame_types) = self
            .guess_number_of_entries()
            .unwrap_or_else(|e| panic!("Failed to estimate index size: {}", e));
        let mut index_entries = Vec::with_capacity(entry_guess);

        let conn = self.tdf_reader.connection();

        let precursors: Vec<_> = SQLPrecursor::read_from(&conn, [])
            .unwrap_or_default()
            .into_iter()
            .map(Arc::new)
            .collect();

        let pasef_msms_info: HashMap<u32, Vec<Arc<SQLPasefFrameMsMs>>, BuildIdentityHasher<u32>> =
            SQLPasefFrameMsMs::read_from(&conn, [])
                .unwrap_or_default()
                .into_iter()
                .map(Arc::new)
                .fold(HashMap::default(), |mut index, p| {
                    index.entry(p.frame as u32).or_default().push(p);
                    index
                });

        let q = format!("{} ORDER BY Id", SQLFrame::get_sql());
        let frames = self
            .tdf_reader
            .query::<SQLFrame>(&q, [])
            .unwrap_or_default();
        let mut frame_index = Vec::with_capacity(frames.len());
        let mut parent_index: HashMap<usize, usize> = HashMap::new();
        let mut last_parent = 0usize;
        for frame in frames.into_iter().map(Arc::new) {
            frame_index.push(frame.clone());
            if frame.num_peaks == 0 {
                continue;
            }
            let msms_type = MsMsType::from(frame.msms_type);
            match msms_type {
                MsMsType::MS1 => {
                    let i = index_entries.len();
                    last_parent = i;
                    parent_index.insert(frame.id, i);
                    index_entries.push(IndexExtry::new(frame, i, None, TDFMSnFacet::None));
                }
                MsMsType::DDAPASEF => {
                    let pasef_infos = pasef_msms_info.get(&(frame.id as u32)).unwrap();
                    for pasef_info in pasef_infos {
                        let precursors = precursors
                            .get(pasef_info.precursor.saturating_sub(1))
                            .cloned();
                        if let Some(precursor) = precursors {
                            let parent = parent_index.get(&precursor.precursor_frame).copied();
                            let entry = IndexExtry::new(
                                frame.clone(),
                                index_entries.len(),
                                parent,
                                PasefPrecursor::new(precursor, pasef_info.clone()).into(),
                            );
                            index_entries.push(entry);
                        }
                    }
                }
                MsMsType::DIAPASEF => {
                    let group_id = self.tdf_reader.dia_window_group_for(frame.id)?;
                    for window in pasef_window_groups.get(&group_id).into_iter().flatten() {
                        let entry = IndexExtry::new(
                            frame.clone(),
                            index_entries.len(),
                            Some(last_parent),
                            window.clone().into(),
                        );
                        index_entries.push(entry);
                    }
                }
                MsMsType::PRMPASEF => todo!(),
                MsMsType::MRM => todo!(),
                MsMsType::Unknown => todo!(),
            }
        }
        self.entry_index = index_entries;
        let mut offset_index = OffsetIndex::new("spectrum".into());
        self.entry_index.iter().enumerate().for_each(|(i, e)| {
            offset_index.insert(e.format_native_id().into_boxed_str(), i as u64);
        });
        self.offset_index = offset_index;
        Ok(())
    }

    fn build_chromatogram(&self, chromatogram_type: ChromatogramType) -> Option<Chromatogram> {
        let mut descr = ChromatogramDescription::default();
        match chromatogram_type {
            ChromatogramType::TotalIonCurrentChromatogram => {
                descr.chromatogram_type = ChromatogramType::TotalIonCurrentChromatogram;
                descr.id = "TIC".into();
                descr.index = 0;
            }
            ChromatogramType::BasePeakChromatogram => {
                descr.chromatogram_type = ChromatogramType::BasePeakChromatogram;
                descr.id = "BPC".into();
                descr.index = 1;
            }

            _ => return None,
        }

        let n = self.entry_index.len();
        let mut time_array: Vec<u8> =
            Vec::with_capacity(n * BinaryDataArrayType::Float64.size_of());
        let mut intensity_array: Vec<u8> =
            Vec::with_capacity(n * BinaryDataArrayType::Float32.size_of());

        for entry in self.entry_index.iter() {
            time_array.extend_from_slice(&(entry.frame.time / 60.0).to_le_bytes());
            match chromatogram_type {
                ChromatogramType::TotalIonCurrentChromatogram => {
                    intensity_array
                        .extend_from_slice(&entry.frame.summed_intensities.to_le_bytes());
                }
                ChromatogramType::BasePeakChromatogram => {
                    intensity_array.extend_from_slice(&entry.frame.max_intensity.to_le_bytes());
                }
                _ => {
                    unimplemented!()
                }
            }
        }

        let mut arrays = BinaryArrayMap::default();
        let mut time_array = DataArray::wrap(
            &ArrayType::TimeArray,
            BinaryDataArrayType::Float64,
            time_array,
        );
        time_array.unit = Unit::Minute;
        arrays.add(time_array);

        let intensity_array = DataArray::wrap(
            &ArrayType::IntensityArray,
            BinaryDataArrayType::Float32,
            intensity_array,
        );
        arrays.add(intensity_array);

        Some(Chromatogram::new(descr, arrays))
    }

    pub(crate) fn tic(&self) -> Chromatogram {
        self.build_chromatogram(ChromatogramType::TotalIonCurrentChromatogram)
            .unwrap()
    }

    pub(crate) fn bpc(&self) -> Chromatogram {
        self.build_chromatogram(ChromatogramType::BasePeakChromatogram)
            .unwrap()
    }

    fn index_by_time(&self, time: f64) -> Option<&IndexExtry> {
        let n = self.entry_index.len();
        let mut lo: usize = 0;
        let mut hi: usize = n;

        let mut best_error: f64 = f64::INFINITY;
        let mut best_match: Option<&IndexExtry> = None;

        if lo == hi {
            return None;
        }
        while hi != lo {
            let mid = (hi + lo) / 2;
            let scan = self.entry_index.get(mid).unwrap();
            let scan_time = scan.frame.time / 60.0;
            let err = (scan_time - time).abs();

            if err < best_error {
                best_error = err;
                best_match = Some(scan);
            }
            if hi.saturating_sub(1) == lo {
                return best_match;
            } else if scan_time > time {
                hi = mid;
            } else {
                lo = mid;
            }
        }
        best_match
    }

    fn frame_to_spectrum<CP: CentroidLike + From<CentroidPeak>, DP: DeconvolutedCentroidLike>(
        &self,
        frame: MultiLayerIonMobilityFrame<C, D>,
        error_tolerance: Tolerance,
        do_consolidate: bool,
    ) -> MultiLayerSpectrum<CP, DP> {
        let (feature_d, mut descr) = frame.into_features_and_parts();
        match feature_d {
            crate::spectrum::FeatureDataLevel::Missing => MultiLayerSpectrum {
                description: descr.into(),
                arrays: None,
                peaks: None,
                deconvoluted_peaks: None,
            },
            crate::spectrum::FeatureDataLevel::RawData(arrays) => {
                let peaks = if do_consolidate {
                    log::trace!("Consolidating peaks for {}", descr.index);
                    consolidate_peaks(
                        &arrays,
                        &(0..arrays.ion_mobility_dimension.len() as u32),
                        &self.metadata,
                        error_tolerance,
                    )
                    .ok()
                } else {
                    None
                };
                descr.signal_continuity = SignalContinuity::Centroid;
                let arrays = arrays.unstack().unwrap();
                let spec = MultiLayerSpectrum {
                    description: descr.into(),
                    arrays: Some(arrays),
                    peaks,
                    deconvoluted_peaks: None,
                };
                spec
            }
            _ => panic!("Failed to extract array data"),
        }
    }

    pub(crate) fn get(
        &self,
        index: usize,
    ) -> Result<Option<MultiLayerIonMobilityFrame<C, D>>, TimsRustError> {
        if let Some(entry) = self.entry_index.get(index) {
            // `timsrust` uses base-zero indexing, but frame IDs start at 1
            let frame = self
                .frame_reader
                .get(entry.frame.id.saturating_sub(1))
                .inspect_err(|e| {
                    log::error!("Failed to read frame {index}: {e}");
                })?;

            let mut descr = frame_to_description(&self.metadata, entry, None);

            if let Some(parent_entry) = entry.parent_index.and_then(|i| self.entry_index.get(i)) {
                descr.precursor = index_to_precursor(entry, &self.metadata, parent_entry);
            }

            let arrays = if !matches!(self.detail_level, DetailLevel::MetadataOnly) {
                if let Some(pasef) = entry.pasef_msms() {
                    log::trace!("Extracting {index} as PasefFrameMsMs with range {:?}", pasef.scan_start..pasef.scan_end);
                    let arrays = FrameToArraysMapper::new(&frame, &self.metadata)
                        .process_3d_slice(pasef.scan_start..pasef.scan_end);
                    Some(arrays)
                } else if let Some(dia_pasef) = entry.dia_window() {
                    log::trace!("Extracting {index} as DIAFrameMsMsWindow with range {:?}", dia_pasef.scan_start..dia_pasef.scan_end);
                    let arrays = FrameToArraysMapper::new(&frame, &self.metadata)
                        .process_3d_slice(dia_pasef.scan_start..dia_pasef.scan_end);
                    Some(arrays)
                } else {
                    Some(FrameToArraysMapper::new(&frame, &self.metadata).process_3d_slice(..))
                }
            } else {
                None
            };

            let frame = MultiLayerIonMobilityFrame::new(arrays, None, None, descr);
            Ok(Some(frame))
        } else {
            Ok(None)
        }
    }

    pub fn get_trace_reader(&self) -> Result<ChromatographyData, TimsRustError> {
        let path = self
            .metadata
            .path
            .parent()
            .expect(".tdf file did not have an enclosing directory")
            .join(super::sql::ChromatographyData::FILE_NAME);
        let handle = super::sql::ChromatographyData::new(&path).map_err(|e| {
            TimsRustError::MetadataReaderError(timsrust::readers::MetadataReaderError::SqlError(
                e.into(),
            ))
        })?;
        Ok(handle)
    }
}

// Metadata construction routine
impl<C: FeatureLike<MZ, IonMobility>, D: FeatureLike<Mass, IonMobility> + KnownCharge>
    TDFFrameReaderType<C, D>
{
    fn build_file_description(&self) -> io::Result<FileDescription> {
        let mut descr = FileDescription::default();

        let (ms1_count, msn_count) = self.entry_index.iter().fold((0, 0), |(ms1, msn), entry| {
            if entry.ms_level() > 1 {
                (ms1, msn + 1)
            } else {
                (ms1 + 1, msn)
            }
        });

        if ms1_count > 0 {
            descr.contents.add_param(
                Param::builder()
                    .name("MS1 spectrum")
                    .curie(curie!(MS:1000579))
                    .build(),
            );
        }

        if msn_count > 0 {
            descr.contents.add_param(
                Param::builder()
                    .name("MSn spectrum")
                    .curie(curie!(MS:1000580))
                    .build(),
            );
        }

        let mut sf = SourceFile::from_path(&self.metadata.path)?;
        sf.file_format = Some(MassSpectrometerFileFormatTerm::BrukerTDF.into());
        sf.id_format = Some(NativeSpectrumIdentifierFormatTerm::BrukerTDFNativeIDFormat.into());
        #[cfg(not(debug_assertions))]
        sf.add_param(ControlledVocabulary::MS.param_val(
            1000569u32,
            "SHA-1",
            checksum_file(&self.metadata.path)?,
        ));
        sf.id = self
            .metadata
            .path
            .file_name()
            .map(|s| s.to_string_lossy().to_string())
            .unwrap_or_else(|| "analysis_tdf".into());
        descr.source_files.push(sf);

        let tdf_bin = self.metadata.path.with_extension("tdf_bin");
        if tdf_bin.exists() {
            let mut sf = SourceFile::from_path(&tdf_bin)?;
            sf.file_format = Some(MassSpectrometerFileFormatTerm::BrukerTDF.into());
            sf.id_format = Some(NativeSpectrumIdentifierFormatTerm::BrukerTDFNativeIDFormat.into());
            #[cfg(not(debug_assertions))]
            sf.add_param(ControlledVocabulary::MS.param_val(
                1000569u32,
                "SHA-1",
                checksum_file(&tdf_bin)?,
            ));
            sf.id = tdf_bin
                .file_name()
                .map(|s| s.to_string_lossy().to_string())
                .unwrap_or_else(|| "analysis_tdf_bin".into());
            descr.source_files.push(sf);
        }

        Ok(descr)
    }

    fn build_software_from_metadata(&self, metadata: &HashMap<String, Value>) -> Vec<Software> {
        let mut sw_entries = Vec::new();

        let sdk = Software::new(
            "TIMS_SDK".into(),
            "*".into(),
            vec![
                SoftwareTerm::BrukerSoftware.into(),
                Param::new_key_value("software name", "timsrust"),
            ],
        );
        sw_entries.push(sdk);

        let acq_sw_name = metadata
            .get("AcquisitionSoftware")
            .map(|s| {
                let s = s.as_str();

                if s.contains("oTOFcontrol") {
                    SoftwareTerm::MicrOTOFcontrol
                } else if s.contains("HCT") {
                    SoftwareTerm::HCTcontrol
                } else {
                    SoftwareTerm::MicrOTOFcontrol
                }
                .to_param()
            })
            .unwrap();

        let acq_sw_vers = metadata
            .get("AcquisitionSoftwareVersion")
            .map(|v| v.to_string())
            .unwrap_or_else(|| "*".to_string());

        sw_entries.push(Software::new(
            "ACQ_SW".to_string(),
            acq_sw_vers,
            vec![acq_sw_name.into()],
        ));

        sw_entries
    }

    fn build_instrument_configuration(
        &self,
        metadata: &HashMap<String, Value>,
    ) -> InstrumentConfiguration {
        let components = {
            let source = metadata
                .get("InstrumentSourceType")
                .into_iter()
                .flat_map(|s| s.to_i32())
                .map(|s| InstrumentSource::from(s as u8).to_component())
                .next()
                .unwrap_or_default();

            let mut components = Vec::new();
            components.push(source);

            let mut analyzer = Component {
                order: 2,
                component_type: ComponentType::Analyzer,
                ..Default::default()
            };
            analyzer.add_param(MassAnalyzerTerm::Quadrupole.into());
            components.push(analyzer);

            let mut analyzer = Component {
                component_type: ComponentType::Analyzer,
                order: 3,
                ..Default::default()
            };
            analyzer.add_param(MassAnalyzerTerm::TimeOfFlight.into());
            components.push(analyzer);

            let mut detector = Component {
                component_type: ComponentType::Detector,
                order: 4,
                ..Default::default()
            };
            detector.add_param(DetectorTypeTerm::MicrochannelPlateDetector.into());
            components.push(detector);

            detector = Component {
                component_type: ComponentType::Detector,
                order: 5,
                ..Default::default()
            };
            detector.add_param(DetectorTypeTerm::Photomultiplier.into());
            components.push(detector);

            components
        };
        let mut config = InstrumentConfiguration {
            components,
            id: 0,
            ..Default::default()
        };
        if let Some(serial) = metadata.get("InstrumentSerialNumber").cloned() {
            config.add_param(
                Param::builder()
                    .curie(curie!(MS:1000529))
                    .name("instrument serial number")
                    .value(serial)
                    .build(),
            );
        }
        if let Some(model) = metadata.get("InstrumentName").cloned() {
            let (name, curie) = match model.as_str().as_ref().trim() {
                "timsTOF Pro" => ("timsTOF Pro", curie!(MS:1003005)),
                "timsTOF fleX" => ("timsTOF fleX", curie!(MS:1003124)),
                "timsTOF Pro 2" => ("timsTOF Pro 2", curie!(MS:1003230)),
                "timsTOF SCP" => ("timsTOF SCP", curie!(MS:1003231)),
                "timsTOF Ultra" => ("timsTOF Ultra", curie!(MS:1003383)),
                "timsTOF fleX MALDI-2" => ("timsTOF fleX MALDI-2", curie!(MS:1003397)),
                "timsTOF HT" => ("timsTOF HT", curie!(MS:1003404)),
                "timsTOF Ultra 2" => ("timsTOF Ultra 2", curie!(MS:1003412)),
                "timsTOF" => ("timsTOF", curie!(MS:1003229)),
                _ => ("Bruker Daltonics timsTOF series", curie!(MS:1003123)),
            };
            config.add_param(ControlledVocabulary::MS.param(curie.accession, name));
        }
        config.software_reference = "ACQ_SW".into();
        config
    }

    fn build_ms_run(
        &self,
        metadata: &HashMap<String, Value>,
        file_description: &FileDescription,
    ) -> MassSpectrometryRun {
        let sf = file_description.source_files.first().unwrap();
        let run_id = self
            .metadata
            .path
            .parent()
            .map(|s| s.as_os_str().to_string_lossy().to_string());

        let start_time = metadata.get("AcquisitionDateTime").and_then(|s| {
            let s = s.as_str();
            DateTime::parse_from_rfc3339(&s).ok()
        });

        MassSpectrometryRun::new(run_id, None, Some(1), Some(sf.id.clone()), start_time)
    }

    fn build_sample(&self, metadata: &HashMap<String, Value>) -> Option<Sample> {
        metadata.get("SampleName").map(|name| {
            let name = name.to_string();
            let mut params = Vec::new();
            if let Some(analysis_id) = metadata.get("AnalysisId").cloned() {
                params.push(Param::new_key_value("TDF:AnalysisId", analysis_id))
            }
            Sample::new("SAMPLE_1".into(), Some(name), params)
        })
    }

    fn build_metadata(&mut self) -> io::Result<()> {
        let metadata = self.tdf_reader.metadata().unwrap();
        let file_description = self.build_file_description()?;

        self.softwares = self.build_software_from_metadata(&metadata);
        self.instrument_configurations
            .insert(0, self.build_instrument_configuration(&metadata));
        self.samples.extend(self.build_sample(&metadata));
        self.run = self.build_ms_run(&metadata, &file_description);
        self.file_description = file_description;
        Ok(())
    }
}

impl<C: FeatureLike<MZ, IonMobility>, D: FeatureLike<Mass, IonMobility> + KnownCharge>
    MSDataFileMetadata for TDFFrameReaderType<C, D>
{
    crate::impl_metadata_trait!();

    fn run_description(&self) -> Option<&MassSpectrometryRun> {
        Some(&self.run)
    }

    fn run_description_mut(&mut self) -> Option<&mut MassSpectrometryRun> {
        Some(&mut self.run)
    }

    fn spectrum_count_hint(&self) -> Option<u64> {
        Some(self.entry_index.len() as u64)
    }

    fn source_file_name(&self) -> Option<&str> {
        self.metadata.path.to_str()
    }
}

impl<C: FeatureLike<MZ, IonMobility>, D: FeatureLike<Mass, IonMobility> + KnownCharge>
    ChromatogramSource for TDFFrameReaderType<C, D>
{
    fn get_chromatogram_by_id(&mut self, id: &str) -> Option<Chromatogram> {
        match id {
            "TIC" => Some(self.tic()),
            "BPC" => Some(self.bpc()),
            _ => None,
        }
    }

    fn get_chromatogram_by_index(&mut self, index: usize) -> Option<Chromatogram> {
        match index {
            0 => Some(self.tic()),
            1 => Some(self.bpc()),
            _ => None,
        }
    }

    fn count_chromatograms(&self) -> usize {
        2
    }
}

impl<C: FeatureLike<MZ, IonMobility>, D: FeatureLike<Mass, IonMobility> + KnownCharge> Iterator
    for TDFFrameReaderType<C, D>
{
    type Item = MultiLayerIonMobilityFrame<C, D>;

    fn next(&mut self) -> Option<Self::Item> {
        let frame = self.get(self.index);
        self.index += 1;
        frame.ok().unwrap_or_default()
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.index += n;
        self.next()
    }
}

impl<C: FeatureLike<MZ, IonMobility>, D: FeatureLike<Mass, IonMobility> + KnownCharge>
    IonMobilityFrameSource<C, D, MultiLayerIonMobilityFrame<C, D>> for TDFFrameReaderType<C, D>
{
    fn reset(&mut self) {
        self.index = 0;
    }

    fn get_frame_by_id(&mut self, id: &str) -> Option<MultiLayerIonMobilityFrame<C, D>> {
        let offset = self.offset_index.get(id)?;
        self.get(offset as usize).ok().unwrap_or_default()
    }

    fn get_frame_by_index(&mut self, index: usize) -> Option<MultiLayerIonMobilityFrame<C, D>> {
        self.get(index).ok().unwrap_or_default()
    }

    fn get_frame_by_time(&mut self, time: f64) -> Option<MultiLayerIonMobilityFrame<C, D>> {
        self.index_by_time(time)
            .and_then(|e| self.get(e.index).ok().unwrap_or_default())
    }

    fn get_index(&self) -> &OffsetIndex {
        &self.offset_index
    }

    fn set_index(&mut self, index: OffsetIndex) {
        self.offset_index = index
    }

    fn detail_level(&self) -> &DetailLevel {
        &self.detail_level
    }

    fn set_detail_level(&mut self, detail_level: DetailLevel) {
        self.detail_level = detail_level;
    }
}

impl<C: FeatureLike<MZ, IonMobility>, D: FeatureLike<Mass, IonMobility> + KnownCharge> FusedIterator
    for TDFFrameReaderType<C, D>
{
}

impl<C: FeatureLike<MZ, IonMobility>, D: FeatureLike<Mass, IonMobility> + KnownCharge>
    RandomAccessIonMobilityFrameIterator<C, D, MultiLayerIonMobilityFrame<C, D>>
    for TDFFrameReaderType<C, D>
{
    fn start_from_id(&mut self, id: &str) -> Result<&mut Self, IonMobilityFrameAccessError> {
        match self.offset_index.index_of(id) {
            Some(i) => {
                self.index = i;
                Ok(self)
            }
            None => Err(IonMobilityFrameAccessError::FrameIdNotFound(id.into())),
        }
    }

    fn start_from_index(&mut self, index: usize) -> Result<&mut Self, IonMobilityFrameAccessError> {
        if index < self.offset_index.len() {
            self.index = index;
            Ok(self)
        } else {
            Err(IonMobilityFrameAccessError::FrameIndexNotFound(index))
        }
    }

    fn start_from_time(&mut self, time: f64) -> Result<&mut Self, IonMobilityFrameAccessError> {
        match self.index_by_time(time) {
            Some(entry) => {
                self.index = entry.index;
                Ok(self)
            }
            None => Err(IonMobilityFrameAccessError::FrameNotFound),
        }
    }
}

pub type TDFFrameReader =
    TDFFrameReaderType<Feature<MZ, IonMobility>, ChargedFeature<Mass, IonMobility>>;

/// A flat spectrum reader for Bruker TDF file format wrapping a [`TDFFrameReaderType`].
///
/// It can sum over ion mobility spectra, consolidating features into peaks. Enable this
/// behavior by [`Self::set_consolidate_peaks`] `true`. The granularity of the merging
/// in the m/z dimension [`Self::peak_merging_tolerance`].
#[derive(Debug)]
pub struct TDFSpectrumReaderType<
    C: FeatureLike<MZ, IonMobility> = Feature<MZ, IonMobility>,
    D: FeatureLike<Mass, IonMobility> + KnownCharge = ChargedFeature<Mass, IonMobility>,
    CP: CentroidLike = CentroidPeak,
    DP: DeconvolutedCentroidLike = DeconvolutedPeak,
> {
    frame_reader: TDFFrameReaderType<C, D>,
    peak_merging_tolerance: Tolerance,
    do_consolidate_peaks: bool,
    _cp: PhantomData<CP>,
    _dp: PhantomData<DP>,
}

impl<
        C: FeatureLike<MZ, IonMobility>,
        D: FeatureLike<Mass, IonMobility> + KnownCharge,
        CP: CentroidLike + From<CentroidPeak>,
        DP: DeconvolutedCentroidLike + From<DeconvolutedPeak>,
    > IntoIonMobilityFrameSource<CP, DP> for TDFSpectrumReaderType<C, D, CP, DP>
{
    type IonMobilityFrameSource<
        CF: FeatureLike<MZ, IonMobility>,
        DF: FeatureLike<Mass, IonMobility> + KnownCharge,
    > = TDFFrameReaderType<CF, DF>;

    fn try_into_frame_source<
        CF: FeatureLike<MZ, IonMobility>,
        DF: FeatureLike<Mass, IonMobility> + KnownCharge,
    >(
        self,
    ) -> Result<Self::IonMobilityFrameSource<CF, DF>, crate::io::IntoIonMobilityFrameSourceError>
    {
        let view = self.into_frame_reader();

        Ok(TDFFrameReaderType {
            tdf_reader: view.tdf_reader,
            metadata: view.metadata,
            frame_reader: view.frame_reader,
            entry_index: view.entry_index,
            index: view.index,
            offset_index: view.offset_index,
            file_description: view.file_description,
            instrument_configurations: view.instrument_configurations,
            softwares: view.softwares,
            samples: view.samples,
            data_processings: view.data_processings,
            detail_level: view.detail_level,
            run: view.run,
            _c: PhantomData,
            _d: PhantomData,
        })
    }
}

impl<
        C: FeatureLike<MZ, IonMobility>,
        D: FeatureLike<Mass, IonMobility> + KnownCharge,
        CP: CentroidLike + From<CentroidPeak>,
        DP: DeconvolutedCentroidLike,
    > RandomAccessSpectrumIterator<CP, DP, MultiLayerSpectrum<CP, DP>>
    for TDFSpectrumReaderType<C, D, CP, DP>
{
    fn start_from_id(&mut self, id: &str) -> Result<&mut Self, SpectrumAccessError> {
        self.frame_reader
            .start_from_id(id)
            .map_err(SpectrumAccessError::from)?;
        Ok(self)
    }

    fn start_from_index(&mut self, index: usize) -> Result<&mut Self, SpectrumAccessError> {
        self.frame_reader
            .start_from_index(index)
            .map_err(SpectrumAccessError::from)?;
        Ok(self)
    }

    fn start_from_time(&mut self, time: f64) -> Result<&mut Self, SpectrumAccessError> {
        self.frame_reader
            .start_from_time(time)
            .map_err(SpectrumAccessError::from)?;
        Ok(self)
    }
}

impl<
        C: FeatureLike<MZ, IonMobility>,
        D: FeatureLike<Mass, IonMobility> + KnownCharge,
        CP: CentroidLike + From<CentroidPeak>,
        DP: DeconvolutedCentroidLike,
    > SpectrumSource<CP, DP> for TDFSpectrumReaderType<C, D, CP, DP>
{
    fn reset(&mut self) {
        self.frame_reader.reset();
    }

    fn get_spectrum_by_id(&mut self, id: &str) -> Option<MultiLayerSpectrum<CP, DP>> {
        self.frame_reader.get_frame_by_id(id).map(|f| {
            self.frame_reader.frame_to_spectrum(
                f,
                self.peak_merging_tolerance,
                self.do_consolidate_peaks,
            )
        })
    }

    fn get_spectrum_by_index(&mut self, index: usize) -> Option<MultiLayerSpectrum<CP, DP>> {
        self.frame_reader.get_frame_by_index(index).map(|f| {
            self.frame_reader.frame_to_spectrum(
                f,
                self.peak_merging_tolerance,
                self.do_consolidate_peaks,
            )
        })
    }

    fn get_spectrum_by_time(&mut self, time: f64) -> Option<MultiLayerSpectrum<CP, DP>> {
        self.frame_reader.get_frame_by_time(time).map(|f| {
            self.frame_reader.frame_to_spectrum(
                f,
                self.peak_merging_tolerance,
                self.do_consolidate_peaks,
            )
        })
    }

    fn get_index(&self) -> &OffsetIndex {
        self.frame_reader.get_index()
    }

    fn set_index(&mut self, index: OffsetIndex) {
        self.frame_reader.set_index(index);
    }

    fn detail_level(&self) -> &DetailLevel {
        &self.frame_reader.detail_level
    }

    fn set_detail_level(&mut self, detail_level: DetailLevel) {
        self.frame_reader.set_detail_level(detail_level);
    }
}

impl<
        C: FeatureLike<MZ, IonMobility>,
        D: FeatureLike<Mass, IonMobility> + KnownCharge,
        CP: CentroidLike + From<CentroidPeak>,
        DP: DeconvolutedCentroidLike,
    > Iterator for TDFSpectrumReaderType<C, D, CP, DP>
{
    type Item = MultiLayerSpectrum<CP, DP>;

    fn next(&mut self) -> Option<Self::Item> {
        self.frame_reader.next().map(|f| {
            self.frame_reader.frame_to_spectrum(
                f,
                self.peak_merging_tolerance,
                self.do_consolidate_peaks,
            )
        })
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.frame_reader.nth(n).map(|f| {
            self.frame_reader.frame_to_spectrum(
                f,
                self.peak_merging_tolerance,
                self.do_consolidate_peaks,
            )
        })
    }
}

impl<
        C: FeatureLike<MZ, IonMobility>,
        D: FeatureLike<Mass, IonMobility> + KnownCharge,
        CP: CentroidLike + From<CentroidPeak>,
        DP: DeconvolutedCentroidLike,
    > FusedIterator for TDFSpectrumReaderType<C, D, CP, DP>
{
}

impl<
        C: FeatureLike<MZ, IonMobility>,
        D: FeatureLike<Mass, IonMobility> + KnownCharge,
        CP: CentroidLike,
        DP: DeconvolutedCentroidLike,
    > MSDataFileMetadata for TDFSpectrumReaderType<C, D, CP, DP>
{
    crate::delegate_impl_metadata_trait!(frame_reader);
}

impl<
        C: FeatureLike<MZ, IonMobility>,
        D: FeatureLike<Mass, IonMobility> + KnownCharge,
        CP: CentroidLike,
        DP: DeconvolutedCentroidLike,
    > ChromatogramSource for TDFSpectrumReaderType<C, D, CP, DP>
{
    fn get_chromatogram_by_id(&mut self, id: &str) -> Option<Chromatogram> {
        self.frame_reader.get_chromatogram_by_id(id)
    }

    fn get_chromatogram_by_index(&mut self, index: usize) -> Option<Chromatogram> {
        self.frame_reader.get_chromatogram_by_index(index)
    }

    fn count_chromatograms(&self) -> usize {
        self.frame_reader.count_chromatograms()
    }
}

impl<
        C: FeatureLike<MZ, IonMobility>,
        D: FeatureLike<Mass, IonMobility> + KnownCharge,
        CP: CentroidLike,
        DP: DeconvolutedCentroidLike,
    > TDFSpectrumReaderType<C, D, CP, DP>
{
    pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, TimsRustError> {
        Self::new_with_peak_merging_tolerance(path, PEAK_MERGE_TOLERANCE, false)
    }

    pub fn new_with_peak_merging_tolerance<P: AsRef<Path>>(
        path: P,
        peak_merging_tolerance: Tolerance,
        do_consolidate_peaks: bool,
    ) -> Result<Self, TimsRustError> {
        TDFFrameReaderType::<C, D>::new(path).map(|s| Self {
            frame_reader: s,
            peak_merging_tolerance,
            do_consolidate_peaks,
            _cp: PhantomData,
            _dp: PhantomData,
        })
    }

    pub fn new_with_detail_level<P: AsRef<Path>>(
        path: P,
        peak_merging_tolerance: Option<Tolerance>,
        do_consolidate_peaks: bool,
        detail_level: DetailLevel,
    ) -> Result<Self, TimsRustError> {
        TDFFrameReaderType::<C, D>::new(path).map(|mut s| {
            s.detail_level = detail_level;
            Self {
                frame_reader: s,
                peak_merging_tolerance: peak_merging_tolerance.unwrap_or(PEAK_MERGE_TOLERANCE),
                do_consolidate_peaks,
                _cp: PhantomData,
                _dp: PhantomData,
            }
        })
    }

    /// The number of spectra available
    pub fn len(&self) -> usize {
        self.frame_reader.len()
    }

    pub fn is_empty(&self) -> bool {
        self.frame_reader.is_empty()
    }

    /// Merge peaks in the ion mobility dimension. This will be done automatically
    /// if [`Self::will_consolidate_peaks`] returns true.
    ///
    /// This modifies the spectrum in-place.
    ///
    /// # See Also
    /// - [`Self::will_consolidate_peaks`]
    /// - [`Self::set_consolidate_peaks`]
    pub fn consolidate_peaks(
        &self,
        spectrum: &mut MultiLayerSpectrum<CP, DP>,
    ) -> Result<(), ArrayRetrievalError> where CP: From<CentroidPeak> {
        if let Some(arrays) = spectrum.arrays.as_ref() {
            let arrays = BinaryArrayMap3D::stack(arrays)?;
            spectrum.peaks = Some(consolidate_peaks(
                &arrays,
                &(0..arrays.ion_mobility_dimension.len() as u32),
                &self.frame_reader.metadata,
                self.peak_merging_tolerance,
            )?);
        };

        Ok(())
    }

    /// Retrieve a spectrum by index
    pub fn get(&self, index: usize) -> Result<Option<MultiLayerSpectrum>, TimsRustError> {
        self.frame_reader.get(index).map(|f| {
            f.map(|f| {
                self.frame_reader.frame_to_spectrum(
                    f,
                    self.peak_merging_tolerance,
                    self.do_consolidate_peaks,
                )
            })
        })
    }

    /// Consume the spectrum reader, retrieving the underlying [`TDFFrameReaderType`]
    pub fn into_inner(self) -> TDFFrameReaderType<C, D> {
        self.frame_reader
    }

    /// Retrieving a mutable reference the underlying [`TDFFrameReaderType`]
    pub fn frame_reader(&mut self) -> &mut TDFFrameReaderType<C, D> {
        &mut self.frame_reader
    }

    /// Consume the spectrum reader, retrieving the underlying [`TDFFrameReaderType`]
    ///
    /// # See also
    /// An alias of [`TDFSpectrumReaderType::into_inner`]
    pub fn into_frame_reader(self) -> TDFFrameReaderType<C, D> {
        self.into_inner()
    }

    /// Get the error tolerance margin for consolidating peaks over the
    /// ion mobility dimension.
    pub fn peak_merging_tolerance(&self) -> Tolerance {
        self.peak_merging_tolerance
    }

    /// Get a mutable reference to the error tolerance margin for consolidating
    /// peaks over the ion mobility dimension.
    pub fn peak_merging_tolerance_mut(&mut self) -> &mut Tolerance {
        &mut self.peak_merging_tolerance
    }

    pub fn get_trace_reader(&self) -> Result<ChromatographyData, TimsRustError> {
        self.frame_reader.get_trace_reader()
    }

    pub fn will_consolidate_peaks(&self) -> bool {
        self.do_consolidate_peaks
    }

    pub fn set_consolidate_peaks(&mut self, do_consolidate_peaks: bool) {
        self.do_consolidate_peaks = do_consolidate_peaks;
    }
}

pub type TDFSpectrumReader = TDFSpectrumReaderType<
    Feature<MZ, IonMobility>,
    ChargedFeature<Mass, IonMobility>,
    CentroidPeak,
    DeconvolutedPeak,
>;

fn index_to_precursor(
    index_entry: &IndexExtry,
    metadata: &Metadata,
    parent_entry: &IndexExtry,
) -> Vec<Precursor> {
    if let Some(prec) = index_entry.precursor() {
        let mut ion = SelectedIon {
            mz: prec.mz,
            intensity: prec.intensity as f32,
            charge: if prec.charge != 0 {
                Some(prec.charge)
            } else {
                None
            },
            ..Default::default()
        };

        let im = metadata.im_converter.convert(prec.scan_average);

        let p = inverse_reduce_ion_mobility_param(im);
        ion.add_param(p);
        let mut act = Activation::default();
        let mut isolation = IsolationWindow::default();
        if let Some(pasef) = index_entry.pasef_msms() {
            act.energy = pasef.collision_energy as f32;
            act.methods_mut().push(CollisionInducedDissociation);

            let iso_width = pasef.isolation_width / 2.0;
            isolation.target = pasef.isolation_mz as f32;
            isolation.lower_bound = (pasef.isolation_mz - iso_width) as f32;
            isolation.upper_bound = (pasef.isolation_mz + iso_width) as f32;
            isolation.flags = IsolationWindowState::Complete;
        }
        if let Some(pasef) = index_entry.dia_window() {
            act.energy = pasef.collision_energy;
            act.methods_mut().push(CollisionInducedDissociation);

            let iso_width = pasef.isolation_width / 2.0;
            isolation.target = pasef.isolation_mz as f32;
            isolation.lower_bound = (pasef.isolation_mz - iso_width) as f32;
            isolation.upper_bound = (pasef.isolation_mz + iso_width) as f32;
            isolation.flags = IsolationWindowState::Complete;
        }

        let mut mz_prec = Precursor::default();
        mz_prec.add_ion(ion);
        mz_prec.activation = act;
        mz_prec.isolation_window = isolation;
        mz_prec.precursor_id = Some(parent_entry.format_native_id());
        vec![mz_prec]
    } else if let Some(dia_window) = index_entry.dia_window() {
        let mut ion = SelectedIon {
            mz: dia_window.isolation_mz,
            intensity: 0.0,
            charge: None,
            ..Default::default()
        };
        let im = metadata
            .im_converter
            .convert((dia_window.scan_end + dia_window.scan_start) as f64 / 2.0);

        let p = inverse_reduce_ion_mobility_param(im);
        ion.add_param(p);
        let mut act = Activation::default();
        let mut isolation = IsolationWindow::default();
        if let Some(pasef) = index_entry.pasef_msms() {
            act.energy = pasef.collision_energy as f32;
            act.methods_mut().push(CollisionInducedDissociation);

            let iso_width = pasef.isolation_width / 2.0;
            isolation.target = pasef.isolation_mz as f32;
            isolation.lower_bound = (pasef.isolation_mz - iso_width) as f32;
            isolation.upper_bound = (pasef.isolation_mz + iso_width) as f32;
            isolation.flags = IsolationWindowState::Complete;
        }
        if let Some(pasef) = index_entry.dia_window() {
            act.energy = pasef.collision_energy;
            act.methods_mut().push(CollisionInducedDissociation);

            let iso_width = pasef.isolation_width / 2.0;
            isolation.target = pasef.isolation_mz as f32;
            isolation.lower_bound = (pasef.isolation_mz - iso_width) as f32;
            isolation.upper_bound = (pasef.isolation_mz + iso_width) as f32;
            isolation.flags = IsolationWindowState::Complete;
        }
        let mut mz_prec = Precursor::default();
        mz_prec.add_ion(ion);
        mz_prec.activation = act;
        mz_prec.isolation_window = isolation;
        mz_prec.precursor_id = Some(parent_entry.format_native_id());
        vec![mz_prec]
    } else {
        Vec::new()
    }
}

fn frame_to_description(
    metadata: &Metadata,
    index_entry: &IndexExtry,
    frame_slice: Option<Range<u32>>,
) -> IonMobilityFrameDescription {
    let mut descr = IonMobilityFrameDescription {
        id: index_entry.format_native_id(),
        index: index_entry.index,
        ms_level: index_entry.ms_level(),
        signal_continuity: SignalContinuity::Centroid,
        polarity: index_entry.frame.polarity,
        ..Default::default()
    };

    descr.add_param(
        Param::builder()
            .name("total ion current")
            .curie(curie!(MS:1000285))
            .value(index_entry.frame.summed_intensities)
            .build(),
    );
    descr.add_param(
        Param::builder()
            .name("base peak intensity")
            .curie(curie!(MS:1000505))
            .value(index_entry.frame.max_intensity)
            .build(),
    );

    let mut scan = ScanEvent::new(
        index_entry.frame.time / 60.0,
        index_entry.frame.accumulation_time,
        vec![ScanWindow {
            lower_bound: metadata.lower_mz as f32,
            upper_bound: metadata.upper_mz as f32,
        }],
        0,
        None,
    );

    if let Some(scan_range) = frame_slice.as_ref() {
        let im = (metadata.im_converter.convert(scan_range.start)
            + metadata.im_converter.convert(scan_range.end))
            / 2.0;
        let p = inverse_reduce_ion_mobility_param(im);
        scan.add_param(p);
    }

    if let Some(pasef) = index_entry.pasef_msms() {
        let im_low = metadata.im_converter.convert(pasef.scan_start as u32);
        let im_high = metadata.im_converter.convert(pasef.scan_end as u32);

        descr.add_param(
            Param::new_key_value("ion mobility lower limit", im_low)
                .with_unit_t(&Unit::VoltSecondPerSquareCentimeter),
        );
        descr.add_param(
            Param::new_key_value("ion mobility upper limit", im_high)
                .with_unit_t(&Unit::VoltSecondPerSquareCentimeter),
        );

        if frame_slice.is_none() {
            scan.add_param(inverse_reduce_ion_mobility_param((im_low + im_high) / 2.0));
        }
    }

    if let Some(pasef) = index_entry.dia_window() {
        let im_low = metadata.im_converter.convert(pasef.scan_start as u32);
        let im_high = metadata.im_converter.convert(pasef.scan_end as u32);

        descr.add_param(
            Param::new_key_value("ion mobility lower limit", im_low)
                .with_unit_t(&Unit::VoltSecondPerSquareCentimeter),
        );
        descr.add_param(
            Param::new_key_value("ion mobility upper limit", im_high)
                .with_unit_t(&Unit::VoltSecondPerSquareCentimeter),
        );

        scan.add_param(
            Param::builder()
                .name("window group")
                .value(Value::Int(pasef.window_group as i64))
                .build(),
        );

        if frame_slice.is_none() {
            scan.add_param(inverse_reduce_ion_mobility_param((im_low + im_high) / 2.0));
        }
    }

    *descr.acquisition.first_scan_mut().unwrap() = scan;
    descr.acquisition.combination = ScanCombination::Sum;
    descr
}

impl<
        C: FeatureLike<MZ, IonMobility>,
        D: FeatureLike<Mass, IonMobility> + KnownCharge,
        CP: CentroidLike + From<CentroidPeak>,
        DP: DeconvolutedCentroidLike,
    > MZFileReader<CP, DP, MultiLayerSpectrum<CP, DP>> for TDFSpectrumReaderType<C, D, CP, DP>
{
    fn construct_index_from_stream(&mut self) -> u64 {
        self.frame_reader.entry_index.len() as u64
    }

    /// The underlying Bruker library requires an explicit file system path to the `.d` directory
    /// and as such this method always fails.
    ///
    /// [`open_path`](Self::open_path) works as normal.
    #[allow(unused)]
    fn open_file(source: std::fs::File) -> io::Result<Self> {
        Err(io::Error::new(
            io::ErrorKind::Unsupported,
            "Cannot read a TDF dataset from an open file handle, only from a directory path",
        ))
    }

    fn open_path<P>(path: P) -> io::Result<Self>
    where
        P: Into<std::path::PathBuf> + Clone,
    {
        let frame_reader = TDFFrameReaderType::new(path.into())
            .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;

        let spectrum_reader = frame_reader.into_spectrum_reader();
        Ok(spectrum_reader)
    }
}

/// Test if a file system path is a TDF directory
///
/// This will first probe the directory for the required
/// files, and only after verifying the files exist, attempt
/// to test the SQL schema.
pub fn is_tdf<P: AsRef<Path>>(path: P) -> bool {
    let path = path.as_ref();

    if !path.extension().map(|e| e == "d").unwrap_or_default() {
        return false;
    }

    if !path.exists() {
        return false;
    }

    let tdf_path = path.join("analysis.tdf");

    if !tdf_path.exists() {
        return false;
    }

    if !path.join("analysis.tdf_bin").exists() {
        return false;
    }

    if MetadataReader::new(tdf_path).is_err() {
        return false;
    }
    true
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_tdf_spectrum() -> io::Result<()> {
        let mut reader = TDFSpectrumReader::open_path("test/data/diaPASEF.d")?;
        reader.set_consolidate_peaks(true);
        eprintln!("{}", reader.len());
        let s = reader.get_spectrum_by_index(0).unwrap();
        assert!(s.peaks.is_some());
        assert_eq!(s.signal_continuity(), SignalContinuity::Centroid);
        assert_eq!(s.ms_level(), 1);
        Ok(())
    }

    #[test]
    fn test_tdf_frame() -> io::Result<()> {
        let mut reader = TDFFrameReader::new("test/data/diaPASEF.d")
            .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
        eprintln!("{}", reader.len());
        let s = reader.get_frame_by_index(0).unwrap();
        assert!(s.features.is_none());
        assert_eq!(s.signal_continuity(), SignalContinuity::Centroid);
        assert_eq!(s.ms_level(), 1);

        let s = reader.get_frame_by_index(1).unwrap();
        assert!(s.features.is_none());
        assert_eq!(s.signal_continuity(), SignalContinuity::Centroid);
        assert_eq!(s.ms_level(), 2);
        Ok(())
    }
}