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
//! Indexed FASTA reader.

mod builder;

pub use self::builder::Builder;

use std::io::{self, BufRead, Seek};

use noodles_core::Region;

use super::Reader;
use crate::{fai, Record};

/// An indexed FASTA reader.
pub struct IndexedReader<R> {
    inner: Reader<R>,
    index: fai::Index,
}

impl<R> IndexedReader<R>
where
    R: BufRead,
{
    /// Creates a new indexed FASTA reader.
    pub fn new(inner: R, index: fai::Index) -> Self {
        Self {
            inner: Reader::new(inner),
            index,
        }
    }

    /// Returns a reference to the underlying reader.
    pub fn get_ref(&self) -> &R {
        self.inner.get_ref()
    }

    /// Returns a mutable reference to the underlying reader.
    pub fn get_mut(&mut self) -> &mut R {
        self.inner.get_mut()
    }

    /// Returns the underlying reader.
    pub fn into_inner(self) -> R {
        self.inner.into_inner()
    }

    /// Reads a raw definition line.
    pub fn read_definition(&mut self, buf: &mut String) -> io::Result<usize> {
        self.inner.read_definition(buf)
    }

    /// Reads a sequence.
    pub fn read_sequence(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
        self.inner.read_sequence(buf)
    }

    /// Returns the associated index.
    pub fn index(&self) -> &fai::Index {
        &self.index
    }
}

impl<R> IndexedReader<R>
where
    R: BufRead + Seek,
{
    /// Returns a record of the given region.
    pub fn query(&mut self, region: &Region) -> io::Result<Record> {
        self.inner.query(&self.index, region)
    }
}