Commit b0ff8506 authored by NOEL Philippe's avatar NOEL Philippe

Implement select atom for structure

parent 035fbbc3
......@@ -2,6 +2,8 @@ use super::chain::Chain;
use super::residue::Residue;
use super::atom::Atom;
use super::selection_atom;
/// A `Structure` is the SUPER structure which contain molecules like Protein, DNA, etc.
///
......@@ -245,4 +247,77 @@ impl Structure {
residue.add_atom(atom);
}
}
\ No newline at end of file
/// Select atoms from a pattern and return a new `Structure`
///
/// The pattern could use keywords "Chain", "Resid" or "Backbone" (keyword are not case sensitive)
///
/// ## "Chain"
/// The Chain keyword is used to select chain. It must be follow by one or two chain names separate by the "to" keyword.
/// The chain name is case sensitive.
/// examples:
/// "Chain A" will select only the Chain A.
/// "Chain A to D" will select chains A, B, C and D.
///
/// ## "Resid"
/// The Resid keyword is used to select residues. It must be follow by one or two chain names separate by the "to" keyword.
/// In case where the protein has multiple chains, the Resid will return residue(s) for all chains.
/// examples:
/// "Resid 1" will select only the residue 1 of each chain
/// "Resid 12 to 50" will select residues 12, 13, .., 50 for all chains
///
/// ## "Backbone"
/// The Backbone keyword is used to select atoms in the backbone for each residues. It don't take parameters.
///
/// ## Special keyword "and"
/// You can use the keyword "and" to separate 2 or more differents selection.
/// examples:
/// "Chain A and Resid 40 to 150"
///
/// # Examples
///
/// ```
/// use pdbparser;
///
/// let my_struct = pdbparser::read_pdb("tests/tests_file/f2.pdb", "f2");
///
/// assert_eq!(66, my_struct.get_residue_number());
/// assert_eq!(1085, my_struct.get_atom_number());
///
/// let prot_backbone = my_struct.select_atoms("resid 10 to 50 and backbone").unwrap();
///
/// assert_eq!(41, prot_backbone.get_residue_number());
/// assert_eq!(164, prot_backbone.get_atom_number());
/// ```
// TODO: The methode is idiot and need to be improve.
// ex: don't parse the chain if it's not selected
pub fn select_atoms(&self, pattern: &str) -> Option<Structure> {
let mut new_struct = Structure::new(self.name.clone());
let select = match selection_atom::parse_select(&pattern) {
Some(x) => x,
None => {
println!("Can't parse the protein with these attributes");
return None;
}
};
for chain in &self.chains {
let c_chain = chain.name;
for residue in &chain.lst_res {
let c_res = residue.res_num;
for atom in &residue.lst_atom {
if selection_atom::atom_match(&select, c_chain, c_res, atom.is_backbone) {
new_struct.update_structure(
c_chain,
residue.name.clone(),
c_res,
atom.name.clone(),
atom.number,
atom.coord,
);
}
}
}
}
Some(new_struct)
}
}
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