-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathehh.cu
2685 lines (2252 loc) · 95.7 KB
/
ehh.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 "functions.cuh"
#include "ehh.cuh"
ehh::ehh(string range_Mode, string file_Mode_path, string fixed_Mode_value, string input_Folder, string output_Path, int cuda_ID, string intermediate_Path, int ploidy, int default_SNP_count, int EHH_CPU_cores, int default_SNP_BP_count)
{
cout << "Initiating CUDA powered Extended Haplotype Homozygosity (EHH) calculator" << endl
<< endl;
this->range_Mode = range_Mode;
this->file_Mode_path = file_Mode_path;
if (range_Mode == "FIXED")
{
this->fixed_mode_add_OR_minus = fixed_Mode_value.at(0);
// cout << this->fixed_mode_add_OR_minus << endl;
if (this->fixed_mode_add_OR_minus != '+' && this->fixed_mode_add_OR_minus != '-')
{
cout << "ERROR IN FIXED RANGE FORMAT. TERMINATING PROGRAM." << endl;
exit(3);
}
string value = fixed_Mode_value.substr(1, fixed_Mode_value.length());
this->fixed_Mode_value = stoi(value);
}
else if (range_Mode == "SNP")
{
this->default_SNP_count = default_SNP_count;
// this->EHH_cutoff = EHH_cutoff;
this->CPU_cores = EHH_CPU_cores;
cout << "CPU cores: " << this->CPU_cores << endl
<< endl;
}
else if (range_Mode == "BP")
{
this->default_SNP_BP_count = default_SNP_BP_count;
this->CPU_cores = EHH_CPU_cores;
cout << "CPU cores: " << this->CPU_cores << endl
<< endl;
}
this->input_Folder = input_Folder;
this->ouput_Path = output_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 ehh::ingress()
{
functions function = functions();
cout << "Range mode: " << this->range_Mode << endl;
if (this->range_Mode == "FIXED")
{
cout << "Core haplotype region will be augmented by " << this->fixed_mode_add_OR_minus << this->fixed_Mode_value << " to obtain the Extended haplotype region." << endl;
cout << "NOTE: Augmentation will be performed to the core region's start marker." << endl;
}
else if (this->range_Mode == "SNP")
{
cout << "Default number of SNPs collected: " << this->default_SNP_count << endl;
}
else if (this->range_Mode == "BP")
{
cout << "Default displacement from core SNP (bp): " << this->default_SNP_BP_count << endl;
}
cout << endl;
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(file_Mode_path, ios::in);
cout << "Processing gene list:" << endl;
string output_File = ouput_Path + "/" +
country.substr(country.find_last_of("/") + 1, country.length()) + "_" +
filesystem::path(file_Mode_path).stem().string();
if (range_Mode == "FILE" || range_Mode == "FIXED")
{
// output_File = ouput_Path + "/" +
// country.substr(country.find_last_of("/") + 1, country.length()) + "_" +
// filesystem::path(file_Mode_path).stem().string() +
// ".ehh";
output_File = output_File + ".ehh";
cout << endl;
cout << "Writing to file\t: " << output_File << endl;
cout << endl;
}
else
{
cout << endl;
cout << "Writing to folder\t: " << output_File << endl;
cout << endl;
}
string intermediate_File = intermediate_Path + "/" +
country.substr(country.find_last_of("/") + 1, country.length()) + "_" +
filesystem::path(file_Mode_path).stem().string() +
".log_ehh";
if (gene_File.is_open())
{
string gene_Combo;
if (filesystem::exists(intermediate_File) == 0)
{
if (range_Mode == "FILE" || range_Mode == "FIXED")
{
function.createFile(output_File, "Gene_name\tCore_coordinates\tExtended_coordinates\tCore_Haplotype_Number\tCt\tTotal_Et\tEHH");
}
else
{
filesystem::create_directory(output_File);
}
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;
if (range_Mode == "FILE" || range_Mode == "FIXED")
{
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];
vector<string> Core_coordinates;
function.split(Core_coordinates, split_Data[1], ':');
if (range_Mode == "SNP" || range_Mode == "BP")
{
cout << "SNP ID\t: " << gene_Name << endl;
int position_SNP = stoi(Core_coordinates[1]);
cout << "Coordinates:" << endl;
cout << "Chromosome: " << Core_coordinates[0] << "\tPosition: " << position_SNP << endl;
// create a new search for single position.
int segment_Position = function.single_segment_retrieval(position_SNP, folder_Index);
if (segment_Position != -1)
{
string SNP_file_Name = folder_Index[segment_Position].second;
// cout << file_Name << endl;
fstream SNP_file;
SNP_file.open(SNP_file_Name, ios::in);
int SNP_Index_in_file = -1;
if (range_Mode == "SNP")
{
vector<string> collect_Segregrating_sites;
int bottom_count = 0;
int top_count = 0;
if (SNP_file.is_open())
{
string line;
getline(SNP_file, line); // skip first header line
while (getline(SNP_file, line))
{
collect_Segregrating_sites.push_back(line);
if (SNP_Index_in_file == -1)
{
vector<string> positions;
function.split_getPos_ONLY(positions, line, '\t');
int pos = stoi(positions[1]);
if (pos == position_SNP)
{
cout << "SNP found in repository: " << SNP_file_Name << endl;
SNP_Index_in_file = collect_Segregrating_sites.size() - 1;
top_count = SNP_Index_in_file;
// cout << SNP_Position_in_file << endl;
}
}
else
{
bottom_count = bottom_count + 1;
if (bottom_count == this->default_SNP_count)
{
break;
}
}
}
SNP_file.close();
}
// cout << top_count << endl
// << bottom_count << endl;
if (SNP_Index_in_file != -1)
{
process_SNP_EHH(position_SNP, folder_Index, segment_Position, SNP_Index_in_file, collect_Segregrating_sites, N_float, top_count, bottom_count);
// Write to file
string results_File = output_File + "/" +
gene_Name + "_" + Core_coordinates[0] + "_" + Core_coordinates[1] + "_" + to_string(this->default_SNP_count) + "_SNP.ehh";
function.createFile(results_File, "Displacement\tSNP_position\tEHH_0\tEHH_1");
cout << "Writing results to file: " << results_File << endl
<< endl;
output.open(results_File, ios::app);
for (int line = 0; line < positions_Collect.size(); line++)
{
output << positions_Collect[line] << "\t";
if (line < EHH_0_up.size())
{
output << EHH_0_up[EHH_0_up.size() - 1 - line] << "\t" << EHH_1_up[EHH_1_up.size() - 1 - line] << "\n";
}
else
{
output << EHH_0_down[line - EHH_0_up.size()] << "\t" << EHH_1_down[line - EHH_0_up.size()] << "\n";
}
}
output.close();
// clear global vectors
positions_Collect.clear();
EHH_0_up.clear();
EHH_1_up.clear();
EHH_0_down.clear();
EHH_1_down.clear();
}
else
{
cout << "ERROR SNP NOT FOUND. PLEASE CHECK THE REPOSITORY" << endl;
}
}
else
{
vector<string> folder_index_Positions;
function.split(folder_index_Positions, folder_Index[0].first, '_');
int low_Value = stoi(folder_index_Positions[0]);
int min_Limit = position_SNP - this->default_SNP_BP_count;
// cout << min_Limit << "\t" << low_Value << endl;
if (min_Limit < low_Value)
{
min_Limit = low_Value;
}
function.split(folder_index_Positions, folder_Index[folder_Index.size() - 1].first, '_');
int high_Value = stoi(folder_index_Positions[1]);
int max_Limit = position_SNP + this->default_SNP_BP_count;
// cout << max_Limit << "\t" << high_Value << endl;
if (max_Limit > high_Value)
{
max_Limit = high_Value;
}
// cout << min_Limit << "\t" << low_Value << endl;
// cout << max_Limit << "\t" << high_Value << endl;
vector<string> collect_SNP_file;
vector<string> SNP_positions;
if (SNP_file.is_open())
{
string line;
getline(SNP_file, line); // skip first header line
while (getline(SNP_file, line))
{
vector<string> positions;
function.split_getPos_ONLY(positions, line, '\t');
int pos = stoi(positions[1]);
if (pos >= min_Limit && pos <= max_Limit)
{
collect_SNP_file.push_back(line);
int displacement = pos - position_SNP;
SNP_positions.push_back(to_string(displacement) + "\t" + to_string(pos));
if (pos == position_SNP)
{
cout << "SNP found in repository: " << SNP_file_Name << endl;
SNP_Index_in_file = collect_SNP_file.size() - 1;
}
}
else if (pos > max_Limit)
{
break;
}
}
SNP_file.close();
}
if (SNP_Index_in_file != -1)
{
// Collet remaining files
vector<string> file_List;
cout << "System is retrieving file(s)" << endl;
if (folder_Index.size() > 1)
{
file_List = function.compound_interpolationSearch_ordered(folder_Index, min_Limit, max_Limit);
}
else
{
file_List.push_back(folder_Index[0].second);
}
cout << "System has retrieved all file(s)" << endl;
vector<string> seg_Sites_ALL;
vector<string> seg_Sites_ALL_Positions;
int SNP_Index_in_FULL = -1;
//cout << file_List.size();
for (string files : file_List)
{
// cout << files << "\t" << SNP_file_Name << endl;
if (files == SNP_file_Name)
{
// Fill the SNP file data from what was collected
// get where position is NOW
SNP_Index_in_FULL = SNP_Index_in_file + seg_Sites_ALL.size();
for (int lines = 0; lines < collect_SNP_file.size(); lines++)
{
seg_Sites_ALL.push_back(collect_SNP_file[lines]);
seg_Sites_ALL_Positions.push_back(SNP_positions[lines]);
}
collect_SNP_file.clear();
SNP_positions.clear();
}
else
{
fstream file;
file.open(files, ios::in);
if (file.is_open())
{
string line;
getline(file, line); // skip header
while (getline(file, line))
{
vector<string> positions;
function.split_getPos_ONLY(positions, line, '\t');
int pos = stoi(positions[1]);
if (pos >= min_Limit && pos <= max_Limit)
{
seg_Sites_ALL.push_back(line);
int displacement = pos - position_SNP;
seg_Sites_ALL_Positions.push_back(to_string(displacement) + "\t" + to_string(pos));
}
else if (pos > max_Limit)
{
break;
}
}
file.close();
}
}
}
// PROCESS EHH_SNP_Distance
// cout << seg_Sites_ALL_Positions[SNP_Index_in_FULL] << endl;
// cout << SNP_Index_in_FULL << "\t" << seg_Sites_ALL_Positions.size();
process_SNP_EHH_BP(seg_Sites_ALL, SNP_Index_in_FULL, N, SNP_Index_in_FULL, (seg_Sites_ALL.size() - SNP_Index_in_FULL - 1));
// print the results
string results_File = output_File + "/" +
gene_Name + "_" + Core_coordinates[0] + "_" + Core_coordinates[1] + "_" + to_string(this->default_SNP_BP_count) + "_BP.ehh";
function.createFile(results_File, "Displacement\tSNP_position\tEHH_0\tEHH_1");
cout << "\nWriting results to file: " << results_File << endl
<< endl;
output.open(results_File, ios::app);
for (int line = 0; line < seg_Sites_ALL_Positions.size(); line++)
{
output << seg_Sites_ALL_Positions[line] << "\t";
if (line < EHH_0_up.size())
{
output << EHH_0_up[EHH_0_up.size() - 1 - line] << "\t" << EHH_1_up[EHH_1_up.size() - 1 - line] << "\n";
}
else
{
output << EHH_0_down[line - EHH_0_up.size()] << "\t" << EHH_1_down[line - EHH_0_up.size()] << "\n";
}
}
output.close();
// clear global vectors
// positions_Collect.clear();
EHH_0_up.clear();
EHH_1_up.clear();
EHH_0_down.clear();
EHH_1_down.clear();
// clear global arrays
}
else
{
cout << "ERROR SNP NOT FOUND. PLEASE CHECK THE REPOSITORY" << endl;
}
}
}
else
{
cout << "ERROR SNP NOT FOUND. PLEASE CHECK THE REPOSITORY" << endl;
}
}
else
{
cout << "Gene name\t: " << gene_Name << endl;
int core_start_Co = stoi(Core_coordinates[1]);
int core_end_Co = stoi(Core_coordinates[2]);
int ext_start_Co = 0;
int ext_end_Co = 0;
// ADD IF TO CHECK IF FIXED OR NOT
if (range_Mode == "FILE")
{
if (split_Data[2].at(0) == '+')
{
string value = split_Data[2].substr(1, split_Data[2].length());
ext_start_Co = core_start_Co;
ext_end_Co = ext_start_Co + stoi(value);
}
else if (split_Data[2].at(0) == '-')
{
string value = split_Data[2].substr(1, split_Data[2].length());
ext_start_Co = core_start_Co - stoi(value);
ext_end_Co = core_end_Co;
}
else
{
vector<string> Ext_coordinates;
function.split(Ext_coordinates, split_Data[2], ':');
ext_start_Co = stoi(Ext_coordinates[0]);
ext_end_Co = stoi(Ext_coordinates[1]);
}
}
else
{
if (fixed_mode_add_OR_minus == '+')
{
ext_start_Co = core_start_Co;
ext_end_Co = ext_start_Co + fixed_Mode_value;
}
else
{
ext_start_Co = core_start_Co - fixed_Mode_value;
ext_end_Co = core_end_Co;
}
}
cout << "Coordinates:" << endl;
cout << "Chromosome: " << Core_coordinates[0] << endl;
cout << "Core region: "
<< "Start: " << core_start_Co << " End: " << core_end_Co << endl;
cout << "Extended region: "
<< "Start: " << ext_start_Co << " End: " << ext_end_Co << endl;
cout << endl;
int VALID_or_NOT = 0;
if (core_start_Co < ext_start_Co)
{
VALID_or_NOT = VALID_or_NOT + 1;
}
if (core_end_Co > ext_end_Co)
{
VALID_or_NOT = VALID_or_NOT + 2;
}
if (VALID_or_NOT == 0)
{
vector<string> file_List;
cout << "System is retrieving file(s)" << endl;
if (folder_Index.size() > 1)
{
file_List = function.compound_interpolationSearch(folder_Index, ext_start_Co, ext_end_Co);
}
else
{
file_List.push_back(folder_Index[0].second);
}
cout << "System has retrieved all file(s)" << endl;
cout << endl;
cout << "System is collecting segregating site(s)" << endl;
vector<string> collect_Segregrating_sites;
// core = 0 ext = 1
vector<int> core_OR_ext;
int core_Count = 0;
// int ext_Count = 0;
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 >= ext_start_Co && pos <= ext_end_Co)
{
collect_Segregrating_sites.push_back(line);
if (pos >= core_start_Co && pos <= core_end_Co)
{
core_OR_ext.push_back(0);
core_Count = core_Count + 1;
}
else
{
core_OR_ext.push_back(1);
// ext_Count = ext_Count + 1;
}
}
else if (pos > ext_end_Co)
{
break;
}
}
file.close();
}
}
// cout << collect_Segregrating_sites.size() << "\t" << (core_Count + ext_Count) << endl;
// vector<string> &total_Segregrating_sites, vector<int> core_OR_ext, int core_Count, float N
// int ext_Count = collect_Segregrating_sites.size() - core_Count;
vector<int> core_Haplotype_Collection;
vector<int> extended_Haplotype_Sums;
process_EHH(collect_Segregrating_sites, core_OR_ext, core_Count, N_float, core_Haplotype_Collection, extended_Haplotype_Sums);
cout << endl;
// Gene_name\tCore_coordinates\tExtended_coordinates\tCore_Haplotype_Number\tCt\tTotal_Et\tEHH
for (int core = 0; core < core_Haplotype_Collection.size(); core++)
{
cout << "Processing core haplotype " << core + 1 << ": " << endl;
// cout << core_Haplotype_Collection[core] << endl;
int core_Hap_Sum = (core_Haplotype_Collection[core] * (core_Haplotype_Collection[core] - 1)) / 2;
cout << "Core combinations: " << core_Hap_Sum << endl;
int ext_Sum = extended_Haplotype_Sums[core];
cout << "Sum of extended haplotype combinations: " << ext_Sum << endl;
float EHH = (float)ext_Sum / (float)core_Hap_Sum;
string EHH_string = to_string(EHH);
if (isnan(EHH))
{
EHH_string = "DIV_0";
}
cout << "EHH: " << EHH_string << endl;
output << gene_Name << "\t"
<< Core_coordinates[0] << ":" << to_string(core_start_Co) << ":" << to_string(core_end_Co)
<< "\t" << Core_coordinates[0] << ":" << to_string(ext_start_Co) << ":" << to_string(ext_end_Co)
<< "\t" << to_string(core + 1)
<< "\t" << to_string(core_Hap_Sum)
<< "\t" << to_string(ext_Sum)
<< "\t" << EHH_string << "\n";
cout << endl;
}
output.flush();
}
else
{
cout << "ERROR IN EXTENDED REGION'S COORDINATES: " << gene_Name << endl;
if (VALID_or_NOT == 1)
{
cout << "ERROR IN START COORDINATES. EXTENDED REGION'S START APPEARS TO FALL INSIDE THE CORE REGION. PLEASE CHECK" << endl;
}
else if (VALID_or_NOT == 2)
{
cout << "ERROR IN END COORDINATES. EXTENDED REGION'S TAIL APPEARS TO FALL INSIDE THE CORE REGION. PLEASE CHECK" << endl;
}
else if (VALID_or_NOT == 3)
{
cout << "ERROR IN BOTH START AND END COORDINATES. ENTIRE EXTENDED REGION APPEARS TO LIE INSIDE THE CORE REGION. PLEASE CHECK" << endl;
}
cout << "GENE " << gene_Name << " WILL BE SKIPPED." << endl;
cout << endl;
}
}
intermediate << gene_Combo << "\n";
intermediate.flush();
}
output.close();
intermediate.close();
gene_File.close();
}
// REMOVE BREAK AFTER TESTING
// break;
}
}
__global__ void cuda_SNP_grid_0_1_BP(int total_Segs, char *sites, int *index, char *Hap_array, int core_SNP_index, int *core_SNP_alleles, int *SNP_counts)
{
int tid = threadIdx.x + blockIdx.x * blockDim.x;
while (tid < total_Segs)
{
int column = 0;
int site_Start = index[tid];
int site_End = index[tid + 1];
int i = site_Start;
while (column < 9)
{
if (sites[i] == '\t')
{
column++;
}
// if (core_SNP_index == tid)
// {
// printf("%c\n", sites[i]);
// }
i++;
}
int start_Hap = tid;
int stride = 0;
// int grid_Row = tid;
int grid_Column = 0;
if (core_SNP_index == tid)
{
int Hap_0 = 0;
int Hap_1 = 0;
while (i < site_End)
{
if (sites[i] == '0' || sites[i] == '1')
{
char value = sites[i];
Hap_array[start_Hap + stride] = value;
stride = stride + total_Segs;
if (sites[i] == '0')
{
core_SNP_alleles[grid_Column] = 0;
Hap_0 = Hap_0 + 1;
}
else
{
core_SNP_alleles[grid_Column] = 1;
Hap_1 = Hap_1 + 1;
}
grid_Column++;
}
i++;
}
SNP_counts[0] = Hap_0;
SNP_counts[1] = Hap_1;
}
else
{
while (i < site_End)
{
if (sites[i] == '0' || sites[i] == '1')
{
char value = sites[i];
Hap_array[start_Hap + stride] = value;
stride = stride + total_Segs;
}
i++;
}
}
tid += blockDim.x * gridDim.x;
}
}
__global__ void cuda_SNP_grid_0_1(int total_Segs, char *sites, int *index, char *Hap_array, int core_SNP_index, int *core_SNP_alleles, int *SNP_counts, int *cuda_pos_start_Index, int *cuda_pos_end_Index)
{
int tid = threadIdx.x + blockIdx.x * blockDim.x;
while (tid < total_Segs)
{
int column = 0;
int site_Start = index[tid];
int site_End = index[tid + 1];
int i = site_Start;
while (column < 1)
{
if (sites[i] == '\t')
{
column++;
}
i++;
}
// POS column
cuda_pos_start_Index[tid] = i;
while (column < 2)
{
if (sites[i] == '\t')
{
column++;
}
i++;
}
// will point to the tab but makes < easier later
cuda_pos_end_Index[tid] = i - 1;
while (column < 9)
{
if (sites[i] == '\t')
{
column++;
}
// if (core_SNP_index == tid)
// {
// printf("%c\n", sites[i]);
// }
i++;
}
int start_Hap = tid;
int stride = 0;
// int grid_Row = tid;
int grid_Column = 0;
if (core_SNP_index == tid)
{
int Hap_0 = 0;
int Hap_1 = 0;
while (i < site_End)
{
if (sites[i] == '0' || sites[i] == '1')
{
char value = sites[i];
Hap_array[start_Hap + stride] = value;
stride = stride + total_Segs;
if (sites[i] == '0')
{
core_SNP_alleles[grid_Column] = 0;
Hap_0 = Hap_0 + 1;
}
else
{
core_SNP_alleles[grid_Column] = 1;
Hap_1 = Hap_1 + 1;
}
grid_Column++;
}
i++;
}
SNP_counts[0] = Hap_0;
SNP_counts[1] = Hap_1;
}
else
{
while (i < site_End)
{
if (sites[i] == '0' || sites[i] == '1')
{
char value = sites[i];
Hap_array[start_Hap + stride] = value;
stride = stride + total_Segs;
}
i++;
}
}
tid += blockDim.x * gridDim.x;
}
}
__global__ void cuda_EHH_up_0(int total_Segs_UP, char **grid, int core_SNP_index, int *core_SNP_alleles, int N, int **Indexes_found_Zero, int zero_Count, float *EHH_Zero, int combo_Zero)
{
// ! TEST CODE, NOT USED IN THE ACTUAL PROGRAMME
int tid = threadIdx.x + blockIdx.x * blockDim.x;
while (tid < total_Segs_UP)
{
int augment = tid + 1;
int check_until_row = core_SNP_index - augment;
// printf("%d\n", check_until_row);
int found_column = 0;
int found_row = tid;
int sum_0_Combos = 0;
for (int query_Hap = 0; query_Hap < N; query_Hap++)
{
if (found_column < zero_Count)
{
if (core_SNP_alleles[query_Hap] == 0)
{
int found_YES_NO_query = 0;
for (size_t check_Found = 0; check_Found < found_column; check_Found++)
{
if (Indexes_found_Zero[found_row][check_Found] == query_Hap)
{
found_YES_NO_query = 1;
break;
}
}
if (found_YES_NO_query == 0)
{
Indexes_found_Zero[found_row][found_column] = query_Hap;
int query_Count = 1;
found_column++;
for (int subject_Hap = query_Hap + 1; subject_Hap < N; subject_Hap++)
{
if (core_SNP_alleles[subject_Hap] == 0)
{
int found_YES_NO_subject = 0;
for (size_t check_Found = 0; check_Found < found_column; check_Found++)
{
if (Indexes_found_Zero[found_row][check_Found] == subject_Hap)
{
found_YES_NO_subject = 1;
break;
}
}
if (found_YES_NO_subject == 0)
{
int match = 0;
for (int grid_row = core_SNP_index - 1; grid_row >= check_until_row; grid_row--)
{
// printf("%d %d %d\n", tid, grid_row, check_until_row);
if (grid[grid_row][query_Hap] != grid[grid_row][subject_Hap])
{
match = 1;
break;
}
}
if (match == 0)
{
// same hap found
query_Count++;
Indexes_found_Zero[found_row][found_column] = subject_Hap;
found_column++;
}
}
}
}
// get_combo
int combo = (query_Count * (query_Count - 1)) / 2;
sum_0_Combos = sum_0_Combos + combo;
}
}
}
else
{
break;
}
}
EHH_Zero[tid] = (float)sum_0_Combos / (float)combo_Zero;
// printf("%d\n", sum_0_Combos);
// EHH_Zero[tid] = (float)sum_0_Combos;
tid += blockDim.x * gridDim.x;
}
}
void ehh::process_SNP_EHH_BP(vector<string> &total_Segregrating_sites, int SNP_Index_in_FULL, int N, int SNPs_above, int SNPs_below)
{
functions function = functions();
cout << "\nSystem is calculating EHH: \n"
<< endl;
cout << "STEP 1 of 4: Organizing SNPs for GPU" << endl;
int num_segregrating_Sites = total_Segregrating_sites.size();
string Seg_sites = "";
int site_Index[num_segregrating_Sites + 1];
site_Index[0] = 0;
for (size_t i = 0; i < num_segregrating_Sites; i++)
{
Seg_sites.append(total_Segregrating_sites[i]);
site_Index[i + 1] = site_Index[i] + total_Segregrating_sites[i].size();
}
char *full_Char;
full_Char = (char *)malloc((Seg_sites.size() + 1) * sizeof(char));
strcpy(full_Char, Seg_sites.c_str());
total_Segregrating_sites.clear();
char *cuda_full_Char;
cudaMallocManaged(&cuda_full_Char, (Seg_sites.size() + 1) * sizeof(char));
int *cuda_site_Index;
cudaMallocManaged(&cuda_site_Index, (num_segregrating_Sites + 1) * sizeof(int));
int *core_SNP_alleles;
int *cuda_core_SNP_alleles;
int *SNP_counts, *cuda_SNP_counts;
core_SNP_alleles = (int *)malloc(N * sizeof(int));
SNP_counts = (int *)malloc(2 * sizeof(int));
cudaMallocManaged(&cuda_SNP_counts, 2 * sizeof(int));
cudaMallocManaged(&cuda_core_SNP_alleles, N * sizeof(int));
cudaMemcpy(cuda_full_Char, full_Char, (Seg_sites.size() + 1) * sizeof(char), cudaMemcpyHostToDevice);
cudaMemcpy(cuda_site_Index, site_Index, (num_segregrating_Sites + 1) * sizeof(int), cudaMemcpyHostToDevice);
/**
* @param cuda_Hap_array stores the forged Haplotypes for the region under study.
* @param Hap_array is used by the CPU. Is a COPY of cuda_Hap_array.