-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk_test.cu
2590 lines (2242 loc) · 104 KB
/
mk_test.cu
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 "mk_test.cuh"
#include "functions.cuh"
mk_test::mk_test(string reference_Path, string alignment_Path, string gene_List, string input_Folder, string ouput_Path, int cuda_ID, string intermediate_Path, int ploidy, string genetic_Code, string start_Codons, string stop_Codons, string mode, string ORF_mode)
{
cout << "Initiating CUDA powered McDonald–Kreitman Neutrality Index (NI) test calculator" << endl
<< endl;
this->reference_Path = reference_Path;
this->alignment_Path = alignment_Path;
this->gene_List = gene_List;
cout << "Gene list file path: " << gene_List << endl;
this->input_Folder = input_Folder;
this->ouput_Path = ouput_Path;
this->intermediate_Path = intermediate_Path;
this->ploidy = ploidy;
this->mode = mode;
transform(ORF_mode.begin(), ORF_mode.end(), ORF_mode.begin(), ::toupper);
if (ORF_mode != "NO")
{
this->ORF_mode = "YES";
}
cout << "Alignment mode: " << this->mode << endl
<< "ORF's known: " << this->ORF_mode << endl
<< endl;
this->genetic_Code = genetic_Code;
this->start_Codons = start_Codons;
this->stop_Codons = stop_Codons;
cudaSetDevice(cuda_ID);
cout << "Properties of selected CUDA GPU:" << endl;
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, cuda_ID);
cout << "GPU number\t: " << cuda_ID << endl;
cout << "GPU name\t: " << prop.name << endl;
size_t l_free = 0;
size_t l_Total = 0;
cudaError_t error_id = cudaMemGetInfo(&l_free, &l_Total);
cout << "GPU memory (GB)\t: " << l_Total / (1000 * 1000 * 1000) << endl;
cout << "GPU number of multiprocessor(s)\t: " << prop.multiProcessorCount << endl;
cout << "GPU block(s) per multiprocessor\t: " << prop.maxBlocksPerMultiProcessor << endl;
this->tot_Blocks = prop.maxBlocksPerMultiProcessor;
this->tot_ThreadsperBlock = prop.maxThreadsPerBlock;
cout << "GPU thread(s) per block\t: " << tot_ThreadsperBlock << endl
<< endl;
this->primary_Intermediate_Path = this->intermediate_Path + "/" + filesystem::path(this->gene_List).stem().string();
if (filesystem::exists(primary_Intermediate_Path) == 0)
{
cout << "Creating primary intermediate index folder: " << primary_Intermediate_Path << endl;
filesystem::create_directory(primary_Intermediate_Path);
}
else
{
cout << "Primary intermediate index folder exists" << endl;
}
if (mode == "GENE")
{
string alignments = this->primary_Intermediate_Path + "/alignments";
if (filesystem::exists(alignments) == 0)
{
cout << "Creating temporary alignment index folder: " << alignments << endl;
filesystem::create_directory(alignments);
}
else
{
cout << "Temporary alignment index folder exists" << endl;
}
}
cout << endl;
}
void mk_test::ingress()
{
functions function = functions();
vector<string> Code_split;
print_Code(Code_split);
cout << "Start codon(s): " << this->start_Codons << endl;
function.split(this->start_Codons_list, this->start_Codons, ',');
function.split(this->stop_Codons_list, this->stop_Codons, ',');
string stop_Codon_All = "";
for (string stop_Codon : this->stop_Codons_list)
{
stop_Codon_All.append(stop_Codon);
}
char *stop_Codons;
stop_Codons = (char *)malloc((stop_Codon_All.size() + 1) * sizeof(char));
strcpy(stop_Codons, stop_Codon_All.c_str());
this->stop_Codon_size = stop_Codon_All.size();
cudaMallocManaged(&cuda_stop_Codons, (stop_Codon_All.size() + 1) * sizeof(char));
cudaMemcpy(cuda_stop_Codons, stop_Codons, (stop_Codon_All.size() + 1) * sizeof(char), cudaMemcpyHostToDevice);
free(stop_Codons);
cout << "Stop codon(s) : " << this->stop_Codons << endl;
cout << endl;
prepration();
// REMOVE AFTER TESTING
// exit(0);
process_Genetic_code();
process_MK();
}
void mk_test::process_Genetic_code()
{
cout << "Indexing genetic code" << endl;
functions function = functions();
string string_Gen_code = "";
vector<string> split_Aminos;
function.split(split_Aminos, this->genetic_Code, ';');
for (string amino : split_Aminos)
{
vector<string> split_Amino_codon;
vector<string> codons;
function.split(split_Amino_codon, amino, '|');
function.split(codons, split_Amino_codon[1], ',');
for (string codon : codons)
{
string_Gen_code.append(codon);
string_Gen_code.append(split_Amino_codon[0]);
}
}
char *index_Gen_code;
index_Gen_code = (char *)malloc((string_Gen_code.size() + 1) * sizeof(char));
strcpy(index_Gen_code, string_Gen_code.c_str());
this->size_of_genetic_Code = string_Gen_code.size() + 1;
this->index_Gen_code = index_Gen_code;
cout << "Genetic code indexed" << endl
<< endl;
}
void mk_test::process_MK()
{
string intermediate_Reference = this->primary_Intermediate_Path + "/" + filesystem::path(this->reference_Path).stem().string();
if (filesystem::exists(intermediate_Reference) == 0)
{
cout << "ERROR: Intermediate reference index folder, " << intermediate_Reference << " has not been found at path.\n";
}
else
{
cout << "Intermediate reference index folder present: " << intermediate_Reference << endl
<< endl;
// check if folder EXISTS
functions function = functions();
vector<string> countries = function.get_Countries(this->input_Folder);
cout << countries.size() << " populations were found: ";
for (int count = 0; count < countries.size(); count++)
{
string folder = countries[count];
cout << folder.substr(folder.find_last_of("/") + 1, folder.length());
if (count < countries.size() - 1)
{
cout << ", ";
}
}
cout << endl
<< endl;
for (string country : countries)
{
cout << "Processing country\t: " << country.substr(country.find_last_of("/") + 1, country.length()) << endl
<< endl;
vector<pair<string, string>> folder_Index = function.index_Folder(country);
cout << "Completed indexing folder\t: " << country << endl;
cout << endl;
int samples = function.getN_Split(folder_Index[0].second);
cout << "Number of samples in " << country.substr(country.find_last_of("/") + 1, country.length()) << " population\t: " << samples << endl;
int N = samples * ploidy;
float N_float = (float)N;
cout << "Number of sequences in " << country.substr(country.find_last_of("/") + 1, country.length()) << " population [ " << samples << " x " << ploidy << " ] (N)\t: " << N << endl;
long int combinations = function.combos_N(N);
cout << "Pairwise combinations\t: " << combinations << endl;
cout << endl;
fstream gene_File;
gene_File.open(gene_List, ios::in);
cout << "Processing gene list:" << endl;
string output_File = ouput_Path + "/" +
country.substr(country.find_last_of("/") + 1, country.length()) + "_" +
filesystem::path(gene_List).stem().string() +
".mc";
string intermediate_File = intermediate_Path + "/" +
country.substr(country.find_last_of("/") + 1, country.length()) + "_" +
filesystem::path(gene_List).stem().string() +
".log_mc";
cout << endl;
cout << "Writing to file\t: " << output_File << endl;
cout << endl;
if (gene_File.is_open())
{
string gene_Combo;
if (filesystem::exists(output_File) == 0)
{
function.createFile(output_File, "Gene_name\tGene_Coordinates\tORF_Coordinates\tDs\tDn\tPs\tPn\tNeutrality_Index");
function.createFile(intermediate_File);
}
else
{
fstream intermediate;
intermediate.open(intermediate_File, ios::in);
string get_finished;
while (getline(intermediate, get_finished))
{
getline(gene_File, gene_Combo);
if (gene_Combo != get_finished)
{
break;
}
}
intermediate.close();
}
fstream output;
fstream intermediate;
output.open(output_File, ios::app);
intermediate.open(intermediate_File, ios::app);
while (getline(gene_File, gene_Combo))
{
vector<string> split_Data;
function.split(split_Data, gene_Combo, '\t');
string gene_Name = split_Data[0];
cout << "Gene name\t: " << gene_Name << endl;
vector<string> coordinates;
function.split(coordinates, split_Data[1], ':');
int start_Co = stoi(coordinates[1]);
int end_Co = stoi(coordinates[2]);
cout << "Coordinates\t: Chromosome: " << coordinates[0] << " Start: " << start_Co << " End: " << end_Co << endl;
// Codon coordinates data
// check if file EXISTS.
string write_Ds, write_Dn, write_Ps, write_Pn, write_NI;
vector<string> codon_Coordinates;
string codon_Index_File_name = intermediate_Reference + "/" + gene_Name + "_" + coordinates[0] + "_" + to_string(start_Co) + "_" + to_string(end_Co) + ".ca";
if (filesystem::exists(codon_Index_File_name) == 0)
{
cout << "ERROR: Codon alignment index file not found at: " << codon_Index_File_name << endl;
cout << "ERROR: Skipping gene: " << gene_Name << endl
<< endl;
write_Ds = "NA";
write_Dn = "NA";
write_Pn = "NA";
write_Ps = "NA";
write_NI = "NA";
for (size_t i = 0; i < 3; i++)
{
codon_Coordinates.push_back("NA");
}
}
else
{
fstream get_Codon_coordinates;
get_Codon_coordinates.open(codon_Index_File_name, ios::in);
// cout << "Codon alignment index file found at " << codon_Index_File_name << endl;
string codon_Line_one;
getline(get_Codon_coordinates, codon_Line_one);
get_Codon_coordinates.close();
function.split(codon_Coordinates, codon_Line_one, '\t');
int codon_Start = stoi(codon_Coordinates[1]);
int codon_Stop = stoi(codon_Coordinates[2]);
cout << "Codon coordinates: Chromosome: " << coordinates[0] << " Start: " << codon_Start << " End: " << codon_Stop << endl;
// vector<string> collect_Segregrating_sites;
// vector<string> collect_Segregrating_POS;
vector<pair<int, string>> collect_Segregrating_site_POS;
vector<string> file_List;
cout << endl;
cout << "System is retrieving file(s)" << endl;
if (folder_Index.size() > 1)
{
file_List = function.compound_interpolationSearch(folder_Index, codon_Start, codon_Stop);
}
else
{
file_List.push_back(folder_Index[0].second);
}
cout << "System has retrieved all file(s)" << endl;
cout << endl;
cout << "System is collecting SNP site(s)" << endl;
for (string files : file_List)
{
fstream file;
file.open(files, ios::in);
if (file.is_open())
{
string line;
getline(file, line); // skip first header line
while (getline(file, line))
{
vector<string> positions;
function.split_getPos_ONLY(positions, line, '\t');
int pos = stoi(positions[1]);
if (pos >= codon_Start && pos <= codon_Stop)
{
// collect_Segregrating_sites.push_back(line);
// collect_Segregrating_POS.push_back(pos);
collect_Segregrating_site_POS.push_back(make_pair(pos, line));
// string check_0 = country.substr(country.find_last_of("/") + 1, country.length()) + "_AF=0";
// string seg_Check = "GO";
// vector<string> info;
// function.split(info, positions[7], ";");
// for (string AF_check : info)
// {
// if (AF_check == check_0)
// {
// seg_Check = "NO";
// break;
// }
// }
// if (seg_Check == "GO")
// {
// collect_Segregrating_sites.push_back(line);
// }
}
else if (pos > end_Co)
{
break;
}
}
file.close();
}
}
// cout << "System has collected " << num_segregrating_Sites << " segregrating site(s)" << endl;
// cout << endl;
int num_segregrating_Sites = 0;
// process_ORF(vector<pair<int, string>> &collect_Segregrating_site_POS, int &real_segregrating_Sites, int codon_Start, int codon_Stop, string codon_Index_File_name, int &tot_Dn, int &tot_Ds, int &tot_Pn, int &tot_Ps, float &NI)
int tot_Dn, tot_Ds, tot_Pn, tot_Ps;
float NI;
process_ORF(collect_Segregrating_site_POS, num_segregrating_Sites, codon_Start, codon_Stop, codon_Index_File_name, tot_Dn, tot_Ds, tot_Pn, tot_Ps, NI);
// calc mk syn and nonsy in cuda. Cant use MAF data cause we dont know which one is the MA
write_Dn = to_string(tot_Dn);
write_Ds = to_string(tot_Ds);
write_Pn = to_string(tot_Pn);
write_Ps = to_string(tot_Ps);
if (isnan(NI))
{
write_NI = "NA_DIV_0";
}
else
{
write_NI = to_string(NI);
}
}
//"Gene_name\tGene_Coordinates\tORF_Coordinates\tDs\tDn\tPs\tPn\tNeutrality_Index"
output << gene_Name << "\t"
<< coordinates[0] << ":" << to_string(start_Co) << ":" << to_string(end_Co) << "\t"
<< coordinates[0] << ":" << codon_Coordinates[1] << ":" << codon_Coordinates[2] << "\t"
<< write_Ds << "\t"
<< write_Dn << "\t"
<< write_Ps << "\t"
<< write_Pn << "\t"
<< write_NI << "\n";
intermediate << gene_Combo << "\n";
output.flush();
intermediate.flush();
}
output.close();
intermediate.close();
gene_File.close();
}
}
}
}
__global__ void cuda_process_SNPS(char *sites, int *index, int tot_Segregrating_sites, int *REF_Count_all, int *ALT_Count_all, char *REF_all, char *ALT_all)
{
int tid = threadIdx.x + blockIdx.x * blockDim.x;
while (tid < tot_Segregrating_sites)
{
int column = 0;
int site_Start = index[tid];
int site_End = index[tid + 1];
int i = site_Start;
char REF = 'N';
while (column < 3)
{
if (sites[i] == '\t')
{
column++;
}
i++;
}
if (sites[i] >= 97)
{
REF = sites[i] - 32;
}
else
{
REF = sites[i];
}
char ALT = 'N';
while (column < 4)
{
if (sites[i] == '\t')
{
column++;
}
i++;
}
if (sites[i] >= 97)
{
ALT = sites[i] - 32;
}
else
{
ALT = sites[i];
}
while (column < 7)
{
if (sites[i] == '\t')
{
column++;
}
i++;
}
while (column < 9)
{
if (sites[i] == '\t')
{
column++;
}
i++;
}
int ALT_count = 0;
int REF_count = 0;
while (i < site_End)
{
if (sites[i] == '1')
{
ALT_count = ALT_count + 1;
}
else if (sites[i] == '0')
{
REF_count = REF_count + 1;
}
i++;
}
REF_all[tid] = REF;
ALT_all[tid] = ALT;
REF_Count_all[tid] = REF_count;
ALT_Count_all[tid] = ALT_count;
tid += blockDim.x * gridDim.x;
}
}
__global__ void cuda_process_Codons(int codon_Number, int *positions, char *REF, char *Outgroup, char *seg_REF, char *seg_ALT, int SEG_size, int *SEG_positions, int *seg_REF_count, int *seg_ALT_count, int codon_Start, int size_of_alignment_File, int genetic_Code_size, char *index_Genetic_code, int *VALID_or_NOT, int *Ds, int *Dn, int *Ps, int *Pn)
{
int tid = threadIdx.x + blockIdx.x * blockDim.x;
// GET SEG SITE POSITIONS FROM PREVIOUS CUDA FUNCTION
while (tid < codon_Number)
{
// start with zero and change to 1 if valid
VALID_or_NOT[tid] = 0;
Ds[tid] = 0;
Dn[tid] = 0;
Ps[tid] = 0;
Pn[tid] = 0;
int start_Pos = (tid * 3) + codon_Start;
// printf("%d\n", start_Pos);
// binary search
char found = 'N';
int top = 0;
int bottom = size_of_alignment_File - 1;
int middle = top + ((bottom - top) / 2);
int pos_Value = -1;
while (top <= bottom)
{
if (positions[middle] == start_Pos)
{
pos_Value = middle;
found = 'Y';
break;
}
else if (positions[middle] < start_Pos)
{
top = middle + 1;
}
else
{
bottom = middle - 1;
}
middle = top + ((bottom - top) / 2);
}
if (found == 'Y')
{
int second_Pos = start_Pos + 1;
if (positions[pos_Value + 1] == second_Pos)
{
int third_Pos = start_Pos + 2;
if (positions[pos_Value + 2] == third_Pos)
{
// if (start_Pos == 206331091 || start_Pos == 206331100 || start_Pos == 206331118 || start_Pos == 206331190)
// {
// printf("%d\n", start_Pos);
// }
// process codon if all 3 match up
char REF_codon_pos_1 = REF[pos_Value];
char REF_codon_pos_2 = REF[pos_Value + 1];
char REF_codon_pos_3 = REF[pos_Value + 2];
char Outgroup_codon_pos_1 = Outgroup[pos_Value];
char Outgroup_codon_pos_2 = Outgroup[pos_Value + 1];
char Outgroup_codon_pos_3 = Outgroup[pos_Value + 2];
int top_SEG = 0;
int bottom_SEG = SEG_size - 1;
int middle_SEG = top_SEG + ((bottom_SEG - top_SEG) / 2);
int SEG_position_location_pos_1 = -1;
int SEG_position_location_pos_2 = -1;
int SEG_position_location_pos_3 = -1;
char seg_Found_pos_1 = 'N';
char seg_Found_pos_2 = 'N';
char seg_Found_pos_3 = 'N';
// char catch_Point = 'N';
while (top_SEG < bottom_SEG)
{
if ((SEG_positions[middle_SEG] >= start_Pos) && (SEG_positions[middle_SEG] <= third_Pos))
{
break;
}
else if (SEG_positions[middle_SEG] < start_Pos)
{
top_SEG = middle_SEG + 1;
}
else
{
bottom_SEG = middle_SEG - 1;
}
middle_SEG = top_SEG + ((bottom_SEG - top_SEG) / 2);
}
// backward
for (int i = middle_SEG; i >= 0; i--)
{
if (SEG_positions[i] == start_Pos)
{
SEG_position_location_pos_1 = i;
seg_Found_pos_1 = 'Y';
}
else if (SEG_positions[i] == second_Pos)
{
SEG_position_location_pos_2 = i;
seg_Found_pos_2 = 'Y';
}
else if (SEG_positions[i] == third_Pos)
{
SEG_position_location_pos_3 = i;
seg_Found_pos_3 = 'Y';
}
if ((SEG_positions[i] < start_Pos) || (seg_Found_pos_1 != 'Y' && seg_Found_pos_2 != 'Y' && seg_Found_pos_3 != 'Y'))
{
break;
}
}
// forward
if (seg_Found_pos_1 != 'Y' || seg_Found_pos_2 != 'Y' || seg_Found_pos_3 != 'Y')
{
// prevernt redundancy of the search space
if (seg_Found_pos_1 == 'Y' && seg_Found_pos_2 == 'N')
{
middle_SEG = seg_Found_pos_1;
}
else if (seg_Found_pos_2 == 'Y')
{
middle_SEG = seg_Found_pos_2;
}
for (size_t i = middle_SEG; i < SEG_size; i++)
{
if (SEG_positions[i] == start_Pos)
{
SEG_position_location_pos_1 = i;
seg_Found_pos_1 = 'Y';
}
else if (SEG_positions[i] == second_Pos)
{
SEG_position_location_pos_2 = i;
seg_Found_pos_2 = 'Y';
}
else if (SEG_positions[i] == third_Pos)
{
SEG_position_location_pos_3 = i;
seg_Found_pos_3 = 'Y';
}
if ((SEG_positions[i] > third_Pos) || (seg_Found_pos_1 != 'Y' && seg_Found_pos_2 != 'Y' && seg_Found_pos_3 != 'Y'))
{
break;
}
}
}
// Use first codon position to try and find second and third
// if (seg_Found_pos_1 == 'Y')
// {
// if (SEG_positions[SEG_position_location_pos_1 + 1] == second_Pos)
// {
// SEG_position_location_pos_2 = SEG_position_location_pos_1 + 1;
// seg_Found_pos_2 = 'Y';
// }
// if (SEG_positions[SEG_position_location_pos_1 + 2] == third_Pos)
// {
// SEG_position_location_pos_3 = SEG_position_location_pos_1 + 2;
// seg_Found_pos_3 = 'Y';
// }
// else if (SEG_positions[SEG_position_location_pos_1 + 1] == third_Pos)
// {
// SEG_position_location_pos_3 = SEG_position_location_pos_1 + 1;
// seg_Found_pos_3 = 'Y';
// }
// }
// if second is not present then go find second from scratch
// if (seg_Found_pos_2 == 'N')
// {
// if (start_Pos == 206331100)
// {
// printf("%d\n", second_Pos);
// }
// top_SEG = 0;
// bottom_SEG = SEG_size - 1;
// // top + ((bottom - top) / 2)
// middle_SEG = top_SEG + ((bottom_SEG - top_SEG) / 2);
// while (top_SEG < bottom_SEG)
// {
// if (SEG_positions[middle_SEG] == second_Pos)
// {
// SEG_position_location_pos_2 = middle_SEG;
// seg_Found_pos_2 = 'Y';
// break;
// }
// else if (SEG_positions[middle_SEG] < second_Pos)
// {
// top_SEG = middle_SEG + 1;
// }
// else
// {
// bottom_SEG = middle_SEG - 1;
// }
// middle_SEG = top_SEG + ((bottom_SEG - top_SEG) / 2);
// }
// }
// // if second is present use it to find third if third is not already found
// if ((seg_Found_pos_2 == 'Y') && (seg_Found_pos_3 == 'N'))
// {
// if (SEG_positions[SEG_position_location_pos_2 + 1] == third_Pos)
// {
// SEG_position_location_pos_2 = SEG_position_location_pos_2 + 1;
// seg_Found_pos_3 = 'Y';
// }
// }
// // if neither second nor first can be used to find third , find third from scratch
// if (seg_Found_pos_3 == 'N')
// {
// top_SEG = 0;
// bottom_SEG = SEG_size - 1;
// middle_SEG = top_SEG + ((bottom_SEG - top_SEG) / 2);
// while (top_SEG < bottom_SEG)
// {
// if (SEG_positions[middle_SEG] == third_Pos)
// {
// SEG_position_location_pos_3 = middle_SEG;
// seg_Found_pos_3 = 'Y';
// break;
// }
// else if (SEG_positions[middle_SEG] < third_Pos)
// {
// top_SEG = middle_SEG + 1;
// }
// else
// {
// bottom_SEG = middle_SEG - 1;
// }
// middle_SEG = top_SEG + ((bottom_SEG - top_SEG) / 2);
// }
// }
// get the counts needed for NI
// get amino acid for REF, Outgroup and Seg
char Seg_codon_pos_1;
char Seg_codon_pos_2;
char Seg_codon_pos_3;
if (seg_Found_pos_1 == 'Y')
{
if (seg_ALT_count[SEG_position_location_pos_1] == 0)
{
Seg_codon_pos_1 = REF_codon_pos_1;
}
else
{
Seg_codon_pos_1 = seg_ALT[SEG_position_location_pos_1];
}
// printf("REF: %c \t REF_VCF: %c \n", REF_codon_pos_1, seg_REF[SEG_position_location_pos_1]);
}
else
{
Seg_codon_pos_1 = REF_codon_pos_1;
}
if (seg_Found_pos_2 == 'Y')
{
if (seg_ALT_count[SEG_position_location_pos_2] == 0)
{
Seg_codon_pos_2 = REF_codon_pos_2;
}
else
{
Seg_codon_pos_2 = seg_ALT[SEG_position_location_pos_2];
}
}
else
{
// if (start_Pos == 206331100)
// {
// printf("%d\n", second_Pos);
// }
Seg_codon_pos_2 = REF_codon_pos_2;
}
if (seg_Found_pos_3 == 'Y')
{
if (seg_ALT_count[SEG_position_location_pos_3] == 0)
{
Seg_codon_pos_3 = REF_codon_pos_3;
}
else
{
Seg_codon_pos_3 = seg_ALT[SEG_position_location_pos_3];
}
}
else
{
Seg_codon_pos_3 = REF_codon_pos_3;
}
// check for single allele mutation
int num_of_mutations = 0;
if (REF_codon_pos_1 != Outgroup_codon_pos_1 || REF_codon_pos_1 != Seg_codon_pos_1 || Outgroup_codon_pos_1 != Seg_codon_pos_1)
{
num_of_mutations = num_of_mutations + 1;
}
if (REF_codon_pos_2 != Outgroup_codon_pos_2 || REF_codon_pos_2 != Seg_codon_pos_2 || Outgroup_codon_pos_2 != Seg_codon_pos_2)
{
num_of_mutations = num_of_mutations + 1;
}
if (REF_codon_pos_3 != Outgroup_codon_pos_3 || REF_codon_pos_3 != Seg_codon_pos_3 || Outgroup_codon_pos_3 != Seg_codon_pos_3)
{
num_of_mutations = num_of_mutations + 1;
}
// process only if pos in the codon is mutatated
if (num_of_mutations != 0)
{
VALID_or_NOT[tid] = 1;
// add check to ensure that all translations are found
// char REF_amino_acid = '0';
// char Outgroup_amino_acid = '0';
// char Seg_amino_acid = '0';
// char REF_found = 'N';
// char Outgroup_found = 'N';
// char Seg_found = 'N';
// for (int i = 0; i < this->genetic_Code_size; i = i + 4)
// {
// // cout << this->index_Genetic_code[i]
// // << this->index_Genetic_code[i + 1]
// // << this->index_Genetic_code[i + 2]
// // << "\t" << this->index_Genetic_code[i + 3] << "\n";
// if (REF_codon_pos_1 == index_Genetic_code[i] && REF_codon_pos_2 == index_Genetic_code[i + 1] && REF_codon_pos_3 == index_Genetic_code[i + 2])
// {
// REF_amino_acid = index_Genetic_code[i + 3];
// REF_found = 'Y';
// }
// if (Outgroup_codon_pos_1 == index_Genetic_code[i] && Outgroup_codon_pos_2 == index_Genetic_code[i + 1] && Outgroup_codon_pos_3 == index_Genetic_code[i + 2])
// {
// Outgroup_amino_acid = index_Genetic_code[i + 3];
// Outgroup_found == 'Y';
// }
// if (Seg_codon_pos_1 == index_Genetic_code[i] && Seg_codon_pos_1 == index_Genetic_code[i + 1] && Seg_codon_pos_1 == index_Genetic_code[i + 2])
// {
// Seg_amino_acid = index_Genetic_code[i + 3];
// Seg_found == 'Y'
// }
// if (REF_found = 'Y' && Outgroup_found == 'Y' && Seg_found == 'Y')
// {
// break;
// }
// }
// count dn ds pn ps
// POSITION 1
if (REF_codon_pos_1 == Seg_codon_pos_1)
{
if (REF_codon_pos_1 != Outgroup_codon_pos_1)
{
// fixed between
char REF_amino_acid = '0';
char Outgroup_amino_acid = '0';
char REF_found = 'N';
char Outgroup_found = 'N';
for (int i = 0; i < genetic_Code_size; i = i + 4)
{
if (REF_codon_pos_1 == index_Genetic_code[i] && REF_codon_pos_2 == index_Genetic_code[i + 1] && REF_codon_pos_3 == index_Genetic_code[i + 2])
{
REF_amino_acid = index_Genetic_code[i + 3];
REF_found = 'Y';
}
if (Outgroup_codon_pos_1 == index_Genetic_code[i] && REF_codon_pos_2 == index_Genetic_code[i + 1] && REF_codon_pos_3 == index_Genetic_code[i + 2])
{
Outgroup_amino_acid = index_Genetic_code[i + 3];
Outgroup_found = 'Y';
}
if (REF_found == 'Y' && Outgroup_found == 'Y')
{
break;
}
}
if (REF_found == 'Y' && Outgroup_found == 'Y')
{
if (REF_amino_acid == Outgroup_amino_acid)
{
// fixed synonymous
Ds[tid] = Ds[tid] + 1;
}
else
{
Dn[tid] = Dn[tid] + 1;
}
}
else
{
if (REF_found == 'N')
{
printf("ERROR: REFERENCE CODON %c %c %c NOT FOUND\n", REF_codon_pos_1, REF_codon_pos_2, REF_codon_pos_3);
}
if (Outgroup_found == 'N')
{
printf("ERROR: OUTGROUP CODON %c %c %c NOT FOUND\n", Outgroup_codon_pos_1, REF_codon_pos_2, REF_codon_pos_3);
}
}
}
}
if (REF_codon_pos_1 != Seg_codon_pos_1)
{
// polymmorphism within
char REF_amino_acid = '0';
char Seg_amino_acid = '0';
char REF_found = 'N';
char Seg_found = 'N';
for (int i = 0; i < genetic_Code_size; i = i + 4)
{
if (REF_codon_pos_1 == index_Genetic_code[i] && REF_codon_pos_2 == index_Genetic_code[i + 1] && REF_codon_pos_3 == index_Genetic_code[i + 2])
{
REF_amino_acid = index_Genetic_code[i + 3];
REF_found = 'Y';
}
if (Seg_codon_pos_1 == index_Genetic_code[i] && REF_codon_pos_2 == index_Genetic_code[i + 1] && REF_codon_pos_3 == index_Genetic_code[i + 2])
{
Seg_amino_acid = index_Genetic_code[i + 3];
Seg_found = 'Y';
}
if (REF_found == 'Y' && Seg_found == 'Y')
{
break;
}
}
if (REF_found == 'Y' && Seg_found == 'Y')
{
// printf("%d\n", start_Pos);
if (REF_amino_acid == Seg_amino_acid)
{
Ps[tid] = Ps[tid] + 1;
}
else
{
Pn[tid] = Pn[tid] + 1;
}
}
else
{
if (REF_found == 'N')
{
printf("ERROR: REFERENCE CODON %c %c %c NOT FOUND\n", REF_codon_pos_1, REF_codon_pos_2, REF_codon_pos_3);
}
if (Seg_found == 'N')
{
printf("ERROR: OUTGROUP CODON %c %c %c NOT FOUND\n", Seg_codon_pos_1, REF_codon_pos_2, REF_codon_pos_3);
}
}
}
// POSITION 2
if (REF_codon_pos_2 == Seg_codon_pos_2)
{
if (REF_codon_pos_2 != Outgroup_codon_pos_2)
{
// fixed between
char REF_amino_acid = '0';
char Outgroup_amino_acid = '0';
char REF_found = 'N';
char Outgroup_found = 'N';
for (int i = 0; i < genetic_Code_size; i = i + 4)
{
if (REF_codon_pos_1 == index_Genetic_code[i] && REF_codon_pos_2 == index_Genetic_code[i + 1] && REF_codon_pos_3 == index_Genetic_code[i + 2])
{
REF_amino_acid = index_Genetic_code[i + 3];
REF_found = 'Y';
}
if (REF_codon_pos_1 == index_Genetic_code[i] && Outgroup_codon_pos_2 == index_Genetic_code[i + 1] && REF_codon_pos_3 == index_Genetic_code[i + 2])
{
Outgroup_amino_acid = index_Genetic_code[i + 3];
Outgroup_found = 'Y';