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

Source Code for Module lib.spectrum.xeasy

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2004,2006-2013 Edward d'Auvergne                         # 
  4  # Copyright (C) 2008 Sebastien Morin                                          # 
  5  # Copyright (C) 2013 Troels E. Linnet                                         # 
  6  #                                                                             # 
  7  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  8  #                                                                             # 
  9  # This program is free software: you can redistribute it and/or modify        # 
 10  # it under the terms of the GNU General Public License as published by        # 
 11  # the Free Software Foundation, either version 3 of the License, or           # 
 12  # (at your option) any later version.                                         # 
 13  #                                                                             # 
 14  # This program is distributed in the hope that it will be useful,             # 
 15  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 16  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 17  # GNU General Public License for more details.                                # 
 18  #                                                                             # 
 19  # You should have received a copy of the GNU General Public License           # 
 20  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Module docstring. 
 25  """Module containing functions for handling XEasy files.""" 
 26   
 27   
 28  # Python module imports. 
 29  from warnings import warn 
 30   
 31  # relax module imports. 
 32  from lib.errors import RelaxError 
 33  from lib.io import strip 
 34  from lib.warnings import RelaxWarning 
 35   
 36   
37 -def read_list(peak_list=None, file_data=None, int_col=None):
38 """Extract the peak intensity information from the XEasy file. 39 40 @keyword peak_list: The peak list object to place all data into. 41 @type peak_list: lib.spectrum.objects.Peak_list instance 42 @keyword file_data: The data extracted from the file converted into a list of lists. 43 @type file_data: list of lists of str 44 @keyword int_col: The column containing the peak intensity data (for a non-standard formatted file). 45 @type int_col: int 46 @raises RelaxError: When the expected peak intensity is not a float. 47 """ 48 49 # The hardcoded column positions (note that w1 and w2 are swapped!). 50 w1_col = 3 51 w2_col = 2 52 ass_w1_col = 7 53 ass_w2_col = 4 54 res_name1_col = 9 55 res_name2_col = 5 56 if int_col == None: 57 int_col = 10 58 59 # Determine the number of header lines. 60 num = 0 61 for line in file_data: 62 # Try to see if the intensity can be extracted. 63 try: 64 intensity = float(line[int_col]) 65 except ValueError: 66 num = num + 1 67 except IndexError: 68 num = num + 1 69 else: 70 break 71 print("Number of header lines found: " + repr(num)) 72 73 # Remove the header. 74 file_data = file_data[num:] 75 76 # Strip the data. 77 file_data = strip(file_data) 78 79 # Loop over the file data. 80 for line in file_data: 81 # Test for invalid assignment lines which have the column numbers changed and return empty data. 82 if line[ass_w1_col] == 'inv.' or line[ass_w2_col] == 'inv.': 83 continue 84 85 # The residue number for dimension 1. 86 try: 87 res_num1 = int(line[8]) 88 except: 89 raise RelaxError("Improperly formatted XEasy file, cannot process the residue number for dimension 1 in assignment: %s." % line) 90 91 # The residue number for dimension 2. 92 try: 93 res_num2 = int(line[5]) 94 except: 95 warn(RelaxWarning("Improperly formatted XEasy file, cannot process the residue number for dimension 2 in assignment: %s. Setting residue number to None." % line)) 96 res_num2 = None 97 98 # Nuclei names. 99 name1 = line[ass_w1_col] 100 name2 = line[ass_w2_col] 101 102 # Residue names. 103 res_name1 = line[res_name1_col] 104 res_name2 = line[res_name2_col] 105 106 # Chemical shifts. 107 try: 108 w1 = float(line[w1_col]) 109 except ValueError: 110 raise RelaxError("The w1 chemical shift from the line %s is invalid." % line) 111 try: 112 w2 = float(line[w2_col]) 113 except ValueError: 114 raise RelaxError("The w2 chemical shift from the line %s is invalid." % line) 115 116 # Intensity. 117 try: 118 intensity = float(line[int_col]) 119 except ValueError: 120 raise RelaxError("The peak intensity value from the line %s is invalid." % line) 121 122 # Add the assignment to the peak list object. 123 peak_list.add(res_nums=[res_num1, res_num2], res_names=[res_name1, res_name2], spin_names=[name1, name2], shifts=[w1, w2], intensity=intensity)
124