def sel_domain(domain_id=None, boolean='OR', change_all=False): """Select all spins and interatomic data containers of the given domain. @keyword domain_id: The domain ID string. @type domain_id: str or None @param boolean: The boolean operator used to select the spin systems with. It can be one of 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'. This will be ignored if the change_all flag is set. @type boolean: str @keyword change_all: A flag which if True will cause all spins and interatomic data containers outside of the domain to be deselected. @type change_all: bool """ # Test if the current data pipe exists. pipes.test() # Test if the domain is defined. if not hasattr(cdp, 'domain') or domain_id not in cdp.domain: raise RelaxNoDomainError(domain_id) # The domain selection object. domain = Selection(cdp.domain[domain_id]) # Loop over the spins and select as required. for spin, mol_name, res_num, res_name in spin_loop(full_info=True): # Inside the domain. if domain.contains_spin(spin_name=spin.name, spin_num=spin.num, res_name=res_name, res_num=res_num, mol=mol_name): spin.select = boolean_select(current=spin.select, boolean=boolean) # Deselect spins outside of the domain. elif change_all: spin.select = False # Interatomic data loop. for interatom in interatomic_loop(): # Decode the spin ids. mol_name1, res_num1, res_name1, spin_num1, spin_name1 = spin_id_to_data_list(interatom.spin_id1) mol_name2, res_num2, res_name2, spin_num2, spin_name2 = spin_id_to_data_list(interatom.spin_id2) # Inside the domain. if domain.contains_spin(spin_name=spin_name1, spin_num=spin_num1, res_name=res_name1, res_num=res_num1, mol=mol_name1) or domain.contains_spin(spin_name=spin_name2, spin_num=spin_num2, res_name=res_name2, res_num=res_num2, mol=mol_name2): interatom.select = boolean_select(current=interatom.select, boolean=boolean) # Deselect containers outside of the domain. elif change_all: interatom.select = False
def sel_domain(domain_id=None, boolean='OR', change_all=False): """Select all spins and interatomic data containers of the given domain. @keyword domain_id: The domain ID string. @type domain_id: str or None @param boolean: The boolean operator used to select the spin systems with. It can be one of 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'. This will be ignored if the change_all flag is set. @type boolean: str @keyword change_all: A flag which if True will cause all spins and interatomic data containers outside of the domain to be deselected. @type change_all: bool """ # Test if the current data pipe exists. check_pipe() # Test if the domain is defined. if not hasattr(cdp, 'domain') or domain_id not in cdp.domain: raise RelaxNoDomainError(domain_id) # The domain selection object. domain = Selection(cdp.domain[domain_id]) # Loop over the spins and select as required. for spin, mol_name, res_num, res_name in spin_loop(full_info=True): # Inside the domain. if domain.contains_spin(spin_name=spin.name, spin_num=spin.num, res_name=res_name, res_num=res_num, mol=mol_name): spin.select = boolean_select(current=spin.select, boolean=boolean) # Deselect spins outside of the domain. elif change_all: spin.select = False # Interatomic data loop. for interatom in interatomic_loop(): # Decode the spin ids. mol_name1, res_num1, res_name1, spin_num1, spin_name1 = spin_id_to_data_list(interatom.spin_id1) mol_name2, res_num2, res_name2, spin_num2, spin_name2 = spin_id_to_data_list(interatom.spin_id2) # Inside the domain. if domain.contains_spin(spin_name=spin_name1, spin_num=spin_num1, res_name=res_name1, res_num=res_num1, mol=mol_name1) or domain.contains_spin(spin_name=spin_name2, spin_num=spin_num2, res_name=res_name2, res_num=res_num2, mol=mol_name2): interatom.select = boolean_select(current=interatom.select, boolean=boolean) # Deselect containers outside of the domain. elif change_all: interatom.select = False
def read_spin_data(file=None, dir=None, file_data=None, spin_id_col=None, mol_name_col=None, res_num_col=None, res_name_col=None, spin_num_col=None, spin_name_col=None, data_col=None, error_col=None, sep=None, spin_id=None, raise_flag=True): """Generator function for reading the spin specific data from file. Description =========== This function reads a columnar formatted file where each line corresponds to a spin system. Spin identification is either through a spin ID string or through columns containing the molecule name, residue name and number, and/or spin name and number. @keyword file: The name of the file to open. @type file: str @keyword dir: The directory containing the file (defaults to the current directory if None). @type dir: str or None @keyword file_data: An alternative to opening a file, if the data already exists in the correct format. The format is a list of lists where the first index corresponds to the row and the second the column. @type file_data: list of lists @keyword spin_id_col: The column containing the spin ID strings. If supplied, the mol_name_col, res_name_col, res_num_col, spin_name_col, and spin_num_col arguments must be none. @type spin_id_col: int or None @keyword mol_name_col: The column containing the molecule name information. If supplied, spin_id_col must be None. @type mol_name_col: int or None @keyword res_name_col: The column containing the residue name information. If supplied, spin_id_col must be None. @type res_name_col: int or None @keyword res_num_col: The column containing the residue number information. If supplied, spin_id_col must be None. @type res_num_col: int or None @keyword spin_name_col: The column containing the spin name information. If supplied, spin_id_col must be None. @type spin_name_col: int or None @keyword spin_num_col: The column containing the spin number information. If supplied, spin_id_col must be None. @type spin_num_col: int or None @keyword data_col: The column containing the data. @type data_col: int or None @keyword error_col: The column containing the errors. @type error_col: int or None @keyword sep: The column separator which, if None, defaults to whitespace. @type sep: str or None @keyword spin_id: The spin ID string used to restrict data loading to a subset of all spins. @type spin_id: None or str @keyword raise_flag: A flag which if True will cause a RelaxError to be raised if no data can be found. @type raise_flag: bool @return: A list of the spin specific data is yielded. The format is a list consisting of the spin ID string, the data value (if data_col is give), and the error value (if error_col is given). If both data_col and error_col are None, then the spin ID string is simply yielded. @rtype: str, list of [str, float], or list of [str, float, float] """ # Argument tests. col_args = [spin_id_col, mol_name_col, res_name_col, res_num_col, spin_name_col, spin_num_col, data_col, error_col] col_arg_names = ['spin_id_col', 'mol_name_col', 'res_name_col', 'res_num_col', 'spin_name_col', 'spin_num_col', 'data_col', 'error_col'] for i in range(len(col_args)): if col_args[i] == 0: raise RelaxError("The '%s' argument cannot be zero, column numbering starts at one." % col_arg_names[i]) if spin_id_col and (mol_name_col or res_name_col or res_num_col or spin_name_col or spin_num_col): raise RelaxError("If the 'spin_id_col' argument has been supplied, then the mol_name_col, res_name_col, res_num_col, spin_name_col, and spin_num_col must all be set to None.") # Minimum number of columns. min_col_num = max([_f for _f in [spin_id_col, mol_name_col, res_num_col, res_name_col, spin_num_col, spin_name_col, data_col, error_col] if _f]) # Extract the data from the file. if not file_data: # Extract. file_data = extract_data(file, dir, sep=sep) # Strip the data of all comments and empty lines. if spin_id_col != None: file_data = strip(file_data, comments=False) else: file_data = strip(file_data) # No data! if not file_data: warn(RelaxFileEmptyWarning(file)) return # Yield the data, spin by spin. missing_data = True for line in file_data: # Convert the spin IDs. if spin_id_col != None and line[spin_id_col-1][0] in ["\"", "\'"]: line[spin_id_col-1] = eval(line[spin_id_col-1]) # Convert. # Validate the sequence. if not check_sequence(line, spin_id_col=spin_id_col, mol_name_col=mol_name_col, res_num_col=res_num_col, res_name_col=res_name_col, spin_num_col=spin_num_col, spin_name_col=spin_name_col, data_col=data_col, error_col=error_col, escalate=1): continue # Get the spin data from the ID. if spin_id_col: # Invalid spin ID. if line[spin_id_col-1] == '#': warn(RelaxWarning("Invalid spin ID, skipping the line %s" % line)) continue mol_name, res_num, res_name, spin_num, spin_name = spin_id_to_data_list(line[spin_id_col-1]) # Convert the spin data. else: # The molecule. mol_name = None if mol_name_col != None and line[mol_name_col-1] != 'None': mol_name = line[mol_name_col-1] # The residue number, catching bad values. res_num = None if res_num_col != None: try: if line[res_num_col-1] == 'None': res_num = None else: res_num = int(line[res_num_col-1]) except ValueError: warn(RelaxWarning("Invalid residue number, skipping the line %s" % line)) continue # The residue name. res_name = None if res_name_col != None and line[res_name_col-1] != 'None': res_name = line[res_name_col-1] # The spin number, catching bad values. spin_num = None if spin_num_col != None: try: if line[spin_num_col-1] == 'None': spin_num = None else: spin_num = int(line[spin_num_col-1]) except ValueError: warn(RelaxWarning("Invalid spin number, skipping the line %s" % line)) continue # The spin name. spin_name = None if spin_name_col != None and line[spin_name_col-1] != 'None': spin_name = line[spin_name_col-1] # Convert the data. value = None if data_col != None: try: # None. if line[data_col-1] == 'None': value = None # A float. else: value = float(line[data_col-1]) # If it a float, test if is nan. if not isFinite(value): warn(RelaxWarning("The value is not finite, skipping the line %s" % line)) continue # Bad data. except ValueError: warn(RelaxWarning("Invalid data, skipping the line %s" % line)) continue # Convert the errors. error = None if error_col != None: try: # None. if line[error_col-1] == 'None': error = None # A float. else: error = float(line[error_col-1]) # If it a float, test if is nan. if not isFinite(error): warn(RelaxWarning("The error is not finite, skipping the line %s" % line)) continue # Bad data. except ValueError: warn(RelaxWarning("Invalid errors, skipping the line %s" % line)) continue # Right, data is OK and exists. missing_data = False # Yield the data. if data_col and error_col: yield mol_name, res_num, res_name, spin_num, spin_name, value, error elif data_col: yield mol_name, res_num, res_name, spin_num, spin_name, value elif error_col: yield mol_name, res_num, res_name, spin_num, spin_name, error else: yield mol_name, res_num, res_name, spin_num, spin_name # Hmmm, no data! if raise_flag and missing_data: raise RelaxError("No corresponding data could be found within the file.")
def read_spin_data(file=None, dir=None, file_data=None, spin_id_col=None, mol_name_col=None, res_num_col=None, res_name_col=None, spin_num_col=None, spin_name_col=None, data_col=None, error_col=None, sep=None, spin_id=None, raise_flag=True): """Generator function for reading the spin specific data from file. Description =========== This function reads a columnar formatted file where each line corresponds to a spin system. Spin identification is either through a spin ID string or through columns containing the molecule name, residue name and number, and/or spin name and number. @keyword file: The name of the file to open. @type file: str @keyword dir: The directory containing the file (defaults to the current directory if None). @type dir: str or None @keyword file_data: An alternative to opening a file, if the data already exists in the correct format. The format is a list of lists where the first index corresponds to the row and the second the column. @type file_data: list of lists @keyword spin_id_col: The column containing the spin ID strings. If supplied, the mol_name_col, res_name_col, res_num_col, spin_name_col, and spin_num_col arguments must be none. @type spin_id_col: int or None @keyword mol_name_col: The column containing the molecule name information. If supplied, spin_id_col must be None. @type mol_name_col: int or None @keyword res_name_col: The column containing the residue name information. If supplied, spin_id_col must be None. @type res_name_col: int or None @keyword res_num_col: The column containing the residue number information. If supplied, spin_id_col must be None. @type res_num_col: int or None @keyword spin_name_col: The column containing the spin name information. If supplied, spin_id_col must be None. @type spin_name_col: int or None @keyword spin_num_col: The column containing the spin number information. If supplied, spin_id_col must be None. @type spin_num_col: int or None @keyword data_col: The column containing the data. @type data_col: int or None @keyword error_col: The column containing the errors. @type error_col: int or None @keyword sep: The column separator which, if None, defaults to whitespace. @type sep: str or None @keyword spin_id: The spin ID string used to restrict data loading to a subset of all spins. @type spin_id: None or str @keyword raise_flag: A flag which if True will cause a RelaxError to be raised if no data can be found. @type raise_flag: bool @return: A list of the spin specific data is yielded. The format is a list consisting of the spin ID string, the data value (if data_col is give), and the error value (if error_col is given). If both data_col and error_col are None, then the spin ID string is simply yielded. @rtype: str, list of [str, float], or list of [str, float, float] """ # Argument tests. col_args = [spin_id_col, mol_name_col, res_name_col, res_num_col, spin_name_col, spin_num_col, data_col, error_col] col_arg_names = ['spin_id_col', 'mol_name_col', 'res_name_col', 'res_num_col', 'spin_name_col', 'spin_num_col', 'data_col', 'error_col'] for i in range(len(col_args)): if col_args[i] == 0: raise RelaxError("The '%s' argument cannot be zero, column numbering starts at one." % col_arg_names[i]) if spin_id_col and (mol_name_col or res_name_col or res_num_col or spin_name_col or spin_num_col): raise RelaxError("If the 'spin_id_col' argument has been supplied, then the mol_name_col, res_name_col, res_num_col, spin_name_col, and spin_num_col must all be set to None.") # Minimum number of columns. min_col_num = max([_f for _f in [spin_id_col, mol_name_col, res_num_col, res_name_col, spin_num_col, spin_name_col, data_col, error_col] if _f]) # Extract the data from the file. if not file_data: # Extract. file_data = extract_data(file, dir, sep=sep) # Strip the data of all comments and empty lines. if spin_id_col != None: file_data = strip(file_data, comments=False) else: file_data = strip(file_data) # No data! if not file_data: warn(RelaxFileEmptyWarning(file)) return # Yield the data, spin by spin. missing_data = True for line in file_data: # Convert the spin IDs. if spin_id_col != None and line[spin_id_col-1][0] in ["\"", "\'"]: line[spin_id_col-1] = eval(line[spin_id_col-1]) # Convert. # Validate the sequence. if not check_sequence(line, spin_id_col=spin_id_col, mol_name_col=mol_name_col, res_num_col=res_num_col, res_name_col=res_name_col, spin_num_col=spin_num_col, spin_name_col=spin_name_col, data_col=data_col, error_col=error_col, escalate=1): continue # Get the spin data from the ID. if spin_id_col: # Invalid spin ID. if line[spin_id_col-1] == '#': warn(RelaxWarning("Invalid spin ID, skipping the line %s" % line)) continue mol_name, res_num, res_name, spin_num, spin_name = spin_id_to_data_list(line[spin_id_col-1]) # Convert the spin data. else: # The molecule. mol_name = None if mol_name_col != None and line[mol_name_col-1] != 'None': mol_name = line[mol_name_col-1] # The residue number, catching bad values. res_num = None if res_num_col != None: try: if line[res_num_col-1] == 'None': res_num = None else: res_num = int(line[res_num_col-1]) except ValueError: warn(RelaxWarning("Invalid residue number, skipping the line %s" % line)) continue # The residue name. res_name = None if res_name_col != None and line[res_name_col-1] != 'None': res_name = line[res_name_col-1] # The spin number, catching bad values. spin_num = None if spin_num_col != None: try: if line[spin_num_col-1] == 'None': spin_num = None else: spin_num = int(line[spin_num_col-1]) except ValueError: warn(RelaxWarning("Invalid spin number, skipping the line %s" % line)) continue # The spin name. spin_name = None if spin_name_col != None and line[spin_name_col-1] != 'None': spin_name = line[spin_name_col-1] # Convert the data. value = None if data_col != None: try: # None. if line[data_col-1] == 'None': value = None # A float. else: value = float(line[data_col-1]) # If it a float, test if is nan. if isnan(value): warn(RelaxWarning("The value is 'nan', skipping the line %s" % line)) continue # Bad data. except ValueError: warn(RelaxWarning("Invalid data, skipping the line %s" % line)) continue # Convert the errors. error = None if error_col != None: try: # None. if line[error_col-1] == 'None': error = None # A float. else: error = float(line[error_col-1]) # If it a float, test if is nan. if isnan(error): warn(RelaxWarning("The error is 'nan', skipping the line %s" % line)) continue # Bad data. except ValueError: warn(RelaxWarning("Invalid errors, skipping the line %s" % line)) continue # Right, data is OK and exists. missing_data = False # Yield the data. if data_col and error_col: yield mol_name, res_num, res_name, spin_num, spin_name, value, error elif data_col: yield mol_name, res_num, res_name, spin_num, spin_name, value elif error_col: yield mol_name, res_num, res_name, spin_num, spin_name, error else: yield mol_name, res_num, res_name, spin_num, spin_name # Hmmm, no data! if raise_flag and missing_data: raise RelaxError("No corresponding data could be found within the file.")