Commit bb26b649 authored by NOEL Philippe's avatar NOEL Philippe

Add comments and tests for AtomTypes enum

parent b5383647
......@@ -15,4 +15,4 @@ path = "src/lib.rs"
[dependencies]
lazy_static="1.3.0"
......@@ -4,6 +4,8 @@
//! You can create a protein structure by parsing with parse_pdb function. Then you can add filters on your protein.
//! And save it with write_pdb function.
//!
#[macro_use]
extern crate lazy_static;
mod pdb;
......@@ -11,7 +13,7 @@ pub use self::pdb::read_pdb::parse_pdb;
pub use self::pdb::write_pdb::write_pdb;
pub use self::pdb::tools::*;
pub use self::pdb::atom::Atom;
pub use self::pdb::atom::*;
pub use self::pdb::chain::Chain;
pub use self::pdb::protein::Protein;
pub use self::pdb::residue::Residue;
use std::collections::HashMap;
use std::ops::Deref;
/// An `Atom` is a sub-structure linked to a `Residue`.
......@@ -65,6 +66,42 @@ impl Atom {
}
}
/// Enumerate to check the types of the parsed atom.
///
/// Atom will be class into the following map:
/// "ARG" => AtomTypes::AminoAcid
/// "LYS" => AtomTypes::AminoAcid
/// "ASN" => AtomTypes::AminoAcid
/// "ASP" => AtomTypes::AminoAcid
/// "GLU" => AtomTypes::AminoAcid
/// "SER" => AtomTypes::AminoAcid
/// "THR" => AtomTypes::AminoAcid
/// "GLN" => AtomTypes::AminoAcid
/// "CYS" => AtomTypes::AminoAcid
/// "HIS" => AtomTypes::AminoAcid
/// "HSD" => AtomTypes::AminoAcid
/// "HSP" => AtomTypes::AminoAcid
/// "HSD" => AtomTypes::AminoAcid
/// "SEC" => AtomTypes::AminoAcid
/// "GLY" => AtomTypes::AminoAcid
/// "PRO" => AtomTypes::AminoAcid
/// "ALA" => AtomTypes::AminoAcid
/// "VAL" => AtomTypes::AminoAcid
/// "ILE" => AtomTypes::AminoAcid
/// "LEU" => AtomTypes::AminoAcid
/// "MET" => AtomTypes::AminoAcid
/// "PHE" => AtomTypes::AminoAcid
/// "TYR" => AtomTypes::AminoAcid
/// "TRP" => AtomTypes::AminoAcid
/// "ADE" => AtomTypes::NucleicAcid
/// "GUA" => AtomTypes::NucleicAcid
/// "THY" => AtomTypes::NucleicAcid
/// "CYT" => AtomTypes::NucleicAcid
/// "TIP3W" => AtomTypes::Wat
/// "POPC" => AtomTypes::Lipid
/// "POPE" => AtomTypes::Lipid
///
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum AtomTypes {
AminoAcid,
NucleicAcid,
......@@ -72,10 +109,57 @@ pub enum AtomTypes {
Water,
}
impl Copy for AtomTypes { }
impl Clone for AtomTypes {
fn clone(&self) -> Self {
*self
impl AtomTypes {
/// Return a AtomTypes according to the "residue" of the atom
///
/// ````
/// use pdbparser::AtomTypes;
///
/// assert_eq!(AtomTypes::AminoAcid, *AtomTypes::get("trp").unwrap());
/// assert_eq!(AtomTypes::Lipid, *AtomTypes::get("POPC").unwrap());
/// assert_eq!(None, AtomTypes::get("toto"));
///
/// ````
pub fn get(atom: &str) -> Option<&AtomTypes> {
ATOM_TYPES.get(&atom.to_uppercase()[..])
}
}
\ No newline at end of file
}
lazy_static! {
static ref ATOM_TYPES: HashMap<&'static str, AtomTypes> = [
("ARG", AtomTypes::AminoAcid),
("LYS", AtomTypes::AminoAcid),
("ASN", AtomTypes::AminoAcid),
("ASP", AtomTypes::AminoAcid),
("GLU", AtomTypes::AminoAcid),
("SER", AtomTypes::AminoAcid),
("THR", AtomTypes::AminoAcid),
("GLN", AtomTypes::AminoAcid),
("CYS", AtomTypes::AminoAcid),
("HIS", AtomTypes::AminoAcid),
("HSD", AtomTypes::AminoAcid),
("HSP", AtomTypes::AminoAcid),
("HSD", AtomTypes::AminoAcid),
("SEC", AtomTypes::AminoAcid),
("GLY", AtomTypes::AminoAcid),
("PRO", AtomTypes::AminoAcid),
("ALA", AtomTypes::AminoAcid),
("VAL", AtomTypes::AminoAcid),
("ILE", AtomTypes::AminoAcid),
("LEU", AtomTypes::AminoAcid),
("MET", AtomTypes::AminoAcid),
("PHE", AtomTypes::AminoAcid),
("TYR", AtomTypes::AminoAcid),
("TRP", AtomTypes::AminoAcid),
("ADE", AtomTypes::NucleicAcid),
("GUA", AtomTypes::NucleicAcid),
("THY", AtomTypes::NucleicAcid),
("CYT", AtomTypes::NucleicAcid),
("TIP3W", AtomTypes::Water),
("POPC", AtomTypes::Lipid),
("POPE", AtomTypes::Lipid),
]
.iter()
.cloned()
.collect();
}
......@@ -2,7 +2,6 @@ use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::process;
use std::collections::HashMap;
use super::protein::Protein;
use super::atom::AtomTypes;
......@@ -53,43 +52,6 @@ fn parse_int(s: &str) -> i64 {
/// assert_eq!(66, my_prot.get_number_residue());
/// ```
pub fn parse_pdb(pdb: &str, name: &str) -> Protein {
// Allocate here to avoid multiple allocation for every call
let atom_type: HashMap<&str, AtomTypes> = [
("ARG", AtomTypes::AminoAcid),
("LYS", AtomTypes::AminoAcid),
("ASN", AtomTypes::AminoAcid),
("ASP", AtomTypes::AminoAcid),
("GLU", AtomTypes::AminoAcid),
("SER", AtomTypes::AminoAcid),
("THR", AtomTypes::AminoAcid),
("GLN", AtomTypes::AminoAcid),
("CYS", AtomTypes::AminoAcid),
("HIS", AtomTypes::AminoAcid),
("HSD", AtomTypes::AminoAcid),
("HSP", AtomTypes::AminoAcid),
("HSD", AtomTypes::AminoAcid),
("SEC", AtomTypes::AminoAcid),
("GLY", AtomTypes::AminoAcid),
("PRO", AtomTypes::AminoAcid),
("ALA", AtomTypes::AminoAcid),
("VAL", AtomTypes::AminoAcid),
("ILE", AtomTypes::AminoAcid),
("LEU", AtomTypes::AminoAcid),
("MET", AtomTypes::AminoAcid),
("PHE", AtomTypes::AminoAcid),
("TYR", AtomTypes::AminoAcid),
("TRP", AtomTypes::AminoAcid),
("ADE", AtomTypes::NucleicAcid),
("GUA", AtomTypes::NucleicAcid),
("THY", AtomTypes::NucleicAcid),
("CYT", AtomTypes::NucleicAcid),
("TIP3W", AtomTypes::Water),
("POPC", AtomTypes::Lipid),
("POPE", AtomTypes::Lipid),
]
.iter()
.cloned()
.collect();
// Check if the file exist and/or can be read
let pdb = match File::open(pdb) {
......@@ -110,7 +72,7 @@ pub fn parse_pdb(pdb: &str, name: &str) -> Protein {
// If the "residue" is a protein residue, continue to parse the line and add informations to the protein
// else continue to the next one line
let residue_name = &l[17..20].trim();
match atom_type.get(&residue_name.to_uppercase()[..]) {
match AtomTypes::get(&residue_name.to_uppercase()[..]) {
Some(AtomTypes::AminoAcid) => {
let atom_name = &l[12..17].trim().to_string();
let chain = l[21..22].chars().next().unwrap();
......@@ -128,12 +90,12 @@ pub fn parse_pdb(pdb: &str, name: &str) -> Protein {
atom_number as u64,
[x, y, z],
);
}
}
Some(AtomTypes::Water) => (),
Some(AtomTypes::Lipid) => (),
Some(AtomTypes::NucleicAcid) => (),
_ => () //TODO: Warning with unrecognize atom
}
}
}
}
protein
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment