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
pub mod key;
pub(crate) mod parser;
pub mod value;
pub use self::key::Key;
use std::{error, fmt, str::FromStr};
use self::value::{
map::{self, AlternativeAllele, Contig, Filter, Format, Info, Meta, Other},
Map,
};
use super::{file_format, FileFormat};
pub(crate) const PREFIX: &str = "##";
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Record {
AlternativeAllele(Map<AlternativeAllele>),
Assembly(String),
Contig(Map<Contig>),
FileFormat(FileFormat),
Filter(Map<Filter>),
Format(Map<Format>),
Info(Map<Info>),
Meta(Map<Meta>),
PedigreeDb(String),
Other(Key, value::Other),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
Invalid,
InvalidFileFormat(file_format::ParseError),
InvalidInfo(map::TryFromFieldsError),
InvalidFilter(map::TryFromFieldsError),
InvalidFormat(map::TryFromFieldsError),
InvalidAlternativeAllele(map::TryFromFieldsError),
InvalidContig(map::TryFromFieldsError),
InvalidMeta(map::TryFromFieldsError),
}
impl error::Error for ParseError {}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Invalid => f.write_str("invalid input"),
Self::InvalidFileFormat(e) => write!(f, "invalid file format: {}", e),
Self::InvalidInfo(e) => write!(f, "invalid INFO: {}", e),
Self::InvalidFilter(e) => write!(f, "invalid FILTER: {}", e),
Self::InvalidFormat(e) => write!(f, "invalid FORMAT: {}", e),
Self::InvalidAlternativeAllele(e) => write!(f, "invalid ALT: {}", e),
Self::InvalidContig(e) => write!(f, "invalid contig: {}", e),
Self::InvalidMeta(e) => write!(f, "invalid META: {}", e),
}
}
}
impl FromStr for Record {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from((FileFormat::default(), s))
}
}
impl TryFrom<(FileFormat, &str)> for Record {
type Error = ParseError;
fn try_from((file_format, s): (FileFormat, &str)) -> Result<Self, Self::Error> {
use self::parser::Value;
let (_, (raw_key, value)) = parser::parse(s).map_err(|_| ParseError::Invalid)?;
match Key::from(raw_key) {
key::FILE_FORMAT => match value {
Value::String(s) => {
let file_format = s.parse().map_err(ParseError::InvalidFileFormat)?;
Ok(Self::FileFormat(file_format))
}
_ => Err(ParseError::Invalid),
},
key::INFO => match value {
Value::Struct(fields) => {
let info = Map::<Info>::try_from((file_format, fields))
.map_err(ParseError::InvalidInfo)?;
Ok(Self::Info(info))
}
_ => Err(ParseError::Invalid),
},
key::FILTER => match value {
Value::Struct(fields) => {
let filter =
Map::<Filter>::try_from(fields).map_err(|_| ParseError::Invalid)?;
Ok(Self::Filter(filter))
}
_ => Err(ParseError::Invalid),
},
key::FORMAT => match value {
Value::Struct(fields) => {
let format = Map::<Format>::try_from((file_format, fields))
.map_err(|_| ParseError::Invalid)?;
Ok(Self::Format(format))
}
_ => Err(ParseError::Invalid),
},
key::ALTERNATIVE_ALLELE => match value {
Value::Struct(fields) => {
let alternative_allele = Map::<AlternativeAllele>::try_from(fields)
.map_err(|_| ParseError::Invalid)?;
Ok(Self::AlternativeAllele(alternative_allele))
}
_ => Err(ParseError::Invalid),
},
key::ASSEMBLY => match value {
Value::String(s) => Ok(Self::Assembly(s)),
_ => Err(ParseError::Invalid),
},
key::CONTIG => match value {
Value::Struct(fields) => {
let contig =
Map::<Contig>::try_from(fields).map_err(|_| ParseError::Invalid)?;
Ok(Self::Contig(contig))
}
_ => Err(ParseError::Invalid),
},
key::META => match value {
Value::Struct(fields) => {
let meta = Map::<Meta>::try_from(fields).map_err(|_| ParseError::Invalid)?;
Ok(Self::Meta(meta))
}
_ => Err(ParseError::Invalid),
},
key::PEDIGREE_DB => match value {
Value::String(s) => Ok(Self::PedigreeDb(s)),
_ => Err(ParseError::Invalid),
},
k => {
let v = match value {
Value::String(s) => value::Other::from(s),
Value::Struct(fields) => {
let map =
Map::<Other>::try_from(fields).map_err(|_| ParseError::Invalid)?;
value::Other::from(map)
}
};
Ok(Self::Other(k, v))
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_str() -> Result<(), ParseError> {
let line = "##fileformat=VCFv4.3";
assert_eq!(line.parse(), Ok(Record::FileFormat(FileFormat::new(4, 3))));
let line =
r#"##INFO=<ID=NS,Number=1,Type=Integer,Description="Number of samples with data">"#;
assert!(matches!(line.parse(), Ok(Record::Info(_))));
assert!("".parse::<Record>().is_err());
Ok(())
}
}