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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use super::AtomSerial;
use super::{Element, Residue};
use serde::Serialize;
use std::str::FromStr;

/// Represents an atom in a structural model.
///
/// # Field
///
/// - `id`: Unique identifier. No two atoms. No two `Atom`s in the same `Model` can have the same `id`.
/// - `name`: Atom name. This represents the exact "role" of an atom in a given residue.
/// - `residue`: The [`Residue`] this atom belongs to. This can be an amino acid, a nucleotide, water, or a custom molecule.
/// - `chain`: The chain identifier
/// - `sequence_number`: The position of this atom in the chain
/// - `coord`: Cartesian coordinates `x, y, z`
/// - `occupancy`:
/// - `temperature_factor`:
/// - `element`: The [`Element`]
/// - `chaege`: The charge on the atom
///
/// [`Residue`]: ../residue/struct.Residue.html
/// [`Element`]: ../element/enum.Element.html
#[derive(Debug, Clone, Serialize)]
pub struct Atom {
    pub id: AtomSerial,
    pub name: AtomName,
    pub id1: char,
    pub residue: Residue,
    pub chain: char,
    pub sequence_number: u32,
    pub insertion_code: char,
    pub coord: [f32; 3],
    pub occupancy: f32,
    pub temperature_factor: f32,
    pub element: Element,
    pub charge: i8,
    // pub is_hetatom: bool, // ! implied
}

pub struct GenericAtomParser;

/// Internally, an `AtomName` is represented as an array of 4 bytes.
///
/// For convinience, `FromStr` is implemented for `AtomName`, so you can:
///
/// ```
/// use std::str::FromStr;
/// use protein_core::structure::AtomName;
///
/// let atom = AtomName::from_str("O2").unwrap();
/// assert_eq!(atom, AtomName([b'O', b'2', b' ', b' ']));
/// ```
#[derive(Debug, Clone, Serialize, Eq, PartialEq)]
pub struct AtomName(pub [u8; 4]);

impl AtomName {
    pub(crate) fn is_n(&self) -> bool {
        &self.0 == b"N   "
    }
    pub(crate) fn is_c(&self) -> bool {
        &self.0 == b"C   "
    }
    pub(crate) fn is_ca(&self) -> bool {
        &self.0 == b"CA  "
    }
    pub(crate) fn is_o(&self) -> bool {
        &self.0 == b"O   "
    }
}

#[derive(Debug)]
pub enum ParseAtomNameError {
    LengthGreaterThan4,
    LengthZero,
}

impl FromStr for AtomName {
    type Err = ParseAtomNameError;
    fn from_str(s: &str) -> Result<Self, ParseAtomNameError> {
        let s = s.as_bytes();
        match s.len() {
            1usize => Ok(AtomName([s[0], b' ', b' ', b' '])),
            2usize => Ok(AtomName([s[0], s[1], b' ', b' '])),
            3usize => Ok(AtomName([s[0], s[1], s[2], b' '])),
            4usize => Ok(AtomName([s[0], s[1], s[2], s[3]])),
            0usize => Err(ParseAtomNameError::LengthZero),
            _ => Err(ParseAtomNameError::LengthGreaterThan4),
        }
    }
}

// #[derive(Debug, Clone, Serialize)]
// pub enum AtomName {
//     AminoAcid(AminoAcidAtomName),
//     Nucleotide(NucleotideAtomName),
//     WaterO,
//     Other(String),
// }

// #[derive(Debug, Clone, Serialize)]
// pub enum AminoAcidAtomName {
//     N,
//     CA,
//     C,
//     O,
//     Other(String),
// }

// impl AminoAcidAtomName {
//     pub fn from_bytes(inp: &[u8]) -> Self {
//         match inp {
//             b"N" => Self::N,
//             b"CA" => Self::CA,
//             b"C" => Self::C,
//             b"O" => Self::O,
//             _ => Self::Other(unsafe { std::str::from_utf8_unchecked(inp).to_owned() }),
//         }
//     }
//     pub fn from_bytes_fixed4(inp: &[u8]) -> Self {
//         match inp {
//             b" N  " => Self::N,
//             b" CA " => Self::CA,
//             b" C  " => Self::C,
//             b" O  " => Self::O,
//             _ => Self::Other(unsafe { std::str::from_utf8_unchecked(inp).trim_start().to_owned() }),
//         }
//     }
// }

// #[derive(Debug, Clone, Serialize)]
// pub enum NucleotideAtomName {
//     OP1,
//     OP2,
//     O5,
//     O4,
//     O3,
//     O2,
//     C5,
//     C4,
//     C3,
//     C2,
//     C1,
//     N9,
//     N7,
//     N6,
//     N4,
//     N3,
//     N2,
//     N1,
//     P,
//     Other(String),
// }

// impl NucleotideAtomName {
//     pub fn from_bytes_uppercase(inp: &[u8]) -> Self {
//         match inp {
//             b"OP1" => Self::OP1,
//             b"OP2" => Self::OP2,
//             b"O5" => Self::O5,
//             b"O4" => Self::O4,
//             b"O3" => Self::O3,
//             b"O2" => Self::O2,
//             b"C5" => Self::C5,
//             b"C4" => Self::C4,
//             b"C3" => Self::C3,
//             b"C2" => Self::C2,
//             b"C1" => Self::C1,
//             b"N9" => Self::N9,
//             b"N7" => Self::N7,
//             b"N6" => Self::N6,
//             b"N4" => Self::N4,
//             b"N3" => Self::N3,
//             b"N2" => Self::N2,
//             b"N1" => Self::N1,
//             b"P" => Self::P,
//             _ => Self::Other(unsafe { std::str::from_utf8_unchecked(inp).to_owned() }),
//         }
//     }
//     pub fn from_bytes_uppercase_fixed4(inp: &[u8]) -> Self {
//         match inp {
//             b" OP1" => Self::OP1,
//             b" OP2" => Self::OP2,
//             b" O5 " => Self::O5,
//             b" O4 " => Self::O4,
//             b" O3 " => Self::O3,
//             b" O2 " => Self::O2,
//             b" C5 " => Self::C5,
//             b" C4 " => Self::C4,
//             b" C3 " => Self::C3,
//             b" C2 " => Self::C2,
//             b" C1 " => Self::C1,
//             b" N9 " => Self::N9,
//             b" N7 " => Self::N7,
//             b" N6 " => Self::N6,
//             b" N4 " => Self::N4,
//             b" N3 " => Self::N3,
//             b" N2 " => Self::N2,
//             b" N1 " => Self::N1,
//             b" P  " => Self::P,
//             _ => Self::Other(unsafe { std::str::from_utf8_unchecked(inp).trim_start().to_owned() }),
//         }
//     }
// }