noodles_cram/io/
indexed_reader.rs

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

mod builder;

pub use self::builder::Builder;

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

use noodles_core::Region;
use noodles_fasta as fasta;
use noodles_sam as sam;

use super::{
    reader::{Query, Records},
    Reader,
};
use crate::{crai, DataContainer, FileDefinition};

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

impl<R> IndexedReader<R>
where
    R: Read,
{
    /// Creates an indexed CRAM reader.
    pub fn new(inner: R, index: crai::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()
    }

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

    /// Returns the reference sequence repository.
    pub fn reference_sequence_repository(&self) -> &fasta::Repository {
        self.inner.reference_sequence_repository()
    }

    /// Reads the CRAM file definition.
    pub fn read_file_definition(&mut self) -> io::Result<FileDefinition> {
        self.inner.read_file_definition()
    }

    /// Reads the SAM header.
    pub fn read_file_header(&mut self) -> io::Result<sam::Header> {
        self.inner.read_file_header()
    }

    /// Reads the SAM header.
    pub fn read_header(&mut self) -> io::Result<sam::Header> {
        self.inner.read_header()
    }

    /// Reads a data container.
    pub fn read_data_container(&mut self) -> io::Result<Option<DataContainer>> {
        self.inner.read_data_container()
    }

    /// Returns a iterator over records starting from the current stream position.
    pub fn records<'r>(&'r mut self, header: &'r sam::Header) -> Records<'r, R> {
        self.inner.records(header)
    }

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

impl<R> IndexedReader<R>
where
    R: Read + Seek,
{
    /// Returns an iterator over records that intersects the given region.
    pub fn query<'a>(
        &'a mut self,
        header: &'a sam::Header,
        region: &Region,
    ) -> io::Result<Query<'a, R>> {
        self.inner.query(header, &self.index, region)
    }
}