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
//! Binning index builder.

use super::{reference_sequence, Header, Index, ReferenceSequence};

/// A binning index builder.
pub struct Builder<I> {
    min_shift: u8,
    depth: u8,
    header: Option<Header>,
    reference_sequences: Vec<ReferenceSequence<I>>,
    unplaced_unmapped_record_count: Option<u64>,
}

impl<I> Builder<I>
where
    I: reference_sequence::Index,
{
    /// Sets a min shift.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_csi::{self as csi, BinningIndex};
    /// let index = csi::Index::builder().set_min_shift(13).build();
    /// assert_eq!(index.min_shift(), 13);
    /// ```
    pub fn set_min_shift(mut self, min_shift: u8) -> Self {
        self.min_shift = min_shift;
        self
    }

    /// Sets a max depth.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_csi::{self as csi, BinningIndex};
    /// let index = csi::Index::builder().set_depth(8).build();
    /// assert_eq!(index.depth(), 8);
    /// ```
    pub fn set_depth(mut self, depth: u8) -> Self {
        self.depth = depth;
        self
    }

    /// Sets a tabix header.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_csi::{self as csi, binning_index::index::Header, BinningIndex};
    /// let header = Header::default();
    /// let index = csi::Index::builder().set_header(header.clone()).build();
    /// assert_eq!(index.header(), Some(&header));
    /// ```
    pub fn set_header(mut self, header: Header) -> Self {
        self.header = Some(header);
        self
    }

    /// Sets reference sequences.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_csi::{self as csi, binning_index::index::ReferenceSequence};
    ///
    /// let reference_sequences = vec![ReferenceSequence::new(Default::default(), Default::default(), None)];
    /// let index = csi::Index::builder()
    ///     .set_reference_sequences(reference_sequences.clone())
    ///     .build();
    ///
    /// assert_eq!(index.reference_sequences(), &reference_sequences);
    /// ```
    pub fn set_reference_sequences(
        mut self,
        reference_sequences: Vec<ReferenceSequence<I>>,
    ) -> Self {
        self.reference_sequences = reference_sequences;
        self
    }

    /// Sets an unplaced, unmapped record count.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_csi::{self as csi, BinningIndex};
    /// let index = csi::Index::builder().set_unplaced_unmapped_record_count(21).build();
    /// assert_eq!(index.unplaced_unmapped_record_count(), Some(21));
    /// ```
    pub fn set_unplaced_unmapped_record_count(
        mut self,
        unplaced_unmapped_record_count: u64,
    ) -> Self {
        self.unplaced_unmapped_record_count = Some(unplaced_unmapped_record_count);
        self
    }

    /// Builds a binning index.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_csi as csi;
    /// let index = csi::Index::builder().build();
    /// ```
    pub fn build(self) -> Index<I> {
        Index {
            min_shift: self.min_shift,
            depth: self.depth,
            header: self.header,
            reference_sequences: self.reference_sequences,
            unplaced_unmapped_record_count: self.unplaced_unmapped_record_count,
        }
    }
}

impl<I> Default for Builder<I> {
    fn default() -> Self {
        Self {
            min_shift: 14,
            depth: 5,
            header: None,
            reference_sequences: Vec::new(),
            unplaced_unmapped_record_count: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::binning_index::index::reference_sequence::index::BinnedIndex;

    #[test]
    fn test_default() {
        let builder = Builder::<BinnedIndex>::default();

        assert_eq!(builder.min_shift, 14);
        assert_eq!(builder.depth, 5);
        assert!(builder.header.is_none());
        assert!(builder.reference_sequences.is_empty());
        assert!(builder.unplaced_unmapped_record_count.is_none());
    }
}