1 | # |
---|
2 | # Copyright v1.0, 1.2, 1.3: 2019, John Badger. |
---|
3 | # |
---|
4 | # This program is free software: you can redistribute it and/or modify |
---|
5 | # it under the terms of the GNU General Public License as published by |
---|
6 | # the Free Software Foundation, either version 3 of the License, or |
---|
7 | # (at your option) any later version. |
---|
8 | # |
---|
9 | # This program is distributed in the hope that it will be useful, |
---|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | # GNU General Public License for more details. |
---|
13 | # |
---|
14 | # You should have received a copy of the GNU General Public License |
---|
15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. |
---|
16 | # |
---|
17 | |
---|
18 | # Version 1.2 is intended to be runnable under Python2 and Python3 |
---|
19 | # Version 1.3 includes an optional 'glue' term for extended structures |
---|
20 | # |
---|
21 | # this version modified by R B. Von Dreele for inclusion in GSAS-II |
---|
22 | |
---|
23 | from __future__ import division, print_function |
---|
24 | import math |
---|
25 | import sys |
---|
26 | import os |
---|
27 | import copy |
---|
28 | import random |
---|
29 | import time |
---|
30 | import cProfile,pstats |
---|
31 | import io as StringIO |
---|
32 | import numpy as np |
---|
33 | |
---|
34 | def G2shapes(Profile,ProfDict,Limits,data): |
---|
35 | |
---|
36 | ########## FUNCTIONS ######################## |
---|
37 | |
---|
38 | # NEW 1.1 Calculate intensity from P(r) dropping all scaling factors |
---|
39 | |
---|
40 | def ft_to_intensity(aList_q,aList_i_calc,aList_r,aList_pr_model,nbeads): |
---|
41 | |
---|
42 | num_q = len(aList_q) |
---|
43 | num_r = len(aList_r) |
---|
44 | |
---|
45 | count_q = 0 |
---|
46 | while count_q < num_q: |
---|
47 | |
---|
48 | q = float(aList_q[count_q]) |
---|
49 | |
---|
50 | # Sets P(r=0) =0.0. Later scaling includes a baseline term. |
---|
51 | integral = 0.0 |
---|
52 | |
---|
53 | count_r = 1 |
---|
54 | while count_r < num_r: |
---|
55 | |
---|
56 | r = float(aList_r[count_r]) |
---|
57 | pr = float(aList_pr_model[count_r]) |
---|
58 | qr = q*r |
---|
59 | integral = integral + pr*math.sin(qr)/qr |
---|
60 | |
---|
61 | count_r = count_r + 1 |
---|
62 | |
---|
63 | aList_i_calc.append(integral) |
---|
64 | |
---|
65 | count_q = count_q + 1 |
---|
66 | |
---|
67 | return; |
---|
68 | |
---|
69 | # NEW 1.1 Scale and Compare I and Ic. Includes a baseline correction |
---|
70 | |
---|
71 | def score_Ic(aList_q,aList_i,aList_i_sd,aList_i_calc): |
---|
72 | |
---|
73 | num_q = len(aList_q) |
---|
74 | |
---|
75 | idif = 0.0 |
---|
76 | isum = 0.0 |
---|
77 | sd_sq = 0.0 |
---|
78 | chi_sq = 0.0 |
---|
79 | |
---|
80 | # Least squares scale for calculated I onto observed I |
---|
81 | |
---|
82 | S = 0.0 |
---|
83 | Sx = 0.0 |
---|
84 | Sy = 0.0 |
---|
85 | Sxx = 0.0 |
---|
86 | Sxy = 0.0 |
---|
87 | |
---|
88 | count = 0 |
---|
89 | while count < num_q: |
---|
90 | x = float(aList_i_calc[count]) |
---|
91 | y = float(aList_i[count]) |
---|
92 | |
---|
93 | S = S + 1.0 |
---|
94 | Sx = Sx + x |
---|
95 | Sy = Sy + y |
---|
96 | Sxx = Sxx + x*x |
---|
97 | Sxy = Sxy + x*y |
---|
98 | |
---|
99 | count = count + 1 |
---|
100 | |
---|
101 | delta = S*Sxx - Sx*Sx |
---|
102 | a = (Sxx*Sy - Sx*Sxy)/delta |
---|
103 | b = (S*Sxy - Sx*Sy)/delta |
---|
104 | |
---|
105 | # Apply scale and find statistics |
---|
106 | |
---|
107 | i = 0 |
---|
108 | while i < num_q: |
---|
109 | iobs = float(aList_i[i]) |
---|
110 | sd = float(aList_i_sd[i]) |
---|
111 | |
---|
112 | aList_i_calc[i] = b*float(aList_i_calc[i]) + a |
---|
113 | icalc = aList_i_calc[i] |
---|
114 | |
---|
115 | idif = idif + abs(iobs - icalc) |
---|
116 | isum = isum + iobs + icalc |
---|
117 | |
---|
118 | dif = iobs - icalc |
---|
119 | dif_sq = dif*dif |
---|
120 | sd_sq = sd*sd |
---|
121 | |
---|
122 | chi_sq = chi_sq + dif_sq/sd_sq |
---|
123 | |
---|
124 | i = i + 1 |
---|
125 | |
---|
126 | rvalue = 2.0*idif/isum |
---|
127 | chi_sq = chi_sq/num_q |
---|
128 | |
---|
129 | return (chi_sq,rvalue); |
---|
130 | |
---|
131 | # NEW 1.1 Write original and calculated data. |
---|
132 | |
---|
133 | def write_all_data(file_intensity,aList_q,aList_i,aList_i_calc,aString): |
---|
134 | |
---|
135 | num_q = len(aList_q) |
---|
136 | |
---|
137 | file = open(file_intensity,'w',) |
---|
138 | aString = '# ' + aString + '\n' |
---|
139 | file.write(aString) |
---|
140 | |
---|
141 | i = 0 |
---|
142 | while i < num_q: |
---|
143 | |
---|
144 | q = aList_q[i] |
---|
145 | intensity = aList_i[i] |
---|
146 | intensity_calc = aList_i_calc[i] |
---|
147 | |
---|
148 | aString = str(q) + ' ' + str(intensity) + ' ' + str(intensity_calc) + '\n' |
---|
149 | |
---|
150 | file.write(aString) |
---|
151 | |
---|
152 | i = i + 1 |
---|
153 | |
---|
154 | file.close() |
---|
155 | |
---|
156 | return; |
---|
157 | |
---|
158 | # NEW 1.1 Read intensity data from GNOM output file |
---|
159 | |
---|
160 | def read_i(aList_q,aList_i,aList_i_sd,inFile,angstrom_scale): |
---|
161 | |
---|
162 | scale_units = 1.0/angstrom_scale |
---|
163 | |
---|
164 | Q,Io,wt,Ic,Ib,Ifb = Profile[:6] |
---|
165 | Qmin = Limits[1][0] |
---|
166 | Qmax = Limits[1][1] |
---|
167 | wtFactor = ProfDict['wtFactor'] |
---|
168 | Back,ifBack = data['Back'] |
---|
169 | Ibeg = np.searchsorted(Q,Qmin) |
---|
170 | Ifin = np.searchsorted(Q,Qmax)+1 #include last point |
---|
171 | aList_q += list(Q[Ibeg:Ifin]*scale_units) |
---|
172 | aList_i += list(Io[Ibeg:Ifin]) |
---|
173 | aList_i_sd += list(1./np.sqrt(wtFactor*wt[Ibeg:Ifin])) |
---|
174 | |
---|
175 | # file = open(inFile,'r') |
---|
176 | # allLines = file.readlines() |
---|
177 | # file.close() |
---|
178 | # |
---|
179 | # parse_data = 'no' |
---|
180 | # for eachLine in allLines: |
---|
181 | # |
---|
182 | # if parse_data == 'yes': |
---|
183 | # |
---|
184 | # aList = eachLine.split() |
---|
185 | # num_params = len(aList) |
---|
186 | # |
---|
187 | # if num_params == 5: |
---|
188 | # |
---|
189 | # q = float(aList[0]) * scale_units |
---|
190 | # if q > 0.0: |
---|
191 | # i = float(aList[1]) |
---|
192 | # i_sd = float(aList[2]) |
---|
193 | # aList_q.append(q) |
---|
194 | # aList_i.append(i) |
---|
195 | # aList_i_sd.append(i_sd) |
---|
196 | # |
---|
197 | # else: |
---|
198 | # if num_params == 0 and len(aList_q) > 0: |
---|
199 | # parse_data = 'no' |
---|
200 | # |
---|
201 | # if eachLine.find('S J EXP ERROR J REG I REG') > -1: |
---|
202 | # parse_data = 'yes' |
---|
203 | # |
---|
204 | return; |
---|
205 | |
---|
206 | # Read PDB for starting point |
---|
207 | |
---|
208 | def read_pdb(aList_beads_x,aList_beads_y,aList_beads_z,pdbfile_in): |
---|
209 | |
---|
210 | xmean = 0.0 |
---|
211 | ymean = 0.0 |
---|
212 | zmean = 0.0 |
---|
213 | nbeads = 0 |
---|
214 | |
---|
215 | file = open(pdbfile_in,'r') |
---|
216 | allLines = file.readlines() |
---|
217 | file.close() |
---|
218 | |
---|
219 | for eachLine in allLines: |
---|
220 | |
---|
221 | tag = eachLine[0:6] |
---|
222 | tag = tag.strip() |
---|
223 | |
---|
224 | if tag == 'ATOM': |
---|
225 | |
---|
226 | atom_name = eachLine[13:16] |
---|
227 | atom_name = atom_name.strip() |
---|
228 | |
---|
229 | if atom_name == 'CA': |
---|
230 | |
---|
231 | x = float(eachLine[30:38]) |
---|
232 | y = float(eachLine[38:46]) |
---|
233 | z = float(eachLine[46:54]) |
---|
234 | |
---|
235 | xmean = xmean + x |
---|
236 | ymean = ymean + y |
---|
237 | zmean = zmean + z |
---|
238 | |
---|
239 | nbeads = nbeads + 1 |
---|
240 | |
---|
241 | xmean = xmean/float(nbeads) |
---|
242 | ymean = ymean/float(nbeads) |
---|
243 | zmean = zmean/float(nbeads) |
---|
244 | |
---|
245 | for eachLine in allLines: |
---|
246 | |
---|
247 | tag = eachLine[0:6] |
---|
248 | tag = tag.strip() |
---|
249 | |
---|
250 | if tag == 'ATOM': |
---|
251 | |
---|
252 | atom_name = eachLine[13:16] |
---|
253 | atom_name = atom_name.strip() |
---|
254 | |
---|
255 | if atom_name == 'CA': |
---|
256 | |
---|
257 | x = float(eachLine[30:38]) - xmean |
---|
258 | y = float(eachLine[38:46]) - ymean |
---|
259 | z = float(eachLine[46:54]) - zmean |
---|
260 | aList_beads_x.append(x) |
---|
261 | aList_beads_y.append(y) |
---|
262 | aList_beads_z.append(z) |
---|
263 | |
---|
264 | return; |
---|
265 | |
---|
266 | # # Write P(r) with SD and calculated value. |
---|
267 | # |
---|
268 | # def pr_writer(aList_pr,aList_r,aList_pr_model,outfile_pr): |
---|
269 | # |
---|
270 | # num_pr = len(aList_pr) |
---|
271 | # |
---|
272 | # file = open(outfile_pr,'w') |
---|
273 | # file.write('#\n') |
---|
274 | # |
---|
275 | # i = 0 |
---|
276 | # while i < num_pr: |
---|
277 | # |
---|
278 | # r = float(aList_r[i]) |
---|
279 | # pr = float(aList_pr[i]) |
---|
280 | # pr_calc = float(aList_pr_model[i]) |
---|
281 | # aString = str(r) + ' ' + str(pr) + ' ' + str(pr_calc) + '\n' |
---|
282 | # file.write(aString) |
---|
283 | # |
---|
284 | # i = i + 1 |
---|
285 | # |
---|
286 | # file.close() |
---|
287 | # |
---|
288 | # return; |
---|
289 | |
---|
290 | # Write a set of points as a pseudo-PDB file |
---|
291 | |
---|
292 | def pdb_writer(aList_x_write,aList_y_write,aList_z_write,out_file,aString): |
---|
293 | |
---|
294 | atom_number = 0 |
---|
295 | res_number = 0 |
---|
296 | dummy_b = 0 |
---|
297 | num_atoms = len(aList_x_write) |
---|
298 | |
---|
299 | file = open(out_file,'w') |
---|
300 | file.write(aString) |
---|
301 | file.write('\n') |
---|
302 | |
---|
303 | i = 0 |
---|
304 | while i < num_atoms: |
---|
305 | |
---|
306 | x = float(aList_x_write[i]) |
---|
307 | y = float(aList_y_write[i]) |
---|
308 | z = float(aList_z_write[i]) |
---|
309 | x = '%.3f'%(x) |
---|
310 | y = '%.3f'%(y) |
---|
311 | z = '%.3f'%(z) |
---|
312 | x = x.rjust(8) |
---|
313 | y = y.rjust(8) |
---|
314 | z = z.rjust(8) |
---|
315 | atom_number = atom_number + 1 |
---|
316 | res_number = res_number + 1 |
---|
317 | atom_number_str = str(atom_number) |
---|
318 | atom_number_str = atom_number_str.rjust(5) |
---|
319 | res_number_str = str(res_number) |
---|
320 | res_number_str = res_number_str.rjust(4) |
---|
321 | bfactor = str(dummy_b) + '.00' |
---|
322 | bfactor = bfactor.rjust(6) |
---|
323 | |
---|
324 | if res_number < 10000: |
---|
325 | aLine_out = 'ATOM ' + atom_number_str + ' CA ALA A' + res_number_str + ' ' + x + y + z + ' 1.00' + bfactor + '\n' |
---|
326 | else: |
---|
327 | res_number_str = str(res_number - 9999) |
---|
328 | aLine_out = 'ATOM ' + atom_number_str + ' CA ALA B' + res_number_str + ' ' + x + y + z + ' 1.00' + bfactor + '\n' |
---|
329 | file.write(aLine_out) |
---|
330 | |
---|
331 | i = i + 1 |
---|
332 | |
---|
333 | file.close() |
---|
334 | |
---|
335 | return; |
---|
336 | |
---|
337 | # Evaluate local bead densities and point density on a notional grid |
---|
338 | |
---|
339 | def set_box(aList_beads_x,aList_beads_y,aList_beads_z,\ |
---|
340 | aList_box_x_all,aList_box_y_all,aList_box_z_all,\ |
---|
341 | aList_box_score,box_step,dmax,rsearch): |
---|
342 | |
---|
343 | dmax_over2 = dmax/2.0 |
---|
344 | search_sq = rsearch**2 |
---|
345 | num_beads = len(aList_beads_x) |
---|
346 | |
---|
347 | count_x = -dmax_over2 |
---|
348 | while count_x < dmax_over2: |
---|
349 | count_y = -dmax_over2 |
---|
350 | while count_y < dmax_over2: |
---|
351 | count_z = -dmax_over2 |
---|
352 | while count_z < dmax_over2: |
---|
353 | |
---|
354 | count_local = 0 |
---|
355 | i = 0 |
---|
356 | while i < num_beads: |
---|
357 | |
---|
358 | dx = float(aList_beads_x[i]) - count_x |
---|
359 | dy = float(aList_beads_y[i]) - count_y |
---|
360 | dz = float(aList_beads_z[i]) - count_z |
---|
361 | |
---|
362 | dsq = dx*dx + dy*dy + dz*dz |
---|
363 | |
---|
364 | if dsq < search_sq: |
---|
365 | count_local = count_local + 1 |
---|
366 | |
---|
367 | i = i + 1 |
---|
368 | |
---|
369 | if count_local > 1: |
---|
370 | aList_box_x_all.append(count_x) |
---|
371 | aList_box_y_all.append(count_y) |
---|
372 | aList_box_z_all.append(count_z) |
---|
373 | aList_box_score.append(count_local) |
---|
374 | |
---|
375 | count_z = count_z + box_step |
---|
376 | count_y = count_y + box_step |
---|
377 | count_x = count_x + box_step |
---|
378 | |
---|
379 | return; |
---|
380 | |
---|
381 | # Evaluate local bead densities and point density on a notional grid - fast version |
---|
382 | |
---|
383 | def set_box_fast(aList_beads_x,aList_beads_y,aList_beads_z,\ |
---|
384 | aList_box_x_all,aList_box_y_all,aList_box_z_all,\ |
---|
385 | aList_box_score,box_step,dmax,rsearch): |
---|
386 | |
---|
387 | dmax_over2 = dmax/2.0 |
---|
388 | num_box = int(dmax/box_step)+1 |
---|
389 | |
---|
390 | box_grid = np.zeros((num_box,num_box,num_box),dtype=int) |
---|
391 | aList_beads_xyz_loc = np.array([(np.array(aList_beads_x)+dmax_over2)/box_step, |
---|
392 | (np.array(aList_beads_y)+dmax_over2)/box_step, |
---|
393 | (np.array(aList_beads_z)+dmax_over2)/box_step],dtype=int) |
---|
394 | |
---|
395 | for ix,iy,iz in aList_beads_xyz_loc.T: |
---|
396 | box_grid[ix,iy,iz] += 1 |
---|
397 | |
---|
398 | non_zero = np.argwhere(box_grid) |
---|
399 | for ix,iy,iz in non_zero: |
---|
400 | aList_box_x_all.append((ix*box_step)-dmax_over2) |
---|
401 | aList_box_y_all.append((iy*box_step)-dmax_over2) |
---|
402 | aList_box_z_all.append((iz*box_step)-dmax_over2) |
---|
403 | aList_box_score.append(box_grid[ix,iy,iz]) |
---|
404 | |
---|
405 | return; |
---|
406 | |
---|
407 | # Establish a volume |
---|
408 | |
---|
409 | def set_vol(aList_box_x_all,aList_box_y_all,aList_box_z_all,aList_box_score,\ |
---|
410 | aList_box_x,aList_box_y,aList_box_z,vol_target,box_pt_vol): |
---|
411 | |
---|
412 | num_pts = len(aList_box_score) |
---|
413 | num_tries = int(max(aList_box_score)) |
---|
414 | density_thresh = max(aList_box_score) - 1.0 |
---|
415 | vol = vol_target + 1.0 |
---|
416 | |
---|
417 | i = 0 |
---|
418 | while i < num_tries: |
---|
419 | |
---|
420 | density_thresh = density_thresh - 1.0 |
---|
421 | num_box_pts = 0.0 |
---|
422 | |
---|
423 | j = 0 |
---|
424 | while j < num_pts: |
---|
425 | density = float(aList_box_score[j]) |
---|
426 | if density >= density_thresh: |
---|
427 | num_box_pts = num_box_pts + 1.0 |
---|
428 | j = j + 1 |
---|
429 | |
---|
430 | vol = num_box_pts*box_pt_vol |
---|
431 | if vol < vol_target: |
---|
432 | density_use = density_thresh |
---|
433 | |
---|
434 | i = i + 1 |
---|
435 | |
---|
436 | # |
---|
437 | |
---|
438 | num_box_pts1 = 0.0 |
---|
439 | num_box_pts2 = 0.0 |
---|
440 | density_thresh1 = density_use |
---|
441 | density_thresh2 = density_use - 1.0 |
---|
442 | i = 0 |
---|
443 | while i < num_pts: |
---|
444 | density_value = float(aList_box_score[i]) |
---|
445 | if density_value >= density_thresh1: |
---|
446 | num_box_pts1 = num_box_pts1 + 1.0 |
---|
447 | if density_value >= density_thresh2: |
---|
448 | num_box_pts2 = num_box_pts2 + 1.0 |
---|
449 | i = i + 1 |
---|
450 | |
---|
451 | vol1 = num_box_pts1*box_pt_vol |
---|
452 | vol2 = num_box_pts2*box_pt_vol |
---|
453 | delta1 = abs(vol1-vol_target) |
---|
454 | delta2 = abs(vol2-vol_target) |
---|
455 | |
---|
456 | if delta1 < delta2: |
---|
457 | density_thresh = density_thresh1 |
---|
458 | else: |
---|
459 | density_thresh = density_thresh2 |
---|
460 | |
---|
461 | i = 0 |
---|
462 | while i < num_pts: |
---|
463 | |
---|
464 | density_value = float(aList_box_score[i]) |
---|
465 | if density_value >= density_thresh: |
---|
466 | aList_box_x.append(aList_box_x_all[i]) |
---|
467 | aList_box_y.append(aList_box_y_all[i]) |
---|
468 | aList_box_z.append(aList_box_z_all[i]) |
---|
469 | |
---|
470 | i = i + 1 |
---|
471 | |
---|
472 | return; |
---|
473 | |
---|
474 | # Find beads that are not in allowed volume |
---|
475 | |
---|
476 | def disallowed_beads(aList_beads_x,aList_beads_y,aList_beads_z,aList_contacts,\ |
---|
477 | aList_box_x,aList_box_y,aList_box_z,rsearch): |
---|
478 | |
---|
479 | num_beads = len(aList_beads_x) |
---|
480 | num_boxes = len(aList_box_x) |
---|
481 | contact_limit_sq = rsearch**2 |
---|
482 | |
---|
483 | count = 0 |
---|
484 | while count < num_beads: |
---|
485 | |
---|
486 | x_bead = float(aList_beads_x[count]) |
---|
487 | y_bead = float(aList_beads_y[count]) |
---|
488 | z_bead = float(aList_beads_z[count]) |
---|
489 | |
---|
490 | inbox = 'no' |
---|
491 | i = 0 |
---|
492 | while i < num_boxes: |
---|
493 | |
---|
494 | x_box = float(aList_box_x[i]) |
---|
495 | y_box = float(aList_box_y[i]) |
---|
496 | z_box = float(aList_box_z[i]) |
---|
497 | dsq = (x_bead - x_box)**2 + (y_bead - y_box)**2 + (z_bead - z_box)**2 |
---|
498 | |
---|
499 | if dsq < contact_limit_sq: |
---|
500 | inbox = 'yes' |
---|
501 | i = num_boxes |
---|
502 | |
---|
503 | i = i + 1 |
---|
504 | |
---|
505 | if inbox == 'no': |
---|
506 | aList_contacts.append(count) |
---|
507 | |
---|
508 | count = count + 1 |
---|
509 | |
---|
510 | return; |
---|
511 | |
---|
512 | # Compute a P(r) |
---|
513 | |
---|
514 | def calc_pr(aList_beads_x,aList_beads_y,aList_beads_z,aList_pr_model,hist_grid): |
---|
515 | |
---|
516 | num_hist = len(aList_pr_model) |
---|
517 | count = 0 |
---|
518 | while count < num_hist: |
---|
519 | aList_pr_model[count] = 0.0 |
---|
520 | count = count + 1 |
---|
521 | |
---|
522 | nbeads = len(aList_beads_x) |
---|
523 | max_dr = (float(num_hist)-1.0)*hist_grid |
---|
524 | |
---|
525 | i = 0 |
---|
526 | while i < nbeads: |
---|
527 | |
---|
528 | j = 0 |
---|
529 | while j < i: |
---|
530 | |
---|
531 | dr = get_dr(aList_beads_x[i],aList_beads_y[i],aList_beads_z[i],\ |
---|
532 | aList_beads_x[j],aList_beads_y[j],aList_beads_z[j]) |
---|
533 | dr = min(dr,max_dr) |
---|
534 | |
---|
535 | # Find pointers and do un-interpolation |
---|
536 | |
---|
537 | dr_grid = dr/hist_grid |
---|
538 | int_dr_grid = int(dr_grid) |
---|
539 | |
---|
540 | int_dr_grid = min(int_dr_grid,num_hist-2) |
---|
541 | ip_low = int_dr_grid |
---|
542 | ip_high = ip_low + 1 |
---|
543 | |
---|
544 | ip_high_frac = dr_grid - float(int_dr_grid) |
---|
545 | ip_low_frac = 1.0 - ip_high_frac |
---|
546 | |
---|
547 | aList_pr_model[ip_low] = float(aList_pr_model[ip_low]) + ip_low_frac |
---|
548 | aList_pr_model[ip_high] = float(aList_pr_model[ip_high]) + ip_high_frac |
---|
549 | |
---|
550 | j = j + 1 |
---|
551 | i = i + 1 |
---|
552 | |
---|
553 | return; |
---|
554 | |
---|
555 | # Score for rms difference between observed and model histograms |
---|
556 | |
---|
557 | def pr_dif(aList_pr,aList_pr_model,skip): |
---|
558 | |
---|
559 | num_hist = len(aList_pr) |
---|
560 | delta_hist_sum = 0.0 |
---|
561 | delta_hist_sum_sq = 0.0 |
---|
562 | hist_sum = 0.0 |
---|
563 | |
---|
564 | i = skip |
---|
565 | while i < num_hist: |
---|
566 | |
---|
567 | model = float(aList_pr_model[i]) |
---|
568 | data = float(aList_pr[i]) |
---|
569 | delta_hist = abs(data - model) |
---|
570 | delta_hist_sum = delta_hist_sum + delta_hist |
---|
571 | hist_sum = hist_sum + data |
---|
572 | |
---|
573 | delta_hist_sum_sq = delta_hist_sum_sq + delta_hist*delta_hist |
---|
574 | |
---|
575 | i = i + 1 |
---|
576 | |
---|
577 | mean_hist_sum = hist_sum/(num_hist - skip) |
---|
578 | delta_hist_sum_sq = delta_hist_sum_sq/(num_hist - skip) |
---|
579 | delta_hist_sum_sq = math.sqrt(delta_hist_sum_sq)/mean_hist_sum |
---|
580 | |
---|
581 | return delta_hist_sum_sq; |
---|
582 | |
---|
583 | # Statistics for fractional difference between observed and model histograms |
---|
584 | |
---|
585 | def pr_rfactor(aList_pr,aList_pr_sd,aList_pr_model,skip): |
---|
586 | |
---|
587 | num_hist = len(aList_pr) |
---|
588 | delta_hist_sum = 0.0 |
---|
589 | hist_sum = 0.0 |
---|
590 | |
---|
591 | i = skip |
---|
592 | while i < num_hist: |
---|
593 | |
---|
594 | model = float(aList_pr_model[i]) |
---|
595 | exp = float(aList_pr[i]) |
---|
596 | delta_hist = exp - model |
---|
597 | delta_hist_sum = delta_hist_sum + abs(delta_hist) |
---|
598 | hist_sum = hist_sum + exp |
---|
599 | |
---|
600 | i = i + 1 |
---|
601 | |
---|
602 | delta_hist_sum = delta_hist_sum/hist_sum |
---|
603 | |
---|
604 | return delta_hist_sum; |
---|
605 | |
---|
606 | # Compute the VDW energy for a interaction |
---|
607 | |
---|
608 | def vdw_energy(econ12,econ6,e_width,dr,bead_sep3): |
---|
609 | |
---|
610 | if dr > bead_sep3: |
---|
611 | vdw = 0.0 |
---|
612 | else: |
---|
613 | dr_e6 = dr**6 |
---|
614 | dr_e12 = dr_e6**2 |
---|
615 | vdw = econ12/dr_e12 - 2.0*econ6/dr_e6 |
---|
616 | vdw = max(vdw,e_width) |
---|
617 | |
---|
618 | return vdw; |
---|
619 | |
---|
620 | # Set a random distribution of beads in a box with maximum extent dmax |
---|
621 | |
---|
622 | def random_beads(aList_beads_x,aList_beads_y,aList_beads_z,\ |
---|
623 | nbeads,dmax,aList_symm,bias_z): |
---|
624 | |
---|
625 | half_side = 0.5*dmax |
---|
626 | |
---|
627 | scale_xy = 1.0 - bias_z |
---|
628 | scale_z = 1.0 + bias_z |
---|
629 | x_range = scale_xy * half_side |
---|
630 | y_range = scale_xy * half_side |
---|
631 | z_range = scale_z * half_side |
---|
632 | |
---|
633 | num_ops = len(aList_symm) |
---|
634 | |
---|
635 | i = 0 |
---|
636 | while i < nbeads: |
---|
637 | |
---|
638 | triangle = random.triangular(-0.7,0.7,0.0) |
---|
639 | x = triangle*x_range |
---|
640 | triangle = random.triangular(-0.7,0.7,0.0) |
---|
641 | y = triangle*y_range |
---|
642 | triangle = random.triangular(-0.7,0.7,0.0) |
---|
643 | z = triangle*z_range |
---|
644 | |
---|
645 | aList_beads_x.append(x) |
---|
646 | aList_beads_y.append(y) |
---|
647 | aList_beads_z.append(z) |
---|
648 | |
---|
649 | j = 0 |
---|
650 | while j < num_ops: |
---|
651 | aList_s = aList_symm[j] |
---|
652 | m11 = float(aList_s[0]) |
---|
653 | m12 = float(aList_s[1]) |
---|
654 | m21 = float(aList_s[2]) |
---|
655 | m22 = float(aList_s[3]) |
---|
656 | |
---|
657 | xs = m11*x + m12*y |
---|
658 | ys = m21*x + m22*y |
---|
659 | zs = z |
---|
660 | aList_beads_x.append(xs) |
---|
661 | aList_beads_y.append(ys) |
---|
662 | aList_beads_z.append(zs) |
---|
663 | |
---|
664 | j = j + 1 |
---|
665 | |
---|
666 | i = i + num_symm |
---|
667 | |
---|
668 | return; |
---|
669 | |
---|
670 | # Read experimentalal P(r) from GNOM output file |
---|
671 | |
---|
672 | def read_pr(aList_r,aList_pr,aList_pr_sd,aList_pr_model,\ |
---|
673 | aList_pr_model_test,aList_pr_model_test2,inFile): |
---|
674 | |
---|
675 | angstrom_scale = 1.0 |
---|
676 | Bins,Dbins,BinMag = data['Pair']['Distribution'] |
---|
677 | |
---|
678 | aList_r += list(Bins) |
---|
679 | aList_pr += list(BinMag) |
---|
680 | aList_pr_sd += list(np.ones_like(Bins)/100.) |
---|
681 | aList_pr_model += list(np.zeros_like(Bins)) |
---|
682 | aList_pr_model_test += list(np.zeros_like(Bins)) |
---|
683 | aList_pr_model_test2 += list(np.zeros_like(Bins)) |
---|
684 | |
---|
685 | # file = open(inFile,'r') |
---|
686 | # allLines = file.readlines() |
---|
687 | # file.close() |
---|
688 | # |
---|
689 | # parse_data = 'no' |
---|
690 | # for eachLine in allLines: |
---|
691 | # |
---|
692 | # if parse_data == 'yes': |
---|
693 | # |
---|
694 | # aList = eachLine.split() |
---|
695 | # num_params = len(aList) |
---|
696 | # |
---|
697 | # if num_params == 3: |
---|
698 | # r = float(aList[0]) |
---|
699 | # pr = float(aList[1]) |
---|
700 | # pr_sd = float(aList[2]) |
---|
701 | # |
---|
702 | # aList_pr.append(pr) |
---|
703 | # aList_pr_sd.append(pr_sd) |
---|
704 | # aList_r.append(r) |
---|
705 | # aList_pr_model.append(0.0) |
---|
706 | # aList_pr_model_test.append(0.0) |
---|
707 | # aList_pr_model_test2.append(0.0) |
---|
708 | # |
---|
709 | # if eachLine.find('R P(R) ERROR') > -1: |
---|
710 | # parse_data = 'yes' |
---|
711 | # |
---|
712 | num_hist = len(aList_r) |
---|
713 | hist_grid = float(aList_r[1]) - float(aList_r[0]) |
---|
714 | |
---|
715 | |
---|
716 | # Heuristic for checking units |
---|
717 | # test_r = max(aList_r) |
---|
718 | # if test_r < 30.0: |
---|
719 | # |
---|
720 | # aString = 'P(r)appears to be in nm. Converting to Angstrom units' |
---|
721 | # print (aString) |
---|
722 | # angstrom_scale = 10.0 |
---|
723 | # |
---|
724 | # i = 0 |
---|
725 | # while i < num_hist: |
---|
726 | # aList_r[i] = angstrom_scale * aList_r[i] |
---|
727 | # i = i + 1 |
---|
728 | # |
---|
729 | # hist_grid = angstrom_scale * hist_grid |
---|
730 | # |
---|
731 | # i = 0 |
---|
732 | # while i < num_hist: |
---|
733 | # r = float(aList_r[i]) |
---|
734 | # r_calc = float(i)*hist_grid |
---|
735 | # |
---|
736 | # if abs(r - r_calc) > 0.15: |
---|
737 | # aString = 'Input P(r) grid is irregular! Exiting' |
---|
738 | # print (aString) |
---|
739 | # time.sleep(4) |
---|
740 | # sys.exit(1) |
---|
741 | # |
---|
742 | # i = i + 1 |
---|
743 | # |
---|
744 | dmax = aList_r[num_hist-1] |
---|
745 | |
---|
746 | # Pad histogram by 5 Angstrom |
---|
747 | |
---|
748 | ipad = int(5.0/hist_grid) |
---|
749 | |
---|
750 | i = 0 |
---|
751 | while i < ipad: |
---|
752 | r = dmax + float(i)*hist_grid |
---|
753 | aList_pr.append(0.0) |
---|
754 | aList_pr_sd.append(0.0) |
---|
755 | aList_r.append(r) |
---|
756 | aList_pr_model.append(0.0) |
---|
757 | aList_pr_model_test.append(0.0) |
---|
758 | aList_pr_model_test2.append(0.0) |
---|
759 | i = i + 1 |
---|
760 | |
---|
761 | return (dmax,hist_grid,num_hist,angstrom_scale); |
---|
762 | |
---|
763 | # Scale P(r) onto model P(r) assuming same grid |
---|
764 | |
---|
765 | def scale_pr(aList_pr,aList_pr_sd,aList_pr_model): |
---|
766 | |
---|
767 | num_hist = len(aList_pr) |
---|
768 | total_dr = 0.0 |
---|
769 | total_pr = 0.0 |
---|
770 | |
---|
771 | i = 0 |
---|
772 | while i < num_hist: |
---|
773 | total_dr = total_dr + float(aList_pr_model[i]) |
---|
774 | total_pr = total_pr + float(aList_pr[i]) |
---|
775 | i = i + 1 |
---|
776 | |
---|
777 | scale = total_dr/total_pr |
---|
778 | |
---|
779 | i = 0 |
---|
780 | while i < num_hist: |
---|
781 | aList_pr[i] = scale*float(aList_pr[i]) |
---|
782 | aList_pr_sd[i] = scale * float(aList_pr_sd[i]) |
---|
783 | i = i + 1 |
---|
784 | |
---|
785 | return; |
---|
786 | |
---|
787 | # Return a non-zero distance between two coordinates |
---|
788 | |
---|
789 | def get_dr(x1,y1,z1,x2,y2,z2): |
---|
790 | |
---|
791 | x1 = float(x1) |
---|
792 | y1 = float(y1) |
---|
793 | z1 = float(z1) |
---|
794 | x2 = float(x2) |
---|
795 | y2 = float(y2) |
---|
796 | z2 = float(z2) |
---|
797 | dr = (x1 - x2)**2 + (y1-y2)**2 + (z1-z2)**2 |
---|
798 | dr = max(dr,0.1) |
---|
799 | dr = math.sqrt(dr) |
---|
800 | |
---|
801 | return dr; |
---|
802 | |
---|
803 | # Find center of beads within a radii |
---|
804 | |
---|
805 | def center_beads(x,y,z,aList_beads_x,aList_beads_y,aList_beads_z,radii_1,radii_2): |
---|
806 | |
---|
807 | num_beads = len(aList_beads_x) |
---|
808 | |
---|
809 | xsum = 0.0 |
---|
810 | ysum = 0.0 |
---|
811 | zsum = 0.0 |
---|
812 | count_beads = 0.0 |
---|
813 | |
---|
814 | i = 0 |
---|
815 | while i < num_beads: |
---|
816 | |
---|
817 | dr = get_dr(x,y,z,aList_beads_x[i],aList_beads_y[i],aList_beads_z[i]) |
---|
818 | |
---|
819 | if dr > radii_1 and dr < radii_2: |
---|
820 | count_beads = count_beads + 1.0 |
---|
821 | xsum = xsum + float(aList_beads_x[i]) |
---|
822 | ysum = ysum + float(aList_beads_y[i]) |
---|
823 | zsum = zsum + float(aList_beads_z[i]) |
---|
824 | |
---|
825 | i = i + 1 |
---|
826 | |
---|
827 | if count_beads > 0.0: |
---|
828 | xsum = xsum/count_beads |
---|
829 | ysum = ysum/count_beads |
---|
830 | zsum = zsum/count_beads |
---|
831 | delta = (xsum - x)**2 + (ysum - y)**2 + (zsum - z)**2 |
---|
832 | delta = math.sqrt(delta) |
---|
833 | else: |
---|
834 | delta = 0.0 |
---|
835 | |
---|
836 | return delta; |
---|
837 | |
---|
838 | # Obtain mean of total VDW energy |
---|
839 | |
---|
840 | def get_total_energy(aList_beads_x,aList_beads_y,aList_beads_z,econ12,econ6,bead_sep3): |
---|
841 | |
---|
842 | nbeads = len(aList_beads_x) |
---|
843 | vdw_all = 0.0 |
---|
844 | |
---|
845 | i = 0 |
---|
846 | while i < nbeads: |
---|
847 | j = 0 |
---|
848 | while j < i: |
---|
849 | dr = get_dr(aList_beads_x[i],aList_beads_y[i],aList_beads_z[i],\ |
---|
850 | aList_beads_x[j],aList_beads_y[j],aList_beads_z[j]) |
---|
851 | vdw = vdw_energy(econ12,econ6,e_width,dr,bead_sep3) |
---|
852 | vdw_all = vdw_all + vdw |
---|
853 | j = j + 1 |
---|
854 | i = i + 1 |
---|
855 | |
---|
856 | vdw_all = vdw_all/float(nbeads) |
---|
857 | |
---|
858 | return vdw_all; |
---|
859 | |
---|
860 | # Energy minimize |
---|
861 | |
---|
862 | def e_min(aList_beads_x,aList_beads_y,aList_beads_z,bead_sep,bead_sep3,aList_symm): |
---|
863 | |
---|
864 | eps = bead_sep/(2.0**(1.0/6.0)) |
---|
865 | eps12 = eps**12 |
---|
866 | eps6 = eps**6 |
---|
867 | step_max = bead_sep |
---|
868 | scale = 0.0 |
---|
869 | icount = -1 |
---|
870 | |
---|
871 | nbeads = len(aList_beads_x) |
---|
872 | num_ops = len(aList_symm) |
---|
873 | num_cycles = 51 |
---|
874 | |
---|
875 | i = 0 |
---|
876 | while i < num_cycles: |
---|
877 | |
---|
878 | icount = icount + 1 |
---|
879 | |
---|
880 | aList_beads_x_new = [] |
---|
881 | aList_beads_y_new = [] |
---|
882 | aList_beads_z_new = [] |
---|
883 | |
---|
884 | sum_forces_scale = 0.0 |
---|
885 | |
---|
886 | k = 0 |
---|
887 | while k < nbeads - num_ops: |
---|
888 | |
---|
889 | xold = float(aList_beads_x[k]) |
---|
890 | yold = float(aList_beads_y[k]) |
---|
891 | zold = float(aList_beads_z[k]) |
---|
892 | |
---|
893 | fx = 0.0 |
---|
894 | fy = 0.0 |
---|
895 | fz = 0.0 |
---|
896 | j = 0 |
---|
897 | while j < nbeads: |
---|
898 | |
---|
899 | xj = aList_beads_x[j] |
---|
900 | yj = aList_beads_y[j] |
---|
901 | zj = aList_beads_z[j] |
---|
902 | |
---|
903 | dr = get_dr(xold,yold,zold,xj,yj,zj) |
---|
904 | |
---|
905 | # Truncate very steep |
---|
906 | dr = min(eps,dr) |
---|
907 | |
---|
908 | if dr < bead_sep3: |
---|
909 | dr_sq = dr*dr |
---|
910 | dr12 = dr_sq**6 |
---|
911 | dr6 = dr_sq**3 |
---|
912 | |
---|
913 | dx = xold - xj |
---|
914 | dy = yold - yj |
---|
915 | dz = zold - zj |
---|
916 | |
---|
917 | force = (1.0/dr_sq)*(eps12/dr12 - 0.5*eps6/dr6) |
---|
918 | fx = fx + force*dx |
---|
919 | fy = fy + force*dy |
---|
920 | fz = fz + force*dz |
---|
921 | |
---|
922 | sum_forces_scale = sum_forces_scale + abs(fx) + abs(fy) + abs(fz) |
---|
923 | |
---|
924 | j = j + 1 |
---|
925 | |
---|
926 | # |
---|
927 | xstep = scale*fx |
---|
928 | ystep = scale*fy |
---|
929 | zstep = scale*fz |
---|
930 | |
---|
931 | if xstep > 0.0: |
---|
932 | xstep = min(xstep,step_max) |
---|
933 | else: |
---|
934 | xstep = max(xstep,-step_max) |
---|
935 | |
---|
936 | if ystep > 0.0: |
---|
937 | ystep = min(ystep,step_max) |
---|
938 | else: |
---|
939 | ystep = max(ystep,-step_max) |
---|
940 | |
---|
941 | if zstep > 0.0: |
---|
942 | zstep = min(zstep,step_max) |
---|
943 | else: |
---|
944 | zstep = max(zstep,-step_max) |
---|
945 | |
---|
946 | xtest = xold + xstep |
---|
947 | ytest = yold + ystep |
---|
948 | ztest = zold + zstep |
---|
949 | aList_beads_x_new.append(xtest) |
---|
950 | aList_beads_y_new.append(ytest) |
---|
951 | aList_beads_z_new.append(ztest) |
---|
952 | |
---|
953 | # Apply shifs to symm positions |
---|
954 | l = 0 |
---|
955 | while l < num_ops: |
---|
956 | aList_s = aList_symm[l] |
---|
957 | m11 = float(aList_s[0]) |
---|
958 | m12 = float(aList_s[1]) |
---|
959 | m21 = float(aList_s[2]) |
---|
960 | m22 = float(aList_s[3]) |
---|
961 | |
---|
962 | xs = m11*xtest + m12*ytest |
---|
963 | ys = m21*xtest + m22*ytest |
---|
964 | zs = ztest |
---|
965 | |
---|
966 | aList_beads_x_new.append(xs) |
---|
967 | aList_beads_y_new.append(ys) |
---|
968 | aList_beads_z_new.append(zs) |
---|
969 | |
---|
970 | l = l + 1 |
---|
971 | |
---|
972 | # |
---|
973 | |
---|
974 | k = k + num_ops + 1 |
---|
975 | |
---|
976 | # Apply shifted positions after first cycle |
---|
977 | if i > 0: |
---|
978 | |
---|
979 | m = 0 |
---|
980 | while m < nbeads: |
---|
981 | aList_beads_x[m] = aList_beads_x_new[m] |
---|
982 | aList_beads_y[m] = aList_beads_y_new[m] |
---|
983 | aList_beads_z[m] = aList_beads_z_new[m] |
---|
984 | m = m + 1 |
---|
985 | |
---|
986 | # |
---|
987 | |
---|
988 | mean_force = (num_ops+1)*sum_forces_scale/(nbeads*3.0) |
---|
989 | scale = bead_sep/mean_force |
---|
990 | |
---|
991 | vdw_all = get_total_energy(aList_beads_x,aList_beads_y,aList_beads_z,econ12,econ6,bead_sep3) |
---|
992 | |
---|
993 | if icount == 0: |
---|
994 | aString = 'Emin cycle: ' + str(i) + ' Energy: ' + str('%4.2f'%(vdw_all)) |
---|
995 | print (aString) |
---|
996 | icount = -10 |
---|
997 | |
---|
998 | if vdw_all < 0.0: |
---|
999 | i = num_cycles |
---|
1000 | |
---|
1001 | i = i + 1 |
---|
1002 | |
---|
1003 | return; |
---|
1004 | |
---|
1005 | # Set up symmetry operators for rotational symmetry |
---|
1006 | |
---|
1007 | def make_symm(aList_symm,num_symm): |
---|
1008 | |
---|
1009 | angle_step = 360.0/float(num_symm) |
---|
1010 | |
---|
1011 | i = 1 |
---|
1012 | while i < num_symm: |
---|
1013 | theta = float(i) * angle_step |
---|
1014 | theta = math.radians(theta) |
---|
1015 | cos_theta = math.cos(theta) |
---|
1016 | sin_theta = math.sin(theta) |
---|
1017 | aList_s = [cos_theta,sin_theta,-sin_theta,cos_theta] |
---|
1018 | aList_symm.append(aList_s) |
---|
1019 | i = i + 1 |
---|
1020 | |
---|
1021 | return aList_symm; |
---|
1022 | |
---|
1023 | # Set up a shift vector in P(r) for a change in bead position |
---|
1024 | |
---|
1025 | def pr_shift_atom(aList_pr_model_test2,x1,y1,z1,\ |
---|
1026 | aList_beads_x,aList_beads_y,aList_beads_z,hist_grid,ii): |
---|
1027 | |
---|
1028 | num_hist = len(aList_r) |
---|
1029 | max_dr = (float(num_hist)-1.0)*hist_grid |
---|
1030 | num_beads = len(aList_beads_x) |
---|
1031 | |
---|
1032 | i = 0 |
---|
1033 | while i < num_hist: |
---|
1034 | aList_pr_model_test2[i] = 0.0 |
---|
1035 | i = i + 1 |
---|
1036 | |
---|
1037 | i = 0 |
---|
1038 | while i < num_beads: |
---|
1039 | |
---|
1040 | if i != ii: |
---|
1041 | x2 = float(aList_beads_x[i]) |
---|
1042 | y2 = float(aList_beads_y[i]) |
---|
1043 | z2 = float(aList_beads_z[i]) |
---|
1044 | dr = get_dr(x1,y1,z1,x2,y2,z2) |
---|
1045 | dr = min(dr,max_dr) |
---|
1046 | dr_grid = dr/hist_grid |
---|
1047 | int_dr_grid = int(dr_grid) |
---|
1048 | int_dr_grid = max(int_dr_grid,0) |
---|
1049 | int_dr_grid = min(int_dr_grid,num_hist-2) |
---|
1050 | ip_low = int_dr_grid |
---|
1051 | ip_high = ip_low + 1 |
---|
1052 | ip_high_frac = dr_grid - float(int_dr_grid) |
---|
1053 | ip_low_frac = 1.0 - ip_high_frac |
---|
1054 | aList_pr_model_test2[ip_low] = float(aList_pr_model_test2[ip_low]) + ip_low_frac |
---|
1055 | aList_pr_model_test2[ip_high] = float(aList_pr_model_test2[ip_high]) + ip_high_frac |
---|
1056 | |
---|
1057 | i = i + 1 |
---|
1058 | |
---|
1059 | return; |
---|
1060 | |
---|
1061 | # Recenter set of beads to origin |
---|
1062 | |
---|
1063 | def recenter_pdb(aList_beads_x,aList_beads_y,aList_beads_z): |
---|
1064 | |
---|
1065 | nbeads = len(aList_beads_x) |
---|
1066 | xsum = 0.0 |
---|
1067 | ysum = 0.0 |
---|
1068 | zsum = 0.0 |
---|
1069 | |
---|
1070 | i = 0 |
---|
1071 | while i < nbeads: |
---|
1072 | xsum = xsum + float(aList_beads_x[i]) |
---|
1073 | ysum = ysum + float(aList_beads_y[i]) |
---|
1074 | zsum = zsum + float(aList_beads_z[i]) |
---|
1075 | i = i + 1 |
---|
1076 | |
---|
1077 | xmean = xsum/float(nbeads) |
---|
1078 | ymean = ysum/float(nbeads) |
---|
1079 | zmean = zsum/float(nbeads) |
---|
1080 | |
---|
1081 | i = 0 |
---|
1082 | while i < nbeads: |
---|
1083 | aList_beads_x[i] = float(aList_beads_x[i]) - xmean |
---|
1084 | aList_beads_y[i] = float(aList_beads_y[i]) - ymean |
---|
1085 | aList_beads_z[i] = float(aList_beads_z[i]) - zmean |
---|
1086 | i = i + 1 |
---|
1087 | |
---|
1088 | return; |
---|
1089 | |
---|
1090 | ############# |
---|
1091 | # EXECUTION # |
---|
1092 | ############# |
---|
1093 | |
---|
1094 | #profiling start |
---|
1095 | pr = cProfile.Profile() |
---|
1096 | pr.enable() |
---|
1097 | time0 = time.time() |
---|
1098 | |
---|
1099 | version_aString = 'Program: SHAPES version 1.3' |
---|
1100 | |
---|
1101 | print (version_aString) |
---|
1102 | aString = 'Author: John Badger' |
---|
1103 | print (aString) |
---|
1104 | aString = 'Copyright: 2019, John Badger' |
---|
1105 | print (aString) |
---|
1106 | aString = 'License: GNU GPLv3' |
---|
1107 | print (aString) |
---|
1108 | |
---|
1109 | localtime = time.asctime( time.localtime(time.time()) ) |
---|
1110 | aString = 'Starting time: ' + str(localtime) + '\n' |
---|
1111 | print (aString) |
---|
1112 | |
---|
1113 | # aList_summary = [] |
---|
1114 | # aList_summary.append(version_aString) |
---|
1115 | # aList_summary.append(str(localtime)) |
---|
1116 | |
---|
1117 | ###################### |
---|
1118 | # Start up parmeters # |
---|
1119 | ###################### |
---|
1120 | # data['Shapes'] = {'outName':'','NumAA':100,'Niter':1,'AAscale':1.0,'Symm':1,'bias-z':0.0, |
---|
1121 | # 'inflateV':1.0,'AAglue':0.0} |
---|
1122 | |
---|
1123 | nbeads = 0 |
---|
1124 | num_sols = 1 |
---|
1125 | num_aa = 1.0 |
---|
1126 | num_symm = 1 |
---|
1127 | bias_z = 0.0 |
---|
1128 | inflate = 1.0 |
---|
1129 | prefix = '' |
---|
1130 | surface_scale = 0.0 |
---|
1131 | starting_pdb = 'no' |
---|
1132 | inFile = 'none' |
---|
1133 | pdbfile_in = 'none' |
---|
1134 | shapeDict = data['Shapes'] |
---|
1135 | prefix = shapeDict['outName'] |
---|
1136 | nbeads = shapeDict['NumAA'] |
---|
1137 | num_sols = shapeDict['Niter'] |
---|
1138 | num_aa = shapeDict['AAscale'] |
---|
1139 | num_symm = shapeDict['Symm'] |
---|
1140 | bias_z = shapeDict['bias-z'] |
---|
1141 | inflate = shapeDict['inflateV'] |
---|
1142 | surface_scale = shapeDict['AAglue'] |
---|
1143 | pdbOut = shapeDict['pdbOut'] |
---|
1144 | box_step = shapeDict.get('boxStep',4.0) |
---|
1145 | Phases = [] |
---|
1146 | Patterns = [] |
---|
1147 | PRcalc = [] |
---|
1148 | |
---|
1149 | # # Parse |
---|
1150 | # |
---|
1151 | # if os.path.exists('shapes_ip.txt'): |
---|
1152 | # file = open('shapes_ip.txt','r') |
---|
1153 | # allLines = file.readlines() |
---|
1154 | # file.close() |
---|
1155 | # else: |
---|
1156 | # aString = 'The local parameter file shapes_ip.txt was not found ! Exiting' |
---|
1157 | # print (aString) |
---|
1158 | # time.sleep(4) |
---|
1159 | # sys.exit(1) |
---|
1160 | # |
---|
1161 | # for eachLine in allLines: |
---|
1162 | # |
---|
1163 | # aList = eachLine.split() |
---|
1164 | # |
---|
1165 | # num_params = len(aList) |
---|
1166 | # if num_params > 0: |
---|
1167 | # |
---|
1168 | # if aList[0] == 'num_amino_acids': |
---|
1169 | # nbeads = int(aList[1]) |
---|
1170 | ## elif aList[0] == 'input_pr': |
---|
1171 | ## inFile = str(aList[1]) |
---|
1172 | # elif aList[0] == 'num_solns': |
---|
1173 | # num_sols = int(aList[1]) |
---|
1174 | # elif aList[0] == 'num_aa_scale': |
---|
1175 | # num_aa = float(aList[1]) |
---|
1176 | # elif aList[0] == 'symm': |
---|
1177 | # num_symm = int(aList[1]) |
---|
1178 | # elif aList[0] == 'bias_z': |
---|
1179 | # bias_z = float(aList[1]) |
---|
1180 | # elif aList[0] == 'inflate_vol': |
---|
1181 | # inflate = float(aList[1]) |
---|
1182 | # elif aList[0] == 'pdb_start': |
---|
1183 | # pdbfile_in = str(aList[1]) |
---|
1184 | # elif aList[0] == 'id': |
---|
1185 | # prefix = str(aList[1]) + '_' |
---|
1186 | # elif aList[0] == 'glue': |
---|
1187 | # surface_scale = float(aList[1]) |
---|
1188 | |
---|
1189 | |
---|
1190 | # Check inputs |
---|
1191 | |
---|
1192 | if num_sols > 0: |
---|
1193 | aString = 'Number of runs: ' + str(num_sols) |
---|
1194 | print (aString) |
---|
1195 | else: |
---|
1196 | aString = 'Zero reconstruction runs specified! Exiting' |
---|
1197 | print (aString) |
---|
1198 | time.sleep(4) |
---|
1199 | sys.exit(1) |
---|
1200 | |
---|
1201 | # |
---|
1202 | if nbeads == 0: |
---|
1203 | if os.path.exists(pdbfile_in): |
---|
1204 | aString = 'Will use CA atoms from ' + str(pdbfile_in) + ' as the initial bead distribution.' |
---|
1205 | print (aString) |
---|
1206 | starting_pdb = 'yes' |
---|
1207 | else: |
---|
1208 | aString = 'Zero amino acid count specified and no starting file found. Exiting' |
---|
1209 | print (aString) |
---|
1210 | time.sleep(4) |
---|
1211 | sys.exit(1) |
---|
1212 | else: |
---|
1213 | aString = 'Number of amino acids: ' + str(nbeads) |
---|
1214 | print (aString) |
---|
1215 | |
---|
1216 | # |
---|
1217 | # if os.path.exists(inFile): |
---|
1218 | # aString = 'Input P(r) file name: ' + str(inFile) |
---|
1219 | # print (aString) |
---|
1220 | # else: |
---|
1221 | # aString = 'P(r) input file not found. Exiting' |
---|
1222 | # print (aString) |
---|
1223 | # time.sleep(4) |
---|
1224 | # sys.exit(1) |
---|
1225 | |
---|
1226 | # |
---|
1227 | if num_aa == 0.0: |
---|
1228 | aString = 'Scale for amino acid count to particle number cannot be zero! Exiting' |
---|
1229 | print (aString) |
---|
1230 | time.sleep(4) |
---|
1231 | sys.exit(1) |
---|
1232 | else: |
---|
1233 | aString = 'Scale aa to bead count: ' + str(num_aa) |
---|
1234 | print (aString) |
---|
1235 | |
---|
1236 | # |
---|
1237 | if num_symm == 0: |
---|
1238 | aString = 'Rotational symmetry cannot be zero! Set to 1 for no symmetry. Exiting' |
---|
1239 | print (aString) |
---|
1240 | time.sleep(4) |
---|
1241 | sys.exit(1) |
---|
1242 | else: |
---|
1243 | aString = 'Point symmetry: ' + str(num_symm) |
---|
1244 | print (aString) |
---|
1245 | |
---|
1246 | # |
---|
1247 | if bias_z > 0.2: |
---|
1248 | aString = 'Max bias on Z axis for initial particle distribution is 0.2 (rods). Reset to 0.2.' |
---|
1249 | print (aString) |
---|
1250 | bias_z = 0.2 |
---|
1251 | elif bias_z < -0.2: |
---|
1252 | aString = 'Min bias on Z axis for initial particle distribution is -0.2 (disks). Reset to -0.2.' |
---|
1253 | print (aString) |
---|
1254 | bias_z = -0.2 |
---|
1255 | else: |
---|
1256 | aString = 'Z-axis bias: ' + str(bias_z) |
---|
1257 | print (aString) |
---|
1258 | |
---|
1259 | # |
---|
1260 | if inflate < 0.0: |
---|
1261 | aString = 'Inflation of PSV cannot be less than zero! Exiting' |
---|
1262 | print (aString) |
---|
1263 | time.sleep(4) |
---|
1264 | sys.exit(1) |
---|
1265 | elif inflate > 2.0: |
---|
1266 | aString = 'Inflation of PSV cannt be greater than 2.0! Exiting' |
---|
1267 | print (aString) |
---|
1268 | time.sleep(4) |
---|
1269 | sys.exit(1) |
---|
1270 | else: |
---|
1271 | aString = 'PSV inflation factor: ' + str(inflate) |
---|
1272 | print (aString) |
---|
1273 | |
---|
1274 | # |
---|
1275 | if surface_scale > 0.0: |
---|
1276 | aString = 'Cavity weight: ' + str(surface_scale) |
---|
1277 | print (aString) |
---|
1278 | |
---|
1279 | ########## UNIVERSAL CONSTANTS ###################### |
---|
1280 | |
---|
1281 | # No of macrocycles (gives extra cycles at constant volume after num_contract) |
---|
1282 | niter = 160 |
---|
1283 | |
---|
1284 | # No of contraction cycles |
---|
1285 | num_contract = 140 |
---|
1286 | |
---|
1287 | # Number of cycles at each fixed volume |
---|
1288 | num_micro_cyc = 10 |
---|
1289 | |
---|
1290 | # Final quench |
---|
1291 | num_sa_max = niter - num_micro_cyc |
---|
1292 | |
---|
1293 | # Initial scale for P(r) shifts versus E shifts |
---|
1294 | hscale = 3000.0 |
---|
1295 | |
---|
1296 | # Standard deviation for annealing acceptance (cf well-depth of -1 unit for two beads) |
---|
1297 | sd_mc = float(num_symm) * 2.0 |
---|
1298 | |
---|
1299 | # Fiddle factor for keeping the accessible, molecular volume larger than PSV |
---|
1300 | scale_vol = 1.15 |
---|
1301 | |
---|
1302 | # Standard amino acid volume MW = 110.0 x 1.21 i.e. mean mw x mw-to-vol-scale |
---|
1303 | vol_bead = 133.1 |
---|
1304 | |
---|
1305 | # Bead separation for best packing ~5.6 (I think) |
---|
1306 | #- 75% better than rectangular grid 5.1 for this amino acid vol |
---|
1307 | bead_sep = 5.6 |
---|
1308 | |
---|
1309 | # Usually num_aa is unity. Adjust parameters otherwise |
---|
1310 | if num_aa != 1 and nbeads != 0: |
---|
1311 | nbeads = int(nbeads*num_aa) |
---|
1312 | vol_bead = vol_bead / num_aa |
---|
1313 | bead_sep = (vol_bead * 4/3)**(1.0/3.0) |
---|
1314 | |
---|
1315 | # Increase bead separation for inflated volumes |
---|
1316 | bead_sep = bead_sep * inflate**(1.0/3.0) |
---|
1317 | |
---|
1318 | # Partial specific volumes at start and end |
---|
1319 | |
---|
1320 | if starting_pdb == 'yes': |
---|
1321 | nmols_vol_start = 1.1 * inflate |
---|
1322 | else: |
---|
1323 | nmols_vol_start = 2.0 * inflate |
---|
1324 | |
---|
1325 | nmols_vol_end = 1.0 * inflate |
---|
1326 | nmols_vol_subtract = nmols_vol_start - nmols_vol_end |
---|
1327 | |
---|
1328 | # Box parametere |
---|
1329 | # box_step = 4.0 #5.0 |
---|
1330 | box_pt_vol = box_step*box_step*box_step |
---|
1331 | |
---|
1332 | # Energy parameters - flat bottomed VDW (2.0A for a 5.6A bead separation) |
---|
1333 | |
---|
1334 | well_width = 0.36*bead_sep |
---|
1335 | econ12 = bead_sep**12 |
---|
1336 | econ6 = bead_sep**6 |
---|
1337 | r_width = bead_sep + well_width |
---|
1338 | r_width6 = r_width**6 |
---|
1339 | r_width12 = r_width6**2 |
---|
1340 | e_width = econ12/r_width12 - 2.0*econ6/r_width6 |
---|
1341 | bead_sep3 = 3.0*bead_sep |
---|
1342 | abs_e_width = abs(e_width) |
---|
1343 | |
---|
1344 | # Range for box identification (might need to increase for poor data) |
---|
1345 | rsearch = (bead_sep + 0.5*well_width)*1.5 |
---|
1346 | |
---|
1347 | # Search range for optional cavity inhibition energy term |
---|
1348 | radii_1 = 1.5*bead_sep |
---|
1349 | radii_2 = 4.0*bead_sep |
---|
1350 | |
---|
1351 | # Setup symmetry operators |
---|
1352 | |
---|
1353 | aList_symm = [] |
---|
1354 | aList_symm = make_symm(aList_symm,num_symm) |
---|
1355 | num_ops = len(aList_symm) |
---|
1356 | |
---|
1357 | # Read experimental histogram |
---|
1358 | |
---|
1359 | aList_r = [] |
---|
1360 | aList_pr = [] |
---|
1361 | aList_pr_sd = [] |
---|
1362 | aList_pr_model = [] |
---|
1363 | aList_pr_model_test = [] |
---|
1364 | aList_pr_model_test2 = [] |
---|
1365 | |
---|
1366 | (dmax,hist_grid,num_hist_in,angstrom_scale) = read_pr(aList_r,aList_pr,aList_pr_sd,\ |
---|
1367 | aList_pr_model,aList_pr_model_test,\ |
---|
1368 | aList_pr_model_test2,inFile) |
---|
1369 | |
---|
1370 | # dmax_over2 = dmax/2.0 |
---|
1371 | num_hist = len(aList_r) |
---|
1372 | |
---|
1373 | aString = 'Number of points read from P(r): ' + str(num_hist_in) |
---|
1374 | print (aString) |
---|
1375 | aString = 'Grid sampling: ' + str(hist_grid) + ' Dmax: ' + str(dmax) |
---|
1376 | print (aString) |
---|
1377 | |
---|
1378 | # Skip over initial points in scoring |
---|
1379 | |
---|
1380 | skip = r_width/float(num_hist) |
---|
1381 | skip = int(skip) + 2 |
---|
1382 | |
---|
1383 | # Read intensity data that was used for P(r) |
---|
1384 | |
---|
1385 | aList_q = [] |
---|
1386 | aList_i = [] |
---|
1387 | aList_i_sd = [] |
---|
1388 | |
---|
1389 | read_i(aList_q,aList_i,aList_i_sd,inFile,angstrom_scale) |
---|
1390 | |
---|
1391 | num_intensities = len(aList_q) |
---|
1392 | aString = 'Number of intensity data points read: ' + str(num_intensities) |
---|
1393 | print (aString) |
---|
1394 | |
---|
1395 | ######################### |
---|
1396 | # CYCLE OVER SOLUTIONS # |
---|
1397 | ######################### |
---|
1398 | |
---|
1399 | i_soln = 0 |
---|
1400 | while i_soln < num_sols: |
---|
1401 | |
---|
1402 | file_no = str(i_soln + 1) |
---|
1403 | |
---|
1404 | aString = '\nReconstruction trial: ' + str(file_no) |
---|
1405 | print (aString) |
---|
1406 | |
---|
1407 | aString = 'Trial:' + file_no |
---|
1408 | # aList_summary.append(aString) |
---|
1409 | |
---|
1410 | file_beads = prefix + 'beads_' + file_no + '.pdb' |
---|
1411 | # file_pr = prefix + 'pr_calc_' + file_no + '.dat' |
---|
1412 | file_psv = prefix + 'psv_shape_' + file_no + '.pdb' |
---|
1413 | # file_intensity = prefix + 'intensity_' + file_no + '.dat' |
---|
1414 | |
---|
1415 | # Setup initial bead distribution |
---|
1416 | |
---|
1417 | aList_beads_x = [] |
---|
1418 | aList_beads_y = [] |
---|
1419 | aList_beads_z = [] |
---|
1420 | |
---|
1421 | # Re-initialize standard deviation for annealing acceptance |
---|
1422 | sd_mc = float(num_symm) * 2.0 |
---|
1423 | |
---|
1424 | # Set random bead distribution |
---|
1425 | |
---|
1426 | if starting_pdb == 'yes': |
---|
1427 | read_pdb(aList_beads_x,aList_beads_y,aList_beads_z,pdbfile_in) |
---|
1428 | nbeads = len(aList_beads_x) |
---|
1429 | num_symm = 1 |
---|
1430 | aString = 'Number of CA sites read: ' + str(nbeads) |
---|
1431 | print (aString) |
---|
1432 | aString = 'Symmetry set to 1 (required)' |
---|
1433 | print (aString) |
---|
1434 | aString = 'Input center was shifted to the origin' |
---|
1435 | print (aString) |
---|
1436 | else: |
---|
1437 | random_beads(aList_beads_x,aList_beads_y,aList_beads_z,nbeads,dmax,aList_symm,bias_z) |
---|
1438 | nbeads = len(aList_beads_x) |
---|
1439 | aString = 'Number of beads randomly placed: ' + str(nbeads) |
---|
1440 | print (aString) |
---|
1441 | |
---|
1442 | # Protein partial specific volume |
---|
1443 | psv_vol = float(nbeads)*vol_bead |
---|
1444 | |
---|
1445 | # Histogram of inter-bead distance |
---|
1446 | |
---|
1447 | calc_pr(aList_beads_x,aList_beads_y,aList_beads_z,aList_pr_model,hist_grid) |
---|
1448 | |
---|
1449 | # Scale experimental P(r) and model histogram |
---|
1450 | |
---|
1451 | scale_pr(aList_pr,aList_pr_sd,aList_pr_model) |
---|
1452 | |
---|
1453 | # Minimize initial energy using expanded VDW |
---|
1454 | |
---|
1455 | if starting_pdb != 'yes': |
---|
1456 | aString = 'Minimize energy of initial positions' |
---|
1457 | print (aString) |
---|
1458 | bead_sep_e = 1.35*bead_sep |
---|
1459 | bead_sep3_e = 3.0*bead_sep_e |
---|
1460 | e_min(aList_beads_x,aList_beads_y,aList_beads_z,bead_sep_e,bead_sep3_e,aList_symm) |
---|
1461 | else: |
---|
1462 | aString = 'Skipping energy minimization of initial positions' |
---|
1463 | print (aString) |
---|
1464 | |
---|
1465 | # Get the initial score between observed and calculated P(r) |
---|
1466 | |
---|
1467 | hist_score_best = pr_dif(aList_pr,aList_pr_model,skip) |
---|
1468 | aString = 'Initial rms P(r): ' + str('%4.3f'%(hist_score_best)) |
---|
1469 | print (aString) |
---|
1470 | |
---|
1471 | ########################### |
---|
1472 | # Iterate # |
---|
1473 | ########################### |
---|
1474 | |
---|
1475 | num_boxes = 0 |
---|
1476 | count_boxing = 0 |
---|
1477 | fraction_psv = 0 |
---|
1478 | success_rate_all = 0.0 |
---|
1479 | box_iter = num_micro_cyc - 1 |
---|
1480 | |
---|
1481 | sum_delta_pack = 0.0 |
---|
1482 | |
---|
1483 | count_it = 0 |
---|
1484 | while count_it < niter: |
---|
1485 | |
---|
1486 | success = 0 |
---|
1487 | count_hist_yes = 0 |
---|
1488 | sum_e = 0.0 |
---|
1489 | sum_h = 0.0 |
---|
1490 | |
---|
1491 | # Find populated volumes and fix solution every 10 macrocycles |
---|
1492 | |
---|
1493 | box_iter = box_iter + 1 |
---|
1494 | |
---|
1495 | if box_iter == num_micro_cyc: |
---|
1496 | |
---|
1497 | box_iter = 0 |
---|
1498 | count_boxing = count_boxing + 1 |
---|
1499 | |
---|
1500 | if count_it < num_contract - 1: |
---|
1501 | scale = float(count_it)/float(num_contract) |
---|
1502 | else: |
---|
1503 | scale = 1.0 |
---|
1504 | |
---|
1505 | # Establish confinement volume using a local average |
---|
1506 | |
---|
1507 | aList_box_x_all = [] |
---|
1508 | aList_box_y_all = [] |
---|
1509 | aList_box_z_all = [] |
---|
1510 | aList_box_score = [] |
---|
1511 | |
---|
1512 | recenter_pdb(aList_beads_x,aList_beads_y,aList_beads_z) |
---|
1513 | |
---|
1514 | # Adaptive masking was not helpful |
---|
1515 | # rsearch_use = (2.0 - scale)*rsearch |
---|
1516 | |
---|
1517 | set_box_fast(aList_beads_x,aList_beads_y,aList_beads_z,\ |
---|
1518 | aList_box_x_all,aList_box_y_all,aList_box_z_all,\ |
---|
1519 | aList_box_score,box_step,dmax,rsearch) |
---|
1520 | |
---|
1521 | aList_box_x = [] |
---|
1522 | aList_box_y = [] |
---|
1523 | aList_box_z = [] |
---|
1524 | |
---|
1525 | psv_ratio = nmols_vol_start - scale*nmols_vol_subtract |
---|
1526 | vol_target = scale_vol*(psv_ratio*psv_vol) |
---|
1527 | |
---|
1528 | set_vol(aList_box_x_all,aList_box_y_all,aList_box_z_all,\ |
---|
1529 | aList_box_score,aList_box_x,aList_box_y,aList_box_z,\ |
---|
1530 | vol_target,box_pt_vol) |
---|
1531 | |
---|
1532 | num_boxes = len(aList_box_x) |
---|
1533 | fraction_psv = float(num_boxes)*box_pt_vol/psv_vol |
---|
1534 | |
---|
1535 | # Find beads that are ouside the allowed volume |
---|
1536 | |
---|
1537 | aList_contacts = [] |
---|
1538 | disallowed_beads(aList_beads_x,aList_beads_y,aList_beads_z,aList_contacts,\ |
---|
1539 | aList_box_x,aList_box_y,aList_box_z,rsearch) |
---|
1540 | num_outof_box = len(aList_contacts) |
---|
1541 | |
---|
1542 | aString = 'Target volume: ' + str('%4.2f'%(scale_vol*psv_ratio)) + ' Actual volume: ' + \ |
---|
1543 | str('%4.2f'%(fraction_psv)) + ' Beads outside volume: ' + str(num_outof_box) |
---|
1544 | print (aString) |
---|
1545 | |
---|
1546 | # Recalculate P(r) and rescore for reliability |
---|
1547 | |
---|
1548 | calc_pr(aList_beads_x,aList_beads_y,aList_beads_z,aList_pr_model,hist_grid) |
---|
1549 | hist_score_best = pr_dif(aList_pr,aList_pr_model,skip) |
---|
1550 | |
---|
1551 | # Reset SA deviation if mean success rate over last trials is under 0.1 |
---|
1552 | |
---|
1553 | mean_success_rate = float(success_rate_all)/float(num_micro_cyc) |
---|
1554 | |
---|
1555 | if count_it < num_contract and count_boxing != 1: |
---|
1556 | |
---|
1557 | if mean_success_rate < 0.1: |
---|
1558 | sd_mc = 1.3*sd_mc |
---|
1559 | aString = 'Raising allowed energy deviation to ' + str('%4.2f'%(sd_mc)) |
---|
1560 | print (aString) |
---|
1561 | |
---|
1562 | if mean_success_rate > 0.2: |
---|
1563 | sd_mc = 0.7*sd_mc |
---|
1564 | aString = 'Reducing allowed energy deviation to ' + str('%4.2f'%(sd_mc)) |
---|
1565 | print (aString) |
---|
1566 | |
---|
1567 | success_rate_all = 0.0 |
---|
1568 | |
---|
1569 | # Make one macrocycle that is a run over the nbeads |
---|
1570 | |
---|
1571 | ii = 0 |
---|
1572 | while ii < nbeads: |
---|
1573 | |
---|
1574 | # Initialize |
---|
1575 | |
---|
1576 | energy_old = 0.0 |
---|
1577 | energy_new = 0.0 |
---|
1578 | |
---|
1579 | i = 0 |
---|
1580 | while i < num_hist: |
---|
1581 | aList_pr_model_test[i] = 0.0 |
---|
1582 | i = i + 1 |
---|
1583 | |
---|
1584 | # Select a target bead and make trial shift |
---|
1585 | |
---|
1586 | xold = float(aList_beads_x[ii]) |
---|
1587 | yold = float(aList_beads_y[ii]) |
---|
1588 | zold = float(aList_beads_z[ii]) |
---|
1589 | |
---|
1590 | ibox = random.randint(0,num_boxes-1) |
---|
1591 | xtest = float(aList_box_x[ibox]) + random.uniform(-rsearch,rsearch) |
---|
1592 | ytest = float(aList_box_y[ibox]) + random.uniform(-rsearch,rsearch) |
---|
1593 | ztest = float(aList_box_z[ibox]) + random.uniform(-rsearch,rsearch) |
---|
1594 | |
---|
1595 | # Calculate and capture symmetry mates |
---|
1596 | |
---|
1597 | aList_temp_save_x = [] |
---|
1598 | aList_temp_save_y = [] |
---|
1599 | aList_temp_save_z = [] |
---|
1600 | aList_symm_x = [] |
---|
1601 | aList_symm_y = [] |
---|
1602 | aList_symm_z = [] |
---|
1603 | |
---|
1604 | l = 0 |
---|
1605 | while l < num_ops: |
---|
1606 | aList_s = aList_symm[l] |
---|
1607 | m11 = float(aList_s[0]) |
---|
1608 | m12 = float(aList_s[1]) |
---|
1609 | m21 = float(aList_s[2]) |
---|
1610 | m22 = float(aList_s[3]) |
---|
1611 | |
---|
1612 | xs = m11*xtest + m12*ytest |
---|
1613 | ys = m21*xtest + m22*ytest |
---|
1614 | zs = ztest |
---|
1615 | |
---|
1616 | aList_symm_x.append(xs) |
---|
1617 | aList_symm_y.append(ys) |
---|
1618 | aList_symm_z.append(zs) |
---|
1619 | |
---|
1620 | ipt = ii + l + 1 |
---|
1621 | aList_temp_save_x.append(aList_beads_x[ipt]) |
---|
1622 | aList_temp_save_y.append(aList_beads_y[ipt]) |
---|
1623 | aList_temp_save_z.append(aList_beads_z[ipt]) |
---|
1624 | |
---|
1625 | l = l + 1 |
---|
1626 | |
---|
1627 | # Get initial VDW energy for interactions of this bead with all others |
---|
1628 | |
---|
1629 | i = 0 |
---|
1630 | while i < nbeads: |
---|
1631 | if i != ii: |
---|
1632 | x = float(aList_beads_x[i]) |
---|
1633 | y = float(aList_beads_y[i]) |
---|
1634 | z = float(aList_beads_z[i]) |
---|
1635 | dr_old = get_dr(xold,yold,zold,x,y,z) |
---|
1636 | vdw_old = vdw_energy(econ12,econ6,e_width,dr_old,bead_sep3) |
---|
1637 | energy_old = energy_old + num_symm*vdw_old |
---|
1638 | i = i + 1 |
---|
1639 | |
---|
1640 | # Get initial contributions to P(r) |
---|
1641 | |
---|
1642 | pr_shift_atom(aList_pr_model_test2,xold,yold,zold,aList_beads_x,\ |
---|
1643 | aList_beads_y,aList_beads_z,hist_grid,ii) |
---|
1644 | |
---|
1645 | i = 0 |
---|
1646 | while i < num_hist: |
---|
1647 | aList_pr_model_test[i] = aList_pr_model_test2[i] |
---|
1648 | i = i + 1 |
---|
1649 | |
---|
1650 | # Get VDW energy for interactions of the shifted bead with all others |
---|
1651 | |
---|
1652 | l = 0 |
---|
1653 | while l < num_ops: |
---|
1654 | ipt = ii + l + 1 |
---|
1655 | aList_beads_x[ipt] = aList_symm_x[l] |
---|
1656 | aList_beads_y[ipt] = aList_symm_y[l] |
---|
1657 | aList_beads_z[ipt] = aList_symm_z[l] |
---|
1658 | l = l + 1 |
---|
1659 | |
---|
1660 | i = 0 |
---|
1661 | while i < nbeads: |
---|
1662 | if i != ii: |
---|
1663 | x = float(aList_beads_x[i]) |
---|
1664 | y = float(aList_beads_y[i]) |
---|
1665 | z = float(aList_beads_z[i]) |
---|
1666 | dr_new = get_dr(xtest,ytest,ztest,x,y,z) |
---|
1667 | vdw_new = vdw_energy(econ12,econ6,e_width,dr_new,bead_sep3) |
---|
1668 | energy_new = energy_new + num_symm*vdw_new |
---|
1669 | i = i + 1 |
---|
1670 | |
---|
1671 | # Get cavity energy difference |
---|
1672 | |
---|
1673 | delta_old = center_beads(xold,yold,zold,aList_beads_x,\ |
---|
1674 | aList_beads_y,aList_beads_z,radii_1,radii_2) |
---|
1675 | delta_new = center_beads(xtest,ytest,ztest,aList_beads_x,\ |
---|
1676 | aList_beads_y,aList_beads_z,radii_1,radii_2) |
---|
1677 | |
---|
1678 | delta_pack = num_symm*surface_scale*(delta_new - delta_old)/(radii_1 + radii_2) |
---|
1679 | sum_delta_pack = sum_delta_pack + abs(delta_pack) |
---|
1680 | |
---|
1681 | # Get shifted contributions to P(r) |
---|
1682 | |
---|
1683 | pr_shift_atom(aList_pr_model_test2,xtest,ytest,ztest,aList_beads_x,\ |
---|
1684 | aList_beads_y,aList_beads_z,hist_grid,ii) |
---|
1685 | |
---|
1686 | # Get net shift in contribution to P(r) |
---|
1687 | |
---|
1688 | i = 0 |
---|
1689 | while i < num_hist: |
---|
1690 | aList_pr_model_test[i] = aList_pr_model_test2[i] - aList_pr_model_test[i] |
---|
1691 | i = i + 1 |
---|
1692 | |
---|
1693 | # Get statistic for agreement with P(r) after accumulating shift vectors |
---|
1694 | |
---|
1695 | i = 0 |
---|
1696 | while i < num_hist: |
---|
1697 | aList_pr_model_test[i] = float(aList_pr_model[i]) + num_symm*float(aList_pr_model_test[i]) |
---|
1698 | i = i + 1 |
---|
1699 | |
---|
1700 | hist_score = pr_dif(aList_pr,aList_pr_model_test,skip) |
---|
1701 | |
---|
1702 | # Scoring shifts |
---|
1703 | |
---|
1704 | delta_h = hist_score - hist_score_best |
---|
1705 | delta_e = energy_new - energy_old + delta_pack |
---|
1706 | |
---|
1707 | # Recalibrate scale so impact of energy and P(r) is equal on plausible shifts |
---|
1708 | |
---|
1709 | if delta_e < abs_e_width: |
---|
1710 | sum_e = sum_e + abs(delta_e) |
---|
1711 | sum_h = sum_h + abs(delta_h) |
---|
1712 | |
---|
1713 | # Monitor potential moves based of P(r) |
---|
1714 | |
---|
1715 | if delta_h < 0.0: |
---|
1716 | count_hist_yes = count_hist_yes + 1.0 |
---|
1717 | |
---|
1718 | # Acceptance and update |
---|
1719 | |
---|
1720 | score = delta_e + delta_h*hscale |
---|
1721 | |
---|
1722 | if count_it < num_sa_max: |
---|
1723 | barrier = abs(random.gauss(0.0,sd_mc)) |
---|
1724 | else: |
---|
1725 | barrier = 0.0 |
---|
1726 | |
---|
1727 | if score < barrier: |
---|
1728 | |
---|
1729 | success = success + 1.0 |
---|
1730 | hist_score_best = hist_score |
---|
1731 | |
---|
1732 | # Update model but symmetry positions that were already put in |
---|
1733 | |
---|
1734 | aList_beads_x[ii] = xtest |
---|
1735 | aList_beads_y[ii] = ytest |
---|
1736 | aList_beads_z[ii] = ztest |
---|
1737 | |
---|
1738 | # Update P(r) |
---|
1739 | |
---|
1740 | i = 0 |
---|
1741 | while i < num_hist: |
---|
1742 | aList_pr_model[i] = aList_pr_model_test[i] |
---|
1743 | i = i + 1 |
---|
1744 | |
---|
1745 | else: |
---|
1746 | |
---|
1747 | # Revert to original model |
---|
1748 | |
---|
1749 | aList_beads_x[ii] = xold |
---|
1750 | aList_beads_y[ii] = yold |
---|
1751 | aList_beads_z[ii] = zold |
---|
1752 | |
---|
1753 | l = 0 |
---|
1754 | while l < num_ops: |
---|
1755 | ipt = ii + l + 1 |
---|
1756 | aList_beads_x[ipt] = aList_temp_save_x[l] |
---|
1757 | aList_beads_y[ipt] = aList_temp_save_y[l] |
---|
1758 | aList_beads_z[ipt] = aList_temp_save_z[l] |
---|
1759 | l = l + 1 |
---|
1760 | # |
---|
1761 | |
---|
1762 | ii = ii + num_symm |
---|
1763 | |
---|
1764 | # Get energy statistics at end of macrocycle |
---|
1765 | |
---|
1766 | vdw_all = get_total_energy(aList_beads_x,aList_beads_y,aList_beads_z,econ12,econ6,bead_sep3) |
---|
1767 | |
---|
1768 | # Rescale and convergence statistics |
---|
1769 | |
---|
1770 | if sum_h > 0.0: |
---|
1771 | hscale = sum_e/sum_h |
---|
1772 | |
---|
1773 | count_hist_yes = count_hist_yes*float(num_symm)/float(nbeads) |
---|
1774 | success_rate = success*float(num_symm)/float(nbeads) |
---|
1775 | success_rate_all = success_rate_all + success_rate |
---|
1776 | |
---|
1777 | aString = 'Cycle ' + str(count_it+1) + ' Moves ' + str('%.2f'%(success_rate)) + \ |
---|
1778 | ' Possibles ' + str('%.2f'%(count_hist_yes)) + ' rms P(r) '+ str('%4.3f'%(hist_score)) + \ |
---|
1779 | ' Energy ' + str('%4.2f'%(vdw_all)) |
---|
1780 | print (aString) |
---|
1781 | |
---|
1782 | # Debug statitics. Weight of 10 gives about 1.0 |
---|
1783 | #sum_delta_pack = sum_delta_pack/float(nbeads) |
---|
1784 | #print (sum_delta_pack) |
---|
1785 | # |
---|
1786 | |
---|
1787 | count_it = count_it + 1 |
---|
1788 | |
---|
1789 | ##################### |
---|
1790 | # ANALYZE AND WRITE # |
---|
1791 | ##################### |
---|
1792 | |
---|
1793 | aString = '\nFinal model statistics' |
---|
1794 | print (aString) |
---|
1795 | |
---|
1796 | calc_pr(aList_beads_x,aList_beads_y,aList_beads_z,aList_pr_model,hist_grid) |
---|
1797 | hist_score_best = pr_dif(aList_pr,aList_pr_model,skip) |
---|
1798 | |
---|
1799 | # P(r) fitting statistics |
---|
1800 | delta_hist_sum = pr_rfactor(aList_pr,aList_pr_sd,aList_pr_model_test,skip) |
---|
1801 | |
---|
1802 | aString = 'Delta P(r): ' + str('%4.3f'%(delta_hist_sum)) |
---|
1803 | print (aString) |
---|
1804 | |
---|
1805 | # Get final energy |
---|
1806 | vdw_all = get_total_energy(aList_beads_x,aList_beads_y,aList_beads_z,econ12,econ6,bead_sep3) |
---|
1807 | |
---|
1808 | aString = 'VDW energy: ' + str('%4.2f'%(vdw_all)) |
---|
1809 | print (aString) |
---|
1810 | |
---|
1811 | Phases.append([file_beads.split('.')[0],aList_beads_x,aList_beads_y,aList_beads_z]) |
---|
1812 | # Write out beads as pseudo a PDB file |
---|
1813 | if pdbOut: |
---|
1814 | pdb_writer(aList_beads_x,aList_beads_y,aList_beads_z,file_beads,aString) |
---|
1815 | |
---|
1816 | # Calculate and write final PSV shape |
---|
1817 | |
---|
1818 | aList_box_x_all = [] |
---|
1819 | aList_box_y_all = [] |
---|
1820 | aList_box_z_all = [] |
---|
1821 | aList_box_score = [] |
---|
1822 | |
---|
1823 | set_box_fast(aList_beads_x,aList_beads_y,aList_beads_z,\ |
---|
1824 | aList_box_x_all,aList_box_y_all,aList_box_z_all,\ |
---|
1825 | aList_box_score,box_step,dmax,rsearch) |
---|
1826 | |
---|
1827 | aList_box_x = [] |
---|
1828 | aList_box_y = [] |
---|
1829 | aList_box_z = [] |
---|
1830 | psv_vol_use = psv_vol*inflate |
---|
1831 | |
---|
1832 | set_vol(aList_box_x_all,aList_box_y_all,aList_box_z_all,\ |
---|
1833 | aList_box_score,aList_box_x,aList_box_y,aList_box_z,\ |
---|
1834 | psv_vol_use,box_pt_vol) |
---|
1835 | |
---|
1836 | num_boxes = len(aList_box_x) |
---|
1837 | fraction_psv = num_boxes*box_pt_vol/psv_vol |
---|
1838 | |
---|
1839 | # Correct final volume if initial estimate is too small |
---|
1840 | |
---|
1841 | fraction_psv_use = num_boxes*box_pt_vol/psv_vol_use |
---|
1842 | |
---|
1843 | if fraction_psv_use < 1.0: |
---|
1844 | aList_box_x = [] |
---|
1845 | aList_box_y = [] |
---|
1846 | aList_box_z = [] |
---|
1847 | vol_use = 1.05*psv_vol_use |
---|
1848 | set_vol(aList_box_x_all,aList_box_y_all,aList_box_z_all,\ |
---|
1849 | aList_box_score,aList_box_x,aList_box_y,aList_box_z,\ |
---|
1850 | vol_use,box_pt_vol) |
---|
1851 | |
---|
1852 | num_boxes = len(aList_box_x) |
---|
1853 | fraction_psv = float(num_boxes)*box_pt_vol/psv_vol |
---|
1854 | |
---|
1855 | aString = 'Final PSV of protein envelope: ' + str('%4.2f'%(fraction_psv)) |
---|
1856 | print (aString) |
---|
1857 | |
---|
1858 | # Write input and model P(r) |
---|
1859 | # pr_writer(aList_pr,aList_r,aList_pr_model,file_pr) |
---|
1860 | PRcalc.append([aList_r,aList_pr,copy.copy(aList_pr_model),delta_hist_sum]) |
---|
1861 | |
---|
1862 | # Calculate comparison versus intensities |
---|
1863 | |
---|
1864 | if num_intensities > 0: |
---|
1865 | |
---|
1866 | # calculate intensity |
---|
1867 | aList_i_calc = [] |
---|
1868 | # num_beads = len(aList_box_x) |
---|
1869 | ft_to_intensity(aList_q,aList_i_calc,aList_r,aList_pr_model,nbeads) |
---|
1870 | |
---|
1871 | # Scale and obtain statistics |
---|
1872 | (chi_sq,rvalue) = score_Ic(aList_q,aList_i,aList_i_sd,aList_i_calc) |
---|
1873 | |
---|
1874 | aString = 'Rvalue: ' + str('%4.3f'%(rvalue)) + ' CHI-squared: ' + str('%4.3f'%(chi_sq)) |
---|
1875 | print (aString) |
---|
1876 | |
---|
1877 | # Write output intensity file |
---|
1878 | Patterns.append([aList_q,aList_i,aList_i_calc,rvalue]) |
---|
1879 | # write_all_data(file_intensity,aList_q,aList_i,aList_i_calc,aString) |
---|
1880 | |
---|
1881 | # aString = 'Output intensity file: ' + str(file_intensity) |
---|
1882 | # print (aString) |
---|
1883 | |
---|
1884 | else: |
---|
1885 | |
---|
1886 | chi_sq = 'NA' |
---|
1887 | |
---|
1888 | # Write final volume |
---|
1889 | |
---|
1890 | delta_hist_sum = '%4.3f'%(delta_hist_sum) |
---|
1891 | vdw_all = '%4.2f'%(vdw_all) |
---|
1892 | fraction_psv = '%4.2f'%(fraction_psv) |
---|
1893 | chi_sq = '%4.3f'%(chi_sq) |
---|
1894 | |
---|
1895 | aString = 'REMARK P(r) dif:'+ str(delta_hist_sum) + ' E:'\ |
---|
1896 | + str(vdw_all) + ' CHIsq:' + str(chi_sq) + \ |
---|
1897 | ' PSV:' + str(fraction_psv) |
---|
1898 | |
---|
1899 | Phases.append([file_psv.split('.')[0],aList_box_x,aList_box_y,aList_box_z]) |
---|
1900 | if pdbOut: |
---|
1901 | pdb_writer(aList_box_x,aList_box_y,aList_box_z,file_psv,aString) |
---|
1902 | |
---|
1903 | # Write Summary |
---|
1904 | |
---|
1905 | aString = 'P(r) dif:' + str(delta_hist_sum) + ' E:' \ |
---|
1906 | + str(vdw_all) + ' CHISQ:' + str(chi_sq) + ' PSV:' + str(fraction_psv) |
---|
1907 | # aList_summary.append(aString) |
---|
1908 | |
---|
1909 | i_soln = i_soln + 1 |
---|
1910 | |
---|
1911 | ######################################### |
---|
1912 | #### END OF LOOP OVER MULTI-SOLUTIONS ### |
---|
1913 | ######################################### |
---|
1914 | |
---|
1915 | #end profiling |
---|
1916 | pr.disable() |
---|
1917 | s = StringIO.StringIO() |
---|
1918 | sortby = 'tottime' |
---|
1919 | ps = pstats.Stats(pr, stream=s).strip_dirs().sort_stats(sortby) |
---|
1920 | print('Profiler of function calculation; top 50% of routines:') |
---|
1921 | ps.print_stats(.5) |
---|
1922 | print(s.getvalue()) |
---|
1923 | print('%s%.3f'%('Run time = ',time.time()-time0)) |
---|
1924 | |
---|
1925 | localtime = time.asctime( time.localtime(time.time()) ) |
---|
1926 | |
---|
1927 | aString = '\nCompletion time: ' + str(localtime) |
---|
1928 | print (aString) |
---|
1929 | |
---|
1930 | # aList_summary.append(str(localtime)) |
---|
1931 | # |
---|
1932 | # # Create summary |
---|
1933 | # |
---|
1934 | # aFile_log = prefix + 'shapes_summary.log' |
---|
1935 | # num_lines = len(aList_summary) |
---|
1936 | # |
---|
1937 | # file = open(aFile_log,'w') |
---|
1938 | # i = 0 |
---|
1939 | # while i < num_lines: |
---|
1940 | # aString = str(aList_summary[i]) |
---|
1941 | # file.write(aString) |
---|
1942 | # file.write('\n') |
---|
1943 | # i = i + 1 |
---|
1944 | # file.close() |
---|
1945 | |
---|
1946 | return Phases,Patterns,PRcalc |
---|
1947 | |
---|
1948 | |
---|
1949 | |
---|