atom.rs 1.78 KB
Newer Older
NOEL Philippe's avatar
NOEL Philippe committed
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
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
/// 
#[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
    /// 
    /// ````
    /// use pdbparser;
    /// 
    /// let hydrogen = pdbparser::Atom::new(String::from("HT1"), 1, [0.0, 0.0, 0.0]);
    /// 
    /// ````   
    pub fn new(name: String, number: u64, coord: [f32; 3]) -> Atom {
        let n = name.deref();
        let back = n == "C" ||  n == "CA" || n == "N" || n == "O" || n == "OT1" || n == "OT2";
        Atom {
            name,
            number,
            coord,
            is_backbone: back,
        }
    }

    /// Get the name of the atom
    /// 
    pub fn name(self) -> String{
        self.name.clone()
    }

    /// Compute the distance between 2 Atoms
    ///
    /// # Examples
    /// 
    /// ````
    /// use pdbparser;
    /// 
    /// 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]);
    /// 
    /// assert_eq!(15.905973, h1.compute_distance(&h2));
    /// 
    /// ````   
    pub fn compute_distance(&self, a: &Atom) -> f32 {
        (
        (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()
    }
}