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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
//! Tabix index header builder.
use super::{format::CoordinateSystem, Format, Header, ReferenceSequenceNames};
/// A tabix index header builder.
pub struct Builder {
format: Format,
reference_sequence_name_index: usize,
start_position_index: usize,
end_position_index: Option<usize>,
line_comment_prefix: u8,
line_skip_count: u32,
reference_sequence_names: ReferenceSequenceNames,
}
impl Builder {
/// Creates a builder that targets the BED format.
///
/// # Examples
///
/// ```
/// use noodles_csi::binning_index::index::header::Builder;
/// let builder = Builder::bed();
/// ```
pub fn bed() -> Self {
Self {
format: Format::Generic(CoordinateSystem::Bed),
reference_sequence_name_index: 0,
start_position_index: 1,
end_position_index: Some(2),
line_comment_prefix: b'#',
line_skip_count: 0,
reference_sequence_names: ReferenceSequenceNames::new(),
}
}
/// Creates a builder that targets the GFF format.
///
/// # Examples
///
/// ```
/// use noodles_csi::binning_index::index::header::Builder;
/// let builder = Builder::gff();
/// ```
pub fn gff() -> Self {
Self {
format: Format::Generic(CoordinateSystem::Gff),
reference_sequence_name_index: 0,
start_position_index: 3,
end_position_index: Some(4),
line_comment_prefix: b'#',
line_skip_count: 0,
reference_sequence_names: ReferenceSequenceNames::new(),
}
}
/// Creates a builder that targets the SAM format.
///
/// # Examples
///
/// ```
/// use noodles_csi::binning_index::index::header::Builder;
/// let builder = Builder::sam();
/// ```
pub fn sam() -> Self {
Self {
format: Format::Sam,
reference_sequence_name_index: 2,
start_position_index: 3,
end_position_index: None,
line_comment_prefix: b'@',
line_skip_count: 0,
reference_sequence_names: ReferenceSequenceNames::new(),
}
}
/// Creates a builder that targets the VCF format.
///
/// # Examples
///
/// ```
/// use noodles_csi::binning_index::index::header::Builder;
/// let builder = Builder::vcf();
/// ```
pub fn vcf() -> Self {
Self {
format: Format::Vcf,
reference_sequence_name_index: 0,
start_position_index: 1,
end_position_index: None,
line_comment_prefix: b'#',
line_skip_count: 0,
reference_sequence_names: ReferenceSequenceNames::new(),
}
}
/// Sets a format.
///
/// # Examples
///
/// ```
/// use noodles_csi::binning_index::index::{header::Format, Header};
/// let header = Header::builder().set_format(Format::Vcf).build();
/// assert_eq!(header.format(), Format::Vcf);
/// ```
pub fn set_format(mut self, format: Format) -> Self {
self.format = format;
self
}
/// Sets a reference sequence name index.
///
/// # Examples
///
/// ```
/// use noodles_csi::binning_index::index::Header;
/// let header = Header::builder().set_reference_sequence_name_index(0).build();
/// assert_eq!(header.reference_sequence_name_index(), 0);
/// ```
pub fn set_reference_sequence_name_index(
mut self,
reference_sequence_name_index: usize,
) -> Self {
self.reference_sequence_name_index = reference_sequence_name_index;
self
}
/// Sets a start position index.
///
/// # Examples
///
/// ```
/// use noodles_csi::binning_index::index::Header;
/// let header = Header::builder().set_start_position_index(3).build();
/// assert_eq!(header.start_position_index(), 3);
/// ```
pub fn set_start_position_index(mut self, start_position_index: usize) -> Self {
self.start_position_index = start_position_index;
self
}
/// Sets an end position index.
///
/// # Examples
///
/// ```
/// use noodles_csi::binning_index::index::Header;
/// let header = Header::builder().set_end_position_index(Some(4)).build();
/// assert_eq!(header.end_position_index(), Some(4));
/// ```
pub fn set_end_position_index(mut self, end_position_index: Option<usize>) -> Self {
self.end_position_index = end_position_index;
self
}
/// Sets a line comment prefix.
///
/// # Examples
///
/// ```
/// use noodles_csi::binning_index::index::Header;
/// let header = Header::builder().set_line_comment_prefix(b'#').build();
/// assert_eq!(header.line_comment_prefix(), b'#');
/// ```
pub fn set_line_comment_prefix(mut self, line_comment_prefix: u8) -> Self {
self.line_comment_prefix = line_comment_prefix;
self
}
/// Sets a line skip count.
///
/// # Examples
///
/// ```
/// use noodles_csi::binning_index::index::Header;
/// let header = Header::builder().set_line_skip_count(0).build();
/// assert_eq!(header.line_skip_count(), 0);
/// ```
pub fn set_line_skip_count(mut self, line_skip_count: u32) -> Self {
self.line_skip_count = line_skip_count;
self
}
/// Sets reference sequence names.
///
/// # Examples
///
/// ```
/// use noodles_csi::binning_index::index::{header::ReferenceSequenceNames, Header};
///
/// let reference_sequence_names = ReferenceSequenceNames::new();
///
/// let header = Header::builder()
/// .set_reference_sequence_names(reference_sequence_names.clone())
/// .build();
///
/// assert_eq!(header.reference_sequence_names(), &reference_sequence_names);
/// ```
pub fn set_reference_sequence_names(
mut self,
reference_sequence_names: ReferenceSequenceNames,
) -> Self {
self.reference_sequence_names = reference_sequence_names;
self
}
/// Builds a tabix index header.
///
/// # Examples
///
/// ```
/// use noodles_csi::binning_index::index::Header;
/// let index = Header::builder().build();
/// ```
pub fn build(self) -> Header {
Header {
format: self.format,
reference_sequence_name_index: self.reference_sequence_name_index,
start_position_index: self.start_position_index,
end_position_index: self.end_position_index,
line_comment_prefix: self.line_comment_prefix,
line_skip_count: self.line_skip_count,
reference_sequence_names: self.reference_sequence_names,
}
}
}
impl Default for Builder {
fn default() -> Self {
Self::gff()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bed() {
let builder = Builder::bed();
assert_eq!(builder.format, Format::Generic(CoordinateSystem::Bed));
assert_eq!(builder.reference_sequence_name_index, 0);
assert_eq!(builder.start_position_index, 1);
assert_eq!(builder.end_position_index, Some(2));
assert_eq!(builder.line_comment_prefix, b'#');
assert_eq!(builder.line_skip_count, 0);
assert!(builder.reference_sequence_names.is_empty());
}
#[test]
fn test_gff() {
let builder = Builder::gff();
assert_eq!(builder.format, Format::Generic(CoordinateSystem::Gff));
assert_eq!(builder.reference_sequence_name_index, 0);
assert_eq!(builder.start_position_index, 3);
assert_eq!(builder.end_position_index, Some(4));
assert_eq!(builder.line_comment_prefix, b'#');
assert_eq!(builder.line_skip_count, 0);
assert!(builder.reference_sequence_names.is_empty());
}
#[test]
fn test_sam() {
let builder = Builder::sam();
assert_eq!(builder.format, Format::Sam);
assert_eq!(builder.reference_sequence_name_index, 2);
assert_eq!(builder.start_position_index, 3);
assert_eq!(builder.end_position_index, None);
assert_eq!(builder.line_comment_prefix, b'@');
assert_eq!(builder.line_skip_count, 0);
assert!(builder.reference_sequence_names.is_empty());
}
#[test]
fn test_vcf() {
let builder = Builder::vcf();
assert_eq!(builder.format, Format::Vcf);
assert_eq!(builder.reference_sequence_name_index, 0);
assert_eq!(builder.start_position_index, 1);
assert_eq!(builder.end_position_index, None);
assert_eq!(builder.line_comment_prefix, b'#');
assert_eq!(builder.line_skip_count, 0);
assert!(builder.reference_sequence_names.is_empty());
}
}