mzannotate 0.1.0

Handle fragmentation of (complex) peptidoforms.
Documentation
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
use std::{
    collections::{HashMap, VecDeque},
    fmt::Display,
    fs::File,
    io::{self, BufReader, prelude::*},
    path::{Path, PathBuf},
};

use context_error::{BoxedError, Context, CreateError, ErrorKind, FullErrorContent};
use indexmap::IndexMap;
use itertools::Itertools;
use mzcore::{
    ontology::Ontologies,
    prelude::*,
    system::{MassOverCharge, isize::Charge},
};
use mzdata::{
    curie,
    mzpeaks::prelude::PeakCollectionMut,
    params::{ParamValue, Unit, Value},
    spectrum::{Precursor, ScanEvent, ScanWindow, SelectedIon},
};

use crate::{
    fragment::Fragment,
    helper_functions::explain_number_error,
    mzspeclib::{
        Analyte, AnalyteTarget, Attribute, AttributeParseError, AttributeSet, AttributeValue,
        EntryType, Id, Interpretation, LibraryHeader, ProteinDescription, merge_attributes,
        populate_spectrum_description_from_attributes,
    },
    spectrum::{AnnotatedPeak, AnnotatedSpectrum},
    term,
};

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
enum ParserState {
    Initial,
    Header,
    AttributeSet,
    Spectrum,
    Analyte,
    Interpretation,
    Peaks,
    Between,
    Eof,
}

impl Display for ParserState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

/// The kind of error that can occur when reading an mzSpecLib file
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum MzSpecLibErrorKind {
    /// An error concerning the underlying stream
    #[default]
    IO,
    /// An error concerning the parsing of a declaration
    Declaration,
    /// An error concerning the parsing of an attribute
    Attribute,
    /// An error concerning the parsing of a peak
    Peak,
    /// The end of the file was found but more data was expected
    Eof,
    /// The state is inconsistent (for example the charge was defined multiple times with different values)
    InconsistentState,
    /// A unit was missing on an attribute that needs one
    MissingUnit,
    /// A ProForma string was invalid
    ProForma,
    /// An mzPAF string was invalid
    MzPAF,
    /// A currently unsupported feature of mzSpecLib was used. Interpretation members are currently unsupported.
    UnsupportedFeature,
}

impl ErrorKind for MzSpecLibErrorKind {
    type Settings = ();
    fn descriptor(&self) -> &'static str {
        "error"
    }
    fn ignored(&self, _settings: Self::Settings) -> bool {
        false
    }
    fn is_error(&self, _settings: Self::Settings) -> bool {
        true
    }
}

/// The state for a parser for mzSpecLib.txt files
#[derive(Debug)]
pub struct MzSpecLibTextParser<'ontologies, Reader: Read> {
    inner: Reader,
    header: LibraryHeader,
    header_attribute_sets_with_context: HashMap<
        EntryType,
        HashMap<String, HashMap<Option<u32>, Vec<(Attribute, Context<'static>)>>>,
    >,
    state: ParserState,
    line_cache: VecDeque<String>,
    line_index: u32,
    path: Option<PathBuf>,
    offsets: LibraryIndex,
    /// A flag to indicate is the last spectrum failed, if so it will scan to the start of the next spectrum next time a spectrum is requested.
    last_error: bool,
    /// The last compound peptidoform ion, used when parsing the mzPAF peaks in a spectrum.
    last_compound_peptidoform: Vec<(u32, AnalyteTarget)>,
    ontologies: &'ontologies Ontologies,
}

impl<'ontologies> MzSpecLibTextParser<'ontologies, BufReader<File>> {
    /// Parse a mzSpecLib file from the given file.
    /// # Errors
    /// If the file does not contain valid mzSpecLib data.
    pub fn open_file(
        path: &Path,
        ontologies: &'ontologies Ontologies,
    ) -> Result<Self, BoxedError<'static, MzSpecLibErrorKind>> {
        Self::open(
            BufReader::new(File::open(path).map_err(|e| {
                BoxedError::new(
                    MzSpecLibErrorKind::IO,
                    "Could not open mzSpecLib file",
                    e.to_string(),
                    Context::none().source(path.to_string_lossy()).to_owned(),
                )
            })?),
            Some(path.to_path_buf()),
            ontologies,
        )
    }
}

impl<'ontologies, R: BufRead> MzSpecLibTextParser<'ontologies, R> {
    /// Parse a mzSpecLib file from the given stream, the original filepath can be given for nicer error messages.
    /// # Errors
    /// If the file does not contain valid mzSpecLib data.
    pub fn open(
        reader: R,
        path: Option<PathBuf>,
        ontologies: &'ontologies Ontologies,
    ) -> Result<Self, BoxedError<'static, MzSpecLibErrorKind>> {
        let mut this = Self {
            inner: reader,
            header: LibraryHeader::default(),
            header_attribute_sets_with_context: HashMap::default(),
            state: ParserState::Initial,
            line_cache: VecDeque::new(),
            line_index: 0,
            path,
            offsets: LibraryIndex {
                done: false,
                primary: IndexMap::new(),
                key: HashMap::new(),
                name: HashMap::new(),
                scan_number: HashMap::new(),
            },
            last_error: false,
            last_compound_peptidoform: Vec::new(),
            ontologies,
        };
        this.read_header()?;
        Ok(this)
    }

    /// Get the header of the file
    pub const fn header(&self) -> &LibraryHeader {
        &self.header
    }

    /// Get the current context, meaning the line index and file name (if known), the line itself will have to be added by the caller.
    fn current_context(&self) -> Context<'_> {
        self.path.as_ref().map_or_else(
            || Context::none().line_index(self.line_index),
            |p| {
                Context::none()
                    .line_index(self.line_index)
                    .source(p.to_string_lossy())
            },
        )
    }

    fn push_back_line(&mut self, line: String) {
        self.line_cache.push_front(line);
        self.line_index -= 1;
    }

    /// Read the next line in the reader and update the internal state.
    /// # Errors
    /// If the reader errors.
    fn read_next_line(&mut self, buf: &mut String) -> io::Result<usize> {
        buf.clear();
        if let Some(cached) = self.line_cache.pop_front() {
            *buf = cached;
            self.line_index += 1;
            Ok(buf.len())
        } else {
            let mut z = self.inner.read_line(buf)?;
            if z != 0 {
                self.line_index += 1;
            } else {
                self.state = ParserState::Eof;
                return Ok(0);
            }

            while buf.trim().is_empty() || buf.starts_with('#') {
                // Inlined the basis of 'read_next_line' to prevent stack overflows with recursively calling this function again
                buf.clear();
                let bytes_read = if let Some(cached) = self.line_cache.pop_front() {
                    *buf = cached;
                    buf.len()
                } else {
                    self.inner.read_line(buf)?
                };
                if bytes_read == 0 {
                    self.state = ParserState::Eof;
                    return Ok(0);
                }
                self.line_index += 1;
                z += bytes_read;
            }
            Ok(z)
        }
    }

    /// Read a single attribute from the current position. Gives back the range of the value.
    /// # Errors
    /// If the current line is not a valid attribute.
    fn read_attribute(
        &mut self,
        buf: &mut String,
    ) -> Result<
        (Option<u32>, Attribute, std::ops::Range<usize>),
        BoxedError<'static, MzSpecLibErrorKind>,
    > {
        let z = self.read_next_line(buf).map_err(|e| {
            BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )
        })?;
        if z == 0 {
            return Err(BoxedError::new(
                MzSpecLibErrorKind::Eof,
                "Early end of file",
                "Expected to read an attribute but the file already ended.",
                self.current_context().lines(0, &*buf).to_owned(),
            ));
        }
        Attribute::parse(buf.trim_ascii_end()).map_err(|e: AttributeParseError| {
            BoxedError::new(
                MzSpecLibErrorKind::Attribute,
                "Invalid attribute",
                e.to_string(),
                self.current_context().lines(0, &*buf).to_owned(),
            )
        })
    }

    /// Read attributes sets at the current point in the reader and store them in the header.
    /// # Errors
    /// When an attribute set is invalid or if the structure is invalid.
    fn read_attribute_sets(&mut self) -> Result<(), BoxedError<'static, MzSpecLibErrorKind>> {
        type IntermediateAttributeSet = (
            EntryType,
            String,
            HashMap<Option<u32>, Vec<(Attribute, Context<'static>)>>,
        );

        fn store<R: Read>(
            current_attribute_set: Option<IntermediateAttributeSet>,
            parser: &mut MzSpecLibTextParser<'_, R>,
        ) {
            if let Some((namespace, id, attributes)) = current_attribute_set {
                let mut attribute_vec = vec![Vec::new(); 1];
                attribute_vec[0].extend(
                    attributes
                        .get(&None)
                        .iter()
                        .flat_map(|a| a.iter())
                        .map(|(a, _)| a.clone()),
                );
                attribute_vec.extend(
                    attributes
                        .iter()
                        .filter_map(|(g, a)| g.map(|g| (g, a)))
                        .sorted_by_key(|(g, _)| *g)
                        .map(|(_, a)| a.iter().map(|(a, _)| a).cloned().collect()),
                );

                parser
                    .header
                    .attribute_classes
                    .entry(namespace)
                    .or_default()
                    .push(AttributeSet {
                        id: id.clone(),
                        namespace,
                        attributes: attribute_vec,
                    });
                let base = parser
                    .header_attribute_sets_with_context
                    .entry(namespace)
                    .or_default()
                    .entry(id)
                    .or_default();
                for (key, value) in &attributes {
                    base.entry(*key).or_default().extend_from_slice(value);
                }
            }
        }

        let mut buf = String::new();
        let mut current_attribute_set: Option<IntermediateAttributeSet> = None;

        loop {
            match self.read_attribute(&mut buf) {
                Ok((group_id, attr, _range)) => {
                    current_attribute_set
                        .as_mut()
                        .ok_or_else(|| {
                            BoxedError::new(
                                // This should be impossible to reach because any lingering attributes should have been picked up in the header
                                MzSpecLibErrorKind::Declaration,
                                "Invalid attribute set",
                                "An attribute was defined before an attribute set was defined",
                                self.current_context().lines(0, &buf).to_owned(),
                            )
                        })?
                        .2
                        .entry(group_id)
                        .or_default()
                        .push((attr, self.current_context().lines(0, &buf).to_owned()));
                }
                Err(e) => {
                    if buf.starts_with('<') {
                        if buf.starts_with("<Spectrum=") {
                            store(current_attribute_set.take(), self);
                            self.state = ParserState::Spectrum;
                            self.push_back_line(buf);
                            break;
                        } else if buf.starts_with("<AttributeSet") {
                            self.state = ParserState::AttributeSet;
                            if let Some((_, rest)) = buf.trim().split_once(' ') {
                                if !rest.ends_with('>') {
                                    return Err(BoxedError::new(
                                        MzSpecLibErrorKind::Declaration,
                                        "Invalid attribute set",
                                        "The closing bracket '>' is missing",
                                        self.current_context().lines(0, &buf).to_owned(),
                                    ));
                                }
                                if let Some((entry_tp, id)) = rest[..rest.len() - 1].split_once('=')
                                {
                                    store(current_attribute_set.take(), self);
                                    let set_entry_tp = match entry_tp {
                                        "Spectrum" => EntryType::Spectrum,
                                        "Analyte" => EntryType::Analyte,
                                        "Interpretation" => EntryType::Interpretation,
                                        "Cluster" => EntryType::Cluster,
                                        _ => {
                                            return Err(BoxedError::new(
                                                MzSpecLibErrorKind::Declaration,
                                                "Invalid attribute set type",
                                                "Use 'Spectrum', 'Analyte', 'Interpretation', or 'Cluster' and note that this is case sensitive",
                                                self.current_context().lines(0, &buf).to_owned(),
                                            ));
                                        }
                                    };
                                    let set_id = id.to_string();
                                    current_attribute_set =
                                        Some((set_entry_tp, set_id, HashMap::new()));
                                } else {
                                    return Err(BoxedError::new(
                                        MzSpecLibErrorKind::Declaration,
                                        "Invalid attribute set",
                                        "The id for the attribute set is missing, is should look like '<AttributeSet Spectrum=all>' and the equals '=' was missing",
                                        self.current_context().lines(0, &buf).to_owned(),
                                    ));
                                }
                            } else {
                                return Err(BoxedError::new(
                                    MzSpecLibErrorKind::Declaration,
                                    "Invalid attribute set",
                                    "The id for the attribute set is missing, is should look like '<AttributeSet Spectrum=all>' and the space was missing",
                                    self.current_context().lines(0, &buf).to_owned(),
                                ));
                            }
                        } else {
                            return Err(BoxedError::new(
                                MzSpecLibErrorKind::Declaration,
                                "Invalid declaration",
                                "In the header only attribute sets can be defined '<AttributeSet Spectrum=all>' or a header can be followed by a '<Spectrum=XX>'",
                                self.current_context().lines(0, &buf).to_owned(),
                            ));
                        }
                    } else if buf.is_empty() {
                        // continue
                    } else {
                        return Err(e);
                    }
                }
            }
        }
        Ok(())
    }

    /// Read the mzSpecLib header and store it in this structure.
    /// # Errors
    /// If the header is invalid
    fn read_header(&mut self) -> Result<(), BoxedError<'static, MzSpecLibErrorKind>> {
        let mut buf = String::new();
        let z = self.read_next_line(&mut buf).map_err(|e| {
            BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )
        })?;
        if z == 0 {
            return Err(BoxedError::new(
                MzSpecLibErrorKind::Eof,
                "Early end of file",
                "Expected to read the header but the file already ended.",
                self.current_context().lines(0, &buf).to_owned(),
            ));
        }

        if !buf.starts_with("<mzSpecLib>") {
            return Err(BoxedError::new(
                MzSpecLibErrorKind::Declaration,
                "Invalid file start",
                "The first line in an mzSpecLib.txt file should be '<mzSpecLib>'",
                self.current_context().lines(0, &buf).to_owned(),
            ));
        }
        self.state = ParserState::Header;

        loop {
            match self.read_attribute(&mut buf) {
                Ok((group_id, attr, _range)) => match attr.name.accession {
                    curie!(MS:1003186) => {
                        self.header.format_version = attr.value.to_string();
                    }
                    _ => {
                        let index = group_id.map_or(0, |i| i as usize + 1);
                        if self.header.attributes.len() <= index {
                            self.header.attributes.extend(std::iter::repeat_n(
                                Vec::new(),
                                index - self.header.attributes.len() + 1,
                            ));
                        }
                        self.header.attributes[index].push(attr);
                    }
                },
                Err(e) => {
                    if buf.starts_with('<') {
                        if buf.starts_with("<Spectrum=") {
                            self.state = ParserState::Spectrum;
                        } else if buf.starts_with("<AttributeSet") {
                            self.state = ParserState::AttributeSet;
                        } else {
                            return Err(BoxedError::new(
                                MzSpecLibErrorKind::Declaration,
                                "Invalid declaration",
                                "In the header only attribute sets can be defined '<AttributeSet Spectrum=all>' or a header can be followed by a '<Spectrum=XX>'",
                                self.current_context().lines(0, &buf).to_owned(),
                            ));
                        }

                        self.push_back_line(buf);
                        break;
                    } else if buf.is_empty() && e.get_kind() != MzSpecLibErrorKind::Eof {
                        // continue
                    } else {
                        return Err(e);
                    }
                }
            }
        }

        if !matches!(self.state, ParserState::AttributeSet) {
            return Ok(());
        }

        self.read_attribute_sets()?;

        let mut first = true;
        self.header.attributes.retain(|v| {
            let retain = !v.is_empty() || first;
            first = false;
            retain
        });

        Ok(())
    }

    /// Parse an open declaration tag. Example: `<Spectrum=1>`
    /// # Errors
    /// * If the open declaration tag is not the first thing in the buffer.
    /// * If the declaration does not contain a '='.
    /// * If the declaration is not followed by a valid integer.
    fn parse_open_declaration(
        &mut self,
        buf: &str,
        declaration: &str,
        to_state: ParserState,
    ) -> Result<u32, BoxedError<'static, MzSpecLibErrorKind>> {
        let id = if buf.starts_with(declaration) {
            self.state = to_state;
            if let Some(stripped) = buf.trim_ascii_end().strip_suffix('>')
                && let Some((before, val)) = stripped.split_once('=')
            {
                val.trim().parse::<Id>().map_err(|e| {
                    BoxedError::new(
                        MzSpecLibErrorKind::Declaration,
                        "Invalid declaration",
                        format!("The ID was {}", explain_number_error(&e)),
                        self.current_context()
                            .lines(0, buf)
                            .to_owned()
                            .add_highlight((0, before.len() + 1, val.len())),
                    )
                })?
            } else {
                return Err(BoxedError::new(
                    MzSpecLibErrorKind::Declaration,
                    "Invalid declaration",
                    "The ID needs to contain an equals symbol `=` and this was missing",
                    self.current_context().lines(0, buf).to_owned(),
                ));
            }
        } else {
            return Err(BoxedError::new(
                MzSpecLibErrorKind::Declaration,
                "Invalid declaration",
                format!("The declaration `{declaration}` was expected but not found"),
                self.current_context().lines(0, buf).to_owned(),
            ));
        };
        Ok(id)
    }

    /// Parse an analyte from the stream.
    /// # Errors
    /// If the analyte
    fn read_analyte(&mut self) -> Result<Analyte, BoxedError<'static, MzSpecLibErrorKind>> {
        let mut buf = String::new();
        let z = self.read_next_line(&mut buf).map_err(|e| {
            BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )
        })?;
        if z == 0 {
            return Err(BoxedError::new(
                MzSpecLibErrorKind::Eof,
                "Early end of file",
                "Expected to read an analyte but the file already ended.",
                self.current_context().to_owned(),
            ));
        }
        let id = self.parse_open_declaration(&buf, "<Analyte=", ParserState::Analyte)?;
        let mut groups: HashMap<u32, Vec<(Attribute, Context)>> = HashMap::new();

        let mut analyte = Analyte::new(id, AnalyteTarget::default());
        let mut protein = ProteinDescription::default();
        loop {
            match self.read_attribute(&mut buf) {
                Ok((group_id, attr, range)) => {
                    if attr.name.accession == curie!(MS:1003270)
                        && let Value::String(_value) = attr.value.scalar().as_ref()
                    {
                        let (mut peptidoform_ion, _) =
                            PeptidoformIon::pro_forma_inner(&self.current_context().lines(0, &buf).to_owned(), &buf, range.clone(), self.ontologies).map_err(|errs| BoxedError::new(context_error::BasicKind::Error, "Invalid ProForma definition", "The string could not be parsed as a ProForma definition", self.current_context().lines(0, &buf).add_highlight((0, range)).to_owned()).add_underlying_errors(errs)).map_err(
                                |e| e.to_owned().convert::<MzSpecLibErrorKind, BoxedError<'static, MzSpecLibErrorKind>>(|_| MzSpecLibErrorKind::ProForma),
                            )?;
                        if peptidoform_ion.get_charge_carriers().is_none() {
                            match analyte.target {
                                AnalyteTarget::Unknown(Some(c)) => {
                                    peptidoform_ion
                                        .set_charge_carriers(Some(MolecularCharge::proton(c)));
                                }
                                AnalyteTarget::MolecularFormula(f)
                                    if peptidoform_ion.get_charge_carriers().is_none()
                                        && f.charge().value != 0 =>
                                {
                                    peptidoform_ion.set_charge_carriers(Some(
                                        MolecularCharge::proton(f.charge()),
                                    ));
                                }
                                _ => (),
                            }
                        }

                        analyte.target = AnalyteTarget::PeptidoformIon(peptidoform_ion);
                    } else if attr.name.accession == curie!(MS:1000866) {
                        let value = attr.value.scalar().to_string();
                        let mut formula = MolecularFormula::pro_forma::<false,false>(
                            &value,

                        )
                        .map_err(|e| e.to_owned().convert::<MzSpecLibErrorKind, BoxedError<'static, MzSpecLibErrorKind>>(|_| MzSpecLibErrorKind::ProForma))?;
                        analyte.target = match analyte.target {
                            AnalyteTarget::Unknown(c) => {
                                if let Some(c) = c {
                                    formula.set_charge(c);
                                }
                                AnalyteTarget::MolecularFormula(formula)
                            }
                            AnalyteTarget::MolecularFormula(_) => {
                                AnalyteTarget::MolecularFormula(formula) // TODO: detect double definitions?
                            }
                            AnalyteTarget::PeptidoformIon(pep) => {
                                AnalyteTarget::PeptidoformIon(pep) // TODO: detect incongruent definitions?
                            }
                        };
                    } else if attr.name.accession == curie!(MS:1000041)
                        && let Value::Int(value) = attr.value.scalar().as_ref()
                    {
                        let charge = Charge::new::<mzcore::system::e>(*value as isize);
                        analyte.target.set_charge(charge);
                    } else if [
                        curie!(MS:1000888), // Stripped peptide sequence
                        curie!(MS:1003043), // number of residues
                        curie!(MS:1003053), // theoretical monoisotopic m/z
                        curie!(MS:1003054), // theoretical average m/z
                        curie!(MS:1000224), // molecular mass (average weight MH+)
                        curie!(MS:1003243), // adduct ion mass (monoisotopic MH+)
                        curie!(MS:1001117), // theoretical neutral mass (monoisotopic M)
                    ]
                    .contains(&attr.name.accession)
                    {
                        // Ignore, can be calculated easily on the fly
                    } else if let Some(group_id) = group_id {
                        groups
                            .entry(group_id)
                            .or_default()
                            .push((attr, self.current_context().lines(0, &buf).to_owned()));
                    } else if !protein.populate_from_attribute(
                        &attr,
                        &self.current_context().lines(0, &buf).to_owned(),
                    )? {
                        analyte.params.push(attr.into());
                    }
                }
                Err(e) => {
                    if buf.starts_with('<') {
                        if !protein.is_empty() {
                            analyte.proteins.push(protein);
                        }
                        self.push_back_line(buf);
                        break;
                    } else if buf.trim().is_empty() {
                        // continue;
                    } else {
                        return Err(e);
                    }
                }
            }
        }

        for group in groups.values() {
            if let Some((value, _)) = group
                .iter()
                .find(|a| a.0.name.accession == curie!(MS:1003275))
                && let Some((name, _)) = group
                    .iter()
                    .find(|a| a.0.name.accession == curie!(MS:1003276))
                && group.len() == 2
            {
                analyte.params.push(match &name.value {
                    AttributeValue::Term(term) => mzdata::params::Param {
                        accession: Some(term.accession.accession),
                        name: term.name.to_string(),
                        value: value.value.scalar().into_owned(),
                        controlled_vocabulary: Some(term.accession.controlled_vocabulary),
                        unit: Unit::Unknown,
                    },
                    e => mzdata::params::Param {
                        accession: None,
                        name: e.scalar().to_string(),
                        value: value.value.scalar().into_owned(),
                        controlled_vocabulary: None,
                        unit: Unit::Unknown,
                    },
                });
            } else {
                let mut protein = ProteinDescription::default();
                let attr_sets: Vec<_> = group
                    .iter()
                    .filter(|(a, _)| a.name == term!(MS:1003212|library attribute set name))
                    .map(|(v, _)| v.value.to_string())
                    .collect();
                for (attribute, context) in group.iter().chain(
                    self.header_attribute_sets_with_context
                        .get(&EntryType::Analyte)
                        .into_iter()
                        .flatten()
                        .filter(|set| attr_sets.contains(set.0))
                        .flat_map(|a| a.1.values())
                        .flatten(),
                ) {
                    if !protein.populate_from_attribute(attribute, context)? {
                        protein.attributes.push(attribute.clone());
                    }
                }
                if group.len() == protein.attributes.len() {
                    // If zero attributes were understood as protein descriptions just store
                    // them as analyte parameters
                    analyte
                        .params
                        .extend(protein.attributes.into_iter().map(Into::into));
                } else {
                    analyte.proteins.push(protein);
                }
            }
        }

        Ok(analyte)
    }

    /// Read an interpretation from the current point in the reader. Assumes the current lines starts with `<Interpretation=`.
    /// # Errors
    /// If there is no valid interpretation at the current point or if the interpretation tag is missing.
    fn read_interpretation(
        &mut self,
    ) -> Result<Interpretation, BoxedError<'static, MzSpecLibErrorKind>> {
        let mut buf = String::new();
        let z = self.read_next_line(&mut buf).map_err(|e| {
            BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )
        })?;
        if z == 0 {
            return Err(BoxedError::new(
                MzSpecLibErrorKind::Eof,
                "Early end of file",
                "Expected to read an interpretation but the file already ended.",
                self.current_context().to_owned(),
            ));
        }
        let id =
            self.parse_open_declaration(&buf, "<Interpretation=", ParserState::Interpretation)?;
        let mut interp = Interpretation {
            id,
            attributes: vec![Vec::new(); 1],
            ..Default::default()
        };
        loop {
            match self.read_attribute(&mut buf) {
                Ok((group_id, attribute, range)) => {
                    if attribute.name == term!(MS:1002357|PSM-level probability) {
                        interp.probability =
                            Some(attribute.value.scalar().to_f64().map_err(|e| {
                                BoxedError::new(
                                    MzSpecLibErrorKind::Attribute,
                                    "Invalid PSM-level probability",
                                    e.to_string(),
                                    self.current_context()
                                        .lines(0, &buf)
                                        .add_highlight((0, range))
                                        .to_owned(),
                                )
                            })?);
                    } else if [
                        curie!(MS:1003288), // number of unassigned peaks
                        curie!(MS:1003079), // total unassigned intensity fraction
                        curie!(MS:1003080), // top 20 peak unassigned intensity fraction
                        curie!(MS:1003290), // number of unassigned peaks among top 20 peaks
                        curie!(MS:1003289), // intensity of highest unassigned peak
                        curie!(MS:1001975), // delta m/z
                        curie!(MS:1003209), // monoisotopic m/z deviation
                        curie!(UO:0000000), // unit (for m/z deviation)
                    ]
                    .contains(&attribute.name.accession)
                    {
                        // Ignore, can be calculated easily on the fly
                    } else {
                        let index = group_id.map_or(0, |i| i as usize + 1);
                        if interp.attributes.len() <= index {
                            interp.attributes.extend(std::iter::repeat_n(
                                Vec::new(),
                                index - interp.attributes.len() + 1,
                            ));
                        }
                        interp.attributes[index].push(attribute);
                    }
                }
                Err(e) => {
                    if buf.starts_with("<InterpretationMember=") {
                        self.push_back_line(buf.clone());
                        return Err(BoxedError::new(
                            MzSpecLibErrorKind::UnsupportedFeature,
                            "Unsupported mzSpecLib feature detected",
                            "Interpretation members are currently not supported",
                            self.current_context().lines(1, &buf).to_owned(),
                        ));
                    } else if buf.starts_with('<') {
                        self.push_back_line(buf);
                        break;
                    } else if buf.trim().is_empty() {
                        // continue;
                    } else {
                        return Err(e);
                    }
                }
            }
        }

        let attr_sets: Vec<_> = interp.attributes[0]
            .iter()
            .filter(|a| a.name == term!(MS:1003212|library attribute set name))
            .map(|v| v.value.to_string())
            .collect();
        for name in attr_sets {
            for attr_set in self
                .header
                .attribute_classes
                .get(&EntryType::Interpretation)
                .into_iter()
                .flatten()
            {
                if attr_set.id == name || attr_set.id == "all" {
                    merge_attributes(&mut interp.attributes, &attr_set.attributes); // TODO: try to interpret the merged attributes as well.
                }
            }
        }
        let mut first = true;
        interp.attributes.retain(|v| {
            let retain = !v.is_empty() || first;
            first = false;
            retain
        });
        Ok(interp)
    }

    /// Parse an mzSpecLib peak line.
    /// # Errors
    /// If the line is not a valid mzSpecLib peak line.
    fn parse_peak_line(
        &self,
        buf: &str,
    ) -> Result<AnnotatedPeak<Fragment>, BoxedError<'static, MzSpecLibErrorKind>> {
        let mut field_offset = buf.chars().take_while(char::is_ascii_whitespace).count(); // Because only ASCII this is a bytes offset as well
        let mut it = buf[field_offset..].split('\t');

        let mz = it
            .next()
            .ok_or_else(|| {
                BoxedError::new(
                    MzSpecLibErrorKind::Peak,
                    "Peak m/z is missing",
                    "At least two columns are neccessary in the peaks data lines",
                    self.current_context().lines(0, buf).to_owned(),
                )
            })
            .and_then(|v| {
                let r = v.parse::<f64>().map_err(|e| {
                    BoxedError::new(
                        MzSpecLibErrorKind::Peak,
                        "Peak m/z is not a number",
                        e.to_string(),
                        self.current_context()
                            .lines(0, buf)
                            .to_owned()
                            .add_highlight((0, field_offset, v.len())),
                    )
                });
                field_offset += v.len() + 1;
                r
            })?;

        let intensity = it
            .next()
            .ok_or_else(|| {
                BoxedError::new(
                    MzSpecLibErrorKind::Peak,
                    "Peak intensity is missing",
                    "At least two columns are neccessary in the peaks data lines",
                    self.current_context().lines(0, buf).to_owned(),
                )
            })
            .and_then(|v| {
                let r = v.parse::<f32>().map_err(|e| {
                    BoxedError::new(
                        MzSpecLibErrorKind::Peak,
                        "Peak intensity is not a number",
                        e.to_string(),
                        self.current_context()
                            .lines(0, buf)
                            .to_owned()
                            .add_highlight((0, field_offset, v.len())),
                    )
                });
                field_offset += v.len() + 1;
                r
            })?;

        let mut peak = AnnotatedPeak::new(
            MassOverCharge::new::<mzcore::system::thomson>(mz),
            intensity,
            0,
            Vec::new(),
            Vec::new(),
        );

        match it.next() {
            Some(v) => {
                let v = v.trim();
                if !v.is_empty() && v != "?" {
                    let annots = Fragment::mz_paf_inner(
                        &self.current_context().lines(0, buf),
                        buf,
                        field_offset..field_offset + v.len(),
                        self.ontologies,
                        &self.last_compound_peptidoform,
                    )
                    .map_err(|e| {
                        e.to_owned()
                            .convert::<MzSpecLibErrorKind, BoxedError<'static, MzSpecLibErrorKind>>(
                                |_| MzSpecLibErrorKind::MzPAF,
                            )
                    })?;
                    peak.annotations = annots;
                }
            }
            None => return Ok(peak),
        }

        match it.next() {
            Some(v) => {
                peak.aggregations = v.split(',').map(ToString::to_string).collect();
            }
            None => return Ok(peak),
        }

        // TODO: what to do with any further columns?

        Ok(peak)
    }

    /// Read the next full spectrum. This assumes it is already at the "<Spectrum=XX>" line.
    /// # Errors
    /// IF the next spectrum contains data that is not a valid spectrum.
    fn read_spectrum(
        &mut self,
    ) -> Result<AnnotatedSpectrum, BoxedError<'static, MzSpecLibErrorKind>> {
        let mut buf = String::new();

        let z = self.read_next_line(&mut buf).map_err(|e| {
            BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )
        })?;
        if z == 0 {
            return Err(BoxedError::new(
                MzSpecLibErrorKind::Eof,
                "Early end of file",
                "Expected to read a spectrum but the file already ended.",
                self.current_context().to_owned(),
            ));
        }
        let key = self.parse_open_declaration(&buf, "<Spectrum=", ParserState::Spectrum)?;
        let mut spec = AnnotatedSpectrum {
            key,
            attributes: vec![Vec::new(); 1],
            ..Default::default()
        };

        // Set default assumptions on mzSpecLib files
        spec.description.ms_level = 2;
        spec.description.signal_continuity = mzdata::spectrum::SignalContinuity::Centroid;
        // TODO: do better but should work for now
        spec.description
            .acquisition
            .scans
            .push(ScanEvent::default());
        spec.description.acquisition.scans[0]
            .scan_windows
            .push(ScanWindow::default());
        spec.description.precursor.push(Precursor::default());
        spec.description.precursor[0]
            .ions
            .push(SelectedIon::default());

        let mut term_collection: HashMap<Option<u32>, Vec<(Attribute, Context<'static>)>> =
            HashMap::new();

        loop {
            match self.read_attribute(&mut buf) {
                Ok((group_id, attr, range)) => term_collection.entry(group_id).or_default().push((
                    attr,
                    self.current_context()
                        .to_owned()
                        .lines(0, buf.clone())
                        .add_highlight((0, range)),
                )),
                Err(e) => {
                    if buf.starts_with("<Analyte=") {
                        self.push_back_line(buf.clone());
                        let analyte = self.read_analyte()?;
                        spec.analytes.push(analyte);
                    } else if buf.starts_with("<Interpretation=") {
                        self.push_back_line(buf.clone());
                        let interp = self.read_interpretation()?;
                        spec.interpretations.push(interp);
                    } else if buf.starts_with("<Peaks>") {
                        self.state = ParserState::Peaks;
                        break;
                    } else if buf.trim().is_empty() {
                        // continue
                    } else {
                        return Err(e);
                    }
                }
            }
        }

        let set_names: Vec<_> = spec.attributes[0]
            .iter()
            .filter(|a| a.name == term!(MS:1003212|library attribute set name))
            .map(|v| v.value.to_string())
            .collect();

        populate_spectrum_description_from_attributes(
            term_collection.iter(),
            &mut spec.description,
            &mut spec.attributes,
        )?;
        if let Some(sets) = self
            .header_attribute_sets_with_context
            .get(&EntryType::Spectrum)
        {
            populate_spectrum_description_from_attributes(
                sets.iter()
                    .filter_map(|attr_set| {
                        (set_names.contains(attr_set.0) || attr_set.0 == "all")
                            .then_some(attr_set.1.iter())
                    })
                    .flatten(),
                &mut spec.description,
                &mut spec.attributes,
            )?;
        }

        spec.description.precursor[0]
            .activation
            ._extract_methods_from_params();

        self.last_compound_peptidoform = spec
            .analytes
            .iter()
            .map(|a| (a.id, a.target.clone()))
            .collect();

        if matches!(self.state, ParserState::Peaks) {
            loop {
                let z = self.read_next_line(&mut buf).map_err(|e| {
                    BoxedError::new(
                        MzSpecLibErrorKind::IO,
                        "IO error",
                        e.to_string(),
                        self.current_context().to_owned(),
                    )
                })?;
                if z == 0 {
                    break;
                }
                let buf_trimmed = buf.trim();
                if buf_trimmed.starts_with('<') {
                    self.push_back_line(buf);
                    self.state = ParserState::Between;
                    break;
                } else if buf_trimmed.is_empty() {
                    self.state = ParserState::Between;
                    break;
                }

                let peak = self.parse_peak_line(buf_trimmed)?;
                // TODO: validate that the peaks only reference existing analytes, also make sure that
                // spectra with both formula and peptidoforms actually get assigned the correct peaks
                spec.peaks.push(peak);
            }
        }
        Ok(spec)
    }

    /// Read the next spectrum. If the previous spectrum resulted in an error this will scan
    /// forward until the next "<Spectrum=XX>" is found.
    ///
    /// # Errors
    /// If the next spectrum contains invalid data.
    pub fn read_next(
        &mut self,
    ) -> Result<AnnotatedSpectrum, BoxedError<'static, MzSpecLibErrorKind>> {
        self.last_compound_peptidoform = Vec::new();
        if self.last_error {
            // If the last element went awry scan the buffer until the start of the next spectrum
            // is found, otherwise it generates an error for each skipped line.

            let mut buf = String::new();

            loop {
                let z = self.read_next_line(&mut buf).map_err(|e| {
                    BoxedError::new(
                        MzSpecLibErrorKind::IO,
                        "IO error",
                        e.to_string(),
                        self.current_context().to_owned(),
                    )
                })?;
                if z == 0 {
                    return Err(BoxedError::new(
                        MzSpecLibErrorKind::Eof,
                        "Early end of file",
                        "Expected to read the next spectrum but the file already ended.",
                        self.current_context().to_owned(),
                    ));
                }
                if buf.starts_with("<Spectrum=") {
                    self.push_back_line(buf);
                    break;
                }
            }
        }
        let res = self.read_spectrum();
        self.last_error = res.is_err();
        res
    }
}

impl<R: BufRead> Iterator for MzSpecLibTextParser<'_, R> {
    type Item = Result<AnnotatedSpectrum, BoxedError<'static, MzSpecLibErrorKind>>;

    fn next(&mut self) -> Option<Self::Item> {
        if matches!(self.state, ParserState::Eof) {
            return None;
        }
        Some(self.read_next())
    }
}

/// The start of a library item for indexed access
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, PartialOrd)]
pub struct LibraryIndexRecord {
    /// The byte offset into the file
    pub offset: u64,
    /// The line index
    pub line_index: u32,
}

/// An index to find the byte offsets of all records in a file
#[derive(Clone, Debug)]
pub struct LibraryIndex {
    /// Indicate if the full file is scanned yet
    done: bool,
    primary: IndexMap<usize, LibraryIndexRecord>,
    key: HashMap<Id, usize>,
    name: HashMap<Box<str>, usize>,
    scan_number: HashMap<usize, usize>,
}

impl LibraryIndex {
    /// Insert a record into the spectrum index
    fn insert(
        &mut self,
        index: usize,
        key: Id,
        name: Option<Box<str>>,
        scan_number: Option<usize>,
        record: LibraryIndexRecord,
    ) {
        self.key.insert(key, index);
        if let Some(name) = name {
            self.name.insert(name, index);
        }
        if let Some(scan_number) = scan_number {
            self.scan_number.insert(scan_number, index);
        }
        self.primary.insert(index, record);
    }

    /// Get a record by index (the 0 based index number of the order of the spectra in the file)
    pub fn get_by_index(&self, index: usize) -> Option<&LibraryIndexRecord> {
        self.primary.get(&index)
    }

    /// Get a record by ID (the record ID as defined "<Spectrum=XX>")
    pub fn get_by_key(&self, key: Id) -> Option<&LibraryIndexRecord> {
        self.primary.get(self.key.get(&key)?)
    }

    /// Get a record by name/native ID (MS:1003061)
    pub fn get_by_name(&self, name: &str) -> Option<&LibraryIndexRecord> {
        self.primary.get(self.name.get(name)?)
    }

    /// Get a record by scan number (MS:1003057)
    pub fn get_by_scan_number(&self, scan_number: usize) -> Option<&LibraryIndexRecord> {
        self.primary.get(self.scan_number.get(&scan_number)?)
    }

    /// Get the number of spectra in the index
    pub fn len(&self) -> usize {
        self.primary.len()
    }

    /// Check if the spectrum index is empty
    pub fn is_empty(&self) -> bool {
        self.primary.is_empty()
    }
}

impl<R: BufRead + Seek> MzSpecLibTextParser<'_, R> {
    /// Build the spectrum index.
    ///
    /// # Errors
    /// When the underlying stream errors.
    pub fn build_index(&mut self) -> io::Result<()> {
        type IntermediateRecord = (
            usize,              // index
            Id,                 // key
            Option<Box<str>>,   // name (if found)
            Option<usize>,      // scan number (if found)
            LibraryIndexRecord, // record
        );

        self.inner.rewind()?;
        self.line_cache.clear();
        let mut buf = String::new();
        let mut line_count = 0;
        let mut offset_index = LibraryIndex {
            done: false,
            primary: IndexMap::new(),
            key: HashMap::new(),
            name: HashMap::new(),
            scan_number: HashMap::new(),
        };
        let mut offset = 0;
        let mut current_record: Option<IntermediateRecord> = None;
        let mut index = 0;
        while let Ok(z) = self.read_next_line(&mut buf) {
            if z == 0 {
                break;
            }

            if let Some(rest) = buf.strip_prefix("<Spectrum=")
                && let Some(key) = rest.trim().strip_suffix('>')
            {
                if let Some((index, key, name, scan_number, record)) = current_record {
                    offset_index.insert(index, key, name, scan_number, record);
                }
                if let Ok(key) = key.parse::<Id>() {
                    current_record = Some((
                        index,
                        key,
                        None,
                        None,
                        LibraryIndexRecord {
                            offset,
                            line_index: line_count,
                        },
                    ));
                    index += 1;
                } else {
                    // This is an 'all' (or something like that) spectrum, so reset all info but do not increase the index number
                    current_record = None;
                }
            } else if let Some(rest) = buf.strip_prefix("MS:1003061|library spectrum name=")
                && let Some((_, _, name, _, _)) = &mut current_record
            {
                *name = Some(rest.trim().to_string().into_boxed_str());
            } else if let Some(rest) = buf.strip_prefix("MS:1003057|scan number=")
                && let Ok(scan_number) = rest.trim().parse::<usize>()
                && let Some((_, _, _, num, _)) = &mut current_record
            {
                *num = Some(scan_number);
            }
            line_count += 1;
            offset += z as u64;
        }
        if let Some((index, key, name, scan_number, record)) = current_record {
            offset_index.insert(index, key, name, scan_number, record);
        }
        offset_index.done = true;
        self.offsets = offset_index;
        self.inner.rewind()?;
        Ok(())
    }

    /// Get the total number of spectra in the spectrum index.
    pub fn len(&self) -> usize {
        self.offsets.len()
    }

    /// See if the spectrum index is empty.
    pub fn is_empty(&self) -> bool {
        self.offsets.is_empty()
    }

    /// Get a spectrum by the ID, if the index is not yet built this first builds the index.
    /// It returns None when the key does not exist and an error if the spectrum index could not
    /// be built, the correct reader could not be repositioned to the correct location, or the
    /// spectrum could not be correctly parsed.
    pub fn get_spectrum_by_key(
        &mut self,
        key: Id,
    ) -> Option<Result<AnnotatedSpectrum, BoxedError<'static, MzSpecLibErrorKind>>> {
        if !self.offsets.done
            && let Err(e) = self.build_index()
        {
            return Some(Err(BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )));
        }
        let rec = self.offsets.get_by_key(key)?;
        if let Err(e) = self.inner.seek(io::SeekFrom::Start(rec.offset)) {
            return Some(Err(BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )));
        }
        self.line_index = rec.line_index;
        self.line_cache.clear();
        Some(self.read_next())
    }

    /// Get a spectrum by the scan number (MS:1003057), if the index is not yet built this first
    /// builds the index. It returns None when the key does not exist and an error if the spectrum
    /// index could not be built, the correct reader could not be repositioned to the correct
    /// location, or the spectrum could not be correctly parsed.
    pub fn get_spectrum_by_scan_number(
        &mut self,
        scan_number: usize,
    ) -> Option<Result<AnnotatedSpectrum, BoxedError<'static, MzSpecLibErrorKind>>> {
        if !self.offsets.done
            && let Err(e) = self.build_index()
        {
            return Some(Err(BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )));
        }
        let rec = self.offsets.get_by_scan_number(scan_number)?;
        if let Err(e) = self.inner.seek(io::SeekFrom::Start(rec.offset)) {
            return Some(Err(BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )));
        }
        self.line_index = rec.line_index;
        self.line_cache.clear();
        Some(self.read_next())
    }

    /// Get a spectrum by the index, if the index is not yet built this first builds the index.
    /// It returns None when the index does not exist and an error if the spectrum index could not
    /// be built, the correct reader could not be repositioned to the correct location, or the
    /// spectrum could not be correctly parsed.
    pub fn get_spectrum_by_index(
        &mut self,
        index: usize,
    ) -> Option<Result<AnnotatedSpectrum, BoxedError<'static, MzSpecLibErrorKind>>> {
        if !self.offsets.done
            && let Err(e) = self.build_index()
        {
            return Some(Err(BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )));
        }
        let rec = self.offsets.get_by_index(index)?;
        if let Err(e) = self.inner.seek(io::SeekFrom::Start(rec.offset)) {
            return Some(Err(BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )));
        }
        self.line_index = rec.line_index;
        self.line_cache.clear();
        Some(self.read_next())
    }

    /// Get a spectrum by the name (MS:1003061), if the index is not yet built this first builds
    /// the index. It returns None when the name does not exist and an error if the spectrum index
    /// could not be built, the correct reader could not be repositioned to the correct location,
    /// or the spectrum could not be correctly parsed.
    pub fn get_spectrum_by_name(
        &mut self,
        name: &str,
    ) -> Option<Result<AnnotatedSpectrum, BoxedError<'static, MzSpecLibErrorKind>>> {
        if !self.offsets.done
            && let Err(e) = self.build_index()
        {
            return Some(Err(BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )));
        }
        let rec = self.offsets.get_by_name(name)?;
        if let Err(e) = self.inner.seek(io::SeekFrom::Start(rec.offset)) {
            return Some(Err(BoxedError::new(
                MzSpecLibErrorKind::IO,
                "IO error",
                e.to_string(),
                self.current_context().to_owned(),
            )));
        }
        self.line_index = rec.line_index;
        self.line_cache.clear();
        Some(self.read_next())
    }
}

#[allow(clippy::missing_panics_doc, clippy::missing_errors_doc)]
#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_indexed_reader() {
        let buf = BufReader::new(
            File::open("../data/chinese_hamster_hcd_selected_head.mzspeclib.txt").unwrap(),
        );

        let mut this =
            MzSpecLibTextParser::open(buf, None, &mzcore::ontology::STATIC_ONTOLOGIES).unwrap();

        this.build_index().unwrap();

        eprintln!("{:#?}", this.offsets);

        assert_eq!(this.len(), 7);

        let spec = this.get_spectrum_by_index(3).unwrap().unwrap();
        assert_eq!(spec.key, 4);

        let spec = this.get_spectrum_by_scan_number(5538).unwrap().unwrap();
        assert_eq!(spec.key, 1);

        let spec = this
            .get_spectrum_by_name("AAAALGSHGSCSSEVEK/2_1(10,C,CAM)_50eV")
            .unwrap()
            .unwrap();
        assert_eq!(spec.key, 6);

        let spec = this.get_spectrum_by_index(6).unwrap().unwrap();
        assert_eq!(spec.key, 7);
    }

    #[test]
    fn test_header() -> Result<(), BoxedError<'static, MzSpecLibErrorKind>> {
        let buf = BufReader::new(
            File::open("../data/chinese_hamster_hcd_selected_head.mzspeclib.txt").unwrap(),
        );

        let mut this = MzSpecLibTextParser::open(buf, None, &mzcore::ontology::STATIC_ONTOLOGIES)?;
        let header = this.header();
        eprintln!("{header:?}");
        // TODO: switch to assigning these basic attributes to header fields
        assert_eq!(header.attributes.len(), 1);

        let _spec = this.read_next()?;

        Ok(())
    }

    #[test]
    fn unknown_cv_value() {
        let text = r"<mzSpecLib>
MS:1003186|library format version=UW:0000000|text";

        let res =
            MzSpecLibTextParser::open(text.as_bytes(), None, &mzcore::ontology::STATIC_ONTOLOGIES);
        assert!(res.is_err());
    }

    #[test]
    fn invalid_declaration() {
        // Fuzz panic: it crashed on having a more than 1 byte character at the end of a declaration
        let text = r"<mzSpecLib>
MS:1003186|library format version=1.0
<Spectrum=1>擅";

        let mut res =
            MzSpecLibTextParser::open(text.as_bytes(), None, &mzcore::ontology::STATIC_ONTOLOGIES)
                .unwrap();
        let first = res.next().unwrap();
        assert!(first.is_err());
    }

    #[test]
    fn unknown_cv_term_hang() {
        let text = r"<mzSpecLib>
MS:0000000|unknown=a";

        let res =
            MzSpecLibTextParser::open(text.as_bytes(), None, &mzcore::ontology::STATIC_ONTOLOGIES);
        assert!(res.is_err());
    }

    #[test]
    fn stack_overflow() {
        let text = b"<mzSpecLib>\n";
        let bytes: Vec<u8> = text
            .iter()
            .copied()
            .chain(std::iter::repeat_n(b'\n', 10000))
            .collect();

        let res =
            MzSpecLibTextParser::open(bytes.as_slice(), None, &mzcore::ontology::STATIC_ONTOLOGIES);
        assert!(res.is_err());
    }
}