Commit 0d2afd1a authored by NOEL Philippe's avatar NOEL Philippe

add some tests in selection_atom and doc

parent 9dafb9c1
...@@ -60,17 +60,21 @@ fn parse_options_char(opt: &[&str]) -> Option<[char; 2]> { ...@@ -60,17 +60,21 @@ fn parse_options_char(opt: &[&str]) -> Option<[char; 2]> {
None None
} }
/// Parse the selection and return enum and options for atom/residue/chain/backbone pub fn parse_select(pattern: &str) -> Option<Vec<Select>> {
/// // The pattern is first split with "and" keyworkd into multiple sub-pattern (selection)
// sub-pattern are then compared with "resid", "chain" and "backbone" keyworkds
//
// Return None if there is a probleme in the pattern parsing
pub fn parse_select(select: &str) -> Option<Vec<Select>> {
if select == "" { if pattern == "" {
return None; //selection empty return None; //selection empty
} }
let mut lst_selection: Vec<Select> = Vec::new(); //vector to return let mut lst_selection: Vec<Select> = Vec::new(); //vector to return containing sub_patterns
let sel: Vec<&str> = select.split("and").collect();
let sel: Vec<&str> = pattern.split("and").collect();
for pattern in sel { for pattern in sel {
let sub_pattern: Vec<&str> = pattern.split_whitespace().collect(); let sub_pattern: Vec<&str> = pattern.split_whitespace().collect();
...@@ -99,55 +103,48 @@ pub fn parse_select(select: &str) -> Option<Vec<Select>> { ...@@ -99,55 +103,48 @@ pub fn parse_select(select: &str) -> Option<Vec<Select>> {
} }
pub fn atom_match(sel: &Vec<Select>, chain: char, res_id: u64, back: bool) -> bool { pub fn atom_match(sel: &Vec<Select>, chain: char, res_id: u64, is_back: bool) -> bool {
let mut is_selected = false;
// For each pattern in sel, the pattern is compare to the caracteristics of the atom
// if at any moment, the caracteristics are not ok, the function return false
// In the end, it return true (consider everythings is ok)
for pattern in sel { for pattern in sel {
match pattern { match pattern {
Select::Chain(x, y) => { Select::Chain(x, y) => {
if chain >= *x && chain <= *y { if ! (chain >= *x && chain <= *y) {
is_selected = true
} else {
return false return false
} }
}, },
Select::Backbone => { Select::Backbone => {
if back { if ! (is_back) {
is_selected = true
} else {
return false return false
} }
}, },
Select::Resid(x, y) => { Select::Resid(x, y) => {
if res_id as usize >= *x && res_id as usize <= *y { if ! (res_id as usize >= *x && res_id as usize <= *y) {
is_selected = true
} else {
return false return false
} }
}, },
} }
} }
is_selected true
} }
#[test] #[test]
fn test_atom_search_chain() { fn test_atom_search() {
let mut s = Vec::new(); let mut s = Vec::new();
s.push(Select::Chain('B', 'C')); s.push(Select::Chain('B', 'C'));
assert!(atom_match(&s, 'C', 15, true)); 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] s.push(Select::Resid(14, 155));
fn test_atom_search_resid() { assert!(atom_match(&s, 'C', 15, true));
//assert!(atom_match(&Select::Resid(1, 105), 'A', 15, true)); assert!(!atom_match(&s, 'C', 13, true));
//assert!(!atom_match(&Select::Resid(1, 105), 'A', 0, true));
//assert!(atom_match(&Select::Resid(1, 105), 'A', 105, true)); s.push(Select::Backbone);
assert!(true); assert!(atom_match(&s, 'C', 15, true));
assert!(!atom_match(&s, 'C', 15, false));
} }
#[test] #[test]
...@@ -157,14 +154,10 @@ fn test_parse_select() { ...@@ -157,14 +154,10 @@ fn test_parse_select() {
println!("{:?}", v); println!("{:?}", v);
assert_eq!(Select::Resid(5, 96), v[0]); assert_eq!(Select::Resid(5, 96), v[0]);
/* let v = parse_select("resid 5 to 96 and chain B").unwrap(); 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::Resid(5, 96), v[0]);
assert_eq!(Select::Chain('B', 'B'), v[1]); */ assert_eq!(Select::Chain('B', 'B'), v[1]);
//let v = parse_select("");
//assert_eq!(None, v);
//let v = parse_select("Select all the protein"); let v = parse_select("Select all the protein");
//assert_eq!(None, v); assert_eq!(None, v);
} }
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