1pub mod cigar;
2pub mod cost;
3
4use std::cmp::Ordering;
5
6pub use cigar::*;
8pub use cost::*;
9
10pub type Base = u8;
13
14pub type Sequence = Vec<Base>;
17
18pub type Seq<'a> = &'a [Base];
20
21pub fn seq_to_string(seq: Seq) -> String {
23 String::from_utf8(seq.to_vec()).unwrap()
24}
25
26pub type I = i32;
28
29#[derive(
33 Debug,
34 Clone,
35 Copy,
36 PartialEq,
37 Eq,
38 Hash,
39 Default,
40 derive_more::Add,
41 derive_more::Sub,
42 derive_more::AddAssign,
43 derive_more::SubAssign,
44)]
45pub struct Pos(pub I, pub I);
46
47impl std::fmt::Display for Pos {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 <Self as std::fmt::Debug>::fmt(self, f)
50 }
51}
52
53impl PartialOrd for Pos {
57 #[inline]
58 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
59 let a = self.0.cmp(&other.0);
60 let b = self.1.cmp(&other.1);
61 if a == b {
62 return Some(a);
63 }
64 if a == Ordering::Equal {
65 return Some(b);
66 }
67 if b == Ordering::Equal {
68 return Some(a);
69 }
70 None
71 }
72
73 #[inline]
74 fn le(&self, other: &Self) -> bool {
75 self.0 <= other.0 && self.1 <= other.1
76 }
77}
78
79pub type Path = Vec<Pos>;
81
82impl Pos {
83 pub fn start() -> Self {
85 Pos(0, 0)
86 }
87
88 pub fn target(a: Seq, b: Seq) -> Self {
90 Pos(a.len() as I, b.len() as I)
91 }
92
93 pub fn diag(&self) -> I {
95 self.0 - self.1
96 }
97
98 pub fn anti_diag(&self) -> I {
100 self.0 + self.1
101 }
102
103 pub fn mirror(&self) -> Pos {
105 Pos(self.1, self.0)
106 }
107
108 pub fn from<T>(i: T, j: T) -> Self
110 where
111 T: TryInto<I>,
112 <T as TryInto<i32>>::Error: std::fmt::Debug,
113 {
114 Pos(i.try_into().unwrap(), j.try_into().unwrap())
115 }
116}
117
118#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub struct LexPos(pub Pos);
121
122impl PartialOrd for LexPos {
123 #[inline]
124 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
125 Some(self.cmp(other))
126 }
127
128 #[inline]
129 fn lt(&self, other: &Self) -> bool {
130 (self.0 .0, self.0 .1) < (other.0 .0, other.0 .1)
131 }
132}
133
134impl Ord for LexPos {
135 #[inline]
136 fn cmp(&self, other: &Self) -> Ordering {
137 (self.0 .0, self.0 .1).cmp(&(other.0 .0, other.0 .1))
138 }
139}
140
141pub trait Aligner: std::fmt::Debug {
143 fn align(&mut self, a: Seq, b: Seq) -> (Cost, Option<Cigar>);
147}