noodles_gff/
record.rs

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
//! Raw GFF record.

pub mod attributes;
pub(crate) mod fields;
mod phase;
mod strand;

use std::{fmt, io};

use noodles_core::Position;

use self::fields::Fields;
pub use self::{attributes::Attributes, phase::Phase, strand::Strand};

const MISSING: &str = ".";

/// An immutable, lazily-evalulated GFF record.
#[derive(Clone, Eq, PartialEq)]
pub struct Record<'l>(Fields<'l>);

impl<'l> Record<'l> {
    pub(super) fn try_new(src: &'l str) -> io::Result<Self> {
        Fields::try_new(src).map(Self)
    }

    /// Returns the reference sequence name.
    pub fn reference_sequence_name(&self) -> &str {
        self.0.reference_sequence_name()
    }

    /// Returns the source.
    pub fn source(&self) -> &str {
        self.0.source()
    }

    /// Returns the feature type.
    pub fn ty(&self) -> &str {
        self.0.ty()
    }

    /// Returns the start position.
    pub fn start(&self) -> io::Result<Position> {
        self.0.start()
    }

    /// Returns the end position.
    pub fn end(&self) -> io::Result<Position> {
        self.0.end()
    }

    /// Returns the score.
    pub fn score(&self) -> Option<io::Result<f32>> {
        parse_score(self.0.score())
    }

    /// Returns the strand.
    pub fn strand(&self) -> io::Result<Strand> {
        parse_strand(self.0.strand())
    }

    /// Returns the phase.
    pub fn phase(&self) -> Option<io::Result<Phase>> {
        parse_phase(self.0.phase())
    }

    /// Returns the attributes.
    pub fn attributes(&self) -> Attributes<'_> {
        self.0.attributes()
    }
}

impl fmt::Debug for Record<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Record")
            .field("reference_sequence_name", &self.reference_sequence_name())
            .field("source", &self.source())
            .field("ty", &self.ty())
            .field("start", &self.start())
            .field("end", &self.end())
            .field("score", &self.score())
            .field("strand", &self.strand())
            .field("phase", &self.phase())
            .field("attributes", &self.attributes())
            .finish()
    }
}

fn parse_score(s: &str) -> Option<io::Result<f32>> {
    match s {
        MISSING => None,
        _ => Some(
            s.parse()
                .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)),
        ),
    }
}

fn parse_strand(s: &str) -> io::Result<Strand> {
    match s {
        "." => Ok(Strand::None),
        "+" => Ok(Strand::Forward),
        "-" => Ok(Strand::Reverse),
        "?" => Ok(Strand::Unknown),
        _ => Err(io::Error::new(io::ErrorKind::InvalidData, "invalid strand")),
    }
}

fn parse_phase(s: &str) -> Option<io::Result<Phase>> {
    match s {
        MISSING => None,
        "0" => Some(Ok(Phase::Zero)),
        "1" => Some(Ok(Phase::One)),
        "2" => Some(Ok(Phase::Two)),
        _ => Some(Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "invalid phase",
        ))),
    }
}

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

    #[test]
    fn test_parse_score() -> io::Result<()> {
        assert!(parse_score(".").is_none());
        assert_eq!(parse_score("0.0").transpose()?, Some(0.0));

        assert!(matches!(
            parse_phase(""),
            Some(Err(e)) if e.kind() == io::ErrorKind::InvalidData
        ));

        Ok(())
    }

    #[test]
    fn test_parse_strand() -> io::Result<()> {
        assert_eq!(parse_strand(".")?, Strand::None);
        assert_eq!(parse_strand("+")?, Strand::Forward);
        assert_eq!(parse_strand("-")?, Strand::Reverse);
        assert_eq!(parse_strand("?")?, Strand::Unknown);

        assert!(matches!(
            parse_strand(""),
            Err(e) if e.kind() == io::ErrorKind::InvalidData
        ));

        Ok(())
    }

    #[test]
    fn test_parse_phase() -> io::Result<()> {
        assert!(parse_phase(".").is_none());
        assert_eq!(parse_phase("0").transpose()?, Some(Phase::Zero));
        assert_eq!(parse_phase("1").transpose()?, Some(Phase::One));
        assert_eq!(parse_phase("2").transpose()?, Some(Phase::Two));

        assert!(matches!(
            parse_phase(""),
            Some(Err(e)) if e.kind() == io::ErrorKind::InvalidData
        ));

        Ok(())
    }
}