Skip to main content

cyanea_align/
lib.rs

1//! Sequence alignment algorithms for the Cyanea bioinformatics ecosystem.
2//!
3//! Provides pairwise alignment via Smith-Waterman (local) and Needleman-Wunsch
4//! (global) with affine gap penalties, supporting both nucleotide and protein
5//! scoring schemes (BLOSUM, PAM, custom).
6//!
7//! # Quick start
8//!
9//! ```
10//! use cyanea_align::{align, AlignmentMode, ScoringMatrix, ScoringScheme};
11//!
12//! let scoring = ScoringScheme::Simple(ScoringMatrix::dna_default());
13//! let result = align(b"ACGT", b"ACGT", AlignmentMode::Global, &scoring).unwrap();
14//! assert_eq!(result.score, 8);
15//! ```
16
17pub mod batch;
18pub mod cigar;
19pub mod lcsk;
20pub mod minimizers;
21pub mod msa;
22pub mod needleman_wunsch;
23pub mod pair_hmm;
24pub mod poa;
25pub mod profile_hmm;
26pub mod scoring;
27pub mod seed_extend;
28pub mod semi_global;
29pub mod simd;
30pub mod simd_sw;
31pub mod smith_waterman;
32pub mod spliced;
33pub mod types;
34pub mod wfa;
35pub mod xdrop;
36
37pub mod gpu;
38
39pub use batch::align_batch;
40pub use lcsk::{find_kmer_matches, lcsk_plusplus, sparse_align, SparseAlignResult};
41pub use minimizers::{find_seed_matches, minimizers, Minimizer};
42pub use needleman_wunsch::needleman_wunsch;
43pub use pair_hmm::{
44    pair_hmm_forward, pair_hmm_viterbi, PairHmmAlignment, PairHmmParams, PairHmmState,
45};
46pub use poa::{PoaGraph, PoaScoring};
47pub use profile_hmm::{
48    Alphabet, GumbelParams, ProfileHmm, ProfileHmmConfig, ProfileHmmResult, ProfileHmmState,
49};
50pub use scoring::{ScoringMatrix, ScoringScheme, SubstitutionMatrix};
51pub use seed_extend::{chain_seeds, seed_extend_align, Seed, SeedChain};
52pub use semi_global::semi_global;
53pub use simd_sw::{sw_scalar_score, sw_simd_score};
54pub use smith_waterman::smith_waterman;
55pub use spliced::{
56    chain_exons, detect_splice_site, spliced_align, ExonAlignment, SpliceSiteType,
57    SplicedAlignParams, SplicedAlignResult,
58};
59pub use types::{AlignmentMode, AlignmentResult, CigarOp};
60pub use wfa::wfa_align;
61pub use xdrop::{
62    xdrop_extend_left, xdrop_extend_right, xdrop_seed_extend, zdrop_extend_left,
63    zdrop_extend_right, zdrop_seed_extend, ExtensionResult, XDropParams, ZDropParams,
64};
65
66/// Convenience function: align two sequences using the specified mode and scoring.
67///
68/// Dispatches to [`smith_waterman()`] for [`AlignmentMode::Local`],
69/// [`needleman_wunsch()`] for [`AlignmentMode::Global`], or
70/// [`semi_global()`] for [`AlignmentMode::SemiGlobal`].
71///
72/// # Errors
73///
74/// Returns an error if either sequence is empty.
75pub fn align(
76    query: &[u8],
77    target: &[u8],
78    mode: AlignmentMode,
79    scoring: &ScoringScheme,
80) -> cyanea_core::Result<AlignmentResult> {
81    match mode {
82        AlignmentMode::Local => crate::smith_waterman::smith_waterman(query, target, scoring),
83        AlignmentMode::Global => crate::needleman_wunsch::needleman_wunsch(query, target, scoring),
84        AlignmentMode::SemiGlobal => crate::semi_global::semi_global(query, target, scoring),
85    }
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    fn dna_scheme() -> ScoringScheme {
93        ScoringScheme::Simple(ScoringMatrix::dna_default())
94    }
95
96    #[test]
97    fn align_global_end_to_end() {
98        let result = align(b"ACGT", b"ACGT", AlignmentMode::Global, &dna_scheme()).unwrap();
99        assert_eq!(result.score, 8);
100        assert_eq!(result.cigar_string(), "4=");
101    }
102
103    #[test]
104    fn align_local_end_to_end() {
105        let result = align(
106            b"AAACGTAAA",
107            b"TTTCGTTTT",
108            AlignmentMode::Local,
109            &dna_scheme(),
110        )
111        .unwrap();
112        assert!(result.score > 0);
113    }
114
115    #[test]
116    fn align_semi_global() {
117        let result = align(b"ACGT", b"ACGT", AlignmentMode::SemiGlobal, &dna_scheme()).unwrap();
118        assert_eq!(result.score, 8);
119    }
120
121    #[test]
122    fn align_protein_global() {
123        let scoring = ScoringScheme::Substitution(SubstitutionMatrix::blosum62());
124        let result = align(b"HEAGAWGHEE", b"PAWHEAE", AlignmentMode::Global, &scoring).unwrap();
125        assert!(result.score > 0);
126        assert!(result.length() > 0);
127    }
128}
129
130#[cfg(test)]
131mod proptests {
132    use super::*;
133    use proptest::prelude::*;
134
135    fn dna_seq(max_len: usize) -> impl Strategy<Value = Vec<u8>> {
136        proptest::collection::vec(
137            prop_oneof![Just(b'A'), Just(b'C'), Just(b'G'), Just(b'T')],
138            1..=max_len,
139        )
140    }
141
142    proptest! {
143        #[test]
144        fn alignment_score_is_deterministic(
145            q in dna_seq(50),
146            t in dna_seq(50),
147        ) {
148            let scoring = ScoringScheme::Simple(ScoringMatrix::dna_default());
149            let r1 = align(&q, &t, AlignmentMode::Global, &scoring).unwrap();
150            let r2 = align(&q, &t, AlignmentMode::Global, &scoring).unwrap();
151            prop_assert_eq!(r1.score, r2.score);
152        }
153
154        #[test]
155        fn identity_in_unit_interval(
156            q in dna_seq(50),
157            t in dna_seq(50),
158        ) {
159            let scoring = ScoringScheme::Simple(ScoringMatrix::dna_default());
160            let result = align(&q, &t, AlignmentMode::Global, &scoring).unwrap();
161            let id = result.identity();
162            prop_assert!(id >= 0.0 && id <= 1.0, "identity={} out of [0,1]", id);
163        }
164
165        #[test]
166        fn identical_sequences_perfect_identity(seq in dna_seq(50)) {
167            let scoring = ScoringScheme::Simple(ScoringMatrix::dna_default());
168            let result = align(&seq, &seq, AlignmentMode::Global, &scoring).unwrap();
169            prop_assert!((result.identity() - 1.0).abs() < 1e-10,
170                "identical seqs should have identity=1.0, got {}", result.identity());
171        }
172
173        #[test]
174        fn local_score_nonnegative(
175            q in dna_seq(50),
176            t in dna_seq(50),
177        ) {
178            let scoring = ScoringScheme::Simple(ScoringMatrix::dna_default());
179            let result = align(&q, &t, AlignmentMode::Local, &scoring).unwrap();
180            prop_assert!(result.score >= 0, "SW score should be >= 0, got {}", result.score);
181        }
182    }
183}