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
//! Efficient FASTQ reading and writing
//!

use memchr::memchr;
use std::char;
use std::fs::File;
use std::io::{self, BufRead, Seek};
use std::iter;
use std::path::Path;
use std::slice;
use std::str::{self, Utf8Error};

use buffer_redux;

use super::policy::{BufPolicy, StdPolicy};
use super::*;

use std::error::Error as StdError;

type DefaultBufPolicy = StdPolicy;

const BUFSIZE: usize = 64 * 1024;

#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
enum SearchPos {
    Head,
    Seq,
    Sep,
    Qual,
}

/// FASTQ parser.
pub struct Reader<R: io::Read, P = DefaultBufPolicy> {
    buf_reader: buffer_redux::BufReader<R>,
    buf_pos: BufferPosition,
    search_pos: SearchPos,
    position: Position,
    finished: bool,
    buf_policy: P,
}

impl<R> Reader<R, DefaultBufPolicy>
where
    R: io::Read,
{
    /// Creates a new reader with the default buffer size of 64 KiB
    ///
    /// # Example:
    ///
    /// ```
    /// use seq_io::fastq::{Reader, Record};
    /// let fastq = b"@id\nACGT\n+\nIIII";
    ///
    /// let mut reader = Reader::new(&fastq[..]);
    /// let record = reader.next().unwrap().unwrap();
    /// assert_eq!(record.id(), Ok("id"))
    /// ```
    #[inline]
    pub fn new(reader: R) -> Reader<R, StdPolicy> {
        Reader::with_capacity(reader, BUFSIZE)
    }

    /// Creates a new reader with a given buffer capacity. The minimum allowed
    /// capacity is 3.
    #[inline]
    pub fn with_capacity(reader: R, capacity: usize) -> Reader<R, StdPolicy> {
        assert!(capacity >= 3);
        Reader {
            buf_reader: buffer_redux::BufReader::with_capacity(capacity, reader),
            buf_pos: BufferPosition::default(),
            search_pos: SearchPos::Head,
            position: Position::new(1, 0),
            finished: false,
            buf_policy: StdPolicy,
        }
    }
}

impl Reader<File, DefaultBufPolicy> {
    /// Creates a reader from a file path.
    ///
    /// # Example:
    ///
    /// ```no_run
    /// use seq_io::fastq::Reader;
    ///
    /// let mut reader = Reader::from_path("seqs.fastq").unwrap();
    ///
    /// // (... do something with the reader)
    /// ```
    #[inline]
    pub fn from_path<P: AsRef<Path>>(path: P) -> io::Result<Reader<File>> {
        File::open(path).map(Reader::new)
    }
}

impl<R, P> Reader<R, P>
where
    R: io::Read,
    P: BufPolicy,
{
    /// Returns a reader with the given buffer policy applied
    #[inline]
    pub fn set_policy<T: BufPolicy>(self, policy: T) -> Reader<R, T> {
        Reader {
            buf_reader: self.buf_reader,
            buf_pos: self.buf_pos,
            position: self.position,
            search_pos: self.search_pos,
            finished: self.finished,
            buf_policy: policy,
        }
    }

    /// Returns the `BufPolicy` of the reader
    #[inline]
    pub fn policy(&self) -> &P {
        &self.buf_policy
    }

    /// Searches the next FASTQ record and returns a [RefRecord](struct.RefRecord.html) that
    /// borrows its data from the underlying buffer of this reader.
    ///
    /// # Example:
    ///
    /// ```no_run
    /// use seq_io::fastq::{Reader, Record};
    ///
    /// let mut reader = Reader::from_path("seqs.fastq").unwrap();
    ///
    /// while let Some(record) = reader.next() {
    ///     let record = record.unwrap();
    ///     println!("{}", record.id().unwrap());
    /// }
    /// ```
    #[allow(clippy::should_implement_trait)]
    #[inline]
    pub fn next(&mut self) -> Option<Result<RefRecord, Error>> {
        if self.finished || !self.initialized() && !try_opt!(self.init()) {
            return None;
        }

        if !self.buf_pos.is_new() {
            self.next_pos();
        }

        if !try_opt!(self.find()) && !try_opt!(self.next_complete()) {
            return None;
        }

        Some(Ok(RefRecord {
            buffer: self.get_buf(),
            buf_pos: &self.buf_pos,
        }))
    }

    #[inline(never)]
    fn init(&mut self) -> Result<bool, Error> {
        let n = fill_buf(&mut self.buf_reader)?;
        if n == 0 {
            self.finished = true;
            return Ok(false);
        }
        Ok(true)
    }

    fn initialized(&self) -> bool {
        !self.get_buf().is_empty()
    }

    /// Updates a [RecordSet](struct.RecordSet.html) with new data. The contents of the internal
    /// buffer are just copied over to the record set and the positions of all records are found.
    /// Old data will be erased. Returns `None` if the input reached its end.
    #[inline]
    pub fn read_record_set(&mut self, rset: &mut RecordSet) -> Option<Result<(), Error>> {
        if self.finished {
            return None;
        }

        if !self.initialized() {
            if !try_opt!(self.init()) {
                return None;
            }
            if !try_opt!(self.find()) {
                return Some(Ok(()));
            }
        } else if !try_opt!(self.next_complete()) {
            return None;
        };

        rset.buffer.clear();
        rset.buffer.extend(self.get_buf());

        rset.buf_positions.clear();
        rset.buf_positions.push(self.buf_pos.clone());

        loop {
            self.next_pos();
            if !try_opt!(self.find()) {
                return Some(Ok(()));
            }
            rset.buf_positions.push(self.buf_pos.clone());
        }
    }

    fn get_buf(&self) -> &[u8] {
        self.buf_reader.buffer()
    }

    // Sets starting points for next position
    fn next_pos(&mut self) {
        self.position.byte += (self.buf_pos.pos.1 + 1 - self.buf_pos.pos.0) as u64;
        self.position.line += 4;
        self.buf_pos.pos.0 = self.buf_pos.pos.1 + 1;
    }

    // Reads the current record and returns true if found.
    // Returns false if incomplete because end of buffer reached,
    // meaning that the last record may be incomplete.
    // Updates self.search_pos.
    fn find(&mut self) -> Result<bool, Error> {
        self.buf_pos.seq = unwrap_or!(self.find_line(self.buf_pos.pos.0), {
            self.search_pos = SearchPos::Head;
            return Ok(false);
        });

        self.buf_pos.sep = unwrap_or!(self.find_line(self.buf_pos.seq), {
            self.search_pos = SearchPos::Seq;
            return Ok(false);
        });

        self.buf_pos.qual = unwrap_or!(self.find_line(self.buf_pos.sep), {
            self.search_pos = SearchPos::Sep;
            return Ok(false);
        });

        self.buf_pos.pos.1 = unwrap_or!(self.find_line(self.buf_pos.qual), {
            self.search_pos = SearchPos::Qual;
            return Ok(false);
        }) - 1;

        self.validate()?;

        Ok(true)
    }

    // Resumes reading an incomplete record without
    // re-searching positions that were already found.
    // The resulting position may still be incomplete (-> false).
    fn find_incomplete(&mut self) -> Result<bool, Error> {
        if self.search_pos == SearchPos::Head {
            self.buf_pos.seq = unwrap_or!(self.find_line(self.buf_pos.pos.0), {
                self.search_pos = SearchPos::Head;
                return Ok(false);
            });
        }

        if self.search_pos <= SearchPos::Seq {
            self.buf_pos.sep = unwrap_or!(self.find_line(self.buf_pos.seq), {
                self.search_pos = SearchPos::Seq;
                return Ok(false);
            });
        }

        if self.search_pos <= SearchPos::Sep {
            self.buf_pos.qual = unwrap_or!(self.find_line(self.buf_pos.sep), {
                self.search_pos = SearchPos::Sep;
                return Ok(false);
            });
        }

        if self.search_pos <= SearchPos::Qual {
            self.buf_pos.pos.1 = unwrap_or!(self.find_line(self.buf_pos.qual), {
                self.search_pos = SearchPos::Qual;
                return Ok(false);
            }) - 1;
        }

        self.search_pos = SearchPos::Head;

        self.validate()?;

        Ok(true)
    }

    // should only be called on a complete BufferPosition
    #[inline(always)] // has performance impact and would not be inlined otherwise
    fn validate(&mut self) -> Result<(), Error> {
        let start_byte = self.get_buf()[self.buf_pos.pos.0];
        if start_byte != b'@' {
            self.finished = true;
            return Err(Error::InvalidStart {
                found: start_byte,
                pos: self.get_error_pos(0, false),
            });
        }

        let sep_byte = self.get_buf()[self.buf_pos.sep];
        if sep_byte != b'+' {
            self.finished = true;
            return Err(Error::InvalidSep {
                found: sep_byte,
                pos: self.get_error_pos(2, true),
            });
        }

        let qual_len = self.buf_pos.pos.1 - self.buf_pos.qual + 1;
        let seq_len = self.buf_pos.sep - self.buf_pos.seq;
        if seq_len != qual_len {
            self.finished = true;
            return Err(Error::UnequalLengths {
                seq: self.buf_pos.seq(self.get_buf()).len(),
                qual: self.buf_pos.qual(self.get_buf()).len(),
                pos: self.get_error_pos(0, true),
            });
        }
        Ok(())
    }

    #[inline(never)]
    fn get_error_pos(&self, offset: u64, parse_id: bool) -> ErrorPosition {
        let id = if parse_id && self.buf_pos.seq - self.buf_pos.pos.0 > 1 {
            let id = self
                .buf_pos
                .head(self.get_buf())
                .split(|b| *b == b' ')
                .next()
                .unwrap();
            Some(String::from_utf8_lossy(id).into())
        } else {
            None
        };
        ErrorPosition {
            line: self.position.line + offset,
            id,
        }
    }

    fn find_line(&self, search_start: usize) -> Option<usize> {
        memchr(b'\n', &self.get_buf()[search_start..]).map(|pos| search_start + pos + 1)
    }

    // To be called when the end of the buffer is reached and `next_pos` does not find
    // the next record. Incomplete bytes will be moved to the start of the buffer.
    // If the record still doesn't fit in, the buffer will be enlarged.
    // After calling this function, the position will therefore always be 'complete'.
    #[inline(never)]
    fn next_complete(&mut self) -> Result<bool, Error> {
        loop {
            if self.get_buf().len() < self.buf_reader.capacity() {
                // EOF reached, there will be no next record
                return self.check_end();
            } else if self.buf_pos.pos.0 == 0 {
                // first record already incomplete -> buffer too small
                self.grow()?;
            } else {
                // not the first record -> buffer may be big enough
                self.make_room();
            }

            fill_buf(&mut self.buf_reader)?;

            // self.buf_pos.pos.1 = 0;
            // self.search_pos = SearchPos::HEAD;
            if self.find_incomplete()? {
                return Ok(true);
            }
        }
    }

    #[inline(never)]
    fn check_end(&mut self) -> Result<bool, Error> {
        self.finished = true;
        if self.search_pos == SearchPos::Qual {
            // no line ending at end of last record
            self.buf_pos.pos.1 = self.get_buf().len();
            self.validate()?;
            return Ok(true);
        }

        let rest = &self.get_buf()[self.buf_pos.pos.0..];
        if rest.split(|c| *c == b'\n').all(|l| trim_cr(l).is_empty()) {
            // allow up to 3 newlines after last record (more will cause an Unexpected error)
            return Ok(false);
        }

        Err(Error::UnexpectedEnd {
            pos: self.get_error_pos(self.search_pos as u64, self.search_pos > SearchPos::Head),
        })
    }

    // grow buffer based on policy
    fn grow(&mut self) -> Result<(), Error> {
        let cap = self.buf_reader.capacity();
        let new_size = self.buf_policy.grow_to(cap).ok_or(Error::BufferLimit)?;
        let additional = new_size - cap;
        self.buf_reader.reserve(additional);
        Ok(())
    }

    // move incomplete bytes to start of buffer and retry
    fn make_room(&mut self) {
        let consumed = self.buf_pos.pos.0;
        self.buf_reader.consume(consumed);
        self.buf_reader.make_room();

        self.buf_pos.pos.0 = 0;

        if self.search_pos >= SearchPos::Seq {
            self.buf_pos.seq -= consumed;
        }
        if self.search_pos >= SearchPos::Sep {
            self.buf_pos.sep -= consumed;
        }
        if self.search_pos >= SearchPos::Qual {
            self.buf_pos.qual -= consumed;
        }
    }

    /// Returns the current position (useful with `seek()`)
    ///
    /// # Example
    ///
    /// ```
    /// # fn main() {
    /// use seq_io::fastq::{Reader, Position};
    ///
    /// let fastq = b"@id1
    /// ACGT
    /// +
    /// IIII
    /// @id2
    /// TGCA
    /// +
    /// IIII";
    ///
    /// let mut reader = Reader::new(&fastq[..]);
    ///
    /// // skip one record
    /// reader.next().unwrap();
    /// // second position
    /// reader.next().unwrap();
    ///
    /// assert_eq!(reader.position(), &Position::new(5, 17));
    /// # }
    /// ```
    #[inline]
    pub fn position(&self) -> &Position {
        &self.position
    }

    /// Returns a borrowed iterator over all FASTQ records. The records
    /// are owned (`OwnedRecord`), this is therefore slower than using
    /// `Reader::next()`.
    ///
    /// # Example
    ///
    /// ```
    /// # fn main() {
    /// use seq_io::fastq::{Reader, OwnedRecord};
    ///
    /// let fastq = b"@id1
    /// ACGT
    /// +
    /// IIII
    /// @id2
    /// TGCA
    /// +
    /// IIII";
    ///
    /// let mut reader = Reader::new(&fastq[..]);
    ///
    /// let records: Result<Vec<_>, _> = reader
    ///     .records()
    ///     .collect();
    ///
    /// assert_eq!(records.unwrap(),
    ///     vec![
    ///         OwnedRecord {head: b"id1".to_vec(), seq: b"ACGT".to_vec(), qual: b"IIII".to_vec()},
    ///         OwnedRecord {head: b"id2".to_vec(), seq: b"TGCA".to_vec(), qual: b"IIII".to_vec()}
    ///     ]
    /// );
    /// # }
    /// ```
    #[inline]
    pub fn records(&mut self) -> RecordsIter<R, P> {
        RecordsIter { rdr: self }
    }

    /// Returns an iterator over all FASTQ records like `Reader::records()`,
    /// but with the difference that it owns the underlying reader.
    #[inline]
    pub fn into_records(self) -> RecordsIntoIter<R, P> {
        RecordsIntoIter { rdr: self }
    }
}

impl<R, P> Reader<R, P>
where
    R: io::Read + Seek,
    P: BufPolicy,
{
    /// Seeks to a specified position.
    /// Keep the underyling buffer if the seek position is found within it, otherwise it has to be
    /// discarded.
    /// If an error was returned before, seeking to that position will return the same error.
    /// The same is not always true with `None`. If there is no newline character at the end of the
    /// file, the last record will be returned instead of `None`.
    ///
    /// # Example
    ///
    /// ```
    /// # fn main() {
    /// use seq_io::fastq::{Reader, Position, OwnedRecord};
    /// use std::io::Cursor;
    ///
    /// let fastq = b"@id1
    /// ACGT
    /// +
    /// IIII
    /// @id2
    /// TGCA
    /// +
    /// IIII";
    ///
    /// let mut cursor = Cursor::new(&fastq[..]);
    /// let mut reader = Reader::new(cursor);
    ///
    /// // read the first record and get its position
    /// let record1 = reader.next().unwrap().unwrap().to_owned_record();
    /// let pos1 = reader.position().to_owned();
    ///
    /// // read the second record
    /// reader.next().unwrap().unwrap();
    ///
    /// // now seek to position of first record
    /// reader.seek(&pos1);
    /// assert_eq!(reader.next().unwrap().unwrap().to_owned_record(), record1);
    /// # }
    /// ```
    #[inline]
    pub fn seek(&mut self, pos: &Position) -> Result<(), Error> {
        self.finished = false;
        let diff = pos.byte as i64 - self.position.byte as i64;
        let endpos = self.buf_pos.pos.0 as i64 + diff;
        self.position = pos.clone();

        if endpos >= 0 && endpos < (self.get_buf().len() as i64) {
            // position reachable within buffer -> no actual seeking necessary
            self.buf_pos.reset(endpos as usize); // is_new() will return true
            return Ok(());
        }

        self.buf_reader.seek(io::SeekFrom::Start(pos.byte))?;
        self.buf_pos.reset(0);
        Ok(())
    }
}

/// Borrowed iterator of `OwnedRecord`
pub struct RecordsIter<'a, R, P = DefaultBufPolicy>
where
    P: 'a,
    R: io::Read + 'a,
{
    rdr: &'a mut Reader<R, P>,
}

impl<'a, R, P> Iterator for RecordsIter<'a, R, P>
where
    P: BufPolicy + 'a,
    R: io::Read + 'a,
{
    type Item = Result<OwnedRecord, Error>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.rdr.next().map(|rec| rec.map(|r| r.to_owned_record()))
    }
}

/// Iterator of `OwnedRecord` that owns the underlying reader
pub struct RecordsIntoIter<R: io::Read, P = DefaultBufPolicy> {
    rdr: Reader<R, P>,
}

impl<R, P> Iterator for RecordsIntoIter<R, P>
where
    P: BufPolicy,
    R: io::Read,
{
    type Item = Result<OwnedRecord, Error>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.rdr.next().map(|rec| rec.map(|r| r.to_owned_record()))
    }
}

/// Holds line number and byte offset of a FASTQ record
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Position {
    line: u64,
    byte: u64,
}

impl Position {
    #[inline]
    pub fn new(line: u64, byte: u64) -> Position {
        Position { line, byte }
    }

    /// Line number (starting with 1)
    #[inline]
    pub fn line(&self) -> u64 {
        self.line
    }

    /// Byte offset within the file
    #[inline]
    pub fn byte(&self) -> u64 {
        self.byte
    }
}

/// FASTQ parsing error
#[derive(Debug)]
pub enum Error {
    Io(io::Error),
    /// sequence and qualitiy lengths are not equal
    UnequalLengths {
        /// Length of sequence
        seq: usize,
        /// Length of qualities
        qual: usize,
        /// Position within file.
        /// `ErrorPosition::line` has the position of the header, not sequence/qualities
        pos: ErrorPosition,
    },
    /// Invalid start byte encountered (expected `@`)
    InvalidStart {
        /// Byte found instead.
        found: u8,
        /// Position within file. `ErrorPosition::id` will be `None`.
        pos: ErrorPosition,
    },
    /// Invalid separator byte encountered (expected `+`)
    InvalidSep {
        /// Byte found instead.
        found: u8,
        /// Position within file
        pos: ErrorPosition,
    },
    /// Truncated record found
    UnexpectedEnd {
        /// Position within file.
        pos: ErrorPosition,
    },
    /// Size limit of buffer was reached, which happens if `policy::BufPolicy::grow_to()` returned
    /// `None`. This does not happen with the default `struct.DoubleUntil.html` policy.
    BufferLimit,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ErrorPosition {
    /// Line number where the error occurred (starting with 1)
    pub line: u64,
    /// ID of record if available
    pub id: Option<String>,
}

impl fmt::Display for ErrorPosition {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(id) = self.id.as_ref() {
            write!(f, "record '{}' at ", id)?;
        }
        write!(f, "line {}", self.line)
    }
}

impl fmt::Display for Error {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Io(ref e) => e.fmt(f),
            Error::UnequalLengths { seq, qual, ref pos } => write!(
                f,
                "FASTQ parse error: sequence length is {}, but quality length is {} ({}).",
                seq, qual, pos
            ),
            Error::InvalidStart { found, ref pos } => write!(
                f,
                "FASTQ parse error: expected '@' at record start but found '{}' ({}).",
                (found as char).escape_default(),
                pos
            ),
            Error::InvalidSep { found, ref pos } => write!(
                f,
                "FASTQ parse error: Expected '+' separator but found '{}' ({}).",
                (found as char).escape_default(),
                pos
            ),
            Error::UnexpectedEnd { ref pos } => {
                write!(f, "FASTQ parse error: unexpected end of input ({}).", pos)
            }
            Error::BufferLimit => write!(f, "FASTQ parse error: Buffer limit reached."),
        }
    }
}

impl From<io::Error> for Error {
    #[inline]
    fn from(e: io::Error) -> Error {
        Error::Io(e)
    }
}

impl StdError for Error {
    #[inline]
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match *self {
            Error::Io(ref err) => Some(err),
            _ => None,
        }
    }
}

/// Represents the position of a record within a buffer
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct BufferPosition {
    // (start, stop), but might include \r at the end
    pos: (usize, usize),
    seq: usize,
    sep: usize,
    qual: usize,
}

impl BufferPosition {
    #[inline]
    fn is_new(&self) -> bool {
        self.pos.1 == 0
    }

    #[inline]
    fn reset(&mut self, start: usize) {
        self.pos.0 = start;
        self.pos.1 = 0;
    }

    #[inline]
    fn head<'a>(&'a self, buffer: &'a [u8]) -> &'a [u8] {
        trim_cr(&buffer[self.pos.0 + 1..self.seq - 1])
    }

    #[inline]
    fn seq<'a>(&'a self, buffer: &'a [u8]) -> &'a [u8] {
        trim_cr(&buffer[self.seq..self.sep - 1])
    }

    #[inline]
    fn qual<'a>(&'a self, buffer: &'a [u8]) -> &'a [u8] {
        trim_cr(&buffer[self.qual..self.pos.1])
    }
}

/// FASTQ record trait implemented by both `RefRecord` and `OwnedRecord`
pub trait Record {
    /// Return the header line of the record as byte slice
    fn head(&self) -> &[u8];
    /// Return the FASTQ sequence as byte slice
    fn seq(&self) -> &[u8];
    /// Return the FASTQ qualities as byte slice
    fn qual(&self) -> &[u8];

    #[inline]
    fn id_bytes(&self) -> &[u8] {
        self.head().split(|b| *b == b' ').next().unwrap()
    }

    /// Return the ID of the record (everything before an optional space) as string slice
    #[inline]
    fn id(&self) -> Result<&str, Utf8Error> {
        str::from_utf8(self.id_bytes())
    }

    #[inline]
    fn desc_bytes(&self) -> Option<&[u8]> {
        self.head().splitn(2, |b| *b == b' ').nth(1)
    }

    /// Return the description of the record as string slice, if present. Otherwise, `None` is returned.
    #[inline]
    fn desc(&self) -> Option<Result<&str, Utf8Error>> {
        self.desc_bytes().map(str::from_utf8)
    }

    /// Return both the ID and the description of the record (if present)
    /// This should be faster than calling `id()` and `desc()` separately.
    #[inline]
    fn id_desc_bytes(&self) -> (&[u8], Option<&[u8]>) {
        let mut h = self.head().splitn(2, |c| *c == b' ');
        (h.next().unwrap(), h.next())
    }

    /// Return both the ID and the description of the record (if present)
    /// This should be faster than calling `id()` and `desc()` separately.
    #[inline]
    fn id_desc(&self) -> Result<(&str, Option<&str>), Utf8Error> {
        let mut h = str::from_utf8(self.head())?.splitn(2, ' ');
        Ok((h.next().unwrap(), h.next()))
    }

    /// Writes a record to the given `io::Write` instance
    #[inline]
    fn write<W: io::Write>(&self, writer: W) -> io::Result<()> {
        write_to(writer, self.head(), self.seq(), self.qual())
    }
}

/// A FASTQ record that borrows data from a buffer
#[derive(Debug, Clone)]
pub struct RefRecord<'a> {
    buffer: &'a [u8],
    buf_pos: &'a BufferPosition,
}

impl<'a> Record for RefRecord<'a> {
    #[inline]
    fn head(&self) -> &[u8] {
        self.buf_pos.head(self.buffer)
    }

    #[inline]
    fn seq(&self) -> &[u8] {
        self.buf_pos.seq(self.buffer)
    }

    #[inline]
    fn qual(&self) -> &[u8] {
        self.buf_pos.qual(self.buffer)
    }
}

impl<'a> RefRecord<'a> {
    #[inline]
    pub fn to_owned_record(&self) -> OwnedRecord {
        OwnedRecord {
            head: self.head().to_vec(),
            seq: self.seq().to_vec(),
            qual: self.qual().to_vec(),
        }
    }

    /// Writes a record to the given `io::Write` instance
    /// by just writing the unmodified input, which is faster than `RefRecord::write`
    #[inline]
    pub fn write_unchanged<W: io::Write>(&self, mut writer: W) -> io::Result<()> {
        let data = &self.buffer[self.buf_pos.pos.0..self.buf_pos.pos.1];
        writer.write_all(data)?;
        writer.write_all(b"\n")
    }
}

/// A FASTQ record that ownes its data (requires allocations)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OwnedRecord {
    pub head: Vec<u8>,
    pub seq: Vec<u8>,
    pub qual: Vec<u8>,
}

impl Record for OwnedRecord {
    #[inline]
    fn head(&self) -> &[u8] {
        &self.head
    }
    #[inline]
    fn seq(&self) -> &[u8] {
        &self.seq
    }
    #[inline]
    fn qual(&self) -> &[u8] {
        &self.qual
    }
}

/// Set of FASTQ records that owns it's buffer
/// and knows the positions of each record.
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct RecordSet {
    buffer: Vec<u8>,
    buf_positions: Vec<BufferPosition>,
}

impl<'a> iter::IntoIterator for &'a RecordSet {
    type Item = RefRecord<'a>;
    type IntoIter = RecordSetIter<'a>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        RecordSetIter {
            buffer: &self.buffer,
            pos: self.buf_positions.iter(),
        }
    }
}

/// Iterator over record sets
pub struct RecordSetIter<'a> {
    buffer: &'a [u8],
    pos: slice::Iter<'a, BufferPosition>,
}

impl<'a> Iterator for RecordSetIter<'a> {
    type Item = RefRecord<'a>;

    #[inline]
    fn next(&mut self) -> Option<RefRecord<'a>> {
        self.pos.next().map(|p| RefRecord {
            buffer: self.buffer,
            buf_pos: p,
        })
    }
}

/// Helper function for writing data (not necessarily stored in a `Record` instance)
/// to the FASTQ format
#[inline]
pub fn write_to<W: io::Write>(
    mut writer: W,
    head: &[u8],
    seq: &[u8],
    qual: &[u8],
) -> io::Result<()> {
    writer.write_all(b"@")?;
    writer.write_all(head)?;
    writer.write_all(b"\n")?;
    writer.write_all(seq)?;
    writer.write_all(b"\n+\n")?;
    writer.write_all(qual)?;
    writer.write_all(b"\n")?;
    Ok(())
}

/// Helper function for writing data (not necessarily stored in a `Record` instance)
/// to the FASTQ format. The ID and description parts of the header are supplied separately
/// instead of a whole header line
#[inline]
pub fn write_parts<W: io::Write>(
    mut writer: W,
    id: &[u8],
    desc: Option<&[u8]>,
    seq: &[u8],
    qual: &[u8],
) -> io::Result<()> {
    writer.write_all(b"@")?;
    writer.write_all(id)?;
    if let Some(d) = desc {
        writer.write_all(b" ")?;
        writer.write_all(d)?;
    }
    writer.write_all(b"\n")?;
    writer.write_all(seq)?;
    writer.write_all(b"\n+\n")?;
    writer.write_all(qual)?;
    writer.write_all(b"\n")?;
    Ok(())
}