Commit 1269b22a authored by NOEL Philippe's avatar NOEL Philippe

Add the function to parse PDB

parent 3f2eb34a
......@@ -4,6 +4,7 @@ use pdbparser::*;
fn main() {
//let tutu = open_pdb_file("src/tests/f2_adn.pdb");
let tutu = parse_pdb("src/tests/f2.pdb");
//println!("Nb res : {}", tutu.get_res_number());
let tutu = parse_pdb("src/tests_file/5jpq.pdb");
println!("Nb res : {}", tutu.get_number_residue());
println!("Nb chain : {}", tutu.get_number_chain());
}
......@@ -59,6 +59,35 @@ impl Chain {
self.lst_res.len() as u64
}
/// Return a mutable reference of a residue with its name. Return None if the
/// residue does not exist
///
/// # Examples
///
/// ````
/// use pdbparser;
///
/// let mut my_chain = pdbparser::Chain::new('a');
/// let lys = pdbparser::Residue::new(String::from("lysine"), 1);
/// my_chain.add_res(lys);
///
/// assert_eq!(1, my_chain.lst_res[0].res_num);
/// {
/// let mut res_ref = my_chain.get_residue_ref(1).unwrap();
/// res_ref.res_num = 4;
/// }
/// assert_eq!(4, my_chain.lst_res[0].res_num);
///
/// ````
pub fn get_residue_ref(&mut self, n: u64) -> Option<&mut Residue> {
for res in &mut self.lst_res {
if res.res_num == n {
return Some(res)
}
}
None
}
/// Get the name of the Chain
pub fn get_name(&self) -> char {
self.name
......
use super::atom::Atom;
use super::chain::Chain;
use super::residue::Residue;
#[derive(Debug)]
pub struct Protein {
......@@ -50,6 +53,32 @@ impl<'a> Protein {
return false
}
/// Return a mutable reference of a chaine with its name. Return None if the
/// chain does not exist
///
/// # Examples
///
/// ````
/// use pdbparser;
///
/// let mut my_prot = pdbparser::Protein::new(String::from("my_prot"));
/// my_prot.add_chain(pdbparser::Chain::new('n'));
/// assert_eq!('n', my_prot.lst_chain[0].get_name());
/// {
/// let mut reference = my_prot.get_chain_ref('n').unwrap();
/// reference.name = 'a';
/// }
/// assert_eq!('a', my_prot.lst_chain[0].get_name());
/// ````
pub fn get_chain_ref(&mut self, c: char) -> Option<&mut Chain> {
for chain in &mut self.lst_chain {
if chain.name == c {
return Some(chain)
}
}
None
}
/// Get the number of chain in the protein
///
/// # Examples
......@@ -86,4 +115,39 @@ impl<'a> Protein {
self.lst_chain.push(c);
}
pub fn get_number_residue(&self) -> u64 {
let mut n: u64 = 0;
for chain in self.lst_chain.iter() {
for _ in chain.lst_res.iter() {
n+= 1;
}
}
n
}
pub fn update_protein(&mut self, chain: char, res_name: String, res_number: u64, atom_name: String, atom_number: u64, coord: [f32; 3]) {
// Get a chain reference. If the chain exist, return a mutable reference to it. If not,
// create a new chain an return the mutable reference
let chain = match self.get_chain_ref(chain) {
Some(c) => c,
None => {
self.add_chain(Chain::new(chain));
self.get_chain_ref(chain).unwrap()
},
};
// Get a residue reference. If the residue exist, return a mutable reference to it. If not,
// create a new residue and return it as mutable reference
let residue = match chain.get_residue_ref(res_number as u64) {
Some(r) => r,
None => {
chain.add_res(Residue::new(res_name, res_number));
chain.get_residue_ref(res_number).unwrap()
},
};
let atom = Atom::new(atom_name, atom_number, coord);
residue.add_atom(atom);
}
// TODO: MAYBE USE MUTABLE FOR STRUCT HERE
}
......@@ -66,7 +66,7 @@ pub fn parse_pdb(f: &str) -> Protein {
let x = parse_float(&l[30..38].to_string());
let y = parse_float(&l[38..46].to_string());
let z = parse_float(&l[46..54].to_string());
//protein.add_info(chain.clone(), residue_name.clone(), residue_number, atom_name.clone(), atom_number as u64, [x, y, z]);
protein.update_protein(chain.clone(), residue_name.clone(), residue_number as u64, atom_name.clone(), atom_number as u64, [x, y, z]);
}
}
protein
......
This diff is collapsed.
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