Skip to main content

cyanea_align/
scoring.rs

1//! Scoring schemes for pairwise sequence alignment.
2//!
3//! Provides simple match/mismatch scoring for nucleotides ([`ScoringMatrix`]),
4//! amino acid substitution matrices ([`SubstitutionMatrix`]) with BLOSUM and PAM
5//! variants, and a unified [`ScoringScheme`] enum that the alignment algorithms accept.
6
7use cyanea_core::{CyaneaError, Result};
8
9// ---------------------------------------------------------------------------
10// Simple scoring (nucleotides)
11// ---------------------------------------------------------------------------
12
13/// A simple match/mismatch scoring matrix with affine gap penalties.
14///
15/// Suitable for nucleotide alignments where all matches score the same
16/// and all mismatches score the same.
17#[derive(Debug, Clone)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19pub struct ScoringMatrix {
20    pub match_score: i32,
21    pub mismatch_score: i32,
22    pub gap_open: i32,
23    pub gap_extend: i32,
24}
25
26impl ScoringMatrix {
27    /// Create a new scoring matrix.
28    ///
29    /// # Errors
30    ///
31    /// Returns an error if `match_score` is not positive, `mismatch_score` is not
32    /// negative, or `gap_open`/`gap_extend` are not negative.
33    pub fn new(
34        match_score: i32,
35        mismatch_score: i32,
36        gap_open: i32,
37        gap_extend: i32,
38    ) -> Result<Self> {
39        if match_score <= 0 {
40            return Err(CyaneaError::InvalidInput(
41                "match_score must be positive".into(),
42            ));
43        }
44        if mismatch_score >= 0 {
45            return Err(CyaneaError::InvalidInput(
46                "mismatch_score must be negative".into(),
47            ));
48        }
49        if gap_open >= 0 {
50            return Err(CyaneaError::InvalidInput(
51                "gap_open must be negative".into(),
52            ));
53        }
54        if gap_extend >= 0 {
55            return Err(CyaneaError::InvalidInput(
56                "gap_extend must be negative".into(),
57            ));
58        }
59        Ok(Self {
60            match_score,
61            mismatch_score,
62            gap_open,
63            gap_extend,
64        })
65    }
66
67    /// Default scoring for DNA alignment: +2 match, -1 mismatch, -5 gap open, -2 gap extend.
68    pub fn dna_default() -> Self {
69        Self {
70            match_score: 2,
71            mismatch_score: -1,
72            gap_open: -5,
73            gap_extend: -2,
74        }
75    }
76
77    /// Score a pair of bases. Case-insensitive.
78    pub fn score_pair(&self, a: u8, b: u8) -> i32 {
79        if a.to_ascii_uppercase() == b.to_ascii_uppercase() {
80            self.match_score
81        } else {
82            self.mismatch_score
83        }
84    }
85}
86
87// ---------------------------------------------------------------------------
88// Amino acid index mapping
89// ---------------------------------------------------------------------------
90
91/// Maps an amino acid letter to a 0-based index in substitution matrices.
92///
93/// Standard 20 amino acids + B (Asx), Z (Glx), X (unknown), * (stop).
94/// Returns `None` for unrecognized characters.
95fn aa_to_index(aa: u8) -> Option<usize> {
96    match aa.to_ascii_uppercase() {
97        b'A' => Some(0),
98        b'R' => Some(1),
99        b'N' => Some(2),
100        b'D' => Some(3),
101        b'C' => Some(4),
102        b'Q' => Some(5),
103        b'E' => Some(6),
104        b'G' => Some(7),
105        b'H' => Some(8),
106        b'I' => Some(9),
107        b'L' => Some(10),
108        b'K' => Some(11),
109        b'M' => Some(12),
110        b'F' => Some(13),
111        b'P' => Some(14),
112        b'S' => Some(15),
113        b'T' => Some(16),
114        b'W' => Some(17),
115        b'Y' => Some(18),
116        b'V' => Some(19),
117        b'B' => Some(20),
118        b'Z' => Some(21),
119        b'X' => Some(22),
120        b'*' => Some(23),
121        _ => None,
122    }
123}
124
125/// Matrix dimension: 24 amino acid symbols.
126const AA_DIM: usize = 24;
127
128// ---------------------------------------------------------------------------
129// Substitution matrices (protein)
130// ---------------------------------------------------------------------------
131
132/// An amino acid substitution matrix with affine gap penalties.
133///
134/// Stores a 24x24 lookup table covering the 20 standard amino acids plus
135/// B (Asx), Z (Glx), X (unknown), and * (stop codon).
136#[derive(Debug, Clone)]
137#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
138pub struct SubstitutionMatrix {
139    /// 24x24 flattened score table (row-major).
140    scores: Vec<i32>,
141    pub gap_open: i32,
142    pub gap_extend: i32,
143    name: &'static str,
144}
145
146impl SubstitutionMatrix {
147    /// Score a pair of amino acids. Case-insensitive.
148    ///
149    /// Returns the worst score in the matrix for unrecognised residues.
150    pub fn score_pair(&self, a: u8, b: u8) -> i32 {
151        match (aa_to_index(a), aa_to_index(b)) {
152            (Some(i), Some(j)) => self.scores[i * AA_DIM + j],
153            _ => self.worst_score(),
154        }
155    }
156
157    fn worst_score(&self) -> i32 {
158        self.scores.iter().copied().min().unwrap_or(-4)
159    }
160
161    /// Matrix name (e.g. "BLOSUM62").
162    pub fn name(&self) -> &str {
163        self.name
164    }
165
166    // -----------------------------------------------------------------------
167    // Standard matrices — values from NCBI
168    // Order: A R N D C Q E G H I L K M F P S T W Y V B Z X *
169    // -----------------------------------------------------------------------
170
171    /// BLOSUM62 substitution matrix. Gap penalties: -11 open, -1 extend.
172    pub fn blosum62() -> Self {
173        Self {
174            scores: BLOSUM62.to_vec(),
175            gap_open: -11,
176            gap_extend: -1,
177            name: "BLOSUM62",
178        }
179    }
180
181    /// BLOSUM45 substitution matrix. Gap penalties: -13 open, -3 extend.
182    pub fn blosum45() -> Self {
183        Self {
184            scores: BLOSUM45.to_vec(),
185            gap_open: -13,
186            gap_extend: -3,
187            name: "BLOSUM45",
188        }
189    }
190
191    /// BLOSUM80 substitution matrix. Gap penalties: -10 open, -1 extend.
192    pub fn blosum80() -> Self {
193        Self {
194            scores: BLOSUM80.to_vec(),
195            gap_open: -10,
196            gap_extend: -1,
197            name: "BLOSUM80",
198        }
199    }
200
201    /// PAM250 substitution matrix. Gap penalties: -11 open, -1 extend.
202    pub fn pam250() -> Self {
203        Self {
204            scores: PAM250.to_vec(),
205            gap_open: -11,
206            gap_extend: -1,
207            name: "PAM250",
208        }
209    }
210
211    /// PAM40 substitution matrix. Gap penalties: -10 open, -2 extend.
212    pub fn pam40() -> Self {
213        Self {
214            scores: PAM40.to_vec(),
215            gap_open: -10,
216            gap_extend: -2,
217            name: "PAM40",
218        }
219    }
220
221    /// PAM120 substitution matrix. Gap penalties: -11 open, -1 extend.
222    pub fn pam120() -> Self {
223        Self {
224            scores: PAM120.to_vec(),
225            gap_open: -11,
226            gap_extend: -1,
227            name: "PAM120",
228        }
229    }
230
231    /// PAM200 substitution matrix. Gap penalties: -11 open, -1 extend.
232    pub fn pam200() -> Self {
233        Self {
234            scores: PAM200.to_vec(),
235            gap_open: -11,
236            gap_extend: -1,
237            name: "PAM200",
238        }
239    }
240
241    /// BLOSUM30 substitution matrix. Gap penalties: -14 open, -4 extend.
242    pub fn blosum30() -> Self {
243        Self {
244            scores: BLOSUM30.to_vec(),
245            gap_open: -14,
246            gap_extend: -4,
247            name: "BLOSUM30",
248        }
249    }
250}
251
252// ---------------------------------------------------------------------------
253// Unified scoring scheme
254// ---------------------------------------------------------------------------
255
256/// A unified scoring scheme accepted by alignment algorithms.
257#[derive(Debug, Clone)]
258pub enum ScoringScheme {
259    /// Simple match/mismatch scoring (typically for nucleotides).
260    Simple(ScoringMatrix),
261    /// Amino acid substitution matrix (BLOSUM, PAM, etc.).
262    Substitution(SubstitutionMatrix),
263}
264
265impl ScoringScheme {
266    /// Score a pair of residues under this scheme.
267    pub fn score_pair(&self, a: u8, b: u8) -> i32 {
268        match self {
269            ScoringScheme::Simple(m) => m.score_pair(a, b),
270            ScoringScheme::Substitution(m) => m.score_pair(a, b),
271        }
272    }
273
274    /// Gap opening penalty (negative).
275    pub fn gap_open(&self) -> i32 {
276        match self {
277            ScoringScheme::Simple(m) => m.gap_open,
278            ScoringScheme::Substitution(m) => m.gap_open,
279        }
280    }
281
282    /// Gap extension penalty (negative).
283    pub fn gap_extend(&self) -> i32 {
284        match self {
285            ScoringScheme::Simple(m) => m.gap_extend,
286            ScoringScheme::Substitution(m) => m.gap_extend,
287        }
288    }
289}
290
291impl From<ScoringMatrix> for ScoringScheme {
292    fn from(m: ScoringMatrix) -> Self {
293        ScoringScheme::Simple(m)
294    }
295}
296
297impl From<SubstitutionMatrix> for ScoringScheme {
298    fn from(m: SubstitutionMatrix) -> Self {
299        ScoringScheme::Substitution(m)
300    }
301}
302
303// ===========================================================================
304// NCBI substitution matrix data
305// Row/column order: A R N D C Q E G H I L K M F P S T W Y V B Z X *
306// ===========================================================================
307
308/// BLOSUM62 — 24x24 flattened, NCBI reference.
309#[rustfmt::skip]
310const BLOSUM62: [i32; AA_DIM * AA_DIM] = [
311//   A   R   N   D   C   Q   E   G   H   I   L   K   M   F   P   S   T   W   Y   V   B   Z   X   *
312     4, -1, -2, -2,  0, -1, -1,  0, -2, -1, -1, -1, -1, -2, -1,  1,  0, -3, -2,  0, -2, -1,  0, -4, // A
313    -1,  5,  0, -2, -3,  1,  0, -2,  0, -3, -2,  2, -1, -3, -2, -1, -1, -3, -2, -3, -1,  0, -1, -4, // R
314    -2,  0,  6,  1, -3,  0,  0,  0,  1, -3, -3,  0, -2, -3, -2,  1,  0, -4, -2, -3,  3,  0, -1, -4, // N
315    -2, -2,  1,  6, -3,  0,  2, -1, -1, -3, -4, -1, -3, -3, -1,  0, -1, -4, -3, -3,  4,  1, -1, -4, // D
316     0, -3, -3, -3,  9, -3, -4, -3, -3, -1, -1, -3, -1, -2, -3, -1, -1, -2, -2, -1, -3, -3, -2, -4, // C
317    -1,  1,  0,  0, -3,  5,  2, -2,  0, -3, -2,  1,  0, -3, -1,  0, -1, -2, -1, -2,  0,  3, -1, -4, // Q
318    -1,  0,  0,  2, -4,  2,  5, -2,  0, -3, -3,  1, -2, -3, -1,  0, -1, -3, -2, -2,  1,  4, -1, -4, // E
319     0, -2,  0, -1, -3, -2, -2,  6, -2, -4, -4, -2, -3, -3, -2,  0, -2, -2, -3, -3, -1, -2, -1, -4, // G
320    -2,  0,  1, -1, -3,  0,  0, -2,  8, -3, -3, -1, -2, -1, -2, -1, -2, -2,  2, -3,  0,  0, -1, -4, // H
321    -1, -3, -3, -3, -1, -3, -3, -4, -3,  4,  2, -3,  1,  0, -3, -2, -1, -3, -1,  3, -3, -3, -1, -4, // I
322    -1, -2, -3, -4, -1, -2, -3, -4, -3,  2,  4, -2,  2,  0, -3, -2, -1, -2, -1,  1, -4, -3, -1, -4, // L
323    -1,  2,  0, -1, -3,  1,  1, -2, -1, -3, -2,  5, -1, -3, -1,  0, -1, -3, -2, -2,  0,  1, -1, -4, // K
324    -1, -1, -2, -3, -1,  0, -2, -3, -2,  1,  2, -1,  5,  0, -2, -1, -1, -1, -1,  1, -3, -1, -1, -4, // M
325    -2, -3, -3, -3, -2, -3, -3, -3, -1,  0,  0, -3,  0,  6, -4, -2, -2,  1,  3, -1, -3, -3, -1, -4, // F
326    -1, -2, -2, -1, -3, -1, -1, -2, -2, -3, -3, -1, -2, -4,  7, -1, -1, -4, -3, -2, -2, -1, -2, -4, // P
327     1, -1,  1,  0, -1,  0,  0,  0, -1, -2, -2,  0, -1, -2, -1,  4,  1, -3, -2, -2,  0,  0,  0, -4, // S
328     0, -1,  0, -1, -1, -1, -1, -2, -2, -1, -1, -1, -1, -2, -1,  1,  5, -2, -2,  0, -1, -1,  0, -4, // T
329    -3, -3, -4, -4, -2, -2, -3, -2, -2, -3, -2, -3, -1,  1, -4, -3, -2, 11,  2, -3, -4, -3, -2, -4, // W
330    -2, -2, -2, -3, -2, -1, -2, -3,  2, -1, -1, -2, -1,  3, -3, -2, -2,  2,  7, -1, -3, -2, -1, -4, // Y
331     0, -3, -3, -3, -1, -2, -2, -3, -3,  3,  1, -2,  1, -1, -2, -2,  0, -3, -1,  4, -3, -2, -1, -4, // V
332    -2, -1,  3,  4, -3,  0,  1, -1,  0, -3, -4,  0, -3, -3, -2,  0, -1, -4, -3, -3,  4,  1, -1, -4, // B
333    -1,  0,  0,  1, -3,  3,  4, -2,  0, -3, -3,  1, -1, -3, -1,  0, -1, -3, -2, -2,  1,  4, -1, -4, // Z
334     0, -1, -1, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2,  0,  0, -2, -1, -1, -1, -1, -1, -4, // X
335    -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,  1, // *
336];
337
338/// BLOSUM45 — 24x24 flattened, NCBI reference.
339#[rustfmt::skip]
340const BLOSUM45: [i32; AA_DIM * AA_DIM] = [
341//   A   R   N   D   C   Q   E   G   H   I   L   K   M   F   P   S   T   W   Y   V   B   Z   X   *
342     5, -2, -1, -2, -1, -1, -1,  0, -2, -1, -1, -1, -1, -2, -1,  1,  0, -2, -2,  0, -1, -1,  0, -5, // A
343    -2,  7,  0, -1, -3,  1,  0, -2,  0, -3, -2,  3, -1, -2, -2, -1, -1, -2, -1, -2, -1,  0, -1, -5, // R
344    -1,  0,  6,  2, -2,  0,  0,  0,  1, -2, -3,  0, -2, -2, -2,  1,  0, -4, -2, -3,  4,  0, -1, -5, // N
345    -2, -1,  2,  7, -3,  0,  2, -1,  0, -4, -3,  0, -3, -4, -1,  0, -1, -4, -2, -3,  5,  1, -1, -5, // D
346    -1, -3, -2, -3, 12, -3, -3, -3, -3, -3, -2, -3, -2, -2, -4, -1, -1, -5, -3, -1, -2, -3, -2, -5, // C
347    -1,  1,  0,  0, -3,  6,  2, -2,  1, -2, -2,  1,  0, -4, -1,  0, -1, -2, -1, -3,  0,  4, -1, -5, // Q
348    -1,  0,  0,  2, -3,  2,  6, -2,  0, -3, -2,  1, -2, -3,  0,  0, -1, -3, -2, -3,  1,  4, -1, -5, // E
349     0, -2,  0, -1, -3, -2, -2,  7, -2, -4, -3, -2, -2, -3, -2,  0, -2, -2, -3, -3, -1, -2, -1, -5, // G
350    -2,  0,  1,  0, -3,  1,  0, -2, 10, -3, -2, -1,  0, -2, -2, -1, -2, -3,  2, -3,  0,  0, -1, -5, // H
351    -1, -3, -2, -4, -3, -2, -3, -4, -3,  5,  2, -3,  2,  0, -2, -2, -1, -2,  0,  3, -3, -3, -1, -5, // I
352    -1, -2, -3, -3, -2, -2, -2, -3, -2,  2,  5, -3,  2,  1, -3, -3, -1, -2,  0,  1, -3, -2, -1, -5, // L
353    -1,  3,  0,  0, -3,  1,  1, -2, -1, -3, -3,  5, -1, -3, -1, -1, -1, -2, -1, -2,  0,  1, -1, -5, // K
354    -1, -1, -2, -3, -2,  0, -2, -2,  0,  2,  2, -1,  6,  0, -2, -2, -1, -2,  0,  1, -2, -1, -1, -5, // M
355    -2, -2, -2, -4, -2, -4, -3, -3, -2,  0,  1, -3,  0,  8, -3, -2, -1,  1,  3,  0, -3, -3, -1, -5, // F
356    -1, -2, -2, -1, -4, -1,  0, -2, -2, -2, -3, -1, -2, -3,  9, -1, -1, -3, -3, -3, -2, -1, -1, -5, // P
357     1, -1,  1,  0, -1,  0,  0,  0, -1, -2, -3, -1, -2, -2, -1,  4,  2, -4, -2, -1,  0,  0,  0, -5, // S
358     0, -1,  0, -1, -1, -1, -1, -2, -2, -1, -1, -1, -1, -1, -1,  2,  5, -3, -1,  0,  0, -1,  0, -5, // T
359    -2, -2, -4, -4, -5, -2, -3, -2, -3, -2, -2, -2, -2,  1, -3, -4, -3, 15,  3, -3, -4, -2, -2, -5, // W
360    -2, -1, -2, -2, -3, -1, -2, -3,  2,  0,  0, -1,  0,  3, -3, -2, -1,  3,  8, -1, -2, -2, -1, -5, // Y
361     0, -2, -3, -3, -1, -3, -3, -3, -3,  3,  1, -2,  1,  0, -3, -1,  0, -3, -1,  5, -3, -3, -1, -5, // V
362    -1, -1,  4,  5, -2,  0,  1, -1,  0, -3, -3,  0, -2, -3, -2,  0,  0, -4, -2, -3,  4,  2, -1, -5, // B
363    -1,  0,  0,  1, -3,  4,  4, -2,  0, -3, -2,  1, -1, -3, -1,  0, -1, -2, -2, -3,  2,  4, -1, -5, // Z
364     0, -1, -1, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  0,  0, -2, -1, -1, -1, -1, -1, -5, // X
365    -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,  1, // *
366];
367
368/// BLOSUM80 — 24x24 flattened, NCBI reference.
369#[rustfmt::skip]
370const BLOSUM80: [i32; AA_DIM * AA_DIM] = [
371//   A   R   N   D   C   Q   E   G   H   I   L   K   M   F   P   S   T   W   Y   V   B   Z   X   *
372     7, -3, -3, -3, -1, -2, -2,  0, -3, -3, -3, -1, -2, -4, -1,  2,  0, -5, -4, -1, -3, -2, -1, -8, // A
373    -3,  9, -1, -3, -6,  1, -1, -4,  0, -5, -4,  3, -3, -5, -3, -2, -2, -5, -4, -4, -2,  0, -2, -8, // R
374    -3, -1,  9,  2, -5,  0, -1, -1,  1, -6, -6,  0, -4, -6, -4,  1,  0, -7, -4, -5,  5,  0, -2, -8, // N
375    -3, -3,  2, 10, -7, -1,  2, -3, -2, -7, -7, -2, -6, -6, -3, -1, -2, -8, -6, -6,  6,  1, -3, -8, // D
376    -1, -6, -5, -7, 13, -5, -7, -6, -7, -2, -3, -6, -3, -4, -6, -2, -2, -5, -5, -2, -6, -7, -4, -8, // C
377    -2,  1,  0, -1, -5,  9,  3, -4,  1, -5, -4,  2, -1, -5, -3, -1, -1, -4, -3, -4, -1,  5, -2, -8, // Q
378    -2, -1, -1,  2, -7,  3,  8, -4,  0, -6, -6,  1, -4, -6, -2,  0, -2, -6, -5, -4,  1,  6, -2, -8, // E
379     0, -4, -1, -3, -6, -4, -4,  9, -4, -7, -7, -3, -5, -6, -5, -1, -3, -6, -6, -6, -2, -4, -3, -8, // G
380    -3,  0,  1, -2, -7,  1,  0, -4, 12, -6, -5, -1, -4, -2, -4, -2, -3, -4,  3, -5, -1,  0, -2, -8, // H
381    -3, -5, -6, -7, -2, -5, -6, -7, -6,  7,  2, -5,  2, -1, -5, -4, -2, -5, -3,  4, -6, -6, -2, -8, // I
382    -3, -4, -6, -7, -3, -4, -6, -7, -5,  2,  6, -4,  3,  0, -5, -4, -3, -4, -2,  1, -7, -5, -2, -8, // L
383    -1,  3,  0, -2, -6,  2,  1, -3, -1, -5, -4,  8, -3, -5, -2, -1, -1, -6, -4, -4, -1,  1, -2, -8, // K
384    -2, -3, -4, -6, -3, -1, -4, -5, -4,  2,  3, -3,  9, -1, -4, -3, -1, -3, -3,  1, -5, -3, -2, -8, // M
385    -4, -5, -6, -6, -4, -5, -6, -6, -2, -1,  0, -5, -1, 10, -6, -4, -4,  0,  4, -2, -6, -6, -3, -8, // F
386    -1, -3, -4, -3, -6, -3, -2, -5, -4, -5, -5, -2, -4, -6, 12, -2, -3, -7, -6, -4, -4, -2, -3, -8, // P
387     2, -2,  1, -1, -2, -1,  0, -1, -2, -4, -4, -1, -3, -4, -2,  7,  2, -6, -3, -3,  0, -1, -1, -8, // S
388     0, -2,  0, -2, -2, -1, -2, -3, -3, -2, -3, -1, -1, -4, -3,  2,  8, -5, -3,  0, -1, -2, -1, -8, // T
389    -5, -5, -7, -8, -5, -4, -6, -6, -4, -5, -4, -6, -3,  0, -7, -6, -5, 16,  3, -5, -8, -5, -5, -8, // W
390    -4, -4, -4, -6, -5, -3, -5, -6,  3, -3, -2, -4, -3,  4, -6, -3, -3,  3, 11, -3, -5, -4, -3, -8, // Y
391    -1, -4, -5, -6, -2, -4, -4, -6, -5,  4,  1, -4,  1, -2, -4, -3,  0, -5, -3,  7, -6, -4, -2, -8, // V
392    -3, -2,  5,  6, -6, -1,  1, -2, -1, -6, -7, -1, -5, -6, -4,  0, -1, -8, -5, -6,  6,  0, -3, -8, // B
393    -2,  0,  0,  1, -7,  5,  6, -4,  0, -6, -5,  1, -3, -6, -2, -1, -2, -5, -4, -4,  0,  6, -1, -8, // Z
394    -1, -2, -2, -3, -4, -2, -2, -3, -2, -2, -2, -2, -2, -3, -3, -1, -1, -5, -3, -2, -3, -1, -2, -8, // X
395    -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8,  1, // *
396];
397
398/// PAM250 — 24x24 flattened, NCBI/Dayhoff reference.
399#[rustfmt::skip]
400const PAM250: [i32; AA_DIM * AA_DIM] = [
401//   A   R   N   D   C   Q   E   G   H   I   L   K   M   F   P   S   T   W   Y   V   B   Z   X   *
402     2, -2,  0,  0, -2,  0,  0,  1, -1, -1, -2, -1, -1, -3,  1,  1,  1, -6, -3,  0,  0,  0,  0, -8, // A
403    -2,  6,  0, -1, -4,  1, -1, -3,  2, -2, -3,  3,  0, -4,  0,  0, -1,  2, -4, -2, -1,  0, -1, -8, // R
404     0,  0,  2,  2, -4,  1,  1,  0,  2, -2, -3,  1, -2, -3,  0,  1,  0, -4, -2, -2,  2,  1,  0, -8, // N
405     0, -1,  2,  4, -5,  2,  3,  1,  1, -2, -4,  0, -3, -6, -1,  0,  0, -7, -4, -2,  3,  3, -1, -8, // D
406    -2, -4, -4, -5, 12, -5, -5, -3, -3, -2, -6, -5, -5, -4, -3,  0, -2, -8,  0, -2, -4, -5, -3, -8, // C
407     0,  1,  1,  2, -5,  4,  2, -1,  3, -2, -2,  1, -1, -5,  0, -1, -1, -5, -4, -2,  1,  3, -1, -8, // Q
408     0, -1,  1,  3, -5,  2,  4,  0,  1, -2, -3,  0, -2, -5, -1,  0,  0, -7, -4, -2,  3,  3, -1, -8, // E
409     1, -3,  0,  1, -3, -1,  0,  5, -2, -3, -4, -2, -3, -5,  0,  1,  0, -7, -5, -1,  0,  0, -1, -8, // G
410    -1,  2,  2,  1, -3,  3,  1, -2,  6, -2, -2,  0, -2, -2,  0, -1, -1, -3,  0, -2,  1,  2, -1, -8, // H
411    -1, -2, -2, -2, -2, -2, -2, -3, -2,  5,  2, -2,  2,  1, -2, -1,  0, -5, -1,  4, -2, -2, -1, -8, // I
412    -2, -3, -3, -4, -6, -2, -3, -4, -2,  2,  6, -3,  4,  2, -3, -3, -2, -2, -1,  2, -3, -3, -1, -8, // L
413    -1,  3,  1,  0, -5,  1,  0, -2,  0, -2, -3,  5,  0, -5, -1,  0,  0, -3, -4, -2,  1,  0, -1, -8, // K
414    -1,  0, -2, -3, -5, -1, -2, -3, -2,  2,  4,  0,  6,  0, -2, -2, -1, -4, -2,  2, -2, -2, -1, -8, // M
415    -3, -4, -3, -6, -4, -5, -5, -5, -2,  1,  2, -5,  0,  9, -5, -3, -3,  0,  7, -1, -4, -5, -2, -8, // F
416     1,  0,  0, -1, -3,  0, -1,  0,  0, -2, -3, -1, -2, -5,  6,  1,  0, -6, -5, -1, -1,  0, -1, -8, // P
417     1,  0,  1,  0,  0, -1,  0,  1, -1, -1, -3,  0, -2, -3,  1,  2,  1, -2, -3, -1,  0,  0,  0, -8, // S
418     1, -1,  0,  0, -2, -1,  0,  0, -1,  0, -2,  0, -1, -3,  0,  1,  3, -5, -3,  0,  0, -1,  0, -8, // T
419    -6,  2, -4, -7, -8, -5, -7, -7, -3, -5, -2, -3, -4,  0, -6, -2, -5, 17,  0, -6, -5, -6, -4, -8, // W
420    -3, -4, -2, -4,  0, -4, -4, -5,  0, -1, -1, -4, -2,  7, -5, -3, -3,  0, 10, -2, -3, -4, -2, -8, // Y
421     0, -2, -2, -2, -2, -2, -2, -1, -2,  4,  2, -2,  2, -1, -1, -1,  0, -6, -2,  4, -2, -2, -1, -8, // V
422     0, -1,  2,  3, -4,  1,  3,  0,  1, -2, -3,  1, -2, -4, -1,  0,  0, -5, -3, -2,  3,  2, -1, -8, // B
423     0,  0,  1,  3, -5,  3,  3,  0,  2, -2, -3,  0, -2, -5,  0,  0, -1, -6, -4, -2,  2,  3, -1, -8, // Z
424     0, -1,  0, -1, -3, -1, -1, -1, -1, -1, -1, -1, -1, -2, -1,  0,  0, -4, -2, -1, -1, -1, -1, -8, // X
425    -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8,  1, // *
426];
427
428/// PAM40 — 24x24 flattened, NCBI reference.
429#[rustfmt::skip]
430const PAM40: [i32; AA_DIM * AA_DIM] = [
431//   A   R   N   D   C   Q   E   G   H   I   L   K   M   F   P   S   T   W   Y   V   B   Z   X   *
432     6, -4, -2, -1, -4, -2, -1,  0, -4, -3, -4, -4, -3, -6,  0,  1,  1, -9, -6, -1, -1, -1, -2,-11, // A
433    -4,  8, -2, -4, -6,  0, -3, -6,  1, -3, -5,  2, -2, -7, -2, -1, -4,  0, -7, -5, -3, -2, -3,-11, // R
434    -2, -2,  6,  3, -7, -1,  0, -1,  2, -4, -5,  0, -5, -6, -3,  1, -1, -6, -3, -5,  5, -1, -2,-11, // N
435    -1, -4,  3,  7, -9,  0,  4, -2, -1, -5, -8, -2, -6, -9, -4, -1, -2,-10, -7, -5,  5,  3, -3,-11, // D
436    -4, -6, -7, -9, 15, -9, -9, -6, -5, -4, -9, -9, -9, -8, -5, -1, -5,-11, -2, -4, -8, -9, -6,-11, // C
437    -2,  0, -1,  0, -9,  7,  2, -4,  2, -5, -3,  0, -2, -8, -1, -3, -3, -8, -7, -4,  0,  5, -2,-11, // Q
438    -1, -3,  0,  4, -9,  2,  7, -2, -2, -4, -6, -2, -4, -9, -3, -2, -3,-11, -6, -4,  3,  5, -3,-11, // E
439     0, -6, -1, -2, -6, -4, -2,  7, -5, -6, -7, -4, -5, -7, -3,  0, -3,-10, -8, -4, -1, -3, -3,-11, // G
440    -4,  1,  2, -1, -5,  2, -2, -5,  8, -5, -4, -3, -5, -4, -2, -3, -4, -5, -1, -4,  1,  0, -3,-11, // H
441    -3, -3, -4, -5, -4, -5, -4, -6, -5,  8,  1, -4,  1,  0, -5, -4, -1, -9, -3,  3, -4, -4, -3,-11, // I
442    -4, -5, -5, -8, -9, -3, -6, -7, -4,  1,  7, -5,  3,  0, -5, -5, -4, -4, -3,  0, -6, -4, -4,-11, // L
443    -4,  2,  0, -2, -9,  0, -2, -4, -3, -4, -5,  6, -1, -9, -4, -2, -2, -7, -7, -5, -1, -1, -3,-11, // K
444    -3, -2, -5, -6, -9, -2, -4, -5, -5,  1,  3, -1, 10, -2, -5, -3, -2, -8, -6,  0, -5, -3, -3,-11, // M
445    -6, -7, -6, -9, -8, -8, -9, -7, -4,  0,  0, -9, -2, 11, -7, -4, -6, -3,  4, -4, -7, -8, -5,-11, // F
446     0, -2, -3, -4, -5, -1, -3, -3, -2, -5, -5, -4, -5, -7,  8,  0, -2, -9, -8, -3, -4, -2, -3,-11, // P
447     1, -1,  1, -1, -1, -3, -2,  0, -3, -4, -5, -2, -3, -4,  0,  5,  2, -4, -4, -3,  0, -2, -2,-11, // S
448     1, -4, -1, -2, -5, -3, -3, -3, -4, -1, -4, -2, -2, -6, -2,  2,  6, -8, -4, -1, -2, -3, -2,-11, // T
449    -9,  0, -6,-10,-11, -8,-11,-10, -5, -9, -4, -7, -8, -3, -9, -4, -8, 17, -3, -9, -7,-10, -7,-11, // W
450    -6, -7, -3, -7, -2, -7, -6, -8, -1, -3, -3, -7, -6,  4, -8, -4, -4, -3, 12, -5, -4, -7, -5,-11, // Y
451    -1, -5, -5, -5, -4, -4, -4, -4, -4,  3,  0, -5,  0, -4, -3, -3, -1, -9, -5,  6, -5, -4, -3,-11, // V
452    -1, -3,  5,  5, -8,  0,  3, -1,  1, -4, -6, -1, -5, -7, -4,  0, -2, -7, -4, -5,  5,  2, -2,-11, // B
453    -1, -2, -1,  3, -9,  5,  5, -3,  0, -4, -4, -1, -3, -8, -2, -2, -3,-10, -7, -4,  2,  5, -2,-11, // Z
454    -2, -3, -2, -3, -6, -2, -3, -3, -3, -3, -4, -3, -3, -5, -3, -2, -2, -7, -5, -3, -2, -2, -3,-11, // X
455   -11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,-11,  1, // *
456];
457
458/// PAM120 — 24x24 flattened, NCBI reference.
459#[rustfmt::skip]
460const PAM120: [i32; AA_DIM * AA_DIM] = [
461//   A   R   N   D   C   Q   E   G   H   I   L   K   M   F   P   S   T   W   Y   V   B   Z   X   *
462     3, -3,  0,  0, -3, -1,  0,  1, -3, -1, -3, -2, -2, -4,  1,  1,  1, -7, -4,  0,  0,  0, -1, -8, // A
463    -3,  6, -1, -3, -4,  1, -3, -4,  1, -2, -4,  2, -1, -5, -1, -1, -2,  1, -5, -3, -2, -1, -2, -8, // R
464     0, -1,  4,  2, -5,  0,  1,  0,  2, -2, -4,  1, -3, -4, -2,  1,  0, -4, -2, -3,  3,  0, -1, -8, // N
465     0, -3,  2,  5, -7,  1,  3,  0,  0, -3, -5, -1, -4, -7, -3,  0, -1, -8, -5, -3,  4,  3, -2, -8, // D
466    -3, -4, -5, -7, 12, -7, -7, -4, -4, -3, -7, -7, -6, -6, -4,  0, -3, -8, -1, -3, -6, -7, -4, -8, // C
467    -1,  1,  0,  1, -7,  6,  2, -3,  3, -3, -2,  0, -1, -6,  0, -2, -2, -6, -5, -3,  0,  4, -1, -8, // Q
468     0, -3,  1,  3, -7,  2,  5, -1, -1, -3, -4, -1, -3, -7, -2,  0, -2, -8, -5, -3,  3,  4, -1, -8, // E
469     1, -4,  0,  0, -4, -3, -1,  5, -4, -4, -5, -3, -4, -5, -2,  1, -1, -8, -6, -2,  0, -2, -2, -8, // G
470    -3,  1,  2,  0, -4,  3, -1, -4,  7, -4, -3, -2, -4, -3, -1, -2, -3, -3,  0, -3,  1,  1, -2, -8, // H
471    -1, -2, -2, -3, -3, -3, -3, -4, -4,  6,  1, -3,  1,  0, -3, -2,  0, -6, -2,  3, -3, -3, -1, -8, // I
472    -3, -4, -4, -5, -7, -2, -4, -5, -3,  1,  5, -4,  3,  0, -4, -4, -3, -3, -2,  1, -5, -3, -2, -8, // L
473    -2,  2,  1, -1, -7,  0, -1, -3, -2, -3, -4,  5,  0, -7, -2, -1, -1, -5, -5, -4,  0, -1, -2, -8, // K
474    -2, -1, -3, -4, -6, -1, -3, -4, -4,  1,  3,  0,  8, -1, -3, -2, -1, -6, -4,  1, -4, -2, -2, -8, // M
475    -4, -5, -4, -7, -6, -6, -7, -5, -3,  0,  1, -7, -1,  8, -5, -3, -4,  0,  5, -3, -5, -6, -3, -8, // F
476     1, -1, -2, -3, -4,  0, -2, -2, -1, -3, -4, -2, -3, -5,  6,  1,  0, -7, -6, -2, -2, -1, -2, -8, // P
477     1, -1,  1,  0,  0, -2,  0,  1, -2, -2, -4, -1, -2, -3,  1,  3,  2, -2, -3, -2,  0, -1, -1, -8, // S
478     1, -2,  0, -1, -3, -2, -2, -1, -3,  0, -3, -1, -1, -4,  0,  2,  4, -6, -3,  0, -1, -2, -1, -8, // T
479    -7,  1, -4, -8, -8, -6, -8, -8, -3, -6, -3, -5, -6,  0, -7, -2, -6, 17, -1, -8, -6, -7, -5, -8, // W
480    -4, -5, -2, -5, -1, -5, -5, -6,  0, -2, -2, -5, -4,  5, -6, -3, -3, -1, 10, -3, -3, -5, -3, -8, // Y
481     0, -3, -3, -3, -3, -3, -3, -2, -3,  3,  1, -4,  1, -3, -2, -2,  0, -8, -3,  5, -3, -3, -1, -8, // V
482     0, -2,  3,  4, -6,  0,  3,  0,  1, -3, -5,  0, -4, -5, -2,  0, -1, -6, -3, -3,  4,  2, -1, -8, // B
483     0, -1,  0,  3, -7,  4,  4, -2,  1, -3, -3, -1, -2, -6, -1, -1, -2, -7, -5, -3,  2,  4, -1, -8, // Z
484    -1, -2, -1, -2, -4, -1, -1, -2, -2, -1, -2, -2, -2, -3, -2, -1, -1, -5, -3, -1, -1, -1, -2, -8, // X
485    -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8,  1, // *
486];
487
488/// PAM200 — 24x24 flattened, NCBI reference.
489#[rustfmt::skip]
490const PAM200: [i32; AA_DIM * AA_DIM] = [
491//   A   R   N   D   C   Q   E   G   H   I   L   K   M   F   P   S   T   W   Y   V   B   Z   X   *
492     2, -2,  0,  0, -2,  0,  0,  1, -1, -1, -2, -1, -1, -3,  1,  1,  1, -6, -3,  0,  0,  0,  0, -8, // A
493    -2,  6,  0, -1, -4,  1, -1, -3,  2, -2, -3,  3,  0, -4,  0,  0, -1,  2, -4, -2, -1,  0, -1, -8, // R
494     0,  0,  2,  2, -4,  1,  1,  0,  2, -2, -3,  1, -2, -3,  0,  1,  0, -4, -2, -2,  2,  1,  0, -8, // N
495     0, -1,  2,  4, -5,  2,  3,  1,  1, -2, -4,  0, -3, -6, -1,  0,  0, -7, -4, -2,  3,  3, -1, -8, // D
496    -2, -4, -4, -5, 12, -5, -5, -3, -3, -2, -6, -5, -5, -4, -3,  0, -2, -8,  0, -2, -4, -5, -3, -8, // C
497     0,  1,  1,  2, -5,  4,  2, -1,  3, -2, -2,  1, -1, -5,  0, -1, -1, -5, -4, -2,  1,  3, -1, -8, // Q
498     0, -1,  1,  3, -5,  2,  4,  0,  1, -2, -3,  0, -2, -5, -1,  0,  0, -7, -4, -2,  3,  3, -1, -8, // E
499     1, -3,  0,  1, -3, -1,  0,  5, -2, -3, -4, -2, -3, -5,  0,  1,  0, -7, -5, -1,  0,  0, -1, -8, // G
500    -1,  2,  2,  1, -3,  3,  1, -2,  6, -2, -2,  0, -2, -2,  0, -1, -1, -3,  0, -2,  1,  2, -1, -8, // H
501    -1, -2, -2, -2, -2, -2, -2, -3, -2,  5,  2, -2,  2,  1, -2, -1,  0, -5, -1,  4, -2, -2, -1, -8, // I
502    -2, -3, -3, -4, -6, -2, -3, -4, -2,  2,  6, -3,  4,  2, -3, -3, -2, -2, -1,  2, -3, -3, -1, -8, // L
503    -1,  3,  1,  0, -5,  1,  0, -2,  0, -2, -3,  5,  0, -5, -1,  0,  0, -3, -4, -2,  1,  0, -1, -8, // K
504    -1,  0, -2, -3, -5, -1, -2, -3, -2,  2,  4,  0,  6,  0, -2, -2, -1, -4, -2,  2, -2, -2, -1, -8, // M
505    -3, -4, -3, -6, -4, -5, -5, -5, -2,  1,  2, -5,  0,  9, -5, -3, -3,  0,  7, -1, -4, -5, -2, -8, // F
506     1,  0,  0, -1, -3,  0, -1,  0,  0, -2, -3, -1, -2, -5,  6,  1,  0, -6, -5, -1, -1,  0, -1, -8, // P
507     1,  0,  1,  0,  0, -1,  0,  1, -1, -1, -3,  0, -2, -3,  1,  2,  1, -2, -3, -1,  0,  0,  0, -8, // S
508     1, -1,  0,  0, -2, -1,  0,  0, -1,  0, -2,  0, -1, -3,  0,  1,  3, -5, -3,  0,  0, -1,  0, -8, // T
509    -6,  2, -4, -7, -8, -5, -7, -7, -3, -5, -2, -3, -4,  0, -6, -2, -5, 17,  0, -6, -5, -6, -4, -8, // W
510    -3, -4, -2, -4,  0, -4, -4, -5,  0, -1, -1, -4, -2,  7, -5, -3, -3,  0, 10, -2, -3, -4, -2, -8, // Y
511     0, -2, -2, -2, -2, -2, -2, -1, -2,  4,  2, -2,  2, -1, -1, -1,  0, -6, -2,  4, -2, -2, -1, -8, // V
512     0, -1,  2,  3, -4,  1,  3,  0,  1, -2, -3,  1, -2, -4, -1,  0,  0, -5, -3, -2,  3,  2, -1, -8, // B
513     0,  0,  1,  3, -5,  3,  3,  0,  2, -2, -3,  0, -2, -5,  0,  0, -1, -6, -4, -2,  2,  3, -1, -8, // Z
514     0, -1,  0, -1, -3, -1, -1, -1, -1, -1, -1, -1, -1, -2, -1,  0,  0, -4, -2, -1, -1, -1, -1, -8, // X
515    -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8,  1, // *
516];
517
518/// BLOSUM30 — 24x24 flattened, NCBI reference.
519#[rustfmt::skip]
520const BLOSUM30: [i32; AA_DIM * AA_DIM] = [
521//   A   R   N   D   C   Q   E   G   H   I   L   K   M   F   P   S   T   W   Y   V   B   Z   X   *
522     4, -1,  0,  0, -3,  1,  0,  0, -2,  0, -1,  0,  1, -2, -1,  1,  1, -5, -4,  1,  0,  0,  0, -7, // A
523    -1,  8, -2, -1, -2,  3, -1, -2, -1, -3, -2,  1,  0, -1, -1, -1, -3,  0,  0, -1, -2,  0, -1, -7, // R
524     0, -2,  8,  1, -1, -1, -1,  0,  0, -2, -2,  0, -1, -1, -3,  0,  1, -7, -4, -2,  5, -1, -1, -7, // N
525     0, -1,  1,  9, -3, -1,  1, -1, -2, -4, -1,  0, -3, -5, -1,  0, -1, -4, -1, -2,  5,  0, -1, -7, // D
526    -3, -2, -1, -3, 17, -2,  1, -4, -5,  0, -2,  0, -2, -3, -3, -2, -2, -2, -6, -2, -2,  0, -2, -7, // C
527     1,  3, -1, -1, -2,  8,  2, -2,  0, -2,  0,  0, -1, -3,  0, -1,  0, -1, -1, -3, -1,  4, -1, -7, // Q
528     0, -1, -1,  1,  1,  2,  6, -2,  0, -3, -1,  1, -1, -4,  1,  0, -2, -1, -2, -3,  0,  5, -1, -7, // E
529     0, -2,  0, -1, -4, -2, -2,  8, -3, -1, -2, -1, -2, -3, -1,  0, -2,  1, -3, -3,  0, -2, -1, -7, // G
530    -2, -1,  0, -2, -5,  0,  0, -3, 14, -2, -1, -2,  2, -3,  1, -1, -2, -5,  0, -3, -1,  0, -1, -7, // H
531     0, -3, -2, -4,  0, -2, -3, -1, -2,  6,  2, -2,  1,  0, -3, -1,  0, -3, -1,  4, -3, -3,  0, -7, // I
532    -1, -2, -2, -1, -2,  0, -1, -2, -1,  2,  4, -2,  2,  2, -3, -2,  0, -2,  3,  1, -1, -1, -1, -7, // L
533     0,  1,  0,  0,  0,  0,  1, -1, -2, -2, -2,  4,  2, -1,  0,  0, -1, -2, -1, -2,  0,  0,  0, -7, // K
534     1,  0, -1, -3, -2, -1, -1, -2,  2,  1,  2,  2,  6,  0, -4, -2,  0, -3, -1,  0, -2, -1,  0, -7, // M
535    -2, -1, -1, -5, -3, -3, -4, -3, -3,  0,  2, -1,  0, 10, -4, -1, -2,  0,  3, -1, -3, -4, -1, -7, // F
536    -1, -1, -3, -1, -3,  0,  1, -1,  1, -3, -3,  0, -4, -4, 11, -1,  0, -3, -2, -4, -2,  0, -1, -7, // P
537     1, -1,  0,  0, -2, -1,  0,  0, -1, -1, -2,  0, -2, -1, -1,  4,  2, -3, -2, -1,  0,  0,  0, -7, // S
538     1, -3,  1, -1, -2,  0, -2, -2, -2,  0,  0, -1,  0, -2,  0,  2,  5, -5, -1,  1,  0, -1,  0, -7, // T
539    -5,  0, -7, -4, -2, -1, -1,  1, -5, -3, -2, -2, -3,  0, -3, -3, -5, 20,  5, -3, -5, -1, -2, -7, // W
540    -4,  0, -4, -1, -6, -1, -2, -3,  0, -1,  3, -1, -1,  3, -2, -2, -1,  5,  9,  1, -3, -2, -1, -7, // Y
541     1, -1, -2, -2, -2, -3, -3, -3, -3,  4,  1, -2,  0, -1, -4, -1,  1, -3,  1,  5, -2, -3,  0, -7, // V
542     0, -2,  5,  5, -2, -1,  0,  0, -1, -3, -1,  0, -2, -3, -2,  0,  0, -5, -3, -2,  5,  0, -1, -7, // B
543     0,  0, -1,  0,  0,  4,  5, -2,  0, -3, -1,  0, -1, -4,  0,  0, -1, -1, -2, -3,  0,  4, -1, -7, // Z
544     0, -1, -1, -1, -2, -1, -1, -1, -1,  0, -1,  0,  0, -1, -1,  0,  0, -2, -1,  0, -1, -1, -1, -7, // X
545    -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7,  1, // *
546];
547
548#[cfg(test)]
549mod tests {
550    use super::*;
551
552    #[test]
553    fn dna_default_values() {
554        let m = ScoringMatrix::dna_default();
555        assert_eq!(m.match_score, 2);
556        assert_eq!(m.mismatch_score, -1);
557        assert_eq!(m.gap_open, -5);
558        assert_eq!(m.gap_extend, -2);
559    }
560
561    #[test]
562    fn dna_score_pair_case_insensitive() {
563        let m = ScoringMatrix::dna_default();
564        assert_eq!(m.score_pair(b'A', b'A'), 2);
565        assert_eq!(m.score_pair(b'a', b'A'), 2);
566        assert_eq!(m.score_pair(b'A', b'T'), -1);
567    }
568
569    #[test]
570    fn scoring_matrix_validation() {
571        assert!(ScoringMatrix::new(0, -1, -5, -2).is_err());
572        assert!(ScoringMatrix::new(2, 0, -5, -2).is_err());
573        assert!(ScoringMatrix::new(2, -1, 0, -2).is_err());
574        assert!(ScoringMatrix::new(2, -1, -5, 0).is_err());
575        assert!(ScoringMatrix::new(2, -1, -5, -2).is_ok());
576    }
577
578    #[test]
579    fn blosum62_diagonal_spot_checks() {
580        let m = SubstitutionMatrix::blosum62();
581        // A-A = 4
582        assert_eq!(m.score_pair(b'A', b'A'), 4);
583        // W-W = 11
584        assert_eq!(m.score_pair(b'W', b'W'), 11);
585        // R-R = 5
586        assert_eq!(m.score_pair(b'R', b'R'), 5);
587        // Case insensitive
588        assert_eq!(m.score_pair(b'a', b'a'), 4);
589    }
590
591    #[test]
592    fn blosum62_off_diagonal() {
593        let m = SubstitutionMatrix::blosum62();
594        // A-R = -1
595        assert_eq!(m.score_pair(b'A', b'R'), -1);
596        // Symmetric
597        assert_eq!(m.score_pair(b'R', b'A'), -1);
598    }
599
600    #[test]
601    fn blosum62_gap_penalties() {
602        let m = SubstitutionMatrix::blosum62();
603        assert_eq!(m.gap_open, -11);
604        assert_eq!(m.gap_extend, -1);
605    }
606
607    #[test]
608    fn blosum45_diagonal() {
609        let m = SubstitutionMatrix::blosum45();
610        assert_eq!(m.score_pair(b'A', b'A'), 5);
611        assert_eq!(m.score_pair(b'W', b'W'), 15);
612    }
613
614    #[test]
615    fn blosum80_diagonal() {
616        let m = SubstitutionMatrix::blosum80();
617        assert_eq!(m.score_pair(b'A', b'A'), 7);
618        assert_eq!(m.score_pair(b'W', b'W'), 16);
619    }
620
621    #[test]
622    fn pam250_diagonal() {
623        let m = SubstitutionMatrix::pam250();
624        assert_eq!(m.score_pair(b'A', b'A'), 2);
625        assert_eq!(m.score_pair(b'W', b'W'), 17);
626    }
627
628    #[test]
629    fn unrecognised_residue_returns_worst() {
630        let m = SubstitutionMatrix::blosum62();
631        let worst = m.worst_score();
632        assert_eq!(m.score_pair(b'?', b'A'), worst);
633    }
634
635    #[test]
636    fn scoring_scheme_delegates() {
637        let dna = ScoringScheme::Simple(ScoringMatrix::dna_default());
638        assert_eq!(dna.score_pair(b'A', b'A'), 2);
639        assert_eq!(dna.gap_open(), -5);
640        assert_eq!(dna.gap_extend(), -2);
641
642        let protein = ScoringScheme::Substitution(SubstitutionMatrix::blosum62());
643        assert_eq!(protein.score_pair(b'W', b'W'), 11);
644        assert_eq!(protein.gap_open(), -11);
645        assert_eq!(protein.gap_extend(), -1);
646    }
647
648    #[test]
649    fn from_conversions() {
650        let _scheme: ScoringScheme = ScoringMatrix::dna_default().into();
651        let _scheme: ScoringScheme = SubstitutionMatrix::blosum62().into();
652    }
653
654    #[test]
655    fn pam40_diagonal_spot_checks() {
656        let m = SubstitutionMatrix::pam40();
657        assert_eq!(m.score_pair(b'A', b'A'), 6);
658        assert_eq!(m.score_pair(b'W', b'W'), 17);
659        assert_eq!(m.score_pair(b'C', b'C'), 15);
660    }
661
662    #[test]
663    fn pam40_symmetry() {
664        let m = SubstitutionMatrix::pam40();
665        assert_eq!(m.score_pair(b'A', b'R'), m.score_pair(b'R', b'A'));
666        assert_eq!(m.score_pair(b'D', b'E'), m.score_pair(b'E', b'D'));
667    }
668
669    #[test]
670    fn pam40_gap_penalties() {
671        let m = SubstitutionMatrix::pam40();
672        assert_eq!(m.gap_open, -10);
673        assert_eq!(m.gap_extend, -2);
674        assert_eq!(m.name(), "PAM40");
675    }
676
677    #[test]
678    fn pam120_diagonal_spot_checks() {
679        let m = SubstitutionMatrix::pam120();
680        assert_eq!(m.score_pair(b'A', b'A'), 3);
681        assert_eq!(m.score_pair(b'W', b'W'), 17);
682        assert_eq!(m.score_pair(b'C', b'C'), 12);
683    }
684
685    #[test]
686    fn pam120_gap_penalties() {
687        let m = SubstitutionMatrix::pam120();
688        assert_eq!(m.gap_open, -11);
689        assert_eq!(m.gap_extend, -1);
690        assert_eq!(m.name(), "PAM120");
691    }
692
693    #[test]
694    fn pam200_diagonal_spot_checks() {
695        let m = SubstitutionMatrix::pam200();
696        assert_eq!(m.score_pair(b'A', b'A'), 2);
697        assert_eq!(m.score_pair(b'W', b'W'), 17);
698        assert_eq!(m.score_pair(b'Y', b'Y'), 10);
699    }
700
701    #[test]
702    fn pam200_gap_penalties() {
703        let m = SubstitutionMatrix::pam200();
704        assert_eq!(m.gap_open, -11);
705        assert_eq!(m.gap_extend, -1);
706        assert_eq!(m.name(), "PAM200");
707    }
708
709    #[test]
710    fn blosum30_diagonal_spot_checks() {
711        let m = SubstitutionMatrix::blosum30();
712        assert_eq!(m.score_pair(b'A', b'A'), 4);
713        assert_eq!(m.score_pair(b'W', b'W'), 20);
714        assert_eq!(m.score_pair(b'H', b'H'), 14);
715        assert_eq!(m.score_pair(b'C', b'C'), 17);
716    }
717
718    #[test]
719    fn blosum30_gap_penalties() {
720        let m = SubstitutionMatrix::blosum30();
721        assert_eq!(m.gap_open, -14);
722        assert_eq!(m.gap_extend, -4);
723        assert_eq!(m.name(), "BLOSUM30");
724    }
725}