zoe 0.0.31

A nightly library for viral genomics
Documentation
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
use crate::{
    alignment::{
        Alignment, MaybeAligned, ProfileError, ScoreAndRanges, ScoreEnds, ScoreStarts, SeqSrc,
        sw::{
            sw_align_3pass, sw_scalar_align, sw_scalar_score, sw_simd_align, sw_simd_score, sw_simd_score_ends,
            sw_simd_score_ends_reverse, sw_simd_score_ranges,
        },
    },
    data::{mappings::ByteIndexMap, matrices::WeightMatrix},
    math::{AlignableIntWidth, AnyInt, FromSameSignedness},
    simd::SimdAnyInt,
};
use std::{
    borrow::Cow,
    convert::Into,
    ops::Range,
    simd::{SimdElement, prelude::*},
    vec,
};

/// Validate the arguments for [`ScalarProfile`] or [`StripedProfile`].
///
/// ## Errors
///
/// - [`ProfileError::EmptySequence`] if `seq` is empty
/// - [`ProfileError::GapOpenOutOfRange`] if `gap_open` is not between -127 and
///   0, inclusive
/// - [`ProfileError::GapExtendOutOfRange`] if `gap_extend` is not between -127
///   and 0, inclusive
/// - [`ProfileError::BadGapWeights`] if `gap_extend` is less than `gap_open`
#[inline]
pub(crate) fn validate_profile_args(seq: &[u8], gap_open: i8, gap_extend: i8) -> Result<(), ProfileError> {
    if seq.as_ref().is_empty() {
        Err(ProfileError::EmptySequence)
    } else if !(-127..=0).contains(&gap_open) {
        Err(ProfileError::GapOpenOutOfRange { gap_open })
    } else if !(-127..=0).contains(&gap_extend) {
        Err(ProfileError::GapExtendOutOfRange { gap_extend })
    } else if gap_extend < gap_open {
        Err(ProfileError::BadGapWeights { gap_open, gap_extend })
    } else {
        Ok(())
    }
}

/// A profile for DNA sequence alignment using scalar operations.
///
/// The profile stores a sequence, weight matrix, and gap penalties to be used
/// in an alignment. The API mirrors that of [`StripedProfile`], but this
/// profile does not use SIMD or create a striped layout.
///
/// ## Parameters
///
/// `S`: The size of the alphabet (usually 5 for DNA including *N*)
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct ScalarProfile<'a, const S: usize> {
    pub(crate) seq:        Cow<'a, [u8]>,
    pub(crate) matrix:     &'a WeightMatrix<'a, i8, S>,
    pub(crate) gap_open:   i32,
    pub(crate) gap_extend: i32,
}

impl<'a, const S: usize> ScalarProfile<'a, S> {
    /// Creates a new profile for use with the scalar alignment algorithm.
    ///
    /// ## Validity
    ///
    /// The [`WeightMatrix`] is asymmetric, then `seq` must correspond to the
    /// query sequence. Otherwise, the scores for `A → B` and `B → A` will be
    /// erroneously swapped.
    ///
    /// ## Errors
    ///
    /// - [`ProfileError::EmptySequence`] if `seq` is empty
    /// - [`ProfileError::GapOpenOutOfRange`] if `gap_open` is not between -127
    ///   and 0, inclusive
    /// - [`ProfileError::GapExtendOutOfRange`] if `gap_extend` is not between
    ///   -127 and 0, inclusive
    /// - [`ProfileError::BadGapWeights`] if `gap_extend` is less than
    ///   `gap_open`
    #[inline]
    pub fn new(
        seq: impl Into<Cow<'a, [u8]>>, matrix: &'a WeightMatrix<'a, i8, S>, gap_open: i8, gap_extend: i8,
    ) -> Result<Self, ProfileError> {
        let seq = seq.into();

        validate_profile_args(&seq, gap_open, gap_extend)?;

        // Validity: validate_profile_args has checked our assumptions
        Ok(Self::new_unchecked(seq, matrix, gap_open, gap_extend))
    }

    /// Creates a new profile for use with the scalar alignment algorithm
    /// without checking for validity of the arguments.
    ///
    /// ## Validity
    ///
    /// - `seq` should be non-empty
    /// - `gap_open` and `gap_extend` must be in the range `-127..=0`
    /// - `gap_extend` must be greater than or equal to `gap_open` (`gap_open`
    ///   must be a worse penalty)
    /// - The [`WeightMatrix`] is asymmetric, then `seq` must correspond to the
    ///   query sequence. Otherwise, the scores for `A → B` and `B → A` will be
    ///   erroneously swapped.
    #[inline]
    #[must_use]
    pub fn new_unchecked(
        seq: impl Into<Cow<'a, [u8]>>, matrix: &'a WeightMatrix<'a, i8, S>, gap_open: i8, gap_extend: i8,
    ) -> Self {
        ScalarProfile {
            seq: seq.into(),
            matrix,
            gap_open: i32::from(gap_open),
            gap_extend: i32::from(gap_extend),
        }
    }

    /// Computes the Smith-Waterman local alignment score between the profile
    /// and a passed sequence.
    ///
    /// For more info, see [`sw_scalar_score`].
    ///
    /// ## Example
    ///
    /// ```
    /// # use zoe::{
    /// #    alignment::{ScalarProfile, SeqSrc, sw::sw_scalar_score},
    /// #    data::matrices::WeightMatrix
    /// # };
    /// let reference: &[u8] = b"GGCCACAGGATTGAG";
    /// let query: &[u8] = b"CTCAGATTG";
    ///
    /// const WEIGHTS: WeightMatrix<i8, 5> = WeightMatrix::new_dna_matrix(4, -2, Some(b'N'));
    /// const GAP_OPEN: i8 = -3;
    /// const GAP_EXTEND: i8 = -1;
    ///
    /// let profile = ScalarProfile::new(query, &WEIGHTS, GAP_OPEN, GAP_EXTEND).unwrap();
    /// let score = profile.sw_score(reference).unwrap();
    /// assert_eq!(score, 27);
    /// ```
    #[inline]
    #[must_use]
    pub fn sw_score<Q>(&self, seq: &Q) -> MaybeAligned<u32>
    where
        Q: AsRef<[u8]> + ?Sized, {
        sw_scalar_score(seq.as_ref(), self)
    }

    /// Computes the Smith-Waterman local alignment between the profile and the
    /// passed sequence.
    ///
    /// For more information, see [`sw_scalar_align`].
    ///
    /// ## Example
    ///
    /// ```
    /// # use zoe::{
    /// #     alignment::{Alignment, ScalarProfile, SeqSrc, sw::sw_scalar_score},
    /// #     data::{matrices::WeightMatrix, cigar::Cigar}
    /// # };
    /// let reference: &[u8] = b"GGCCACAGGATTGAG";
    /// let query: &[u8] = b"CTCAGATTG";
    ///
    /// const WEIGHTS: WeightMatrix<i8, 5> = WeightMatrix::new_dna_matrix(4, -2, Some(b'N'));
    /// const GAP_OPEN: i8 = -3;
    /// const GAP_EXTEND: i8 = -1;
    ///
    /// let profile = ScalarProfile::new(query, &WEIGHTS, GAP_OPEN, GAP_EXTEND).unwrap();
    /// let alignment = profile.sw_align(SeqSrc::Reference(reference)).unwrap();
    /// assert_eq!(alignment.ref_range.start, 3);
    /// assert_eq!(alignment.states, Cigar::from_slice_unchecked("5M1D4M"));
    /// assert_eq!(alignment.score, 27);
    /// ```
    #[inline]
    #[must_use]
    pub fn sw_align<Q>(&self, seq: SeqSrc<&Q>) -> MaybeAligned<Alignment<u32>>
    where
        Q: AsRef<[u8]> + ?Sized, {
        seq.make_alignment(|reference| sw_scalar_align(reference.as_ref(), self))
    }
}

/// A striped profile for DNA sequence alignment using SIMD operations.
///
/// The profile contains pre-computed scoring vectors for each residue in the
/// specified mapping, arranged in a striped pattern to optimize SIMD operations
/// during alignment.
///
/// ## Parameters
///
/// - `'a`: The lifetime of the alphabet ([`ByteIndexMap`]).
/// - `T`: The numeric type used for scores. `i8`, `i16`, `i32`, and `i64` use
///   the signed algorithm, which is the most common. `u8`, `u16`, `u32`, and
///   `u64` use the unsigned algorithm.
/// - `N`: The number of SIMD lanes (usually 16, 32 or 64).
/// - `S`: The size of the alphabet (usually 5 for DNA including `N`).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StripedProfile<'a, T, const N: usize, const S: usize>
where
    T: SimdElement, {
    pub(crate) profile:    Vec<Simd<T, N>>,
    pub(crate) gap_open:   T,
    pub(crate) gap_extend: T,
    pub(crate) bias:       T,
    pub(crate) mapping:    &'a ByteIndexMap<S>,
    pub(crate) seq_len:    usize,
}

impl<'a, T, const N: usize, const S: usize> StripedProfile<'a, T, N, S>
where
    T: AnyInt + SimdElement,
{
    /// Creates a new striped profile from a sequence.
    ///
    /// If the SIMD integer type `T` being used is signed, then [`WeightMatrix`]
    /// should contain signed parameters. Otherwise, a biased weight matrix must
    /// be passed, which is obtained by calling [`to_biased_matrix`] on a
    /// regular signed weight matrix (or using the constructor
    /// [`new_biased_dna_matrix`] if using a simple DNA weight matrix).
    ///
    /// ## Validity
    ///
    /// The [`WeightMatrix`] is asymmetric, then `seq` must correspond to the
    /// query sequence. Otherwise, the scores for `A → B` and `B → A` will be
    /// erroneously swapped.
    ///
    /// ## Errors
    ///
    /// - [`ProfileError::EmptySequence`] if `seq` is empty
    /// - [`ProfileError::GapOpenOutOfRange`] if `gap_open` is not between -127
    ///   and 0, inclusive
    /// - [`ProfileError::GapExtendOutOfRange`] if `gap_extend` is not between
    ///   -127 and 0, inclusive
    /// - [`ProfileError::BadGapWeights`] if `gap_extend` is less than
    ///   `gap_open`
    ///
    /// [`to_biased_matrix`]: WeightMatrix::to_biased_matrix
    /// [`new_biased_dna_matrix`]: WeightMatrix::new_biased_dna_matrix
    pub fn new<U>(seq: &[u8], matrix: &WeightMatrix<'a, U, S>, gap_open: i8, gap_extend: i8) -> Result<Self, ProfileError>
    where
        T: FromSameSignedness<U> + AlignableIntWidth,
        U: AnyInt, {
        validate_profile_args(seq, gap_open, gap_extend)?;
        // Validity: we validated the required assumptions with
        // validate_profile_args
        Ok(Self::new_unchecked(seq, matrix, gap_open, gap_extend))
    }

    /// Creates a new striped profile from a sequence, without checking the
    /// validity of the parameters.
    ///
    /// If the SIMD integer type `T` being used is signed, then [`WeightMatrix`]
    /// should contain signed parameters. Otherwise, a biased weight matrix must
    /// be passed, which is obtained by calling [`to_biased_matrix`] on a
    /// regular signed weight matrix (or using the constructor
    /// [`new_biased_dna_matrix`] if using a simple DNA weight matrix).
    ///
    /// ## Validity
    ///
    /// - `seq` should be non-empty
    /// - `gap_open` and `gap_extend` must be in the range `-127..=0`
    /// - `gap_extend` must be greater than or equal to `gap_open` (`gap_open`
    ///   must be a worse penalty)
    /// - The [`WeightMatrix`] is asymmetric, then `seq` must correspond to the
    ///   query sequence. Otherwise, the scores for `A → B` and `B → A` will be
    ///   erroneously swapped.
    ///
    /// [`to_biased_matrix`]: WeightMatrix::to_biased_matrix
    /// [`new_biased_dna_matrix`]: WeightMatrix::new_biased_dna_matrix
    pub fn new_unchecked<U>(seq: &[u8], matrix: &WeightMatrix<'a, U, S>, gap_open: i8, gap_extend: i8) -> Self
    where
        T: From<U>,
        U: AnyInt, {
        // A SIMD vector cannot presently be of length 0
        let number_vectors = seq.len().div_ceil(N);
        let total_lanes = N * number_vectors;

        let bias = matrix.bias.into();
        let biases = Simd::splat(bias);
        let mut profile = vec![biases; S * number_vectors];

        for v in 0..number_vectors {
            for ref_index in 0..matrix.mapping.len() {
                let mut vector = biases;
                for (i, q) in (v..total_lanes).step_by(number_vectors).enumerate() {
                    if q < seq.len() {
                        // Validity: Either the matrix is symmetric in which
                        // case index order does not matter, or seq is the query
                        // in which case we index by that second
                        let query_index = matrix.mapping.to_index(seq[q]);
                        vector[i] = matrix.weights[ref_index][query_index].into();
                    }
                }
                profile[ref_index * number_vectors + v] = vector;
            }
        }

        StripedProfile {
            profile,
            gap_open: T::from_literal(-gap_open),
            gap_extend: T::from_literal(-gap_extend),
            bias,
            mapping: matrix.mapping,
            seq_len: seq.len(),
        }
    }

    /// Given a profile for a forward sequence, returns a reversed profile of a
    /// subsequence ending at the given index.
    ///
    /// `seq_end` represents the 0-based exclusive-end index. `None` is returned
    /// if the bound is invalid.
    #[must_use]
    pub(crate) fn reverse_from_forward(&self, seq_end: usize) -> Option<Self> {
        if seq_end == 0 || seq_end > self.seq_len {
            return None;
        }

        let number_vectors = seq_end.div_ceil(N);
        let number_vecs_old = self.number_vectors();
        let total_lanes = N * number_vectors;

        let biases = Simd::splat(self.bias);
        let mut profile = vec![biases; S * number_vectors];

        for v in 0..number_vectors {
            for ref_index in 0..self.mapping.len() {
                let mut vector = biases;
                for (i, q) in (v..total_lanes).step_by(number_vectors).enumerate() {
                    if q < seq_end {
                        let q_old = seq_end - 1 - q;
                        let v_old = q_old % number_vecs_old;
                        let lane_old = q_old / number_vecs_old;

                        vector[i] = self.profile[ref_index * number_vecs_old + v_old][lane_old];
                    }
                }
                profile[ref_index * number_vectors + v] = vector;
            }
        }

        Some(StripedProfile {
            profile,
            gap_open: self.gap_open,
            gap_extend: self.gap_extend,
            bias: self.bias,
            mapping: self.mapping,
            seq_len: seq_end,
        })
    }

    /// Returns a sub-profile for a specific range of the query sequence.
    ///
    /// `None` is returned if the bound is invalid.
    #[must_use]
    #[allow(dead_code)]
    pub(crate) fn new_with_range(&self, range: Range<usize>) -> Option<Self> {
        if range.is_empty() || range.end > self.seq_len {
            return None;
        }

        let new_len = range.end - range.start;
        let number_vectors = new_len.div_ceil(N);
        let number_vecs_old = self.number_vectors();
        let total_lanes = N * number_vectors;

        let biases = Simd::splat(self.bias);
        let mut profile = vec![biases; S * number_vectors];

        for v in 0..number_vectors {
            for ref_index in 0..self.mapping.len() {
                let mut vector = biases;
                for (i, q) in (v..total_lanes).step_by(number_vectors).enumerate() {
                    if q < new_len {
                        let q_old = range.start + q;
                        let v_old = q_old % number_vecs_old;
                        let lane_old = q_old / number_vecs_old;

                        vector[i] = self.profile[ref_index * number_vecs_old + v_old][lane_old];
                    }
                }
                profile[ref_index * number_vectors + v] = vector;
            }
        }

        Some(StripedProfile {
            profile,
            gap_open: self.gap_open,
            gap_extend: self.gap_extend,
            bias: self.bias,
            mapping: self.mapping,
            seq_len: new_len,
        })
    }

    /// Returns the number of SIMD vectors in the profile.
    #[inline]
    #[must_use]
    pub fn number_vectors(&self) -> usize {
        self.profile.len() / S
    }
}

impl<T, const N: usize, const S: usize> StripedProfile<'_, T, N, S>
where
    T: AlignableIntWidth,
    Simd<T, N>: SimdAnyInt<T, N>,
{
    /// Computes the Smith-Waterman local alignment score between the profile
    /// and the passed sequence.
    ///
    /// Returns [`Overflowed`] if the score overflowed and [`Unmapped`] if no
    /// portion of the query mapped to the reference.
    ///
    /// For more information, see [`sw_simd_score`].
    ///
    /// ## Example
    ///
    /// ```
    /// # use zoe::{
    /// #    alignment::{SeqSrc, StripedProfile, sw::sw_simd_score},
    /// #    data::matrices::WeightMatrix
    /// # };
    /// let reference: &[u8] = b"ATGCATCGATCGATCGATCGATCGATCGATGC";
    /// let query: &[u8] = b"CGTTCGCCATAAAGGGGG";
    ///
    /// const WEIGHTS: WeightMatrix<u8, 5> = WeightMatrix::new_biased_dna_matrix(4, -2, Some(b'N'));
    /// const GAP_OPEN: i8 = -3;
    /// const GAP_EXTEND: i8 = -1;
    ///
    /// let profile = StripedProfile::<u8, 32, 5>::new(query, &WEIGHTS, GAP_OPEN, GAP_EXTEND).unwrap();
    /// let score = profile.sw_score(reference).unwrap();
    /// assert_eq!(score, 26);
    /// ```
    ///
    /// [`Overflowed`]: MaybeAligned::Overflowed
    /// [`Unmapped`]: MaybeAligned::Unmapped
    #[inline]
    #[must_use]
    pub fn sw_score<Q>(&self, seq: &Q) -> MaybeAligned<u32>
    where
        Q: AsRef<[u8]> + ?Sized, {
        // Validity: The order of the query and reference does not impact final
        // output
        sw_simd_score::<T, N, S>(seq.as_ref(), self)
    }

    /// Similar to [`sw_score`] but includes reference and query
    /// 0-based, exclusive end indices.
    ///
    /// Note: these coordinates are equivalent to the 1-based end positions.
    ///
    /// [`sw_score`]: Self::sw_score
    #[inline]
    #[must_use]
    pub fn sw_score_ends<Q>(&self, seq: SeqSrc<&Q>) -> MaybeAligned<ScoreEnds<u32>>
    where
        Q: AsRef<[u8]> + ?Sized, {
        seq.make_alignment(|reference| sw_simd_score_ends::<T, N, S>(reference.as_ref(), self))
    }

    /// Similar to [`sw_simd_score`], but includes the reference and query
    /// inclusive start index (0-based).
    ///
    /// <div class="warning important">
    ///
    /// **Important**
    ///
    /// The profile should also be in reverse orientation, such as using
    /// [`reverse_from_forward`].
    ///
    /// </div>
    ///
    /// [`reverse_from_forward`]: StripedProfile::reverse_from_forward
    #[inline]
    #[must_use]
    #[allow(dead_code)]
    pub(crate) fn sw_score_ends_reverse<Q>(&self, seq: SeqSrc<&Q>) -> MaybeAligned<ScoreStarts<u32>>
    where
        Q: AsRef<[u8]> + ?Sized, {
        seq.make_alignment(|reference| sw_simd_score_ends_reverse::<T, N, S>(reference.as_ref(), self))
    }

    /// Computes the Smith-Waterman local alignment between the query profile
    /// and the passed reference.
    ///
    /// Returns [`Overflowed`] if the score overflowed and [`Unmapped`] if no
    /// portion of the query mapped to the reference.
    ///
    /// For more info, see: [`sw_simd_align`].
    ///
    /// ## Example
    ///
    /// ```
    /// # use zoe::{
    /// #     alignment::{StripedProfile, SeqSrc, sw::sw_simd_score},
    /// #     data::matrices::WeightMatrix
    /// # };
    /// let reference: &[u8] = b"ATGCATCGATCGATCGATCGATCGATCGATGC";
    /// let query: &[u8] = b"CGTTCGCCATAAAGGGGG";
    ///
    /// const WEIGHTS: WeightMatrix<u8, 5> = WeightMatrix::new_biased_dna_matrix(4, -2, Some(b'N'));
    /// const GAP_OPEN: i8 = -3;
    /// const GAP_EXTEND: i8 = -1;
    ///
    /// let profile = StripedProfile::<u8, 32, 5>::new(query, &WEIGHTS, GAP_OPEN, GAP_EXTEND).unwrap();
    /// let alignment = profile.sw_align(SeqSrc::Reference(reference)).unwrap();
    /// // alignment contains ref_range, states (cigar), and score
    /// ```
    ///
    /// [`Overflowed`]: MaybeAligned::Overflowed
    /// [`Unmapped`]: MaybeAligned::Unmapped
    #[inline]
    #[must_use]
    pub fn sw_align<Q>(&self, seq: SeqSrc<&Q>) -> MaybeAligned<Alignment<u32>>
    where
        Q: AsRef<[u8]> + ?Sized, {
        seq.make_alignment(|reference| sw_simd_align::<T, N, S>(reference.as_ref(), self))
    }

    /// Similar to [`sw_score`] but includes the reference and query
    /// alignment ranges.
    ///
    /// These are standard 0-based, half-open ranges for slicing.
    ///
    /// [`sw_score`]: Self::sw_score
    #[inline]
    #[must_use]
    pub fn sw_score_ranges<Q>(&self, seq: SeqSrc<&Q>) -> MaybeAligned<ScoreAndRanges<u32>>
    where
        Q: AsRef<[u8]> + ?Sized, {
        seq.make_alignment(|reference| sw_simd_score_ranges::<T, N, S>(reference.as_ref(), self))
    }

    /// Similar to [`sw_align`] but computes the Smith-Waterman
    /// local alignment using a 3-pass algorithm.
    ///
    /// This approach was inspired by (7), albeit this implementation is for
    /// large reference / small query because banded is not used.
    ///
    /// See **[module citations](crate::alignment::sw#module-citations)**.
    ///
    /// [`sw_align`]: Self::sw_align
    #[inline]
    #[must_use]
    pub(crate) fn sw_align_3pass<Q>(
        &self, seq: SeqSrc<&Q>, profile_seq: &[u8], matrix: &WeightMatrix<i8, S>, gap_open: i8, gap_extend: i8,
    ) -> MaybeAligned<Alignment<u32>>
    where
        Q: AsRef<[u8]> + ?Sized, {
        seq.make_alignment(|reference| sw_align_3pass(reference.as_ref(), self, profile_seq, matrix, gap_open, gap_extend))
    }
}

#[cfg(test)]
mod bench {
    use test::Bencher;
    extern crate test;
    use super::*;
    use crate::alignment::sw::test_data::{GAP_EXTEND, GAP_OPEN};
    use crate::data::{constants::mappings::DNA_PROFILE_MAP, matrices::WeightMatrix};
    pub(crate) static DATA: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data/KJ907631.1.txt")); // H5 HA, complete CDS
    pub(crate) static MATRIX: WeightMatrix<u8, 5> =
        WeightMatrix::new(&DNA_PROFILE_MAP, 2, -5, Some(b'N')).to_biased_matrix();

    #[bench]
    fn build_profile_u8(b: &mut Bencher) {
        b.iter(|| std::hint::black_box(StripedProfile::<u8, 32, 5>::new(DATA, &MATRIX, GAP_OPEN, GAP_EXTEND)));
    }

    #[bench]
    fn build_profile_u8_rev_half_recreate(b: &mut Bencher) {
        b.iter(|| {
            let data_rev: Vec<u8> = DATA[..DATA.len() / 2].iter().copied().rev().collect();
            std::hint::black_box(StripedProfile::<u8, 32, 5>::new(&data_rev, &MATRIX, GAP_OPEN, GAP_EXTEND))
        });
    }

    #[bench]
    fn build_profile_u8_rev_half_mapping(b: &mut Bencher) {
        let prof = StripedProfile::<u8, 32, 5>::new(DATA, &MATRIX, GAP_OPEN, GAP_EXTEND).unwrap();

        b.iter(|| std::hint::black_box(prof.reverse_from_forward(DATA.len() / 2)));
    }
}