use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AlignmentMode {
Local,
Global,
SemiGlobal,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CigarOp {
Match(usize),
Mismatch(usize),
Insertion(usize),
Deletion(usize),
AlnMatch(usize),
Skip(usize),
SoftClip(usize),
HardClip(usize),
Padding(usize),
}
impl CigarOp {
pub fn code(&self) -> char {
match self {
CigarOp::Match(_) => '=',
CigarOp::Mismatch(_) => 'X',
CigarOp::Insertion(_) => 'I',
CigarOp::Deletion(_) => 'D',
CigarOp::AlnMatch(_) => 'M',
CigarOp::Skip(_) => 'N',
CigarOp::SoftClip(_) => 'S',
CigarOp::HardClip(_) => 'H',
CigarOp::Padding(_) => 'P',
}
}
pub fn len(&self) -> usize {
match self {
CigarOp::Match(n)
| CigarOp::Mismatch(n)
| CigarOp::Insertion(n)
| CigarOp::Deletion(n)
| CigarOp::AlnMatch(n)
| CigarOp::Skip(n)
| CigarOp::SoftClip(n)
| CigarOp::HardClip(n)
| CigarOp::Padding(n) => *n,
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn consumes_reference(&self) -> bool {
matches!(
self,
CigarOp::Match(_)
| CigarOp::Mismatch(_)
| CigarOp::Deletion(_)
| CigarOp::AlnMatch(_)
| CigarOp::Skip(_)
)
}
pub fn consumes_query(&self) -> bool {
matches!(
self,
CigarOp::Match(_)
| CigarOp::Mismatch(_)
| CigarOp::Insertion(_)
| CigarOp::AlnMatch(_)
| CigarOp::SoftClip(_)
)
}
}
impl fmt::Display for CigarOp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{}", self.len(), self.code())
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AlignmentResult {
pub score: i32,
pub aligned_query: Vec<u8>,
pub aligned_target: Vec<u8>,
pub query_start: usize,
pub query_end: usize,
pub target_start: usize,
pub target_end: usize,
pub cigar: Vec<CigarOp>,
}
impl AlignmentResult {
pub fn cigar_string(&self) -> String {
let mut s = String::new();
for op in &self.cigar {
s.push_str(&format!("{}{}", op.len(), op.code()));
}
s
}
pub fn identity(&self) -> f64 {
let total = self.length();
if total == 0 {
return 0.0;
}
self.matches() as f64 / total as f64
}
pub fn matches(&self) -> usize {
self.cigar
.iter()
.filter_map(|op| match op {
CigarOp::Match(n) => Some(n),
_ => None,
})
.sum()
}
pub fn mismatches(&self) -> usize {
self.cigar
.iter()
.filter_map(|op| match op {
CigarOp::Mismatch(n) => Some(n),
_ => None,
})
.sum()
}
pub fn gaps(&self) -> usize {
self.cigar
.iter()
.filter_map(|op| match op {
CigarOp::Insertion(n) | CigarOp::Deletion(n) => Some(n),
_ => None,
})
.sum()
}
pub fn length(&self) -> usize {
self.cigar
.iter()
.filter(|op| matches!(
op,
CigarOp::Match(_)
| CigarOp::Mismatch(_)
| CigarOp::Insertion(_)
| CigarOp::Deletion(_)
| CigarOp::AlnMatch(_)
))
.map(|op| op.len())
.sum()
}
}
impl cyanea_core::Scored for AlignmentResult {
fn score(&self) -> f64 {
self.score as f64
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cigar_string_formatting() {
let result = AlignmentResult {
score: 10,
aligned_query: b"ACGT".to_vec(),
aligned_target: b"ACGT".to_vec(),
query_start: 0,
query_end: 4,
target_start: 0,
target_end: 4,
cigar: vec![CigarOp::Match(4)],
};
assert_eq!(result.cigar_string(), "4=");
}
#[test]
fn cigar_string_mixed_ops() {
let result = AlignmentResult {
score: 5,
aligned_query: Vec::new(),
aligned_target: Vec::new(),
query_start: 0,
query_end: 0,
target_start: 0,
target_end: 0,
cigar: vec![
CigarOp::Match(3),
CigarOp::Insertion(1),
CigarOp::Match(2),
CigarOp::Deletion(1),
CigarOp::Mismatch(1),
],
};
assert_eq!(result.cigar_string(), "3=1I2=1D1X");
}
#[test]
fn identity_perfect_match() {
let result = AlignmentResult {
score: 8,
aligned_query: b"ACGT".to_vec(),
aligned_target: b"ACGT".to_vec(),
query_start: 0,
query_end: 4,
target_start: 0,
target_end: 4,
cigar: vec![CigarOp::Match(4)],
};
assert!((result.identity() - 1.0).abs() < f64::EPSILON);
}
#[test]
fn identity_with_mismatches_and_gaps() {
let result = AlignmentResult {
score: 0,
aligned_query: Vec::new(),
aligned_target: Vec::new(),
query_start: 0,
query_end: 0,
target_start: 0,
target_end: 0,
cigar: vec![
CigarOp::Match(3),
CigarOp::Mismatch(1),
CigarOp::Insertion(1),
],
};
assert!((result.identity() - 0.6).abs() < f64::EPSILON);
}
#[test]
fn identity_empty_alignment() {
let result = AlignmentResult {
score: 0,
aligned_query: Vec::new(),
aligned_target: Vec::new(),
query_start: 0,
query_end: 0,
target_start: 0,
target_end: 0,
cigar: vec![],
};
assert!((result.identity() - 0.0).abs() < f64::EPSILON);
}
#[test]
fn gap_count() {
let result = AlignmentResult {
score: 0,
aligned_query: Vec::new(),
aligned_target: Vec::new(),
query_start: 0,
query_end: 0,
target_start: 0,
target_end: 0,
cigar: vec![
CigarOp::Match(2),
CigarOp::Insertion(3),
CigarOp::Match(1),
CigarOp::Deletion(2),
],
};
assert_eq!(result.gaps(), 5);
assert_eq!(result.matches(), 3);
assert_eq!(result.length(), 8);
}
#[test]
fn scored_trait() {
use cyanea_core::Scored;
let result = AlignmentResult {
score: 42,
aligned_query: Vec::new(),
aligned_target: Vec::new(),
query_start: 0,
query_end: 0,
target_start: 0,
target_end: 0,
cigar: vec![],
};
assert!((result.score() - 42.0).abs() < f64::EPSILON);
}
#[test]
fn cigar_op_display() {
assert_eq!(format!("{}", CigarOp::Match(5)), "5=");
assert_eq!(format!("{}", CigarOp::Mismatch(2)), "2X");
assert_eq!(format!("{}", CigarOp::Insertion(1)), "1I");
assert_eq!(format!("{}", CigarOp::Deletion(3)), "3D");
assert_eq!(format!("{}", CigarOp::AlnMatch(10)), "10M");
assert_eq!(format!("{}", CigarOp::Skip(100)), "100N");
assert_eq!(format!("{}", CigarOp::SoftClip(4)), "4S");
assert_eq!(format!("{}", CigarOp::HardClip(2)), "2H");
assert_eq!(format!("{}", CigarOp::Padding(1)), "1P");
}
#[test]
fn consumes_reference_and_query() {
assert!(CigarOp::Match(1).consumes_reference());
assert!(CigarOp::Match(1).consumes_query());
assert!(CigarOp::Mismatch(1).consumes_reference());
assert!(CigarOp::Mismatch(1).consumes_query());
assert!(!CigarOp::Insertion(1).consumes_reference());
assert!(CigarOp::Insertion(1).consumes_query());
assert!(CigarOp::Deletion(1).consumes_reference());
assert!(!CigarOp::Deletion(1).consumes_query());
assert!(CigarOp::AlnMatch(1).consumes_reference());
assert!(CigarOp::AlnMatch(1).consumes_query());
assert!(CigarOp::Skip(1).consumes_reference());
assert!(!CigarOp::Skip(1).consumes_query());
assert!(!CigarOp::SoftClip(1).consumes_reference());
assert!(CigarOp::SoftClip(1).consumes_query());
assert!(!CigarOp::HardClip(1).consumes_reference());
assert!(!CigarOp::HardClip(1).consumes_query());
assert!(!CigarOp::Padding(1).consumes_reference());
assert!(!CigarOp::Padding(1).consumes_query());
}
}