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
//! FASTA reader, which requires the sequence to be on a single line, it should
//! *not* to be wrapped to multiple lines. This is a rather unusually strict
//! definition of the format and may not be of general use. This parser was
//! mostly created for exploring performance optimizations.

use crate::core::{LinePositionIterKind, PositionStore, SearchPos};
use crate::LinePositionIter;
use serde::{Deserialize, Serialize};
use std::str;

impl_fasta_reader!(false, RangeStore, ("fasta::single_line", "fasta"));

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RangeStore {
    // All line positions of a record are stored in an array
    // This way, `move_to_start()` is easier to implement
    // than with a struct.
    pos: [usize; 3],
}

impl PositionStore for RangeStore {
    type SeqLinesType = LinePositionIterKind;
    type QualLinesType = LinePositionIterKind;

    #[inline]
    fn move_to_start(&mut self, search_pos: SearchPos, offset: usize) {
        for i in 0..search_pos as usize + 1 {
            self.pos[i] -= offset;
        }
    }

    #[inline]
    fn record_start(&self) -> usize {
        self.pos[0]
    }

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

    #[inline]
    fn sep_pos(&self) -> usize {
        self.record_end()
    }

    #[inline]
    fn qual_start(&self) -> usize {
        self.record_end()
    }

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

    #[inline]
    fn add_seq_line_start(&mut self, _: usize) {}

    #[inline]
    fn seq_start(&self) -> usize {
        self.pos[1]
    }

    #[inline]
    fn seq_starts(&self) -> &[usize] {
        &self.pos[1..2]
    }

    #[inline]
    fn set_record_end(&mut self, pos: usize, _: bool) {
        self.pos[2] = pos;
    }

    #[inline]
    fn record_end(&self) -> usize {
        self.pos[2]
    }

    // TODO: does not work with normal FASTA (line numbers not correctly incremented)
    #[inline]
    fn num_lines(&self) -> usize {
        2
    }

    #[inline]
    fn line_offset(&self, search_pos: SearchPos, has_line: bool) -> usize {
        search_pos as usize - !has_line as usize
    }

    #[inline]
    fn seq_lines<'s>(&'s self, buffer: &'s [u8]) -> LinePositionIter<'s> {
        // TODO: does not work with normal FASTA (lines not parsed)
        LinePositionIter::new(buffer, &self.pos[1..=2])
    }

    #[inline]
    fn qual_lines<'a>(&'a self, buffer: &'a [u8]) -> LinePositionIter<'a> {
        LinePositionIter::new(buffer, &[])
    }

    #[inline]
    fn num_seq_lines(&self) -> usize {
        1
    }
}