1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.xray;
39
40 import ffx.crystal.ReflectionList;
41 import ffx.numerics.math.ComplexNumber;
42 import org.apache.commons.configuration2.CompositeConfiguration;
43
44 import java.util.Random;
45 import java.util.logging.Level;
46 import java.util.logging.Logger;
47
48 import static ffx.numerics.math.ScalarMath.b2u;
49 import static java.lang.Double.isNaN;
50 import static java.lang.String.format;
51 import static org.apache.commons.math3.util.FastMath.PI;
52 import static org.apache.commons.math3.util.FastMath.sqrt;
53
54
55
56
57
58
59
60 public class DiffractionRefinementData {
61
62 private static final Logger logger = Logger.getLogger(DiffractionRefinementData.class.getName());
63
64 public final int n;
65
66 public final int[] freeR;
67
68 public final double[][] fc;
69
70 public final double[][] fs;
71
72 public final double[][] fomPhi;
73
74 public final double[][] foFc1;
75
76 final double[][] foFc2;
77
78
79
80 public final double fSigFCutoff;
81
82 final int nScale;
83
84 final double[][] fSigF;
85
86 final double[][] fcTot;
87
88 final double[][] dFc;
89
90 final double[][] dFs;
91
92 final int nBins;
93
94 public double[] spline;
95
96 public double[] sigmaA;
97
98 public double[] sigmaW;
99
100
101
102
103 public int rFreeFlag;
104
105 double llkR, llkF;
106
107 CrystalReciprocalSpace crystalReciprocalSpaceFc;
108
109
110
111
112 CrystalReciprocalSpace crystalReciprocalSpaceFs;
113
114 double[] esqFc;
115
116 double[] esqFo;
117
118 double bulkSolventK, bulkSolventUeq;
119
120
121
122 boolean bulkSolventFixed = false;
123
124 double modelScaleK;
125
126 double[] modelAnisoB = new double[6];
127
128
129
130
131
132
133
134 public DiffractionRefinementData(
135 CompositeConfiguration properties, ReflectionList reflectionList) {
136
137 int rflag = -1;
138 if (properties != null) {
139 rflag = properties.getInt("rfree-flag", -1);
140 fSigFCutoff = properties.getDouble("f-sigf-cutoff", -1.0);
141 } else {
142 fSigFCutoff = -1.0;
143 }
144
145 this.n = reflectionList.hklList.size();
146 this.nScale = reflectionList.crystal.scaleN;
147 this.rFreeFlag = rflag;
148 this.nBins = reflectionList.nBins;
149 fSigF = new double[n][2];
150 freeR = new int[n];
151 fc = new double[n][2];
152 fs = new double[n][2];
153 fcTot = new double[n][2];
154 fomPhi = new double[n][2];
155 foFc2 = new double[n][2];
156 foFc1 = new double[n][2];
157 dFc = new double[n][2];
158 dFs = new double[n][2];
159
160 for (int i = 0; i < n; i++) {
161 fSigF[i][0] = fSigF[i][1] = Double.NaN;
162 fcTot[i][0] = fcTot[i][1] = Double.NaN;
163 }
164
165 spline = new double[nBins * 2];
166 sigmaA = new double[nBins];
167 sigmaW = new double[nBins];
168 esqFc = new double[nBins];
169 esqFo = new double[nBins];
170 for (int i = 0; i < nBins; i++) {
171 spline[i] = spline[i + nBins] = sigmaA[i] = esqFc[i] = esqFo[i] = 1.0;
172 sigmaW[i] = 0.05;
173 }
174
175
176 bulkSolventK = 0.33;
177 bulkSolventUeq = b2u(50.0);
178 modelScaleK = 0.0;
179 }
180
181
182
183
184
185
186
187 public double FoFc2F(int i) {
188 ComplexNumber c = new ComplexNumber(foFc2[i][0], foFc2[i][1]);
189 return c.abs();
190 }
191
192
193
194
195
196
197
198 public double FoFc2Phi(int i) {
199 ComplexNumber c = new ComplexNumber(foFc2[i][0], foFc2[i][1]);
200 return c.phase();
201 }
202
203
204
205
206
207
208
209 public double fcF(int i) {
210 ComplexNumber c = new ComplexNumber(fc[i][0], fc[i][1]);
211 return c.abs();
212 }
213
214
215
216
217
218
219
220 public double fcPhi(int i) {
221 ComplexNumber c = new ComplexNumber(fc[i][0], fc[i][1]);
222 return c.phase();
223 }
224
225
226
227
228
229
230
231 public double fcTotF(int i) {
232 ComplexNumber c = new ComplexNumber(fcTot[i][0], fcTot[i][1]);
233 return c.abs();
234 }
235
236
237
238
239
240
241
242 public double fcTotPhi(int i) {
243 ComplexNumber c = new ComplexNumber(fcTot[i][0], fcTot[i][1]);
244 return c.phase();
245 }
246
247
248
249
250
251
252
253 public double foFc1F(int i) {
254 ComplexNumber c = new ComplexNumber(foFc1[i][0], foFc1[i][1]);
255 return c.abs();
256 }
257
258
259
260
261
262
263
264 public double foFc1Phi(int i) {
265 ComplexNumber c = new ComplexNumber(foFc1[i][0], foFc1[i][1]);
266 return c.phase();
267 }
268
269
270
271
272
273
274
275 public double fsF(int i) {
276 ComplexNumber c = new ComplexNumber(fs[i][0], fs[i][1]);
277 return c.abs();
278 }
279
280
281
282
283
284
285
286 public double fsPhi(int i) {
287 ComplexNumber c = new ComplexNumber(fs[i][0], fs[i][1]);
288 return c.phase();
289 }
290
291
292
293
294
295
296 public void generateFsigFfromAnomalousFsigF(double[][] anomalousFsigF) {
297 for (int i = 0; i < n; i++) {
298 if (isNaN(anomalousFsigF[i][0]) && isNaN(anomalousFsigF[i][2])) {
299
300 } else if (isNaN(anomalousFsigF[i][0])) {
301 fSigF[i][0] = anomalousFsigF[i][2];
302 fSigF[i][1] = anomalousFsigF[i][3];
303 } else if (isNaN(anomalousFsigF[i][2])) {
304 fSigF[i][0] = anomalousFsigF[i][0];
305 fSigF[i][1] = anomalousFsigF[i][1];
306 } else {
307 fSigF[i][0] = (anomalousFsigF[i][0] + anomalousFsigF[i][2]) / 2.0;
308 fSigF[i][1] =
309 sqrt(
310 anomalousFsigF[i][1] * anomalousFsigF[i][1]
311 + anomalousFsigF[i][3] * anomalousFsigF[i][3]);
312 }
313 }
314 }
315
316
317 public void generateRFree() {
318 Random generator = new Random();
319 int free;
320 int nonfree;
321 int nfree = 0;
322
323 if (rFreeFlag == 0) {
324 free = 0;
325 nonfree = 1;
326 } else {
327 free = 1;
328 nonfree = 0;
329 }
330
331 for (int i = 0; i < n; i++) {
332 if (isNaN(fSigF[i][0])) {
333 freeR[i] = nonfree;
334 continue;
335 }
336
337 int randomi = generator.nextInt(100);
338 if (randomi < 5) {
339 freeR[i] = free;
340 nfree++;
341 } else {
342 freeR[i] = nonfree;
343 }
344 }
345
346 if (logger.isLoggable(Level.INFO)) {
347 logger.info(format(" Assigned %d of %d reflections to the R free set (5%%).\n", nfree, n));
348 }
349 }
350
351
352
353
354
355
356
357 public double getF(int i) {
358 return fSigF[i][0];
359 }
360
361
362
363
364
365
366
367 public double[] getFSigF(int i) {
368 return fSigF[i];
369 }
370
371
372
373
374
375
376
377 public ComplexNumber getFcTot(int i) {
378 return new ComplexNumber(fcTot[i][0], fcTot[i][1]);
379 }
380
381
382
383
384
385
386
387 public ComplexNumber getFoFc1(int i) {
388 return new ComplexNumber(foFc1[i][0], foFc1[i][1]);
389 }
390
391
392
393
394
395
396
397 public void getFoFc1IP(int i, ComplexNumber c) {
398 c.re(foFc1[i][0]);
399 c.im(foFc1[i][1]);
400 }
401
402
403
404
405
406
407
408 public ComplexNumber getFoFc2(int i) {
409 return new ComplexNumber(foFc2[i][0], foFc2[i][1]);
410 }
411
412
413
414
415
416
417
418 public void getFoFc2IP(int i, ComplexNumber c) {
419 c.re(foFc2[i][0]);
420 c.im(foFc2[i][1]);
421 }
422
423
424
425
426
427
428
429 public int getFreeR(int i) {
430 return freeR[i];
431 }
432
433
434
435
436
437
438
439 public ComplexNumber getFs(int i) {
440 return new ComplexNumber(fs[i][0], fs[i][1]);
441 }
442
443
444
445
446
447
448
449 public double getSigF(int i) {
450 return fSigF[i][1];
451 }
452
453
454
455
456
457
458 public void intensitiesToAmplitudes() {
459 double tmp;
460
461 for (int i = 0; i < n; i++) {
462 if (fSigF[i][0] > 0.0) {
463 tmp = fSigF[i][0];
464 fSigF[i][0] = sqrt(tmp);
465 if (fSigF[i][1] < tmp) {
466 fSigF[i][1] = fSigF[i][0] - sqrt(tmp - fSigF[i][1]);
467 } else {
468 fSigF[i][1] = fSigF[i][0];
469 }
470 } else if (!isNaN(fSigF[i][0])) {
471 fSigF[i][0] = 0.0;
472 fSigF[i][1] = 0.0;
473 }
474 }
475
476 if (logger.isLoggable(Level.WARNING)) {
477 StringBuilder sb = new StringBuilder();
478 sb.append("\n Internally converting intensities to amplitudes.\n");
479 sb.append(" This does not use French & Wilson scaling.\n");
480 logger.info(sb.toString());
481 }
482 }
483
484
485
486
487
488
489
490
491 public boolean isFreeR(int i, int f) {
492 return (freeR[i] == f);
493 }
494
495
496
497
498
499
500 public double likelihoodFree() {
501 return llkF;
502 }
503
504
505
506
507
508
509 public double likelihoodWork() {
510 return llkR;
511 }
512
513
514
515
516
517
518
519 public void setF(int i, double f) {
520 fSigF[i][0] = f;
521 }
522
523
524
525
526
527
528
529
530 public void setFSigF(int i, double f, double sigf) {
531 fSigF[i][0] = f;
532 fSigF[i][1] = sigf;
533 }
534
535
536
537
538
539
540
541 public void setFc(int i, ComplexNumber c) {
542 fc[i][0] = c.re();
543 fc[i][1] = c.im();
544 }
545
546
547
548
549
550
551
552 public void setFcTot(int i, ComplexNumber c) {
553 fcTot[i][0] = c.re();
554 fcTot[i][1] = c.im();
555 }
556
557
558
559
560
561
562
563 public void setFoFc1(int i, ComplexNumber c) {
564 foFc1[i][0] = c.re();
565 foFc1[i][1] = c.im();
566 }
567
568
569
570
571
572
573
574 public void setFoFc2(int i, ComplexNumber c) {
575 foFc2[i][0] = c.re();
576 foFc2[i][1] = c.im();
577 }
578
579
580
581
582
583
584
585 public void setFreeR(int i, int f) {
586 freeR[i] = f;
587 }
588
589
590
591
592
593
594 public void setFreeRFlag(int i) {
595 rFreeFlag = i;
596 }
597
598
599
600
601
602
603
604 public void setFs(int i, ComplexNumber c) {
605 fs[i][0] = c.re();
606 fs[i][1] = c.im();
607 }
608
609
610
611
612
613
614
615 public void setSigF(int i, double sigF) {
616 fSigF[i][1] = sigF;
617 }
618
619
620
621
622
623
624 void setCrystalReciprocalSpaceFc(CrystalReciprocalSpace crystalReciprocalSpace) {
625 this.crystalReciprocalSpaceFc = crystalReciprocalSpace;
626 }
627
628
629
630
631
632
633 void setCrystalReciprocalSpaceFs(CrystalReciprocalSpace crystalReciprocalSpace) {
634 this.crystalReciprocalSpaceFs = crystalReciprocalSpace;
635 }
636
637
638
639
640
641
642
643 boolean isFreeR(int i) {
644 return (freeR[i] == rFreeFlag);
645 }
646
647
648
649
650
651
652
653 ComplexNumber getFc(int i) {
654 return new ComplexNumber(fc[i][0], fc[i][1]);
655 }
656
657
658
659
660
661
662
663 void getFcIP(int i, ComplexNumber c) {
664 c.re(fc[i][0]);
665 c.im(fc[i][1]);
666 }
667
668
669
670
671
672
673
674 void getFsIP(int i, ComplexNumber c) {
675 c.re(fs[i][0]);
676 c.im(fs[i][1]);
677 }
678
679
680
681
682
683
684
685 void getFcTotIP(int i, ComplexNumber c) {
686 c.re(fcTot[i][0]);
687 c.im(fcTot[i][1]);
688 }
689 }