Commit 9dafb9c1 authored by NOEL Philippe's avatar NOEL Philippe

Add the keyword 'and' in the selection

parent 7ec7b2b7
......@@ -7,10 +7,13 @@ fn main() {
println!("Nb res : {}", toto.get_number_residue());
println!("Nb chain : {}", toto.get_number_chain());
println!("Nb atom : {}", toto.get_atom_index().len());
let toto_A = toto.select_atoms("chain A");
let toto_A = toto_A.select_atoms("backbone");
let toto_A = toto.select_atoms("chain A").unwrap();
let toto_A = toto_A.select_atoms("backbone").unwrap();
println!("Nb res : {}", toto_A.get_number_residue());
println!("Nb chain : {}", toto_A.get_number_chain());
println!("Nb atom : {}", toto_A.get_atom_index().len());
println!("Nb atom : {:?}", toto_A.get_atom_index());
let toto_A_b = toto.select_atoms("chain A and backbone").unwrap();
println!("Nb res : {}", toto_A_b.get_number_residue());
println!("Nb chain : {}", toto_A_b.get_number_chain());
println!("Nb atom : {}", toto_A_b.get_atom_index().len());
}
......@@ -98,28 +98,6 @@ impl<'a> Protein {
self.lst_chain.len() as u32
}
/// Add a new chain structure in the protein
///
/// # Examples
///
/// ````
/// use pdbparser;
///
/// let mut my_prot = pdbparser::Protein::new(String::from("my_prot"));
/// let my_chain_a = pdbparser::Chain::new('n');
///
/// my_prot.add_chain(my_chain_a);
///
/// assert_eq!(1, my_prot.get_number_chain());
///
/// ````
pub fn add_chain(&mut self, c: Chain) {
self.last_chain_added = c.get_name();
self.lst_chain.push(c);
}
/// Return the number of residue in the protein
///
/// # Examples
......@@ -173,6 +151,27 @@ impl<'a> Protein {
n
}
/// Add a new chain structure in the protein
///
/// # Examples
///
/// ````
/// use pdbparser;
///
/// let mut my_prot = pdbparser::Protein::new(String::from("my_prot"));
/// let my_chain_a = pdbparser::Chain::new('n');
///
/// my_prot.add_chain(my_chain_a);
///
/// assert_eq!(1, my_prot.get_number_chain());
///
/// ````
pub fn add_chain(&mut self, c: Chain) {
self.last_chain_added = c.get_name();
self.lst_chain.push(c);
}
/// Function that add information on the protein (used in the parsing)
/// /!\Change this to a macro!
///
......@@ -252,13 +251,16 @@ impl<'a> Protein {
/// ex: don't parse the chain if it's not selected
///
pub fn select_atoms(&self, pattern: &str) -> Protein {
pub fn select_atoms(&self, pattern: &str) -> Option<Protein> {
let mut n_prot = Protein::new(String::from("toto"));
let mut n_prot = Protein::new(String::from(self.name.clone()));
let select = match selection_atom::parse_select(&pattern) {
Some(x) => x,
None => panic!("tutu"),
None => {
println!("Can't parse the protein with these attributes");
return None
},
};
for chain in &self.lst_chain {
let c_chain = chain.name;
......@@ -271,6 +273,6 @@ impl<'a> Protein {
}
}
}
n_prot
Some(n_prot)
}
}
......@@ -63,84 +63,108 @@ fn parse_options_char(opt: &[&str]) -> Option<[char; 2]> {
/// Parse the selection and return enum and options for atom/residue/chain/backbone
///
pub fn parse_select(select: &str) -> Option<Select> {
let sel: Vec<&str> = select.split_whitespace().collect();
if sel.len() == 0 {
return None;
pub fn parse_select(select: &str) -> Option<Vec<Select>> {
if select == "" {
return None; //selection empty
}
match sel[0].to_lowercase().as_str() {
"resid" => {
match parse_options_int(&sel[1..]) {
Some(x) => return Some(Select::Resid(x[0], x[1])),
None => return None,
let mut lst_selection: Vec<Select> = Vec::new(); //vector to return
let sel: Vec<&str> = select.split("and").collect();
for pattern in sel {
let sub_pattern: Vec<&str> = pattern.split_whitespace().collect();
match sub_pattern[0].to_lowercase().as_str() {
"resid" => {
match parse_options_int(&sub_pattern[1..]) {
Some(x) => lst_selection.push(Select::Resid(x[0], x[1])),
None => return None,
}
}
}
"chain" => {
match parse_options_char(&sel[1..]) {
Some(x) => return Some(Select::Chain(x[0], x[1])),
None => return None,
"chain" => {
match parse_options_char(&sub_pattern[1..]) {
Some(x) => lst_selection.push(Select::Chain(x[0], x[1])),
None => return None,
}
}
"backbone" => lst_selection.push(Select::Backbone),
_ => {
println!("Error in the selection");
return None
},
}
"backbone" => Some(Select::Backbone),
_ => {
println!("Error in the selection");
None
},
}
Some(lst_selection)
}
pub fn atom_match(sel: &Select, chain: char, res_id: u64, back: bool) -> bool {
match sel {
Select::Chain(x, y) => {
if chain >= *x && chain <= *y {
return true
} else {
return false
}
},
Select::Backbone => {
if back {
return true
} else {
return false
}
},
Select::Resid(x, y) => {
if res_id as usize >= *x && res_id as usize <= *y {
return true
} else {
return false
}
},
pub fn atom_match(sel: &Vec<Select>, chain: char, res_id: u64, back: bool) -> bool {
let mut is_selected = false;
for pattern in sel {
match pattern {
Select::Chain(x, y) => {
if chain >= *x && chain <= *y {
is_selected = true
} else {
return false
}
},
Select::Backbone => {
if back {
is_selected = true
} else {
return false
}
},
Select::Resid(x, y) => {
if res_id as usize >= *x && res_id as usize <= *y {
is_selected = true
} else {
return false
}
},
}
}
is_selected
}
#[test]
fn test_parse_select() {
assert_eq!(None, parse_select(""));
assert_eq!(Some(Select::Resid(5, 96)), parse_select("resid 5 to 96"));
assert_eq!(None, parse_select("resid 5 t 96"));
assert_eq!(Some(Select::Backbone), parse_select("BaCkbone"));
assert_eq!(Some(Select::Chain('B', 'B')), parse_select("chain B"));
assert_eq!(Some(Select::Chain('B', 'C')), parse_select("chain B to C"));
assert_eq!(None, parse_select("resid 3 to TO"));
fn test_atom_search_chain() {
let mut s = Vec::new();
s.push(Select::Chain('B', 'C'));
assert!(atom_match(&s, 'C', 15, true));
//assert!(!atom_match(&Select::Chain('A', 'B'), 'C', 15, true));
//assert!(atom_match(&Select::Chain('1', '4'), '2', 15, true));
//assert!(atom_match(&Select::Chain('A', '9'), '2', 15));
//assert!(atom_match(&Select::Chain('A', 'a'), 'Z', 15, true));
//assert!(!atom_match(&Select::Chain('A', 'a'), 'd', 15, true));
}
#[test]
fn test_atom_search_Chain() {
assert!(atom_match(&Select::Chain('B', 'C'), 'C', 15, true));
assert!(!atom_match(&Select::Chain('A', 'B'), 'C', 15, true));
assert!(atom_match(&Select::Chain('1', '4'), '2', 15, true));
//assert!(atom_match(&Select::Chain('A', '9'), '2', 15));
assert!(atom_match(&Select::Chain('A', 'a'), 'Z', 15, true));
assert!(!atom_match(&Select::Chain('A', 'a'), 'd', 15, true));
fn test_atom_search_resid() {
//assert!(atom_match(&Select::Resid(1, 105), 'A', 15, true));
//assert!(!atom_match(&Select::Resid(1, 105), 'A', 0, true));
//assert!(atom_match(&Select::Resid(1, 105), 'A', 105, true));
assert!(true);
}
#[test]
fn test_atom_search_resid() {
assert!(atom_match(&Select::Resid(1, 105), 'A', 15, true));
assert!(!atom_match(&Select::Resid(1, 105), 'A', 0, true));
assert!(atom_match(&Select::Resid(1, 105), 'A', 105, true));
}
\ No newline at end of file
fn test_parse_select() {
assert_eq!(None, parse_select(""));
let v = parse_select("resid 5 to 96").unwrap();
println!("{:?}", v);
assert_eq!(Select::Resid(5, 96), v[0]);
/* let v = parse_select("resid 5 to 96 and chain B").unwrap();
println!("{:?}", v);
assert_eq!(Select::Resid(5, 96), v[0]);
assert_eq!(Select::Chain('B', 'B'), v[1]); */
//let v = parse_select("");
//assert_eq!(None, v);
//let v = parse_select("Select all the protein");
//assert_eq!(None, v);
}
File added
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