1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*! AA-colour

Add colours to your amino acids in the terminal.

Should work with Linux, MacOS and Windows.

```rust
use aa_colour::{AaColour, palettes::Clustal};

println!("{}", AaColour::colour::<Clustal>('A').unwrap());
println!("{}", AaColour::blank::<Clustal>('-').unwrap());
```
*/
// #![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]

pub mod error;
pub mod palettes;

use core::fmt;
use error::AaColourError;
use palettes::{Palette, Rgb};

/// Amino acid background colour
#[derive(Debug, Clone)]
pub struct AaColour {
    aa: char,
    colour: &'static Rgb,
}

impl fmt::Display for AaColour {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_fmt(format_args!(
            "\x1B[48;2;{};{};{};30m",
            self.colour.r, self.colour.g, self.colour.b
        ))?;
        fmt::Display::fmt(&self.aa, f)?;
        f.write_str("\x1b[0m")
    }
}

impl AaColour {
    /// Coloured amino acid according to its name
    /// # Errors
    ///
    /// Fails if `x` is not a 1-letter code for an amino acid
    /// # Example
    ///
    /// ```rust
    /// use aa_colour::{AaColour, palettes::Clustal};
    ///
    /// let coloured_aa = AaColour::colour::<Clustal>('C');
    /// ````
    pub fn colour<P: Palette>(x: char) -> Result<Self, AaColourError> {
        let col = match_char_aa::<P>(x)?;

        Ok(Self { aa: x, colour: col })
    }

    /// Uncoloured amino acid
    /// # Errors
    ///
    /// Fails if `x` is not a 1-letter code for an amino acid (TODO)
    /// # Example
    ///
    /// ```rust
    /// use aa_colour::{AaColour, palettes::Clustal};
    ///
    /// let blank_aa = AaColour::blank::<Clustal>('C');
    /// ````
    pub fn blank<P: Palette>(x: char) -> Result<Self, AaColourError> {
        Ok(Self {
            aa: x,
            colour: P::GAP,
        })
    }
}

macro_rules! match_aa {
    ($(
        $aa:literal $colour:ident
    ),* $(,)?) => {
        fn match_char_aa<P: Palette>(x: char) -> Result<&'static Rgb, AaColourError> {
            match x.to_ascii_uppercase() {
                $(
                    $aa => Ok(P::$colour),
                )*
                _ => Err(AaColourError::NotAnAminoAcid(x)),
            }
        }
    };
}

match_aa! {
    'A' A,
    'C' C,
    'D' D,
    'E' E,
    'F' F,
    'G' G,
    'H' H,
    'I' I,
    'K' K,
    'L' L,
    'M' M,
    'N' N,
    'P' P,
    'Q' Q,
    'R' R,
    'S' S,
    'T' T,
    'V' V,
    'W' W,
    'Y' Y,
    '-' GAP
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::palettes::Clustal;

    #[test]
    fn all_amino_acids_clustal() {
        let aas = vec![
            'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
            'V', 'W', '-',
        ];
        for aa in aas {
            print!("{}", AaColour::colour::<Clustal>(aa).unwrap());
        }
        println!(); //
    }

    #[test]
    fn all_amino_acids_clustal_blanks() {
        let aas = vec![
            'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
            'V', 'W', '-',
        ];
        for aa in aas {
            print!("{}", AaColour::blank::<Clustal>(aa).unwrap());
        }
        println!(); //
    }
}