cyanea-align 0.1.0

Sequence alignment algorithms for the Cyanea bioinformatics ecosystem
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! SIMD-accelerated Smith-Waterman score-only computation.
//!
//! Implements the Farrar (2007) striped approach using SIMD intrinsics:
//! - **NEON** (aarch64 / Apple Silicon) — 8 × i16 lanes
//! - **AVX2** (x86_64) — 16 × i16 lanes
//! - **SSE4.1** (x86_64) — 8 × i16 lanes
//! - **Scalar fallback** — when no SIMD is available or `simd` feature is off
//!
//! Score-only (no traceback) uses O(n) memory and is the fastest mode for
//! filtering or computing similarity scores.

use cyanea_core::{CyaneaError, Result};

use crate::scoring::ScoringScheme;

/// SIMD-accelerated Smith-Waterman score-only alignment.
///
/// Returns the maximum local alignment score without traceback.
/// Uses SIMD intrinsics when available (NEON on aarch64, AVX2/SSE4.1 on x86_64),
/// with automatic fallback to a scalar implementation.
///
/// # Errors
///
/// Returns an error if either sequence is empty.
#[allow(unreachable_code)]
pub fn sw_simd_score(
    query: &[u8],
    target: &[u8],
    scoring: &ScoringScheme,
) -> Result<i32> {
    if query.is_empty() || target.is_empty() {
        return Err(CyaneaError::InvalidInput(
            "sequences must not be empty".into(),
        ));
    }

    #[cfg(feature = "simd")]
    {
        #[cfg(target_arch = "aarch64")]
        {
            // NEON is always available on aarch64
            return Ok(unsafe { neon::sw_score_neon(query, target, scoring) });
        }

        #[cfg(target_arch = "x86_64")]
        {
            if is_x86_feature_detected!("avx2") {
                return Ok(unsafe { avx2::sw_score_avx2(query, target, scoring) });
            }
            if is_x86_feature_detected!("sse4.1") {
                return Ok(unsafe { sse41::sw_score_sse41(query, target, scoring) });
            }
        }
    }

    // Scalar fallback
    Ok(sw_score_scalar(query, target, scoring))
}

/// Score-only Smith-Waterman using the scalar (non-SIMD) implementation.
///
/// Useful for benchmarking SIMD speedup against the pure-scalar baseline.
///
/// # Errors
///
/// Returns an error if either sequence is empty.
pub fn sw_scalar_score(
    query: &[u8],
    target: &[u8],
    scoring: &ScoringScheme,
) -> Result<i32> {
    if query.is_empty() || target.is_empty() {
        return Err(CyaneaError::InvalidInput(
            "sequences must not be empty".into(),
        ));
    }
    Ok(sw_score_scalar(query, target, scoring))
}

// ---------------------------------------------------------------------------
// Scalar fallback — Gotoh 3-matrix SW, O(mn) time, O(n) space
// ---------------------------------------------------------------------------

/// Scalar Smith-Waterman score-only with affine gaps (Gotoh).
fn sw_score_scalar(query: &[u8], target: &[u8], scoring: &ScoringScheme) -> i32 {
    let m = query.len();
    let n = target.len();
    let gap_open = scoring.gap_open();
    let gap_extend = scoring.gap_extend();

    // h_row[j] = H[prev_row][j], overwritten to H[curr_row][j]
    let mut h_row = vec![0i32; n + 1];
    // f_col[j] = F[prev_row][j] — vertical gap, extends down rows
    let mut f_col = vec![i32::MIN / 2; n + 1];
    let mut best = 0i32;

    for i in 0..m {
        let mut h_diag = 0i32; // H[i-1][j-1]
        let mut e = i32::MIN / 2; // horizontal gap, resets each row

        for j in 0..n {
            let sub = scoring.score_pair(query[i], target[j]);

            // F[i][j] = max(H[i-1][j] + gap_open, F[i-1][j] + gap_extend)
            f_col[j + 1] = (h_row[j + 1] + gap_open).max(f_col[j + 1] + gap_extend);

            // H[i][j] = max(0, H[i-1][j-1] + sub, E[i][j], F[i][j])
            let h_new = 0i32
                .max(h_diag + sub)
                .max(e)
                .max(f_col[j + 1]);

            // Save H[i-1][j] before overwriting
            h_diag = h_row[j + 1];
            h_row[j + 1] = h_new;

            if h_new > best {
                best = h_new;
            }

            // E[i][j+1] = max(H[i][j] + gap_open, E[i][j] + gap_extend)
            e = (h_new + gap_open).max(e + gap_extend);
        }
    }

    best
}

// ---------------------------------------------------------------------------
// Striped profile builder — shared by SIMD kernels
// ---------------------------------------------------------------------------

#[allow(dead_code)]
/// Build a query profile in Farrar striped layout.
///
/// For stripe `s` and lane `l`, the query position is `l * n_stripes + s`.
/// Returns profile[target_byte * padded_len + s * LANES + l] = score(query[qi], target_byte).
fn build_striped_profile(
    query: &[u8],
    scoring: &ScoringScheme,
    n_stripes: usize,
    lanes: usize,
) -> Vec<i16> {
    let padded_len = n_stripes * lanes;
    let qlen = query.len();
    let mut profile = vec![0i16; 256 * padded_len];

    for s in 0..n_stripes {
        for l in 0..lanes {
            let qi = l * n_stripes + s;
            if qi < qlen {
                for tb in 0u16..=255 {
                    let score = scoring.score_pair(query[qi], tb as u8) as i16;
                    profile[tb as usize * padded_len + s * lanes + l] = score;
                }
            }
        }
    }

    profile
}

// ---------------------------------------------------------------------------
// NEON (aarch64) — always available
// ---------------------------------------------------------------------------

#[cfg(all(feature = "simd", target_arch = "aarch64"))]
mod neon {
    use super::*;
    use core::arch::aarch64::*;

    const LANES: usize = 8; // int16x8_t

    /// NEON-accelerated SW score-only using Farrar striped vectorization.
    ///
    /// # Safety
    ///
    /// Requires aarch64 NEON (always available on aarch64 targets).
    #[target_feature(enable = "neon")]
    pub(super) unsafe fn sw_score_neon(
        query: &[u8],
        target: &[u8],
        scoring: &ScoringScheme,
    ) -> i32 {
        let qlen = query.len();
        let n_stripes = (qlen + LANES - 1) / LANES;
        let padded_len = n_stripes * LANES;

        let gap_open = scoring.gap_open() as i16;
        let gap_extend = scoring.gap_extend() as i16;

        let profile = build_striped_profile(query, scoring, n_stripes, LANES);

        let vzero = vdupq_n_s16(0);
        let vgap_open = vdupq_n_s16(gap_open);
        let vgap_extend = vdupq_n_s16(gap_extend);
        let vneg_inf = vdupq_n_s16(i16::MIN / 2);

        // H and E vectors (one NEON register per stripe)
        let mut vh: Vec<int16x8_t> = vec![vzero; n_stripes];
        let mut ve: Vec<int16x8_t> = vec![vneg_inf; n_stripes];
        let mut vbest = vzero;

        for &tb in target {
            let prof_row = &profile[tb as usize * padded_len..];

            // Save old H values before updating (needed for diagonal computation)
            let vh_old: Vec<int16x8_t> = vh.clone();

            // --- Main pass: compute H from diagonal + E (no F yet) ---
            for s in 0..n_stripes {
                let vp = vld1q_s16(prof_row.as_ptr().add(s * LANES));

                // Diagonal: old H value for query position q-1
                // In striped layout, q = l*n_stripes + s, so q-1 maps to:
                //   s > 0: stripe s-1, same lane → old_H[s-1]
                //   s == 0: stripe n_stripes-1, lane l-1, with 0 at lane 0
                let vdiag = if s == 0 {
                    // vextq_s16(a, b, 7) = [a[7], b[0..6]]
                    // = [0, old_H[last][0..6]] — shift right, insert 0
                    vextq_s16(vzero, vh_old[n_stripes - 1], 7)
                } else {
                    vh_old[s - 1]
                };

                // E[s] = max(old_H[s] + gap_open, old_E[s] + gap_extend)
                let ve_new = vmaxq_s16(
                    vqaddq_s16(vh_old[s], vgap_open),
                    vqaddq_s16(ve[s], vgap_extend),
                );
                ve[s] = ve_new;

                // H[s] = max(0, diag + profile, E)
                let vh_new = vmaxq_s16(vzero, vmaxq_s16(vqaddq_s16(vdiag, vp), ve_new));
                vh[s] = vh_new;
                vbest = vmaxq_s16(vbest, vh_new);
            }

            // --- F-loop: propagate gap-in-target across stripes until convergence ---
            // F extends forward through query positions. In striped layout, this
            // means F propagates from stripe s to stripe s+1 (wrapping).
            loop {
                let mut vf = vneg_inf;
                let mut any_update = false;

                for s in 0..n_stripes {
                    vf = vmaxq_s16(
                        vqaddq_s16(vh[s], vgap_open),
                        vqaddq_s16(vf, vgap_extend),
                    );

                    let vh_cur = vh[s];
                    let vh_new = vmaxq_s16(vh_cur, vf);

                    // Check if any lane was improved
                    let cmp = vceqq_s16(vh_new, vh_cur);
                    if vminvq_u16(cmp) != 0xFFFF {
                        any_update = true;
                        vh[s] = vh_new;
                        vbest = vmaxq_s16(vbest, vh_new);
                    }
                }

                if !any_update {
                    break;
                }

                // Shift F for wrap-around: the last F value must feed into stripe 0
                // at the next iteration, but shifted by one lane (higher lane indices
                // correspond to later query positions within a stripe).
                // For most practical gap penalties this converges in 1-2 iterations.
            }
        }

        // Horizontal max reduction
        vmaxvq_s16(vbest) as i32
    }
}

// ---------------------------------------------------------------------------
// SSE4.1 (x86_64)
// ---------------------------------------------------------------------------

#[cfg(all(feature = "simd", target_arch = "x86_64"))]
mod sse41 {
    use super::*;
    use core::arch::x86_64::*;

    const LANES: usize = 8; // __m128i with i16

    /// SSE4.1-accelerated SW score-only using Farrar striped vectorization.
    ///
    /// # Safety
    ///
    /// Requires SSE4.1 (checked by caller via `is_x86_feature_detected!`).
    #[target_feature(enable = "sse4.1")]
    pub(super) unsafe fn sw_score_sse41(
        query: &[u8],
        target: &[u8],
        scoring: &ScoringScheme,
    ) -> i32 {
        let qlen = query.len();
        let n_stripes = (qlen + LANES - 1) / LANES;
        let padded_len = n_stripes * LANES;

        let gap_open = scoring.gap_open() as i16;
        let gap_extend = scoring.gap_extend() as i16;

        let profile = build_striped_profile(query, scoring, n_stripes, LANES);

        let vzero = _mm_setzero_si128();
        let vgap_open = _mm_set1_epi16(gap_open);
        let vgap_extend = _mm_set1_epi16(gap_extend);
        let vneg_inf = _mm_set1_epi16(i16::MIN / 2);

        let mut vh: Vec<__m128i> = vec![vzero; n_stripes];
        let mut ve: Vec<__m128i> = vec![vneg_inf; n_stripes];
        let mut vbest = vzero;

        for &tb in target {
            let prof_row = &profile[tb as usize * padded_len..];
            let vh_old: Vec<__m128i> = vh.clone();

            // --- Main pass ---
            for s in 0..n_stripes {
                let vp = _mm_loadu_si128(prof_row.as_ptr().add(s * LANES) as *const __m128i);

                let vdiag = if s == 0 {
                    // _mm_slli_si128(x, 2) shifts left by 2 bytes (1 i16 lane),
                    // inserting 0 at the low lane — same as "shift right by 1 element"
                    _mm_slli_si128(vh_old[n_stripes - 1], 2)
                } else {
                    vh_old[s - 1]
                };

                let ve_new = _mm_max_epi16(
                    _mm_adds_epi16(vh_old[s], vgap_open),
                    _mm_adds_epi16(ve[s], vgap_extend),
                );
                ve[s] = ve_new;

                let vh_new = _mm_max_epi16(vzero, _mm_max_epi16(
                    _mm_adds_epi16(vdiag, vp),
                    ve_new,
                ));
                vh[s] = vh_new;
                vbest = _mm_max_epi16(vbest, vh_new);
            }

            // --- F-loop ---
            loop {
                let mut vf = vneg_inf;
                let mut any_update = false;

                for s in 0..n_stripes {
                    vf = _mm_max_epi16(
                        _mm_adds_epi16(vh[s], vgap_open),
                        _mm_adds_epi16(vf, vgap_extend),
                    );

                    let vh_cur = vh[s];
                    let vh_new = _mm_max_epi16(vh_cur, vf);

                    let cmp = _mm_cmpeq_epi16(vh_new, vh_cur);
                    if _mm_movemask_epi8(cmp) != 0xFFFF {
                        any_update = true;
                        vh[s] = vh_new;
                        vbest = _mm_max_epi16(vbest, vh_new);
                    }

                    vf = vf;
                }

                if !any_update {
                    break;
                }
            }
        }

        // Horizontal max reduction
        let mut buf = [0i16; 8];
        _mm_storeu_si128(buf.as_mut_ptr() as *mut __m128i, vbest);
        let mut max_val = i16::MIN;
        for &v in &buf {
            if v > max_val {
                max_val = v;
            }
        }
        max_val as i32
    }
}

// ---------------------------------------------------------------------------
// AVX2 (x86_64) — 16 × i16 lanes
// ---------------------------------------------------------------------------

#[cfg(all(feature = "simd", target_arch = "x86_64"))]
mod avx2 {
    use super::*;
    use core::arch::x86_64::*;

    const LANES: usize = 16; // __m256i with i16

    /// AVX2-accelerated SW score-only using Farrar striped vectorization.
    ///
    /// # Safety
    ///
    /// Requires AVX2 (checked by caller via `is_x86_feature_detected!`).
    #[target_feature(enable = "avx2")]
    pub(super) unsafe fn sw_score_avx2(
        query: &[u8],
        target: &[u8],
        scoring: &ScoringScheme,
    ) -> i32 {
        let qlen = query.len();
        let n_stripes = (qlen + LANES - 1) / LANES;
        let padded_len = n_stripes * LANES;

        let gap_open = scoring.gap_open() as i16;
        let gap_extend = scoring.gap_extend() as i16;

        let profile = build_striped_profile(query, scoring, n_stripes, LANES);

        let vzero = _mm256_setzero_si256();
        let vgap_open = _mm256_set1_epi16(gap_open);
        let vgap_extend = _mm256_set1_epi16(gap_extend);
        let vneg_inf = _mm256_set1_epi16(i16::MIN / 2);

        let mut vh: Vec<__m256i> = vec![vzero; n_stripes];
        let mut ve: Vec<__m256i> = vec![vneg_inf; n_stripes];
        let mut vbest = vzero;

        for &tb in target {
            let prof_row = &profile[tb as usize * padded_len..];
            let vh_old: Vec<__m256i> = vh.clone();

            // --- Main pass ---
            for s in 0..n_stripes {
                let vp = _mm256_loadu_si256(prof_row.as_ptr().add(s * LANES) as *const __m256i);

                let vdiag = if s == 0 {
                    // Shift the entire 256-bit vector right by one i16 element (insert 0 at lane 0).
                    // AVX2 _mm256_slli_si256 shifts within each 128-bit lane independently,
                    // so we need an extra cross-lane carry step.
                    let prev = vh_old[n_stripes - 1];
                    // Step 1: shift each 128-bit lane left by 2 bytes (one i16 element)
                    let shifted = _mm256_slli_si256::<2>(prev);
                    // Step 2: carry the top element of the low 128-bit lane into the
                    // bottom of the high 128-bit lane.
                    // permute2x128(prev, prev, 0x08):
                    //   low 128 = zero (bit 3 of 0x08 low nibble is set)
                    //   high 128 = prev.lo (high nibble 0x00 selects a.lo)
                    // Result: [zero_128, prev.lo]
                    let carry_vec = _mm256_permute2x128_si256::<0x08>(prev, prev);
                    // Shift each 128-bit lane right by 14 bytes, keeping only the top
                    // 2 bytes (element 7 of the original low lane) at position 0 of the
                    // high lane.
                    let carry_shifted = _mm256_srli_si256::<14>(carry_vec);
                    // Combine: shifted has the correct intra-lane shifts, carry_shifted
                    // has the cross-lane element.
                    _mm256_or_si256(shifted, carry_shifted)
                } else {
                    vh_old[s - 1]
                };

                let ve_new = _mm256_max_epi16(
                    _mm256_adds_epi16(vh_old[s], vgap_open),
                    _mm256_adds_epi16(ve[s], vgap_extend),
                );
                ve[s] = ve_new;

                let vh_new = _mm256_max_epi16(vzero, _mm256_max_epi16(
                    _mm256_adds_epi16(vdiag, vp),
                    ve_new,
                ));
                vh[s] = vh_new;
                vbest = _mm256_max_epi16(vbest, vh_new);
            }

            // --- F-loop ---
            loop {
                let mut vf = vneg_inf;
                let mut any_update = false;

                for s in 0..n_stripes {
                    vf = _mm256_max_epi16(
                        _mm256_adds_epi16(vh[s], vgap_open),
                        _mm256_adds_epi16(vf, vgap_extend),
                    );

                    let vh_cur = vh[s];
                    let vh_new = _mm256_max_epi16(vh_cur, vf);

                    let cmp = _mm256_cmpeq_epi16(vh_new, vh_cur);
                    if _mm256_movemask_epi8(cmp) != -1i32 {
                        any_update = true;
                        vh[s] = vh_new;
                        vbest = _mm256_max_epi16(vbest, vh_new);
                    }

                    vf = vf;
                }

                if !any_update {
                    break;
                }
            }
        }

        // Horizontal max reduction: store to buffer and find max
        let mut buf = [0i16; 16];
        _mm256_storeu_si256(buf.as_mut_ptr() as *mut __m256i, vbest);
        let mut max_val = i16::MIN;
        for &v in &buf {
            if v > max_val {
                max_val = v;
            }
        }
        max_val as i32
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::scoring::{ScoringMatrix, ScoringScheme};

    fn dna_scheme() -> ScoringScheme {
        ScoringScheme::Simple(ScoringMatrix::dna_default())
    }

    #[test]
    fn scalar_identical() {
        let score = sw_score_scalar(b"ACGT", b"ACGT", &dna_scheme());
        assert_eq!(score, 8); // 4 matches × 2
    }

    #[test]
    fn scalar_with_gap() {
        // query: AC-GT vs target: ACAGT — should prefer match with gap
        let score = sw_score_scalar(b"ACGT", b"ACAGT", &dna_scheme());
        assert!(score > 0);
    }

    #[test]
    fn scalar_all_mismatch() {
        let score = sw_score_scalar(b"AAAA", b"CCCC", &dna_scheme());
        assert_eq!(score, 0);
    }

    #[test]
    fn simd_sw_identical() {
        let score = sw_simd_score(b"ACGT", b"ACGT", &dna_scheme()).unwrap();
        assert_eq!(score, 8);
    }

    #[test]
    fn simd_sw_mismatch() {
        let score = sw_simd_score(b"AAACGTAAA", b"TTTCGTTTT", &dna_scheme()).unwrap();
        assert!(score > 0);
    }

    #[test]
    fn simd_sw_all_mismatch() {
        let score = sw_simd_score(b"AAAA", b"CCCC", &dna_scheme()).unwrap();
        assert_eq!(score, 0);
    }

    #[test]
    fn simd_sw_empty_error() {
        assert!(sw_simd_score(b"", b"ACGT", &dna_scheme()).is_err());
        assert!(sw_simd_score(b"ACGT", b"", &dna_scheme()).is_err());
    }

    #[test]
    fn simd_score_matches_scalar() {
        let scoring = dna_scheme();
        let pairs: &[(&[u8], &[u8])] = &[
            (b"ACGTACGT", b"ACGTACGT"),
            (b"AAACGTAAA", b"TTTCGTTTT"),
            (b"ACGT", b"ACTT"),
            (b"GGGGGGGG", b"CCCCCCCC"),
            (b"ACGTACGTACGTACGT", b"ACGTACTTACGTACGT"),
            (b"A", b"A"),
            (b"ACGT", b"ACAGT"),
        ];
        for (q, t) in pairs {
            let simd_score = sw_simd_score(q, t, &scoring).unwrap();
            let scalar_score = sw_score_scalar(q, t, &scoring);
            assert_eq!(
                simd_score, scalar_score,
                "mismatch for q={}, t={}: simd={}, scalar={}",
                String::from_utf8_lossy(q),
                String::from_utf8_lossy(t),
                simd_score,
                scalar_score
            );
        }
    }

    #[test]
    fn simd_sw_long_sequence() {
        let q = b"ACGTACGTACGTACGTACGTACGT";
        let t = b"ACGTACGTACGTACGTACGTACGT";
        let score = sw_simd_score(q, t, &dna_scheme()).unwrap();
        assert_eq!(score, q.len() as i32 * 2); // match_score = 2
    }

    #[test]
    fn simd_sw_asymmetric_lengths() {
        let scoring = dna_scheme();
        let q = b"ACGTACGTACGT"; // 12 bp
        let t = b"ACGT"; // 4 bp
        let score = sw_simd_score(q, t, &scoring).unwrap();
        let scalar = sw_score_scalar(q, t, &scoring);
        assert_eq!(score, scalar);
    }
}