use crate::scoring::ScoringScheme;
use crate::types::{AlignmentResult, CigarOp};
use cyanea_core::{CyaneaError, Result};
pub fn needleman_wunsch(
query: &[u8],
target: &[u8],
scoring: &ScoringScheme,
) -> Result<AlignmentResult> {
let m = query.len();
let n = target.len();
if m == 0 || n == 0 {
return Err(CyaneaError::InvalidInput(
"sequences must not be empty".into(),
));
}
let gap_open = scoring.gap_open();
let gap_extend = scoring.gap_extend();
let rows = m + 1;
let cols = n + 1;
let mut h = vec![i32::MIN / 2; rows * cols];
let mut e = vec![i32::MIN / 2; rows * cols];
let mut f = vec![i32::MIN / 2; rows * cols];
let idx = |i: usize, j: usize| -> usize { i * cols + j };
h[idx(0, 0)] = 0;
for i in 1..rows {
h[idx(i, 0)] = gap_open + (i as i32 - 1) * gap_extend;
f[idx(i, 0)] = h[idx(i, 0)];
}
for j in 1..cols {
h[idx(0, j)] = gap_open + (j as i32 - 1) * gap_extend;
e[idx(0, j)] = h[idx(0, j)];
}
for i in 1..rows {
for j in 1..cols {
e[idx(i, j)] = (h[idx(i, j - 1)] + gap_open).max(e[idx(i, j - 1)] + gap_extend);
f[idx(i, j)] = (h[idx(i - 1, j)] + gap_open).max(f[idx(i - 1, j)] + gap_extend);
let sub = scoring.score_pair(query[i - 1], target[j - 1]);
let diag = h[idx(i - 1, j - 1)] + sub;
h[idx(i, j)] = diag.max(e[idx(i, j)]).max(f[idx(i, j)]);
}
}
let mut aligned_query = Vec::new();
let mut aligned_target = Vec::new();
let mut cigar_ops: Vec<CigarOp> = Vec::new();
let mut i = m;
let mut j = n;
#[derive(Clone, Copy, PartialEq)]
enum State {
H,
E,
F,
}
let mut state = State::H;
while i > 0 || j > 0 {
match state {
State::H => {
if i > 0 && j > 0 {
let sub = scoring.score_pair(query[i - 1], target[j - 1]);
let diag = h[idx(i - 1, j - 1)] + sub;
if h[idx(i, j)] == diag {
aligned_query.push(query[i - 1]);
aligned_target.push(target[j - 1]);
let op = if query[i - 1].to_ascii_uppercase()
== target[j - 1].to_ascii_uppercase()
{
CigarOp::Match(1)
} else {
CigarOp::Mismatch(1)
};
push_cigar(&mut cigar_ops, op);
i -= 1;
j -= 1;
} else if h[idx(i, j)] == e[idx(i, j)] {
state = State::E;
} else {
state = State::F;
}
} else if j > 0 {
state = State::E;
} else {
state = State::F;
}
}
State::E => {
aligned_query.push(b'-');
aligned_target.push(target[j - 1]);
push_cigar(&mut cigar_ops, CigarOp::Insertion(1));
if e[idx(i, j)] == h[idx(i, j - 1)] + gap_open {
state = State::H;
}
j -= 1;
}
State::F => {
aligned_query.push(query[i - 1]);
aligned_target.push(b'-');
push_cigar(&mut cigar_ops, CigarOp::Deletion(1));
if f[idx(i, j)] == h[idx(i - 1, j)] + gap_open {
state = State::H;
}
i -= 1;
}
}
}
aligned_query.reverse();
aligned_target.reverse();
cigar_ops.reverse();
Ok(AlignmentResult {
score: h[idx(m, n)],
aligned_query,
aligned_target,
query_start: 0,
query_end: m,
target_start: 0,
target_end: n,
cigar: cigar_ops,
})
}
fn push_cigar(ops: &mut Vec<CigarOp>, op: CigarOp) {
if let Some(last) = ops.last_mut() {
match (last, &op) {
(CigarOp::Match(ref mut n), CigarOp::Match(1)) => {
*n += 1;
return;
}
(CigarOp::Mismatch(ref mut n), CigarOp::Mismatch(1)) => {
*n += 1;
return;
}
(CigarOp::Insertion(ref mut n), CigarOp::Insertion(1)) => {
*n += 1;
return;
}
(CigarOp::Deletion(ref mut n), CigarOp::Deletion(1)) => {
*n += 1;
return;
}
_ => {}
}
}
ops.push(op);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::scoring::{ScoringMatrix, SubstitutionMatrix};
fn dna_scheme() -> ScoringScheme {
ScoringScheme::Simple(ScoringMatrix::dna_default())
}
#[test]
fn identical_sequences() {
let result = needleman_wunsch(b"ACGT", b"ACGT", &dna_scheme()).unwrap();
assert_eq!(result.score, 8);
assert_eq!(result.aligned_query, b"ACGT");
assert_eq!(result.aligned_target, b"ACGT");
assert_eq!(result.cigar_string(), "4=");
assert!((result.identity() - 1.0).abs() < f64::EPSILON);
}
#[test]
fn single_mismatch() {
let result = needleman_wunsch(b"ACGT", b"ACAT", &dna_scheme()).unwrap();
assert_eq!(result.score, 5);
assert_eq!(result.matches(), 3);
assert_eq!(result.mismatches(), 1);
}
#[test]
fn gap_insertion() {
let result = needleman_wunsch(b"ACGT", b"ACT", &dna_scheme()).unwrap();
assert!(result.gaps() > 0, "expected at least one gap");
assert_eq!(result.query_start, 0);
assert_eq!(result.query_end, 4);
assert_eq!(result.target_start, 0);
assert_eq!(result.target_end, 3);
}
#[test]
fn protein_with_blosum62() {
let scheme = ScoringScheme::Substitution(SubstitutionMatrix::blosum62());
let result = needleman_wunsch(b"HEAGAWGHEE", b"PAWHEAE", &scheme).unwrap();
assert!(result.score > 0, "expected positive score for related peptides");
assert!(result.length() > 0);
}
#[test]
fn empty_sequence_errors() {
assert!(needleman_wunsch(b"", b"ACGT", &dna_scheme()).is_err());
assert!(needleman_wunsch(b"ACGT", b"", &dna_scheme()).is_err());
}
#[test]
fn single_base() {
let result = needleman_wunsch(b"A", b"A", &dna_scheme()).unwrap();
assert_eq!(result.score, 2);
assert_eq!(result.cigar_string(), "1=");
}
#[test]
fn completely_different() {
let result = needleman_wunsch(b"AAAA", b"TTTT", &dna_scheme()).unwrap();
assert_eq!(result.score, -4);
assert_eq!(result.mismatches(), 4);
}
}