-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhistograms.cpp
More file actions
1638 lines (1539 loc) · 56.8 KB
/
Copy pathhistograms.cpp
File metadata and controls
1638 lines (1539 loc) · 56.8 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*IMa3 2018 Jody Hey, Rasmus Nielsen, Sang Chul Choi, Vitor Sousa, Janeen Pisciotta, Yujin Chung and Arun Sethuraman */
#undef GLOBVARS
#include "ima.hpp"
struct histprintstructure
{
char str[PARAMSTRLEN];
struct plotpoint *xy;
double yscaleadjust;
double xscaleadjust;
int before;
int after;
};
static struct histprintstructure *hp;
static double scaleumean, timeumean;
static double *smthmaxvals;
static double *qpriorsmthmaxvals,*mpriorsmthmaxvals;
static int smthmax_firstu; /* 7/27/2012 JH added this to fix a bug in the calculation of the geometric mean of mutation scalars for a subset of the loci */
static int numstepsrecorded;
static char histfstr[40];
static struct plotpoint **popmigxy;
void writepriorfile(char priorfilename[],double *popsizepriorvals, double *mpriorvals);
/******* LOCAL FUNCTIONS ***********/
static char *histformatdouble (double pval);
static void fillvec (void); //fills up the marginal distribution estimates
static double multi_t_prior_func (double x, double y, double tmax, int ti,
int numt);
//static void writehistogram (FILE * outfile, int numhistprint);
static void writehistogram (FILE * outfile, int numhistprint, int dosmooth, const char * histtitle);
static int getdemogscale (double scaleumeaninput);
static void prepare_splittime_and_mutation_rate_histograms (int
*numhistprint);
static void prepare_parameter_histograms (int *numhistprint);
void prepare_demographic_scale_histograms (int *numhistprint,
double generationtime);
static void prepare_tmrca_histograms (int *numhistprint);
static void prepare_prior_histograms (int *numhistprint);
static void print_tprior_divide_histograms (FILE * outfile,
int *numhistprint);
static void free_print_histogram (void);
static void init_print_histogram (int maxnumhist);
static void prepare_migration_histograms (int locusrow, int nummigdirs);
void copysmthmaxvals(void);
double get_cdf_percentile_value(struct plotpoint *xy,int n, double p);
void print_prior_posterior_suggestions(FILE * outfile, long int mcmcrecords,char priorfilename[]);
#ifdef XMLOUTPUT
extern std::stack<TiXmlElement*> xstack;
extern TiXmlDocument global_doc;
#endif
extern char *outfilename;
char *
histformatdouble (double pval)
{
histfstr[0] = '\0';
if (pval < -1e9 || pval > 1e9)
{
sprintf (histfstr, "%-9.0lg", pval);
return &histfstr[0];
}
if (fabs (pval) < 1e-4)
{
sprintf (histfstr, "%-9.8lf", pval);
if (strcmp("0.00000000",histfstr)==0)
strcpy(histfstr,"0.0");
}
else if (fabs (pval) < 1e-3)
sprintf (histfstr, "%-9.7lf", pval);
else if (fabs (pval) < 1e-2)
sprintf (histfstr, "%-9.6lf", pval);
else if (fabs (pval) < 1e-1)
sprintf (histfstr, "%-9.5lf", pval);
else if (fabs (pval) < 1e-0)
sprintf (histfstr, "%-9.4lf", pval);
else if (fabs (pval) < 1e1)
sprintf (histfstr, "%-9.3lf", pval);
else if (fabs (pval) < 1e2)
sprintf (histfstr, "%-9.2lf", pval);
else if (fabs (pval) < 1e3)
sprintf (histfstr, "%-9.1lf", pval);
else
sprintf (histfstr, "%-9.0lf", pval);
return &histfstr[0];
} //histformatdouble
void
fillvec (void)
{
int i, j, p;
struct priorvalues holdprior;
for (j = 0; j < GRIDSIZE; j++)
{
for (i = 0, p = 0; i < numpopsizeparams; i++, p++)
{
C[ARBCHAIN]->itheta[i].xy[j].y = margincalc ((double) C[ARBCHAIN]->itheta[i].xy[j].x, 0.0, p, 0);
}
for (i = 0; i < nummigrateparams; i++, p++)
{
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR]) // need to set the priors based on hyperpriors, and make them the same for migration parameters
{
if (modeloptions[EXPOMIGRATIONPRIOR]==0)
{
holdprior = C[ARBCHAIN]->imig[i].pr;
C[ARBCHAIN]->imig[i].pr.max = hyperprior_uniform_m_max;
}
else
{
holdprior = C[ARBCHAIN]->imig[i].pr;
C[ARBCHAIN]->imig[i].pr.expomean = hyperprior_expo_m_mean;
C[ARBCHAIN]->imig[i].pr.max = EXPOMIGPLOTSCALE * hyperprior_expo_m_mean;
C[ARBCHAIN]->imig[i].pr.min = 0.0;
}
}
if (C[ARBCHAIN]->imig[i].pr.max > MPRIORMIN)
{
C[ARBCHAIN]->imig[i].xy[j].y = margincalc ((double) C[ARBCHAIN]->imig[i].xy[j].x, 0.0, p, 0);
}
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR]) // reset priors back to what they were
{
if (modeloptions[EXPOMIGRATIONPRIOR]==0)
{
C[ARBCHAIN]->imig[i].pr = holdprior;
}
else
{
C[ARBCHAIN]->imig[i].pr = holdprior;
}
}
}
}
} /* fillvec */
double
multi_t_prior_func (double x, double y, double tmax, int ti, int numt)
{
double priorprob;
priorprob =
exp (logfact[numt] - logfact[ti] - logfact[numt - ti - 1] +
(numt - ti - 1) * log (tmax - x) + ti * log (x) - numt * log (tmax));
if (priorprob < 0)
IM_err(IMERR_MULTITPRIOR,"beta distribution calculation for prior doesn't make sense %lf",priorprob);
return y / priorprob;
} // multi_t_prior_func
/* notes on writehistogram()
Loop thru numparamsh
Main loop:
Print summaries:
string
Minbin lowest x bin with nonzero y val
Maxbin highest x bin with nonzero y val
HiPt x bin with highest y val
also while identifying HiPt, calculate the sum of x, sum of y and sum of x*y
HiSmth - x bin with highest y val on a smoothed curve
also, if mode == 0 identify the peak and save this in uscaleml[] to be used for when mode==1
Mean calculated using x sum and y sum
95Lo calculate lower 95% conf limit
95Hi calculate higher 95% conf limit
Build a sorted list of smoothed probablities use to calculate HPD90Lo and HPD90Hi
HPD90Lo
HPD90Hi
Print Histograms
print row of parameter strings and 'P'
print row of max values 'HiPt'
print GRIDSIZE rows of x and y values
for y values, multiply them by denscale[]
print before and after values, and sum of likelihoods
Free all the pointers that were set up at the beginning
*/
void
writehistogram (FILE * outfile, int numhistprint, int dosmooth, const char * histtitle)
{
double *xysum;
double *ysum;
double *xsum;
double *hpdlo, *hpdhi;
char *hpdchar1,*hpdchar2;
double *smthprobvals;
int i, j, k, imax;
double maxval;
double sum, smoothsum, smoothdenom, smoothterm, tempsum;
int cellnum, smoothcellnum = 10;
double hpdmax, hpdmin;
struct hlists hlist[GRIDSIZE];
// can set to 0.95 or 0.9 or whatever
char hpdstr[3] = "95"; // c++ will append null terminator during init
double hpdcutoff = 0.95;
double hpdboundarycheck = 0.05; // use to see if probability at peak of curve is much higher than probability at boundaries
double vminhold;
xysum = static_cast<double *>
(calloc ((size_t) numhistprint, sizeof (double)));
xsum = static_cast<double *>
(calloc ((size_t) numhistprint, sizeof (double)));
ysum = static_cast<double *>
(calloc ((size_t) numhistprint, sizeof (double)));
hpdlo = static_cast<double *>
(calloc ((size_t) numhistprint, sizeof (double)));
hpdhi = static_cast<double *>
(calloc ((size_t) numhistprint, sizeof (double)));
hpdchar1 = static_cast<char *>
(calloc ((size_t) numhistprint + 1, sizeof (char)));
hpdchar2 = static_cast<char *>
(calloc ((size_t) numhistprint + 1, sizeof (char)));
/* CR: 110512.1
* Change malloc to calloc so that memory would be initialilzed.
* before being used.
*/
smthprobvals = static_cast<double *>
(calloc ((size_t) numhistprint, sizeof (double)));
#ifdef XMLOUTPUT
TiXmlElement *histogram = new TiXmlElement("Histogram");
histogram->SetAttribute("title",histtitle);
TiXmlElement *data = new TiXmlElement("Data");
TiXmlElement *vars = new TiXmlElement("Variables");
histogram->LinkEndChild(vars);
histogram->LinkEndChild(data);
xstack.top()->LinkEndChild(histogram);
#endif
std::stringstream s("");
FP " Summaries\n\tValue ");
for (j = 0; j < numhistprint; j++)
{
FP "\t %s", hp[j].str);
s << hp[j].str << " ";
}
#ifdef XMLOUTPUT
TiXmlText *vt = new TiXmlText(s.str().c_str());
vars->LinkEndChild(vt);
#endif
s.str("");
FP "\n\tMinbin ");
for (j = 0; j < numhistprint; j++)
{
i = 0;
while (i < GRIDSIZE - 1 && hp[j].yscaleadjust * hp[j].xy[i].y <= 0)
{
i++;
}
FP "\t%s", histformatdouble (hp[j].xscaleadjust * hp[j].xy[i].x));
}
FP "\n\tMaxbin");
for (j = 0; j < numhistprint; j++)
{
i = GRIDSIZE - 1;
while (i > 0 && hp[j].yscaleadjust * hp[j].xy[i].y <= 0)
{
i--;
}
FP "\t%s", histformatdouble (hp[j].xscaleadjust * hp[j].xy[i].x));
}
FP "\n\tHiPt ");
for (j = 0; j < numhistprint; j++)
{
xysum[j] = 0;
xsum[j] = 0;
maxval = -1;
imax = 0;
for (i = 0; i < GRIDSIZE; i++)
{
xysum[j] += hp[j].xscaleadjust * hp[j].xy[i].x * hp[j].yscaleadjust * hp[j].xy[i].y;
xsum[j] += hp[j].xscaleadjust * hp[j].xy[i].x;
ysum[j] += hp[j].yscaleadjust * hp[j].xy[i].y;
if (maxval < hp[j].yscaleadjust * hp[j].xy[i].y)
{
maxval = hp[j].yscaleadjust * hp[j].xy[i].y;
imax = i;
}
}
FP "\t%s", histformatdouble (hp[j].xscaleadjust * hp[j].xy[imax].x));
}
if (dosmooth)
{
FP "\n\tHiSmth");
for (j = 0; j < numhistprint; j++)
{
maxval = -1;
imax = 0;
i = 0;
for (; i < GRIDSIZE; i++)
{
cellnum = IMIN (smoothcellnum, 2 * i);
cellnum = IMIN (cellnum, 2 * (GRIDSIZE - 1 - i));
k = IMAX (0, i - (cellnum / 2));
smoothdenom = 0;
smoothsum = 0;
for (; k <= IMIN (GRIDSIZE - 1, i + (cellnum / 2)); k++)
{
smoothterm = 1.0 / (0.5 + abs (k - i));
smoothsum += hp[j].xy[k].y * smoothterm;
smoothdenom += smoothterm;
}
smoothsum /= smoothdenom;
if (maxval < smoothsum)
{
maxval = smoothsum;
smthprobvals[j] = maxval;
imax = i;
}
}
smthmaxvals[j] = hp[j].xscaleadjust * hp[j].xy[imax].x;
FP "\t%s", histformatdouble (smthmaxvals[j]));
}
}
FP "\n\tMean ");
for (j = 0; j < numhistprint; j++)
{
FP "\t%s", histformatdouble (xysum[j] / ysum[j]));
}
FP "\n\t95%%Lo ");
for (j = 0; j < numhistprint; j++)
{
i = 0;
sum = 0;
while ( i < GRIDSIZE &&(sum + hp[j].yscaleadjust * hp[j].xy[i].y) / ysum[j] <= 0.025)
{
sum += hp[j].yscaleadjust * hp[j].xy[i].y;
i++;
}
FP "\t%s", histformatdouble (hp[j].xscaleadjust * hp[j].xy[i].x));
}
FP "\n\t95%%Hi ");
for (j = 0; j < numhistprint; j++)
{
i = GRIDSIZE - 1;
sum = 0;
while (i > 0 && (sum + hp[j].yscaleadjust * hp[j].xy[i].y) / ysum[j] <= 0.025)
{
sum += hp[j].yscaleadjust * hp[j].xy[i].y;
i--;
}
FP "\t%s", histformatdouble (hp[j].xscaleadjust * hp[j].xy[i].x));
}
/* print out Highest Posterior Density intervals - first, smooth the curve
and make a copy of the curve in hlist */
smoothcellnum = 30;
for (j = 0; j < numhistprint; j++)
{
hpdchar1[j] = ' ';
hpdchar2[j] = ' ';
maxval = -1;
i = 0;
tempsum = 0;
for (; i < GRIDSIZE; i++)
{
cellnum = IMIN (smoothcellnum, 2 * i);
cellnum = IMIN (cellnum, 2 * (GRIDSIZE - 1 - i));
k = IMAX (0, i - (cellnum / 2));
smoothdenom = 0;
smoothsum = 0;
for (; k <= IMIN (GRIDSIZE - 1, i + (cellnum / 2)); k++)
{
smoothterm = 1.0 / (0.5 + abs (k - i));
smoothsum += hp[j].xy[k].y * smoothterm;
smoothdenom += smoothterm;
}
hlist[i].v = hp[j].xscaleadjust * hp[j].xy[i].x;
smoothsum /= smoothdenom;
tempsum += smoothsum;
hlist[i].p = smoothsum;
}
vminhold = hlist[0].v;
/* short hlist by probability from low to high
move up the list from the bottom and accumulate a sum
until the sum = hpdcutoff of the total.
while moving up the list, find the values associated with the
probability that pushes the sum over hpdcutoff
*/
shellhist (&(hlist[0]), GRIDSIZE);
sum = 0;
hpdmax = -1;
hpdmin = 1e10;
i = GRIDSIZE - 1;
sum = hlist[i].p;
while (i >= 0 && sum <= (hpdcutoff * tempsum))
{
if (i > 0 && hlist[i].p < hlist[i - 1].p)
IM_err(IMERR_HPD95,"problem calculating HPD interval",i,hlist[i].p, hlist[i-1].p);
if (hlist[i].v > hpdmax)
{
hpdmax = hlist[i].v;
}
if (hlist[i].v < hpdmin)
{
hpdmin = hlist[i].v;
}
i--;
sum += hlist[i].p;
}
// set the lower bound to zero if the found lower bound is at the minimum possible value of the hpd histogram
// this will break if we start using priors with nonzero lower bounds
if (hpdmin <= vminhold)
hpdlo[j] = 0.0;
else
hpdlo[j] = hpdmin;
hpdhi[j] = hpdmax;
while (i > 0 && (hlist[i].v < hpdmin || hlist[i].v > hpdmax))
i--;
if (i > 0)
{
hpdchar2[j] = '?';
}
if ((smthprobvals[j] * hpdboundarycheck < hp[j].xy[0].y) && (smthprobvals[j] * hpdboundarycheck < hp[j].xy[GRIDSIZE-1].y))
{
hpdchar1[j] = '#';
}
}
FP "\n\tHPD%sLo",hpdstr);
for (j = 0; j < numhistprint; j++)
{
FP "\t%s%c%c", histformatdouble (hpdlo[j]), hpdchar1[j], hpdchar2[j]);
}
FP "\n\tHPD%sHi",hpdstr);
for (j = 0; j < numhistprint; j++)
{
FP "\t%s%c%c", histformatdouble (hpdhi[j]), hpdchar1[j],hpdchar2[j]);
}
FP "\n");
FP "\n");
FP "\tParameter");
for (j = 0; j < numhistprint; j++)
FP "\t%s\tP", hp[j].str);
FP "\n\tHiPt");
for (j = 0; j < numhistprint; j++)
{
maxval = -1;
imax = 0;
for (i = 0; i < GRIDSIZE; i++)
{
if (maxval < hp[j].yscaleadjust * hp[j].xy[i].y)
{
maxval = hp[j].yscaleadjust * hp[j].xy[i].y;
imax = i;
}
}
FP "\t%s", histformatdouble (hp[j].xscaleadjust * hp[j].xy[imax].x));
FP "\t%s", histformatdouble (hp[j].yscaleadjust * hp[j].xy[imax].y));
}
FP "\n\n");
for (i = 0; i < GRIDSIZE; i++)
{
s.str("");
FP "\t%4d", i);
for (j = 0; j < numhistprint; j++)
{
char * hfd1 = histformatdouble (hp[j].xscaleadjust * hp[j].xy[i].x);
FP "\t%s", hfd1);
s << hfd1 << " ";
char * hfd2 = histformatdouble (hp[j].yscaleadjust * hp[j].xy[i].y);
FP "\t%s", hfd2);
s << hfd2 << " ";
}
FP "\n");
#ifdef XMLOUTPUT
TiXmlElement *row = new TiXmlElement("Row");
TiXmlText *rt = new TiXmlText(s.str().c_str());
row->LinkEndChild(rt);
data->LinkEndChild(row);
#endif
}
FP " SumP\t");
for (j = 0; j < numhistprint; j++)
FP "\t\t%s", histformatdouble (ysum[j]));
FP "\n");
FP " Before\t");
for (j = 0; j < numhistprint; j++)
FP "\t\t%s", histformatdouble (hp[j].before * hp[j].yscaleadjust));
FP "\n");
FP " After\t");
for (j = 0; j < numhistprint; j++)
FP "\t\t%s", histformatdouble (hp[j].after * hp[j].yscaleadjust));
FP "\n");
XFREE (xysum);
XFREE (xsum);
XFREE (ysum);
XFREE (hpdlo);
XFREE (hpdhi);
XFREE (hpdchar1);
XFREE (hpdchar2);
XFREE (smthprobvals);
return;
} /* writehistogram */
int getdemogscale (double scaleumeaninput)
{
int i, cui, ui, li;
if (runoptions[LOADRUN])
{
if (scaleumeaninput <= 0)
{
return 0;
}
else
{
scaleumean = scaleumeaninput;
timeumean = 0;
for (li = 0, cui = 0; li < nloci; li++)
for (i = 0; i < L[li].nlinked; i++)
if (L[li].uperyear_vals[i] > 0)
{
timeumean += log (L[li].uperyear_vals[i]);
cui++;
}
assert (cui > 0);
timeumean = exp (timeumean / cui);
}
}
else
{
scaleumean = 0;
timeumean = 0;
for (li = 0, ui = smthmax_firstu , cui = 0; li < nloci; li++) /* 7/27/2012 JH replaced ui = 0 with ui = smthmax_firstu. This to fix a bug in the calculation of the geometric mean of mutation scalars for a subset of the loci */
for (i = 0; i < L[li].nlinked; i++, ui++)
{
if (L[li].uperyear_vals[i] > 0)
{
timeumean += log (L[li].uperyear_vals[i]);
if (nurates > 1)
scaleumean += log (smthmaxvals[ui]);
cui++;
}
}
timeumean = exp (timeumean / cui);
if (scaleumeaninput > 0)
{
scaleumean = scaleumeaninput;
}
else
{
assert (cui);
scaleumean = exp (scaleumean / cui);
}
}
return cui;
} //getdemogscale
void prepare_splittime_and_mutation_rate_histograms (int *numhistprint)
{
int i, ui, li;
for (i = 0, *numhistprint = 0; i < numsplittimes; i++, (*numhistprint)++)
{
strcpy (hp[*numhistprint].str, T[i].str);
hp[*numhistprint].xy = T[i].v->xy;
/* use full range, assumming minimum t is zero, for this purpose */
hp[*numhistprint].yscaleadjust = (GRIDSIZE / (T[i].pr.max)) / numstepsrecorded;
hp[*numhistprint].xscaleadjust = 1;
hp[*numhistprint].before = T[i].v->beforemin;
hp[*numhistprint].after = T[i].v->aftermax;
}
if (runoptions[LOADRUN] == 0 && (nurates > 1))
{
smthmax_firstu = *numhistprint; /* 7/27/2012 JH added this to fix a bug in the calculation of the geometric mean of mutation scalars for a subset of the loci */
for (li = 0; li < nloci; li++)
for (ui = 0; ui < L[li].nlinked; ui++)
{
assert (ui < L[li].nlinked);
strcpy (hp[*numhistprint].str, L[li].u_rec[ui].str);
hp[*numhistprint].xy = L[li].u_rec[ui].v->xy;
hp[*numhistprint].yscaleadjust = (GRIDSIZE / (exp (L[li].u_rec[ui].pr.max) - exp (L[li].u_rec[ui].pr.min))) / numstepsrecorded;
hp[*numhistprint].before = L[li].u_rec[ui].v->beforemin;
hp[*numhistprint].after = L[li].u_rec[ui].v->aftermax;
hp[*numhistprint].xscaleadjust = 1;
(*numhistprint)++;
}
for (li = 0; li < nloci; li++)
for (ui = 0; ui < L[li].nlinked; ui++)
if (L[li].umodel[0] == HKY)
{
assert (ui < L[li].nlinked);
strcpy (hp[*numhistprint].str, L[li].kappa_rec->str);
hp[*numhistprint].xy = L[li].kappa_rec->v->xy;
hp[*numhistprint].yscaleadjust = (GRIDSIZE / (L[li].kappa_rec->pr.max - L[li].kappa_rec->pr.min)) / numstepsrecorded;
hp[*numhistprint].before = L[li].kappa_rec->v->beforemin;
hp[*numhistprint].after = L[li].kappa_rec->v->aftermax;
hp[*numhistprint].xscaleadjust = 1;
(*numhistprint)++;
}
}
} // prepare_splittime_and_mutation_rate_histograms(int *numhistprint)
/* use C[ARBCHAIN]
all chains have itheta and imig and each element of each of these arrays has xy
we need one to use for printing, and it does not matter because the curves will be based on saved genealogies
*/
void prepare_parameter_histograms (int *numhistprint)
{
int i;
fillvec ();
*numhistprint = 0;
for (i = 0; i < numpopsizeparams; i++)
{
hp[*numhistprint].xy = C[ARBCHAIN]->itheta[i].xy;
strcpy (hp[*numhistprint].str, C[ARBCHAIN]->itheta[i].str);
hp[*numhistprint].yscaleadjust = 1;
hp[*numhistprint].xscaleadjust = 1;
hp[*numhistprint].before = 0;
hp[*numhistprint].after = 0;
(*numhistprint)++;
}
for (i = 0; i < nummigrateparams; i++)
if (C[ARBCHAIN]->imig[i].pr.max > MPRIORMIN)
{
hp[*numhistprint].xy = C[ARBCHAIN]->imig[i].xy;
strcpy (hp[*numhistprint].str, C[ARBCHAIN]->imig[i].str);
hp[*numhistprint].yscaleadjust = 1;
hp[*numhistprint].xscaleadjust = 1;
hp[*numhistprint].before = 0;
hp[*numhistprint].after = 0;
(*numhistprint)++;
}
} //void prepare_parameter_histograms(int *numhistprint);
void prepare_demographic_scale_histograms (int *numhistprint,
double generationtime)
{
int i;
for (i = 0, *numhistprint = 0; i < numsplittimes; i++, (*numhistprint)++)
{
strcpy (hp[*numhistprint].str, T[i].str);
hp[*numhistprint].xy = T[i].v->xy;
/* use full range, assumming minimum t is zero, for this purpose */
hp[*numhistprint].yscaleadjust = (GRIDSIZE / (T[i].pr.max)) / numstepsrecorded;
hp[*numhistprint].xscaleadjust = scaleumean / timeumean;
hp[*numhistprint].before = T[i].v->beforemin;
hp[*numhistprint].after = T[i].v->aftermax;
}
for (i = 0; i < numpopsizeparams; i++)
{
hp[*numhistprint].xy = C[ARBCHAIN]->itheta[i].xy;
strcpy (hp[*numhistprint].str, C[ARBCHAIN]->itheta[i].str);
hp[*numhistprint].yscaleadjust = 1;
hp[*numhistprint].xscaleadjust =
scaleumean / (4 * timeumean * generationtime);
hp[*numhistprint].before = 0;
hp[*numhistprint].after = 0;
(*numhistprint)++;
}
} //void prepare_demographic_scale_histograms(int *numhistprint);
void prepare_tmrca_histograms (int *numhistprint)
{
int li;
for (*numhistprint = 0, li = 0; li < nloci; li++)
{
hp[*numhistprint].xy = L[li].g_rec->v->xy;
strcpy (hp[*numhistprint].str, L[li].g_rec->v->str);
hp[*numhistprint].yscaleadjust = 1 / (double) numstepsrecorded;
hp[*numhistprint].xscaleadjust = 1;
hp[*numhistprint].before = 0;
hp[*numhistprint].after = 0;
(*numhistprint)++;
}
} //void prepare_tmrca_histograms(int *numhistprint);
void prepare_prior_histograms (int *numhistprint)
{
int i;
*numhistprint = 0;
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR])
for (i = 0; i < numpopsizeparams; i++)
{
hp[*numhistprint].xy = qh[i].v->xy;
strcpy (hp[*numhistprint].str, qh[i].v->str);
hp[*numhistprint].yscaleadjust = 1 / (double) numstepsrecorded;
hp[*numhistprint].xscaleadjust = 1;
hp[*numhistprint].before = 0;
hp[*numhistprint].after = 0;
(*numhistprint)++;
}
for ( i = 0; i < nummigrateparams; i++)
{
hp[*numhistprint].xy = mh[i].v->xy;
strcpy (hp[*numhistprint].str, mh[i].v->str);
hp[*numhistprint].yscaleadjust = 1 / (double) numstepsrecorded;
hp[*numhistprint].xscaleadjust = 1;
hp[*numhistprint].before = 0;
hp[*numhistprint].after = 0;
(*numhistprint)++;
}
} //void prepare_prior_histograms(int *numhistprint);
void print_tprior_divide_histograms (FILE * outfile, int *numhistprint)
{
struct plotpoint **t_prior_divide;
int i, j;
t_prior_divide = static_cast<plotpoint **> (malloc (numsplittimes * sizeof (struct plotpoint *)));
for (i = 0; i < numsplittimes; i++)
{
t_prior_divide[i] = static_cast<plotpoint *> (malloc (GRIDSIZE * sizeof (struct plotpoint)));
for (j = 0; j < GRIDSIZE; j++)
{
t_prior_divide[i][j].x = T[i].v->xy[j].x;
t_prior_divide[i][j].y =
multi_t_prior_func (T[i].v->xy[j].x, T[i].v->xy[j].y,
T[i].pr.max, i, numsplittimes);
}
}
for (i = 0, *numhistprint = 0; i < numsplittimes; i++, (*numhistprint)++)
{
strcpy (hp[*numhistprint].str, T[i].str);
hp[*numhistprint].xy = t_prior_divide[i];
/* use full range, assumming minimum t is zero, for this purpose */
hp[*numhistprint].yscaleadjust = (GRIDSIZE / (T[i].pr.max)) / numstepsrecorded;
hp[*numhistprint].xscaleadjust = scaleumean / timeumean;;
hp[*numhistprint].before = T[i].v->beforemin;
hp[*numhistprint].after = T[i].v->aftermax;
}
writehistogram (outfile, *numhistprint,1,"NA");
orig2d_free2D ((void **) t_prior_divide, numsplittimes);
} //void prepare_tprior_divide_histograms(int *numhistprint);
void print_populationmigrationrate_histograms (FILE * outfile,
int *numhistprint,
int prob_or_like)
{
int i, j, k, hpi, mpop, thetai, mi, found;
char tempstr[PARAMSTRLEN];
double pmmax, tempy, tempx, maxxfind;
popmigxy = (struct plotpoint **) malloc((size_t)(*numhistprint * sizeof (struct plotpoint *)));
for (i = 0; i <*numhistprint; i++)
popmigxy[i] = (struct plotpoint *) malloc ((size_t) (GRIDSIZE * sizeof (struct plotpoint)));
hpi = 0;
if (modeloptions[PARAMETERSBYPERIOD])
{
for (k = 0; k < lastperiodnumber; k++)
{
for (i = 0; i < npops - k; i++)
{
mpop = C[ARBCHAIN]->plist[k][i];
thetai = 0;
found = 0;
while (!found && thetai < numpopsizeparams)
{
found = (k == atoi (&C[ARBCHAIN]->itheta[thetai].str[1])
&& mpop == atoi (&C[ARBCHAIN]->itheta[thetai].str[3]));
if (!found)
thetai++;
}
assert (thetai < numpopsizeparams);
for (mi = 0; mi < nummigrateparams; mi++)
{
found = 0;
if (modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS])
{
found = (k == atoi (&C[ARBCHAIN]->imig[mi].str[1])
&& (mpop == atoi (&C[ARBCHAIN]->imig[mi].str[3])
|| mpop == atoi (&C[ARBCHAIN]->imig[mi].str[6])));
}
else
{
found = (k == atoi (&C[ARBCHAIN]->imig[mi].str[1])
&& mpop == atoi (&C[ARBCHAIN]->imig[mi].str[3]));
}
if (found)
{
sprintf (tempstr, "%d,2N%d", k, mpop);
if (modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS])
{
strcat (tempstr, "m");
strcat (tempstr, &C[ARBCHAIN]->imig[mi].str[3]);
}
else
{
strcat (tempstr, C[ARBCHAIN]->imig[mi].str);
}
strcpy (hp[hpi].str, tempstr);
if (modeloptions[EXPOMIGRATIONPRIOR])
{
pmmax = EXPOMIGPLOTSCALE * C[ARBCHAIN]->imig[mi].pr.expomean;
}
else
{
pmmax = C[ARBCHAIN]->itheta[thetai].pr.max * C[ARBCHAIN]->imig[mi].pr.max / 2.0;
}
j = GRIDSIZE-1;
maxxfind = 0;
while (maxxfind==0 && j >= 0)
{
tempx = (j + 0.5) * pmmax / GRIDSIZE;
if (modeloptions[EXPOMIGRATIONPRIOR])
tempy = calc_pop_expomig (thetai, mi,tempx , prob_or_like);
else
tempy = calc_popmig (thetai, mi,tempx , prob_or_like);
if (tempy > 1e-9)
maxxfind = tempx;
else
j--;
}
if (j < 0)
maxxfind = pmmax;
for (j = 0; j < GRIDSIZE; j++)
{
popmigxy[hpi][j].x = (j + 0.5) * maxxfind / GRIDSIZE;
if (modeloptions[EXPOMIGRATIONPRIOR])
{
popmigxy[hpi][j].y = 1;
}
else
{
popmigxy[hpi][j].y = calc_popmig (thetai, mi, popmigxy[hpi][j].x, prob_or_like);
}
}
hp[hpi].xy = popmigxy[hpi];
hp[hpi].xscaleadjust = hp[hpi].yscaleadjust = 1;
hp[hpi].before = 0;
hp[hpi].after = 0;
hpi++;
}
}
}
}
}
else
{
for (i = 0; i < numtreepops - 1; i++)
{
thetai = i;
for (mi = 0; mi < nummigrateparams; mi++)
{
if (modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS])
{
found = ((thetai == atoi (&C[ARBCHAIN]->imig[mi].str[1])) || (thetai == atoi (&C[ARBCHAIN]->imig[mi].str[4])));
}
else
{
found = thetai == atoi (&C[ARBCHAIN]->imig[mi].str[1]);
}
if (found)
{
sprintf (tempstr, "2N%d", thetai);
strcat (tempstr, C[ARBCHAIN]->imig[mi].str);
strcpy (hp[hpi].str, tempstr);
if (modeloptions[EXPOMIGRATIONPRIOR])
{
pmmax = EXPOMIGPLOTSCALE * C[ARBCHAIN]->imig[mi].pr.expomean;
}
else
{
pmmax = C[ARBCHAIN]->itheta[thetai].pr.max * C[ARBCHAIN]->imig[mi].pr.max / 2.0;
}
j = GRIDSIZE-1;
maxxfind = 0;
while (maxxfind==0 && j >= 0)
{
tempx = (j + 0.5) * pmmax / GRIDSIZE;
if (modeloptions[EXPOMIGRATIONPRIOR])
tempy = calc_pop_expomig (thetai, mi,tempx , prob_or_like);
else
tempy = calc_popmig (thetai, mi,tempx , prob_or_like);
if (tempy > 1e-9)
maxxfind = tempx;
else
j--;
}
if (j < 0)
maxxfind = pmmax;
for (j = 0; j < GRIDSIZE; j++)
{
popmigxy[hpi][j].x = (j + 0.5) * maxxfind / GRIDSIZE;
if (modeloptions[EXPOMIGRATIONPRIOR])
popmigxy[hpi][j].y = calc_pop_expomig (thetai, mi, popmigxy[hpi][j].x, prob_or_like);
else
popmigxy[hpi][j].y = calc_popmig (thetai, mi, popmigxy[hpi][j].x, prob_or_like);
}
hp[hpi].xy = popmigxy[hpi];
hp[hpi].xscaleadjust = hp[hpi].yscaleadjust = 1;
hp[hpi].before = 0;
hp[hpi].after = 0;
hpi++;
}
}
}
}
writehistogram (outfile, hpi,0,"NA");
orig2d_free2D ((void **) popmigxy, *numhistprint);
} //void print_populationmigrationrate_histograms
void prepare_migration_histograms (int locusrow, int nummigdirs)
{
int i;
/* 8/26/2011 */
for (i = 0; i < nummigdirs; i++)
{
hp[i].xy = migration_counts[locusrow][i].xy;
strcpy (hp[i].str, migration_counts[locusrow][i].str);
hp[i].yscaleadjust = 1 / (double) numstepsrecorded;
hp[i].xscaleadjust = 1;
hp[i].before = 0;
hp[i].after = 0;
}
/*
for (i = 0; i < 2 * nummigdirs; i++)
{
hp[i].xy = migration_counts_times[locusrow][i].xy;
strcpy (hp[i].str, migration_counts_times[locusrow][i].str);
hp[i].yscaleadjust = 1 / (double) numstepsrecorded;
hp[i].xscaleadjust = 1;
hp[i].before = 0;
hp[i].after = 0;
} */
} // prepare migration histograms
void free_print_histogram (void)
{
XFREE (hp);
XFREE (smthmaxvals);
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR] == 1)
{
XFREE(qpriorsmthmaxvals);
XFREE(mpriorsmthmaxvals);
}
} // free_print_histogram
void init_print_histogram (int maxnumhist)
{
hp = (struct histprintstructure *) malloc (maxnumhist * sizeof (struct histprintstructure));
smthmaxvals = static_cast<double *> (malloc (maxnumhist * sizeof (double)));
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR] == 1)
{
qpriorsmthmaxvals = static_cast<double *> (malloc (numpopsizeparams * sizeof (double)));
mpriorsmthmaxvals = static_cast<double *> (malloc (nummigrateparams * sizeof (double)));
}
} // init_print_histogram
/***** GLOBAL FUNCTIONS **********/
/* to print one or more histograms:
-----------------------------------
histograms are printing using
static struct histprintstructure *hp;
The details of stuct histprintstructure are given at the top of this file.
There are two main steps to printing a table with multiple histograms:
1)write a function to prepare *hp ( pointer to struct histprintstructure)
hp already exists, but where it points to needs to be set.
This function should also set the value of numhistprint, the number of histograms to print
e.g. prepare_myhistogram(&humhistprint)
2) make a call to writehistogram(), which should print whatever *hp is pointing at
For example:
write a function prepare_myhistogram(&humhistprint)
Add the following function calls to printhistograms():
prepare_myhistogram(&humhistprint)
writehistogram (outfile, numhistprint);
It is also helpful to precede these by some FP statements that explain the histograms.
*/
/* scaleumeaninput is the user provide (with -y) mean mutation rate. Used in L mode with -p3. If all loci are mutation rates in the data file, then use -y1*/
void printhistograms (FILE * outfile, long int mcmcrecords,
double generationtime, int usegenerationtimedefault, double scaleumeaninput,char priorfilename[])
{
int numhistprint = 0, uratecount; // 5/17/2017 uratecount usage unclear ??
int li, predictmcmchist = 0, predictparamhist = 0, numhist;
int numhistsets, histsetcount;
#ifdef XMLOUTPUT
//TiXmlDocument doc;
//TiXmlDeclaration *decl = new TiXmlDeclaration("1.0","","");
//doc.LinkEndChild(decl);
TiXmlElement *allhistograms = new TiXmlElement("AllHistograms");
//doc.LinkEndChild(allhistograms);
xstack.top()->LinkEndChild(allhistograms);