Author: macraild Date: Fri Mar 30 15:35:39 2007 New Revision: 3255 URL: http://svn.gna.org/viewcvs/relax?rev=3255&view=rev Log: Improvements and unit-tests for spin selection Modified: 1.3/generic_fns/selection.py 1.3/test_suite/unit_tests/generic_fns/test_selection.py Modified: 1.3/generic_fns/selection.py URL: http://svn.gna.org/viewcvs/relax/1.3/generic_fns/selection.py?rev=3255&r1=3254&r2=3255&view=diff ============================================================================== --- 1.3/generic_fns/selection.py (original) +++ 1.3/generic_fns/selection.py Fri Mar 30 15:35:39 2007 @@ -60,63 +60,62 @@ class Selection(object): """An object containing mol-res-spin selections""" - def __init__(self, selectString): + def __init__(self, select_string): self._union = None self._intersect = None - self.molecules = None - self.residues = None - self.spins = None - - if not selectString: + self.molecules = [] + self.residues = [] + self.spins = [] + + if not select_string: return - if '&' in selectString: - and_split = selectString.split('&') - Selection.__init__(self, and_split[0].strip()) - part1 = Selection(and_split[1].strip()) - self.intersection(part1) - - elif '|' in selectString: - and_split = selectString.split('|') - Selection.__init__(self, and_split[0].strip()) - part1 = Selection(and_split[1].strip()) - self.union(part1) + if '&' in select_string: + and_split = select_string.split('&') + sel0 = Selection(and_split[0].strip()) + sel1 = Selection(and_split[1].strip()) + self.intersection(sel0, sel1) + + elif '|' in select_string: + and_split = select_string.split('|') + sel0 = Selection(and_split[0].strip()) + sel1 = Selection(and_split[1].strip()) + self.union(sel0, sel1) else: - mol_token, res_token, spin_token = tokenise(selectString) + mol_token, res_token, spin_token = tokenise(select_string) self.molecules = parse_token(mol_token) self.residues = parse_token(res_token) self.spins = parse_token(spin_token) def __contains__(self, obj): - - in_self = False - if isinstance(obj, MoleculeContainer) and obj.name in self.molecules: - in_self = True + + if self._union: + return (obj in self._union[0]) or (obj in self._union[1]) + elif self._intersect: + return (obj in self._intersect[0]) and (obj in self._intersect[1]) + elif isinstance(obj, MoleculeContainer) and obj.name in self.molecules: + return True elif isinstance(obj, ResidueContainer) and obj.name in self.residues: - in_self = True + return True elif isinstance(obj, SpinContainer) and obj.name in self.spins: - in_self = True - if self._union: - return in_self or (obj in self._union) - if self._intersect: - return in_self and (obj in self._union) + return True else: - return in_self - - def intersection(self, selectObj): + return False + + def intersection(self, select_obj0, select_obj1): if self._union or self._intersect: raise RelaxError, "Cannot define multiple Boolean relationships between Selection objects" - self._intersect = selectObj + self._intersect = (select_obj0, select_obj1) - def union(self, selectObj): + def union(self, select_obj0, select_obj1): if self._union or self._intersect: raise RelaxError, "Cannot define multiple Boolean relationships between Selection objects" - self._union = selectObj + self._union = (select_obj0, select_obj1) def desel_all(self, run=None): @@ -314,7 +313,7 @@ # No token. if token == None: - return None + return [] # Split by the ',' character. elements = split(',', token) Modified: 1.3/test_suite/unit_tests/generic_fns/test_selection.py URL: http://svn.gna.org/viewcvs/relax/1.3/test_suite/unit_tests/generic_fns/test_selection.py?rev=3255&r1=3254&r2=3255&view=diff ============================================================================== --- 1.3/test_suite/unit_tests/generic_fns/test_selection.py (original) +++ 1.3/test_suite/unit_tests/generic_fns/test_selection.py Fri Mar 30 15:35:39 2007 @@ -359,6 +359,11 @@ # Increment i. i = i + 1 + def test_boolean_selection1(self): + """Test boolean mol-res-spin selections.""" + + # The selection object: + sel = selection.Selection("#Ap4Aase | #RNA@N5") def test_tokenise1(self): """Test the generic_fns.selection.tokenise() function on the string '@1'.""" @@ -652,3 +657,31 @@ # Tokenise an invalid string. self.assertRaises(RelaxError, selection.tokenise, '') + + + def test_boolean_or_selection(self): + """Test boolean or in mol-res-spin selections.""" + + self.assert_(list(selection.spin_loop("#Ap4Aase | #RNA@N5")) == list(selection.spin_loop()) + + + def test_boolean_and_selection(self): + """Test boolean and in mol-res-spin selections.""" + + # The selection loop: + sel = selection.residue_loop("#Ap4Aase:4 & :Pro") + + # Test: + for res in sel: + self.assert_(res.name == "Pro" and res.num == 4) + + + def test_boolean_complex_selection(self): + """Test complex boolean mol-res-spin selections.""" + a syntax error + # The selection loops: + sel = selection.residue_loop("#Ap4Aase:4 & :Pro | #RNA@N5") + + # Test: + for res in sel: + self.assert_(res.num in [-5,-4,4])