-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcPRFCluster.cpp
More file actions
2627 lines (2225 loc) · 107 KB
/
Copy pathcPRFCluster.cpp
File metadata and controls
2627 lines (2225 loc) · 107 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
#include "cPRFCluster.h"
#include <string>
#include <list>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <thread>
using namespace std;
#ifdef __linux__
unsigned int NUM_CPU = get_nprocs_conf();
#else
unsigned int NUM_CPU = std::thread::hardware_concurrency();
#endif
/***************************************************
* Function:
* Input Parameter:
* Output:
* Return Value:
***************************************************/
//Declare and initiate variables for the function cPRFCluster
cPRFCluster::cPRFCluster() {
flag_N_pol=0; //polymorphism
flag_N_div=0; //divergence
confidence_interval=0.95;
confidence_interval=(1.0-confidence_interval)/2.0; //0.025
divergent_time=0.0;
flag_found_pr=0; //polymorphism replacement
flag_found_dr=0; //divergence replacement
flag_found_ps=0; //polymorphism sysnonymous
flag_found_ds=0; //divergence synonymous
//Initiate parameters, require inputs from the user
pol_cons_seqfile = ""; // required user input with the option '-pc file_name', the polymorphism file name with sequences in format of R, S, *.
div_cons_seqfile = ""; // required user input with the option '-dc file_name', the divergence file name with sequences in format of R, S, *.
SilentRate = 0.0;
SilentRate_flag=0;
polymorphism_num_s =""; // required user input with the option '-pn polymorphism_seq_number'.
tumor_num_s =""; // required user input with the option '-dn divergence_seq_number'.
polymorphism_num =-299;
tumor_num =-299;
output_format_num=0;
genetic_code = 1;
criterion_type = 0;
Sys_cluster=0; // By default, option for clustering Synonymous sites in the polymorphism and divergence sequence is off. Users need to use '-s 1' to turn it on.
MS_only=0;
ci_ma=0; // confidence interval for model average
Div_time=0.0; // Species divergent time; can be initiate by the user using prior species divergent time, or it will be estimated based on the given sequence, and it may be biased by the gene.
r_estimate=1; // variable for the estimated selection coefficient r.
ci_r=1; // confidence intervals for selection coefficient
ci_r_exact=0; //exact algorithm estimated r confidence interval
//ZMZ 04/28/2016 added option - regional gamma only
regional_gamma_only=0; //regional only gamma option when regional_gamma_only=1, default weighted gamma
Nuc_replace=1;
NI_estimate=0; // By default, Nuetrality Index will not be estimated. The user can turn it on with '-NI 1'.
TotalRecurrentCount=0;
TotalRecurrentSite=0;
TotalReplacementSite=0;
TotalSilentSite=0;
}
/***************************************************
* Function:
* Input Parameter:
* Output:
* Return Value:
***************************************************/
cPRFCluster::~cPRFCluster() {
div_cons_seq.clear();
div_cons_seqname.clear();
vec_r_c.clear();
vec_r_c_r.clear();
vec_rModels_c.clear();
vec_lower_r_c.clear();
vec_upper_r_c.clear();
vec_lower_r_c_r.clear();
vec_upper_r_c_r.clear();
vec_lower_rate_ds.clear();
vec_lower_rate_dr.clear();
vec_upper_rate_ds.clear();
vec_upper_rate_dr.clear();
vec_MA_rate_ds.clear();
vec_MA_rate_dr.clear();
vec_MS_rate_ds.clear();
vec_MS_rate_dr.clear();
vec_SelectedModels.clear();
vec_SelectedModels_ds.clear();
vec_SelectedModels_dr.clear();
vec_AllModels.clear();
vec_AllModels_ds.clear();
vec_AllModels_dr.clear();
}
/***************************************************
* Function: Change the size of the vectors and initiations; remove elements in the vector
* Input Parameter:
* Output:
* Return Value:
***************************************************/
int cPRFCluster::init(long N){
vec_r_c.resize(N,0.0);
vec_r_c_r.resize(N,0.0);
//vec_rModels_c.resize(N,0.0);
vec_lower_r_c.resize(N,0.0);
vec_upper_r_c.resize(N,0.0);
vec_lower_r_c_r.resize(N,0.0);
vec_upper_r_c_r.resize(N,0.0);
vec_MS_rate_ds.resize(N,0.0);
vec_MS_rate_dr.resize(N,0.0);
vec_MA_rate_ds.resize(N,0.0);
vec_MA_rate_dr.resize(N,0.0);
vec_lower_rate_ds.resize(N,0.0);
vec_lower_rate_dr.resize(N,0.0);
vec_upper_rate_ds.resize(N,0.0);
vec_upper_rate_dr.resize(N,0.0);
vec_SelectedModels_ds.clear();
vec_SelectedModels_dr.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);
return 1;
}
/***************************************************
* Function: Read the input files and execute the main function RunML, and screen output;
* Input Parameter: required divergence file names, and the number of sequences in divergence
***************************************************/
int cPRFCluster::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 {
//Write in the output file with program name,Version, LastUpdate, and Reference.
cout<<endl<<NAME<<", Version: "<<VERSION<<" [Last Update: "<<LASTUPDATE<<"]"<<endl;
cout<<"Reference: "<<REFERENCE<<endl<<endl;
static time_t time_start = time(NULL); // Record the start time
//Parse input parameters
if (parseParameter(argc, argv)!=1) throw "Error in parsing parameters!";
//Check input file names and sequence numbers for divergence
if(div_cons_seqfile=="" || tumor_num_s=="") throw "Failed to input all required files! Use -H to find out.";
//convert the format for the sequence number from string to int
tumor_num=CONVERT<int>(tumor_num_s);
cout<<"Read cancer divergence consensus input file: "<<div_cons_seqfile<<endl<<endl;
if (readFasta(div_cons_seqfile, div_cons_seqname, div_cons_seq)!=1) throw "Error in reading divergent sequences.";
//Print the divergence consensus sequences and their taxon names
cout<<endl<<"Divergence Consensus Sequences:"<<endl<<">"<<div_cons_seqname[0].c_str()<<endl;
cout<<div_cons_seq[0].c_str()<<endl<<endl<<endl;
//Check sequence length for divergence
if(div_cons_seq[0].size()%3!=0) cout<< "Warning: the length of divergence sequences can be divided by 3 (codon size)."<<endl;
//Get the recurrent position and count in Recurrents by taking recurrent.txt as an input as the seq file, flag
cout<<"Get Recurrent list: "<<endl;
RecurrentList(recurrent_file);
cout<<"***The size of the recurrent: "<<Recurrents.size()<<endl;
long jj;
for (jj=0;jj<Recurrents.size();jj++){
cout<<"RecurrentSite:\t"<<Recurrents[jj].sites<<"\tCount: "<<Recurrents[jj].counts<<endl;
int count=Recurrents[jj].counts;
TotalRecurrentCount+=count;
TotalRecurrentSite+=1;
}
//Calculate the gamma and 95% CI gamma for recurrent site, using formula 2r/(1-e^(-2r))=RecurrentNumber/(ReplacementRate*TumorNumber)
//Get the lookup table read for CI for all different k
string input_lookup_file="LookupTable_cMACPRF_CI_Recurrent_v9.dat";
LambdaCIs.clear();
LambdaCILookupTable(input_lookup_file);
cout<<"The size of the lookup LambdaCI: "<<LambdaCIs.size()<<endl;
if (LambdaCIs.size()==0) throw "The LambdaCILookupTable is empty...\n";
cout<<"Test Lambda lookup table:"<<endl;
cout<<"Recurrent count: "<<LambdaCIs[0].count<<"\t";
cout<<"LowerCI: "<<LambdaCIs[0].lowerCIlambda<<"\t";
cout<<"UpperCI: "<<LambdaCIs[0].upperCIlambda<<endl;
//**** Main Step: Run the main function Maximum Likelihood for cancer divergence sequences, to get site specific gamma and 95% CI gamma
RunML(div_cons_seq);
//Calculate the gamma and 95% CI gamma for recurrent site, using formula 2r/(1-e^(-2r))=(1-(1-p)^RecurrentNumber)/(ReplacementRate*TumorNumber)
//Display on screen after finish running the program and print out the time used.
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: Print out the cluster information for Replacement sites in the Divergence sequences by default. The cluster status for Synonymous sites in the Divergence sequences can be printed out if the user assign '-s 1' when run './cMAC-PRF'.
* Input Parameter:
* Output:
* Return Value:
***************************************************/
int cPRFCluster::output(long N){
cout<<endl<<"//Results based on model selection: "<<endl;
cout<<"****** Cancer Divergence synonymous mutation rate ucs: "<<ucs<<endl;
cout<<"****** Cancer Divergence replacement mutation rate ucr: "<<ucr<<endl;
// Print out cluster information for synonymous sites in the Divergence sequence if the user assigns '-s 1'.
if(Sys_cluster==1){
cout<<endl<<"Clusters from Divergence Synonymous:"<<endl;
if(vec_SelectedModels_ds.size()==0){
cout<<"Note: Divergence Synonymous (DS) =1 or 0. There is not enough information for clustering Divergence Synonymous!"<<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 for synonymous sites in this Divergence sequence."<<endl<<endl;
}else{
for(long i=0; i<vec_SelectedModels_ds.size(); i++){
if (output_format_num==1)
{
cout<<(vec_SelectedModels_ds[i].pos_start*Scale+1)<<" nucleotide ~ "<<(vec_SelectedModels_ds[i].pos_end*Scale+1)<<" nucleotide";
cout<<"\tCluser_Start_Position= "<<(vec_SelectedModels_ds[i].cs*Scale+1)<<"\tCluster_End_Position= "<<(vec_SelectedModels_ds[i].ce*Scale+1);
}
else if (output_format_num==0)
{
cout<<(vec_SelectedModels_ds[i].pos_start*Scale/3+1)<<" amino acid ~ "<<(vec_SelectedModels_ds[i].pos_end*Scale/3+1)<<" amino acid";
cout<<"\tCluser_Start_Position= "<<(vec_SelectedModels_ds[i].cs*Scale/3+1)<<"\tCluster_End_Position= "<<(vec_SelectedModels_ds[i].ce*Scale/3+1);
}
cout<<endl;
cout<<"InL_0= "<<vec_SelectedModels_ds[i].InL0<<"\tInL= "<<vec_SelectedModels_ds[i].InL;
cout<<"\tAIC_0= "<<vec_SelectedModels_ds[i].AIC0<<"\tAIC= "<<vec_SelectedModels_ds[i].AIC;
cout<<"\tAICc_0= "<<vec_SelectedModels_ds[i].AICc0<<"\tAICc= "<<vec_SelectedModels_ds[i].AICc;
cout<<"\tBIC_0= "<<vec_SelectedModels_ds[i].BIC0<<"\tBIC= "<<vec_SelectedModels_ds[i].BIC;
cout<<endl;
cout<<"P0_DivergenceSynonymous= "<<vec_SelectedModels_ds[i].p0<<"\tPc_DivergenceSynonymous= "<<vec_SelectedModels_ds[i].pc;
cout<<endl<<endl;
}
}
}
// Print out cluster information for Replacement sites in the Divergence sequence by default.
cout<<endl<<"Clusters from Divergence Replacement:"<<endl;
if(vec_SelectedModels_dr.size()==0){
cout<<"Note: Divergence Replacement (DR) =1 or 0. There is not enough information for clustering Divergence Replacement!"<<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 for replacement sites in this Divergence sequence."<<endl<<endl;
}else{
for(long i=0; i<vec_SelectedModels_dr.size(); i++){
if (output_format_num==1)
{
cout<<(vec_SelectedModels_dr[i].pos_start*Scale+1)<<" nucleotide ~ "<<(vec_SelectedModels_dr[i].pos_end*Scale+1)<<" nucleotide";
cout<<"\tCluster_Start_Position= "<<(vec_SelectedModels_dr[i].cs*Scale+1)<<"\tCluster_End_Position= "<<(vec_SelectedModels_dr[i].ce*Scale+1);
}
else if (output_format_num==0)
{
cout<<(vec_SelectedModels_dr[i].pos_start*Scale/3+1)<<" amino acid ~ "<<(vec_SelectedModels_dr[i].pos_end*Scale/3+1)<<" amino acid";
cout<<"\tCluster_Start_Position= "<<(vec_SelectedModels_dr[i].cs*Scale/3+1)<<"\tCluster_End_Position= "<<(vec_SelectedModels_dr[i].ce*Scale/3+1);
}
/* cout<<vec_SelectedModels_dr[i].pos_start<<" ~ "<<vec_SelectedModels_dr[i].pos_end;
cout<<"\tCluster_Start_Position= "<<vec_SelectedModels_dr[i].cs<<"\tCluster_End_Position= "<<vec_SelectedModels_dr[i].ce;
*/
cout<<endl;
cout<<"InL_0= "<<vec_SelectedModels_dr[i].InL0<<"\tInL= "<<vec_SelectedModels_dr[i].InL;
cout<<"\tAIC_0= "<<vec_SelectedModels_dr[i].AIC0<<"\tAIC= "<<vec_SelectedModels_dr[i].AIC;
cout<<"\tAICc_0= "<<vec_SelectedModels_dr[i].AICc0<<"\tAICc= "<<vec_SelectedModels_dr[i].AICc;
cout<<"\tBIC_0= "<<vec_SelectedModels_dr[i].BIC0<<"\tBIC= "<<vec_SelectedModels_dr[i].BIC;
cout<<endl;
cout<<"P0_DivergenceReplacement= "<<vec_SelectedModels_dr[i].p0<<"\tPc_DivergenceReplacement= "<<vec_SelectedModels_dr[i].pc;
cout<<endl<<endl;
}
}
if (N*Scale%3!=0) { cout<< "Warning: the length of divergence sequence can not be divided by 3 (codon size)."<<endl;}
//Print the results of Model Averaging for each position
if (MS_only==0) {
cout<<endl<<"//Results based on model averaging: "<<endl;
cout.setf(ios::left);
int width=15;
//Output the title
cout.width(width); cout<<"Position\t";
if(Sys_cluster==1){
cout.width(width); cout<<"MS_DivRep\t";
cout.width(width); cout<<"MA_DivRep\t";
cout.width(width); cout<<"MS_DivSys\t";
cout.width(width); cout<<"MA_DivSys\t";
if(ci_ma==1){
cout.width(width); cout<<"Lower_CI_DivRep\t";
cout.width(width); cout<<"Upper_CI_DivRep\t";
cout.width(width); cout<<"Lower_CI_DivSys\t";
cout.width(width); cout<<"Upper_CI_DivSys\t";
}
}
if(r_estimate==1){
cout.width(width); cout<<"Gamma_Cancer";
if(ci_r==1){
cout.width(width); cout<<"\tLower_CI_Gamma_c\t";
cout.width(width); cout<<"Upper_CI_Gamma_c";
cout.width(width); cout<<"\tMutationSymbol_Q2R1S-1*0\t";
cout.width(width); cout<<"MutationStatus";
}
}
cout<<endl;
//Output the data in the format of nucleotide/codon sequence
if (output_format_num==1 or (output_format_num==0 and Scale!=1))
{
for(long i=0; i<N; i++) {
for (long j=0;j<Scale/3;j++)
{
cout.width(width);cout<<Scale/3*i+j+1<<"\t";
if(Sys_cluster==1){
cout.width(width);cout<<vec_MS_rate_dr[i]<<"\t";
cout.width(width);cout<<vec_MA_rate_dr[i]<<"\t";
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_dr[i]<<"\t";
cout.width(width);cout<<vec_upper_rate_dr[i]<<"\t";
cout.width(width);cout<<vec_lower_rate_ds[i]<<"\t";
cout.width(width);cout<<vec_upper_rate_ds[i]<<"\t";
}
}
//r_estimate: cancer divergence gamma
if(r_estimate==1){
cout.width(width); //cancer gamma
if(vec_r_c[i]==299){
cout.width(width);cout<<"INF";
}else if (vec_r_c[i]==-299){
cout.width(width);cout<<"N-INF";
}else if (vec_r_c[i]==0 || vec_r_c[i]==-199){
cout.width(width);cout<<"NULL";
}else{
cout.width(width);cout<<vec_r_c[i];
}
//ci_r_c: cancer divergent gamma Lower and Upper confidence intervals
if(vec_lower_r_c[i]==299){
cout<<"\t";cout.width(width);cout<<"INF"<<"\t";
}else if(vec_lower_r_c[i]==-299){
cout<<"\t";cout.width(width);cout<<"N-INF"<<"\t";
}else if(vec_lower_r_c[i]==0 || vec_lower_r_c[i]==-199){
cout<<"\t";cout.width(width);cout<<"NULL"<<"\t";
}else{
cout<<"\t";cout.width(width);cout<<vec_lower_r_c[i]<<"\t";
}
if(vec_upper_r_c[i]==299){
cout.width(width);cout<<"INF";
}else if(vec_upper_r_c[i]==-299){
cout.width(width);cout<<"N-INF";
}else if(vec_upper_r_c[i]==0 || vec_upper_r_c[i]==-199){
cout.width(width);cout<<"NULL";
}else{
cout.width(width);cout<<vec_upper_r_c[i];
}
}
if (div_codon_consensus[i]=='*') { cout.width(width);cout<<"\t"<<0; }
else if (div_codon_consensus[i]=='S') { cout.width(width);cout<<"\t"<<-1; } //Re-ordered
else if (div_codon_consensus[i]=='R') { cout.width(width);cout<<"\t"<<1; }
else if (div_codon_consensus[i]=='Q') { cout.width(width);cout<<"\t"<<2; }//recurrent sites
else if (div_codon_consensus[i]=='D') { cout.width(width);cout<<"\t"<<-2; } //Added Damaging mutation records
cout<<"\t"<<div_codon_consensus[i];
cout<<endl;
}
}
cout<<endl;
}
//Default Output the data in the format of amino acids
if (output_format_num==0 and Scale==1)
{
for(long i=0; i<N/3; i++) {
for (long j=0;j<Scale;j++)
{
cout.width(width);cout<<i*Scale+j+1<<"\t";
if(Sys_cluster==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<<"\t";
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_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<<"\t";
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";
}
}
//r_estimate: human polymorphism gamma and cancer divergence gamma
if(r_estimate==1){
cout.width(width); //cancer gamma
if(vec_r_c[i*3]==299 or vec_r_c[i*3+1]==299 or vec_r_c[i*3+2]==299){
cout.width(width);cout<<"INF";
}else if (vec_r_c[i*3]==-299 or vec_r_c[i*3+1]==-299 or vec_r_c[i*3+2]==-299){
cout.width(width);cout<<"N-INF";
}else if (vec_r_c[i*3]==0 || vec_r_c[i*3]==-199 or vec_r_c[i*3+1]==0 || vec_r_c[i*3+1]==-199 or vec_r_c[i*3+2]==0 || vec_r_c[i*3+2]==-199){
cout.width(width);cout<<"NULL";
}else{
cout.width(width);cout<<(vec_r_c[i*3]+vec_r_c[i*3+1]+vec_r_c[i*3+2])/3;
}
//ci_r_c: cancer divergent gamma Lower and Upper confidence intervals
if(vec_lower_r_c[i*3]==299 or vec_lower_r_c[i*3+1]==299 or vec_lower_r_c[i*3+2]==299){
cout<<"\t";cout.width(width);cout<<"INF"<<"\t";
}else if(vec_lower_r_c[i*3]==-299 or vec_lower_r_c[i*3+1]==-299 or vec_lower_r_c[i*3+2]==-299){
cout<<"\t";cout.width(width);cout<<"N-INF"<<"\t";
}else if(vec_lower_r_c[i*3]==0 || vec_lower_r_c[i*3]==-199 or vec_lower_r_c[i*3+1]==0 || vec_lower_r_c[i*3+1]==-199 or vec_lower_r_c[i*3+2]==0 || vec_lower_r_c[i*3+2]==-199){
cout<<"\t";cout.width(width);cout<<"NULL"<<"\t";
}else{
cout<<"\t";cout.width(width);cout<<(vec_lower_r_c[i*3]+vec_lower_r_c[i*3+1]+vec_lower_r_c[i*3+2])/3<<"\t";
}
if(vec_upper_r_c[i*3]==299 or vec_upper_r_c[i*3+1]==299 or vec_upper_r_c[i*3+2]==299){
cout.width(width);cout<<"INF";
}else if(vec_upper_r_c[i*3]==-299 or vec_upper_r_c[i*3+1]==-299 or vec_upper_r_c[i*3+2]==-299){
cout.width(width);cout<<"N-INF";
}else if(vec_upper_r_c[i*3]==0 || vec_upper_r_c[i*3]==-199 or vec_upper_r_c[i*3+1]==0 || vec_upper_r_c[i*3+1]==-199 or vec_upper_r_c[i*3+2]==0 || vec_upper_r_c[i*3+2]==-199){
cout.width(width);cout<<"NULL";
}else{
cout.width(width);cout<<(vec_upper_r_c[i*3]+vec_upper_r_c[i*3+1]+vec_upper_r_c[i*3+2])/3;
}
}
if (div_codon_consensus[i*3]=='*' and div_codon_consensus[i*3+1]=='*' and div_codon_consensus[i*3+2]=='*') { cout.width(width);cout<<"\t"<<0; }
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<<"\t"<<-1; } // Reordered; S first, can be overwritten by laters
else 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<<"\t"<<1; }
else if (div_codon_consensus[i*3]=='Q' or div_codon_consensus[i*3+1]=='Q' or div_codon_consensus[i*3+2]=='Q') { cout.width(width);cout<<"\t"<<2; }
else if (div_codon_consensus[i*3]=='D' or div_codon_consensus[i*3+1]=='D' or div_codon_consensus[i*3+2]=='D') { cout.width(width);cout<<"\t"<<-2; } // Added Damaging mutation records
else { throw 1;}
if (div_codon_consensus[i*3]=='*' and div_codon_consensus[i*3+1]=='*' and div_codon_consensus[i*3+2]=='*') { cout<<"\t*"; }
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<<"\tS"; }
else if (div_codon_consensus[i*3]=='R' or div_codon_consensus[i*3+1]=='R' or div_codon_consensus[i*3+2]=='R') { cout<<"\tR"; }
else if (div_codon_consensus[i*3]=='Q' or div_codon_consensus[i*3+1]=='Q' or div_codon_consensus[i*3+2]=='Q') { cout<<"\tQ"; }
else if (div_codon_consensus[i*3]=='D' or div_codon_consensus[i*3+1]=='D' or div_codon_consensus[i*3+2]=='D') { cout<<"\tD"; }
else { throw 1;}
cout<<endl;
}
}
cout<<endl;
}
}
cout<<endl<<"Abbreviation: MS=Model Selection; MA=Model Averaging; CI=Confidence Interval; ds=Divergence Synonymous; dr=Divergence Replacement; Gamma=N*s (Gamma: scaled selection coefficient (selection intensity); s: selection coefficient); gamma >1 Negative selection, <1 Positive selection); INF=Infinite; N-INF=Negative Infinite; NULL=Not enough information for this site"<<endl;
cout<<"Abbreviation: MutationSymbol_Q2R1S-1*0: Q stands for Recurrent using 2; R stands for Replacement using 1; S stands for Silent using -1; * stands for conserved using 0."<<endl;
cout<<endl<<"#End of clustering"<<endl<<endl;
return 1;
}
/***************************************************
* Function: Main function for Clustering by Maximum likelihood for synonymous and replacement sites in the polymorphism and divergence sequences.
* Input Parameter: polymorphism sequence, divergence sequence
* Output:
* Return Value:
***************************************************/
int cPRFCluster::RunML(vector<string> div_cons_seq) {
//use only one format of input for the final version
div_codon_consensus = div_cons_seq[0]; // Get divergence sequence with synonymous (S) and replacement (R) sites labeled.
long N=div_codon_consensus.length(); //polymorphism and divergence sequence length, the two are equal.
init(N); //sequence length
//Print the #Divergence & #Polymorphism codon sequences with synonymous and replacement sites labeled with 'S' and 'R'
//Other consensus sites labeled with '*'
double ds=0.0;//divergence synonymous
double dr=0.0;//divergence replacement
// Count divergence synonymous sites 'S' by finding symbol 'S' from the divergence sequence from start position 0 to the end position N-1
ds=getDifference(div_codon_consensus,0,N-1,'S');
// Count divergence replacement sites by finding symbol 'R' from the divergence sequence from start position 0 to the end position N-1
dr=getDifference(div_codon_consensus,0,N-1,'R');
TotalReplacementSite=dr;
TotalSilentSite=ds;
cout<<"Divergence Synonymous Counts (DS): "<<ds<<endl;
cout<<"Divergence Replacement Counts (DR): "<<dr<<endl;
//Need to input the number of sequences within species used for polymorphism, polymorphism_num.
//cout<<"The number of divergence species number: "<<tumor_num<<endl;
long N_ScaledBack=N*Scale; //ScaledProbability
cout<<"The gene length: "<<N_ScaledBack<<" bp"<<endl;
//cout<<" Estimated time: "<<(12*N/1000+8)<<" minutes for this gene with 20000 models."<<endl;
cout<<" Two time-consuming steps ClusterSubSeq and CI_r_stochastic."<<endl;
//cout<<" ClusterSubSeq depends on the model number, rate: ~4 minutes for each additional 10000 models, ~6 hours for 1 million models"<<endl;
//cout<<" CI_r_stochastic is determined by gene length, with the speed rate 12 minutes per 1000 sites."<<endl;
//Get the synonymous rate from the sequence or input MutSigCV SilentRate, the latter as priority.
ucs=CancerSynonymousRate(tumor_num, N);
//double ratio_NS=CalculateNS(ref_seq_gene); // calculate the ratio of replacement and synonymous N/S
double ratio_NS= 0.345291479; //the ratio is (0.77/2.23), based on the first and third position of being nonsynonymous is 5% and 72% based on Nei and Gojobori's paper 1986
cout<<"The ratio of Nonsynonymous and Synonymous is "<<ratio_NS<<endl;
ucr=ReplacementRate(ratio_NS, ucs); // Get replacement rate for cancer divergence
cout<<"Cancer Divergence replacement mutation rate (Silent*RatioNS): "<<ucr<<endl;
//Use the positions and numbers of synonymous and replacements from the newly generated divergence sequences (ds/dr) to find the presence of cluster.
//flag_seq==0 means using the polymorphism sequence, pol_seq
int flag_seq=1; // indicating the use of divergence sequence
//// 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_div[10000];
if (N>10000) { cout<<"The length of the gene exceeds 10000, Revise the SiteModels upper-boundary array size!"<<endl; throw 1;}
// Clustering and 95% CI for the clusters, this will be calculated for Divergence Silent if (Sys_cluster==1)
if(Sys_cluster==1){
//Initialize for DS
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);
vec_MS_rate_ds.clear();
vec_MA_rate_ds.clear();
vec_MS_rate_ds.resize(N,0.0);
vec_MA_rate_ds.resize(N,0.0);
ClusterSubSeq(0, N-1,'S',flag_seq,sm_div); // Major step for clustering
vec_SelectedModels_ds=vec_SelectedModels;
vec_MS_rate_ds=vec_MS_rate;
vec_MA_rate_ds=vec_MA_rate;
if(MS_only==0 && ci_ma==1 && ds >0){
CI_MA(sm_div,N); // Major step for 95% CI of clusterings for each site
}
vec_lower_rate_ds=vec_lower_rate;
vec_upper_rate_ds=vec_upper_rate;
}
//Empty vectors and re-size for Divergence Replacement - DR
vec_SelectedModels.clear();
vec_SelectedModels_dr.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);
vec_MS_rate_dr.clear();
vec_MA_rate_dr.clear();
vec_MS_rate_dr.resize(N,0.0);
vec_MA_rate_dr.resize(N,0.0);
// **** Major Step: Find cluster and calculate probability using different models for Divergence Replacement
cout<<endl<<"***Step: Start ClusterSubSeq for cancer Replacement!***"<<endl;
time_t time_start1 = time(NULL); // Record the start time
ClusterSubSeq(0, N-1,'R',flag_seq,sm_div); //find clusters in the sequence, flag_seq=1 represents divergent sequence.
cout<<"***Step: Finish ClusterSubSeq for cancer Replacement!***"<<endl;
cout<<"ClusterSubSeq (Time elapsed: ";
time_t t2 = time(NULL)-time_start1;
int h=t2/3600, m=(t2%3600)/60, s=t2-(t2/60)*60;
if(h) cout<<h<<":"<<m<<":"<<s<<")"<<endl;
else cout<<m<<":"<<s<<")"<<endl;
vec_SelectedModels_dr=vec_SelectedModels; // all selected models for the clustering
vec_MS_rate_dr=vec_MS_rate; // selected model rate for the clustering rate
vec_MA_rate_dr=vec_MA_rate; // model averaged rate for the clustering rate
// Calculate the 95% CI for the clustering probability
if(MS_only==0 && ci_ma==1 && dr>0){
cout<<endl<<"***Step: Start CI_MA for cancer Replacement!***"<<endl;
time_t time_start1 = time(NULL); // Record the start time
CI_MA(sm_div,N);
cout<<endl<<"***Step: Finish CI_MA for cancer Replacement!***"<<endl;
cout<<"CI_MA (Time elapsed: ";
time_t t2 = time(NULL)-time_start1;
int h=t2/3600, m=(t2%3600)/60, s=t2-(t2/60)*60;
if(h) cout<<h<<":"<<m<<":"<<s<<")"<<endl;
else cout<<m<<":"<<s<<")"<<endl;
}
vec_lower_rate_dr=vec_lower_rate; // lower 95% CI rate for the clustering probability
vec_upper_rate_dr=vec_upper_rate; // upper 95% CI rate for the clustering probability
//Testing
/*
for(long i=0; i<N; i++) {
cout<<"Site:"<<i<<"\tModel selection rate: "<<vec_MS_rate_dr[i]<<"\tGamma:"<<vec_r_c[i]<<endl;
}
*/
//Do model averaging and estimate gamma for Replacement sites
if (MS_only==0 && r_estimate==1) {
//If pr & dr ==1 or 0, r couldn't be estimated.
if(dr>0){
cout<<endl<<"***Start to Estimate gamma for human cancer replacement!***"<<endl;
time_t time_start1 = time(NULL); // Record the start time
rc_SitePRF(tumor_num,ucr, N);//estimate gamma for cancer divergence
cout<<"***End to Estimate gamma for human cancer replacement!***"<<endl;
cout<<"rc_SitePRF (Time elapsed: ";
time_t t2 = time(NULL)-time_start1;
int h=t2/3600, m=(t2%3600)/60, s=t2-(t2/60)*60;
if(h) cout<<h<<":"<<m<<":"<<s<<")"<<endl;
else cout<<m<<":"<<s<<")"<<endl;
//Calculate gamma for recurrent mutation recurrent_SiteGamma(int tumor_num, double ucr, int RecurrentNum, int Site)
}else{
cout<<endl<<"*************"<<endl;
cout<<"Note: There are not enough Replacement sites for estimating gamma!"<<endl;
cout<<"*************"<<endl;
}
//calculate r Confidence Intervals using exact or stochastic algorithm.
if( dr >0 && ci_r==1){
if(ci_r_exact==1){
//exact algorithm to calculate r Confidence Intervals
cout<<endl<<"***Start Estimating Gamma CI by CIr_exact!***"<<endl;
time_t time_start1 = time(NULL); // Record the start time
CIr_exact(sm_div,N);
cout<<endl<<"***Finish Estimating Gamma CI by CIr_exact!***"<<endl;
cout<<"CIr_exact (Time elapsed: ";
time_t t2 = time(NULL)-time_start1;
int h=t2/3600, m=(t2%3600)/60, s=t2-(t2/60)*60;
if(h) cout<<h<<":"<<m<<":"<<s<<")"<<endl;
else cout<<m<<":"<<s<<")"<<endl;
}else{
//stochastic algorithm to calculate r Confidence Intervals
cout<<endl<<"***Start Estimating Gamma CI by CIr_stochastic!***"<<endl;
time_t time_start1 = time(NULL); // Record the start time
CIr_stochastic(sm_div,N);
cout<<endl<<"***Finish Estimating Gamma CI by CIr_stochastic!***"<<endl;
cout<<"CIr_stochastic (Time elapsed: ";
time_t t2 = time(NULL)-time_start1;
int h=t2/3600, m=(t2%3600)/60, s=t2-(t2/60)*60;
if(h) cout<<h<<":"<<m<<":"<<s<<")"<<endl;
else cout<<m<<":"<<s<<")"<<endl;
}
}
//for cases replacement sites in the polymorphism and divergence sequences are too few.
else if(ci_r==1){
cout<<endl<<"*************"<<endl;
cout<<"Note: There are not enough Replacement sites for estimating confidence interval of gamma!"<<endl;
cout<<"*************"<<endl;
}
}
if(MS_only==1){
cout<<endl<<"*************"<<endl;
cout<<"Warning:"<<endl<<"In terms of NO model averaging, it won't estimate selection coefficient (gamma) and its confidence intervals. Please check tutorial for more details!"<<endl;
cout<<"*************"<<endl;
}
//Calculate Recurrent gamma and 95% CI gamma, using Lookup table (Lookup table contains 95% CI Lambda (occurrence) for Lammda=k, based on poisson maximum likelihood distribution, Lambda^k*e^(-Lambda)/k!
int rs=0;
int RecurSize=Recurrents.size();
for (rs=0;rs<RecurSize;rs++)
{
int pos=Recurrents[rs].sites;
int count=Recurrents[rs].counts;
div_codon_consensus[pos]='Q'; //Record recurrent sites as Q
/* Alternative
//Calculate the gamma and 95% CI gamma for recurrent site, using formula 2r/(1-e^(-2r))=RecurrentNumber/(ReplacementRate*TumorNumber)
recurrent_SiteGamma(tumor_num, ucr, count, pos,sm_div); //calculate recurrent site gamma
double lowCI, upCI;
LambdaCIs[count-2].count=count;
lowCI=LambdaCIs[count-2].lowerCIlambda;
upCI=LambdaCIs[count-2].upperCIlambda;
recurrent_SiteGammaCI(tumor_num, ucr, lowCI, pos,0,sm_div); // 0 - lowerCI
recurrent_SiteGammaCI(tumor_num, ucr, upCI, pos,1,sm_div); //1 - upperCI
Alternative */
}
//print the output cMACPRF main results (gamma, 95% CI gamma) in the output file
output(N);
return 1;
}
/***************************************************
Subfunction - open and read the recurrent file
Input: the file name, use the same format of the Recurrent.txt
Output: recurrent list
***************************************************/
int cPRFCluster::RecurrentList(string input_f){
ifstream myfileFn2(input_f.c_str());
if (!myfileFn2) throw "Error in opening Recurrent File for RecurrentList...\n";
//Read the file; remove the empty lines at the end of the file
string str;
while ( myfileFn2.good()) {
getline(myfileFn2,str);
// cout<<"Each line: "<<str<<endl;
unsigned position1 = str.find(" => ");
if (position1!=std::string::npos)
{
//cout<<"The position1: "<<position1<<endl;
}
else {cout<<"Error! Failed to find the marker => in the recurrent file for RecurrentList!\n";}
unsigned position2=str.find(" ", position1+2);
//Get the recurrent position and recurrent count
if (position1<20 and position1>0)
{
string e1 = str.substr(0, position1);
string e2 = str.substr(position1 + 4,
str.length() - position1 - 4);
//cout<<e1<<"\t**"<<e2<<"$$\t***"<<endl;
int pos=CONVERT<int>(e1);
int count=CONVERT<int>(e2);
RecurrentSitesCounts tmp_rc(pos, count);
Recurrents.push_back(tmp_rc);
//Compact into one site for each codon
}
}
myfileFn2.close();
/*
cout<<"***The size of the recurrent: "<<Recurrents.size()<<endl;
cout<<"RecurrentSite: "<<Recurrents[0].sites<<"\tCount: ";
cout<<Recurrents[0].counts<<endl;
*/
return 1;
}
/***************************************************
* Function: Count the number for Synonymous or Replacement
* Input Parameter: seq - polymorphism or divergence sequence;start position; end position; symbol - Synonymous (S) or Replacement (R)
* Output: the number of symbols (Synonymous or Replacement)
* Return Value: the number of symbols (Synonymous or Replacement)
***************************************************/
long cPRFCluster::getDifference(string seq, int pos_start, int pos_end, char symbol) {
long i, n = 0;
for (i=pos_start; i<=pos_end; i++) {
if (seq[i]==symbol) n++;
}
if (symbol == 'S')
return n;
else if (symbol == 'R')
{
for (long i = 0; i < Recurrents.size(); i++)
{
if (Recurrents[i].sites >= pos_start and Recurrents[i].sites <= pos_end)
{
n += (Recurrents[i].counts - 1);
}
}
return n;
}
else return -1;
}
/***************************************************
* Function: calculate the likelihood of Binomial Probability i*log(p)+(n-i)*log(1-p); p=i/n
* Input Parameter: total number n and occurence i
* Output: probability
* Return Value: probability
***************************************************/
// 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]
double cPRFCluster::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;
}
double cPRFCluster::factorial(int n) {
//ZMZ added 04/26/2016
double ans = 1;
//ZMZ added 04/25/2016
if (n<1) {
cout<<"\nRecurrent Count in Factorial: "<<n<<endl;
throw "Error in Factorial with negative values from Recurrent!\n";}
for (int i = 1; i <=n; i++)
ans += log(i);
return ans;
}
double cPRFCluster::LogLikelihoodCluster(long cs, long ce, long start, long end) {
//ZMZ added 04/26/2016
double M = 0;
long N = (ce - cs + 1)*Scale;
long n = getDifference(div_codon_consensus,cs,ce,'R');
double lambda = (double)n/N;
//ZMZ added 04/27/2016
/*
if (n==0) {
lambda = ucr * tumor_num;
return lambda * N * log(lambda) - N * lambda - M; }
*/
//ZMZ 04/28/2016
if (n==0) { return 0; }
for (long i = 0; i < Recurrents.size(); i++)
{
if (Recurrents[i].sites >= cs and Recurrents[i].sites <= ce)
{
//ZMZ added 04/26/2016
int RecCount=CONVERT<int>(Recurrents[i].counts);
//n += (Recurrents[i].counts - 1); calculation moved to getDifference
M += factorial(RecCount);
//ZMZ added 04/26/2016
if (RecCount<2) {
cout<<"\nLogLikelihoodCluster: Recurrent Count:$"<<Recurrents[i].counts<<"$Converted Recurrent Count:$"<<RecCount<<"$Recurrent Factorial:$"<<M<<endl;
throw "LogLikelihoodCluster: Error in Recurrent Factorial: not integer Recurrent Count!\n";
}
}
}
double LogLikelihoodCluster_num=n * log(lambda) - N * lambda - M;
//ZMZ found problem of M 04/25/2016
//cout<<"\nLogLikelihoodCluster information: "<<"lambda:\t"<<lambda<<"\tn:\t"<<n<<"\tM:\t"<<M<<"\tlog(lambda):\t"<<log(lambda)<<"\tLogLikelihoodCluster_num:\t"<<LogLikelihoodCluster_num<<endl;
//ZMZ fixed bug 04/27/2016
return n * log(lambda) - N * lambda - M;
}
double cPRFCluster::LogLikelihoodNonCluster(long cs, long ce, long start, long end) {
//ZMZ added 04/26/2016
double M = 0;
long N = ((end-start + 1)-(ce - cs + 1))*Scale;
long n = getDifference(div_codon_consensus,start,cs-1,'R') + getDifference(div_codon_consensus,ce+1,end,'R');
double lambda = (double)n/N;
//ZMZ added 04/27/2016
/*
if (n==0) {
lambda = ucr * tumor_num;
return lambda * N * log(lambda) - N * lambda - M; }
*/
//ZMZ 04/28/2016
if (n==0) { return 0; }
for (long i = 0; i < Recurrents.size(); i++)
{
if (Recurrents[i].sites < cs and Recurrents[i].sites > ce)
{
//ZMZ added 04/26/2016
int RecCount=CONVERT<int>(Recurrents[i].counts);
//n += (Recurrents[i].counts - 1); calculation moved to getDifference
M += factorial(RecCount);
//ZMZ added 04/26/2016
if (RecCount<2) {
cout<<"\nLogLikelihoodNonCluster: Recurrent Count:$"<<Recurrents[i].counts<<"$Converted Recurrent Count:$"<<RecCount<<"$Recurrent Factorial:$"<<M<<endl;
throw "nLogLikelihoodNonCluster: Error in Recurrent Factorial: not integer Recurrent Count!\n";
}
}
}
//return n * (log(lambda) - 1) - M;
//ZMZ fixed bug 04/27/2016
return n * log(lambda) - N * lambda - M;
}
/*
bool cPRFCluster::Cmp (struct ClusterModelRecord *a, struct ClusterModelRecord *b)
{
return (a->LogLikelihood) < (b->LogLikelihood);
}
*/
/***************************************************
* Function: Determine the hot and cold spots by calculating the percentage of symbol counts in the cluster (nc/cent_len) and non-cluster regions (nw-nc)/non_cent_len.
* Input Parameter: sequence start and end as pos_start, pos_end;cluster start and end as cs and ce; probablity of cluster and non-cluster region as pc and p0; number of symbol (Synonymous or Replacement) counts in the whole sequence and in the cluster only (nw and nc)
* Output: percentage of symbols in the cluster and non-cluster regions
* Return Value:
***************************************************/
double cPRFCluster::getp0pc_MK(int pos_start, int pos_end, int cs, int ce, float &p0, float &pc, int nw, int nc) {
//p0 means the whole sequence except the cluster part.
int non_cent_len=pos_end-pos_start-ce+cs; //the length for non-cluster sequence.
int non_cent_len_ScaledBack=non_cent_len*Scale; //ScaledProbability
//if the cluster part is the whole sequence, then the non-cluster sequence will be none.
if(cs==pos_start && ce==pos_end){
p0=0.0;
}else{
//nw means the number of symbols in the whole sequence;nc means the number of symbols in the cluster region.
p0=(float)(nw-nc)/non_cent_len_ScaledBack; //percentage of symbols (Synonymous or Replacement) in the non-cluster region
}
//pc means the cluster part; nc means number of symbols (Synonymous or Replacement) in the cluster region
int cent_len=ce-cs+1; // the length for the cluster
int cent_len_ScaledBack=cent_len*Scale; //ScaledProbability
pc=(float)nc/cent_len_ScaledBack; // percentage of symbols (Synonymous or Replacement) in the cluster region
return 1;
}
/***************************************************
* Function: Find cluster and calculate probability using different models AIC, BIC, AICc
* Input Parameter: start position, end position, symbol - synonymous or replacement,flag_seq: polymorphism or divergence sequence, ?SiteModels *pointer?
* Output: vectors vec_AllModels; vec_SelectedModels
* Return Value:
***************************************************/
int cPRFCluster::ClusterSubSeq(int pos_start, int pos_end,char symbol,int flag_seq, struct SiteModels *pointer) {
time_t time_start1 = time(NULL); // Record the start time