diff --git a/src/main.rs b/src/main.rs index d7b3f27710ae27aa4ed8070b46d9f7c1ed3e91fa..8c883c23b972261632939aeec86848df05e8118f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ fn main() { println!("Prot : {} \nn chain: {}\nn res: {}\nn atom: {}", my_prot.name, my_prot.get_number_chain(), my_prot.get_number_residue(), my_prot.get_number_atom()); println!("Reduce protein"); - let chainA = my_prot.select_atoms("chain a").unwrap(); - println!("Prot : {} \nn chain: {}\nn res: {}\nn atom: {}", chainA.name, chainA.get_number_chain(), chainA.get_number_residue(), chainA.get_number_atom()); + let chain_a = my_prot.select_atoms("chain a").unwrap(); + println!("Prot : {} \nn chain: {}\nn res: {}\nn atom: {}", chain_a.name, chain_a.get_number_chain(), chainA.get_number_residue(), chainA.get_number_atom()); } diff --git a/src/pdb/atom.rs b/src/pdb/atom.rs index 130d0d2c9e7a097aadb5fe1bb153836eb3d7394d..c854bccf59c56fb80d921ed85f50e0cdaad930b5 100644 --- a/src/pdb/atom.rs +++ b/src/pdb/atom.rs @@ -17,6 +17,7 @@ pub struct Atom { 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 /// @@ -28,11 +29,7 @@ impl Atom { /// ```` pub fn new(name: String, number: u64, coord: [f32; 3]) -> Atom { let n = name.deref(); - let back = if n == "C" || n == "CA" || n == "N" || n == "O" || n == "OT1" || n == "OT2" { - true - } else { - false - }; + let back = n == "C" || n == "CA" || n == "N" || n == "O" || n == "OT1" || n == "OT2"; Atom { name, number, @@ -41,6 +38,12 @@ impl Atom { } } + /// Get the name of the atom + /// + pub fn name(self) -> String{ + self.name.clone() + } + /// Compute the distance between 2 Atoms /// /// # Examples diff --git a/src/pdb/protein.rs b/src/pdb/protein.rs index c0cd079e7ff462439382c5f6e8ec7fd943339a14..e565506d5a7c6408600f40cfe9f8a68cc4bdc922 100644 --- a/src/pdb/protein.rs +++ b/src/pdb/protein.rs @@ -286,7 +286,7 @@ impl<'a> Protein { // ex: don't parse the chain if it's not selected pub fn select_atoms(&self, pattern: &str) -> Option { - let mut n_prot = Protein::new(String::from(self.name.clone())); + let mut n_prot = Protein::new(self.name.clone()); let select = match selection_atom::parse_select(&pattern) { Some(x) => x, diff --git a/src/pdb/read_pdb.rs b/src/pdb/read_pdb.rs index 49a2b3c7e8e4f0eab1a2cfc7c546fb91838e62ac..69c18e3aef57a6bbbe6324d9ac5ab9f3c3980c18 100644 --- a/src/pdb/read_pdb.rs +++ b/src/pdb/read_pdb.rs @@ -12,7 +12,7 @@ use super::protein::Protein; /// # Errors /// Will return 0.0 if the String cannot be convert and print the error /// -fn parse_float(s: &String) -> f32 { +fn parse_float(s: &str) -> f32 { match s.trim().parse::() { Ok(n) => n, Err(e) => { @@ -30,15 +30,15 @@ fn parse_float(s: &String) -> f32 { /// # Errors /// Will return 0 if the String cannot be convert and print the error /// -fn parse_int(s: &String) -> i64 { +fn parse_int(s: &str) -> i64 { match s.trim().parse::() { Ok(n) => n, Err(e) => { match i64::from_str_radix(s.trim(), 16) { - Ok(n) => return n, + Ok(n) => n, Err(_) => { println!("{}", e); - return 0 + 0 } } } @@ -79,7 +79,7 @@ pub fn parse_pdb(pdb: &str, name: &str) -> Protein { let l = line.unwrap(); if l.starts_with("HETAM") || l.starts_with("ATOM") { let atom_name = &l[12..17].trim().to_string(); - let residue_name = &l[17..20].trim().to_string(); + let residue_name = &l[17..20].trim(); let chain = l[21..22].chars().next().unwrap(); let atom_number = parse_int(&l[6..11].to_string()); let residue_number = parse_int(&l[22..26].to_string()); @@ -87,14 +87,15 @@ pub fn parse_pdb(pdb: &str, name: &str) -> Protein { let y = parse_float(&l[38..46].to_string()); let z = parse_float(&l[46..54].to_string()); if is_protein_res(residue_name, &lst_res) { - protein.update_protein(chain, residue_name.clone(), residue_number as u64, atom_name.clone(), atom_number as u64, [x, y, z]); + protein.update_protein(chain, residue_name.to_string(), residue_number as u64, atom_name.clone(), atom_number as u64, [x, y, z]); } } } protein } - -fn is_protein_res(r: &str, lst: &Vec<&str>) -> bool { +/// Test if the selected line is a residue +/// +fn is_protein_res(r: &str, lst: &[&str]) -> bool { let r = r.to_uppercase(); diff --git a/src/pdb/residue.rs b/src/pdb/residue.rs index de85d05ab8235a536d5bac36eae3e9f0f5ad30c2..08c151f498379481ca9d679a4486f65f71a0d7c5 100644 --- a/src/pdb/residue.rs +++ b/src/pdb/residue.rs @@ -34,7 +34,14 @@ impl Residue { } } + /// Get the name of the residue + /// + pub fn name(&self) -> String { + self.name.clone() + } + /// Get the number of the residue + /// pub fn get_res_num(&self) -> u64 { self.res_num } diff --git a/tests/test.rs b/tests/test.rs index de6b5b54d4bd5cf6395817760b1be6d427c3b07a..cab684998aeaece6d7fe4aafca1d78d8c6206f2a 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -2,8 +2,8 @@ extern crate pdbparser; #[test] fn parse_f2() { - use pdbparser; let my_prot = pdbparser::parse_pdb("tests/tests_file/f2.pdb", "f2"); + assert_eq!("f2", my_prot.name()); assert_eq!(1, my_prot.get_number_chain()); assert_eq!(66, my_prot.get_number_residue()); assert_eq!(1085, my_prot.get_number_atom()); @@ -11,7 +11,6 @@ fn parse_f2() { #[test] fn parse_f2_adn() { - use pdbparser; let my_prot = pdbparser::parse_pdb("tests/tests_file/f2_adn.pdb", "f2"); assert_eq!(1, my_prot.get_number_chain()); assert_eq!(66, my_prot.get_number_residue()); @@ -20,7 +19,6 @@ fn parse_f2_adn() { #[test] fn parse_trp() { - use pdbparser; let my_prot = pdbparser::parse_pdb("tests/tests_file/trp_MD.pdb", "trp"); assert_eq!(1, my_prot.get_number_chain()); assert_eq!(704, my_prot.get_number_residue()); @@ -29,9 +27,23 @@ fn parse_trp() { #[test] fn parse_5jpq() { - use pdbparser; let my_prot = pdbparser::parse_pdb("tests/tests_file/5jpq.pdb", "5jpq"); assert_eq!(35, my_prot.get_number_chain()); assert_eq!(8173, my_prot.get_number_residue()); assert_eq!(44801, my_prot.get_number_atom()); +} + +#[test] +fn f2_res() { + let mut my_prot = pdbparser::parse_pdb("tests/tests_file/f2.pdb", "f2"); + let chain_a = my_prot.get_chain_ref('A').unwrap(); + + let res = chain_A.get_residue_ref(1).unwrap(); + assert_eq!("THR", res.name()); + + let res = chain_A.get_residue_ref(2).unwrap(); + assert_eq!("SER", res.name()); + + let res = chain_A.get_residue_ref(chain_A.get_number_residue()).unwrap(); + assert_eq!("HSD", res.name()); } \ No newline at end of file