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
use noodles_core::Position;
use super::{Attributes, Phase, Record, Strand, MISSING_FIELD};
/// A GFF record builder.
#[derive(Debug)]
pub struct Builder {
reference_sequence_name: String,
source: String,
ty: String,
start: Position,
end: Position,
score: Option<f32>,
strand: Strand,
phase: Option<Phase>,
attributes: Attributes,
}
impl Builder {
/// Creates a GFF record builder.
///
/// Typically, [`Record::builder`] is used instead of calling this.
///
/// # Examples
///
/// ```
/// use noodles_gff as gff;
/// let builder = gff::Record::builder();
/// ```
pub fn new() -> Self {
Self::default()
}
/// Sets a GFF record reference sequence name.
///
/// # Examples
///
/// ```
/// use noodles_gff as gff;
///
/// let record = gff::Record::builder()
/// .set_reference_sequence_name(String::from("sq0"))
/// .build();
///
/// assert_eq!(record.reference_sequence_name(), "sq0");
/// ```
pub fn set_reference_sequence_name(mut self, reference_sequence_name: String) -> Self {
self.reference_sequence_name = reference_sequence_name;
self
}
/// Sets a GFF record source.
///
/// # Examples
///
/// ```
/// use noodles_gff as gff;
///
/// let record = gff::Record::builder()
/// .set_source(String::from("NOODLES"))
/// .build();
///
/// assert_eq!(record.source(), "NOODLES");
/// ```
pub fn set_source(mut self, source: String) -> Self {
self.source = source;
self
}
/// Sets a GFF record feature type.
///
/// # Examples
///
/// ```
/// use noodles_gff as gff;
///
/// let record = gff::Record::builder()
/// .set_type(String::from("gene"))
/// .build();
///
/// assert_eq!(record.ty(), "gene");
/// ```
pub fn set_type(mut self, ty: String) -> Self {
self.ty = ty;
self
}
/// Sets a GFF record start position.
///
/// # Examples
///
/// ```
/// use noodles_core::Position;
/// use noodles_gff as gff;
/// let start = Position::MIN;
/// let record = gff::Record::builder().set_start(start).build();
/// assert_eq!(record.start(), start);
/// ```
pub fn set_start(mut self, start: Position) -> Self {
self.start = start;
self
}
/// Sets a GFF record end position.
///
/// # Examples
///
/// ```
/// use noodles_core::Position;
/// use noodles_gff as gff;
/// let end = Position::MIN;
/// let record = gff::Record::builder().set_end(end).build();
/// assert_eq!(record.end(), end);
/// ```
pub fn set_end(mut self, end: Position) -> Self {
self.end = end;
self
}
/// Sets a GFF record score.
///
/// # Examples
///
/// ```
/// use noodles_gff as gff;
/// let record = gff::Record::builder().set_score(21.0).build();
/// assert_eq!(record.score(), Some(21.0));
/// ```
pub fn set_score(mut self, score: f32) -> Self {
self.score = Some(score);
self
}
/// Sets a GFF record strand.
///
/// # Examples
///
/// ```
/// use noodles_gff::{self as gff, record::Strand};
///
/// let record = gff::Record::builder()
/// .set_strand(Strand::Forward)
/// .build();
///
/// assert_eq!(record.strand(), Strand::Forward);
/// ```
pub fn set_strand(mut self, strand: Strand) -> Self {
self.strand = strand;
self
}
/// Sets a GFF record phase.
///
/// # Examples
///
/// ```
/// use noodles_gff::{self as gff, record::Phase};
/// let record = gff::Record::builder().set_phase(Phase::Zero).build();
/// assert_eq!(record.phase(), Some(Phase::Zero));
/// ```
pub fn set_phase(mut self, phase: Phase) -> Self {
self.phase = Some(phase);
self
}
/// Sets GFF record attributes.
///
/// # Examples
///
/// ```
/// use noodles_gff::{
/// self as gff,
/// record::{
/// attributes::field::{Tag, Value},
/// Attributes,
/// },
/// };
///
/// let attributes: Attributes = [(Tag::from("gene_id"), Value::from("ndls0"))]
/// .into_iter()
/// .collect();
///
/// let record = gff::Record::builder()
/// .set_attributes(attributes.clone())
/// .build();
///
/// assert_eq!(record.attributes(), &attributes);
/// ```
pub fn set_attributes(mut self, attributes: Attributes) -> Self {
self.attributes = attributes;
self
}
/// Builds a GFF record.
///
/// # Example
///
/// ```
/// use noodles_gff as gff;
/// let record = gff::Record::builder().build();
/// ```
pub fn build(self) -> Record {
Record {
reference_sequence_name: self.reference_sequence_name,
source: self.source,
ty: self.ty,
start: self.start,
end: self.end,
score: self.score,
strand: self.strand,
phase: self.phase,
attributes: self.attributes,
}
}
}
impl Default for Builder {
fn default() -> Self {
Self {
reference_sequence_name: MISSING_FIELD.into(),
source: MISSING_FIELD.into(),
ty: MISSING_FIELD.into(),
start: Position::MIN,
end: Position::MIN,
score: None,
strand: Strand::default(),
phase: None,
attributes: Attributes::default(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default() {
let record = Builder::default();
assert_eq!(record.reference_sequence_name, ".");
assert_eq!(record.source, ".");
assert_eq!(record.ty, ".");
assert_eq!(record.start, Position::MIN);
assert_eq!(record.end, Position::MIN);
assert!(record.score.is_none());
assert_eq!(record.strand, Strand::default());
assert!(record.phase.is_none());
assert!(record.attributes.is_empty());
}
#[test]
fn test_build() -> Result<(), noodles_core::position::TryFromIntError> {
use crate::record::attributes::field::{Tag, Value};
let attributes: Attributes = [(Tag::from("gene_id"), Value::from("ndls0"))]
.into_iter()
.collect();
let record = Builder::new()
.set_reference_sequence_name(String::from("sq0"))
.set_source(String::from("NOODLES"))
.set_type(String::from("CDS"))
.set_start(Position::try_from(8)?)
.set_end(Position::try_from(13)?)
.set_score(21.0)
.set_strand(Strand::Forward)
.set_phase(Phase::Zero)
.set_attributes(attributes.clone())
.build();
assert_eq!(record.reference_sequence_name(), "sq0");
assert_eq!(record.source(), "NOODLES");
assert_eq!(record.ty(), "CDS");
assert_eq!(record.start(), Position::try_from(8)?);
assert_eq!(record.end(), Position::try_from(13)?);
assert_eq!(record.score(), Some(21.0));
assert_eq!(record.strand(), Strand::Forward);
assert_eq!(record.phase(), Some(Phase::Zero));
assert_eq!(record.attributes(), &attributes);
Ok(())
}
}