-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithin_host_test_2.cu
More file actions
5335 lines (4594 loc) · 249 KB
/
within_host_test_2.cu
File metadata and controls
5335 lines (4594 loc) · 249 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "within_host_test_2.cuh"
#include "functions_library.cuh"
#include "parameter_load.h"
within_host_test_2::within_host_test_2(int CUDA_device_number, int CPU_cores, int gpu_Limit, string output_Folder, string reference_genome_Location, string replication_profile_file, mt19937 gen, string sequence_Profile_file, string write_Progeny_parents, string write_Sequences, string intermediate_Folders, string multi_READ, int at_a_Time_cells)
{
functions_library function = functions_library();
this->gen = gen;
this->CPU_cores = CPU_cores;
cout << "Available CPU cores: " << this->CPU_cores << endl
<< endl;
this->multi_READ = multi_READ;
cout << "Multiple read and write: " << this->multi_READ << endl
<< endl;
this->CUDA_device_number = CUDA_device_number;
function.print_Cuda_device(this->CUDA_device_number, this->tot_Blocks, this->tot_ThreadsperBlock);
this->gpu_Limit = gpu_Limit;
cout << "Per round GPU max unit: " << this->gpu_Limit << endl
<< endl;
this->at_a_Time_cells = at_a_Time_cells;
cout << "Number of cells simulated at a time: " << this->at_a_Time_cells << endl
<< endl;
this->output_Folder = output_Folder + "/within_host";
function.config_Folder(this->output_Folder, "Within host results");
this->intermediate_Folders = intermediate_Folders;
this->intermediate_sequence_Store = this->intermediate_Folders + "/sequences";
function.config_Folder(this->intermediate_sequence_Store, "Intermediate sequences");
this->intermediate_profile_Store = this->intermediate_Folders + "/sequence_profiles";
function.config_Folder(this->intermediate_profile_Store, "Intermediate sequence profiles");
// cout << "Output results folder: " << this->output_Folder << endl;
if (write_Progeny_parents == "YES")
{
this->write_Progeny_parent = "YES";
this->progeny_Parent_folder = this->output_Folder + "/progeny_parent_Data";
function.config_Folder(this->progeny_Parent_folder, "Progeny parent relationship");
this->progeny_File = progeny_Parent_folder + "/progeny_parent_relationships.csv";
this->progeny_Recombination_File = progeny_Parent_folder + "/progeny_Recombination_details.csv";
this->sequence_Profiles = progeny_Parent_folder + "/sequence_Profiles.csv";
function.config_File_delete_create(this->progeny_File, "Source\tTarget\tType");
}
if (write_Sequences == "YES")
{
this->write_Sequences = "YES";
this->progeny_sequences_Folder = this->output_Folder + "/sequences";
function.config_Folder(this->progeny_sequences_Folder, "Progeny sequences");
}
this->references_Folder = reference_genome_Location;
cout << "Reference sequences folder: " << this->references_Folder << endl;
this->replication_profile_file = replication_profile_file;
cout << "Replication profile: " << this->replication_profile_file << endl;
this->sequence_profile_file = sequence_Profile_file;
cout << "Sequence profile: " << this->sequence_profile_file << endl;
cout << endl;
}
__global__ void cuda_convert_back_Sequence(int genome_Size, int **sequences, char *cuda_Sequence, int sequence_Index)
{
int tid = threadIdx.x + blockIdx.x * blockDim.x;
while (tid < genome_Size)
{
int base_Numeral = sequences[sequence_Index][tid];
if (base_Numeral == 0)
{
cuda_Sequence[tid] = 'A';
}
else if (base_Numeral == 1)
{
cuda_Sequence[tid] = 'T';
}
else if (base_Numeral == 2)
{
cuda_Sequence[tid] = 'G';
}
else if (base_Numeral == 3)
{
cuda_Sequence[tid] = 'C';
}
tid += blockDim.x * gridDim.x;
}
}
__global__ void cuda_fill_Master_sequences(int genome_Size, int *master_Sequence, char *cuda_reference)
{
int tid = threadIdx.x + blockIdx.x * blockDim.x;
while (tid < genome_Size)
{
// A = 0
// T = 1
// G = 2
// C = 3
char base = cuda_reference[tid];
if (base == 'A' || base == 'a')
{
master_Sequence[tid] = 0;
}
else if (base == 'T' || base == 't')
{
master_Sequence[tid] = 1;
}
else if (base == 'G' || base == 'g')
{
master_Sequence[tid] = 2;
}
else if (base == 'C' || base == 'c')
{
master_Sequence[tid] = 3;
}
tid += blockDim.x * gridDim.x;
}
}
void within_host_test_2::ingress(float rep_time, float host_days, string mode)
{
functions_library function = functions_library(tot_Blocks, tot_ThreadsperBlock, gpu_Limit, CPU_cores);
// int size_Array = 2;
// int *test_Sum = (int *)malloc(sizeof(int) * size_Array);
// for (int i = 0; i < size_Array; i++)
// {
// test_Sum[i] = 1;
// }
// int *cuda_test_Sum;
// cudaMallocManaged(&cuda_test_Sum, size_Array * sizeof(int));
// cudaMemcpy(cuda_test_Sum, test_Sum, size_Array * sizeof(int), cudaMemcpyHostToDevice);
// int sum = function.sum_CUDA(cuda_test_Sum, size_Array);
// cout << sum << endl;
// exit(-1);
cout << "Time in host (days) : " << host_days << endl;
cout << "Time per replication (days): " << rep_time << endl;
int generations = ceil(host_days / rep_time);
cout << "Generations (upper round) : " << generations << endl
<< endl;
// exit(-1);
cout << "Sequences found: " << endl;
vector<string> reference_Sequence_list = function.get_Files(references_Folder, ".fasta");
int parents_in_current_generation = reference_Sequence_list.size();
cout << "\nTotal reference sequences: " << parents_in_current_generation << endl
<< endl;
vector<string> parent_Sequence_Headers;
// int parents_in_current_generation = total_ref_Sequences;
int *parents = (int *)malloc(parents_in_current_generation * sizeof(int));
// vector<string> parent_IDs;
// for (size_t i = 0; i < parents_in_current_generation; i++)
// {
// parents[i] = i;
// if (this->write_Progeny_parent == "YES")
// {
// parent_IDs.push_back("0_" + to_string(i));
// }
// }
string parent_Sequences_Store = this->intermediate_sequence_Store + "/generation_0";
function.config_Folder(parent_Sequences_Store, "Generation 0 sequence store");
for (int num_Sequences_ref = 0; num_Sequences_ref < parents_in_current_generation; num_Sequences_ref++)
{
int *cuda_parent_Sequences;
parents[num_Sequences_ref] = num_Sequences_ref;
// parent_IDs.push_back("0_" + to_string(num_Sequences_ref));
int genome_bp;
string header;
string reference_Seq = "";
cout << "Processing sequence " << num_Sequences_ref + 1 << endl;
reference_Seq = function.read_Reference(reference_Sequence_list[num_Sequences_ref], header, genome_bp);
// cout << reference_Seq << endl;
string clean_Header = header.substr(1);
clean_Header.erase(std::remove(clean_Header.begin(), clean_Header.end(), '\r'), clean_Header.end());
// cout << clean_Header << endl;
// exit(-1);
parent_Sequence_Headers.push_back(clean_Header);
if (num_Sequences_ref == 0)
{
this->genome_SIZE = genome_bp;
function.genome_Size = genome_bp;
// cudaMallocManaged(&cuda_parent_Sequences, (genome_SIZE + 1) * reference_Sequence_list.size() * sizeof(int));
// int **tmp = (int **)malloc(reference_Sequence_list.size() * sizeof(tmp[0]));
// for (int i = 0; i < reference_Sequence_list.size(); i++)
// {
// cudaMalloc((void **)&tmp[i], (genome_SIZE + 1) * sizeof(tmp[0][0]));
// }
// cudaMemcpy(cuda_parent_Sequences, tmp, reference_Sequence_list.size() * sizeof(int *), cudaMemcpyHostToDevice);
// free(tmp);
}
else
{
if (genome_bp != genome_SIZE)
{
cout << "ERROR Sequence sizes do not match: " << reference_Sequence_list[num_Sequences_ref] << endl;
exit(-1);
}
}
cudaMalloc(&cuda_parent_Sequences, genome_SIZE * sizeof(int));
char *reference_full, *cuda_reference;
reference_full = (char *)malloc((reference_Seq.size() + 1) * sizeof(char));
cudaMallocManaged(&cuda_reference, (reference_Seq.size() + 1) * sizeof(char));
strcpy(reference_full, reference_Seq.c_str());
cudaMemcpy(cuda_reference, reference_full, (reference_Seq.size() + 1) * sizeof(char), cudaMemcpyHostToDevice);
free(reference_full);
// cuda_fill_Master_sequences(int genome_Size, int **master_Sequence, char *cuda_reference, int ref_Num)
cuda_fill_Master_sequences<<<tot_Blocks, tot_ThreadsperBlock>>>(this->genome_SIZE, cuda_parent_Sequences, cuda_reference);
cudaDeviceSynchronize();
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess)
{
printf("CUDA Error (Master fill sequences): %s\n", cudaGetErrorString(err));
}
int *parent_Sequences = (int *)malloc(genome_SIZE * sizeof(int));
cudaMemcpy(parent_Sequences, cuda_parent_Sequences, genome_SIZE * sizeof(int), cudaMemcpyDeviceToHost);
string n_FASTA_location = parent_Sequences_Store + "/0_" + to_string(num_Sequences_ref) + ".nfasta";
function.config_File_delete_create(n_FASTA_location);
fstream nfasta_Write;
nfasta_Write.open(n_FASTA_location, ios::out);
if (nfasta_Write.is_open())
{
for (int base_Write = 0; base_Write < genome_SIZE; base_Write++)
{
nfasta_Write << to_string(parent_Sequences[base_Write]);
}
nfasta_Write.close();
}
free(parent_Sequences);
cudaFree(cuda_reference);
cudaFree(cuda_parent_Sequences);
}
// function.find_Unique_values();
// exit(-1);
// Load sequence profiles and mutation profiles
// MUTATION PROFILE LOAD IN PROGRESS
parameter_load Parameters = parameter_load();
vector<string> activate_List = {"\"Mutation activate\"",
"\"Recombination activate\"",
"\"Proof reading activate\""};
vector<string> activate_Parameters = Parameters.get_parameters(replication_profile_file, activate_List);
string mutation_Activate = Parameters.get_STRING(activate_Parameters[0]);
string recombination_Activate = Parameters.get_STRING(activate_Parameters[1]);
string proof_Reading = Parameters.get_STRING(activate_Parameters[2]);
// int rows_seqeunce_mutation_Tracker = 0;
transform(mutation_Activate.begin(), mutation_Activate.end(), mutation_Activate.begin(), ::toupper);
if (mutation_Activate == "YES")
{
this->mutation_Activate_parent = 1;
// rows_seqeunce_mutation_Tracker++;
}
transform(recombination_Activate.begin(), recombination_Activate.end(), recombination_Activate.begin(), ::toupper);
if (recombination_Activate == "YES")
{
this->recombination_Activate_parent = 1;
// rows_seqeunce_mutation_Tracker = rows_seqeunce_mutation_Tracker + 2;
}
transform(proof_Reading.begin(), proof_Reading.end(), proof_Reading.begin(), ::toupper);
if (proof_Reading == "YES")
{
this->proof_reading_Activate_parent = 1;
}
sequence_Mutation_tracker = function.create_Fill_2D_array(7, this->genome_SIZE, -1);
// for (size_t row = 0; row < 3; row++)
// {
// for (size_t col = 0; col < this->genome_SIZE; col++)
// {
// cout << sequence_Mutation_tracker[row][col] << " ";
// }
// // cin.ignore();
// // cout << row + 1 << endl;
// }
// exit(-1);
configure_Mutation_Profiles(generations);
fitness_Profiles();
survivability_Profiles();
configure_Recombination_Profiles();
configure_proof_reading_Profiles();
// cout << sequence_Mutation_tracker[4][149] << "\n";
// for (size_t r = 0; r < 5; r++)
// {
// for (size_t i = 0; i < this->genome_SIZE; i++)
// {
// cout << sequence_Mutation_tracker[r][i] << " ";
// }
// cout << endl;
// }
configure_sequence_Profile(parents_in_current_generation, parent_Sequence_Headers);
// exit(-1);
// for (size_t r = 0; r < 2; r++)
// {
// for (size_t c = 0; c < 10; c++)
// {
// cout << current_gen_Parent_data[r][c] << " ";
// }
// cout << endl;
// }
// exit(-1);
// Proof checking mechanism
cout << "Processing replication phase data:" << endl;
allocate_Phases(generations);
cout << endl;
cout << "Getting cell distribution data for viral units:" << endl;
string cell_distribution_Type;
get_Cell_distribution(cell_distribution_Type);
if (cell_Limit != -1)
{
cout << "Number of cells available for infecetion is limited\n";
cout << "Available cell distribution shape: " << total_Cells_shape << endl
<< "Available cell distribution scale: " << total_Cells_scale << endl;
gamma_distribution<double> gamma_Cells(total_Cells_shape, total_Cells_scale);
cell_Limit = (int)gamma_Cells(gen);
cout << "Cells available for infection: " << cell_Limit << endl;
}
cout << "Distribution type: " << cell_distribution_Type << endl;
if (cell_distribution_Type == "Gamma")
{
cout << "Shape: " << this->cell_shape << endl;
cout << "Scale: " << this->cell_scale << endl;
}
else if (cell_distribution_Type == "Negative binomial")
{
cout << "R: " << this->cell_r << endl;
cout << "Probability: " << this->cell_prob << endl;
}
cout << endl;
// exit(-1);
cout << "Getting progeny distribution data for viral units:" << endl;
// string progeny_distribution_Type;
get_Progeny_distribution(function);
cout << "Distribution type: " << function.progeny_distribution_Type << endl;
if (function.progeny_distribution_Type == "Gamma")
{
cout << "Shape: " << function.progeny_shape << endl;
cout << "Scale: " << function.progeny_scale << endl;
}
else if (function.progeny_distribution_Type == "Negative binomial")
{
cout << "Mean: " << function.progeny_mean << endl;
cout << "Dispersion: " << function.progeny_dispersion << endl;
function.progeny_prob = function.progeny_dispersion / (function.progeny_mean + function.progeny_dispersion);
function.progeny_r = round((function.progeny_mean * function.progeny_prob) / (1 - function.progeny_prob));
cout << "R: " << function.progeny_r << endl;
cout << "Probability: " << function.progeny_prob << endl;
}
cout << endl;
// exit(-1);
// rows
cout << "Intializing within host engine\n"
<< endl;
// DELETE
// parents_in_current_generation = 15;
// int **parent_child;
//= function.create_INT_2D_arrays(sequences_in_current_generation, 2);
// for (size_t i = 0; i < sequences_in_current_generation; i++)
// {
// cout << name_ID[parent_child[i][0]] << "\t" << name_ID[parent_child[i][1]] << endl;
// }
string parent_Profiles_Store = this->intermediate_profile_Store + "/generation_0";
function.config_Folder(parent_Profiles_Store, "Generation 0 profile store");
// //! Remove
// float **CUDA_current_gen_Parent_data = function.float_2D_Array_load_to_CUDA(this->current_gen_Parent_data, parents_in_current_generation, 1 + (3 * recombination_hotspots));
// //! Remove
// float *CUDA_parent_Proof_reading_probability;
// if (proof_reading_Activate_parent != 0)
// {
// CUDA_parent_Proof_reading_probability = function.copy_1D_to_CUDA_FLOAT(sequences_Proof_reading_probability, parents_in_current_generation);
// }
// int **CUDA_recombination_hotspots_start_stop;
int *stride_Array;
if (recombination_Activate_parent != 0)
{
function.recombination_hotspots = this->recombination_hotspots;
function.CUDA_recombination_hotspots_start_stop = function.int_2D_Array_load_to_CUDA(recombination_hotspots_start_stop, recombination_hotspots, 2);
float **A_0_Recombination = function.create_FLOAT_2D_arrays(rows_Fitness + rows_Prob + rows_Selectivity + rows_Survivability, 6);
float **T_1_Recombination = function.create_FLOAT_2D_arrays(rows_Fitness + rows_Prob + rows_Selectivity + rows_Survivability, 6);
float **G_2_Recombination = function.create_FLOAT_2D_arrays(rows_Fitness + rows_Prob + rows_Selectivity + rows_Survivability, 6);
float **C_3_Recombination = function.create_FLOAT_2D_arrays(rows_Fitness + rows_Prob + rows_Selectivity + rows_Survivability, 6);
stride_Array = (int *)malloc(5 * sizeof(int));
stride_Array[0] = 0;
stride_Array[1] = stride_Array[0] + rows_Prob;
stride_Array[2] = stride_Array[1] + rows_Selectivity;
stride_Array[3] = stride_Array[2] + rows_Fitness;
stride_Array[4] = stride_Array[3] + rows_Survivability;
function.CUDA_stride_Array = function.copy_1D_to_CUDA_INT(stride_Array, 5);
for (int row_Prob = 0; row_Prob < rows_Prob; row_Prob++)
{
for (int col = 0; col < 6; col++)
{
A_0_Recombination[stride_Array[0] + row_Prob][col] = A_0_probability_Recombination[row_Prob][col];
T_1_Recombination[stride_Array[0] + row_Prob][col] = T_1_probability_Recombination[row_Prob][col];
G_2_Recombination[stride_Array[0] + row_Prob][col] = G_2_probability_Recombination[row_Prob][col];
C_3_Recombination[stride_Array[0] + row_Prob][col] = C_3_probability_Recombination[row_Prob][col];
}
}
for (int row_Selectivity = 0; row_Selectivity < rows_Selectivity; row_Selectivity++)
{
for (int col = 0; col < 6; col++)
{
A_0_Recombination[stride_Array[1] + row_Selectivity][col] = A_0_selectivity_Recombination[row_Selectivity][col];
T_1_Recombination[stride_Array[1] + row_Selectivity][col] = T_1_selectivity_Recombination[row_Selectivity][col];
G_2_Recombination[stride_Array[1] + row_Selectivity][col] = G_2_selectivity_Recombination[row_Selectivity][col];
C_3_Recombination[stride_Array[1] + row_Selectivity][col] = C_3_selectivity_Recombination[row_Selectivity][col];
}
}
for (int row_Fitness = 0; row_Fitness < rows_Fitness; row_Fitness++)
{
for (int col = 0; col < 6; col++)
{
A_0_Recombination[stride_Array[2] + row_Fitness][col] = A_0_fitness_Recombination[row_Fitness][col];
T_1_Recombination[stride_Array[2] + row_Fitness][col] = T_1_fitness_Recombination[row_Fitness][col];
G_2_Recombination[stride_Array[2] + row_Fitness][col] = G_2_fitness_Recombination[row_Fitness][col];
C_3_Recombination[stride_Array[2] + row_Fitness][col] = C_3_fitness_Recombination[row_Fitness][col];
}
}
for (int row_Survivability = 0; row_Survivability < rows_Survivability; row_Survivability++)
{
for (int col = 0; col < 6; col++)
{
A_0_Recombination[stride_Array[3] + row_Survivability][col] = A_0_survivability_Recombination[row_Survivability][col];
T_1_Recombination[stride_Array[3] + row_Survivability][col] = T_1_survivability_Recombination[row_Survivability][col];
G_2_Recombination[stride_Array[3] + row_Survivability][col] = G_2_survivability_Recombination[row_Survivability][col];
C_3_Recombination[stride_Array[3] + row_Survivability][col] = C_3_survivability_Recombination[row_Survivability][col];
}
}
// for (int type = 0; type < 3; type++)
// {
// for (int row = stride_Array[type]; row < stride_Array[type + 1]; row++)
// {
// for (int col = 0; col < 6; col++)
// {
// cout << A_0_Recombination[row][col] << " ";
// }
// cout << endl;
// }
// cout << endl;
// }
function.CUDA_A_0_Recombination = function.float_2D_Array_load_to_CUDA(A_0_Recombination, (rows_Prob + rows_Fitness + rows_Selectivity + rows_Survivability), 6);
function.CUDA_T_1_Recombination = function.float_2D_Array_load_to_CUDA(T_1_Recombination, (rows_Prob + rows_Fitness + rows_Selectivity + rows_Survivability), 6);
function.CUDA_G_2_Recombination = function.float_2D_Array_load_to_CUDA(G_2_Recombination, (rows_Prob + rows_Fitness + rows_Selectivity + rows_Survivability), 6);
function.CUDA_C_3_Recombination = function.float_2D_Array_load_to_CUDA(C_3_Recombination, (rows_Prob + rows_Fitness + rows_Selectivity + rows_Survivability), 6);
free(A_0_Recombination);
free(T_1_Recombination);
free(G_2_Recombination);
free(C_3_Recombination);
free(A_0_fitness_Recombination);
free(T_1_fitness_Recombination);
free(G_2_fitness_Recombination);
free(C_3_fitness_Recombination);
free(A_0_selectivity_Recombination);
free(T_1_selectivity_Recombination);
free(G_2_selectivity_Recombination);
free(C_3_selectivity_Recombination);
free(A_0_probability_Recombination);
free(T_1_probability_Recombination);
free(G_2_probability_Recombination);
free(C_3_probability_Recombination);
free(A_0_survivability_Recombination);
free(T_1_survivability_Recombination);
free(G_2_survivability_Recombination);
free(C_3_survivability_Recombination);
free(recombination_hotspots_start_stop);
free(stride_Array);
}
// exit(-1);
if (mutation_Activate_parent != 0)
{
function.mutation_hotspots = this->mutation_hotspots;
function.CUDA_mutation_rates_Hotspot_generation = function.float_2D_Array_load_to_CUDA(this->mutation_rates_Hotspot_generation, this->mutation_hotspots, generations);
function.CUDA_mutation_Regions_start_stop = function.int_2D_Array_load_to_CUDA(this->mutation_Regions_start_stop, mutation_hotspots, 2);
free(mutation_rates_Hotspot_generation);
free(mutation_Regions_start_stop);
function.CUDA_A_0_mutation = function.float_2D_Array_load_to_CUDA(A_0_mutation, this->mutation_hotspots, 4);
function.CUDA_T_1_mutation = function.float_2D_Array_load_to_CUDA(T_1_mutation, this->mutation_hotspots, 4);
function.CUDA_G_2_mutation = function.float_2D_Array_load_to_CUDA(G_2_mutation, this->mutation_hotspots, 4);
function.CUDA_C_3_mutation = function.float_2D_Array_load_to_CUDA(C_3_mutation, this->mutation_hotspots, 4);
free(A_0_mutation);
free(T_1_mutation);
free(G_2_mutation);
free(C_3_mutation);
}
if (this->fitness_points != -1)
{
function.CUDA_A_0_fitness = function.float_2D_Array_load_to_CUDA(A_0_fitness, this->fitness_points, 4);
function.CUDA_T_1_fitness = function.float_2D_Array_load_to_CUDA(T_1_fitness, this->fitness_points, 4);
function.CUDA_G_2_fitness = function.float_2D_Array_load_to_CUDA(G_2_fitness, this->fitness_points, 4);
function.CUDA_C_3_fitness = function.float_2D_Array_load_to_CUDA(C_3_fitness, this->fitness_points, 4);
free(A_0_fitness);
free(T_1_fitness);
free(G_2_fitness);
free(C_3_fitness);
}
if (this->survivability_points != -1)
{
function.CUDA_A_0_survivability = function.float_2D_Array_load_to_CUDA(A_0_survivability, this->fitness_points, 4);
function.CUDA_T_1_survivability = function.float_2D_Array_load_to_CUDA(T_1_survivability, this->fitness_points, 4);
function.CUDA_G_2_survivability = function.float_2D_Array_load_to_CUDA(G_2_survivability, this->fitness_points, 4);
function.CUDA_C_3_survivability = function.float_2D_Array_load_to_CUDA(C_3_survivability, this->fitness_points, 4);
free(A_0_survivability);
free(T_1_survivability);
free(G_2_survivability);
free(C_3_survivability);
}
if (this->proof_Reading_mutations != -1)
{
function.CUDA_A_0_probability_Proof_reading = function.float_2D_Array_load_to_CUDA(A_0_probability_Proof_reading, this->proof_Reading_mutations, 4);
function.CUDA_T_1_probability_Proof_reading = function.float_2D_Array_load_to_CUDA(T_1_probability_Proof_reading, this->proof_Reading_mutations, 4);
function.CUDA_G_2_probability_Proof_reading = function.float_2D_Array_load_to_CUDA(G_2_probability_Proof_reading, this->proof_Reading_mutations, 4);
function.CUDA_C_3_probability_Proof_reading = function.float_2D_Array_load_to_CUDA(C_3_probability_Proof_reading, this->proof_Reading_mutations, 4);
free(A_0_probability_Proof_reading);
free(T_1_probability_Proof_reading);
free(G_2_probability_Proof_reading);
free(C_3_probability_Proof_reading);
}
if (this->write_Progeny_parent == "YES")
{
string header_recombination = "ID\tParent_ID";
string header_profiles = "ID\tGeneration\tSurvivability\tFitness";
if (proof_reading_Activate_parent != 0)
{
header_profiles = header_profiles + "\tProof_reading_accuracy";
}
int recomb_Columns = 0;
// cout << "Check 1\n";
if (recombination_hotspots != -1)
{
for (size_t i = 0; i < recombination_hotspots; i++)
{
header_recombination = header_recombination + "\t" + "Recombination_hotspot_" + to_string(i + 1);
header_profiles = header_profiles +
"\t" + "Recombination_hotspot_" + to_string(i + 1) + "_Selectivity" +
"\t" + "Recombination_hotspot_" + to_string(i + 1) + "_Fitness" +
"\t" + "Recombination_hotspot_" + to_string(i + 1) + "_Ratio_Progeny" +
"\t" + "Recombination_hotspot_" + to_string(i + 1) + "_Survivability";
}
recomb_Columns = recombination_hotspots;
}
// cout << "Check 2\n";
header_profiles = header_profiles + "\tSurvive_or_Not";
function.config_File_delete_create(this->progeny_Recombination_File, header_recombination);
function.config_File_delete_create(this->sequence_Profiles, header_profiles);
fstream profile_File;
profile_File.open(sequence_Profiles, ios::app);
if (profile_File.is_open())
{
// cout << "Check 3\n";
for (size_t i = 0; i < parents_in_current_generation; i++)
{
fstream parent_Profile_Files;
fstream parent_Probability_Files;
fstream survivability_Files;
parent_Profile_Files.open(parent_Profiles_Store + "/0_" + to_string(parents[i]) + ".profile", ios::out);
survivability_Files.open(parent_Profiles_Store + "/0_" + to_string(parents[i]) + ".profile_surv", ios::out);
// cout << "1\n";
if (proof_reading_Activate_parent != 0)
{
function.proof_reading_Activate_parent = 1;
parent_Probability_Files.open(parent_Profiles_Store + "/0_" + to_string(parents[i]) + ".profile_prob", ios::out);
}
// cout << "2\n";
parent_Profile_Files << to_string(current_gen_Parent_data[i][0]);
// cout << "3\n";
if (proof_reading_Activate_parent != 0)
{
parent_Probability_Files << to_string(sequences_Proof_reading_probability[i]);
parent_Probability_Files.close();
}
// cout << "4\n";
survivability_Files << to_string(sequences_Survivability[i][0]);
// cout << "sub Check 1\n";
for (int hotspot_surv = 0; hotspot_surv < recomb_Columns; hotspot_surv++)
{
survivability_Files << "\t" << to_string(sequences_Survivability[i][hotspot_surv + 1]);
}
survivability_Files << "\n";
survivability_Files.close();
profile_File << "0_" << to_string(parents[i]) << "\t"
<< "0\t" << to_string(sequences_Survivability[i][0])
<< "\t" << to_string(current_gen_Parent_data[i][0]);
if (proof_reading_Activate_parent != 0)
{
profile_File << "\t" << to_string(sequences_Proof_reading_probability[i]);
}
int track = 0;
// cout << "sub Check 2\n";
for (int hotspots = 0; hotspots < (recomb_Columns * 3); hotspots++)
{
profile_File << "\t" << to_string(current_gen_Parent_data[i][hotspots + 1]);
if (hotspots == ((track * 3) + 2))
{
profile_File << "\t" << to_string(sequences_Survivability[i][track + 1]);
track++;
}
parent_Profile_Files << "\t" << to_string(current_gen_Parent_data[i][hotspots + 1]);
}
profile_File << "\tYes";
profile_File << "\n";
parent_Profile_Files.close();
}
// cout << " sub Check final\n";
profile_File.close();
}
// cout << "Check 4\n";
}
// exit(-1);
function.CUDA_sequence_Mutation_tracker = function.int_2D_Array_load_to_CUDA(this->sequence_Mutation_tracker, 7, this->genome_SIZE);
// Assign remaining variables to functions
// for (size_t i = 0; i < generations; i++)
// {
// cout << "**************************" << endl;
// cout << "Generation: " << i << endl;
// cout << "generation_modes: " << generation_modes[i] << endl;
// cout << "phase_Modes: " << phase_Modes[generation_modes[i]] << endl;
// cout << "phase_paramters: " << phase_parameters[generation_modes[i]][0] << " " << phase_parameters[generation_modes[i]][1] << " " << phase_parameters[generation_modes[i]][2] << endl;
// cout << "**************************" << endl;
// }
// exit(-1);
string generation_Summary_Folder = this->output_Folder + "/generations_Summary";
function.config_Folder(generation_Summary_Folder, "Generational summary data");
string generation_Summary_File = generation_Summary_Folder + "/generation_Summary_File.csv";
function.config_File_delete_create(generation_Summary_File, "Generation\tType\tParents_number\tProgeny_number");
function.mode = mode;
function.cells_of_parents = this->progeny_Parent_folder + "/cells_of_parents.csv";
function.config_File_delete_create(function.cells_of_parents, "ID\tParent_Cell_ID");
function.cells_of_progeny = this->progeny_Parent_folder + "/cells_of_progeny.csv";
function.config_File_delete_create(function.cells_of_progeny, "ID\tProgeny_Cell_ID");
for (int generation = 0; generation < generations; generation++)
{
// parents_in_current_generation = 50;
int gen_Real = generation + 1;
cout << "Processing " << gen_Real << " of " << generations << " generations" << endl
<< endl;
// Assigning viral particles per cell
string generation_line = to_string(generation + 1) + "\t";
if (phase_Modes[generation_modes[generation]] == 0)
{
generation_line = generation_line + "Growth\t";
}
else if (phase_Modes[generation_modes[generation]] == 1)
{
generation_line = generation_line + "Stationary\t";
}
else
{
generation_line = generation_line + "Depriciation\t";
}
if (parents_in_current_generation > 0)
{
// parents_in_current_generation = 50;
// free(parents);
// parents = (int *)malloc(parents_in_current_generation * sizeof(int));
// for (size_t i = 0; i < parents_in_current_generation; i++)
// {
// parents[i] = i;
// }
cout << "Potential parents in generation " << generation + 1 << ": " << parents_in_current_generation << endl;
generation_line = generation_line + to_string(parents_in_current_generation);
// cout << "Calculating parental fitness" << endl;
// float *cuda_Parental_Fitness = parent_Fitness(parents_in_current_generation, CUDA_current_gen_Parent_data);
string progeny_Sequences_Store = this->intermediate_sequence_Store + "/generation_" + to_string(generation + 1);
function.config_Folder(progeny_Sequences_Store, "Generation " + to_string(generation + 1) + " sequence store");
string progeny_Profile_Store = this->intermediate_profile_Store + "/generation_" + to_string(generation + 1);
function.config_Folder(progeny_Profile_Store, "Generation " + to_string(generation + 1) + " sequence profile store");
// int *parents_Cells = (int *)malloc(parents_in_current_generation * sizeof(int));
vector<int> cells_Start_Stop;
// int cell_ID = 0;
cells_Start_Stop.push_back(0);
// int max_Count = 0;
cout << "Attaching viral unit(s) to cell(s)" << endl;
int assigned_parents = 0;
while (assigned_parents < parents_in_current_generation)
{
int num_particles;
if (cell_distribution_Type == "Gamma")
{
gamma_distribution<float> dist(cell_shape, cell_scale);
num_particles = (int)round(dist(gen));
// cout << cell_Count << endl;
}
else if (cell_distribution_Type == "Negative binomial")
{
negative_binomial_distribution<int> dist(cell_r, cell_prob);
num_particles = dist(gen);
}
// cout << "p: " << num_particles << endl;
int current_Count = 0;
if (num_particles > 0)
{
// unique_Cells++;
for (int i = 0; i < num_particles; i++)
{
if (assigned_parents >= parents_in_current_generation)
{
break;
}
else
{
// cout << "a: " << assigned_parents << endl;
// parents_Cells[assigned_parents] = cell_ID;
assigned_parents++;
current_Count++;
}
}
// if (current_Count > max_Count)
// {
// max_Count = current_Count;
// }
// cell_ID++;
cells_Start_Stop.push_back(cells_Start_Stop[cells_Start_Stop.size() - 1] + current_Count);
}
if (cell_Limit != -1)
{
if ((cells_Start_Stop.size() - 1) >= cell_Limit)
{
// cout << "cell_Limit reached: " << cell_Limit << endl;
// cout << "cells infected: " << cells_Start_Stop.size() - 1;
// exit(-1);
break;
}
}
}
// scramble parents array then assign them to the cells.
// int array_Size = sizeof(parents) / sizeof(parents[0]);
// for (size_t i = 0; i < cells_Start_Stop.size() - 1; i++)
// {
// for (int print = cells_Start_Stop[i]; print < cells_Start_Stop[i + 1]; print++)
// {
// cout << parents[print] << " ";
// }
// cout << endl;
// }
random_shuffle(&parents[0], &parents[parents_in_current_generation]);
// int temp = parents[0];
// parents[0] = parents[1];
// parents[1] = temp;
int num_Unique_cells = cells_Start_Stop.size() - 1;
cout << "Number of infected cell(s): " << num_Unique_cells << endl
<< endl;
// exit(-1);
vector<pair<int, int>> start_Stop_Rounds;
int full_Rounds = num_Unique_cells / this->at_a_Time_cells;
int partial_Rounds = num_Unique_cells % this->at_a_Time_cells;
for (int full = 0; full < full_Rounds; full++)
{
int start = full * this->at_a_Time_cells;
int stop = start + this->at_a_Time_cells;
start_Stop_Rounds.push_back(make_pair(start, stop));
}
if (partial_Rounds != 0)
{
int start = num_Unique_cells - partial_Rounds;
start_Stop_Rounds.push_back(make_pair(start, num_Unique_cells));
}
int sum_Progeny_in_Generation = 0;
int cells_Processed = 0;
vector<int> surviving_Progeny;
for (int cells_Process = 0; cells_Process < start_Stop_Rounds.size(); cells_Process++)
{
int num_of_Cells = start_Stop_Rounds[cells_Process].second - start_Stop_Rounds[cells_Process].first;
cout << "Processing round " << cells_Process + 1 << " of " << start_Stop_Rounds.size() << ": " << num_of_Cells << " cell(s)" << endl;
function.process_Cells(this->multi_READ, generation, sum_Progeny_in_Generation,
num_of_Cells, start_Stop_Rounds[cells_Process].first, start_Stop_Rounds[cells_Process].second,
cells_Start_Stop,
parent_Profiles_Store, parents,
parent_Sequences_Store,
progeny_File, progeny_Recombination_File, sequence_Profiles,
progeny_Sequences_Store, progeny_Profile_Store,
cells_Processed, surviving_Progeny);
// cout << endl;
}
cout << "Completed generation via " << start_Stop_Rounds.size() << " rounds" << endl;
cout << "\nSimulated progeny: " << sum_Progeny_in_Generation << endl;
cout << "Progeny that survived till parenthood: " << surviving_Progeny.size() << endl;
cout << "Progeny that perished before becoming parents: " << sum_Progeny_in_Generation - surviving_Progeny.size() << endl;
// if (generation == 1)
// {
// exit(-1);
// }
generation_line = generation_line + "\t" + to_string(sum_Progeny_in_Generation) + "\n";
cout << "Purging parent profile intermediaries" << endl;
if (filesystem::exists(parent_Profiles_Store))
{
filesystem::remove_all(parent_Profiles_Store);
// string sourceFolder = parent_Sequences_Store;
string tar_Folder = parent_Sequences_Store + ".tar.gz";
string command_Tar = "tar -czf " + tar_Folder + " " + parent_Sequences_Store + " && rm -R " + parent_Sequences_Store;
int result = system(command_Tar.c_str());
if (result == 0)
{
cout << "Tar successful" << endl;
}
else
{
cout << "Failed to tar the sequence folder: " << parent_Sequences_Store << endl;
exit(-1);
}
}
parent_Profiles_Store = progeny_Profile_Store;
parent_Sequences_Store = progeny_Sequences_Store;
cout << endl;
free(parents);
// if (generation == 1)
// {
// cout << "Stop check" << endl;
// exit(-1);
// }
if (phase_Modes[generation_modes[generation]] == 0)