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