Package generic_fns :: Module noesy
[hide private]
[frames] | no frames]

Source Code for Module generic_fns.noesy

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-2009 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Module for NOESY related operations.""" 
 25   
 26  # Python module imports. 
 27  from re import search 
 28  from string import split 
 29  from warnings import warn 
 30   
 31  # relax module imports. 
 32  from generic_fns import pipes 
 33  from generic_fns.mol_res_spin import exists_mol_res_spin_data, return_spin, tokenise 
 34  from generic_fns import xplor 
 35  from relax_errors import RelaxError, RelaxNoSequenceError 
 36  from relax_io import open_read_file 
 37  from relax_warnings import RelaxWarning 
 38   
 39   
40 -def __file_format(lines):
41 """Determine the format of the NOE restraints data. 42 43 @param lines: The file data converted to a list of file lines. 44 @type lines: list of str 45 @return: The format of the file. 46 @rtype: str 47 """ 48 49 # Loop over the lines. 50 for line in lines: 51 # Xplor format. 52 if search('^assign ', line): 53 print("Xplor formatted file.") 54 return 'xplor' 55 56 # Print out. 57 print("Generic formatted file.") 58 return 'generic'
59 60
61 -def parse_noe_restraints(lines, proton1_col=None, proton2_col=None, lower_col=None, upper_col=None, sep=None):
62 """Parse and return the NOE restraints from the generic formatted file. 63 64 @param lines: The file, or file fragment, split into lines. 65 @type lines: list of str 66 @keyword proton1_col: The column containing the first proton of the NOE or ROE cross peak. 67 @type proton1_col: None or int 68 @keyword proton2_col: The column containing the second proton of the NOE or ROE cross peak. 69 @type proton2_col: None or int 70 @keyword lower_col: The column containing the lower NOE bound. 71 @type lower_col: None or int 72 @keyword upper_col: The column containing the upper NOE bound. 73 @type upper_col: None or int 74 @keyword sep: The column separator (the default is white space). 75 @type sep: None or str 76 @return: The NOE restraint list in the format of two atom identification strings 77 and the lower and upper restraints. 78 @rtype: list of lists of [str, str, float, float] 79 """ 80 81 # Default column numbers. 82 if proton1_col == None: 83 warn(RelaxWarning("The proton1_col argument has not been supplied, defaulting to column 1.")) 84 proton1_col = 1 85 if proton2_col == None: 86 warn(RelaxWarning("The proton2_col argument has not been supplied, defaulting to column 2.")) 87 proton2_col = 2 88 if lower_col == None: 89 warn(RelaxWarning("The lower_col argument has not been supplied, defaulting to column 3.")) 90 lower_col = 3 91 if upper_col == None: 92 warn(RelaxWarning("The upper_col argument has not been supplied, defaulting to column 4.")) 93 upper_col = 4 94 95 # Loop over the lines. 96 data = [] 97 for line in lines: 98 # Split the line. 99 row = split(line, sep) 100 101 # Header lines: 102 if len(row) < 4: 103 continue 104 try: 105 tokenise(row[proton1_col-1]) 106 except RelaxError: 107 continue 108 109 # Pack the data. 110 data.append([row[proton1_col-1], row[proton2_col-1], float(row[lower_col-1]), float(row[upper_col-1])]) 111 112 # Return the data. 113 return data
114 115
116 -def read_restraints(file=None, dir=None, proton1_col=None, proton2_col=None, lower_col=None, upper_col=None, sep=None):
117 """Load NOESY or ROESY constraint information from file. 118 119 If the input file is a pre-formatted Xplor file, the column number and separator arguments will 120 be ignored. 121 122 123 @keyword file: The name of the file containing the relaxation data. 124 @type file: str 125 @keyword dir: The directory where the file is located. 126 @type dir: None or str 127 @keyword proton1_col: The column containing the first proton of the NOE or ROE cross peak. 128 @type proton1_col: None or int 129 @keyword proton2_col: The column containing the second proton of the NOE or ROE cross peak. 130 @type proton2_col: None or int 131 @keyword lower_col: The column containing the lower NOE bound. 132 @type lower_col: None or int 133 @keyword upper_col: The column containing the upper NOE bound. 134 @type upper_col: None or int 135 @keyword sep: The column separator (the default is white space). 136 @type sep: None or str 137 """ 138 139 # Test if the current data pipe exists. 140 pipes.test() 141 142 # Test if sequence data is loaded. 143 if not exists_mol_res_spin_data(): 144 raise RelaxNoSequenceError 145 146 # Open the file. 147 file = open_read_file(file_name=file, dir=dir) 148 lines = file.readlines() 149 file.close() 150 151 # Determine the file type. 152 format = __file_format(lines) 153 154 # Parse and extract the NOE restraints. 155 if format == 'xplor': 156 noe_restraints = xplor.parse_noe_restraints(lines) 157 elif format == 'generic': 158 noe_restraints = parse_noe_restraints(lines, proton1_col=proton1_col, proton2_col=proton2_col, lower_col=lower_col, upper_col=upper_col, sep=sep) 159 160 # Pseudoatom conversion. 161 for i in range(len(noe_restraints)): 162 # Loop over atom IDs. 163 for j in range(2): 164 # Skip normal atoms. 165 if isinstance(noe_restraints[i][j], str): 166 continue 167 168 # Loop over the pseudoatoms. 169 pseudo_name = None 170 for k in range(len(noe_restraints[i][j])): 171 # Get the spin. 172 spin = return_spin(noe_restraints[i][j][k]) 173 174 # Check the pseudoatom consistency. 175 if pseudo_name and pseudo_name != spin.pseudo_name: 176 raise RelaxError("The pseudoatom names '%s' and '%s' do not match." % (pseudo_name, spin.pseudo_name)) 177 178 # Set the name. 179 pseudo_name = spin.pseudo_name 180 181 # No pseudoatom. 182 if not pseudo_name: 183 raise RelaxError("Cannot find the pseudoatom corresponding to the atoms in %s." % noe_restraints[i][j]) 184 185 # Otherwise, place the pseudoatom name into the NOE restraint list. 186 noe_restraints[i][j] = pseudo_name 187 188 # Place the restraints into the current data pipe. 189 cdp.noe_restraints = noe_restraints 190 191 # Check for the presence of the spin containers corresponding to the atom ids. 192 for restraint in cdp.noe_restraints: 193 if not return_spin(restraint[0]): 194 raise RelaxError("The spin container corresponding to '%s' cannot be found." % restraint[0]) 195 if not return_spin(restraint[1]): 196 raise RelaxError("The spin container corresponding to '%s' cannot be found." % restraint[1])
197