forked from zimingz/MASSPRF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRFCluster.cpp
3431 lines (3148 loc) · 122 KB
/
PRFCluster.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
/* Updated by Zi-Ming Zhao, 07/11/2016 */
/***************************************************
* Function:
* Input Parameter:
* Output:
***************************************************/
#include "PRFCluster.h"
#ifdef __linux__
unsigned int NUM_CPU = get_nprocs_conf();
#else
unsigned int NUM_CPU = std::thread::hardware_concurrency();
#endif
PRFCluster::PRFCluster() {
flag_N_pol=0;
flag_N_div=0;
if (NUM_CPU == 0) NUM_CPU = 8;
confidence_interval=0.95;
confidence_interval=(1.0-confidence_interval)/2.0;
site_specific_flag = 0;
divtime_flag = 0;
output_format_num=0;
input_format_num=0;
species_num=0;
flag_found_pr=0;
flag_found_dr=0;
flag_found_ps=0;
flag_found_ds=0;
//Initiate parameters
pol_seqfile = "";
div_seqfile = "";
genetic_code = 1;
criterion_type = 0;
Sys_cluster=0;
MS_only=0;
ci_ma=0;
Div_time=0.0;
r_estimate=1;
ci_r=1;
ci_r_exact=0;
Nuc_replace=1;
NI_estimate=0;
modelAveraged_p_gamma=0;
scale_factor = 0;
scale_flag = 0;
}
PRFCluster::~PRFCluster() {
pol_seq.clear();
pol_seqname.clear();
div_seq.clear();
div_seqname.clear();
vec_rModels.clear();
vec_lower_r.clear();
vec_upper_r.clear();
vec_time.clear();
vec_r.clear();
vec_NI.clear();
vec_SelectedModels.clear();
vec_AllModels.clear();
vec_MA_rate.clear();
vec_lower_rate.clear();
vec_upper_rate.clear();
vec_lower_rate_ps.clear();
vec_lower_rate_pr.clear();
vec_lower_rate_ds.clear();
vec_lower_rate_dr.clear();
vec_upper_rate_ps.clear();
vec_upper_rate_pr.clear();
vec_upper_rate_ds.clear();
vec_upper_rate_dr.clear();
vec_MA_rate_ps.clear();
vec_MA_rate_pr.clear();
vec_MA_rate_ds.clear();
vec_MA_rate_dr.clear();
vec_MS_rate_ps.clear();
vec_MS_rate_pr.clear();
vec_MS_rate_ds.clear();
vec_MS_rate_dr.clear();
vec_SelectedModels_ps.clear();
vec_SelectedModels_pr.clear();
vec_SelectedModels_ds.clear();
vec_SelectedModels_dr.clear();
vec_AllModels_ps.clear();
vec_AllModels_pr.clear();
vec_AllModels_ds.clear();
vec_AllModels_dr.clear();
}
/***************************************************
* Function: Log likelihood of Bernoulli distribution. BernoulliProb= (i/n)^i*[(n-i)/n]^(n-i); prob=Log(BernoulliProb)=i*log(i/n)+(n-i)*log[(n-i)/n]
* Input Parameter:
* Output:
***************************************************/
double PRFCluster::BinomialProb(long n, long i) {
double prob = 0.0;
prob += (i==0)?0:i*log(double(i)/n);
prob += (n-i==0)?0:(n-i)*log(double(n-i)/n);
return prob;
}
/***************************************************
* Function:
* Input Parameter:
* Output:
***************************************************/
double PRFCluster::getp0pc_MK(int pos_start, int pos_end, int cs, int ce, double &p0, double &pc, int nw, int nc) {
//p0 means the whole sequence except the center part.
int non_cent_len=pos_end-pos_start-ce+cs;
if(cs==pos_start && ce==pos_end){
p0=0.0;
}else{
p0=(double)(nw-nc)/non_cent_len;
}
//pc means the center part.
int cent_len=ce-cs+1;
pc=(double)nc/cent_len;
return 1;
}
/***************************************************
* Function:
* Input Parameter:
* Output:
***************************************************/
int PRFCluster::Run(int argc, const char*argv[]) {
int i, flag=1;
////srand(1234); // to fix random number generator, to make sure the program is repeatable; need to remove for the final version of the program
try {
//Parse input parameters
if (parseParameter(argc, argv)!=1) throw 1;
if (site_specific_flag && !(Sys_cluster))
{
cout << "Can't do site_specific divergence time calculation without silent clustering -s option" <<endl;
throw 1;
}
//Check input file names
if(pol_seqfile=="" || div_seqfile=="") throw 1;
cout<<endl<<NAME<<", Version: "<<VERSION<<" ["<<LASTUPDATE<<"]"<<endl;
cout<<"Reference: "<<REFERENCE<<endl<<endl;
static time_t time_start = time(NULL);
long p_species_n=0;
//Read input seqeunces: polymorphic and divergent sequences
if (input_format_num==0){
if (readFasta(pol_seqfile, pol_seqname, pol_seq)!=1) throw "Error in reading polymorphic sequences.";
if (readFasta(div_seqfile, div_seqname, div_seq)!=1) throw "Error in reading divergent sequences.";
p_species_n=pol_seq.size();
}
else if (input_format_num==1){
if (readFastaConsensus(pol_seqfile, pol_seqname, pol_seq)!=1) throw "Error in reading polymorphic sequences.";
if (readFastaConsensus(div_seqfile, div_seqname, div_seq)!=1) throw "Error in reading divergent sequences.";
if (species_num!=0) {
p_species_n=species_num;
}
else {
cout<< "Error! The number of polymorphism sequence number has to be input when using consensus sequence!\n";
throw 1;
}
}
if (verbose==1){
//Print the sequences and their names
cout<<endl<<"Criterion used: "<<CRI(criterion_type)<<endl<<endl;
cout<<endl<<"Polymorphism data:"<<endl;
cout<<endl<<">";
for(int i=0; i<pol_seqname.size();i++){
cout<<pol_seqname[i].c_str()<<"/";
}
cout<<endl;
for(int i=0; i<pol_seq.size();i++){
cout<<pol_seq[i].c_str()<<endl;
}
cout<<endl;
cout<<endl<<"Divergence data:"<<endl;
cout<<endl<<">";
for(int i=0; i<div_seqname.size();i++){
cout<<div_seqname[i].c_str()<<"/";
}
cout<<endl;
for(int i=0; i<div_seq.size();i++){
cout<<div_seq[i].c_str()<<endl;
}
cout<<endl<<endl;
}
//Check sequences' length
for (i=0; i<pol_seq.size(); i++) {
if (div_seq[0].length()!=pol_seq[i].length()) throw "Input sequences are not equal in length.";
}
if(pol_seq[0].size()%3!=0) cout<<"Warning: the length of sequences can not be divided by 3 (codon size).\n";
//throw "Error: the length of sequences can not be divided by 3 (codon size).";
//Open and Read the lookup table for numeric integration for the specific number of species
//special attention to gamma=0,purposely put the corresponding numeric integration to 0; treat separately for this case.
if (verbose==1){
cout<<endl<<"Open and Read the lookup table for numeric integration:"<<endl;
cout<<endl<<"The number of species in the polymorphism data:"<<p_species_n<<endl;
}
int tmp_n=p_species_n; //the number of species in the polymorphism data, according to the line in the lookup table
if (p_species_n>100 and p_species_n<=1500)
{
tmp_n=98+int(p_species_n/50); //for n>100, the interval is 50 in the lookup table
}
else if (p_species_n>1500)
{
cout<<"Error! The number of sequences in the polymorphism data is over the range in the Lookup Table 1500."<<endl;
}
//cout<<"The row to be extracted in the lookup table is "<<tmp_n<<endl;
//Initialize the vectors,the size of the vector is based on the number of gamma values in the lookup table, from -50 to 50 in the interval of 0.5.
Fn1.resize(201,0.0);
Fn1_d.resize(201,0.0);
Fn2.resize(201,0.0);
Fn2_d.resize(201,0.0);
cout<<endl<<"Extract LookupTable_gx1_n_gamma_integration_50_v9.dat"<<endl;
Fn1=GammaLookupTable(tmp_n,"LookupTable_gx1_n_gamma_integration_50_v9.dat",Fn1);
cout<<endl<<"Extract LookupTable_gx1_derivative_n_gamma_50_v9.dat"<<endl;
Fn1_d=GammaLookupTable(tmp_n,"LookupTable_gx1_derivative_n_gamma_50_v9.dat",Fn1_d);
cout<<endl<<"Extract LookupTable_gx2_n_gamma_integration_50_v9.dat"<<endl;
Fn2=GammaLookupTable(tmp_n,"LookupTable_gx2_n_gamma_integration_50_v9.dat", Fn2);
cout<<endl<<"Extract LookupTable_gx2_derivative_n_gamma_50_v9.dat"<<endl;
Fn2_d=GammaLookupTable(tmp_n,"LookupTable_gx2_derivative_n_gamma_50_v9.dat",Fn2_d);
//test the sucess of assignment of values in the vectors of Fn and Fn_d
//cout<<"Test Fn1[0]: "<<Fn1[0]<<endl;
//cout<<"Test Fn1[86]: "<<Fn1[86]<<endl;
//cout<<"Test Fn1_d[0]: "<<Fn1_d[0]<<endl;
//Run the main function
RunML(pol_seq, div_seq);
//Display on screen
cout<<endl<<"Mission accomplished. (Time elapsed: ";
time_t t = time(NULL)-time_start;
int h=t/3600, m=(t%3600)/60, s=t-(t/60)*60;
if(h) cout<<h<<":"<<m<<":"<<s<<")"<<endl;
else cout<<m<<":"<<s<<")"<<endl;
}
catch (const char* e) {
cout<<e<<endl;
flag = 0;
}
catch (...) {
flag = 0;
}
return flag;
}
/***************************************************
* Function: open and read the lookup table, to extract the values for the certain species number.
* Input Parameter:the lookup table file name, p_specie_n converted tmp_n;
* Output: vector<double> Fn
***************************************************/
vector<double> PRFCluster::GammaLookupTable(int tmp_n, string input_f_name, vector<double> Fn){
double t_value; // Temporary value
string t_Fn2; // Temporary Fn
int line_n=0; // the number of the item in the file
double t_r=-199; //the Temporary gamma value
int ii=0;
ifstream myfileFn2(input_f_name.c_str());
if (!myfileFn2) throw "Error in opening GammaLookupTable...\n";
if (myfileFn2.is_open())
{
//t_r=-199;
line_n=0;
ii=0;
while ( myfileFn2.good() )
{
if (line_n>tmp_n*202 and line_n<(tmp_n+1)*202) // the number 202 is based on the number of gamma values in the lookup table, from -50 to 50 in the interval of 0.5, plus the n description column.
{
myfileFn2 >>t_value;
t_r=(ii-100.0)/2.0;
//cout<<"***r"<<t_r<<":"<<t_value<<"***\t"; // the values in the table for specific gamma
Fn[ii]=t_value;
ii+=1;
}
else
{
myfileFn2 >>t_Fn2;
}
line_n=line_n+1;
}
myfileFn2.close();
}
else
{
cout << "Unable to open file"<<endl;
}
cout <<endl<<"Vector size after extraction: "<<Fn.size()<<endl;
//cout<<"Test Fn[0]: "<<Fn[0]<<endl;
return Fn;
}
/***************************************************
* Function:
* Input Parameter:
* Output:
***************************************************/
int PRFCluster::init(long N){
vec_time.resize(N,0.0);
vec_r.resize(N,0.0);
vec_NI.resize(N,0.0);
vec_MS_rate_ps.resize(N,0.0);
vec_MS_rate_pr.resize(N,0.0);
vec_MS_rate_ds.resize(N,0.0);
vec_MS_rate_dr.resize(N,0.0);
vec_MA_rate_ps.resize(N,0.0);
vec_MA_rate_pr.resize(N,0.0);
vec_MA_rate_ds.resize(N,0.0);
vec_MA_rate_dr.resize(N,0.0);
vec_lower_rate_ps.resize(N,0.0);
vec_lower_rate_pr.resize(N,0.0);
vec_lower_rate_ds.resize(N,0.0);
vec_lower_rate_dr.resize(N,0.0);
vec_upper_rate_ps.resize(N,0.0);
vec_upper_rate_pr.resize(N,0.0);
vec_upper_rate_ds.resize(N,0.0);
vec_upper_rate_dr.resize(N,0.0);
vec_SelectedModels_ps.clear();
vec_SelectedModels_pr.clear();
vec_SelectedModels_ds.clear();
vec_SelectedModels_dr.clear();
vec_AllModels_ps.clear();
vec_AllModels_pr.clear();
vec_AllModels_ds.clear();
vec_AllModels_dr.clear();
vec_SelectedModels.clear();
vec_AllModels.clear();
vec_MA_rate.resize(N,0.0);
vec_lower_rate.resize(N,0.0);
vec_upper_rate.resize(N,0.0);
vec_rModels.clear();
vec_lower_r.resize(N,0.0);
vec_upper_r.resize(N,0.0);
return 1;
}
/***************************************************
* Function:
* Input Parameter:
* Output:
***************************************************/
int PRFCluster::output(long N){
cout<<endl<<"//Results based on model selection: "<<endl;
if(Sys_cluster==1){
cout<<endl<<"Clusters from Polymorphism Synonymous:"<<endl;
if(vec_SelectedModels_ps.size()==0){
cout<<"Note: PS=1 or 0. There is not enough information for clustering!"<<endl<<endl;
}else if(vec_SelectedModels_ps.size()==1 && vec_SelectedModels_ps[0].pos_start==vec_SelectedModels_ps[0].cs && vec_SelectedModels_ps[0].pos_end==vec_SelectedModels_ps[0].ce){
cout<<"Note: There is no cluster in this sequences for Polymorphism Synonymous"<<endl<<endl;
}else{
for(long i=0; i<vec_SelectedModels_ps.size(); i++){
//Converted the start and end positions for the cluster, the pos_start, pos_end, cs, ce according to the the option output_format_num of amino acid or nucleotide output in output()
if (output_format_num==1)
{
cout<<(vec_SelectedModels_ps[i].pos_start+1)<<" nucleotide ~ "<<(vec_SelectedModels_ps[i].pos_end+1)<<" nucleotide";
cout<<"\tcs= "<<(vec_SelectedModels_ps[i].cs+1)<<"\tce= "<<(vec_SelectedModels_ps[i].ce+1);
}
else if (output_format_num==0)
{
cout<<(vec_SelectedModels_ps[i].pos_start/3+1)<<" amino acid ~ "<<(vec_SelectedModels_ps[i].pos_end/3+1)<<" amino acid";
cout<<"\tcs= "<<(vec_SelectedModels_ps[i].cs/3+1)<<"\tce= "<<(vec_SelectedModels_ps[i].ce/3+1);
}
//cout<<vec_SelectedModels_ps[i].pos_start<<" ~ "<<vec_SelectedModels_ps[i].pos_end;
//cout<<"\tcs= "<<vec_SelectedModels_ps[i].cs<<"\tce= "<<vec_SelectedModels_ps[i].ce;
cout<<endl;
cout<<"InL0= "<<vec_SelectedModels_ps[i].InL0<<"\tInL= "<<vec_SelectedModels_ps[i].InL;
cout<<"\tAIC0= "<<vec_SelectedModels_ps[i].AIC0<<"\tAIC= "<<vec_SelectedModels_ps[i].AIC;
cout<<"\tAICc0= "<<vec_SelectedModels_ps[i].AICc0<<"\tAICc= "<<vec_SelectedModels_ps[i].AICc;
cout<<"\tBIC0= "<<vec_SelectedModels_ps[i].BIC0<<"\tBIC= "<<vec_SelectedModels_ps[i].BIC;
cout<<endl;
cout<<"P0ps= "<<vec_SelectedModels_ps[i].p0<<"\tPcps= "<<vec_SelectedModels_ps[i].pc;
cout<<endl<<endl;
}
}
}
if(Sys_cluster==1){
cout<<endl<<"Clusters from Divergence Synonymous:"<<endl;
if(vec_SelectedModels_ds.size()==0){
cout<<"Note: DS=1 or 0. There is not enough information for clustering!"<<endl<<endl;
}else if(vec_SelectedModels_ds.size()==1 && vec_SelectedModels_ds[0].pos_start==vec_SelectedModels_ds[0].cs && vec_SelectedModels_ds[0].pos_end==vec_SelectedModels_ds[0].ce){
cout<<"Note: There is no cluster in this Sequences for Divergence Synonymous"<<endl<<endl;
}else{
for(long i=0; i<vec_SelectedModels_ds.size(); i++){
//Converted the start and end positions for the cluster, the pos_start, pos_end, cs, ce according to the the option output_format_num of amino acid or nucleotide output in output()
if (output_format_num==1)
{
cout<<(vec_SelectedModels_ds[i].pos_start+1)<<" nucleotide ~ "<<(vec_SelectedModels_ds[i].pos_end+1)<<" nucleotide";
cout<<"\tcs= "<<(vec_SelectedModels_ds[i].cs+1)<<"\tce= "<<(vec_SelectedModels_ds[i].ce+1);
}
else if (output_format_num==0)
{
cout<<(vec_SelectedModels_ds[i].pos_start/3+1)<<" amino acid ~ "<<(vec_SelectedModels_ds[i].pos_end/3+1)<<" amino acid";
cout<<"\tcs= "<<(vec_SelectedModels_ds[i].cs/3+1)<<"\tce= "<<(vec_SelectedModels_ds[i].ce/3+1);
}
//cout<<vec_SelectedModels_ds[i].pos_start<<" ~ "<<vec_SelectedModels_ds[i].pos_end;
//cout<<"\tcs= "<<vec_SelectedModels_ds[i].cs<<"\tce= "<<vec_SelectedModels_ds[i].ce;
cout<<endl;
cout<<"InL0= "<<vec_SelectedModels_ds[i].InL0<<"\tInL= "<<vec_SelectedModels_ds[i].InL;
cout<<"\tAIC0= "<<vec_SelectedModels_ds[i].AIC0<<"\tAIC= "<<vec_SelectedModels_ds[i].AIC;
cout<<"\tAICc0= "<<vec_SelectedModels_ds[i].AICc0<<"\tAICc= "<<vec_SelectedModels_ds[i].AICc;
cout<<"\tBIC0= "<<vec_SelectedModels_ds[i].BIC0<<"\tBIC= "<<vec_SelectedModels_ds[i].BIC;
cout<<endl;
cout<<"P0ds= "<<vec_SelectedModels_ds[i].p0<<"\tPcds= "<<vec_SelectedModels_ds[i].pc;
cout<<endl<<endl;
}
}
}
if (site_specific_flag && vec_SelectedModels_ds.size()!=0 && vec_SelectedModels_ps.size()!=0)
{
for (long i=0;i<N;i++)
{
divergent_time[i] = divergent_time_sums[i]/divergent_time_weights[i];//model averaged divergence time for site i
}
}
else{
cout<<"Warning in site specific divergence time calculation; -ssd. There is no silent clustering for PS or DS."<<endl;
}
cout<<endl<<"Clusters from Polymorphism Replacement:"<<endl;
if(vec_SelectedModels_pr.size()==0){
cout<<"Note: PR=1 or 0. There is not enough information for clustering!"<<endl<<endl;
}else if(vec_SelectedModels_pr.size()==1 && vec_SelectedModels_pr[0].pos_start==vec_SelectedModels_pr[0].cs && vec_SelectedModels_pr[0].pos_end==vec_SelectedModels_pr[0].ce){
cout<<"Note: There is no cluster in this Sequences for Polymorphism non-Synonymous"<<endl<<endl;
}else{
for(long i=0; i<vec_SelectedModels_pr.size(); i++){
//Converted the start and end positions for the cluster, the pos_start, pos_end, cs, ce according to the the option output_format_num of amino acid or nucleotide output in output()
if (output_format_num==1)
{
cout<<(vec_SelectedModels_pr[i].pos_start+1)<<" nucleotide ~ "<<(vec_SelectedModels_pr[i].pos_end+1)<<" nucleotide";
cout<<"\tcs= "<<(vec_SelectedModels_pr[i].cs+1)<<"\tce= "<<(vec_SelectedModels_pr[i].ce+1);
}
else if (output_format_num==0)
{
cout<<(vec_SelectedModels_pr[i].pos_start/3+1)<<" amino acid ~ "<<(vec_SelectedModels_pr[i].pos_end/3+1)<<" amino acid";
cout<<"\tcs= "<<(vec_SelectedModels_pr[i].cs/3+1)<<"\tce= "<<(vec_SelectedModels_pr[i].ce/3+1);
}
//cout<<vec_SelectedModels_pr[i].pos_start<<" ~ "<<vec_SelectedModels_pr[i].pos_end;
//cout<<"\tcs= "<<vec_SelectedModels_pr[i].cs<<"\tce= "<<vec_SelectedModels_pr[i].ce;
cout<<endl;
cout<<"InL0= "<<vec_SelectedModels_pr[i].InL0<<"\tInL= "<<vec_SelectedModels_pr[i].InL;
cout<<"\tAIC0= "<<vec_SelectedModels_pr[i].AIC0<<"\tAIC= "<<vec_SelectedModels_pr[i].AIC;
cout<<"\tAICc0= "<<vec_SelectedModels_pr[i].AICc0<<"\tAICc= "<<vec_SelectedModels_pr[i].AICc;
cout<<"\tBIC0= "<<vec_SelectedModels_pr[i].BIC0<<"\tBIC= "<<vec_SelectedModels_pr[i].BIC;
cout<<endl;
cout<<"\tP0pr= "<<vec_SelectedModels_pr[i].p0<<"\tPcpr= "<<vec_SelectedModels_pr[i].pc;
cout<<endl<<endl;
}
}
cout<<endl<<"Clusters from Divergence Replacement:"<<endl;
if(vec_SelectedModels_dr.size()==0){
cout<<"Note: DR=1 or 0. There is not enough information for clustering!"<<endl<<endl;
}else if(vec_SelectedModels_dr.size()==1 && vec_SelectedModels_dr[0].pos_start==vec_SelectedModels_dr[0].cs && vec_SelectedModels_dr[0].pos_end==vec_SelectedModels_dr[0].ce){
cout<<"Note: There is no cluster in this Sequences for Divergence Replacement"<<endl<<endl;
}else{
for(long i=0; i<vec_SelectedModels_dr.size(); i++){
//Converted the start and end positions for the cluster, the pos_start, pos_end, cs, ce according to the the option output_format_num of amino acid or nucleotide output in output()
if (output_format_num==1)
{
cout<<(vec_SelectedModels_dr[i].pos_start+1)<<" nucleotide ~ "<<(vec_SelectedModels_dr[i].pos_end+1)<<" nucleotide";
cout<<"\tcs= "<<(vec_SelectedModels_dr[i].cs+1)<<"\tce= "<<(vec_SelectedModels_dr[i].ce+1);
}
else if (output_format_num==0)
{
cout<<(vec_SelectedModels_dr[i].pos_start/3+1)<<" amino acid ~ "<<(vec_SelectedModels_dr[i].pos_end/3+1)<<" amino acid";
cout<<"\tcs= "<<(vec_SelectedModels_dr[i].cs/3+1)<<"\tce= "<<(vec_SelectedModels_dr[i].ce/3+1);
}
//cout<<vec_SelectedModels_dr[i].pos_start<<" ~ "<<vec_SelectedModels_dr[i].pos_end;
//cout<<"\tcs= "<<vec_SelectedModels_dr[i].cs<<"\tce= "<<vec_SelectedModels_dr[i].ce;
cout<<endl;
cout<<"InL0= "<<vec_SelectedModels_dr[i].InL0<<"\tInL= "<<vec_SelectedModels_dr[i].InL;
cout<<"\tAIC0= "<<vec_SelectedModels_dr[i].AIC0<<"\tAIC= "<<vec_SelectedModels_dr[i].AIC;
cout<<"\tAICc0= "<<vec_SelectedModels_dr[i].AICc0<<"\tAICc= "<<vec_SelectedModels_dr[i].AICc;
cout<<"\tBIC0= "<<vec_SelectedModels_dr[i].BIC0<<"\tBIC= "<<vec_SelectedModels_dr[i].BIC;
cout<<endl;
cout<<"P0dr= "<<vec_SelectedModels_dr[i].p0<<"\tPcdr= "<<vec_SelectedModels_dr[i].pc;
cout<<endl<<endl;
}
}
if (N%3!=0) { cout<< "Warning: the length of sequence can not be divided by 3 (codon size)."<<endl;}
//Print the results of Model Averaging
if (MS_only==0) {
if (modelAveraged_p_gamma==0) {cout<<endl<<"//Results based on model averaging of gamma using different models: "<<endl;}
if (modelAveraged_p_gamma==1) {cout<<endl<<"//Results based on gamma calculated from model averaged probablity of polymorphism and divergence replacement: "<<endl;}
cout.setf(ios::left);
int width=15;
//Output the headings for the data table containing the values of gamma and other relatives.
cout.width(width); cout<<"Position\t";
if(Sys_cluster==1){
cout.width(width); cout<<"MS_PolSys\t";
cout.width(width); cout<<"MA_PolSys\t";
if(ci_ma==1){
cout.width(width); cout<<"Lower_CI_PolSys\t";
cout.width(width); cout<<"Upper_CI_PolSys\t";
}
}
cout.width(width); cout<<"MS_PolRep\t";
cout.width(width); cout<<"MA_PolRep\t";
if(ci_ma==1){
cout.width(width); cout<<"Lower_CI_PolRep\t";
cout.width(width); cout<<"Upper_CI_PolRep\t";
}
if(Sys_cluster==1){
cout.width(width); cout<<"MS_DivSys\t";
cout.width(width); cout<<"MA_DivSys\t";
if(ci_ma==1){
cout.width(width); cout<<"Lower_CI_DivSys\t";
cout.width(width); cout<<"Upper_CI_DivSys\t";
}
}
cout.width(width); cout<<"MS_DivRep\t";
cout.width(width); cout<<"MA_DivRep";
if(ci_ma==1){
cout.width(width); cout<<"\tLower_CI_DivRep\t";
cout.width(width); cout<<"Upper_CI_DivRep";
}
if(r_estimate==1){
cout.width(width); cout<<"\tDivergentTime\t";
cout.width(width); cout<<"Gamma";
if(ci_r==1){
cout.width(width); cout<<"\tLower_CI_Gamma\t";
cout.width(width); cout<<"Upper_CI_Gamma";
}
}
if(NI_estimate==1){
cout.width(width); cout<<"\tNI";
}
cout.width(width); cout<<"\tPolymorphismMutationStatus\t";
cout.width(width); cout<<"DivergenceMutationStatus";
cout<<endl;
//Output the values for the data table containing the values of gamma and other relatives.
//nucleotide/codon format output
if (output_format_num==1)
{
for(long i=0; i<N; i++) {
cout.width(width);cout<<i+1<<"\t";
//Output synonymous polymorphism model selection and model average probabilities, and confidence interval if ci_ma==1
if(Sys_cluster==1){
cout.width(width);cout<<vec_MS_rate_ps[i]<<"\t";
cout.width(width);cout<<vec_MA_rate_ps[i]<<"\t";
if(ci_ma==1){
cout.width(width);cout<<vec_lower_rate_ps[i]<<"\t";
cout.width(width);cout<<vec_upper_rate_ps[i]<<"\t";
}
}
//Output replacment polymorphism model selection and model average probabilities, and confidence interval if ci_ma==1
cout.width(width);cout<<vec_MS_rate_pr[i]<<"\t";
cout.width(width);cout<<vec_MA_rate_pr[i]<<"\t";
if(ci_ma==1){
cout.width(width);cout<<vec_lower_rate_pr[i]<<"\t";
cout.width(width);cout<<vec_upper_rate_pr[i]<<"\t";
}
//Output synonymous replacment model selection and model average probabilities, and confidence interval if ci_ma==1
if(Sys_cluster==1){
cout.width(width);cout<<vec_MS_rate_ds[i]<<"\t";
cout.width(width);cout<<vec_MA_rate_ds[i]<<"\t";
if(ci_ma==1){
cout.width(width);cout<<vec_lower_rate_ds[i]<<"\t";
cout.width(width);cout<<vec_upper_rate_ds[i]<<"\t";
}
}
//Output divergence replacement model selection and model average probabilities, and confidence interval if ci_ma==1
cout.width(width);cout<<vec_MS_rate_dr[i]<<"\t";
cout.width(width);cout<<vec_MA_rate_dr[i];
if(ci_ma==1){
cout<<"\t";cout.width(width);cout<<vec_lower_rate_dr[i]<<"\t";
cout.width(width);cout<<vec_upper_rate_dr[i];
}
//Output gamma value and confidence interval of gamma if ci_r==1
if(r_estimate==1){
cout<<"\t";cout.width(width);cout<<divergent_time[i]<<"\t";
if(vec_r[i]==50){
cout.width(width);cout<<INF;
}else if (vec_r[i]==-50){
cout.width(width);cout<<NINF;
}
else if (vec_r[i]==0 || vec_r[i]==-66){
cout.width(width);cout<<"NULL";
}
else if (vec_r[i]==-99){
cout.width(width);cout<<"NA";
}
else{
cout.width(width);cout<<vec_r[i];
}
if(ci_r==1){
if(vec_lower_r[i]==50){
cout<<"\t";cout.width(width);cout<<INF<<"\t";
}else if(vec_lower_r[i]==-50){
cout<<"\t";cout.width(width);cout<<NINF<<"\t";
}
else if(vec_lower_r[i]==0 || vec_lower_r[i]==-66){
cout<<"\t";cout.width(width);cout<<"NULL"<<"\t";
}
else if(vec_lower_r[i]==-99){
cout<<"\t";cout.width(width);cout<<"NA"<<"\t";
}
else{
cout<<"\t";cout.width(width);cout<<vec_lower_r[i]<<"\t";
}
if(vec_upper_r[i]==50){
cout.width(width);cout<<INF;
}else if(vec_upper_r[i]==-50){
cout.width(width);cout<<NINF;
}
else if(vec_upper_r[i]==0 || vec_upper_r[i]==-66){
cout.width(width);cout<<"NULL";
}
else if(vec_upper_r[i]==-99){
cout.width(width);cout<<"NA";
}
else{
cout.width(width);cout<<vec_upper_r[i];
}
}//ci_r
}//r_estimate
//Estimate Neutrality Index
if(NI_estimate==1){
if(vec_NI[i]==99){
cout<<"\t";cout.width(width);cout<<"INF";
}else if(vec_NI[i]==-99){
cout<<"\t";cout.width(width);cout<<"N-INF";
}
else if(vec_NI[i]==-66){
cout<<"\t";cout.width(width);cout<<"NULL";
}else{
cout<<"\t";cout.width(width);cout<<vec_NI[i];
}
}//NI
//Output the Replacement or Synonymous status of the mutation of polymorphism and divergence.
cout<<"\t";cout.width(width);cout<<pol_codon_consensus[i]<<"\t";
cout.width(width);cout<<div_codon_consensus[i];
cout<<endl;
}
}
//amino acid format output
else if (output_format_num==0)
{
for(long i=0; i<N/3; i++) {
cout.width(width);cout<<i+1<<"\t";
//Output synonymous polymorphism model selection and model average probabilities, and confidence interval if ci_ma==1
if(Sys_cluster==1){
cout.width(width);cout<<(vec_MS_rate_ps[i*3]+vec_MS_rate_ps[i*3+1]+vec_MS_rate_ps[i*3+2])/3<<"\t";
cout.width(width);cout<<(vec_MA_rate_ps[i*3]+vec_MA_rate_ps[i*3+1]+vec_MA_rate_ps[i*3+2])/3<<"\t";
if(ci_ma==1){
cout.width(width);cout<<(vec_lower_rate_ps[i*3]+vec_lower_rate_ps[i*3+1]+vec_lower_rate_ps[i*3+2])/3<<"\t";
cout.width(width);cout<<(vec_upper_rate_ps[i*3]+vec_upper_rate_ps[i*3+1]+vec_upper_rate_ps[i*3+2])/3<<"\t";
}
}
//Output replacment polymorphism model selection and model average probabilities, and confidence interval if ci_ma==1
cout.width(width);cout<<(vec_MS_rate_pr[i*3]+vec_MS_rate_pr[i*3+1]+vec_MS_rate_pr[i*3+2])/3<<"\t";
cout.width(width);cout<<(vec_MA_rate_pr[i*3]+vec_MA_rate_pr[i*3+1]+vec_MA_rate_pr[i*3+2])/3<<"\t";
if(ci_ma==1){
cout.width(width);cout<<(vec_lower_rate_pr[i*3]+vec_lower_rate_pr[i*3+1]+vec_lower_rate_pr[i*3+2])/3<<"\t";
cout.width(width);cout<<(vec_upper_rate_pr[i*3]+vec_upper_rate_pr[i*3+1]+vec_upper_rate_pr[i*3+2])/3<<"\t";
}
//Output synonymous replacment model selection and model average probabilities, and confidence interval if ci_ma==1
if(Sys_cluster==1){
cout.width(width);cout<<(vec_MS_rate_ds[i*3]+vec_MS_rate_ds[i*3+1]+vec_MS_rate_ds[i*3+2])/3<<"\t";
cout.width(width);cout<<(vec_MA_rate_ds[i*3]+vec_MA_rate_ds[i*3+1]+vec_MA_rate_ds[i*3+2])/3<<"\t";
if(ci_ma==1){
cout.width(width);cout<<(vec_lower_rate_ds[i*3]+vec_lower_rate_ds[i*3+1]+vec_lower_rate_ds[i*3+2])/3<<"\t";
cout.width(width);cout<<(vec_upper_rate_ds[i*3]+vec_upper_rate_ds[i*3+1]+vec_upper_rate_ds[i*3+2])/3<<"\t";
}
}
//Output divergence replacement model selection and model average probabilities, and confidence interval if ci_ma==1
cout.width(width);cout<<(vec_MS_rate_dr[i*3]+vec_MS_rate_dr[i*3+1]+vec_MS_rate_dr[i*3+2])/3<<"\t";
cout.width(width);cout<<(vec_MA_rate_dr[i*3]+vec_MA_rate_dr[i*3+1]+vec_MA_rate_dr[i*3+2])/3;
if(ci_ma==1){
cout<<"\t";cout.width(width);cout<<(vec_lower_rate_dr[i*3]+vec_lower_rate_dr[i*3+1]+vec_lower_rate_dr[i*3]+2)/3<<"\t";
cout.width(width);cout<<(vec_upper_rate_dr[i*3]+vec_upper_rate_dr[i*3+1]+vec_upper_rate_dr[i*3+2])/3;
}
//Output gamma value and confidence interval of gamma if ci_r==1
if(r_estimate==1){
cout<<"\t";cout.width(width);cout<<divergent_time[i*3]<<"\t";
if(vec_r[i*3]==50 or vec_r[i*3+1]==50 or vec_r[i*3+2]==50){
cout.width(width);cout<<"INF";
}else if (vec_r[i*3]==-50 or vec_r[i*3+1]==-50 or vec_r[i*3+2]==-50){
cout.width(width);cout<<"N-INF";
}else if (vec_r[i*3]==0 || vec_r[i*3]==-66 or vec_r[i*3+1]==0 || vec_r[i*3+1]==-66 or vec_r[i*3+2]==0 || vec_r[i*3+2]==-66 ){
cout.width(width);cout<<"NULL";
}else{
cout.width(width);cout<<(vec_r[i*3]+vec_r[i*3+1]+vec_r[i*3+2])/3;
}
if(ci_r==1){
if(vec_lower_r[i*3]==50 or vec_lower_r[i*3+1]==50 or vec_lower_r[i*3+2]==50){
cout<<"\t";cout.width(width);cout<<"INF"<<"\t";
}else if(vec_lower_r[i*3]==-50 or vec_lower_r[i*3+1]==-50 or vec_lower_r[i*3+2]==-50){
cout<<"\t";cout.width(width);cout<<"N-INF"<<"\t";
}else if(vec_lower_r[i*3]==0 || vec_lower_r[i*3]==-66 or vec_lower_r[i*3+1]==0 || vec_lower_r[i*3+1]==-66 or vec_lower_r[i*3+2]==0 || vec_lower_r[i*3+2]==-66){
cout<<"\t";cout.width(width);cout<<"NULL"<<"\t";
}else{
cout<<"\t";cout.width(width);cout<<(vec_lower_r[i*3]+vec_lower_r[i*3+1]+vec_lower_r[i*3+2])/3<<"\t";
}
if(vec_upper_r[i*3]==50 or vec_upper_r[i*3+1]==50 or vec_upper_r[i*3+2]==50){
cout.width(width);cout<<"INF";
}else if(vec_upper_r[i*3]==-50 or vec_upper_r[i*3+1]==-50 or vec_upper_r[i*3+2]==-50){
cout.width(width);cout<<"N-INF";
}else if(vec_upper_r[i*3]==0 || vec_upper_r[i*3]==-66 or vec_upper_r[i*3+1]==0 || vec_upper_r[i*3+1]==-66 or vec_upper_r[i*3+2]==0 || vec_upper_r[i*3+2]==-66){
cout.width(width);cout<<"NULL";
}else{
cout.width(width);cout<<(vec_upper_r[i*3]+vec_upper_r[i*3+1]+vec_upper_r[i*3+2])/3;
}
}//ci_r
}//r_estimate
//Estimate Neutrality Index
if(NI_estimate==1){
if(vec_NI[i*3]==99 or vec_NI[i*3+1]==99 or vec_NI[i*3+2]==99){
cout<<"\t";cout.width(width);cout<<"INF";
}else if(vec_NI[i*3]==-99 or vec_NI[i*3+1]==-99 or vec_NI[i*3+2]==-99){
cout<<"\t";cout.width(width);cout<<"N-INF";
}else if(vec_NI[i*3]==-66 or vec_NI[i*3+1]==-66 or vec_NI[i*3+2]==-66){
cout<<"\t";cout.width(width);cout<<"NULL";
}else{
cout<<"\t";cout.width(width);cout<<(vec_NI[i*3]+vec_NI[i*3+1]+vec_NI[i*3+2])/3;
}
}//NI
//Output the Replacement or Synonymous status of the mutation of polymorphism and divergence.
if (pol_codon_consensus[i*3]=='R' or pol_codon_consensus[i*3+1]=='R' or pol_codon_consensus[i*3+2]=='R') { cout<<"\tR\t"; }
else if (pol_codon_consensus[i*3]=='S' or pol_codon_consensus[i*3+1]=='S' or pol_codon_consensus[i*3+2]=='S') { cout<<"\tS\t"; }
else { cout<<"\t*\t"; }
//(pol_codon_consensus[i*3]=='*' and pol_codon_consensus[i*3+1]=='*' and pol_codon_consensus[i*3+2]=='*')
/*else { cout <<endl<<"Error in polymorphism codon consensus output, the three codon positions are not *, R or S! "<<pol_codon_consensus[i*3]<<pol_codon_consensus[i*3+1]<<pol_codon_consensus[i*3+2]<<endl;
throw 1;}
*/
if (div_codon_consensus[i*3]=='R' or div_codon_consensus[i*3+1]=='R' or div_codon_consensus[i*3+2]=='R') { cout.width(width);cout<<'R'; }
else if (div_codon_consensus[i*3]=='S' or div_codon_consensus[i*3+1]=='S' or div_codon_consensus[i*3+2]=='S') { cout.width(width);cout<<'S'; }
else { cout.width(width);cout<<'*'; }
//div_codon_consensus contains "_" in the sequence in addition to "*RS"; so program quit using else throw if not seeing *RS in output().
/*
if (div_codon_consensus[i*3]=='*' and div_codon_consensus[i*3+1]=='*' and div_codon_consensus[i*3+2]=='*') { cout.width(width);cout<<'*'; }
else { cout <<endl<<"Error in divergence codon consensus output, the three codon positions are not *, R or S! "<<div_codon_consensus[i*3]<<div_codon_consensus[i*3+1]<<div_codon_consensus[i*3+2]<<endl;
throw 1;}
*/
//cout<<"\t";cout.width(width);cout<<pol_codon_consensus[i*3]<<"\t";
//cout.width(width);cout<<div_codon_consensus[i*3];
cout<<endl;
}
}
else { throw 1;}
cout<<endl<<"Abbreviation: MS=Model Selection; MA=Model Averaging; CI=Confidence Interval; ps=Polymorphism Synonymous; pr=Polymorphism Replacement; ds=Divergence Synonymous; dr=Divergence Replacement; Gamma=N*s (Gamma: scaled selection coefficient (selection intensity); N: haploid effective population size; s: selection coefficient); NI=Neutrality Index (NI=(pr/dr)/(ps/ds), >1 Negative selection, <1 Positive selection); INF=Infinite; N-INF=Negative Infinite;"<<endl;
cout<<"NULL (Not enough information for this site or both pr=0 and dr=0); NA - gamma value is not available from our calculation."<<endl;
}
cout<<endl<<"#End of clustering"<<endl<<endl;
return 1;
}
/***************************************************
* Function:
* Input Parameter:
* Output:
***************************************************/
int PRFCluster::RunML(vector<string> pol_seq, vector<string> div_seq) {
cout<<"****** Start RunML"<<endl;
time_t time_startRunML = time(NULL); // Record the start time
long N=pol_seq[0].length();
init(N);
int q;
//if model number is not input from the user, default 10000 is used, else, use the user input Model_Number.
if (model_num_flag==0) {
Model_Number=10000;
}
pol_codon_consensus = getPolSysRep(pol_seq);
div_codon_consensus = getDivSysRep(pol_seq, div_seq);
long species_n=pol_seq.size();
if (input_format_num==1){
pol_codon_consensus = pol_seq[0];
div_codon_consensus = div_seq[0];
species_n=species_num;
}
cout<<"Polymorphism: "<<pol_codon_consensus<<endl;
cout<<"Divergence: "<<div_codon_consensus<<endl;
cout<<endl;
if(flag_N_pol==1){
cout<<endl<<"*****************"<<endl;
cout<<"Note: Nucleotide other than A, T, G or C is in the polymorphism data sequences! Please see details in manual document."<<endl;
if(Nuc_replace==1){
cout<<" It is substituted by the most frequent used nucleotide in polymorphism sequence at this site!"<<endl;
}else{
cout<<" This codon is seen as gap!"<<endl;
}
cout<<"*****************"<<endl<<endl;
}
if(flag_N_div==1){
cout<<endl<<"*****************"<<endl;
cout<<"Note: Nucleotide other than A, T, G or C is in the divergence data sequence! Please see details in manual document."<<endl;
if(Nuc_replace==1){
cout<<" It is substituted by the most frequent used nucleotide in polymorphism sequence at this site!"<<endl;
}else{
cout<<" This codon is seen as gap!"<<endl;
}
cout<<"*****************"<<endl<<endl;
}
//Print the #Div & #Pol
double ps=0.0;
double ds=0.0;
double pr=0.0;
double dr=0.0;
ps=getDifference(pol_codon_consensus,0,N-1,'S');
ds=getDifference(div_codon_consensus,0,N-1,'S');
pr=getDifference(pol_codon_consensus,0,N-1,'R');
dr=getDifference(div_codon_consensus,0,N-1,'R');
long unscaled_length = N;
cout<<"The gene length: "<<unscaled_length<<"bp"<<endl;
cout<<"PS: "<<ps<<endl;
cout<<"DS: "<<ds<<endl;
cout<<"PR: "<<pr<<endl;
cout<<"DR: "<<dr<<endl;
if(scale_flag == 0){
scale_factor = scaleFactor(pol_codon_consensus.length());
cout << "default scaling used, computed scale factor is " <<scale_factor << "\n";
}
if(scale_flag ==1 && (scale_factor == 0 || scale_factor ==1)){
cout <<"No Scaling requested \n";
}
else if (scale_flag == 1 && (scale_factor > 1)){
cout << "Scaling by supplied factor of" << scale_factor <<" \n";
}
pol_s_scaled = scaleSeq(pol_codon_consensus, scale_factor, 'S');
pol_r_scaled = scaleSeq(pol_codon_consensus,scale_factor,'R');
div_s_scaled = scaleSeq(div_codon_consensus, scale_factor,'S');
div_r_scaled = scaleSeq(div_codon_consensus, scale_factor, 'R');
long scaled_size = pol_s_scaled.size();
N = scaled_size;
double ps_scaled=0.0;
double ds_scaled=0.0;
double pr_scaled=0.0;
double dr_scaled=0.0;
ps_scaled=getDifference(pol_s_scaled,0,N-1,'S');
ds_scaled=getDifference(div_s_scaled,0,N-1,'S');
pr_scaled=getDifference(pol_r_scaled,0,N-1,'R');
dr_scaled=getDifference(div_r_scaled,0,N-1,'R');
cout << "Scaling outputs (displayed for unscaled features too):" <<endl;
cout << "Scaled size:" << scaled_size <<endl;
cout << "Polymorphism Synonymous: "<<pol_s_scaled<<endl;
cout << "Polymorphism Replacement: " << pol_r_scaled<<endl;
cout << "Divergence Synonymous: " << div_s_scaled<<endl;
cout << "Divergence Replacement: " << div_r_scaled<<endl;
cout<<endl;
cout<<"PS: "<<ps<<endl;
cout<<"DS: "<<ds<<endl;
cout<<"PR: "<<pr<<endl;
cout<<"DR: "<<dr<<endl;
cout<< endl;
//Estimate the divergence time between species using the information from whole sequence.
cout<<"Number of Individuals in Polymorphism="<<species_n<<endl;
/* cout<<"Divergent Time: "<<divergent_time<<endl;
cout<<endl;
*/
cout<<"Before Clustering (Total Time elapsed: ";
time_t t2total = time(NULL)-time_startRunML;
int h=t2total/3600, m=(t2total%3600)/60, s=t2total-(t2total/60)*60;
if(h) cout<<h<<":"<<m<<":"<<s<<")"<<endl;
else cout<<m<<":"<<s<<")"<<endl;
//Use ps/pr/ds/dr information to find the cluster from the sequences
//flag_seq==0 means using pol_s_scaled
//flag_seq==1 means using pol_r_scaled
//flag_seq==2 means using div_s_scaled
//flag_seq==3 means using div_r_scaled
int flag_seq=0;
//// Fixed bug due to the new OS X 10.9 system [error: variable length array of non-POD element type 'struct SiteModels']
////Solution: use a very large number instead of the parameter N for the gene length, for keeping all models for each gene site, to make sure the number is larger than the gene length.
// struct SiteModels sm_pol[N];
struct SiteModels sm_pol[10000];
struct SiteModels rep_pol[10000];
if (N>10000) { cout<<"The length of the gene exceeds 10000, Revise the SiteModels upper-boundary array size!"<<endl; throw 1;}
if(Sys_cluster==1){
//Initialize for PS
vec_SelectedModels.clear();
vec_MS_rate.clear();
vec_MA_rate.clear();
vec_lower_rate.clear();
vec_upper_rate.clear();
vec_MS_rate.resize(N,0.0);
vec_MA_rate.resize(N,0.0);
vec_lower_rate.resize(N,0.0);
vec_upper_rate.resize(N,0.0);
flag_seq=0;
cout<<"****** Start Clustering Polymorphism Sysnonymous"<<endl;
time_t time_start1 = time(NULL); // Record the start time
q = ClusterSubSeq(0, N-1,'S',flag_seq,sm_pol);
if (q == -1)
{
cout << "Error clustering (PS is too low!)" << endl;
throw 1;
}
cout<<"End Clustering Polymorphism Sysnonymous (Time elapsed: ";
time_t t2 = time(NULL)-time_start1;
h=t2/3600, m=(t2%3600)/60, s=t2-(t2/60)*60;
if(h) cout<<h<<":"<<m<<":"<<s<<")"<<endl;
else cout<<m<<":"<<s<<")"<<endl;
cout<<"End Clustering Polymorphism Sysnonymous (Total Time elapsed: ";
t2total = time(NULL)-time_startRunML;
h=t2total/3600, m=(t2total%3600)/60, s=t2total-(t2total/60)*60;
if(h) cout<<h<<":"<<m<<":"<<s<<")"<<endl;
else cout<<m<<":"<<s<<")"<<endl;
vec_SelectedModels_ps=vec_SelectedModels;
vec_MS_rate_ps=vec_MS_rate;
vec_MA_rate_ps=vec_MA_rate;
if(MS_only==0 && ci_ma==1 && ps>1){
cout<<"****** Start Model Averaging Polymorphism Sysnonymous"<<endl;
time_t time_start1 = time(NULL); // Record the start time
CI_MA(sm_pol,N);
cout<<"End Model Averaging Polymorphism Sysnonymous (Time elapsed: ";