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
pub mod genome_build;
pub mod gff_version;
pub mod sequence_region;
pub use self::{
genome_build::GenomeBuild, gff_version::GffVersion, sequence_region::SequenceRegion,
};
use std::{error, fmt, str::FromStr};
pub(crate) const PREFIX: &str = "##";
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Directive {
GffVersion(GffVersion),
SequenceRegion(SequenceRegion),
FeatureOntology(String),
AttributeOntology(String),
SourceOntology(String),
Species(String),
GenomeBuild(GenomeBuild),
ForwardReferencesAreResolved,
StartOfFasta,
}
impl fmt::Display for Directive {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::GffVersion(version) => write!(f, "{}gff-version {}", PREFIX, version),
Self::SequenceRegion(sequence_region) => write!(f, "{}", sequence_region),
Self::FeatureOntology(uri) => write!(f, "{}feature-ontology {}", PREFIX, uri),
Self::AttributeOntology(uri) => write!(f, "{}attribute-ontology {}", PREFIX, uri),
Self::SourceOntology(uri) => write!(f, "{}source-ontology {}", PREFIX, uri),
Self::Species(uri) => write!(f, "{}species {}", PREFIX, uri),
Self::GenomeBuild(genome_build) => write!(f, "{}", genome_build),
Self::ForwardReferencesAreResolved => write!(f, "{}#", PREFIX),
Self::StartOfFasta => write!(f, "{}FASTA", PREFIX),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
MissingPrefix,
MissingName,
InvalidName(String),
MissingValue,
InvalidGffVersion(gff_version::ParseError),
InvalidSequenceRegion(sequence_region::ParseError),
InvalidGenomeBuild(genome_build::ParseError),
}
impl error::Error for ParseError {}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingPrefix => f.write_str("directive prefix is missing"),
Self::MissingName => f.write_str("directive name is missing"),
Self::InvalidName(s) => write!(f, "invalid directive name: {}", s),
Self::MissingValue => f.write_str("directive value is missing"),
Self::InvalidGffVersion(e) => write!(f, "{}", e),
Self::InvalidSequenceRegion(e) => write!(f, "{}", e),
Self::InvalidGenomeBuild(e) => write!(f, "{}", e),
}
}
}
impl FromStr for Directive {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.starts_with(PREFIX) {
return Err(ParseError::MissingPrefix);
}
let mut components = s[PREFIX.len()..].splitn(2, |c: char| c.is_ascii_whitespace());
let name = components.next().ok_or(ParseError::MissingName)?;
match name {
"gff-version" => components
.next()
.ok_or(ParseError::MissingValue)
.and_then(|s| s.parse().map_err(ParseError::InvalidGffVersion))
.map(Self::GffVersion),
"sequence-region" => components
.next()
.ok_or(ParseError::MissingValue)
.and_then(|s| s.parse().map_err(ParseError::InvalidSequenceRegion))
.map(Self::SequenceRegion),
"feature-ontology" => components
.next()
.map(|s| Self::FeatureOntology(s.into()))
.ok_or(ParseError::MissingValue),
"attribute-ontology" => components
.next()
.map(|s| Self::AttributeOntology(s.into()))
.ok_or(ParseError::MissingValue),
"source-ontology" => components
.next()
.map(|s| Self::SourceOntology(s.into()))
.ok_or(ParseError::MissingValue),
"species" => components
.next()
.map(|s| Self::Species(s.into()))
.ok_or(ParseError::MissingValue),
"genome-build" => components
.next()
.ok_or(ParseError::MissingValue)
.and_then(|s| s.parse().map_err(ParseError::InvalidGenomeBuild))
.map(Self::GenomeBuild),
"#" => Ok(Self::ForwardReferencesAreResolved),
"FASTA" => Ok(Self::StartOfFasta),
_ => Err(ParseError::InvalidName(name.into())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() {
assert_eq!(
Directive::GffVersion(GffVersion::default()).to_string(),
"##gff-version 3"
);
let directive = Directive::SequenceRegion(SequenceRegion::new(String::from("sq0"), 8, 13));
assert_eq!(directive.to_string(), "##sequence-region sq0 8 13");
assert_eq!(
Directive::FeatureOntology(String::from("https://example.com/fo.obo")).to_string(),
"##feature-ontology https://example.com/fo.obo"
);
assert_eq!(
Directive::AttributeOntology(String::from("https://example.com/ao.obo")).to_string(),
"##attribute-ontology https://example.com/ao.obo"
);
assert_eq!(
Directive::SourceOntology(String::from("https://example.com/so.obo")).to_string(),
"##source-ontology https://example.com/so.obo"
);
assert_eq!(
Directive::Species(String::from("https://example.com/species?id=1")).to_string(),
"##species https://example.com/species?id=1"
);
let directive =
Directive::GenomeBuild(GenomeBuild::new(String::from("NDLS"), String::from("r1")));
assert_eq!(directive.to_string(), "##genome-build NDLS r1");
assert_eq!(Directive::ForwardReferencesAreResolved.to_string(), "###");
assert_eq!(Directive::StartOfFasta.to_string(), "##FASTA");
}
}