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 types;
18pub mod cigar;
19pub mod scoring;
20pub mod needleman_wunsch;
21pub mod smith_waterman;
22pub mod semi_global;
23pub mod batch;
24pub mod simd;
25pub mod simd_sw;
26pub mod msa;
27pub mod poa;
28pub mod minimizers;
29pub mod seed_extend;
30pub mod wfa;
31pub mod lcsk;
32pub mod pair_hmm;
33pub mod profile_hmm;
34pub mod spliced;
35pub mod xdrop;
36
37pub mod gpu;
38
39pub use types::{AlignmentMode, AlignmentResult, CigarOp};
40pub use scoring::{ScoringMatrix, ScoringScheme, SubstitutionMatrix};
41pub use needleman_wunsch::needleman_wunsch;
42pub use smith_waterman::smith_waterman;
43pub use semi_global::semi_global;
44pub use batch::align_batch;
45pub use simd_sw::{sw_simd_score, sw_scalar_score};
46pub use minimizers::{minimizers, find_seed_matches, Minimizer};
47pub use seed_extend::{seed_extend_align, chain_seeds, Seed, SeedChain};
48pub use wfa::wfa_align;
49pub use lcsk::{sparse_align, find_kmer_matches, lcsk_plusplus, SparseAlignResult};
50pub use poa::{PoaGraph, PoaScoring};
51pub use pair_hmm::{
52    pair_hmm_forward, pair_hmm_viterbi, PairHmmAlignment, PairHmmParams, PairHmmState,
53};
54pub use profile_hmm::{
55    Alphabet, GumbelParams, ProfileHmm, ProfileHmmConfig, ProfileHmmResult, ProfileHmmState,
56};
57pub use spliced::{
58    chain_exons, detect_splice_site, spliced_align, ExonAlignment, SpliceSiteType,
59    SplicedAlignParams, SplicedAlignResult,
60};
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(prop_oneof![Just(b'A'), Just(b'C'), Just(b'G'), Just(b'T')], 1..=max_len)
137    }
138
139    proptest! {
140        #[test]
141        fn alignment_score_is_deterministic(
142            q in dna_seq(50),
143            t in dna_seq(50),
144        ) {
145            let scoring = ScoringScheme::Simple(ScoringMatrix::dna_default());
146            let r1 = align(&q, &t, AlignmentMode::Global, &scoring).unwrap();
147            let r2 = align(&q, &t, AlignmentMode::Global, &scoring).unwrap();
148            prop_assert_eq!(r1.score, r2.score);
149        }
150
151        #[test]
152        fn identity_in_unit_interval(
153            q in dna_seq(50),
154            t in dna_seq(50),
155        ) {
156            let scoring = ScoringScheme::Simple(ScoringMatrix::dna_default());
157            let result = align(&q, &t, AlignmentMode::Global, &scoring).unwrap();
158            let id = result.identity();
159            prop_assert!(id >= 0.0 && id <= 1.0, "identity={} out of [0,1]", id);
160        }
161
162        #[test]
163        fn identical_sequences_perfect_identity(seq in dna_seq(50)) {
164            let scoring = ScoringScheme::Simple(ScoringMatrix::dna_default());
165            let result = align(&seq, &seq, AlignmentMode::Global, &scoring).unwrap();
166            prop_assert!((result.identity() - 1.0).abs() < 1e-10,
167                "identical seqs should have identity=1.0, got {}", result.identity());
168        }
169
170        #[test]
171        fn local_score_nonnegative(
172            q in dna_seq(50),
173            t in dna_seq(50),
174        ) {
175            let scoring = ScoringScheme::Simple(ScoringMatrix::dna_default());
176            let result = align(&q, &t, AlignmentMode::Local, &scoring).unwrap();
177            prop_assert!(result.score >= 0, "SW score should be >= 0, got {}", result.score);
178        }
179    }
180}