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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use std::collections::{HashSet, VecDeque};

use crate::op::{Backtrack, BestCost, IndexedOperation, Operation};
use crate::{Measure, SeqPair};

/// Trait enabling alignment of all `Measure`s.
///
/// This trait is used to implement alignment using dynamic programming
/// for every type that implements the `Measure` trait.
pub trait Align<'a, M, T>
where
    M: Measure<T>,
    T: Eq,
{
    /// Align two sequences.
    ///
    /// This function aligns two sequences and returns the alignment.
    fn align(&'a self, source: &'a [T], target: &'a [T]) -> Alignment<'a, M, T>;
}

impl<'a, M, T> Align<'a, M, T> for M
where
    M: Measure<T>,
    T: Eq,
{
    fn align(&'a self, source: &'a [T], target: &'a [T]) -> Alignment<'a, M, T> {
        let pair = SeqPair { source, target };

        let source_len = pair.source.len() + 1;
        let target_len = pair.target.len() + 1;

        let mut cost_matrix = vec![vec![0; target_len]; source_len];

        // Fill first row. This is separated from the rest of the matrix fill
        // because we do not want to fill cell [0][0].
        for target_idx in 1..target_len {
            cost_matrix[0][target_idx] = self
                .best_cost(&pair, &cost_matrix, 0, target_idx)
                .expect("No applicable operation");
        }

        // Fill the matrix
        for source_idx in 1..source_len {
            for target_idx in 0..target_len {
                cost_matrix[source_idx][target_idx] = self
                    .best_cost(&pair, &cost_matrix, source_idx, target_idx)
                    .expect("No applicable operation");
            }
        }

        Alignment {
            measure: self,
            pair,
            cost_matrix,
        }
    }
}

/// Edit distance cost matrix.
pub struct Alignment<'a, M, T>
where
    M: Measure<T>,
    T: Eq,
{
    measure: &'a M,
    pair: SeqPair<'a, T>,
    cost_matrix: Vec<Vec<usize>>,
}

impl<'a, M, T> Alignment<'a, M, T>
where
    M: Measure<T>,
    T: Eq,
{
    /// Get the edit distance.
    pub fn distance(&self) -> usize {
        self.cost_matrix[self.cost_matrix.len() - 1][self.cost_matrix[0].len() - 1]
    }

    /// Return the script of edit operations to rewrite the source sequence
    /// to the target sequence. If there are multiple possible edit scripts,
    /// this method will return one of the possible edit scripts. If you want
    /// to retrieve all possible edit scripts, use the `edit_scripts` method.
    pub fn edit_script(&self) -> Vec<IndexedOperation<M::Operation>> {
        let mut source_idx = self.pair.source.len();
        let mut target_idx = self.pair.target.len();
        let mut script = Vec::new();

        while let Some(op) =
            self.measure
                .backtrack(&self.pair, &self.cost_matrix, source_idx, target_idx)
        {
            let (new_source_idx, new_target_idx) = op
                .backtrack(&self.pair, source_idx, target_idx)
                .expect("Cannot backtrack");
            source_idx = new_source_idx;
            target_idx = new_target_idx;

            script.push(IndexedOperation::new(op, source_idx, target_idx));

            if source_idx == 0 && target_idx == 0 {
                break;
            }
        }

        assert_eq!(source_idx, 0, "Cannot backtrack to cell 0, 0");
        assert_eq!(target_idx, 0, "Cannot backtrack to cell 0, 0");

        script.reverse();

        script
    }

    /// Return all the edit scripts to rewrite the source sequence to the
    /// target sequence. If you want just one edit script, use the
    /// `edit_script` method instead.
    pub fn edit_scripts(&self) -> HashSet<Vec<IndexedOperation<M::Operation>>> {
        // Find all scripts that lead to the lowest edit distance using
        // breadth-first search.

        // Start the search in the lower-right corner with the final cost.
        let mut q: VecDeque<BacktrackState<M, T>> = VecDeque::new();
        q.push_back(BacktrackState {
            source_idx: self.pair.source.len(),
            target_idx: self.pair.target.len(),
            script: Vec::new(),
        });

        let mut scripts = HashSet::new();
        while let Some(BacktrackState {
            source_idx,
            target_idx,
            script,
        }) = q.pop_front()
        {
            // Process all operations/origins that can lead to the current cell's
            // cost.
            for op in self
                .measure
                .backtracks(&self.pair, &self.cost_matrix, source_idx, target_idx)
            {
                let (new_source_idx, new_target_idx) = op
                    .backtrack(&self.pair, source_idx, target_idx)
                    .expect("Cannot backtrack");
                let mut new_script = script.clone();

                new_script.push(IndexedOperation::new(op, new_source_idx, new_target_idx));

                if new_source_idx == 0 && new_target_idx == 0 {
                    // If we are in the upper-left cell, we have a complete script.
                    new_script.reverse();
                    scripts.insert(new_script);
                } else {
                    // Otherwise, add the state to the queue to explore later.
                    q.push_back(BacktrackState {
                        source_idx: new_source_idx,
                        target_idx: new_target_idx,
                        script: new_script,
                    })
                }
            }
        }

        assert!(!scripts.is_empty(), "Cannot backtrack to cell 0, 0");

        scripts
    }

    /// Get the cost matrix.
    pub fn cost_matrix(&self) -> &Vec<Vec<usize>> {
        &self.cost_matrix
    }

    /// Get the sequence pair associated with this cost matrix.
    pub fn seq_pair(&self) -> &SeqPair<T> {
        &self.pair
    }
}

struct BacktrackState<M, T>
where
    M: Measure<T>,
{
    source_idx: usize,
    target_idx: usize,
    script: Vec<IndexedOperation<M::Operation>>,
}

#[cfg(test)]
mod tests {
    use crate::measures::Levenshtein;
    use crate::measures::LevenshteinOp::*;
    use crate::op::IndexedOperation;

    use super::Align;

    #[test]
    fn distance_test() {
        let applet: Vec<char> = "applet".chars().collect();
        let pineapple: Vec<char> = "pineapple".chars().collect();
        let pen: Vec<char> = "pen".chars().collect();

        let levenshtein = Levenshtein::new(1, 1, 1);

        assert_eq!(levenshtein.align(&pineapple, &pen).distance(), 7);
        assert_eq!(levenshtein.align(&pen, &pineapple).distance(), 7);
        assert_eq!(levenshtein.align(&pineapple, &applet).distance(), 5);
        assert_eq!(levenshtein.align(&applet, &pen).distance(), 4);
    }

    #[test]
    fn edit_script_test() {
        let applet: Vec<char> = "applet".chars().collect();
        let pineapple: Vec<char> = "pineapple".chars().collect();
        let pen: Vec<char> = "pen".chars().collect();

        let levenshtein = Levenshtein::new(1, 1, 1);

        assert_eq!(
            vec![
                IndexedOperation::new(Match, 0, 0),
                IndexedOperation::new(Substitute(1), 1, 1),
                IndexedOperation::new(Match, 2, 2),
                IndexedOperation::new(Delete(1), 3, 3),
                IndexedOperation::new(Delete(1), 4, 3),
                IndexedOperation::new(Delete(1), 5, 3),
                IndexedOperation::new(Delete(1), 6, 3),
                IndexedOperation::new(Delete(1), 7, 3),
                IndexedOperation::new(Delete(1), 8, 3),
            ],
            levenshtein.align(&pineapple, &pen).edit_script()
        );

        assert_eq!(
            vec![
                IndexedOperation::new(Match, 0, 0),
                IndexedOperation::new(Substitute(1), 1, 1),
                IndexedOperation::new(Match, 2, 2),
                IndexedOperation::new(Insert(1), 3, 3),
                IndexedOperation::new(Insert(1), 3, 4),
                IndexedOperation::new(Insert(1), 3, 5),
                IndexedOperation::new(Insert(1), 3, 6),
                IndexedOperation::new(Insert(1), 3, 7),
                IndexedOperation::new(Insert(1), 3, 8),
            ],
            levenshtein.align(&pen, &pineapple).edit_script()
        );

        assert_eq!(
            vec![
                IndexedOperation::new(Delete(1), 0, 0),
                IndexedOperation::new(Delete(1), 1, 0),
                IndexedOperation::new(Delete(1), 2, 0),
                IndexedOperation::new(Delete(1), 3, 0),
                IndexedOperation::new(Match, 4, 0),
                IndexedOperation::new(Match, 5, 1),
                IndexedOperation::new(Match, 6, 2),
                IndexedOperation::new(Match, 7, 3),
                IndexedOperation::new(Match, 8, 4),
                IndexedOperation::new(Insert(1), 9, 5),
            ],
            levenshtein.align(&pineapple, &applet).edit_script()
        );
    }

    #[test]
    fn edit_script_tests() {
        let applet: Vec<char> = "applet".chars().collect();
        let pineapple: Vec<char> = "pineapple".chars().collect();
        let aplpet: Vec<char> = "aplpet".chars().collect();

        let levenshtein = Levenshtein::new(1, 1, 1);
        assert_eq!(
            hashset![vec![
                IndexedOperation::new(Delete(1), 0, 0),
                IndexedOperation::new(Delete(1), 1, 0),
                IndexedOperation::new(Delete(1), 2, 0),
                IndexedOperation::new(Delete(1), 3, 0),
                IndexedOperation::new(Match, 4, 0),
                IndexedOperation::new(Match, 5, 1),
                IndexedOperation::new(Match, 6, 2),
                IndexedOperation::new(Match, 7, 3),
                IndexedOperation::new(Match, 8, 4),
                IndexedOperation::new(Insert(1), 9, 5),
            ],],
            levenshtein.align(&pineapple, &applet).edit_scripts()
        );

        assert_eq!(
            hashset![
                vec![
                    IndexedOperation::new(Match, 0, 0),
                    IndexedOperation::new(Match, 1, 1),
                    IndexedOperation::new(Substitute(1), 2, 2),
                    IndexedOperation::new(Substitute(1), 3, 3),
                    IndexedOperation::new(Match, 4, 4),
                    IndexedOperation::new(Match, 5, 5),
                ],
                vec![
                    IndexedOperation::new(Match, 0, 0),
                    IndexedOperation::new(Match, 1, 1),
                    IndexedOperation::new(Delete(1), 2, 2),
                    IndexedOperation::new(Match, 3, 2),
                    IndexedOperation::new(Insert(1), 4, 3),
                    IndexedOperation::new(Match, 4, 4),
                    IndexedOperation::new(Match, 5, 5),
                ],
                vec![
                    IndexedOperation::new(Match, 0, 0),
                    IndexedOperation::new(Delete(1), 1, 1),
                    IndexedOperation::new(Match, 2, 1),
                    IndexedOperation::new(Match, 3, 2),
                    IndexedOperation::new(Insert(1), 4, 3),
                    IndexedOperation::new(Match, 4, 4),
                    IndexedOperation::new(Match, 5, 5),
                ],
                vec![
                    IndexedOperation::new(Match, 0, 0),
                    IndexedOperation::new(Match, 1, 1),
                    IndexedOperation::new(Insert(1), 2, 2),
                    IndexedOperation::new(Match, 2, 3),
                    IndexedOperation::new(Delete(1), 3, 4),
                    IndexedOperation::new(Match, 4, 4),
                    IndexedOperation::new(Match, 5, 5),
                ],
            ],
            levenshtein.align(&applet, &aplpet).edit_scripts()
        );

        assert_eq!(
            hashset![vec![
                IndexedOperation::new(Match, 0, 0),
                IndexedOperation::new(Match, 1, 1),
                IndexedOperation::new(Match, 2, 2),
                IndexedOperation::new(Match, 3, 3),
                IndexedOperation::new(Match, 4, 4),
                IndexedOperation::new(Match, 5, 5),
            ]],
            levenshtein.align(&applet, &applet).edit_scripts()
        );
    }

    #[test]
    fn align_empty_test() {
        let empty: &[char] = &[];
        let non_empty: Vec<char> = "hello".chars().collect();

        let levenshtein = Levenshtein::new(1, 1, 1);

        assert_eq!(levenshtein.align(empty, empty).distance(), 0);
        assert_eq!(levenshtein.align(non_empty.as_slice(), empty).distance(), 5);
        assert_eq!(levenshtein.align(empty, non_empty.as_slice()).distance(), 5);
    }
}