1pub mod cigar;
22pub mod cost;
23
24use std::cmp::Ordering;
25
26pub use cigar::*;
28pub use cost::*;
29
30pub type Base = u8;
33
34pub type Sequence = Vec<Base>;
37
38pub type Seq<'a> = &'a [Base];
40
41pub fn seq_to_string(seq: Seq) -> String {
45 String::from_utf8(seq.to_vec()).unwrap()
46}
47
48pub type I = i32;
50
51#[derive(
55 Debug,
56 Clone,
57 Copy,
58 PartialEq,
59 Eq,
60 Hash,
61 Default,
62 derive_more::Add,
63 derive_more::Sub,
64 derive_more::AddAssign,
65 derive_more::SubAssign,
66)]
67pub struct Pos(pub I, pub I);
68
69impl std::fmt::Display for Pos {
70 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71 <Self as std::fmt::Debug>::fmt(self, f)
72 }
73}
74
75impl PartialOrd for Pos {
79 #[inline]
80 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
81 let a = self.0.cmp(&other.0);
82 let b = self.1.cmp(&other.1);
83 if a == b {
84 return Some(a);
85 }
86 if a == Ordering::Equal {
87 return Some(b);
88 }
89 if b == Ordering::Equal {
90 return Some(a);
91 }
92 None
93 }
94
95 #[inline]
96 fn le(&self, other: &Self) -> bool {
97 self.0 <= other.0 && self.1 <= other.1
98 }
99}
100
101pub type Path = Vec<Pos>;
103
104impl Pos {
105 pub fn start() -> Self {
107 Pos(0, 0)
108 }
109
110 pub fn target(a: Seq, b: Seq) -> Self {
112 Pos(a.len() as I, b.len() as I)
113 }
114
115 pub fn diag(&self) -> I {
117 self.0 - self.1
118 }
119
120 pub fn anti_diag(&self) -> I {
122 self.0 + self.1
123 }
124
125 pub fn mirror(&self) -> Pos {
127 Pos(self.1, self.0)
128 }
129
130 pub fn from<T>(i: T, j: T) -> Self
132 where
133 T: TryInto<I>,
134 <T as TryInto<i32>>::Error: std::fmt::Debug,
135 {
136 Pos(i.try_into().unwrap(), j.try_into().unwrap())
137 }
138}
139
140#[derive(Debug, Clone, Copy, PartialEq, Eq)]
142pub struct LexPos(pub Pos);
143
144impl PartialOrd for LexPos {
145 #[inline]
146 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
147 Some(self.cmp(other))
148 }
149
150 #[inline]
151 fn lt(&self, other: &Self) -> bool {
152 (self.0 .0, self.0 .1) < (other.0 .0, other.0 .1)
153 }
154}
155
156impl Ord for LexPos {
157 #[inline]
158 fn cmp(&self, other: &Self) -> Ordering {
159 (self.0 .0, self.0 .1).cmp(&(other.0 .0, other.0 .1))
160 }
161}
162
163pub trait Aligner: std::fmt::Debug {
165 fn align(&mut self, a: Seq, b: Seq) -> (Cost, Option<Cigar>);
169}