1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Module containing functions for handling Sparky files."""
25
26
27
28 from re import split
29
30
31 from lib.errors import RelaxError
32 from lib.io import open_write_file, strip
33
34
35 -def read_list(peak_list=None, file_data=None):
36 """Extract the peak intensity information from the Sparky peak intensity file.
37
38 @keyword peak_list: The peak list object to place all data into.
39 @type peak_list: lib.spectrum.objects.Peak_list instance
40 @keyword file_data: The data extracted from the file converted into a list of lists.
41 @type file_data: list of lists of str
42 @raises RelaxError: When the expected peak intensity is not a float.
43 """
44
45
46 num = 0
47 if file_data[0][0] == 'Assignment':
48 num = num + 1
49 if file_data[1] == '':
50 num = num + 1
51 print("Number of header lines found: %s" % num)
52
53
54 w1_col = None
55 w2_col = None
56 w3_col = None
57 w4_col = None
58 int_col = None
59 for i in range(len(file_data[0])):
60
61 if file_data[0][i] == 'w1':
62 w1_col = i
63 elif file_data[0][i] == 'w2':
64 w2_col = i
65 elif file_data[0][i] == 'w3':
66 w3_col = i
67 elif file_data[0][i] == 'w4':
68 w4_col = i
69
70
71 elif file_data[0][i] == 'Data' and file_data[0][i+1] == 'Height':
72 int_col = i
73
74
75 elif file_data[0][i] == 'Intensity':
76 int_col = i
77
78
79 file_data = file_data[num:]
80
81
82 file_data = strip(file_data)
83
84
85 if w4_col != None:
86 dim = 4
87 elif w3_col != None:
88 dim = 3
89 elif w2_col != None:
90 dim = 2
91 elif w1_col != None:
92 dim = 1
93 else:
94 raise RelaxError("The dimensionality of the peak list cannot be determined.")
95 print("%sD peak list detected." % dim)
96
97
98 for line in file_data:
99
100 if line[0] == '?-?':
101 continue
102
103
104 if dim == 1:
105 assign1 = line[0]
106 elif dim == 2:
107 assign1, assign2 = split('-', line[0])
108 elif dim == 3:
109 assign1, assign2, assign3 = split('-', line[0])
110 elif dim == 4:
111 assign1, assign2, assign3, assign4 = split('-', line[0])
112
113
114 if dim >= 1:
115 row1 = split('([a-zA-Z]+)', assign1)
116 name1 = row1[-2] + row1[-1]
117 if dim >= 2:
118 row2 = split('([a-zA-Z]+)', assign2)
119 name2 = row2[-2] + row2[-1]
120 if dim >= 3:
121 row3 = split('([a-zA-Z]+)', assign3)
122 name3 = row3[-2] + row3[-1]
123 if dim >= 4:
124 row4 = split('([a-zA-Z]+)', assign4)
125 name4 = row4[-2] + row4[-1]
126
127
128 try:
129 res_num = int(row1[-3])
130 except:
131 raise RelaxError("Improperly formatted Sparky file, cannot process the assignment '%s'." % line[0])
132
133
134 w1 = None
135 w2 = None
136 w3 = None
137 w4 = None
138 if w1_col != None:
139 try:
140 w1 = float(line[w1_col])
141 except ValueError:
142 raise RelaxError("The chemical shift from the line %s is invalid." % line)
143 if w2_col != None:
144 try:
145 w2 = float(line[w2_col])
146 except ValueError:
147 raise RelaxError("The chemical shift from the line %s is invalid." % line)
148 if w3_col != None:
149 try:
150 w3 = float(line[w3_col])
151 except ValueError:
152 raise RelaxError("The chemical shift from the line %s is invalid." % line)
153 if w4_col != None:
154 try:
155 w4 = float(line[w4_col])
156 except ValueError:
157 raise RelaxError("The chemical shift from the line %s is invalid." % line)
158
159
160 if int_col != None:
161 try:
162 intensity = float(line[int_col])
163 except ValueError:
164 raise RelaxError("The peak intensity value from the line %s is invalid." % line)
165
166
167 if dim == 1:
168 peak_list.add(res_nums=[res_num], spin_names=[name1], shifts=[w1], intensity=intensity)
169 elif dim == 2:
170 peak_list.add(res_nums=[res_num, res_num], spin_names=[name1, name2], shifts=[w1, w2], intensity=intensity)
171 elif dim == 3:
172 peak_list.add(res_nums=[res_num, res_num, res_num], spin_names=[name1, name2, name3], shifts=[w1, w2, w3], intensity=intensity)
173 elif dim == 4:
174 peak_list.add(res_nums=[res_num, res_num, res_num, res_num], spin_names=[name1, name2, name3, name4], shifts=[w1, w2, w3, w4], intensity=intensity)
175
176
177 -def write_list(file_prefix=None, dir=None, res_names=None, res_nums=None, atom1_names=None, atom2_names=None, w1=None, w2=None, data_height=None, force=True):
178 """Create a Sparky .list file.
179
180 @keyword file_prefix: The base part of the file name without the .list extension.
181 @type file_prefix: str
182 @keyword dir: The directory to place the file in.
183 @type dir: str or None
184 @keyword res_names: The residue name list for each peak entry.
185 @type res_names: list of str
186 @keyword res_nums: The residue number list for each peak entry.
187 @type res_nums: list of int
188 @keyword atom1_names: The atom name list for w1 for each peak entry.
189 @type atom1_names: list of str
190 @keyword atom2_names: The atom name list for w2 for each peak entry.
191 @type atom2_names: list of str
192 @keyword w1: The w1 chemical shift list in ppm for each peak entry.
193 @type w1: list of float
194 @keyword w2: The w2 chemical shift list in ppm for each peak entry.
195 @type w2: list of float
196 @keyword data_height: The optional data height list for each peak entry.
197 @type data_height: None or list of float
198 @keyword force: A flag which if True will cause any pre-existing files to be overwritten.
199 @type force: bool
200 """
201
202
203 N = len(w1)
204 if len(res_names) != N:
205 raise RelaxError("The %s residue names does not match the %s number of entries." % (len(res_names), N))
206 if len(res_nums) != N:
207 raise RelaxError("The %s residue numbers does not match the %s number of entries." % (len(res_nums), N))
208 if len(atom1_names) != N:
209 raise RelaxError("The %s w1 atom names does not match the %s number of entries." % (len(atom1_names), N))
210 if len(atom2_names) != N:
211 raise RelaxError("The %s w2 atom names does not match the %s number of entries." % (len(atom2_names), N))
212 if len(w1) != N:
213 raise RelaxError("The %s w1 chemical shifts does not match the %s number of entries." % (len(w1), N))
214 if len(w2) != N:
215 raise RelaxError("The %s w2 chemical shifts does not match the %s number of entries." % (len(w2), N))
216 if data_height and len(data_height) != N:
217 raise RelaxError("The %s data heights does not match the %s number of entries." % (len(data_height), N))
218
219
220 print("Creating the Sparky list file.")
221
222
223 if isinstance(file_prefix, str):
224 file = open_write_file(file_name=file_prefix+".list", dir=dir, force=force)
225 else:
226 file = file_prefix
227
228
229 file.write("%17s %10s %10s" % ("Assignment ", "w1 ", "w2 "))
230 if data_height != None:
231 file.write(" %12s" % "Data Height")
232 file.write("\n\n")
233
234
235 for i in range(N):
236
237 assign = "%s%i%s-%s" % (res_names[i], res_nums[i], atom1_names[i], atom2_names[i])
238
239
240 file.write("%17s %10.3f %10.3f" % (assign, w1[i], w2[i]))
241 if data_height != None:
242 file.write(" %12i" % data_height[i])
243 file.write("\n")
244