-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcf_splitter_2.cu
2338 lines (1878 loc) · 83.3 KB
/
vcf_splitter_2.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 "vcf_splitter_2.cuh"
vcf_splitter_2::vcf_splitter_2(int cuda_ID, string input_vcf_Folder, string output_Folder, int cores, int SNPs_per_time_CPU, int SNPs_per_time_GPU, int allele_Count_REF, int allele_Count_ALT, int ploidy, int summary_Individuals)
{
// ! THIS CONSTRUCTOR IS FOR THE BY CHROMOSOME SPLIT
// trim to GT as well.
// trim by REF and ALT allele count as well
// split by chromosome
cout << "Starting up VCF SPLITTER: Split by chromosome\n\nNOTE: Only the GT column will be extracted." << endl
<< endl;
this->input_vcf_Folder = input_vcf_Folder;
this->output_Folder = output_Folder;
this->cores = cores;
this->SNPs_per_time_CPU = SNPs_per_time_CPU;
this->SNPs_per_time_GPU = SNPs_per_time_GPU;
this->allele_Count_REF = allele_Count_REF;
this->allele_Count_ALT = allele_Count_ALT;
this->ploidy = ploidy;
this->hap_Size = (2 * ploidy) - 1;
this->summary_Individuals = summary_Individuals;
cuda_Set_device(cuda_ID);
}
vcf_splitter_2::vcf_splitter_2(int cuda_ID, string input_vcf_Folder, string output_Folder, string population_File, int sampled_ID_col, int pop_ID_column, int cores, int SNPs_per_time_CPU, int SNPs_per_time_GPU, int ploidy, int max_SNPs_per_file, int logic_MAF, double MAF)
{
cout << "Starting up VCF SPLITTER: Creating file hierarchy\n\nNOTE: VCF files should have only the GT columns and must be SNPs." << endl
<< endl;
this->input_vcf_Folder = input_vcf_Folder;
this->output_Folder = output_Folder;
this->cores = cores;
this->SNPs_per_time_CPU = SNPs_per_time_CPU;
this->SNPs_per_time_GPU = SNPs_per_time_GPU;
this->ploidy = ploidy;
this->hap_Size = (2 * ploidy) - 1;
this->population_File_path = population_File;
this->column_Sample_ID = sampled_ID_col;
this->column_Population_ID = pop_ID_column;
this->SNP_count_per_File = max_SNPs_per_file;
this->logic_MAF = logic_MAF;
this->MAF = MAF;
cout << "MAF: " << MAF << endl;
cout << "Logical comparison: ";
if (logic_MAF == 0)
{
cout << "equal\n"
<< endl;
}
else if (logic_MAF == 1)
{
cout << "greater than\n"
<< endl;
}
else if (logic_MAF == 2)
{
cout << "less than\n"
<< endl;
}
else if (logic_MAF == 10)
{
cout << "greater than or equal\n"
<< endl;
}
else if (logic_MAF == 20)
{
cout << "less than or equal\n"
<< endl;
}
cuda_Set_device(cuda_ID);
}
void vcf_splitter_2::cuda_Set_device(int cuda_ID)
{
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 vcf_splitter_2::ingress_file_hierarchy()
{
functions function = functions();
fstream population_File;
population_File.open(this->population_File_path, ios::in);
vector<pair<string, string>> sample_population;
set<string> population_Unique_IDs_SET;
if (population_File.is_open())
{
cout << "Processing population file: " << this->population_File_path << endl;
string line;
getline(population_File, line);
while (getline(population_File, line))
{
vector<string> data_Line;
function.split(data_Line, line, '\t');
sample_population.push_back(make_pair(data_Line[column_Sample_ID - 1], data_Line[column_Population_ID - 1]));
population_Unique_IDs_SET.insert(data_Line[column_Population_ID - 1]);
}
population_File.close();
}
int num_Unique_populations = population_Unique_IDs_SET.size();
cout << num_Unique_populations << " unique population(s) were found: ";
for (string ID : population_Unique_IDs_SET)
{
vector<int> row;
cout << ID;
population_Unique_IDs.push_back(ID);
population_sample_IDs.push_back(row);
if (population_Unique_IDs.size() != num_Unique_populations)
{
cout << ", ";
}
}
population_Unique_IDs_SET.clear();
cout << endl
<< endl;
cout << "Identifying VCF file(s): " << this->input_vcf_Folder << endl;
vector<pair<string, string>> vcf_Files;
for (const auto &entry : filesystem::directory_iterator(input_vcf_Folder))
{
string coordinates = entry.path().string();
string extension = coordinates.substr(coordinates.find_last_of(".") + 1);
transform(extension.begin(), extension.end(), extension.begin(), ::toupper);
if (extension == "VCF")
{
vcf_Files.push_back(make_pair(coordinates, filesystem::path(coordinates).stem().string()));
}
// cout << coordinates << "\t"
// << extension << endl;
}
cout << vcf_Files.size() << " VCF file(s) have been found\n"
<< endl;
for (int vcf_Index = 0; vcf_Index < vcf_Files.size(); vcf_Index++)
{
fstream file;
file.open(vcf_Files[vcf_Index].first, ios::in);
if (file.is_open())
{
cout << "Processing file: " << vcf_Files[vcf_Index].first << endl
<< endl;
string line;
getline(file, line);
while (line.substr(0, 2) == "##")
{
getline(file, line);
}
function.split(header_Data, line, '\t');
for (int erase = 0; erase < 9; erase++)
{
header_Data.erase(header_Data.begin());
}
int N = header_Data.size();
int augment = ((2 * ploidy) - 1) * N;
cout << "Found " << N << " samples in VCF" << endl;
cout << "Mapping samples to populations" << endl
<< endl;
sample_ID_population_ID = (int *)malloc(N * sizeof(int));
vector<thread> threads_vec;
int N_per_Thread = N / cores;
int remainder = N % cores;
for (int core_ID = 0; core_ID < cores; core_ID++)
{
int start_N = core_ID * N_per_Thread;
int stop_N = start_N + N_per_Thread;
threads_vec.push_back(thread{&vcf_splitter_2::individual_Map, this, start_N, stop_N, header_Data, sample_population, population_Unique_IDs});
}
if (remainder != 0)
{
int start_N = N - remainder;
int stop_N = N;
threads_vec.push_back(thread{&vcf_splitter_2::individual_Map, this, start_N, stop_N, header_Data, sample_population, population_Unique_IDs});
}
for (thread &t : threads_vec)
{
if (t.joinable())
{
t.join();
}
}
threads_vec.clear();
sample_population.clear();
// for (size_t i = 0; i < N; i++)
// {
// cout << sample_ID_population_ID[i] << " ";
// }
// cout << endl;
// for (size_t i = 0; i < N; i++)
// {
// cout << sample_ID_population_ID[i] << ", ";
// }
cout << "Creating directories" << endl;
string output_vcf_Folder = output_Folder + "/" + vcf_Files[vcf_Index].second;
cout << "Creating root folder: " << output_vcf_Folder << endl;
filesystem::create_directory(output_vcf_Folder);
cout << "\nCreating population folders and calculating MAF: \n\n";
cudaMallocManaged(&cuda_MAF_count_per_Population, population_Unique_IDs.size() * sizeof(int));
MAF_count_per_Population = (int *)malloc(population_Unique_IDs.size() * sizeof(int));
cudaMallocManaged(&cuda_sample_ID_population_ID, N * sizeof(int));
for (int pop_ID = 0; pop_ID < population_Unique_IDs.size(); pop_ID++)
{
cout << "Population: " << population_Unique_IDs[pop_ID] << endl;
string population_Folder = output_vcf_Folder + "/" + population_Unique_IDs[pop_ID];
cout << "Folder created: " << population_Folder << endl;
string header_String = "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t";
filesystem::create_directory(population_Folder);
sort(population_sample_IDs[pop_ID].begin(), population_sample_IDs[pop_ID].end());
for (int sample = 0; sample < population_sample_IDs[pop_ID].size(); sample++)
{
header_String = header_String + header_Data[population_sample_IDs[pop_ID][sample]];
if (sample != population_sample_IDs[pop_ID].size() - 1)
{
header_String = header_String + "\t";
}
}
pop_Header.push_back(header_String);
int MAF = population_sample_IDs[pop_ID].size() * this->ploidy * this->MAF;
cout << "MAF count (" << population_sample_IDs[pop_ID].size()
<< " x " << this->ploidy << " x " << this->MAF << "): "
<< MAF << endl
<< endl;
MAF_count_per_Population[pop_ID] = MAF;
}
cudaMemcpy(cuda_MAF_count_per_Population, MAF_count_per_Population, population_Unique_IDs.size() * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(cuda_sample_ID_population_ID, sample_ID_population_ID, N * sizeof(int), cudaMemcpyHostToDevice);
// cudaError_t err = cudaGetLastError();
// if (err != cudaSuccess)
// {
// printf("CUDA Error 1: %s\n", cudaGetErrorString(err));
// // Possibly: exit(-1) if program cannot continue....
// }
// cudaDeviceSynchronize();
while (getline(file, line))
{
all_Lines.push_back(line);
if (all_Lines.size() == this->SNPs_per_time_CPU)
{
process_SNPs_Hierarchy(N, population_Unique_IDs.size(), augment, output_vcf_Folder);
}
}
file.close();
// exit(3);
if (all_Lines.size() != 0)
{
process_SNPs_Hierarchy(N, population_Unique_IDs.size(), augment, output_vcf_Folder);
}
for (int pop_ID = 0; pop_ID < population_Unique_IDs.size(); pop_ID++)
{
if (position_Complete_segs[pop_ID].size() > 0)
{
string pop_Name = population_Unique_IDs[pop_ID];
string population_Folder = output_vcf_Folder + "/" + pop_Name;
sort(position_Complete_segs[pop_ID].begin(), position_Complete_segs[pop_ID].end());
vector<pair<int, string>> Segs_population = position_Complete_segs[pop_ID];
int pos_1 = Segs_population[0].first;
int pos_last = Segs_population[Segs_population.size() - 1].first;
string file_Name = population_Folder + "/" + file_CHR_value + "_" + pop_Name + "_" + to_string(pos_1) + "_" + to_string(pos_last) + ".vcf";
cout << "Writing file segment: " << file_Name << endl;
function.createFile(file_Name, pop_Header[pop_ID]);
fstream file_Segment;
file_Segment.open(file_Name, ios::app);
for (int line = 0; line < Segs_population.size(); line++)
{
file_Segment << Segs_population[line].second + "\n";
}
file_Segment.close();
}
}
}
free(sample_ID_population_ID);
free(MAF_count_per_Population);
cudaFree(cuda_sample_ID_population_ID);
cudaFree(cuda_MAF_count_per_Population);
header_Data.clear();
pop_Header.clear();
population_sample_IDs.clear();
population_Unique_IDs.clear();
position_Complete_segs.clear();
}
cout << endl;
}
void vcf_splitter_2::individual_Map(int start_N, int stop_N, vector<string> header_Data, vector<pair<string, string>> sample_population, vector<string> population_Unique_IDs)
{
vector<pair<int, int>> sample_ID_population_ID_partial;
for (int N_ID = start_N; N_ID < stop_N; N_ID++)
{
string sample_Name = header_Data[N_ID];
for (int check_Sample = 0; check_Sample < sample_population.size(); check_Sample++)
{
if (sample_Name == sample_population[check_Sample].first)
{
string country = sample_population[check_Sample].second;
for (int check_Country = 0; check_Country < population_Unique_IDs.size(); check_Country++)
{
if (country == population_Unique_IDs[check_Country])
{
sample_ID_population_ID_partial.push_back(make_pair(N_ID, check_Country));
break;
}
}
sample_population.erase(sample_population.begin() + check_Sample);
break;
}
}
}
unique_lock<shared_mutex> ul(g_mutex);
for (size_t i = 0; i < sample_ID_population_ID_partial.size(); i++)
{
sample_ID_population_ID[sample_ID_population_ID_partial[i].first] = sample_ID_population_ID_partial[i].second;
population_sample_IDs[sample_ID_population_ID_partial[i].second].push_back(sample_ID_population_ID_partial[i].first);
}
}
__global__ void cuda_seg_Pop_process(char *sites, int *index, int num_Segregrating_sites, int ploidy, int tot_N_individuals, int *cuda_CHR_start_Index, int *cuda_CHR_end_Index, int *cuda_pos_start_Index, int *cuda_pos_end_Index, int *cuda_ID_start_Index, int *cuda_ID_end_Index, int *cuda_REF_start_Index, int *cuda_REF_end_Index, int *cuda_ALT_start_Index, int *cuda_ALT_end_Index, int *cuda_six_9_start_Index, int *cuda_six_9_end_Index, int *cuda_VALID_or_NOT, char *seg_Array, int num_pop, int **cuda_REF_populations, int **cuda_ALT_populations, int **cuda_VALID_or_NOT_populations, int *cuda_sample_ID_population_ID, int *cuda_MAF_count_per_Population, int logic_MAF)
{
int tid = threadIdx.x + blockIdx.x * blockDim.x;
while (tid < num_Segregrating_sites)
{
int column = 0;
int site_Start = index[tid];
int site_End = index[tid + 1];
int i = site_Start;
cuda_CHR_start_Index[tid] = site_Start;
while (column < 1)
{
if (sites[i] == '\t')
{
column++;
}
i++;
}
cuda_CHR_end_Index[tid] = i - 1;
// 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;
cuda_ID_start_Index[tid] = i;
while (column < 3)
{
if (sites[i] == '\t')
{
column++;
}
i++;
}
// will point to the tab but makes < easier later
cuda_ID_end_Index[tid] = i - 1;
cuda_REF_start_Index[tid] = i;
while (column < 4)
{
if (sites[i] == '\t')
{
column++;
}
i++;
}
// will point to the tab but makes < easier later
cuda_REF_end_Index[tid] = i - 1;
int num_REF = cuda_REF_end_Index[tid] - cuda_REF_start_Index[tid];
if (num_REF == 1)
{
cuda_ALT_start_Index[tid] = i;
while (column < 5)
{
if (sites[i] == '\t')
{
column++;
}
i++;
}
// will point to the tab but makes < easier later
cuda_ALT_end_Index[tid] = i - 1;
int num_ALT = cuda_ALT_end_Index[tid] - cuda_ALT_start_Index[tid];
if (num_ALT == 1)
{
cuda_six_9_start_Index[tid] = i;
while (column < 9)
{
if (sites[i] == '\t')
{
column++;
}
i++;
}
// will point to the tab but makes < easier later
cuda_six_9_end_Index[tid] = i - 1;
// int all_pops = 0;
cuda_VALID_or_NOT[tid] = 1;
int sample_ID = 0;
int pop_ID = cuda_sample_ID_population_ID[sample_ID];
for (int pop = 0; pop < num_pop; pop++)
{
cuda_REF_populations[tid][pop] = 0;
cuda_ALT_populations[tid][pop] = 0;
cuda_VALID_or_NOT_populations[tid][pop] = 1;
}
int seg_array_Start = (tid * ((2 * ploidy) - 1)) * tot_N_individuals;
// // printf("%d\n", tid);
while (i < site_End)
{
if (sites[i] != '\t' && sites[i] != '\n')
{
seg_Array[seg_array_Start] = sites[i];
seg_array_Start++;
if (cuda_VALID_or_NOT_populations[tid][pop_ID] == 1)
{
if (sites[i] == '1')
{
cuda_ALT_populations[tid][pop_ID] = cuda_ALT_populations[tid][pop_ID] + 1;
}
else if (sites[i] == '0')
{
cuda_REF_populations[tid][pop_ID] = cuda_REF_populations[tid][pop_ID] + 1;
}
else if (sites[i] == '.')
{
cuda_VALID_or_NOT_populations[tid][pop_ID] = 0;
}
}
}
else
{
if (sites[i] == '\t')
{
sample_ID++;
pop_ID = cuda_sample_ID_population_ID[sample_ID];
}
}
i++;
}
int pop_Valid = 0;
for (int pop = 0; pop < num_pop; pop++)
{
if (cuda_VALID_or_NOT_populations[tid][pop] == 1)
{
int MA_Count;
if (cuda_REF_populations[tid][pop] > cuda_ALT_populations[tid][pop])
{
MA_Count = cuda_ALT_populations[tid][pop];
}
else
{
MA_Count = cuda_REF_populations[tid][pop];
}
// if (MA_Count != 0)
// {
if (logic_MAF == 0)
{
if (MA_Count == cuda_MAF_count_per_Population[pop])
{
// cuda_VALID_or_NOT_populations[tid][pop] = 1;
}
else
{
cuda_VALID_or_NOT_populations[tid][pop] = 0;
pop_Valid++;
}
}
else if (logic_MAF == 1)
{
if (MA_Count > cuda_MAF_count_per_Population[pop])
{
// cuda_VALID_or_NOT_populations[tid][pop] = 1;
}
else
{
cuda_VALID_or_NOT_populations[tid][pop] = 0;
pop_Valid++;
}
}
else if (logic_MAF == 2)
{
if (MA_Count < cuda_MAF_count_per_Population[pop])
{
// cuda_VALID_or_NOT_populations[tid][pop] = 1;
}
else
{
cuda_VALID_or_NOT_populations[tid][pop] = 0;
pop_Valid++;
}
}
else if (logic_MAF == 10)
{
if (MA_Count >= cuda_MAF_count_per_Population[pop])
{
// cuda_VALID_or_NOT_populations[tid][pop] = 1;
}
else
{
cuda_VALID_or_NOT_populations[tid][pop] = 0;
pop_Valid++;
}
}
else if (logic_MAF == 20)
{
if (MA_Count <= cuda_MAF_count_per_Population[pop])
{
// cuda_VALID_or_NOT_populations[tid][pop] = 1;
}
else
{
cuda_VALID_or_NOT_populations[tid][pop] = 0;
pop_Valid++;
}
}
// }
// else
// {
// cuda_VALID_or_NOT_populations[tid][pop] = 0;
// pop_Valid++;
// }
}
else
{
pop_Valid++;
}
}
if (pop_Valid == num_pop)
{
cuda_VALID_or_NOT[tid] = 0;
}
}
else
{
cuda_VALID_or_NOT[tid] = 0;
}
}
else
{
cuda_VALID_or_NOT[tid] = 0;
}
if (cuda_VALID_or_NOT[tid] == 0)
{
int seg_array_Start = (tid * ((2 * ploidy) - 1)) * tot_N_individuals;
int seg_array_Stop = seg_array_Start + (((2 * ploidy) - 1) * tot_N_individuals);
for (size_t i = seg_array_Start; i < seg_array_Stop; i++)
{
seg_Array[i] = 'x';
}
}
tid += blockDim.x * gridDim.x;
}
}
void vcf_splitter_2::process_SNPs_Hierarchy(int N, int num_Populations, int augment, string output_vcf_Folder)
{
functions function = functions();
// cout << num_Populations << endl;
/**
* The GPU is permitted to handle only a certain max number of SNPs at a time.
* Therefore the number of rounds of GPU processing and,
* the range of SNPs that will be processed in each round will have to be determined.
*
* @param GPU_rounds_full rounds requiring the max set of SNPs to be processed.
* @param GPU_rounds_partial rounds requiring the remaining set of SNPs to be processed.
*
* The start and stop range of each round is stored.
**/
int tot_Segs_Round = all_Lines.size();
cout << "System is processing and filtering " << tot_Segs_Round << " segregating site(s)" << endl;
int GPU_rounds_full = tot_Segs_Round / SNPs_per_time_GPU;
int GPU_rounds_partial = tot_Segs_Round % SNPs_per_time_GPU;
// vector<pair<int, int>> start_stop;
for (int i = 0; i < GPU_rounds_full; i++)
{
int start = i * SNPs_per_time_GPU;
int stop = start + SNPs_per_time_GPU;
start_stop.push_back(make_pair(start, stop));
}
if (GPU_rounds_partial != 0)
{
int start = tot_Segs_Round - GPU_rounds_partial;
int stop = tot_Segs_Round;
start_stop.push_back(make_pair(start, stop));
}
vector<thread> threads_vec;
/**
* Concatenation of SNPs for GPU processing is also done in parallel,
* Number of threads needed is based on the number of rounds needed.
**/
for (int rounds = 0; rounds < start_stop.size(); rounds++)
{
concat_Segs.push_back("");
int *row;
all_site_Index.push_back(row);
}
for (int rounds = 0; rounds < start_stop.size(); rounds++)
{
threads_vec.push_back(thread{&vcf_splitter_2::seg_Concat, this, rounds, start_stop[rounds].first, start_stop[rounds].second});
}
for (thread &t : threads_vec)
{
if (t.joinable())
{
t.join();
}
}
threads_vec.clear();
all_Lines.clear();
for (int rounds = 0; rounds < start_stop.size(); rounds++)
{
string Seg_sites = concat_Segs[rounds];
int *site_Index = all_site_Index[rounds];
int start = start_stop[rounds].first;
int stop = start_stop[rounds].second;
int total_Segs = stop - start;
char *full_Char;
full_Char = (char *)malloc((Seg_sites.size() + 1) * sizeof(char));
strcpy(full_Char, Seg_sites.c_str());
char *cuda_full_Char;
cudaMallocManaged(&cuda_full_Char, (Seg_sites.size() + 1) * sizeof(char));
int *cuda_site_Index;
cudaMallocManaged(&cuda_site_Index, (total_Segs + 1) * sizeof(int));
cudaMemcpy(cuda_full_Char, full_Char, (Seg_sites.size() + 1) * sizeof(char), cudaMemcpyHostToDevice);
cudaMemcpy(cuda_site_Index, site_Index, (total_Segs + 1) * sizeof(int), cudaMemcpyHostToDevice);
int *chr_start_Index, *chr_end_Index, *cuda_chr_start_Index, *cuda_chr_end_Index;
cudaMallocManaged(&cuda_chr_start_Index, total_Segs * sizeof(int));
cudaMallocManaged(&cuda_chr_end_Index, total_Segs * sizeof(int));
chr_start_Index = (int *)malloc(total_Segs * sizeof(int));
chr_end_Index = (int *)malloc(total_Segs * sizeof(int));
int *pos_start_Index, *pos_end_Index, *cuda_pos_start_Index, *cuda_pos_end_Index;
cudaMallocManaged(&cuda_pos_start_Index, total_Segs * sizeof(int));
cudaMallocManaged(&cuda_pos_end_Index, total_Segs * sizeof(int));
pos_start_Index = (int *)malloc(total_Segs * sizeof(int));
pos_end_Index = (int *)malloc(total_Segs * sizeof(int));
int *ID_start_Index, *ID_end_Index, *cuda_ID_start_Index, *cuda_ID_end_Index;
cudaMallocManaged(&cuda_ID_start_Index, total_Segs * sizeof(int));
cudaMallocManaged(&cuda_ID_end_Index, total_Segs * sizeof(int));
ID_start_Index = (int *)malloc(total_Segs * sizeof(int));
ID_end_Index = (int *)malloc(total_Segs * sizeof(int));
int *REF_start, *REF_stop, *ALT_start, *ALT_stop, *cuda_REF_start, *cuda_REF_stop, *cuda_ALT_start, *cuda_ALT_stop;
cudaMallocManaged(&cuda_REF_start, total_Segs * sizeof(int));
cudaMallocManaged(&cuda_REF_stop, total_Segs * sizeof(int));
REF_start = (int *)malloc(total_Segs * sizeof(int));
REF_stop = (int *)malloc(total_Segs * sizeof(int));
cudaMallocManaged(&cuda_ALT_start, total_Segs * sizeof(int));
cudaMallocManaged(&cuda_ALT_stop, total_Segs * sizeof(int));
ALT_start = (int *)malloc(total_Segs * sizeof(int));
ALT_stop = (int *)malloc(total_Segs * sizeof(int));
int *six_9_start_Index, *six_9_stop_Index, *cuda_six_9_start_Index, *cuda_six_9_stop_Index;
cudaMallocManaged(&cuda_six_9_start_Index, total_Segs * sizeof(int));
cudaMallocManaged(&cuda_six_9_stop_Index, total_Segs * sizeof(int));
six_9_start_Index = (int *)malloc(total_Segs * sizeof(int));
six_9_stop_Index = (int *)malloc(total_Segs * sizeof(int));
int *VALID_or_NOT, *cuda_VALID_or_NOT;
VALID_or_NOT = (int *)malloc(total_Segs * sizeof(int));
cudaMallocManaged(&cuda_VALID_or_NOT, total_Segs * sizeof(int));
// cudaError_t err4 = cudaGetLastError();
// if (err4 != cudaSuccess)
// {
// printf("CUDA Error 4: %s\n", cudaGetErrorString(err4));
// // Possibly: exit(-1) if program cannot continue....
// }
// cudaDeviceSynchronize();
int **VALID_or_NOT_populations, **cuda_VALID_or_NOT_populations;
int **cuda_REF_populations, **cuda_ALT_populations;
VALID_or_NOT_populations = (int **)malloc(total_Segs * sizeof(int *));
for (int i = 0; i < total_Segs; i++)
{
VALID_or_NOT_populations[i] = (int *)malloc((num_Populations + 1) * sizeof(int));
}
cudaMallocManaged(&cuda_VALID_or_NOT_populations, total_Segs * (num_Populations + 1) * sizeof(int));
cudaMallocManaged(&cuda_REF_populations, total_Segs * (num_Populations + 1) * sizeof(int));
cudaMallocManaged(&cuda_ALT_populations, total_Segs * (num_Populations + 1) * sizeof(int));
// cudaError_t err5 = cudaGetLastError();
// if (err5 != cudaSuccess)
// {
// printf("CUDA Error 5: %s\n", cudaGetErrorString(err5));
// // Possibly: exit(-1) if program cannot continue....
// }
// cudaDeviceSynchronize();
int **tmp = (int **)malloc(total_Segs * sizeof(tmp[0]));
int **tmp_2 = (int **)malloc(total_Segs * sizeof(tmp_2[0]));
int **tmp_3 = (int **)malloc(total_Segs * sizeof(tmp_3[0]));
for (int i = 0; i < total_Segs; i++)
{
cudaMalloc((void **)&tmp[i], (num_Populations + 1) * sizeof(tmp[0][0]));
cudaMalloc((void **)&tmp_2[i], (num_Populations + 1) * sizeof(tmp_2[0][0]));
cudaMalloc((void **)&tmp_3[i], (num_Populations + 1) * sizeof(tmp_3[0][0]));
}
// cudaError_t err6 = cudaGetLastError();
// if (err6 != cudaSuccess)
// {
// printf("CUDA Error 6: %s\n", cudaGetErrorString(err6));
// // Possibly: exit(-1) if program cannot continue....
// }
// cudaDeviceSynchronize();
cudaMemcpy(cuda_VALID_or_NOT_populations, tmp, total_Segs * sizeof(int *), cudaMemcpyHostToDevice);
cudaMemcpy(cuda_REF_populations, tmp_2, total_Segs * sizeof(int *), cudaMemcpyHostToDevice);
cudaMemcpy(cuda_ALT_populations, tmp_3, total_Segs * sizeof(int *), cudaMemcpyHostToDevice);
// cudaError_t err2 = cudaGetLastError();
// if (err2 != cudaSuccess)
// {
// printf("CUDA Error 2: %s\n", cudaGetErrorString(err2));
// // Possibly: exit(-1) if program cannot continue....
// }
// cudaDeviceSynchronize();
free(tmp);
free(tmp_2);
free(tmp_3);
char *Hap_array, *cuda_Hap_array;
cudaMallocManaged(&cuda_Hap_array, (((N * ((2 * ploidy) - 1)) * total_Segs) + 1) * sizeof(char));
Hap_array = (char *)malloc((((N * ((2 * ploidy) - 1)) * total_Segs) + 1) * sizeof(char));
// cout << N << endl;
// cout << ploidy << endl;
// cout << total_Segs << endl;
cout << "\nRound " << rounds + 1 << ": System is extracting and filtering " << total_Segs << " segregating site(s)" << endl
<< endl;
// MAF and no blank areas (samples with .) in seg for valid or not.
// cuda_seg_Pop_process(char *sites, int *index, int num_Segregrating_sites, int ploidy, int tot_N_individuals, int *cuda_CHR_start_Index, int *cuda_CHR_end_Index, int *cuda_pos_start_Index, int *cuda_pos_end_Index, int *cuda_ID_start_Index, int *cuda_ID_end_Index, int *cuda_REF_start_Index, int *cuda_REF_end_Index, int *cuda_ALT_start_Index, int *cuda_ALT_end_Index, int *cuda_six_9_start_Index, int *cuda_six_9_end_Index, int *cuda_VALID_or_NOT, char *seg_Array, int num_pop, int **cuda_REF_populations, int **cuda_ALT_populations, int **cuda_VALID_or_NOT_populations, int *cuda_sample_ID_population_ID, int *cuda_MAF_count_per_Population, int logic_MAF)
cuda_seg_Pop_process<<<tot_Blocks, tot_ThreadsperBlock>>>(cuda_full_Char, cuda_site_Index, total_Segs, ploidy, N, cuda_chr_start_Index, cuda_chr_end_Index, cuda_pos_start_Index, cuda_pos_end_Index, cuda_ID_start_Index, cuda_ID_end_Index, cuda_REF_start, cuda_REF_stop, cuda_ALT_start, cuda_ALT_stop, cuda_six_9_start_Index, cuda_six_9_stop_Index, cuda_VALID_or_NOT, cuda_Hap_array, num_Populations, cuda_REF_populations, cuda_ALT_populations, cuda_VALID_or_NOT_populations, cuda_sample_ID_population_ID, cuda_MAF_count_per_Population, logic_MAF);
cudaError_t err3 = cudaGetLastError();
if (err3 != cudaSuccess)
{
printf("CUDA Error: %s\n", cudaGetErrorString(err3));
// Possibly: exit(-1) if program cannot continue....
}
cudaDeviceSynchronize();
cout << "System is filtering valid segregating site(s)" << endl;
cudaMemcpy(VALID_or_NOT, cuda_VALID_or_NOT, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(chr_start_Index, cuda_chr_start_Index, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(chr_end_Index, cuda_chr_end_Index, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(pos_start_Index, cuda_pos_start_Index, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(pos_end_Index, cuda_pos_end_Index, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(ID_start_Index, cuda_ID_start_Index, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(ID_end_Index, cuda_ID_end_Index, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(REF_start, cuda_REF_start, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(REF_stop, cuda_REF_stop, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(ALT_start, cuda_ALT_start, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(ALT_stop, cuda_ALT_stop, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(six_9_start_Index, cuda_six_9_start_Index, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(six_9_stop_Index, cuda_six_9_stop_Index, total_Segs * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(Hap_array, cuda_Hap_array, (((N * ((2 * ploidy) - 1)) * total_Segs) + 1) * sizeof(char), cudaMemcpyDeviceToHost);
string seg_Full(Hap_array);
for (size_t i = 0; i < total_Segs; i++)
{
seg_Collection.push_back("");
}
int segs_per_Thread = total_Segs / cores;
int remainder = total_Segs % cores;
// for (size_t i = 0; i < total_Segs; i++)
// {
// cout << VALID_or_NOT[i] << " ";
// }
// cout << endl;
// exit(0);
for (int core_ID = 0; core_ID < cores; core_ID++)
{
int start_Seg = core_ID * segs_per_Thread;
int stop_Seg = start_Seg + segs_per_Thread;
threads_vec.push_back(thread{&vcf_splitter_2::seg_VALID_OR_NOT_list, this, start_Seg, stop_Seg, VALID_or_NOT, N, seg_Full, augment});
}
if (remainder != 0)
{
int start_Seg = total_Segs - remainder;
int stop_Seg = total_Segs;
threads_vec.push_back(thread{&vcf_splitter_2::seg_VALID_OR_NOT_list, this, start_Seg, stop_Seg, VALID_or_NOT, N, seg_Full, augment});
}
for (thread &t : threads_vec)
{
if (t.joinable())
{
t.join();
}
}
threads_vec.clear();
int valid_Count = VALID_List.size();
cout << valid_Count << " valid segregating site(s) were found." << endl
<< endl;
if (valid_Count > 0)
{
cout << "System is concatenating data from segregating site(s)" << endl;
sort(VALID_List.begin(), VALID_List.end());
for (size_t i = 0; i < valid_Count; i++)
{
write_CHR.push_back("");
POS_valid.push_back(-1);
}
segs_per_Thread = valid_Count / cores;
remainder = valid_Count % cores;
for (int core_ID = 0; core_ID < cores; core_ID++)
{
int start_Seg = core_ID * segs_per_Thread;
int stop_Seg = start_Seg + segs_per_Thread;
// void concat_ALL_hierarchy(int start_Seg, int stop_Seg, vector<int> VALID_Segs, char *full_Char, int *chr_start_Index, int *chr_end_Index, int *pos_start_Index, int *pos_end_Index, int *ID_start_Index, int *ID_end_Index, int *REF_start, int *REF_stop, int *ALT_start, int *ALT_stop, int *six_9_start_Index, int *six_9_stop_Index);
threads_vec.push_back(thread{&vcf_splitter_2::concat_ALL_hierarchy, this, start_Seg, stop_Seg, VALID_List, full_Char, chr_start_Index, chr_end_Index, pos_start_Index, pos_end_Index, ID_start_Index, ID_end_Index, REF_start, REF_stop, ALT_start, ALT_stop, six_9_start_Index, six_9_stop_Index});
}
if (remainder != 0)
{
int start_Seg = valid_Count - remainder;
int stop_Seg = valid_Count;
threads_vec.push_back(thread{&vcf_splitter_2::concat_ALL_hierarchy, this, start_Seg, stop_Seg, VALID_List, full_Char, chr_start_Index, chr_end_Index, pos_start_Index, pos_end_Index, ID_start_Index, ID_end_Index, REF_start, REF_stop, ALT_start, ALT_stop, six_9_start_Index, six_9_stop_Index});
}
for (thread &t : threads_vec)
{
if (t.joinable())
{
t.join();
}
}
threads_vec.clear();