-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.cpp
1614 lines (1447 loc) · 58.4 KB
/
start.cpp
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
#include <time.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <math.h>
#include <omp.h>
#include "anyoption.h"
#define UN_EXTERN
#include "global_defs.h"
#include "errors.cpp"
using namespace std;
/* 1. CREATE AN OBJECT */
AnyOption *opt = new AnyOption();
//////////////////////////////////////
// main program
//////////////////////////////////////
int main(int argc, char **argv) {
int last_printf = 0;
int acc_rate;
int cur_out = 0;
nr_out = 5000;
interval = 10;
discard = 50000;
nb_pilot = 20;
pilot_length = 5000;
prior_odds = 10;
// put nb of pressure to 0 to check latter if pressure is in the structure file
// if not, I will automatically create one pressure per group
P=0;
string file_name;
string struct_file_name;
string odir = "";
string file_prefix;
/* 2. SET PREFERENCES */
opt->noPOSIX(); /* do not check for POSIX style character options */
/* 3. SET THE USAGE/HELP */
opt->addUsage(" --------------------------- ");
opt->addUsage(" | BayeScanH 1.0 usage: | ");
opt->addUsage(" --------------------------- ");
opt->addUsage(" -help Prints this help ");
opt->addUsage(" --------------------------- ");
opt->addUsage(" | Input | ");
opt->addUsage(" --------------------------- ");
opt->addUsage(" alleles.txt Name of the genotypes data input file ");
opt->addUsage(" -d discarded Optional input file containing list of loci to discard");
opt->addUsage(" -s structure Input file containing the population structure");
opt->addUsage(" -snp Use SNP genotypes matrix");
opt->addUsage(" --------------------------- ");
opt->addUsage(" | Output | ");
opt->addUsage(" --------------------------- ");
opt->addUsage(" -od . Output file directory, default is the same as program file");
opt->addUsage(" -o alleles Output file prefix, default is input file without the extension");
opt->addUsage(" -fstat Only estimate F-stats (no selection)");
opt->addUsage( " -all_trace Write out MCMC trace also for alpha paremeters (can be a very large file)" );
opt->addUsage(" --------------------------- ");
opt->addUsage(" | Parameters of the chain | ");
opt->addUsage(" --------------------------- ");
opt->addUsage(" -threads n Number of threads used, default is number of cpu available ");
opt->addUsage(" -n 5000 Number of outputted iterations, default is 5000 ");
opt->addUsage(" -thin 10 Thinning interval size, default is 10 ");
opt->addUsage(" -nbp 20 Number of pilot runs, default is 20 ");
opt->addUsage(" -pilot 5000 Length of pilot runs, default is 5000 ");
opt->addUsage(" -burn 50000 Burn-in length, default is 50000 ");
opt->addUsage(" --------------------------- ");
opt->addUsage(" | Parameters of the model | ");
opt->addUsage(" --------------------------- ");
opt->addUsage(" -pr_odds 10 Prior odds for the neutral model, default is 10 ");
opt->addUsage(" -lb_fis 0 Lower bound for uniform prior on Fis (dominant data), default is 0");
opt->addUsage(" -hb_fis 1 Higher bound for uniform prior on Fis (dominant data), default is 1");
opt->addUsage(" -beta_fis Optional beta prior for Fis (dominant data, m_fis and sd_fis need to be set)");
opt->addUsage(" -m_fis 0.05 Optional mean for beta prior on Fis (dominant data with -beta_fis)");
opt->addUsage(" -sd_fis 0.01 Optional std. deviation for beta prior on Fis (dominant data with -beta_fis)");
opt->addUsage(" -aflp_pc 0.1 Threshold for the recessive genotype as a fraction of maximum band intensity, default is 0.1");
opt->addUsage(" --------------------------- ");
opt->addUsage(" | Output files | ");
opt->addUsage(" --------------------------- ");
opt->addUsage(" -out_pilot Optional output file for pilot runs");
opt->addUsage(" -out_freq Optional output file for allele frequencies");
/* 4. SET THE OPTION STRINGS/CHARACTERS */
opt->setFlag("help");
opt->setOption("n");
opt->setOption("thin");
opt->setOption("burn");
opt->setOption("pr_odds");
opt->setOption("o");
opt->setOption("d");
opt->setOption("threads");
opt->setFlag("out_pilot");
opt->setFlag("out_freq");
opt->setFlag("beta_fis");
opt->setFlag("fstat");
opt->setFlag( "all_trace");
opt->setOption("s");
opt->setFlag("snp");
opt->setOption("lb_fis");
opt->setOption("hb_fis");
opt->setOption("m_fis");
opt->setOption("sd_fis");
opt->setOption("aflp_pc");
opt->setOption("od");
opt->setOption("nbp");
opt->setOption("pilot");
/* 5. PROCESS THE COMMANDLINE AND RESOURCE FILE */
/* go through the command line and get the options */
opt->processCommandArgs(argc, argv);
if (!opt->hasOptions() || opt->getArgc() == 0 || opt->getValue("s") == NULL) /* print usage if no options */ {
opt->printUsage();
delete opt;
exit(0);
}
/* 6. GET THE VALUES */
// create and seed parallels randgens
num_threads = omp_get_max_threads();
if (opt->getValue("threads") != NULL)
num_threads = max(1, atoi(opt->getValue("threads")));
cout << "Using " << num_threads << " threads (" << omp_get_max_threads() << " cpu detected on this machine)" << endl;
omp_set_num_threads(num_threads);
randgen_parallel = new MTRand[num_threads];
for (int k = 0; k < num_threads; k++)
randgen_parallel[k].seed(randgen.randInt());
if (opt->getFlag("help") || opt->getFlag('h'))
opt->printUsage();
if (opt->getValue("n") != NULL)
nr_out = atoi(opt->getValue("n"));
if (opt->getValue("thin") != NULL)
interval = atoi(opt->getValue("thin"));
if (opt->getValue("burn") != NULL)
discard = atoi(opt->getValue("burn"));
if (opt->getValue("nbp") != NULL)
nb_pilot = atoi(opt->getValue("nbp"));
if (opt->getValue("pilot") != NULL)
pilot_length = atoi(opt->getValue("pilot"));
if (opt->getValue("pr_odds") != NULL)
prior_odds = atof(opt->getValue("pr_odds"));
file_name = opt->getArgv(0);
struct_file_name = opt->getValue("s");
if (opt->getFlag("snp"))
SNP_genotypes = true;
else SNP_genotypes = false;
if (opt->getFlag("fstat"))
fstat = true;
else fstat = false;
if (opt->getFlag( "all_trace" ) )
all_trace=true;
else all_trace=false;
if (opt->getValue("od") != NULL) {
odir = opt->getValue("od");
odir = odir + "/";
}
if (opt->getValue("o") != NULL)
file_prefix = opt->getValue("o");
else {
file_prefix = file_name;
if (file_prefix.find(".", 1) != string::npos)
file_prefix = file_prefix.substr(0, file_prefix.length() - 4);
int position;
if (position = file_prefix.find_last_of('/'))
file_prefix = file_prefix.substr(position + 1, file_prefix.length());
//cout << file_prefix << endl;
}
// prior for Fis
double prior_fis_mean;
double prior_fis_sd;
prior_fis_unif = true; // true if uniform prior, false if beta
if (opt->getFlag("beta_fis")) {
prior_fis_unif = false;
if (opt->getValue("m_fis") != NULL && opt->getValue("sd_fis") != NULL) {
prior_fis_mean = atof(opt->getValue("m_fis")); // mean for beta
prior_fis_sd = atof(opt->getValue("sd_fis")); // sd for beta
prior_fis_a = prior_fis_mean * (prior_fis_mean * (1 - prior_fis_mean) / (prior_fis_sd * prior_fis_sd) - 1);
prior_fis_b = (1 - prior_fis_mean)*(prior_fis_mean * (1 - prior_fis_mean) / (prior_fis_sd * prior_fis_sd) - 1);
if (prior_fis_a <= 0 || prior_fis_b <= 0) {
cout << "Error in prior definition for Fis";
return 1;
}
} else {
cout << "Error in prior definition for Fis";
return 1;
}
} else {
if (opt->getValue("lb_fis") != NULL && opt->getValue("hb_fis") != NULL) {
prior_fis_lb = atof(opt->getValue("lb_fis")); // lower bound for uniform prior
prior_fis_hb = atof(opt->getValue("hb_fis")); // higher bound
if (prior_fis_unif && (prior_fis_lb < 0 || prior_fis_hb > 1 || prior_fis_lb >= prior_fis_hb)) {
cout << "Error in prior definition for Fis";
return 1;
}
} else {
prior_fis_lb = 0;
prior_fis_hb = 1;
}
}
abscence_pc = 0.1;
if (opt->getValue("aflp_pc") != NULL) {
abscence_pc = atof(opt->getValue("aflp_pc"));
}
// Calculate length of run based on the Parameters of Markov Chain
tot_nr_of_iter = nr_out * interval + discard; //number of iterations of the algorithm
// create main output file
ofstream mainout((odir + file_prefix + ".sel").c_str());
assure(mainout);
ofstream modelout;
// create ouput file for fst mean values
if (!opt->getFlag("fstat") && all_trace) {
modelout.open((odir + file_prefix + "_models.txt").c_str());
assure(modelout);
}
// read input file
ifstream infile(file_name.c_str());
assure(infile);
// determine if data are dominant or codominant markers
string line;
int k = 0;
while (getline(infile, line, '=')) {
if (line.length() >= 5 && line.substr(line.length() - 5, line.length()) == "[pop]") //read allele count
{
getline(infile, line);
getline(infile, line); // read the line of allele count at locus i
istringstream read_line(line);
int trash;
/*while(read_line.good())
{
read_line >> trash;
k++;
}*/
while (read_line >> trash)
k++;
break;
}
}
// selection2
if (k == 0)
codominant = 0.5;
else
codominant = (float) (k >= 4);
infile.close();
infile.clear();
if (codominant == 0.5) // selection2
{
string verif_name = odir + file_prefix + "_Verif.txt";
if (read_input_intensity(file_name,struct_file_name, verif_name)) {
return 1;
}
} else {
infile.open(file_name.c_str());
string verif_name = odir + file_prefix + "_Verif.txt";
if (read_input(infile,struct_file_name, verif_name)) {
return 1;
}
infile.close();
infile.clear();
}
//read discarded input file
for (int i = 0; i < I; i++) {
discarded_loci[i] = false;
}
if (opt->getValue("d") != NULL) {
// read input file
ifstream infile_discarded(opt->getValue("d"));
assure(infile_discarded);
read_discarded(infile_discarded);
infile_discarded.close();
}
// public version: remove frequency output, keep only posterior mean matrix
/*ofstream ancfreq;
// create ouput file for ancestral freq
if (opt->getFlag( "out_freq" ) )
{
ancfreq.open((odir+file_prefix+"_ancestral.txt").c_str());
assure(ancfreq);
// write header
if (codominant<1)
{
for (int i=0;i<I;i++)
{
if (!discarded_loci[i])
ancfreq << "locus" << (i+1) << " " ;
}
ancfreq << endl;
}
}*/
ofstream fst_file;
// create ouput file for fst mean values
if (!opt->getFlag("fstat")) {
fst_file.open((odir + file_prefix + "_fst.txt").c_str());
assure(fst_file);
}
// public version: remove frequency output, keep only posterior mean matrix
// create output files for allele frequencies in each population
//ofstream *freq_pop2;
ofstream freq_pop;
if (opt->getFlag("out_freq") && codominant < 1) // selection2
{
freq_pop.open((odir + file_prefix + "_freq_pops.txt").c_str());
/* try
{
freq_pop2=new ofstream[J];
}
catch (const std::exception & Exp )
{
cout << "Not enough memory for freq_pop" << endl;
return 1;
}
for (int j=0;j<J;j++) //cycle over populations
{
ostringstream oss;
oss << odir << file_prefix << "_pop" << (j+1) << ".txt";
freq_pop2[j].open(oss.str().c_str());
if (freq_pop2[j].fail())
{
cout << "Cannot not open file " << oss.str().c_str();
return 1;
}
else
{
// write header
if (codominant<1)
{
for (int i=0;i<I;i++)
{
if (!discarded_loci[i])
freq_pop2[j] << "locus" << (i+1) << " " ;
}
freq_pop2[j] << endl;
}
}
}*/
}
// public version: remove frequency output, keep only posterior mean matrix
// create output files for allele frequencies in each group
/* ofstream *freq_group2;
ofstream freq_group;
if (opt->getFlag( "out_freq" ) && codominant<1) // selection2
{
freq_group.open((odir+file_prefix+"_freq_groups.txt").c_str());
try
{
freq_group2=new ofstream[G];
}
catch (const std::exception & Exp )
{
cout << "Not enough memory for freq_group" << endl;
return 1;
}
for (int g=0;g<G;g++) //cycle over populations
{
ostringstream oss;
oss << odir << file_prefix << "_group" << (g+1) << ".txt";
freq_group2[g].open(oss.str().c_str());
if (freq_group2[g].fail())
{
cout << "Cannot not open file " << oss.str().c_str();
return 1;
}
else
{
// write header
if (codominant<1)
{
for (int i=0;i<I;i++)
{
if (!discarded_loci[i])
freq_group2[g] << "locus" << (i+1) << " " ;
}
freq_group2[g] << endl;
}
}
}
}*/
// create ouput file for acceptance rate
ofstream outAccRte;
outAccRte.open((odir + file_prefix + "_AccRte.txt").c_str());
assure(outAccRte);
acc_a_p = 0;
if (codominant == 0.5) {
for (int i = 0; i < I; i++) // selection2
{
acc_mu[i] = 0;
acc_delta[i] = 0;
acc_sigma1[i] = 0;
acc_sigma2[i] = 0;
}
}
for (int i = 0; i < I; i++) // selection2
{
acc_alpha[i] = 0;
acc_freq_ancestral[i] = 0;
for (int p = 0; p < P; p++)
acc_eta[p][i] = 0;
}
for (int j = 0; j < J; j++) // selection2
{
acc_theta[j] = 0;
}
for (int g = 0; g < G; g++) // selection2
{
acc_beta[g] = 0;
}
if (codominant < 1) {
for (int j = 0; j < J; j++) {
acc_f[j] = 0;
}
}
for (int i = 0; i < I; i++) // selection2
{
for (int j = 0; j < J; j++) // selection2
{
acc_freq[i][j] = 0;
}
}
for (int i = 0; i < I; i++) // selection2
{
for (int g = 0; g < G; g++) // selection2
{
acc_freq_group[i][g] = 0;
}
}
if (codominant == 1) // to do with theta and eta
outAccRte /*<< "alpha "*/ << "beta " << "ances " << "group_freq " /* << "f "* << "a_p " << "b_p "*/ << endl;
else if (codominant == 0)
outAccRte /*<< "alpha "*/ << "beta " << "ances "/* << "f "*/ << "group_freq " << "freq " /*<< "a_p " << "b_p "*/ << endl;
else // selection2
outAccRte /*<< "alpha "*/ << "beta " << "ances "/* << "f "*/ << "group_freq " << "freq " /*<< "a_p " << "b_p "*/ << "f " << "mu " << "delta " << "sigma1 " << "sigma2 " << endl;
// create output file for proposal variance
ofstream outprop;
if (opt->getFlag("out_pilot")) {
outprop.open((odir + file_prefix + "_prop" + ".txt").c_str());
assure(outprop);
}
// initialize f from uniform distribution selection2
if (codominant < 1) {
if (prior_fis_unif) {
for (int j = 0; j < J; j++)
f[j] = (prior_fis_lb + prior_fis_hb) / 2;
} else {
for (int j = 0; j < J; j++)
f[j] = prior_fis_mean;
}
}
// initialize alpha with normal(0,1) values
for (int i = 0; i < I; i++) {
alpha[i] = 0; //randgen.randNorm(0,1);
alpha_included[i] = true;
post_alpha[i] = 0;
post_fct[i] = 0;
nb_alpha[i] = 0;
for (int p = 0; p < P; p++) {
eta[p][i] = 0;
eta_included[p][i] = false;
post_eta[p][i] = 0;
nb_eta[p][i] = 0;
}
for (int g = 0; g < G; g++) {
eta2[g][i] = 0;
eta2_included[g][i] = true;
post_eta2[g][i] = 0;
nb_eta2[g][i] = 0;
}
for (int g = 0; g < G; g++)
post_fsc[g][i] = 0;
}
//alpha_updates=0;
//eta_updates=new int[G];
//for (int g=0;g<G;g++)
// eta_updates[g]=0;
// nb_alpha_included=0;
//for (int g=0;g<G;g++)
// nb_eta_included[g]=0;
// initialize beta and theta with fst=0.1 values
for (int g = 0; g < G; g++)
beta[g] = log(0.1 / (1 - 0.1));
for (int j = 0; j < J; j++)
theta[j] = log(0.1 / (1 - 0.1));
// initialize beta prior for ancestral allele frequencies
a_p = 1;
if (codominant == 1) // selection2
{
// initialize group allele frequences from data count
double *alphaP;
for (int g = 0; g < G; g++) {
for (int i = 0; i < I; i++) //cycle over loci within populations
{
alphaP = new double[freq_locus[i].ar];
for (int k = 0; k < freq_locus[i].ar; k++) {
alphaP[k] = 1.0;
for (int j_g = 0; j_g < group[g].member.size(); j_g++)
alphaP[k] += pop[group[g].member[j_g]].locus[i].data_allele_count[k];
}
dirichlet_dev(group[g].locus[i].allele, alphaP, freq_locus[i].ar);
delete[] alphaP;
}
}
// initialize ancetral allele frequences from mean of groups
for (int i = 0; i < I; i++) //cycle over loci within populations
{
for (int k = 0; k < freq_locus[i].ar; k++) {
freq_locus[i].allele[k] = 0.0;
for (int g = 0; g < G; g++)
freq_locus[i].allele[k] += group[g].locus[i].allele[k];
freq_locus[i].allele[k] /= G;
}
}
} else {
// initialize ancestral allele frequencies from beta distribution
for (int i = 0; i < I; i++)
freq_ancestral[i] = randgen.randDblExc();
// initialize allele frequencies from beta(theta*pi,theta*(1-pi))
double phi_sc, phi_ct;
int anc_pop;
for (int i = 0; i < I; i++) {
for (int g = 0; g < G; g++) {
phi_ct = exp(-(alpha[i] + beta[g]));
group[g].locus[i].p = genbet(phi_ct * freq_ancestral[i], phi_ct * (1 - freq_ancestral[i])); // selection2 bug : removed a_p*...
if (group[g].locus[i].p <= 0.0001)
group[g].locus[i].p = 0.0001;
if (group[g].locus[i].p >= 0.9999)
group[g].locus[i].p = 0.9999;
group[g].locus[i].mean_p = 0;
}
for (int j = 0; j < J; j++) {
phi_sc = exp(-(eta[group[pop[j].group].pressure][i] + theta[j]));
pop[j].locus[i].p = genbet(phi_sc * group[pop[j].group].locus[i].p, phi_sc * (1 - group[pop[j].group].locus[i].p)); // selection2 bug : removed a_p*...
if (pop[j].locus[i].p <= 0.0001)
pop[j].locus[i].p = 0.0001;
if (pop[j].locus[i].p >= 0.9999)
pop[j].locus[i].p = 0.9999;
pop[j].locus[i].mean_p = 0;
}
}
}
// initialize mu and sigma (to do) selection2
if (codominant == 0.5) {
for (int i = 0; i < I; i++) {
mu[i] = 0.2;
delta[i] = 0.2;
sigma1[i] = 0.05;
sigma2[i] = 0.05;
}
}
log_likelihood = allelecount_loglikelihood();
//interval between output of acceptance rates
acc_rate = tot_nr_of_iter / 50;
////////////////////////////////////////////////////////
// Pilot runs for within model updates acceptance rates
// and reversible jump efficient proposal
////////////////////////////////////////////////////////
//Form1->Time->Caption="";
cout << "Pilot runs..." << endl;
m1_prior_alpha = 0;
m2_prior_alpha = 0;
sd_prior_alpha = 1;
for (int i = 0; i < I; i++) {
e_ancestral[i] = 0.2;
var_prop_alpha[i] = 1.0;
for (int p = 0; p < P; p++) {
var_prop_eta[p][i] = 1.0;
}
for (int g = 0; g < G; g++) {
var_prop_eta2[g][i] = 1.0;
}
}
if (codominant == 0.5) {
for (int i = 0; i < I; i++) {
e_mu[i] = 0.01; // selection2 -> to do : add as parameters?
e_delta[i] = 0.01; // selection2 -> to do : add as parameters?
var_prop_sigma1[i] = 0.3; // selection2 -> to do : add as parameters?
var_prop_sigma2[i] = 0.3; // selection2 -> to do : add as parameters?
}
}
for (int j = 0; j < J; j++) {
if (codominant < 1)
e_f[j] = 0.05; // selection2 -> to do : add as parameters?
var_prop_theta[j] = 0.2; // selection3 -> to do : add as parameters ?
}
for (int g = 0; g < G; g++) {
var_prop_beta[g] = 0.7;
}
for (int i = 0; i < I; i++) {
for (int j = 0; j < J; j++) {
e_freq[i][j] = 0.2;
}
}
for (int i = 0; i < I; i++) {
for (int g = 0; g < G; g++) {
e_freq_group[i][g] = 0.2;
}
}
var_prop_a_p = 0.2;
for (int i = 0; i < I; i++) {
mean_alpha[i] = 0.0;
var_alpha[i] = 5.0;
for (int p = 0; p < P; p++) {
mean_eta[p][i] = 0.0;
var_eta[p][i] = 5.0;
}
for (int g = 0; g < G; g++) {
mean_eta2[g][i] = 0.0;
var_eta2[g][i] = 5.0;
}
}
int nb_pilot_alpha = 0;
int nb_pilot_eta = 0;
int nb_pilot_eta2 = 0;
double *m2 = new double[I];
for (int i = 0; i < I; i++)
m2[i] = 0;
double **m2_eta2 = new double*[G];
for (int g = 0; g < G; g++)
m2_eta2[g] = new double[I];
for (int i = 0; i < I; i++) {
for (int g = 0; g < G; g++)
m2_eta2[g][i] = 0;
}
double **m2_eta = new double*[P];
for (int p = 0; p < P; p++)
m2_eta[p] = new double[I];
for (int i = 0; i < I; i++) {
for (int p = 0; p < P; p++)
m2_eta[p][i] = 0;
}
bool convergent_switch=false;
for (int k = 0; k < nb_pilot; k++) {
// half of pilot runs: switch to convergent evolution model
if (!convergent_switch && (4*k>=3*nb_pilot) ) {
convergent_switch=true;
for (int i = 0; i < I; i++) {
for (int p = 0; p < P; p++) {
eta[p][i] = 0;
eta_included[p][i] = true;
}
for (int g = 0; g < G; g++) {
eta2[g][i] = 0;
eta2_included[g][i] = false;
}
}
cout.flush();
}
// pilot runs
for (iter = 0; iter < pilot_length; ++iter) {
if ((100 * (pilot_length * k + iter)) / (pilot_length * nb_pilot) > last_printf) {
last_printf = (100 * (pilot_length * k + iter)) / (pilot_length * nb_pilot);
cout << last_printf << "% ";
cout.flush();
}
// do not block application during the long process
if (codominant < 1) //selection2
update_beta();
else
update_beta_codominant();
if (codominant < 1) //selection2
update_theta();
else
update_theta_codominant();
if (!fstat) // update selection related parameters
{
/*for (int i=0;i<I;i++)
{
if (!discarded_loci[i])
{
if (codominant<1) //selection2
update_alpha_i(i);
else
update_alpha_i_codominant(i);
for (int g=0;g<G;g++)
{
if (codominant<1)
update_eta_g_i(g,i);
else
update_eta_g_i_codominant(g,i);
}
}
}*/
if (codominant < 1) //selection2
update_alpha();
else
update_alpha_codominant();
if (codominant < 1) {
update_eta();
update_eta2();
}
else {
update_eta_codominant();
update_eta2_codominant();
}
}
// update ancestral allele frequences
if (codominant < 1) // selection2
{
update_freq_group();
update_ancestral_freq();
// update beta prior for ancestral allele frequences
// update_a_p(Application);
a_p = 1;
} else {
update_ancestral_freq_codominant();
}
// update f
//update_f();
if (codominant == 0) // selection2
update_f_random();
else if (codominant == 0.5) update_f_intensity();
if (codominant == 0.5 && !SNP_genotypes) // selection2
{
update_mu_intensity();
update_delta_intensity();
update_sigma1_intensity();
update_sigma2_intensity();
}
// update allele frequences
if (codominant == 0) // selection2
update_freq();
else if (codominant == 1)
update_freq_group_codominant();
else
update_freq_intensity();
//calculate mean and variance for alpha
if (2*k >= nb_pilot) {
for (int i = 0; i < I; i++) {
if (!discarded_loci[i]) {
mean_alpha[i] += alpha[i];
m2[i] += alpha[i] * alpha[i];
if (!convergent_switch) {
for (int g = 0; g < G; g++) {
mean_eta2[g][i] += eta2[g][i];
m2_eta2[g][i] += eta2[g][i] * eta2[g][i];
}
}
else {
for (int p = 0; p < P; p++) {
mean_eta[p][i] += eta[p][i];
m2_eta[p][i] += eta[p][i] * eta[p][i];
}
}
}
}
}
} // end of one pilot run
if (2*k>= nb_pilot) {
nb_pilot_alpha++;
if (!convergent_switch)
nb_pilot_eta2++;
else
nb_pilot_eta++;
}
// check acc_rates
for (int i = 0; i < I; i++) {
if (!discarded_loci[i]) {
// for alpha
if (acc_alpha[i] / (pilot_length) > 0.4)
var_prop_alpha[i] *= 1.2;
if (acc_alpha[i] / (pilot_length) < 0.2)
var_prop_alpha[i] /= 1.2;
if (!convergent_switch) {
// for eta2
for (int g = 0; g < G; g++) {
if (acc_eta2[g][i] / (pilot_length) > 0.4)
var_prop_eta2[g][i] *= 1.2;
if (acc_eta2[g][i] / (pilot_length) < 0.2)
var_prop_eta2[g][i] /= 1.2;
}
}
else {
for (int p = 0; p < P; p++) {
if (acc_eta[p][i] / (pilot_length) > 0.4)
var_prop_eta[p][i] *= 1.2;
if (acc_eta[p][i] / (pilot_length) < 0.2)
var_prop_eta[p][i] /= 1.2;
}
}
// for ancestral allele frequences
if (acc_freq_ancestral[i] / (pilot_length) > 0.4)
e_ancestral[i] *= 1.1;
if (acc_freq_ancestral[i] / (pilot_length) < 0.2)
e_ancestral[i] /= 1.1;
if (codominant == 0.5) // selection2
{
// for mu
if (acc_mu[i] / (pilot_length) > 0.2)
e_mu[i] *= 1.1;
if (acc_mu[i] / (pilot_length) < 0.1)
e_mu[i] /= 1.1;
// for delta
if (acc_delta[i] / (pilot_length) > 0.2)
e_delta[i] *= 1.1;
if (acc_delta[i] / (pilot_length) < 0.1)
e_delta[i] /= 1.1;
// for sigma1
if (acc_sigma1[i] / (pilot_length) > 0.4)
var_prop_sigma1[i] *= 1.2;
if (acc_sigma1[i] / (pilot_length) < 0.2)
var_prop_sigma1[i] /= 1.2;
// for sigma2
if (acc_sigma2[i] / (pilot_length) > 0.4)
var_prop_sigma2[i] *= 1.2;
if (acc_sigma2[i] / (pilot_length) < 0.2)
var_prop_sigma2[i] /= 1.2;
}
}
}
for (int j = 0; j < J; j++) {
// for theta
if (acc_theta[j] / (pilot_length) > 0.4)
var_prop_theta[j] *= 1.2;
if (acc_theta[j] / (pilot_length) < 0.2)
var_prop_theta[j] /= 1.2;
if (codominant < 1) // selection2
{
// for f
if (acc_f[j] / (pilot_length) > 0.4)
e_f[j] *= 1.1;
if (acc_f[j] / (pilot_length) < 0.2)
e_f[j] /= 1.1;
}
}
for (int g = 0; g < G; g++) {
// for beta
if (acc_beta[g] / (pilot_length) > 0.4)
var_prop_beta[g] *= 1.2;
if (acc_beta[g] / (pilot_length) < 0.2)
var_prop_beta[g] /= 1.2;
}
if (codominant < 1) // selection2
{
for (int j = 0; j < J; j++) {
for (int i = 0; i < I; i++) {
if (!discarded_loci[i]) {
// for allele frequences
if (acc_freq[i][j] / (pilot_length) > 0.5)
e_freq[i][j] *= 1.2;
if (acc_freq[i][j] / (pilot_length) < 0.3)
e_freq[i][j] /= 1.2;
}
}
}
// for a
if (acc_a_p / pilot_length > 0.4)
var_prop_a_p *= 1.2;
if (acc_a_p / pilot_length < 0.2)
var_prop_a_p /= 1.2;
}
for (int g = 0; g < G; g++) {
for (int i = 0; i < I; i++) {
if (!discarded_loci[i]) {
// for group allele frequences
if (acc_freq_group[i][g] / (pilot_length) > 0.4)
e_freq_group[i][g] *= 1.1;
if (acc_freq_group[i][g] / (pilot_length) < 0.2)
e_freq_group[i][g] /= 1.1;
}
}
}
// re-initialize acc rates
acc_a_p = 0;
if (codominant == 0.5) {
for (int i = 0; i < I; i++) // selection2
{
acc_mu[i] = 0;
acc_delta[i] = 0;
acc_sigma1[i] = 0;
acc_sigma2[i] = 0;
}
}
for (int i = 0; i < I; i++) // selection2
{
acc_alpha[i] = 0;
acc_freq_ancestral[i] = 0;
}
for (int i = 0; i < I; i++) // selection2
{
for (int g = 0; g < G; g++)
acc_eta2[g][i] = 0;
for (int p = 0; p < P; p++)
acc_eta[p][i] = 0;
}
for (int j = 0; j < J; j++) // selection2
{
acc_theta[j] = 0;
if (codominant < 1)
acc_f[j] = 0;
}
for (int g = 0; g < G; g++) // selection2
{
acc_beta[g] = 0;
}