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
use std::{error, fmt};
use noodles_sam as sam;
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Base {
A,
C,
G,
T,
N,
}
impl Default for Base {
fn default() -> Self {
Self::N
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct TryFromError;
impl fmt::Display for TryFromError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid substitution matrix base",)
}
}
impl error::Error for TryFromError {}
impl TryFrom<sam::record::sequence::Base> for Base {
type Error = TryFromError;
fn try_from(base: sam::record::sequence::Base) -> Result<Self, Self::Error> {
use sam::record::sequence::Base as SamBase;
match base {
SamBase::A => Ok(Self::A),
SamBase::C => Ok(Self::C),
SamBase::G => Ok(Self::G),
SamBase::T => Ok(Self::T),
SamBase::N => Ok(Self::N),
_ => Err(TryFromError),
}
}
}
impl TryFrom<u8> for Base {
type Error = TryFromError;
fn try_from(n: u8) -> Result<Self, Self::Error> {
match n {
b'A' => Ok(Self::A),
b'C' => Ok(Self::C),
b'G' => Ok(Self::G),
b'T' => Ok(Self::T),
b'N' => Ok(Self::N),
_ => Err(TryFromError),
}
}
}
impl From<Base> for sam::record::sequence::Base {
fn from(base: Base) -> Self {
use sam::record::sequence::Base as SamBase;
match base {
Base::A => SamBase::A,
Base::C => SamBase::C,
Base::G => SamBase::G,
Base::T => SamBase::T,
Base::N => SamBase::N,
}
}
}
impl From<Base> for u8 {
fn from(base: Base) -> Self {
match base {
Base::A => b'A',
Base::C => b'C',
Base::G => b'G',
Base::T => b'T',
Base::N => b'N',
}
}
}
#[cfg(test)]
mod tests {
use sam::record::sequence::Base as SamBase;
use super::*;
#[test]
fn test_default() {
assert_eq!(Base::default(), Base::N);
}
#[test]
fn test_try_from_sam_record_sequence_base_for_base() {
assert_eq!(Base::try_from(SamBase::A), Ok(Base::A));
assert_eq!(Base::try_from(SamBase::C), Ok(Base::C));
assert_eq!(Base::try_from(SamBase::G), Ok(Base::G));
assert_eq!(Base::try_from(SamBase::T), Ok(Base::T));
assert_eq!(Base::try_from(SamBase::N), Ok(Base::N));
assert_eq!(Base::try_from(SamBase::U), Err(TryFromError));
}
#[test]
fn test_try_from_u8_for_base() {
assert_eq!(Base::try_from(b'A'), Ok(Base::A));
assert_eq!(Base::try_from(b'C'), Ok(Base::C));
assert_eq!(Base::try_from(b'G'), Ok(Base::G));
assert_eq!(Base::try_from(b'T'), Ok(Base::T));
assert_eq!(Base::try_from(b'N'), Ok(Base::N));
assert_eq!(Base::try_from(b'U'), Err(TryFromError));
}
#[test]
fn test_from_base_for_sam_record_sequence_base() {
assert_eq!(SamBase::from(Base::A), SamBase::A);
assert_eq!(SamBase::from(Base::C), SamBase::C);
assert_eq!(SamBase::from(Base::G), SamBase::G);
assert_eq!(SamBase::from(Base::T), SamBase::T);
assert_eq!(SamBase::from(Base::N), SamBase::N);
}
#[test]
fn test_from_base_for_u8() {
assert_eq!(u8::from(Base::A), b'A');
assert_eq!(u8::from(Base::C), b'C');
assert_eq!(u8::from(Base::G), b'G');
assert_eq!(u8::from(Base::T), b'T');
assert_eq!(u8::from(Base::N), b'N');
}
}