Package lib :: Package spectrum :: Module peak_list
[hide private]
[frames] | no frames]

Source Code for Module lib.spectrum.peak_list

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2004,2006-2013,2015 Edward d'Auvergne                    # 
  4  # Copyright (C) 2008 Sebastien Morin                                          # 
  5  # Copyright (C) 2010 Michael Bieri                                            # 
  6  # Copyright (C) 2013 Troels E. Linnet                                         # 
  7  #                                                                             # 
  8  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  9  #                                                                             # 
 10  # This program is free software: you can redistribute it and/or modify        # 
 11  # it under the terms of the GNU General Public License as published by        # 
 12  # the Free Software Foundation, either version 3 of the License, or           # 
 13  # (at your option) any later version.                                         # 
 14  #                                                                             # 
 15  # This program is distributed in the hope that it will be useful,             # 
 16  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 18  # GNU General Public License for more details.                                # 
 19  #                                                                             # 
 20  # You should have received a copy of the GNU General Public License           # 
 21  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 22  #                                                                             # 
 23  ############################################################################### 
 24   
 25  # Module docstring. 
 26  """Module containing functions for the handling of peak intensities.""" 
 27   
 28   
 29  # Python module imports. 
 30  from warnings import warn 
 31   
 32  # relax module imports. 
 33  from lib.errors import RelaxError 
 34  from lib.io import extract_data, strip 
 35  from lib.sequence import read_spin_data 
 36  from lib.spectrum import nmrpipe, nmrview, sparky, xeasy 
 37  from lib.spectrum.objects import Peak_list 
 38  from lib.warnings import RelaxWarning 
 39   
 40   
41 -def autodetect_format(file_data):
42 """Automatically detect the format of the peak list. 43 44 @param file_data: The processed results file data. 45 @type file_data: list of lists of str 46 """ 47 48 # The first header line. 49 for line in file_data: 50 if line != []: 51 break 52 53 # Sparky format. 54 if line[0] == 'Assignment': 55 return 'sparky' 56 57 # NMRView format. 58 if line == ['label', 'dataset', 'sw', 'sf']: 59 return 'nmrview' 60 61 # NMRPipe SeriesTab. 62 if line[0] == 'REMARK' and line[1] == 'SeriesTab': 63 return 'seriestab' 64 65 # XEasy format. 66 if line == ['No.', 'Color', 'w1', 'w2', 'ass.', 'in', 'w1', 'ass.', 'in', 'w2', 'Volume', 'Vol.', 'Err.', 'Method', 'Comment']: 67 return 'xeasy' 68 69 # Assume a generic format. 70 return 'generic'
71 72
73 -def intensity_generic(peak_list=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, sep=None, spin_id=None):
74 """Extract the peak intensity information from the generic column formatted peak list. 75 76 @keyword peak_list: The peak list object to place all data into. 77 @type peak_list: lib.spectrum.objects.Peak_list instance 78 @keyword file_data: The data extracted from the file converted into a list of lists. 79 @type file_data: list of lists of str 80 @keyword spin_id_col: The column containing the spin ID strings (used by the generic intensity file format). 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 (used by the generic intensity file format). If supplied, spin_id_col must be None. 81 @type mol_name_col: int or None 82 @keyword res_name_col: The column containing the residue name information (used by the generic intensity file format). If supplied, spin_id_col must be None. 83 @type res_name_col: int or None 84 @keyword res_num_col: The column containing the residue number information (used by the generic intensity file format). If supplied, spin_id_col must be None. 85 @type res_num_col: int or None 86 @keyword spin_name_col: The column containing the spin name information (used by the generic intensity file format). If supplied, spin_id_col must be None. 87 @type spin_name_col: int or None 88 @keyword spin_num_col: The column containing the spin number information (used by the generic intensity file format). If supplied, spin_id_col must be None. 89 @type spin_num_col: int or None 90 @keyword data_col: The column containing the peak intensities. 91 @type data_col: int or list of int 92 @keyword sep: The column separator which, if None, defaults to whitespace. 93 @type sep: str or None 94 @keyword spin_id: The spin ID string used to restrict data loading to a subset of all spins. 95 @type spin_id: None or str 96 @raises RelaxError: When the expected peak intensity is not a float. 97 """ 98 99 # Strip the data. 100 file_data = strip(file_data) 101 102 # Check the intensity column argument. 103 data_present = True 104 if data_col == None: 105 warn(RelaxWarning("The data column argument has not been supplied, and function will only return spin data.")) 106 data_present = False 107 108 # Convert the the data_col argument to a list if needed. 109 if not isinstance(data_col, list): 110 data_col = [data_col] 111 112 # Loop over the file data. 113 for line in file_data: 114 # Loop over the intensity columns, storing the data. 115 intensity = [] 116 data_flag = False 117 for i in range(len(data_col)): 118 # Extract the data for the single line (loop of a single element). 119 for values in read_spin_data(file_data=[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[i], sep=sep, spin_id=spin_id, raise_flag=False): 120 # The data flag. 121 data_flag = True 122 123 # Check the values. 124 if len(values) != 6 and data_present: 125 raise RelaxError("The molecule name, residue number and name, spin number and name, and value columns could not be found in the data %s." % repr(values)) 126 127 # Unpack when peak data is present 128 elif data_present: 129 # Unpack. 130 mol_name, res_num, res_name, spin_num, spin_name, value = values 131 132 # Store the intensity. 133 intensity.append(value) 134 135 # Unpack when peak data is not present. 136 elif not data_present: 137 # Unpack. 138 mol_name, res_num, res_name, spin_num, spin_name = values 139 140 # Add the assignment to the peak list object. 141 if data_flag: 142 peak_list.add(mol_names=[mol_name, mol_name], res_nums=[res_num, res_num], res_names=[res_name, res_name], spin_nums=[spin_num, spin_num], spin_names=[spin_name, spin_name], intensity=intensity)
143 144
145 -def read_peak_list(file=None, dir=None, int_col=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, sep=None, spin_id=None):
146 """Read the peak intensity data. 147 148 @keyword file: The name of the file containing the peak intensities. 149 @type file: str 150 @keyword dir: The directory where the file is located. 151 @type dir: str 152 @keyword int_col: The column containing the peak intensity data. If set to None, the auto-detection of intensity data will be attempted. 153 @type int_col: None or int 154 @keyword spin_id_col: The column containing the spin ID strings (used by the generic intensity file format). If supplied, the mol_name_col, res_name_col, res_num_col, spin_name_col, and spin_num_col arguments must be none. 155 @type spin_id_col: int or None 156 @keyword mol_name_col: The column containing the molecule name information (used by the generic intensity file format). If supplied, spin_id_col must be None. 157 @type mol_name_col: int or None 158 @keyword res_name_col: The column containing the residue name information (used by the generic intensity file format). If supplied, spin_id_col must be None. 159 @type res_name_col: int or None 160 @keyword res_num_col: The column containing the residue number information (used by the generic intensity file format). If supplied, spin_id_col must be None. 161 @type res_num_col: int or None 162 @keyword spin_name_col: The column containing the spin name information (used by the generic intensity file format). If supplied, spin_id_col must be None. 163 @type spin_name_col: int or None 164 @keyword spin_num_col: The column containing the spin number information (used by the generic intensity file format). If supplied, spin_id_col must be None. 165 @type spin_num_col: int or None 166 @keyword sep: The column separator which, if None, defaults to whitespace. 167 @type sep: str or None 168 @keyword spin_id: The spin ID string used to restrict data loading to a subset of all spins. 169 @type spin_id: None or str 170 @return: The peak list object containing all relevant data in the peak list. 171 @rtype: lib.spectrum.objects.Peak_list instance 172 """ 173 174 # Extract the data from the file. 175 file_data = extract_data(file, dir, sep=sep) 176 177 # Initialise the peak list object. 178 peak_list = Peak_list() 179 180 # Automatic format detection. 181 format = autodetect_format(file_data) 182 183 # Generic. 184 if format == 'generic': 185 # Print out. 186 print("Generic formatted data file.\n") 187 188 # Extract the data. 189 intensity_generic(peak_list=peak_list, file_data=file_data, 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=int_col, sep=sep, spin_id=spin_id) 190 191 # NMRView. 192 elif format == 'nmrview': 193 # Print out. 194 print("NMRView formatted data file.\n") 195 196 # Extract the data. 197 nmrview.read_list(peak_list=peak_list, file_data=file_data) 198 199 # NMRPipe SeriesTab. 200 elif format == 'seriestab': 201 # Print out. 202 print("NMRPipe SeriesTab formatted data file.\n") 203 204 # Extract the data. 205 nmrpipe.read_seriestab(peak_list=peak_list, file_data=file_data, int_col=int_col) 206 207 # Sparky. 208 elif format == 'sparky': 209 # Print out. 210 print("Sparky formatted data file.\n") 211 212 # Extract the data. 213 sparky.read_list(peak_list=peak_list, file_data=file_data) 214 215 # XEasy. 216 elif format == 'xeasy': 217 # Print out. 218 print("XEasy formatted data file.\n") 219 220 # Extract the data. 221 xeasy.read_list(peak_list=peak_list, file_data=file_data, int_col=int_col) 222 223 # Return the peak list object. 224 return peak_list
225