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
use noodles_core::Position;

use super::{Attributes, Frame, Record, Strand, MISSING_FIELD};

/// A GTF record builder.
#[derive(Debug)]
pub struct Builder {
    reference_sequence_name: String,
    source: String,
    ty: String,
    start: Position,
    end: Position,
    score: Option<f32>,
    strand: Option<Strand>,
    frame: Option<Frame>,
    attributes: Attributes,
}

impl Builder {
    /// Sets the reference sequence name.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_gtf as gtf;
    /// let record = gtf::Record::builder().set_reference_sequence_name("sq0").build();
    /// assert_eq!(record.reference_sequence_name(), "sq0");
    /// ```
    pub fn set_reference_sequence_name<N>(mut self, reference_sequence_name: N) -> Self
    where
        N: Into<String>,
    {
        self.reference_sequence_name = reference_sequence_name.into();
        self
    }

    /// Sets the source.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_gtf as gtf;
    /// let record = gtf::Record::builder().set_source("NOODLES").build();
    /// assert_eq!(record.source(), "NOODLES");
    /// ```
    pub fn set_source<S>(mut self, source: S) -> Self
    where
        S: Into<String>,
    {
        self.source = source.into();
        self
    }

    /// Sets the feature type.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_gtf as gtf;
    /// let record = gtf::Record::builder().set_type("exon").build();
    /// assert_eq!(record.ty(), "exon");
    /// ```
    pub fn set_type<T>(mut self, ty: T) -> Self
    where
        T: Into<String>,
    {
        self.ty = ty.into();
        self
    }

    /// Sets the start position.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_core::Position;
    /// use noodles_gtf as gtf;
    /// let start = Position::MIN;
    /// let record = gtf::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 the end position.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_core::Position;
    /// use noodles_gtf as gtf;
    /// let end = Position::MIN;
    /// let record = gtf::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 the score.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_gtf as gtf;
    /// let record = gtf::Record::builder().set_score(1.0).build();
    /// assert_eq!(record.score(), Some(1.0));
    /// ```
    pub fn set_score(mut self, score: f32) -> Self {
        self.score = Some(score);
        self
    }

    /// Sets the strand.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_gtf::{self as gtf, record::Strand};
    /// let record = gtf::Record::builder().set_strand(Strand::Forward).build();
    /// assert_eq!(record.strand(), Some(Strand::Forward));
    /// ```
    pub fn set_strand(mut self, strand: Strand) -> Self {
        self.strand = Some(strand);
        self
    }

    /// Sets the frame.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_gtf::{self as gtf, record::Frame};
    /// let frame = Frame::try_from(0)?;
    /// let record = gtf::Record::builder().set_frame(frame).build();
    /// assert_eq!(record.frame(), Some(frame));
    /// Ok::<_, gtf::record::frame::ParseError>(())
    /// ```
    pub fn set_frame(mut self, frame: Frame) -> Self {
        self.frame = Some(frame);
        self
    }

    /// Sets the attributes;
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_gtf::{self as gtf, record::{attributes::Entry, Attributes}};
    /// let attributes = Attributes::from(vec![Entry::new("gene_id", "g0")]);
    /// let record = gtf::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 the GTF record.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_gtf as gtf;
    /// let record = gtf::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,
            frame: self.frame,
            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: None,
            frame: None,
            attributes: Attributes::default(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

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

        assert_eq!(builder.reference_sequence_name, ".");
        assert_eq!(builder.source, ".");
        assert_eq!(builder.ty, ".");
        assert_eq!(builder.start, Position::MIN);
        assert_eq!(builder.end, Position::MIN);
        assert!(builder.score.is_none());
        assert!(builder.strand.is_none());
        assert!(builder.frame.is_none());
        assert!(builder.attributes.is_empty());
    }
}