-
Notifications
You must be signed in to change notification settings - Fork 0
/
fay_wu.cu
1737 lines (1544 loc) · 77.7 KB
/
fay_wu.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 "fay_wu.cuh"
#include "functions.cuh"
#include "prometheus.cuh"
fay_wu::fay_wu(string gene_List, string input_Folder, string ouput_Path, int cuda_ID, string intermediate_Path, int ploidy)
{
/**
* * Constructor Function
* NORMAL - GENE MODE constructor
**/
cout << "Initiating CUDA powered Fay and Wu's normalized H and E calculator" << endl
<< endl;
set_Values(gene_List, input_Folder, ouput_Path, cuda_ID, intermediate_Path, ploidy);
// this->gene_List = gene_List;
// cout << "Gene list file path\t: " << gene_List << endl
// << endl;
// this->input_Folder = input_Folder;
// this->ouput_Path = ouput_Path;
// this->intermediate_Path = intermediate_Path;
// this->ploidy = ploidy;
// 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;
}
fay_wu::fay_wu(string gene_List, string input_Folder, string ouput_Path, int cuda_ID, string intermediate_Path, int ploidy, string prometheus_Activate, string Multi_read, int number_of_genes, int CPU_cores, int SNPs_per_Run)
{
/**
* * Constructor Function
* PROMETHEUS - GENE MODE constructor
**/
// PROMETHEUS CONSTRUCTOR
cout << "Initiating CUDA powered Fay and Wu's normalized H and E calculator on PROMETHEUS" << endl
<< endl;
set_Values(gene_List, input_Folder, ouput_Path, cuda_ID, intermediate_Path, ploidy);
this->prometheus_Activate = "YES";
this->CPU_cores = CPU_cores;
this->SNPs_per_Run = SNPs_per_Run;
transform(Multi_read.begin(), Multi_read.end(), Multi_read.begin(), ::toupper);
this->Multi_read = Multi_read;
this->number_of_genes = number_of_genes;
}
fay_wu::fay_wu(string calc_Mode, int window_Size, int step_Size, string input_Folder, string ouput_Path, int cuda_ID, int ploidy, string prometheus_Activate, string Multi_read, int number_of_genes, int CPU_cores, int SNPs_per_Run)
{
/**
* * Constructor Function
* PROMETHEUS - WINDOW MODE constructor
**/
// PROMETHEUS WINDOW MODE
cout << "Initiating CUDA powered Fay and Wu's normalized H and E calculator on PROMETHEUS" << endl
<< endl;
this->calc_Mode = "WINDOW";
this->window_Size = window_Size;
this->step_Size = step_Size;
set_Values("", input_Folder, ouput_Path, cuda_ID, "", ploidy);
this->prometheus_Activate = "YES";
this->CPU_cores = CPU_cores;
this->SNPs_per_Run = SNPs_per_Run;
transform(Multi_read.begin(), Multi_read.end(), Multi_read.begin(), ::toupper);
this->Multi_read = Multi_read;
this->number_of_genes = number_of_genes;
}
fay_wu::fay_wu(string calc_Mode, int window_Size, int step_Size, string input_Folder, string ouput_Path, int cuda_ID, int ploidy)
{
/**
* * Constructor Function
* NORMAL - WINDOW MODE constructor
**/
// NORMAL WINDOW CONSTRUCTOR
cout << "Initiating CUDA powered Fay and Wu's normalized H and E calculator" << endl
<< endl;
this->calc_Mode = "WINDOW";
this->window_Size = window_Size;
this->step_Size = step_Size;
set_Values("", input_Folder, ouput_Path, cuda_ID, "", ploidy);
}
void fay_wu::set_Values(string gene_List, string input_Folder, string ouput_Path, int cuda_ID, string intermediate_Path, int ploidy)
{
/**
* This function is used in conjunction with the constructor to set the common private variables.
* Notifies the user if it is WINDOW mode or GENE (FILE) mode.
* If WINDOW user is also notified if it is sliding window or normal step wise window mode.
* Here the first call to the selected CUDA device occurs.
**/
if (this->calc_Mode != "WINDOW")
{
cout << "Calculation mode: FILE" << endl;
this->gene_List = gene_List;
cout << "Gene list file path\t: " << gene_List << endl;
}
else
{
cout << "Calculation mode: WINDOW" << endl;
cout << "Window size: " << this->window_Size << endl;
if (step_Size != 0)
{
cout << "Step size: " << this->step_Size << endl;
}
else
{
cout << "Sliding Window mode" << endl;
}
cout << endl;
}
cout << endl;
this->input_Folder = input_Folder;
this->ouput_Path = ouput_Path;
this->intermediate_Path = intermediate_Path;
this->ploidy = ploidy;
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;
}
void fay_wu::ingress()
{
/**
* Execution function.
**/
/**
* Call the "functions" class. Bespoke functions commonly used by CATE.
**/
functions function = functions();
/**
* CATE indexed VCF folder is analyzed to extract the available super populations.
* @param countries vector captures the available super populations.
* Each population is processed separately.
**/
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)
{
/**
* To reiterate each population is processed separately.
**/
cout << "Processing country\t: " << country.substr(country.find_last_of("/") + 1, country.length()) << endl
<< endl;
/**
* @param folder_Index vector captures the sorted and indexed VCF file list from the query population folder.
**/
// first: start_stop second: filename
vector<pair<string, string>> folder_Index = function.index_Folder(country);
cout << "Completed indexing folder\t: " << country << endl;
cout << endl;
/**
* The first VCF file is read to obtain information of the sample size.
* @param samples captures the sample size of the population under study.
**/
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;
/**
* @param N defines number of total sequences being present per SNP.
**/
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;
/**
* @param combinations defines number of total pairwise combinations being present.
**/
long int combinations = function.combos_N(N);
cout << "Pairwise combinations\t: " << combinations << endl;
cout << endl;
/**
* Pre-requisite values needed for determination of Fay and Wu.
**/
float an, bn, bn_plus1;
calc_Pre(an, bn, bn_plus1, N);
/**
* @param test is used by Prometheus, to tell it which test is being processed.
* T = Tajima
* FU = Fu and Li
* * FA = Fay and Wu
* N = All 3 Neutrality tests
**/
string test = "FA";
/**
* Ensures which mode is being run. GENE (FILE) mode or WINDOW mode.
**/
if (this->calc_Mode != "FILE")
{
/**
* * WINDOW mode configuration:
**/
/**
* Output file is created for the population in the output folder for WINDOW mode.
* @param output_File stores the output file's location.
* The file name is a combination of the the country, window size and step size. Sliding window files will have a step size of 0.
**/
string output_File = ouput_Path + "/" +
country.substr(country.find_last_of("/") + 1, country.length()) + "_" +
to_string(window_Size) + "_" + to_string(step_Size) +
".fw";
/**
* Ensures if PROMETHEUS is being activated.
**/
if (prometheus_Activate == "YES")
{
/**
* If Prometheus is being ACTIVATED then it is initialised accordingly.
**/
prometheus pro_Fay_Wu_Window = prometheus(output_File, window_Size, step_Size, folder_Index, Multi_read, tot_Blocks, tot_ThreadsperBlock, CPU_cores, SNPs_per_Run, number_of_genes, N, combinations, an, bn, bn_plus1);
/**
* Ensures if it is NORMAL window or SLIDING window mode.
* If step_Size is = 0 then it is sliding window mode.
**/
if (step_Size != 0)
{
/**
* Initiates processing of Fay and Wu on PROMETHEUS on step wise window mode.
**/
pro_Fay_Wu_Window.process_Window(test);
}
else
{
/**
* Initiates processing of Fay and Wu on PROMETHEUS on sliding window mode.
**/
pro_Fay_Wu_Window.process_C_sliding_Window(test);
}
}
else
{
/**
* If Prometheus is NOT being activated the window calls be done accordingly.
**/
// Prometheus OFF Window Mode
if (step_Size != 0)
{
/**
* Initiates processing of Fay and Wu on step wise window mode.
**/
window(output_File, an, bn, bn_plus1, N_float, combinations, folder_Index);
}
else
{
/**
* Initiates processing of Fay and Wu on sliding window mode.
**/
window_Sliding(output_File, an, bn, bn_plus1, N_float, combinations, folder_Index);
}
}
}
else
{
/**
* * GENE (FILE) mode configuration:
**/
fstream gene_File;
gene_File.open(gene_List, ios::in);
cout << "Processing gene list:" << endl;
/**
* Output file is created for the population in the output folder for FILE mode.
* @param output_File stores the output file's location.
* The file name is a combination of the the country, and gene file name.
**/
string output_File = ouput_Path + "/" +
country.substr(country.find_last_of("/") + 1, country.length()) + "_" +
filesystem::path(gene_List).stem().string() +
".fw";
/**
* Log file created in the intermediate folder for the population.
* @param intermediate_File stores the log file's location.
* ! This helps with the resume function. Automatically resumes from the last completely processed gene in the event of a program crash.
**/
string intermediate_File = intermediate_Path + "/" +
country.substr(country.find_last_of("/") + 1, country.length()) + "_" +
filesystem::path(gene_List).stem().string() +
".log_fw";
cout << endl;
cout << "Writing to file\t: " << output_File << endl;
cout << endl;
/**
* Initiate the reading of the gene file.
**/
if (gene_File.is_open())
{
/**
* @param gene_Combo used to capture and extract info of each gene combination.
**/
string gene_Combo;
/**
* If the output file is absent this run will be considered as a brand new run of this query and,
* the output file and the intermediate log file will be created.
**/
if (filesystem::exists(output_File) == 0)
{
function.createFile(output_File, "Gene_name\tCoordinates\tPi\tS\tTotal_iEi\tFay_Wu_Normalized_H\tFay_Wu_Normalized_E");
function.createFile(intermediate_File);
}
else
{
/**
* If the intermediate log file present then the resume process will initiated.
* This is a unintelligent resume. Essentially it matches the each read line written with the lines read from the gene file.
* The break will occur as soon as their is a mismatch.
* To counter any errors it is advised to have a new gene file name or a new intermediate folder per new run.
**/
fstream intermediate;
intermediate.open(intermediate_File, ios::in);
/**
* @param get_finished comparison variable. Used o compare the intermediate file data with that of the gene file.
**/
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);
/**
* Ensures if PROMETHEUS is being activated.
**/
// PROMETHEUS HERE
if (prometheus_Activate == "YES")
{
cout << "Initializing Prometheus:" << endl
<< endl;
/**
* If Prometheus is being ACTIVATED then it is initialised accordingly.
**/
prometheus pro_Fay_Wu = prometheus(folder_Index, Multi_read, tot_Blocks, tot_ThreadsperBlock, CPU_cores, SNPs_per_Run, number_of_genes, N, combinations, an, bn, bn_plus1);
/**
* @param gene_Collect vector is used to collect the batch of query regions to be processed by Prometheus at once.
**/
vector<string> gene_Collect;
while (getline(gene_File, gene_Combo))
{
gene_Collect.push_back(gene_Combo);
/**
* Ensures that the number of collected query regions match the user set limit to be processed at a time.
**/
if (gene_Collect.size() == number_of_genes)
{
cout << "Prometheus batch intialized" << endl;
cout << "From: " << gene_Collect[0] << endl;
cout << "To : " << gene_Collect[gene_Collect.size() - 1] << endl
<< endl;
/**
* LAUNCH Prometheus to process the collected query batch.
* @param write_Lines vector collects the lines that should be written to the output file.
*/
// launch prometheus
vector<string> write_Lines = pro_Fay_Wu.collection_Engine(gene_Collect, test);
// print
cout << "System is writing Fay and Wu results" << endl;
/**
* Outputs are written and logs are made.
**/
for (size_t i = 0; i < write_Lines.size(); i++)
{
output << write_Lines[i] << "\n";
intermediate << gene_Combo << "\n";
}
// clear prometheus
output.flush();
intermediate.flush();
pro_Fay_Wu.erase();
gene_Collect.clear();
cout << endl;
}
}
/**
* Ensures that there are no left over collected regions after finishing reading the gene file.
**/
if (gene_Collect.size() != 0)
{
/**
* If so then Prometheus is executed to process these regions.
**/
// RUN PROMETHEUS for remaining
// launch prometheus
cout << "Prometheus batch intialized" << endl;
cout << "From: " << gene_Collect[0] << endl;
cout << "To : " << gene_Collect[gene_Collect.size() - 1] << endl
<< endl;
/**
* LAUNCH Prometheus to process the collected query batch.
* @param write_Lines vector collects the lines that should be written to the output file.
*/
vector<string> write_Lines = pro_Fay_Wu.collection_Engine(gene_Collect, test);
// print
cout << "System is writing Fay and Wu results" << endl;
/**
* Outputs are written and logs are made.
**/
for (size_t i = 0; i < write_Lines.size(); i++)
{
if (write_Lines[i] != "")
{
output << write_Lines[i] << "\n";
intermediate << gene_Combo << "\n";
}
}
cout << endl;
}
output.flush();
intermediate.flush();
pro_Fay_Wu.erase();
gene_Collect.clear();
}
else
{
/**
* If Prometheus is NOT activated each query gene region in the gene file is handled individually.
* This will be suitable for low powered systems and normal users.
* Because there will be no excessive use of resources nor any requirement to have extensive knowledge of your system.
**/
while (getline(gene_File, gene_Combo))
{
/**
* @param split_Data vector captures split function's outputs on the genes information.
**/
vector<string> split_Data;
function.split(split_Data, gene_Combo, '\t');
/**
* @param gene_Name captures the gene's name.
**/
string gene_Name = split_Data[0];
cout << "Gene name\t: " << gene_Name << endl;
/**
* @param coordinates vector captures split function's outputs on gene coordinates.
* [0] = chromosome
* [1] = start position
* [2] = end position
**/
vector<string> coordinates;
function.split(coordinates, split_Data[1], ':');
/**
* @param start_Co captures query gene's start position as an integer.
**/
int start_Co = stoi(coordinates[1]);
/**
* @param end_Co captures query gene's end position as an integer.
**/
int end_Co = stoi(coordinates[2]);
cout << "Coordinates\t: Chromosome: " << coordinates[0] << " Start: " << start_Co << " End: " << end_Co << endl;
/**
* @param tot_pairwise_Differences Fay and Wu also requires the tot_pairwise_Differences in the query region to determine the average number of pairwise differences in the region.
**/
float tot_pairwise_Differences = 0;
/**
* The SNPs (Segregating sites) that fall within the query region are collected from the VCF's.
* @param collect_Segregrating_sites vector stores the collected SNPs.
**/
vector<string> collect_Segregrating_sites;
/**
* @param file_List vector is used to store the list of VCF files (found via CATES CIS algorithm) that satisfy the query region.
**/
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, start_Co, end_Co);
}
else
{
/**
* IF only one file is present in the index folder that file will be used as is.
**/
file_List.push_back(folder_Index[0].second);
}
cout << "System has retrieved all file(s)" << endl;
cout << endl;
cout << "System is collecting segregrating site(s)" << endl;
/**
* Once the required files are found they are read sequentially to get the required SNP data for processing.
**/
for (string files : file_List)
{
fstream file;
file.open(files, ios::in);
if (file.is_open())
{
string line;
/**
* The first line of each VCF is skipped as it is the header line.
**/
getline(file, line); // skip first header line
while (getline(file, line))
{
/**
* @param positions vector is used to capture the SNP data upto the position column (Column 2 (non zero count)).
**/
vector<string> positions;
function.split_getPos_ONLY(positions, line, '\t');
int pos = stoi(positions[1]);
/**
* Ensures that the query SNP's position satisfies the query region's range.
**/
if (pos >= start_Co && pos <= end_Co)
{
/**
* If the SNP is between the range of the query region, it is collected.
* Information from the SNP is extracted via the GPU.
**/
collect_Segregrating_sites.push_back(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")
// {
// string check_AF_country = country.substr(country.find_last_of("/") + 1, country.length()) + "_AF";
// float MAF_float = 0.0000;
// // collect_Segregrating_sites.push_back(line);
// for (string AF_check : info)
// {
// vector<string> split_info;
// function.split(split_info, AF_check, "=");
// if (split_info[0] == check_AF_country)
// {
// MAF_float = stof(split_info[1]);
// if (MAF_float > 0.5)
// {
// MAF_float = 1 - MAF_float;
// }
// break;
// }
// }
// tot_pairwise_Differences = tot_pairwise_Differences + (MAF_float * (1 - MAF_float) * pow(N_float, 2));
// }
}
else if (pos > end_Co)
{
/**
* If the read files query SNP exceeds the query regions range then the read loop is broken.
* This is because VCF's by nature, are sorted by position.
**/
break;
}
}
file.close();
}
}
/**
* @param num_segregrating_Sites Fay and Wu requires the total number of segregating sites/ SNPS in the query region.
**/
int num_segregrating_Sites;
string Fay_Wu_H, Fay_Wu_E;
float pi = 0.0;
int Total_iEi = 0;
/**
* Calls the function to process the segregating sites and calculate theta L required for calculating the Fay and Wu values.
**/
float theta_L = calc_theta_L(collect_Segregrating_sites, N_float, num_segregrating_Sites, Total_iEi, tot_pairwise_Differences);
cout << "Total segregating sites (S)\t: " << num_segregrating_Sites << endl;
cout << endl;
if (num_segregrating_Sites != 0)
{
float S = (float)num_segregrating_Sites;
float theta_squared = (float)(S * (S - 1)) / (pow(an, 2) + bn);
cout << "Theta_squared\t: " << theta_squared << endl;
cout << "Theta_L\t: " << theta_L << endl;
float theta_W = (float)S / an;
cout << "Theta_W\t: " << theta_W << endl;
pi = (float)tot_pairwise_Differences / combinations;
cout << "Average pairwise polymorphisms (pi)\t: " << pi << endl;
cout << endl;
float VAR_pi_MINUS_theta_L = (float)(((N_float - 2.0) / (6.0 * (N_float - 1.0))) * theta_W) + ((((18.0 * pow(N_float, 2) * ((3.0 * N_float) + 2.0) * bn_plus1) - ((88.0 * pow(N_float, 3)) + (9.0 * pow(N_float, 2)) - (13.0 * N_float) + 6.0)) / (9.0 * N_float * pow(N_float - 1, 2))) * theta_squared);
// cout << "VAR_pi_MINUS_theta_L: " << VAR_pi_MINUS_theta_L << endl;
float VAR_theta_L_MINUS_theta_W = (float)(((N_float / (2.0 * (N_float - 1.0))) - (1.0 / an)) * theta_W) + (((bn / (pow(an, 2))) + (2.0 * pow((N_float / (N_float - 1.0)), 2) * bn) - ((2.0 * ((N_float * bn) - N_float + 1.0)) / ((N_float - 1.0) * an)) - (((3.0 * N_float) + 1) / (N_float - 1.0))) * theta_squared);
// cout << "VAR_theta_L_MINUS_theta_W: " << VAR_theta_L_MINUS_theta_W << endl;
float H = (float)(pi - theta_L) / (sqrt(VAR_pi_MINUS_theta_L));
Fay_Wu_H = to_string(H);
cout << "Fay and Wu's normalized H\t: " << Fay_Wu_H << endl;
float E = (float)(theta_L - theta_W) / (sqrt(VAR_theta_L_MINUS_theta_W));
Fay_Wu_E = to_string(E);
cout << "Fay and Wu's normalized E\t: " << Fay_Wu_E << endl;
}
else
{
cout << "Fay and Wu's H and E\t: "
<< "Not Available" << endl;
Fay_Wu_H = "NA";
Fay_Wu_E = "NA";
}
cout << endl;
// Gene_name\tCoordinates\tPi\tS\tTotal_iEi\tFay_Wu_Normalized_H\tFay_Wu_Normalized_E
output << gene_Name << "\t"
<< coordinates[0] << ":" << to_string(start_Co) << ":" << to_string(end_Co)
<< "\t" << to_string(pi)
<< "\t" << to_string(num_segregrating_Sites)
<< "\t" << to_string(Total_iEi)
<< "\t" << Fay_Wu_H
<< "\t" << Fay_Wu_E << "\n";
intermediate << gene_Combo << "\n";
output.flush();
intermediate.flush();
}
}
output.close();
intermediate.close();
gene_File.close();
}
}
}
}
void fay_wu::window_Sliding(string output_File, float an, float bn, float bn_plus1, float N_float, long int combinations, vector<pair<string, string>> &folder_Index)
{
/**
* NORMAL MODE SLIDING WINDOW FUNCTION
**/
/**
* Call the "functions" class. Bespoke functions commonly used by CATE.
**/
functions function = functions();
cout << "Writing to file\t: " << output_File << endl;
cout << endl;
/**
* WINDOW functions have their own bespoke resume function that does not need an intermediate log file.
* @param file_Count_Start is used to keep track of the files that have already been processed.
* @param line_Num is used to keep track of the number of lines in that file that have already been processed.
* ! This helps with the resume function. Automatically resumes from the last completely processed gene in the event of a program crash.
**/
int file_Count_Start = 0;
int line_Num = 0;
/**
* If the output file is absent this run will be considered as a brand new run of this query and,
* the output file and the intermediate log file will be created.
**/
if (filesystem::exists(output_File) == 0)
{
/**
* Window outputs have NO gene name column.
**/
function.createFile(output_File, "Coordinates\tPi\tS\tTotal_iEi\tFay_Wu_Normalized_H\tFay_Wu_Normalized_E");
}
else
{
/**
* If the output file is already present then the resume process will initiated.
* This is a unintelligent resume. Essentially it matches the each read line written with the lines read from the gene file.
* The break will occur as soon as their is a mismatch.
* To counter any errors it is advised to have a new gene file name or a new intermediate folder per new run.
* @param found acts as a boolean variable. found = 0 if the lines need to be skipped and will equal 1 when the resume position is found.
**/
int found = 0;
/**
* Open the output file to be begin finding the resume point.
**/
fstream output_Check;
output_Check.open(output_File, ios::in);
if (output_Check.is_open())
{
/**
* @param line_Check is used to get the line from the output file to be compared.
* First line is skipped cause it is a header line containing column names.
**/
string line_Check;
getline(output_Check, line_Check); // skip first header line
/**
* We go through the files in the folder hierarchy one by one till we find the resume point.
**/
for (int file_Count = 0; file_Count < folder_Index.size(); file_Count++)
{
/**
* @param file_Path gets the path of the query file being checked.
* @param line_Current gets the line number currently being checked.
**/
string file_Path = folder_Index[file_Count].second;
fstream file;
file.open(file_Path, ios::in);
int line_Current = 0;
if (file.is_open())
{
string line;
/**
* The first line of each VCF is skipped as it is the header line.
**/
getline(file, line); // skip first header line
while (getline(file, line))
{
line_Current++;
/**
* Checks if the line being queried is a valid seg site.
* If so it is checked if it has been already processed.
**/
int VALID = function.get_Valid(line);
if (VALID != -1)
{
getline(output_Check, line_Check);
string trim = line_Check.substr(0, line_Check.find('\t'));
vector<string> positions;
function.split_getPos_ONLY(positions, line, '\t');
string pos = positions[1] + ":" + to_string((stoi(positions[1]) + window_Size));
/**
* Ensures the query line does not match that of the output
**/
if (pos != trim)
{
/**
* If they do not match,
* found is set to 1 indicating the resume position has been found and,
* the loop is broken.
**/
found = 1;
file_Count_Start = file_Count;
line_Num = line_Current;
break;
}
}
}
file.close();
}
/**
* If found is 1 that means the resume location has been found and the loop is broken.
**/
if (found == 1)
{
break;
}
}
output_Check.close();
}
}
fstream output;
output.open(output_File, ios::app);
/**
* @param line_Current is used to skip over the lines that have already been processed.
**/
int line_Current = 0;
for (int file_Count = file_Count_Start; file_Count < folder_Index.size(); file_Count++)
{
/**
* @param file_Path gets the path of the file being processed.
**/
string file_Path = folder_Index[file_Count].second;
fstream file_Main;
file_Main.open(file_Path, ios::in);
if (file_Main.is_open())
{
string line_Main;
/**
* The first line of each VCF is skipped as it is the header line.
**/
getline(file_Main, line_Main); // skip first header line
while (getline(file_Main, line_Main))
{
/**
* Skips over lines that have already been processed.
**/
if (line_Current < line_Num)
{
line_Current++;
}
else
{
// check VALID
/**
* Checks if the line being queried is a valid seg site.
* If so it is processed.
* @param VALID captures the position of the query site if it is valid, else it returns -1.
**/
int VALID = function.get_Valid(line_Main);
// cout << line_Main << endl;
if (VALID != -1)
{
/**
* @param start_Co captures the start position as an integer.
* @param end_Co captures the end position as an integer.
**/
int start_Co = VALID;
int end_Co = start_Co + window_Size;
cout << "Coordinates\t: Start: " << start_Co << " End: " << end_Co << endl;
/**
* @param tot_pairwise_Differences Fay and Wu also requires the tot_pairwise_Differences in the query region to determine the average number of pairwise differences in the region.
**/
float tot_pairwise_Differences = 0;
/**
* @param file_List vector is used to store the list of VCF files (found via CATES CIS algorithm) that satisfy the query region.
**/
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, start_Co, end_Co);
}
else
{
/**
* IF only one file is present in the index folder that file will be used as is.
**/
file_List.push_back(folder_Index[0].second);
}
cout << "System has retrieved all file(s)" << endl;
cout << "System is collecting segregrating site(s)" << endl;
/**
* The SNPs (Segregating sites) that fall within the query region are collected from the VCF's.
* @param collect_Segregrating_sites vector stores the collected SNPs.
**/
vector<string> collect_Segregrating_sites;
/**
* Once the required files are found they are read sequentially to get the required SNP data for processing.
**/
for (string files : file_List)
{
// cout << files << endl;
fstream file;
file.open(files, ios::in);
if (file.is_open())
{
string line;
/**
* The first line of each VCF is skipped as it is the header line.
**/
getline(file, line); // skip first header line
while (getline(file, line))
{
/**
* @param positions vector is used to capture the SNP data upto the position column (Column 2 (non zero count)).
**/
vector<string> positions;
function.split_getPos_ONLY(positions, line, '\t');
int pos = stoi(positions[1]);
/**
* Ensures that the query SNP's position satisfies the query region's range.
**/
if (pos >= start_Co && pos <= end_Co)
{
/**
* If the SNP is between the range of the query region, it is collected.
* Information from the SNP is extracted via the GPU.
**/
collect_Segregrating_sites.push_back(line);
}
else if (pos > end_Co)
{
/**
* If the read files query SNP exceeds the query regions range then the read loop is broken.
* This is because VCF's by nature, are sorted by position.
**/
break;
}
}
file.close();
}
}
int num_segregrating_Sites;
string Fay_Wu_H, Fay_Wu_E;
float pi = 0.0;
int Total_iEi = 0;
/**
* Calls the function to process the segregating sites and calculate theta L requried for calculating the Fay and Wu values.
**/
float theta_L = calc_theta_L(collect_Segregrating_sites, N_float, num_segregrating_Sites, Total_iEi, tot_pairwise_Differences);
cout << "Total segregating sites (S)\t: " << num_segregrating_Sites << endl;
cout << endl;
if (num_segregrating_Sites != 0)
{
float S = (float)num_segregrating_Sites;
float theta_squared = (float)(S * (S - 1)) / (pow(an, 2) + bn);
cout << "Theta_squared\t: " << theta_squared << endl;
cout << "Theta_L\t: " << theta_L << endl;
float theta_W = (float)S / an;
cout << "Theta_W\t: " << theta_W << endl;
pi = (float)tot_pairwise_Differences / combinations;
cout << "Average pairwise polymorphisms (pi)\t: " << pi << endl;
cout << endl;
float VAR_pi_MINUS_theta_L = (float)(((N_float - 2.0) / (6.0 * (N_float - 1.0))) * theta_W) + ((((18.0 * pow(N_float, 2) * ((3.0 * N_float) + 2.0) * bn_plus1) - ((88.0 * pow(N_float, 3)) + (9.0 * pow(N_float, 2)) - (13.0 * N_float) + 6.0)) / (9.0 * N_float * pow(N_float - 1, 2))) * theta_squared);
// cout << "VAR_pi_MINUS_theta_L: " << VAR_pi_MINUS_theta_L << endl;
float VAR_theta_L_MINUS_theta_W = (float)(((N_float / (2.0 * (N_float - 1.0))) - (1.0 / an)) * theta_W) + (((bn / (pow(an, 2))) + (2.0 * pow((N_float / (N_float - 1.0)), 2) * bn) - ((2.0 * ((N_float * bn) - N_float + 1.0)) / ((N_float - 1.0) * an)) - (((3.0 * N_float) + 1) / (N_float - 1.0))) * theta_squared);
// cout << "VAR_theta_L_MINUS_theta_W: " << VAR_theta_L_MINUS_theta_W << endl;
float H = (float)(pi - theta_L) / (sqrt(VAR_pi_MINUS_theta_L));
Fay_Wu_H = to_string(H);