noodles_csi/binning_index/index/reference_sequence/metadata.rs
1use noodles_bgzf as bgzf;
2
3use super::bin::Chunk;
4
5/// Index reference sequence metadata.
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct Metadata {
8 start_position: bgzf::VirtualPosition,
9 end_position: bgzf::VirtualPosition,
10 mapped_record_count: u64,
11 unmapped_record_count: u64,
12}
13
14impl Metadata {
15 /// Creates reference sequence metadata.
16 ///
17 /// # Examples
18 ///
19 /// ```
20 /// use noodles_bgzf as bgzf;
21 /// use noodles_csi::binning_index::index::reference_sequence::Metadata;
22 ///
23 /// let metadata = Metadata::new(
24 /// bgzf::VirtualPosition::from(610),
25 /// bgzf::VirtualPosition::from(1597),
26 /// 55,
27 /// 0,
28 /// );
29 /// ```
30 pub fn new(
31 start_position: bgzf::VirtualPosition,
32 end_position: bgzf::VirtualPosition,
33 mapped_record_count: u64,
34 unmapped_record_count: u64,
35 ) -> Self {
36 Self {
37 start_position,
38 end_position,
39 mapped_record_count,
40 unmapped_record_count,
41 }
42 }
43
44 /// Returns the start virtual position.
45 ///
46 /// # Examples
47 ///
48 /// ```
49 /// use noodles_bgzf as bgzf;
50 /// use noodles_csi::binning_index::index::reference_sequence::Metadata;
51 ///
52 /// let metadata = Metadata::new(
53 /// bgzf::VirtualPosition::from(610),
54 /// bgzf::VirtualPosition::from(1597),
55 /// 55,
56 /// 0,
57 /// );
58 ///
59 /// assert_eq!(metadata.start_position(), bgzf::VirtualPosition::from(610));
60 /// ```
61 pub fn start_position(&self) -> bgzf::VirtualPosition {
62 self.start_position
63 }
64
65 /// Returns the end virtual position.
66 ///
67 /// # Examples
68 ///
69 /// ```
70 /// use noodles_bgzf as bgzf;
71 /// use noodles_csi::binning_index::index::reference_sequence::Metadata;
72 ///
73 /// let metadata = Metadata::new(
74 /// bgzf::VirtualPosition::from(610),
75 /// bgzf::VirtualPosition::from(1597),
76 /// 55,
77 /// 0,
78 /// );
79 ///
80 /// assert_eq!(metadata.end_position(), bgzf::VirtualPosition::from(1597));
81 /// ```
82 pub fn end_position(&self) -> bgzf::VirtualPosition {
83 self.end_position
84 }
85
86 /// Returns the number of mapped records.
87 ///
88 /// # Examples
89 ///
90 /// ```
91 /// use noodles_bgzf as bgzf;
92 /// use noodles_csi::binning_index::index::reference_sequence::Metadata;
93 ///
94 /// let metadata = Metadata::new(
95 /// bgzf::VirtualPosition::from(610),
96 /// bgzf::VirtualPosition::from(1597),
97 /// 55,
98 /// 0,
99 /// );
100 ///
101 /// assert_eq!(metadata.mapped_record_count(), 55);
102 /// ```
103 pub fn mapped_record_count(&self) -> u64 {
104 self.mapped_record_count
105 }
106
107 /// Returns the number of unmapped records.
108 ///
109 /// # Examples
110 ///
111 /// ```
112 /// use noodles_bgzf as bgzf;
113 /// use noodles_csi::binning_index::index::reference_sequence::Metadata;
114 ///
115 /// let metadata = Metadata::new(
116 /// bgzf::VirtualPosition::from(610),
117 /// bgzf::VirtualPosition::from(1597),
118 /// 55,
119 /// 0,
120 /// );
121 ///
122 /// assert_eq!(metadata.unmapped_record_count(), 0);
123 /// ```
124 pub fn unmapped_record_count(&self) -> u64 {
125 self.unmapped_record_count
126 }
127
128 pub(super) fn update(&mut self, is_mapped: bool, chunk: Chunk) {
129 if is_mapped {
130 self.mapped_record_count += 1;
131 } else {
132 self.unmapped_record_count += 1;
133 }
134
135 self.start_position = self.start_position.min(chunk.start());
136 self.end_position = self.end_position.max(chunk.end());
137 }
138}