bio 3.0.0

A bioinformatics library for Rust. This library provides implementations of many algorithms and data structures that are useful for bioinformatics, but also in other fields.
Documentation
use super::lookup;
use std::sync::LazyLock;

// taken from https://github.com/seqan/seqan/blob/master/include%2Fseqan%2Fscore%2Fscore_matrix_data.h
static MAT: LazyLock<[i32; 27 * 27]> = LazyLock::new(|| {
    [
        5, -1, -1, -2, -1, -2, 0, -2, -1, -1, -1, -1, -1, -1, 0, -1, -1, -2, 1, 0, 0, 0, -2, -2,
        -1, 0, -5, -1, 4, -2, 5, 1, -3, -1, 0, -3, -3, 0, -3, -2, 4, -1, -2, 0, -1, 0, 0, -1, -3,
        -4, -2, 2, -1, -5, -1, -2, 12, -3, -3, -2, -3, -3, -3, -3, -3, -2, -2, -2, -2, -4, -3, -3,
        -1, -1, -2, -1, -5, -3, -3, -2, -5, -2, 5, -3, 7, 2, -4, -1, 0, -4, -4, 0, -3, -3, 2, -1,
        -1, 0, -1, 0, -1, -1, -3, -4, -2, 1, -1, -5, -1, 1, -3, 2, 6, -3, -2, 0, -3, -3, 1, -2, -2,
        0, -1, 0, 2, 0, 0, -1, -1, -3, -3, -2, 4, -1, -5, -2, -3, -2, -4, -3, 8, -3, -2, 0, 1, -3,
        1, 0, -2, -1, -3, -4, -2, -2, -1, -1, 0, 1, 3, -3, -1, -5, 0, -1, -3, -1, -2, -3, 7, -2,
        -4, -4, -2, -3, -2, 0, -1, -2, -2, -2, 0, -2, -1, -3, -2, -3, -2, -1, -5, -2, 0, -3, 0, 0,
        -2, -2, 10, -3, -3, -1, -2, 0, 1, -1, -2, 1, 0, -1, -2, -1, -3, -3, 2, 0, -1, -5, -1, -3,
        -3, -4, -3, 0, -4, -3, 5, 4, -3, 2, 2, -2, -1, -2, -2, -3, -2, -1, -1, 3, -2, 0, -3, -1,
        -5, -1, -3, -3, -4, -3, 1, -4, -3, 4, 4, -3, 4, 2, -3, -1, -3, -2, -3, -3, -1, -1, 2, -2,
        0, -3, -1, -5, -1, 0, -3, 0, 1, -3, -2, -1, -3, -3, 5, -3, -1, 0, -1, -1, 1, 3, -1, -1, -1,
        -2, -2, -1, 1, -1, -5, -1, -3, -2, -3, -2, 1, -3, -2, 2, 4, -3, 5, 2, -3, -1, -3, -2, -2,
        -3, -1, -1, 1, -2, 0, -2, -1, -5, -1, -2, -2, -3, -2, 0, -2, 0, 2, 2, -1, 2, 6, -2, -1, -2,
        0, -1, -2, -1, -1, 1, -2, 0, -1, -1, -5, -1, 4, -2, 2, 0, -2, 0, 1, -2, -3, 0, -3, -2, 6,
        -1, -2, 0, 0, 1, 0, -1, -3, -4, -2, 0, -1, -5, 0, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, 0, 0, -1, -1, -2, -1, -1, -1, -5, -1, -2, -4, -1, 0, -3, -2,
        -2, -2, -3, -1, -3, -2, -2, -1, 9, -1, -2, -1, -1, -1, -3, -3, -3, -1, -1, -5, -1, 0, -3,
        0, 2, -4, -2, 1, -2, -2, 1, -2, 0, 0, -1, -1, 6, 1, 0, -1, -1, -3, -2, -1, 4, -1, -5, -2,
        -1, -3, -1, 0, -2, -2, 0, -3, -3, 3, -2, -1, 0, -1, -2, 1, 7, -1, -1, -1, -2, -2, -1, 0,
        -1, -5, 1, 0, -1, 0, 0, -2, 0, -1, -2, -3, -1, -3, -2, 1, 0, -1, 0, -1, 4, 2, 0, -1, -4,
        -2, 0, 0, -5, 0, 0, -1, -1, -1, -1, -2, -2, -1, -1, -1, -1, -1, 0, 0, -1, -1, -1, 2, 5, 0,
        0, -3, -1, -1, 0, -5, 0, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, 0, 0, -1, -1, -2, -1, -1, -1, -5, 0, -3, -1, -3, -3, 0, -3, -3, 3, 2, -2, 1, 1, -3, -1,
        -3, -3, -2, -1, 0, -1, 5, -3, -1, -3, -1, -5, -2, -4, -5, -4, -3, 1, -2, -3, -2, -2, -2,
        -2, -2, -4, -2, -3, -2, -2, -4, -3, -2, -3, 15, 3, -2, -2, -5, -2, -2, -3, -2, -2, 3, -3,
        2, 0, 0, -1, 0, 0, -2, -1, -3, -1, -1, -2, -1, -1, -1, 3, 8, -2, -1, -5, -1, 2, -3, 1, 4,
        -3, -2, 0, -3, -3, 1, -2, -1, 0, -1, -1, 4, 0, 0, -1, -1, -3, -2, -2, 4, -1, -5, 0, -1, -2,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, -1, -1, -2, -1, -1, -1,
        -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
        -5, -5, -5, -5, 1,
    ]
});

/// Return the BLOSUM45 substitution matrix score of [a, b]
///
/// # Example
///
/// ```
/// use bio::scores::blosum45;
/// assert_eq!(blosum45(b'H', b'A'), -2);
/// ```
pub fn blosum45(a: u8, b: u8) -> i32 {
    let a = lookup(a);
    let b = lookup(b);
    MAT[27 * a + b] // a = column index, b = row index
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_blosum45() {
        let score1 = blosum45(b'H', b'H');
        assert_eq!(score1, 10);
        let score2 = blosum45(b'O', b'*');
        assert_eq!(score2, -5);
        let score3 = blosum45(b'A', b'*');
        assert_eq!(score3, -5);
        let score4 = blosum45(b'*', b'*');
        assert_eq!(score4, 1);
        let score5 = blosum45(b'X', b'X');
        assert_eq!(score5, -1);
        let score6 = blosum45(b'X', b'Z');
        assert_eq!(score6, -1);
    }
}