atom.rs 1.95 KB
Newer Older
NOEL Philippe's avatar
NOEL Philippe committed
1 2 3 4 5 6 7 8
use std::ops::Deref;

/// An `Atom` is a sub-structure linked to a `Residue`.
/// It stores the following properties:
/// - atom name;
/// - atom number (atomid);
/// - Coordinates x, y and z
/// - if the atom is a constituant of the backbone of the protein
9
///
NOEL Philippe's avatar
NOEL Philippe committed
10 11 12 13 14 15 16 17 18 19 20 21 22
#[derive(Debug)]
pub struct Atom {
    pub name: String,
    pub number: u64,
    pub coord: [f32; 3],
    pub is_backbone: bool,
}

impl Atom {
    /// Create a new structure Atom. An atom have a name, a number and x, y, z coordinates
    /// If the atom name is "C", "CA", "N", "O", "OT1" or "OT2", it will be consider as backbone
    ///
    /// # Examples
23
    ///
NOEL Philippe's avatar
NOEL Philippe committed
24 25
    /// ````
    /// use pdbparser;
26
    ///
NOEL Philippe's avatar
NOEL Philippe committed
27
    /// let hydrogen = pdbparser::Atom::new(String::from("HT1"), 1, [0.0, 0.0, 0.0]);
28
    ///
NOEL Philippe's avatar
NOEL Philippe committed
29 30 31
    /// ````   
    pub fn new(name: String, number: u64, coord: [f32; 3]) -> Atom {
        let n = name.deref();
32
        let back = n == "C" || n == "CA" || n == "N" || n == "O" || n == "OT1" || n == "OT2";
NOEL Philippe's avatar
NOEL Philippe committed
33 34 35 36 37 38 39 40 41
        Atom {
            name,
            number,
            coord,
            is_backbone: back,
        }
    }

    /// Get the name of the atom
42 43
    ///
    pub fn name(&self) -> String {
NOEL Philippe's avatar
NOEL Philippe committed
44 45 46 47 48 49
        self.name.clone()
    }

    /// Compute the distance between 2 Atoms
    ///
    /// # Examples
50
    ///
NOEL Philippe's avatar
NOEL Philippe committed
51 52
    /// ````
    /// use pdbparser;
53
    ///
NOEL Philippe's avatar
NOEL Philippe committed
54 55
    /// let h1 = pdbparser::Atom::new(String::from("HT1"), 1, [1.0, 5.0, 2.0]);
    /// let h2 = pdbparser::Atom::new(String::from("HT1"), 1, [11.0, 17.0, 5.0]);
56
    ///
NOEL Philippe's avatar
NOEL Philippe committed
57
    /// assert_eq!(15.905973, h1.compute_distance(&h2));
58
    ///
NOEL Philippe's avatar
NOEL Philippe committed
59 60
    /// ````   
    pub fn compute_distance(&self, a: &Atom) -> f32 {
61 62 63 64
        ((self.coord[0] - a.coord[0]).powi(2)
            + (self.coord[1] - a.coord[1]).powi(2)
            + (self.coord[2] - a.coord[2]).powi(2))
        .sqrt()
NOEL Philippe's avatar
NOEL Philippe committed
65 66
    }
}
NOEL Philippe's avatar
NOEL Philippe committed
67 68 69 70 71 72 73 74

pub enum AtomTypes {
    AminoAcid,
    NucleicAcid,
    Lipid,
    Water,
}

75 76 77
impl Copy for AtomTypes { }

impl Clone for AtomTypes {
NOEL Philippe's avatar
NOEL Philippe committed
78 79 80 81
    fn clone(&self) -> Self {
        *self
    }
}