mailr2927 - in /1.3: generic_fns/selection.py prompt/select.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on December 08, 2006 - 03:22:
Author: bugman
Date: Fri Dec  8 03:21:49 2006
New Revision: 2927

URL: http://svn.gna.org/viewcvs/relax?rev=2927&view=rev
Log:
Spin systems can now be selected using boolean operators.

The user functions 'select.res()' and 'select.read()' now accept the boolean 
keyword option which
can be set to 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'.  The default, and 
previous behaviour, is
'OR'.


Modified:
    1.3/generic_fns/selection.py
    1.3/prompt/select.py

Modified: 1.3/generic_fns/selection.py
URL: 
http://svn.gna.org/viewcvs/relax/1.3/generic_fns/selection.py?rev=2927&r1=2926&r2=2927&view=diff
==============================================================================
--- 1.3/generic_fns/selection.py (original)
+++ 1.3/generic_fns/selection.py Fri Dec  8 03:21:49 2006
@@ -1,6 +1,6 @@
 
###############################################################################
 #                                                                            
 #
-# Copyright (C) 2003, 2004 Edward d'Auvergne                                 
 #
+# Copyright (C) 2003, 2004, 2006 Edward d'Auvergne                           
 #
 #                                                                            
 #
 # This file is part of the program relax.                                    
 #
 #                                                                            
 #
@@ -80,8 +80,24 @@
                 self.relax.data.res[self.run][i].select = 1
 
 
-    def sel_read(self, run=None, file=None, dir=None, change_all=None, 
column=None):
-        """Function for selecting the residues contained in a file."""
+    def sel_read(self, run=None, file=None, dir=None, boolean='OR', 
change_all=0, column=None):
+        """Select the residues contained in the given file.
+
+        @param run:         The run name.
+        @type run:          str
+        @param file:        The name of the file.
+        @type file:         str
+        @param dir:         The directory containing the file.
+        @type dir:          str
+        @param boolean:     The boolean operator used to select the spin 
systems with.  It can be
+            one of 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'.
+        @type boolean:      str
+        @param change_all:  A flag which if set will set the selection to 
solely those of the file.
+        @type change_all:   int
+        @param column:      The whitespace separated column in which the 
residue numbers are
+            located.
+        @type column:       int
+        """
 
         # Extract the data from the file.
         file_data = self.relax.IO.extract_data(file, dir)
@@ -114,7 +130,6 @@
         self.runs = self.relax.generic.runs.list_of_runs(run)
 
         # Loop over the runs.
-        no_match = 1
         for self.run in self.runs:
             # Test if the run exists.
             if not self.run in self.relax.data.run_names:
@@ -129,24 +144,50 @@
                 # Remap the data structure 
'self.relax.data.res[self.run][i]'.
                 data = self.relax.data.res[self.run][i]
 
-                # Unselect all residues.
+                # The spin system is in the new selection list.
+                if data.num in select:
+                    new_select = 1
+                else:
+                    new_select = 0
+
+                # Select just the residues in the file.
                 if change_all:
-                    data.select = 0
-
-                # Select the residue if it is in the list select.
-                if data.num in select:
-                    data.select = 1
-
-                # Match flag.
-                no_match = 0
-
-        # No residue matched.
-        if no_match:
-            print "No residues match."
-
-
-    def sel_res(self, run=None, num=None, name=None, change_all=None):
-        """Function for selecting specific residues."""
+                    data.select = new_select
+
+                # Boolean selections.
+                if boolean == 'OR':
+                    data.select = data.select or new_select
+                elif boolean == 'NOR':
+                    data.select = not (data.select or new_select)
+                elif boolean == 'AND':
+                    data.select = data.select and new_select
+                elif boolean == 'NAND':
+                    data.select = not (data.select and new_select)
+                elif boolean == 'XOR':
+                    data.select = not (data.select and new_select) and 
(data.select or new_select)
+                elif boolean == 'XNOR':
+                    data.select = (data.select and new_select) or not 
(data.select or new_select)
+                else:
+                    raise RelaxError, "Unknown boolean operator " + `boolean`
+
+
+
+    def sel_res(self, run=None, num=None, name=None, boolean='OR', 
change_all=0):
+        """Select specific residues.
+        
+        @param run:         The run name.
+        @type run:          str
+        @param num:         The residue number.
+        @type num:          int or regular expression str
+        @param name:        The residue name.
+        @type name:         regular expression str
+        @param boolean:     The boolean operator used to select the spin 
systems with.  It can be
+            one of 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'.
+        @type boolean:      str
+        @param change_all:  A flag which if set will set the selection to 
solely those residues
+            specified.
+        @type change_all:   int
+        """
 
         # Test if the residue number is a valid regular expression.
         if type(num) == str:
@@ -181,28 +222,45 @@
                 # Remap the data structure 
'self.relax.data.res[self.run][i]'.
                 data = self.relax.data.res[self.run][i]
 
-                # Unselect all residues.
+                # Initialise the new selection flag.
+                new_select = 0
+
+                # Set the new selection flag if the residue matches 'num'.
+                if type(num) == int:
+                    if data.num == num:
+                        new_select = 1
+                elif type(num) == str:
+                    if match(num, `data.num`):
+                        new_select = 1
+
+                # Set the new selection flag if the residue matches 'name'.
+                if name != None:
+                    if match(name, data.name):
+                        new_select = 1
+
+                # Select just the specified residues.
                 if change_all:
-                    data.select = 0
-
-                # Skip the residue if there is no match to 'num'.
-                if type(num) == int:
-                    if not data.num == num:
-                        continue
-                elif type(num) == str:
-                    if not match(num, `data.num`):
-                        continue
-
-                # Skip the residue if there is no match to 'name'.
-                if name != None:
-                    if not match(name, data.name):
-                        continue
-
-                # Select the residue.
-                data.select = 1
+                    data.select = new_select
+
+                # Boolean selections.
+                if boolean == 'OR':
+                    data.select = data.select or new_select
+                elif boolean == 'NOR':
+                    data.select = not (data.select or new_select)
+                elif boolean == 'AND':
+                    data.select = data.select and new_select
+                elif boolean == 'NAND':
+                    data.select = not (data.select and new_select)
+                elif boolean == 'XOR':
+                    data.select = not (data.select and new_select) and 
(data.select or new_select)
+                elif boolean == 'XNOR':
+                    data.select = (data.select and new_select) or not 
(data.select or new_select)
+                else:
+                    raise RelaxError, "Unknown boolean operator " + `boolean`
 
                 # Match flag.
-                no_match = 0
+                if new_selection:
+                    no_match = 0
 
         # No residue matched.
         if no_match:

Modified: 1.3/prompt/select.py
URL: 
http://svn.gna.org/viewcvs/relax/1.3/prompt/select.py?rev=2927&r1=2926&r2=2927&view=diff
==============================================================================
--- 1.3/prompt/select.py (original)
+++ 1.3/prompt/select.py Fri Dec  8 03:21:49 2006
@@ -1,6 +1,6 @@
 
###############################################################################
 #                                                                            
 #
-# Copyright (C) 2003, 2004 Edward d'Auvergne                                 
 #
+# Copyright (C) 2003, 2004, 2006 Edward d'Auvergne                           
 #
 #                                                                            
 #
 # This file is part of the program relax.                                    
 #
 #                                                                            
 #
@@ -26,6 +26,38 @@
 
 
 class Select:
+    boolean_doc = """
+        Boolean operators
+        ~~~~~~~~~~~~~~~~~
+
+        The 'boolean' keyword argument can be used to change how spin 
systems are selected.  The
+        allowed values are: 'OR', 'NOR', 'AND', 'NAND', 'XOR', 'XNOR'.  The 
following table details
+        how the selections will occur for the different boolean operators.
+        __________________________________________________________
+        |                    |   |   |   |   |   |   |   |   |   |
+        | Spin system        | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+        |____________________|___|___|___|___|___|___|___|___|___|
+        |                    |   |   |   |   |   |   |   |   |   |
+        | Original selection | 0 | 1 | 1 | 1 | 1 | 0 | 1 | 0 | 1 |
+        |                    |   |   |   |   |   |   |   |   |   |
+        | New selection      | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
+        |____________________|___|___|___|___|___|___|___|___|___|
+        |                    |   |   |   |   |   |   |   |   |   |
+        | OR                 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 |
+        |                    |   |   |   |   |   |   |   |   |   |
+        | NOR                | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
+        |                    |   |   |   |   |   |   |   |   |   |
+        | AND                | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
+        |                    |   |   |   |   |   |   |   |   |   |
+        | NAND               | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
+        |                    |   |   |   |   |   |   |   |   |   |
+        | XOR                | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 |
+        |                    |   |   |   |   |   |   |   |   |   |
+        | XNOR               | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 0 |
+        |____________________|___|___|___|___|___|___|___|___|___|
+    """
+
+
     def __init__(self, relax):
         # Help.
         self.__relax_help__ = \
@@ -80,7 +112,7 @@
         self.__relax__.generic.selection.sel_all(run=run)
 
 
-    def read(self, run=None, file=None, dir=None, change_all=0, column=0):
+    def read(self, run=None, file=None, dir=None, boolean='OR', 
change_all=0, column=0):
         """Function for selecting the residues contained in a file.
 
         Keyword Arguments
@@ -92,6 +124,8 @@
         file:  The name of the file containing the list of residues to 
select.
 
         dir:  The directory where the file is located.
+
+        boolean:  The boolean operator specifying how residues should be 
selected.
 
         change_all:  A flag specifying if all other residues should be 
changed.
 
@@ -129,6 +163,7 @@
             text = text + "run=" + `run`
             text = text + ", file=" + `file`
             text = text + ", dir=" + `dir`
+            text = text + ", boolean=" + `boolean`
             text = text + ", change_all=" + `change_all`
             text = text + ", column=" + `column` + ")"
             print text
@@ -149,6 +184,10 @@
         if dir != None and type(dir) != str:
             raise RelaxNoneStrError, ('directory name', dir)
 
+        # Boolean operator.
+        if type(boolean) != str:
+            raise RelaxStrError, ('boolean operator', boolean)
+
         # Change all flag.
         if type(change_all) != int or (change_all != 0 and change_all != 1):
             raise RelaxBinError, ('change_all', change_all)
@@ -158,10 +197,10 @@
             raise RelaxIntError, ('residue number column', column)
 
         # Execute the functional code.
-        self.__relax__.generic.selection.sel_read(run=run, file=file, 
dir=dir, change_all=change_all, column=column)
-
-
-    def res(self, run=None, num=None, name=None, change_all=0):
+        self.__relax__.generic.selection.sel_read(run=run, file=file, 
dir=dir, boolean=boolean, change_all=change_all, column=column)
+
+
+    def res(self, run=None, num=None, name=None, boolean='OR', change_all=0):
         """Function for selecting specific residues.
 
         Keyword Arguments
@@ -173,6 +212,8 @@
         num:  The residue number.
 
         name:  The residue name.
+
+        boolean:  The boolean operator specifying how residues should be 
selected.
 
         change_all:  A flag specifying if all other residues should be 
changed.
 
@@ -215,6 +256,7 @@
             text = text + "run=" + `run`
             text = text + ", num=" + `num`
             text = text + ", name=" + `name`
+            text = text + ", boolean=" + `boolean`
             text = text + ", change_all=" + `change_all` + ")"
             print text
 
@@ -238,12 +280,16 @@
         if num == None and name == None:
             raise RelaxError, "At least one of the number or name arguments 
is required."
 
+        # Boolean operator.
+        if type(boolean) != str:
+            raise RelaxStrError, ('boolean operator', boolean)
+
         # Change all flag.
         if type(change_all) != int or (change_all != 0 and change_all != 1):
             raise RelaxBinError, ('change_all', change_all)
 
         # Execute the functional code.
-        self.__relax__.generic.selection.sel_res(run=run, num=num, 
name=name, change_all=change_all)
+        self.__relax__.generic.selection.sel_res(run=run, num=num, 
name=name, boolean=boolean, change_all=change_all)
 
 
     def reverse(self, run=None):
@@ -280,3 +326,11 @@
 
         # Execute the functional code.
         self.__relax__.generic.selection.reverse(run=run)
+
+
+
+    # Docstring modification.
+    #########################
+
+    # Read function.
+    read.__doc__ = read.__doc__ + "\n\n" + boolean_doc + "\n"




Related Messages


Powered by MHonArc, Updated Fri Dec 08 08:00:06 2006