-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinitialize.cpp
More file actions
2504 lines (2341 loc) · 89.3 KB
/
Copy pathinitialize.cpp
File metadata and controls
2504 lines (2341 loc) · 89.3 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"
#include "update_gtree_common.hpp"
/* Most initializations */
/*********** LOCAL STUFF **********/
extern double pi[MAXLOCI][4];
// moved these down to within condition if (calcoptions[MUTATIONPRIORRANGE])
// should probably make them dynamic
//extern int urri[2 * MAXLOCI][2 * MAXLOCI]; // used mostly in update_mc_params.c
//extern double urrlow[2 * MAXLOCI][2 * MAXLOCI], urrhi[2 * MAXLOCI][2 * MAXLOCI]; // used mostly in update_mc_params.c
static double **uvals;
//static double geomeanvar; JH 4/14/2017 not used
static int **numsitesIS; // maxpops * maxloci temporarily holds the number of polymorphic sites in loci with infinite sites model
static double uval_preliminary_sum;
static int maxpossiblemigrateparams;
static int numpossiblepoppairstrings;
extern int numdistinctpopulationpairs[];
extern int hashvalmaxes[];
/* prototypes */
static int numvarHKY (int li, int b, int e);
static int checkaresis (int ci, int period, int i, int j);
static void imaAsnInitAssign (int ci);
static void set_nomigrationchecklist ();
static void setuinfo (double summut);
static double setup_uval (void);
void set_x (struct value_record *v, int isint);
void init_value_record (struct value_record *v, int isint);
static void init_g_rec (int li);
static void init_a_rec (int li);
static void init_lpgpd_v (void);
static void init_i_params (int ci);
static void getparamnums(void);
static void setup_iparams (int ci);
/* 8/26/2011 */
void fillm_times_rec(int j, int i, int pi, int pj);
static void init_migration_counts (void);
static void init_mutation_scalar_rec (int li);
static void fixmutationratescalars(void);
static void start_setup_L (char infilename[], int *fpstri, char fpstr[], int currentid);
static void add_priorinfo_to_output(char priorfilename[],int *fpstri, char fpstr[]);
static void start_setup_C (void);
static void finish_setup_C (int currentid);
static void set_tvalues (int ci);
static void setup_T ();
static void setup_migprior_recording();
static void setup_qprior_recording();
static void finish_setup_L (void);
static void reportparamcounts(int *fpstri, char fpstr[]);
/* changes made to include hidden genealogies */
static void start_setup_poptree(char *ps,char topologypriorinfostring[]);
static void finish_setup_poptree();
/* some extern prototypes */
extern void fillplist (int ci);
extern void fillancplist (int ci);
extern void init_holdgweight_array_for_hg(void);
/******** LOCAL FUNCTIONS ************/
int
numvarHKY (int li, int b, int e)
{
int i, j, tot = 0;
for (i = 0; i < L[li].numsites; i++)
{
j = b + 1;
while (j < e && L[li].seq[b][i] == L[li].seq[j][i])
j++;
if (j < L[li].numgenes)
tot++;
}
return tot;
}
int
checkaresis (int ci, int period, int i, int j) // returns 1 if populations i and j are both in period, and they are sister populations
{
int aresis, k;
aresis = 0;
if (ISELEMENT (C[ci]->plist[period][i], C[ci]->periodset[period])
&& ISELEMENT (C[ci]->plist[period][j], C[ci]->periodset[period]))
{
for (k = period + 1; k < npops; k++)
{
if ((C[ci]->droppops[k][0] == C[ci]->plist[period][i]
&& C[ci]->droppops[k][1] == C[ci]->plist[period][j])
|| (C[ci]->droppops[k][0] == C[ci]->plist[period][j]
&& C[ci]->droppops[k][1] == C[ci]->plist[period][i]))
aresis = 1;
}
} return aresis;
} //checkaresis
void
imaAsnInitAssign (int ci)
{
struct genealogy *G;
struct edge *gtree;
int j;
int k;
int li;
int pi;
G = NULL;
gtree = NULL;
for (li = 0; li < nloci; li++)
{
G = &(C[ci]->G[li]);
gtree = G->gtree;
j = 0;
k = 0;
for (pi = 0; pi < npops; pi++)
{
while (j < L[li].samppop[pi] + k)
{
gtree[j].pop = pi;
if (hiddenoptions[HIDDENGENEALOGY]==1)
gtree[j].pophg = pi;
j++;
}
k += L[li].samppop[pi];
}
}
return;
}
/*
length of the p and r arrays were messed up
no popsize or migration parameter can extend for more than npops-1 periods.
*/
void init_i_params (void)
{
int i, ci,n;
for (ci = 0;ci<numchainspp;ci++)
{
C[ci]->itheta = static_cast<i_param *>
(malloc (numpopsizeparams * sizeof (struct i_param)));
for (i = 0; i < numpopsizeparams; i++)
{
C[ci]->itheta[i].xy = static_cast<plotpoint *> (calloc (GRIDSIZE, sizeof (struct plotpoint)));
/* try just making these big enough, I think just need for each population which should be 2*npops - 1 */
C[ci]->itheta[i].wp.p = static_cast<int *> (malloc ((npops-1)* sizeof (int)));
//(malloc (2*npops* sizeof (int)));
C[ci]->itheta[i].wp.r = static_cast<int *> (malloc ((npops-1)* sizeof (int)));
//(malloc (2*npops * sizeof (int)));
}
C[ci]->imig = static_cast<i_param *>
(malloc (nummigrateparams * sizeof (struct i_param)));
/* initialize the parts of wp for imig*/
for (i = 0; i < nummigrateparams; i++) // allow for 2 in each, hope that's enough, confusing // changed to 3
{
if (modeloptions[ONEMIGRATIONPARAMETER]==1) // n must be big enough to hold
n = ((npops *npops - 1) * npops) /3; // sum of total number of migration types across periods (>= # of migration parameters)
else
n = (npops-1); //# periods
C[ci]->imig[i].xy = static_cast<plotpoint *>
(calloc (GRIDSIZE, sizeof (struct plotpoint)));
C[ci]->imig[i].wp.p = static_cast<int *>
(malloc (n * sizeof (int)));
C[ci]->imig[i].wp.r = static_cast<int *>
(malloc (n * sizeof (int)));
C[ci]->imig[i].wp.c = static_cast<int *>
(malloc (n * sizeof (int)));
}
}
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR])
{
// holdimig is used to hold copies of priors, way overkill as no other part of struct i_param is used as of 4/25/2018 JH
holdimig = static_cast<i_param *>
(malloc (nummigrateparams * sizeof (struct i_param)));
init_migration_prior_update();
}
}
/* initialize the splitting rate, population size and migration instances of struct i_param.
This function is complicated with lots of sections because of many user options */
void getparamnums()
{
int i, j, si,sj, k, mi, mcheck;
/* set up the population size parameters */
if (modeloptions[PARAMETERSBYPERIOD])
{
numpopsizeparams = (npops * (npops + 1)) / 2; // every population in every period gets a parameter
}
else
{
numpopsizeparams = numtreepops; // every distinct population gets a parameter
}
numpopsets = 1 << npops;//# of possible subsets of sampled populations, =2^npops, this includes null set and full set
if (!modeloptions[PARAMETERSBYPERIOD])
maxpossiblemigrateparams = 2*(npops-1)*(npops-1);
/*count how many migration parameters are needed, use mi
set up a standard sequence of checks that get repeated when building imig[]
there is a complex series of checks to ensure that the intended model is being followed
loop through periods
check order:
MIGRATIONBETWEENSAMPLED - migration only between sampled populations
PARAMETERSBYPERIOD - every population size and migration parameter applies for only 1 period
NOMIGBETWEENNONSISTERS - set migration to zero between non-sister populations
SINGLEMIGRATIONBOTHDIRECTIONS
if ONEMIGRATIONPARAMETER applies then there is only one migration rate, whenever there is migration in the model
*/
mi = 0;
if (!modeloptions[NOMIGRATION] && npops > 1)
{
if (modeloptions[ONEMIGRATIONPARAMETER])
{
mi = 1;
goto outsidefirstloop; // get out of this big condition section
}
/* each chain will have the same number of migration parameters, so C[ARBCHAIN] is ok */
if (calcoptions[LOADPRIORSFROMFILE] ==0)
{
for (k = 0; k < lastperiodnumber; k++)
for (i = 0; i < npops - k - 1; i++)
for (j = i + 1; j < npops - k; j++) if ( k == 0 ||
( modeloptions[MIGRATIONBETWEENSAMPLED]==0 &&
(modeloptions[PARAMETERSBYPERIOD]==1 ||
( !ISELEMENT (C[ARBCHAIN]->plist[k][i], C[ARBCHAIN]->periodset[k - 1]) ||
!ISELEMENT (C[ARBCHAIN]->plist[k][j], C[ARBCHAIN]->periodset[k - 1])
) ) ) )
/* tricky condition. proceed to set mcheck if:
where are considering only sampled populations (k==0) or
(not considering just sampled populations AND
EITHER we are considering each parameter to apply only to one period
OR one of the two populations being considered was not in the previous (k-1) period (meaning it is a new ancestral pop) */
{
/* if mcheck ends up as 1, we have one more parameter to add
if it is also the case that migration is not the same in both directions, then we have two parameters to add
mcheck can get set to 0 if we require two populations to be sisters, and they are not
or if the prior given for that pair of populations is zero
*/
if (modeloptions[NOMIGBETWEENNONSISTERS])
mcheck = checkaresis (ARBCHAIN,k, i, j);
else
{
//if (mprior > 0.0 || (calcoptions[LOADPRIORSFROMFILE] && mprior < 0.0) || (calcoptions[LOADPRIORSFROMFILE] && mprior_fromfile[C[ARBCHAIN]->plist[k][i]][C[ARBCHAIN]->plist[k][j]] > MINPARAMVAL))
if (mprior > 0.0 || (calcoptions[LOADPRIORSFROMFILE] && mprior_fromfile[C[ARBCHAIN]->plist[k][i]][C[ARBCHAIN]->plist[k][j]] > MINPARAMVAL))
mcheck = 1;
else
mcheck = 0;
}
if (mcheck)
{
mi++;
if (calcoptions[LOADPRIORSFROMFILE] ==0)
mi += modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS]==0;
}
}
}
else /* calcoptions[LOADPRIORSFROMFILE]*/
{
for (si=0; si< numtreepops;si++)
for (sj = 0; sj < numtreepops; sj++) if ( sj != si)
{
mcheck = 0; // each ordered pair of populations can only be counted once
for (k = 0; k < lastperiodnumber; k++)
for (i = 0; i < npops - k; i++)
for (j = 0; j < npops - k; j++) if ( j != i)
if (C[ARBCHAIN]->plist[k][i] == si && C[ARBCHAIN]->plist[k][j]==sj)
if (mprior_fromfile[si][sj] > MINPARAMVAL)
mcheck = 1;
if (mcheck==1)
mi++;
}
}
outsidefirstloop: ;
}
/* now do another loop, much like the previous one,
but now start to build the imig[] structures, including str, b, e
start to build the wp part of imig[] and determine wp.n*/
nummigrateparams = mi;
nummigrateparampairs = nummigrateparams / 2;
/*
type | # values | cumulative total at end
cc numpopsizeparams numpopsizeparams
fc numpopsizeparams 2*numpopsizeparams
hcc numpopsizeparams 3*numpopsizeparams
mc nummigrateparams 3*numpopsizeparams + nummigrateparams
fm nummigrateparams 3*numpopsizeparams + 2*nummigrateparams
qintegrate numpopsizeparams 4*numpopsizeparams + 2*nummigrateparams
mintegrate nummigrateparams 4*numpopsizeparams + 3*nummigrateparams
pdg 1 4*numpopsizeparams + 3*nummigrateparams + 1
probg 1 4*numpopsizeparams + 3*nummigrateparams + 2
t numsplittimes 4*numpopsizeparams + 3*nummigrateparams + numsplittimes + 2
*/
/* set values of position markers for gsampinf */
gsamp_ccp = 0;
gsamp_fcp = gsamp_ccp + numpopsizeparams;
gsamp_hccp = gsamp_fcp + numpopsizeparams;
gsamp_mcp = gsamp_hccp + numpopsizeparams;
gsamp_fmp = gsamp_mcp + nummigrateparams;
gsamp_qip = gsamp_fmp + nummigrateparams;
gsamp_mip = gsamp_qip + numpopsizeparams;
gsamp_pdgp = gsamp_mip + nummigrateparams;
gsamp_probgp = gsamp_pdgp + 1;
gsamp_tp = gsamp_probgp + 1;
} /* getparamnums */
void set_iparam_poptreeterms(int ci)
{
int i, j, k, ii, jj, kk, pj, ni, mi, mcheck;
char temps[PARAMSTRLEN];
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR])
filldescendantpops(ci);
// ithetas
if (modeloptions[PARAMETERSBYPERIOD])
{
for (i = 0, ii = 0, k = npops; i <= lastperiodnumber; i++, k--)
{
for (j = 0; j < k; j++)
{
pj = C[ci]->plist[i][j];
sprintf (C[ci]->itheta[ii].str, "q%d,%d", i, pj);
C[ci]->itheta[ii].b = C[ci]->poptree[pj].b;
C[ci]->itheta[ii].e = C[ci]->poptree[pj].e;
C[ci]->itheta[ii].wp.n = 1;
*C[ci]->itheta[ii].wp.p = i;
*C[ci]->itheta[ii].wp.r = j;
ii++;
}
}
}
else
{
for (ii = 0; ii < numpopsizeparams; ii++)
{
sprintf (C[ci]->itheta[ii].str, "q%d", ii);
C[ci]->itheta[ii].b = C[ci]->poptree[ii].b;
C[ci]->itheta[ii].e = C[ci]->poptree[ii].e;
C[ci]->itheta[ii].wp.n = 0;
for (i = 0, k = npops; i <= lastperiodnumber; i++, k--)
{
for (j = 0; j < k; j++)
{
if (ii == C[ci]->plist[i][j])
C[ci]->itheta[ii].wp.n += 1;
}
}
assert((ii==numpopsizeparams-1 && C[ci]->itheta[ii].wp.n==1) || (C[ci]->itheta[ii].wp.n == C[ci]->poptree[ii].e - C[ci]->poptree[ii].b ));
ni = 0;
for (i = 0, k = npops; i <= lastperiodnumber; i++, k--)
for (j = 0; j < k; j++)
{
if (ii == C[ci]->plist[i][j])
{
C[ci]->itheta[ii].wp.p[ni] = i; // the period number
C[ci]->itheta[ii].wp.r[ni] = j; // the position in plist for pop ii in that period (i.e. period i)
ni++;
}
}
assert (ni == C[ci]->itheta[ii].wp.n);
}
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR])
{
for (i=npops;i<numpopsizeparams;i++)
{
C[ci]->itheta[i].pr.max = C[ci]->qhyperparams[(int) C[ci]->descendantpops[i]];
}
}
}
/* set up the migration parameters
two loops here
1. build most of the imig[] structures, including str, b, e; start to build wp part of imig[]
2. finish building wp parts of imig[]
there is a complex series of checks to ensure that the intended model is being followed
loop through periods
check order:
MIGRATIONBETWEENSAMPLED - migration only between sampled populations
PARAMETERSBYPERIOD - every population size and migration parameter applies for only 1 period
NOMIGBETWEENNONSISTERS - set migration to zero between non-sister populations
SINGLEMIGRATIONBOTHDIRECTIONS
if ONEMIGRATIONPARAMETER applies then there is only one migration rate, whenever there is migration in the model
*/
/* this loops is similar to that used for counting migration parameters
but now start to build the imig[] structures, including str, b, e
start to build the wp part of imig[] and determine wp.n*/
if (!modeloptions[NOMIGRATION] && npops > 1)
{
if (modeloptions[ONEMIGRATIONPARAMETER])
{
mi = 0;
C[ci]->imig[mi].wp.n = 0;
C[ci]->imig[mi].b = 0;
C[ci]->imig[mi].e = lastperiodnumber - 1;
sprintf (C[ci]->imig[mi].str, "m_all");
}
else
mi = -1;
for (k = 0; k < lastperiodnumber; k++)
for (i = 0; i < npops - k - 1; i++)
for (j = i + 1; j < npops - k; j++) if ((k == 0) ||
(!modeloptions[MIGRATIONBETWEENSAMPLED] && (modeloptions[PARAMETERSBYPERIOD] ||
((!ISELEMENT(C[ci]->plist[k][i], C[ci]->periodset[k - 1]))
|| (!ISELEMENT(C[ci]->plist[k][j], C[ci]->periodset[k - 1]))))))
{
if (modeloptions[NOMIGBETWEENNONSISTERS])
mcheck = checkaresis (ci,k, i, j);
else
{
if (mprior > 0.0 || (calcoptions[LOADPRIORSFROMFILE] && mprior_fromfile[C[ci]->plist[k][i]][C[ci]->plist[k][j]] > MINPARAMVAL))
mcheck = 1;
else
mcheck = 0;
}
if (mcheck)
{
if (!modeloptions[ONEMIGRATIONPARAMETER])
{
mi++;
C[ci]->imig[mi].b = k;
C[ci]->imig[mi].e = k;
C[ci]->imig[mi].wp.n = 1;
// for m hyperprior stuff
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR])
{
C[ci]->imig[mi].md.from = C[ci]->plist[k][i];
C[ci]->imig[mi].md.to = C[ci]->plist[k][j];
//dir is 0 if 'from' pops are on the left side of descstr and 'to' pops are on the right, else dir is 1
C[ci]->imig[mi].dir = makepairstring(C[ci]->descendantpops[C[ci]->imig[mi].md.from],C[ci]->descendantpops[C[ci]->imig[mi].md.to],C[ci]->imig[mi].descstr);
if (modeloptions[EXPOMIGRATIONPRIOR])
{
C[ci]->imig[mi].pr.min = -1.0;
C[ci]->imig[mi].pr.max = -1.0;
if (C[ci]->imig[mi].dir == 0)
C[ci]->imig[mi].pr.expomean = getvalue(C[ci]->imig[mi].descstr,C[ci]->mltorhyperparams);
else
C[ci]->imig[mi].pr.expomean = getvalue(C[ci]->imig[mi].descstr,C[ci]->mrtolhyperparams);
}
else
{
C[ci]->imig[mi].pr.min = 0.0;
C[ci]->imig[mi].pr.expomean = -1.0;
if (C[ci]->imig[mi].dir == 0)
C[ci]->imig[mi].pr.max = getvalue(C[ci]->imig[mi].descstr,C[ci]->mltorhyperparams);
else
C[ci]->imig[mi].pr.max = getvalue(C[ci]->imig[mi].descstr,C[ci]->mrtolhyperparams);
}
}
if (hiddenoptions[HIDDENGENEALOGY]&& hiddenoptions[GSAMPINFOEXTRA])
{
sprintf (C[ci]->imig[mi].str, "%d>%d", C[ci]->plist[k][j], C[ci]->plist[k][i]);
}
}
else
C[ci]->imig[mi].wp.n++;
C[ci]->imig[mi].wp.n += modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS]; // can't also have ONEMIGRATIONPARAMETER
if (modeloptions[PARAMETERSBYPERIOD] && !modeloptions[ONEMIGRATIONPARAMETER])
{
sprintf (C[ci]->imig[mi].str, "m%d,", k);
if (modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS])
sprintf (temps, "%d<>%d", C[ci]->plist[k][i], C[ci]->plist[k][j]);
else
sprintf (temps, "%d>%d", C[ci]->plist[k][i], C[ci]->plist[k][j]);
strcat (C[ci]->imig[mi].str, temps);
}
else
{
kk = k + 1;
while (kk < lastperiodnumber
&& ISELEMENT (C[ci]->plist[k][i],
C[ci]->periodset[kk])
&& ISELEMENT (C[ci]->plist[k][j],
C[ci]->periodset[kk]))
{
if (!modeloptions[ONEMIGRATIONPARAMETER])
C[ci]->imig[mi].e = kk;
kk++;
C[ci]->imig[mi].wp.n++;
C[ci]->imig[mi].wp.n += modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS]; // can't also have ONEMIGRATIONPARAMETER
}
if (!modeloptions[ONEMIGRATIONPARAMETER])
{
if (modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS]) // can't also have ONEMIGRATIONPARAMETER
sprintf (C[ci]->imig[mi].str, "m%d<>%d", C[ci]->plist[k][i], C[ci]->plist[k][j]);
else
sprintf (C[ci]->imig[mi].str, "m%d>%d", C[ci]->plist[k][i], C[ci]->plist[k][j]);
}
}
if (!modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS])
{
if (!modeloptions[ONEMIGRATIONPARAMETER])
{
mi++;
C[ci]->imig[mi].b = k;
C[ci]->imig[mi].e = k;
C[ci]->imig[mi].wp.n = 1;
// for m hyperprior stuff
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR])
{
C[ci]->imig[mi].md.from = C[ci]->plist[k][j];
C[ci]->imig[mi].md.to = C[ci]->plist[k][i];
//dir is 0 if 'from' pops are on the left side of descstr and 'to' pops are on the right, else dir is 1
C[ci]->imig[mi].dir = makepairstring(C[ci]->descendantpops[C[ci]->imig[mi].md.from],C[ci]->descendantpops[C[ci]->imig[mi].md.to],C[ci]->imig[mi].descstr);
if (modeloptions[EXPOMIGRATIONPRIOR])
{
C[ci]->imig[mi].pr.min = -1.0;
C[ci]->imig[mi].pr.max = -1.0;
if (C[ci]->imig[mi].dir == 0)
C[ci]->imig[mi].pr.expomean = getvalue(C[ci]->imig[mi].descstr,C[ci]->mltorhyperparams);
else
C[ci]->imig[mi].pr.expomean = getvalue(C[ci]->imig[mi].descstr,C[ci]->mrtolhyperparams);
}
else // uniform migration prior
{
C[ci]->imig[mi].pr.min = 0.0;
C[ci]->imig[mi].pr.expomean = -1.0;
if (C[ci]->imig[mi].dir == 0)
C[ci]->imig[mi].pr.max = getvalue(C[ci]->imig[mi].descstr,C[ci]->mltorhyperparams);
else
C[ci]->imig[mi].pr.max = getvalue(C[ci]->imig[mi].descstr,C[ci]->mrtolhyperparams);
}
}
}
else
C[ci]->imig[mi].wp.n++;
if (modeloptions[PARAMETERSBYPERIOD] && !modeloptions[ONEMIGRATIONPARAMETER])
{
sprintf (C[ci]->imig[mi].str, "m%d,%d>%d", k, C[ci]->plist[k][j], C[ci]->plist[k][i]);
}
else
{
kk = k + 1;
while (kk < lastperiodnumber &&
ISELEMENT (C[ci]->plist[k][i],
C[ci]->periodset[kk])
&& ISELEMENT (C[ci]->plist[k][j],
C[ci]->periodset[kk]))
{
if (!modeloptions[ONEMIGRATIONPARAMETER])
C[ci]->imig[mi].e = kk;
kk++;
C[ci]->imig[mi].wp.n++;
}
if (!modeloptions[ONEMIGRATIONPARAMETER])
sprintf (C[ci]->imig[mi].str, "m%d>%d", C[ci]->plist[k][j], C[ci]->plist[k][i]);
}
}
}
}
/* now do another loop, much like the previous one. this time finish building the wp parts of C[ci]->imig[]
using wp.n that was determined in the previous loop */
if (modeloptions[ONEMIGRATIONPARAMETER])
{
mi = 0;
ni=-1;
}
else
mi = -1;
for (k = 0; k < lastperiodnumber; k++)
for (i = 0; i < npops - k - 1; i++)
for (j = i + 1; j < npops - k; j++) if ((k == 0) ||
(!modeloptions[MIGRATIONBETWEENSAMPLED] && (modeloptions[PARAMETERSBYPERIOD] ||
((!ISELEMENT (C[ci]->plist[k][i], C[ci]->periodset[k - 1]))
|| (!ISELEMENT(C[ci]->plist[k][j], C[ci]->periodset[k - 1]))))))
{
if (modeloptions[NOMIGBETWEENNONSISTERS])
mcheck = checkaresis (ci,k, i, j);
else
{
//if (mprior > 0.0 || (calcoptions[LOADPRIORSFROMFILE]==1 && mprior < 0.0) || (calcoptions[LOADPRIORSFROMFILE] && mprior_fromfile[C[ci]->plist[k][i]][C[ci]->plist[k][j]] > MINPARAMVAL))
if (mprior > 0.0 || (calcoptions[LOADPRIORSFROMFILE] && mprior_fromfile[C[ci]->plist[k][i]][C[ci]->plist[k][j]] > MINPARAMVAL))
mcheck = 1;
else
mcheck = 0;
}
if (mcheck)
{
if (!modeloptions[ONEMIGRATIONPARAMETER])
{
mi++;
ni = 0;
}
else
ni++;
C[ci]->imig[mi].wp.p[ni] = k;
C[ci]->imig[mi].wp.r[ni] = i;
C[ci]->imig[mi].wp.c[ni] = j;
assert (ni < C[ci]->imig[mi].wp.n);
if (modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS]) // can't also have ONEMIGRATIONPARAMETER
{
ni++;
C[ci]->imig[mi].wp.p[ni] = k;
C[ci]->imig[mi].wp.r[ni] = j;
C[ci]->imig[mi].wp.c[ni] = i;
assert (ni < C[ci]->imig[mi].wp.n);
}
if (!modeloptions[PARAMETERSBYPERIOD])
{
kk = k + 1;
while (kk < lastperiodnumber &&
ISELEMENT (C[ci]->plist[k][i],
C[ci]->periodset[kk])
&& ISELEMENT (C[ci]->plist[k][j],
C[ci]->periodset[kk]))
{
ii = 0;
while (C[ci]->plist[kk][ii] != C[ci]->plist[k][i])
ii++;
assert (ii < npops - kk);
jj = 0;
while (C[ci]->plist[kk][jj] != C[ci]->plist[k][j])
jj++;
assert (jj < npops - kk);
ni++;
C[ci]->imig[mi].wp.p[ni] = kk;
C[ci]->imig[mi].wp.r[ni] = ii;
C[ci]->imig[mi].wp.c[ni] = jj;
assert (ni < C[ci]->imig[mi].wp.n);
if (modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS]) // can't also have ONEMIGRATIONPARAMETER
{
ni++;
C[ci]->imig[mi].wp.p[ni] = kk;
C[ci]->imig[mi].wp.r[ni] = jj;
C[ci]->imig[mi].wp.c[ni] = ii;
assert (ni < C[ci]->imig[mi].wp.n);
}
kk++;
}
}
if (!modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS])
{
if (!modeloptions[ONEMIGRATIONPARAMETER])
{
mi++;
ni = 0;
}
else
ni++;
C[ci]->imig[mi].wp.p[ni] = k;
C[ci]->imig[mi].wp.r[ni] = j;
C[ci]->imig[mi].wp.c[ni] = i;
assert (ni < C[ci]->imig[mi].wp.n);
if (!modeloptions[PARAMETERSBYPERIOD])
{
kk = k + 1;
while (kk < lastperiodnumber &&
ISELEMENT (C[ci]->plist[k][i],
C[ci]->periodset[kk])
&& ISELEMENT (C[ci]->plist[k][j],
C[ci]->periodset[kk]))
{
ii = 0;
while (C[ci]->plist[kk][ii] != C[ci]->plist[k][i])
ii++;
assert (ii < npops - kk);
jj = 0;
while (C[ci]->plist[kk][jj] != C[ci]->plist[k][j])
jj++;
assert (jj < npops - kk);
ni++;
C[ci]->imig[mi].wp.p[ni] = kk;
C[ci]->imig[mi].wp.r[ni] = jj;
C[ci]->imig[mi].wp.c[ni] = ii;
kk++;
assert (ni < C[ci]->imig[mi].wp.n);
}
}
}
}
} // initialize wp
} /* set_iparam_poptreeterms */
}
/* set priors and xy plot stuff for population size and migration parameters */
void
setup_iparams (int ci)
{
int i, j, k, ii, jj, mi, mcheck;
if (calcoptions[LOADPRIORSFROMFILE])
{
for (i = 0; i < numpopsizeparams; i++)
{
C[ci]->itheta[i].pr.max = popsizeprior_fromfile[i];
if (C[ci]->itheta[i].pr.max <= 0.0)
IM_err(IMERR_PRIORFILEVALS,"prior for population %d is set less than or equal to zero",i);
C[ci]->itheta[i].pr.min = 0;
C[ci]->itheta[i].pr.expomean = -1.0;
}
}
else
{
for (i = 0; i < numpopsizeparams; i++)
{
C[ci]->itheta[i].pr.max = thetaprior;
C[ci]->itheta[i].pr.min = 0;
}
}
for (i = 0; i < numpopsizeparams; i++)
{
for (j = 0; j < GRIDSIZE; j++)
{
C[ci]->itheta[i].xy[j].x =
C[ci]->itheta[i].pr.min +
((j + 0.5) * (C[ci]->itheta[i].pr.max - C[ci]->itheta[i].pr.min)) / GRIDSIZE;
}
}
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR])
{
init_hyperprior_arrays(ci);
if (ci==ARBCHAIN)
{
numpossiblepoppairstrings = fillmigratepairs();
assert(numpossiblepoppairstrings==numdistinctpopulationpairs[npops]);
}
}
/* set up the migration parameter priors if a prior file is uses
this loop mirrors the first loop for mig parameters in set_iparam_poptreeterms()
but here we just deal with setting the priors
*/
if (calcoptions[LOADPRIORSFROMFILE])
{
if (!modeloptions[NOMIGRATION] && npops > 1)
{
if (modeloptions[ONEMIGRATIONPARAMETER])
{
mi = 0;
}
else
mi = -1;
for (k = 0; k < lastperiodnumber; k++)
for (i = 0; i < npops - k - 1; i++)
for (j = i + 1; j < npops - k; j++) if ((k == 0) ||
(!modeloptions[MIGRATIONBETWEENSAMPLED] && (modeloptions[PARAMETERSBYPERIOD] ||
((!ISELEMENT(C[ci]->plist[k][i], C[ci]->periodset[k - 1]))
|| (!ISELEMENT(C[ci]->plist[k][j], C[ci]->periodset[k - 1]))))))
{
if (modeloptions[NOMIGBETWEENNONSISTERS])
mcheck = checkaresis (ci,k, i, j);
else
{
if (mprior_fromfile[C[ci]->plist[k][i]][C[ci]->plist[k][j]] > MINPARAMVAL)
mcheck = 1;
else
mcheck = 0;
}
if (mcheck)
{
if (!modeloptions[ONEMIGRATIONPARAMETER])
{
mi++;
if (modeloptions[EXPOMIGRATIONPRIOR])
{
C[ci]->imig[mi].pr.expomean = mprior_fromfile[C[ci]->plist[k][i]][C[ci]->plist[k][j]];
C[ci]->imig[mi].pr.min = 0.0;
C[ci]->imig[mi].pr.max = EXPOMIGPLOTSCALE * C[ci]->imig[mi].pr.expomean;
}
else
{
C[ci]->imig[mi].pr.max =mprior_fromfile[C[ci]->plist[k][i]][C[ci]->plist[k][j]];
C[ci]->imig[mi].pr.min = 0;
C[ci]->imig[mi].pr.expomean = -1.0;
}
if (C[ci]->imig[mi].pr.max <= MINPARAMVAL)
IM_err(IMERR_PRIORFILEVALS,"migration rate set too low from %d to %d",i,j);
}
if (!modeloptions[SINGLEMIGRATIONBOTHDIRECTIONS])
{
if (!modeloptions[ONEMIGRATIONPARAMETER])
{
mi++;
if (modeloptions[EXPOMIGRATIONPRIOR])
{
C[ci]->imig[mi].pr.expomean = mprior_fromfile[C[ci]->plist[k][j]][C[ci]->plist[k][i]];
C[ci]->imig[mi].pr.min = 0.0;
C[ci]->imig[mi].pr.max = EXPOMIGPLOTSCALE * C[ci]->imig[mi].pr.expomean;
}
else
{
C[ci]->imig[mi].pr.max =mprior_fromfile[C[ci]->plist[k][j]][C[ci]->plist[k][i]];
C[ci]->imig[mi].pr.min = 0;
C[ci]->imig[mi].pr.expomean = -1.0;
}
if (C[ci]->imig[mi].pr.max <= MINPARAMVAL)
IM_err(IMERR_PRIORFILEVALS,"migration rate set too low from %d to %d",i,j);
}
}
}
}
}
}
else
{
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR])
{
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR])
{
for (i = 0; i < numpossiblepoppairstrings; i++)
{
if (modeloptions[EXPOMIGRATIONPRIOR])
{
//fill the dictionaries, keys are poppair strings, values are priors
struct dictionary_node_kr *temp;
temp = dictionary_install(poppairs[i],uniforminterval(MINVALFROMHYPERPRIOR,hyperprior_expo_m_mean),C[ci]->mltorhyperparams);
temp = dictionary_install(poppairs[i],uniforminterval(MINVALFROMHYPERPRIOR,hyperprior_expo_m_mean),C[ci]->mrtolhyperparams);
}
else
{
//fill the dictionaries, keys are poppair strings, values are priors
struct dictionary_node_kr *temp;
temp = dictionary_install(poppairs[i],uniforminterval(MINVALFROMHYPERPRIOR,hyperprior_uniform_m_max),C[ci]->mltorhyperparams);
temp = dictionary_install(poppairs[i],uniforminterval(MINVALFROMHYPERPRIOR,hyperprior_uniform_m_max),C[ci]->mrtolhyperparams);
}
}
C[ci]->qhyperparams[0] = -1.0; // does not get used because indexes are SET values and 0 is NULL set .
for (i=1;i<numpopsets;i++)
C[ci]->qhyperparams[i] = uniforminterval(MINVALFROMHYPERPRIOR,hyperprior_uniform_q_max);
}
}
else
{
for (ii=0;ii< nummigrateparams;ii++)
{
if (modeloptions[EXPOMIGRATIONPRIOR])
{
C[ci]->imig[ii].pr.expomean = expo_m_mean;
C[ci]->imig[ii].pr.min = 0.0;
C[ci]->imig[ii].pr.max = EXPOMIGPLOTSCALE * expo_m_mean;
}
else
{
C[ci]->imig[ii].pr.max = DMAX (m_max, MPRIORMIN);
C[ci]->imig[ii].pr.min = 0;
C[ci]->imig[ii].pr.expomean = -1.0;
}
}
}
}
for (ii=0;ii< nummigrateparams;ii++)
{
for (jj = 0; jj < GRIDSIZE; jj++)
{
if (modeloptions[EXPOMIGRATIONPRIOR])
{
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR]==0)
C[ci]->imig[ii].xy[jj].x =
((jj + 0.5) * (C[ci]->imig[ii].pr.expomean * EXPOMIGPLOTSCALE)) / GRIDSIZE;
else
C[ci]->imig[ii].xy[jj].x =
((jj + 0.5) * (hyperprior_expo_m_mean * EXPOMIGPLOTSCALE)) / GRIDSIZE;
}
else
{
if (modeloptions[POPSIZEANDMIGRATEHYPERPRIOR]==0)
C[ci]->imig[ii].xy[jj].x = C[ci]->imig[ii].pr.min +
((jj + 0.5) * (C[ci]->imig[ii].pr.max - C[ci]->imig[ii].pr.min)) / GRIDSIZE;
else
C[ci]->imig[ii].xy[jj].x = C[ci]->imig[ii].pr.min +
((jj + 0.5) * (hyperprior_uniform_m_max)) / GRIDSIZE;
}
}
C[ci]->imig[ii].wp.n = 0;
}
} /* setup_iparams */
/* nomigrationchecklist is three arrays (p for period, r for row, and c for column), each of a set length (nomigrationchecklist.n )
nomigrationchecklist is used to check gweight->mc[][][]
gweight->mc[k][i][j] is the number of migration events in the genealogies in period k
from population C[ci]->plist[k][i] to population C[ci]->plist[k][j]
So the ith element of the arrays in nomigrationchecklist is used to check the value of
gweight->mc[nomigrationchecklist.p[i]][nomigrationchecklist.r[i]][nomigrationchecklist.c[i]]
(this is done in update_gree_common.c). If that value if not zero the update gets rejected.
building nomigrationchecklist requires identifying which pairs of populations,
C[ci]->plist[k][i] and C[ci]->plist[k][j], in each period k, should not be
exchanging migrants.
These pairs are those that meet any of the following:
1. modeloptions[MIGRATIONBETWEENSAMPLED]==1 and the populations are not both sampled populations
2. modeloptions[NOMIGBETWEENNONSISTERS]==1 and the populations are not sisters
3. they have had their priors set to zero in a priorfile
These correspond to the following:
1. modeloptions[MIGRATIONBETWEENSAMPLED]==1 && C[ci]->plist[k][i] >= npops && C[ci]->plist[k][j] >= npops
2. modeloptions[NOMIGBETWEENNONSISTERS]==1 && checkaresis (ci,k, i, j)==1
3. calcoptions[LOADPRIORSFROMFILE]==1 && mprior_fromfile[C[ci]->plist[k][i]][C[ci]->plist[k][j]] <= MINPARAMVAL
To set this up we must go through two loops, first to cound how many (i.e. set nomigrationchecklist.n)
Then after initializing the arrays in nomigrationchecklist, we must do another similar loop to fill them up.
*/
/* this should not be needed if modeloptions[NOMIGRATION]==1 */
void
set_nomigrationchecklist () // not used for population tree updating so work on C[ARBCHAIN]
{
int n, i, j, k;
nomigrationchecklist.n = 0;
nomigrationchecklist.p = NULL;
nomigrationchecklist.r = NULL;
nomigrationchecklist.c = NULL;
for (k = 0; k < lastperiodnumber; k++)
for (i = 0; i < npops - k - 1; i++)
for (j = i + 1; j < npops - k; j++)
{
if ((modeloptions[MIGRATIONBETWEENSAMPLED]==1 && (C[ARBCHAIN]->plist[k][i] >= npops || C[ARBCHAIN]->plist[k][j] >= npops)) ||
(modeloptions[NOMIGBETWEENNONSISTERS]==1 && checkaresis (ARBCHAIN,k, i, j)==0) ||
(calcoptions[LOADPRIORSFROMFILE]==1 && mprior_fromfile[C[ARBCHAIN]->plist[k][i]][C[ARBCHAIN]->plist[k][j]] <= MINPARAMVAL))
{
nomigrationchecklist.n+=2;
}
}
if (nomigrationchecklist.n > 0)
{
nomigrationchecklist.p = static_cast<int *>
(malloc (nomigrationchecklist.n * sizeof (int)));
nomigrationchecklist.r = static_cast<int *>
(malloc (nomigrationchecklist.n * sizeof (int)));
nomigrationchecklist.c = static_cast<int *>
(malloc (nomigrationchecklist.n * sizeof (int)));
}
for (n = -1, k = 0; k < lastperiodnumber; k++)
for (i = 0; i < npops - k - 1; i++)
for (j = i + 1; j < npops - k; j++)
{
if ((modeloptions[MIGRATIONBETWEENSAMPLED]==1 && (C[ARBCHAIN]->plist[k][i] >= npops || C[ARBCHAIN]->plist[k][j] >= npops)) ||
(modeloptions[NOMIGBETWEENNONSISTERS]==1 && checkaresis (ARBCHAIN,k, i, j)==0) ||
(calcoptions[LOADPRIORSFROMFILE]==1 && mprior_fromfile[C[ARBCHAIN]->plist[k][i]][C[ARBCHAIN]->plist[k][j]] <= MINPARAMVAL))
{
n++;
nomigrationchecklist.p[n] = k;
nomigrationchecklist.r[n] = i;
nomigrationchecklist.c[n] = j;
n++;
nomigrationchecklist.p[n] = k;
nomigrationchecklist.r[n] = j;
nomigrationchecklist.c[n] = i;
}
}
assert(nomigrationchecklist.n == n+1);
return;
} // set_nomigrationchecklist
#define MAXPRIORSCALETRY 10000000 // max number of times to try getting starting mutation rates compatible with priors
void
setuinfo (double summut) // called only for chain 0 because mutation rate info gets copied into other chains
{
int i, li, ui, uj, k;
int mutchain0 = 0;
int numuprior = 0;
double priorscaletry = 0;
double U, r;
int doneu, rcheck, numupair, *upriorpairlist1, *upriorpairlist2;
double prodcheck, maxr, newr, d;
/* ul is global. It is a list of struct upairlist that contains a all of the locations of the mutation rate scalars.
each upairlist has a .l value (has the locus #) and .u value which is the scalar number for that locus
Each locus L[li] has a list uii that has the position in ul of that scalar
uii is the position in that list
in set_mcparam_values() the priors on mutation rate scalars are set to standard pos (max) and neg (min) values (they are on a log scale)
and are stored in the mcinf.pr (e.g. C[mutchain0]->G[0].u[0].mcinf.pr)
if the input file has prior ranges on mutation yets, these are read into uperyear.pr (e.g. C[ci]->G[li].u[ui].uperyear.pr)
The ratios of these uperyear values among loci must be taken to reset the values of mcinf.pr
*/
for (i = 0, li = 0; li < nloci; li++)
for (ui = 0; ui < L[li].nlinked; ui++)
{
ul[i].l = li;
ul[i].u = ui;
L[li].uii[ui] = i;
i++;
}
if (hiddenoptions[NOMUTATIONSCALARUPATES] == 1) // fix them all to be 1 and then do not update them
{
domutationscalarupdate = 0;