Skip to main content

gen_core/
strand.rs

1use std::fmt;
2
3use noodles::gff::feature::record::Strand as GFFStrand;
4use rusqlite::{
5    ToSql,
6    types::{FromSql, FromSqlResult, ToSqlOutput, Value, ValueRef},
7};
8use serde::{Deserialize, Serialize};
9
10use crate::{errors::StrandError, gen_core_capnp};
11
12#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize, Ord, PartialOrd)]
13pub enum Strand {
14    Forward,
15    Reverse,
16    Unknown,
17    ImportantButUnknown,
18}
19
20impl Strand {
21    pub fn is_ambiguous(a: Strand) -> bool {
22        a == Strand::ImportantButUnknown || a == Strand::Unknown
23    }
24
25    pub fn is_compatible(a: Strand, b: Strand) -> bool {
26        let a_ambig = Strand::is_ambiguous(a);
27        let b_ambig = Strand::is_ambiguous(b);
28        (a_ambig || b_ambig) || a == b
29    }
30
31    pub fn complement(self) -> Strand {
32        match self {
33            Strand::Forward => Strand::Reverse,
34            Strand::Reverse => Strand::Forward,
35            Strand::Unknown => Strand::Unknown,
36            Strand::ImportantButUnknown => Strand::ImportantButUnknown,
37        }
38    }
39}
40
41// example https://docs.rs/rusqlite/latest/rusqlite/types/index.html
42impl ToSql for Strand {
43    fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
44        let result = match self {
45            Strand::Forward => "+".into(),
46            Strand::Reverse => "-".into(),
47            Strand::Unknown => ".".into(),
48            Strand::ImportantButUnknown => "?".into(),
49        };
50        Ok(result)
51    }
52}
53
54impl From<Strand> for Value {
55    fn from(value: Strand) -> Value {
56        let result = match value {
57            Strand::Forward => "+",
58            Strand::Reverse => "-",
59            Strand::Unknown => ".",
60            Strand::ImportantButUnknown => "?",
61        };
62        Value::Text(result.to_string())
63    }
64}
65
66impl From<GFFStrand> for Strand {
67    fn from(value: GFFStrand) -> Strand {
68        match value {
69            GFFStrand::Forward => Strand::Forward,
70            GFFStrand::Unknown => Strand::Unknown,
71            GFFStrand::Reverse => Strand::Reverse,
72            GFFStrand::None => Strand::Unknown,
73        }
74    }
75}
76
77impl From<Strand> for GFFStrand {
78    fn from(value: Strand) -> GFFStrand {
79        match value {
80            Strand::Forward => GFFStrand::Forward,
81            Strand::Unknown => GFFStrand::Unknown,
82            Strand::Reverse => GFFStrand::Reverse,
83            Strand::ImportantButUnknown => GFFStrand::None,
84        }
85    }
86}
87
88impl FromSql for Strand {
89    fn column_result(value: ValueRef) -> FromSqlResult<Self> {
90        let result = match value.as_str() {
91            Ok("+") => Strand::Forward,
92            Ok("-") => Strand::Reverse,
93            Ok(".") => Strand::Unknown,
94            Ok("?") => Strand::ImportantButUnknown,
95            _ => panic!("Invalid entry in database"),
96        };
97        Ok(result)
98    }
99}
100
101impl TryFrom<&str> for Strand {
102    type Error = StrandError;
103
104    fn try_from(value: &str) -> Result<Self, Self::Error> {
105        match value {
106            "+" => Ok(Self::Forward),
107            "-" => Ok(Self::Reverse),
108            "." => Ok(Self::Unknown),
109            "?" => Ok(Self::ImportantButUnknown),
110            v => Err(StrandError::InvalidStrand(format!(
111                "Invalid strand entry: {v}"
112            ))),
113        }
114    }
115}
116
117impl From<Strand> for gen_core_capnp::Strand {
118    fn from(value: Strand) -> gen_core_capnp::Strand {
119        match value {
120            Strand::Forward => gen_core_capnp::Strand::Forward,
121            Strand::Unknown => gen_core_capnp::Strand::Unknown,
122            Strand::Reverse => gen_core_capnp::Strand::Reverse,
123            Strand::ImportantButUnknown => gen_core_capnp::Strand::ImportantButUnknown,
124        }
125    }
126}
127
128impl From<gen_core_capnp::Strand> for Strand {
129    fn from(value: gen_core_capnp::Strand) -> Strand {
130        match value {
131            gen_core_capnp::Strand::Forward => Strand::Forward,
132            gen_core_capnp::Strand::Unknown => Strand::Unknown,
133            gen_core_capnp::Strand::Reverse => Strand::Reverse,
134            gen_core_capnp::Strand::ImportantButUnknown => Strand::ImportantButUnknown,
135        }
136    }
137}
138
139impl fmt::Display for Strand {
140    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
141        let result = match self {
142            Strand::Forward => "+",
143            Strand::Reverse => "-",
144            Strand::Unknown => ".",
145            Strand::ImportantButUnknown => "?",
146        };
147        formatter.write_str(result)
148    }
149}
150
151#[cfg(test)]
152mod tests {
153    use super::*;
154
155    #[test]
156    fn test_formats() {
157        assert_eq!(format!("{v}", v = Strand::Forward), "+");
158        assert_eq!(format!("{v}", v = Strand::Reverse), "-");
159        assert_eq!(format!("{v}", v = Strand::ImportantButUnknown), "?");
160        assert_eq!(format!("{v}", v = Strand::Unknown), ".");
161    }
162
163    #[test]
164    fn test_column_conversion() {
165        assert_eq!(
166            Strand::column_result(ValueRef::Text("+".as_bytes())).unwrap(),
167            Strand::Forward
168        );
169        assert_eq!(
170            Strand::column_result(ValueRef::Text("-".as_bytes())).unwrap(),
171            Strand::Reverse
172        );
173        assert_eq!(
174            Strand::column_result(ValueRef::Text(".".as_bytes())).unwrap(),
175            Strand::Unknown
176        );
177        assert_eq!(
178            Strand::column_result(ValueRef::Text("?".as_bytes())).unwrap(),
179            Strand::ImportantButUnknown
180        );
181    }
182
183    #[test]
184    fn test_complement() {
185        assert_eq!(Strand::Forward.complement(), Strand::Reverse);
186        assert_eq!(Strand::Reverse.complement(), Strand::Forward);
187        assert_eq!(Strand::Unknown.complement(), Strand::Unknown);
188        assert_eq!(
189            Strand::ImportantButUnknown.complement(),
190            Strand::ImportantButUnknown
191        );
192    }
193}