-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcancer_Host.cu
4578 lines (3842 loc) · 238 KB
/
cancer_Host.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 "cancer_Host.cuh"
cancer_Host::cancer_Host()
{
cout << "\nSTEP 5: Cancer host intialization\n";
}
void cancer_Host::cell_Migration_set(int &max_Limit, multiset<pair<float, int>> &migration_cell_List, pair<float, int> candidate_Cell)
{
cout << "\nMigration set\n";
migration_cell_List.insert(candidate_Cell);
if (migration_cell_List.size() > max_Limit)
{
migration_cell_List.erase(prev(migration_cell_List.end()));
}
}
void cancer_Host::simulate_Generations(functions_library &functions,
int &overall_Generations, float &date_Increment,
int &stop_Type,
int &stop_gen_Mode,
int &stop_generations_Count, float &decimal_Date, float &stop_Date,
vector<string> &tissue_Names,
int &terminal_tissues, int *terminal_array,
string source_sequence_Data_folder,
string &enable_Folder_management, string &enable_Compression,
int &terminal_Load,
string &output_Node_location,
vector<vector<float>> &time_Ratios_per_Tissue, vector<vector<string>> &phase_Type_per_tissue, vector<vector<pair<float, float>>> &phase_paramaters_per_Tissue,
int &max_Cells_at_a_time,
string &multi_Read, int &CPU_cores, int &num_Cuda_devices, int *CUDA_device_IDs,
int &genome_Length,
float *Reference_fitness_survivability_proof_reading, float *Reference_cancer_parameters,
float **A_0_mutation,
float **T_1_mutation,
float **G_2_mutation,
float **C_3_mutation,
int &mutation_Hotspots,
float **mutation_hotspot_parameters,
int *num_effect_Segregating_sites,
float **sequence_Survivability_changes,
float **sequence_Proof_reading_changes,
int *num_effect_Segregating_sites_Cancer,
float **sequence_replication_factor_changes,
float **sequence_mutation_rate_changes,
float **sequence_generation_death_changes,
float **sequence_replication_prob_changes,
float **sequence_metastatic_prob_changes,
int &max_sequences_per_File,
string &viral_Migration, float **viral_Migration_Values, int *migration_start_Generation,
int &count_tajima_Regions, int **tajima_regions_Start_Stop,
string &reference_Genome_location,
int *tissue_selection_Position_Count,
int *Survivability_Positions,
int *Proof_Positions,
int *Replication_factor_Positions,
int *Mutation_rate_factor_Positions,
int *Generation_death_Positions,
int *Replication_prob_Positions,
int *Metastatic_Positions,
float **tissues_ATGC_positions_Survivability,
float **tissues_ATGC_positions_Proof,
float **tissues_ATGC_positions_Replication_factor,
float **tissues_ATGC_positions_Mutation_rate_factor,
float **tissues_ATGC_positions_Generation_death,
float **tissues_ATGC_positions_Replication_prob,
float **tissues_ATGC_positions_Metastatic,
int *profile_tissue_Limits,
string null_Distribution_mode,
string intermediate_null_Location,
string result_null_Location,
int null_buffer_generation,
int null_target_generation,
string temp_Original_intermediary)
{
cout << "\nSTEP 6: Conducting simulation\n";
// this->terminal_Load = terminal_Load;
random_device rd;
mt19937 gen(rd());
this->CPU_cores = CPU_cores;
this->genome_Length = genome_Length;
if (null_Distribution_mode == "YES")
{
cout << "\nConfiguring null distribution generation intialization:\n";
num_Tissues = tissue_Names.size();
source_sequence_Data_folder = intermediate_null_Location + "/sequence_Data/cancer_Host";
functions.config_Folder(intermediate_null_Location + "/sequence_Data", "Intermediary sequence data");
functions.config_Folder(intermediate_null_Location + "/sequence_Data/cancer_Host", "Intermediary host data");
// output_Node_location = result_null_Location + "/node_Data";
// functions.config_Folder(output_Node_location, "null Results");
vector<vector<pair<string, string>>> tissue_Sequences;
vector<vector<string>> profile_Lines_Tissues;
intialize_Tissues(source_sequence_Data_folder, tissue_Sequences, profile_Lines_Tissues, functions, null_buffer_generation);
// overall_Generations = null_buffer_generation;
cout << "Start generation: " << overall_Generations << endl;
// exit(-1);
cout << "Transferring original intermediary data to null intermediary per tissue:\n";
for (int tissue = 0; tissue < num_Tissues; tissue++)
{
cout << "Processing tissue: " << tissue + 1 << endl;
try
{
cout << "Copying tissue contents\n";
filesystem::copy(temp_Original_intermediary + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations), source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations), filesystem::copy_options::update_existing | filesystem::copy_options::recursive);
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << '\n';
exit(-1);
}
int dead_Count = 0;
if (filesystem::exists(source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations) + "/dead_List.txt"))
{
cout << "Loading dead file: " << source_sequence_Data_folder << "/" + to_string(tissue) << "/generation_" << to_string(overall_Generations) << "/dead_List.txt\n";
fstream dead_File;
dead_File.open(source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations) + "/dead_List.txt", ios::in);
if (dead_File.is_open())
{
string line;
while (getline(dead_File, line))
{
if (line != "")
{
dead_Count++;
}
}
dead_File.close();
dead_Particle_count[tissue] = dead_Count;
}
else
{
cout << "ERROR: UNABLE TO OPEN DEAD LIST FILE: " << source_sequence_Data_folder << "/" + to_string(tissue) << "/generation_" << to_string(overall_Generations) << "/dead_List.txt\n";
exit(-1);
}
}
}
// get genome length
cout << "Getting genome length: ";
for (const auto &entry : filesystem::directory_iterator(source_sequence_Data_folder + "/" + to_string(0) + "/generation_" + to_string(overall_Generations)))
{
if (filesystem::is_regular_file(entry))
{
string check_Extenstion = entry.path().extension();
if (check_Extenstion == ".fasta" || check_Extenstion == ".fa" || check_Extenstion == ".nfa" || check_Extenstion == ".nfasta")
{
fstream sequence_Read;
sequence_Read.open(entry.path().string(), ios::in);
if (sequence_Read.is_open())
{
string line;
// skipe header
getline(sequence_Read, line);
// get first line
getline(sequence_Read, line);
this->genome_Length = line.length();
genome_Length = line.length();
}
else
{
cout << "ERROR: UNABLE TO OPEN SEQUENCE FILE: " << entry.path().string() << endl;
exit(-1);
}
break;
}
}
}
cout << genome_Length << endl;
if (!filesystem::exists(output_Node_location + "/cancer_Host"))
{
functions.config_Folder(output_Node_location + "/cancer_Host", "Cancer host node");
functions.create_File(output_Node_location + "/cancer_Host/sequence_Profiles.csv", "Sequence_ID\tTissue\treplication_Factor\tgenerational_death_Prob\treplication_Prob\tmetastatic_Prob\tSurvivability");
functions.create_File(output_Node_location + "/cancer_Host/sequence_parent_Progeny_relationships.csv", "Source\tTarget\tType");
}
//! end program after the set count of generations
stop_generations_Count = null_target_generation + 1;
}
generational_Summary = output_Node_location + "/cancer_Host/node_generational_Summary.csv";
functions.create_File(generational_Summary, "Generation\tTissue\tPhase\tnum_Parents\tnum_Progeny\tdead_Progeny\trapid_Progeny");
sequence_Profiles = output_Node_location + "/cancer_Host/sequence_Profiles.csv";
// functions.create_File(sequence_Profiles, "Sequence_ID\tTissue");
sequence_parent_Progeny_relationships = output_Node_location + "/cancer_Host/sequence_parent_Progeny_relationships.csv";
string output_Tajima_File = output_Node_location + "/cancer_Host/tajimas_D_time_series.csv";
if (count_tajima_Regions > 0)
{
// string columns_Tajima = "";
// for (int region = 0; region < count_tajima_Regions; region++)
// {
// columns_Tajima = columns_Tajima + "\tRegion_" + to_string(region + 1);
// }
functions.create_File(output_Tajima_File, "Type\tGeneration\tTissue\tN_cells\tseg_Sites\tpairwise_Diff\tCombinations\tPi\ttajima_D");
}
// functions.create_File(sequence_parent_Progeny_relationships, "Source\tTarget\tType");
// cells_of_parents_location = output_Node_location + "/cancer_Host/cells_of_Parents.csv";
// functions.create_File(cells_of_parents_location, "Sequence_ID\tParent_Cell_ID");
// cells_of_progeny_location = output_Node_location + "/cancer_Host/cells_of_Progeny.csv";
// functions.create_File(cells_of_progeny_location, "Sequence_ID\tProgeny_Cell_ID");
do
{
cout << "Calculating actual particles in each tissue: \n";
////clear array
int *real_Particle_count_per_Tissue = (int *)malloc(sizeof(int) * num_Tissues);
int sum_Check = 0;
vector<int> tissue_Migration_Totals;
vector<vector<pair<int, int>>> tissue_migration_Targets_amount;
vector<vector<pair<int, int>>> indexed_Source_Folders = functions.index_sequence_Folders(source_sequence_Data_folder, num_Tissues, overall_Generations, multi_Read);
for (int tissue = 0; tissue < num_Tissues; tissue++)
{
if (indexed_Source_Folders[tissue].size() > 0)
{
current_cell_load_per_Tissue[tissue] = indexed_Source_Folders[tissue][indexed_Source_Folders[tissue].size() - 1].second + 1;
cout << tissue << " Check: " << current_cell_load_per_Tissue[tissue] << endl;
}
real_Particle_count_per_Tissue[tissue] = current_cell_load_per_Tissue[tissue] - removed_by_Transfer_Indexes[tissue].size() - dead_Particle_count[tissue];
cout << tissue_Names[tissue] << " tissue: " << real_Particle_count_per_Tissue[tissue] << endl;
sum_Check = sum_Check + real_Particle_count_per_Tissue[tissue];
tissue_Migration_Totals.push_back(0);
vector<pair<int, int>> intialize_vec;
tissue_migration_Targets_amount.push_back(intialize_vec);
}
if (viral_Migration == "YES")
{
cout << "\nPutative migrating particles per tissue: \n";
for (int migration_Check = 0; migration_Check < (num_Tissues * (num_Tissues - 1)); migration_Check++)
{
if (viral_Migration_Values[migration_Check][0] != -1)
{
if (overall_Generations >= migration_start_Generation[migration_Check])
{
int source = migration_Check / (num_Tissues - 1);
int destination = migration_Check % (num_Tissues - 1);
if (destination >= source)
{
destination = destination + 1;
}
binomial_distribution<int> num_Particles(viral_Migration_Values[migration_Check][0], viral_Migration_Values[migration_Check][1]);
int num_viruses_to_transfer = num_Particles(gen);
tissue_Migration_Totals[source] = tissue_Migration_Totals[source] + num_viruses_to_transfer;
tissue_migration_Targets_amount[source].push_back(make_pair(destination, num_viruses_to_transfer));
}
}
}
for (int tissue = 0; tissue < num_Tissues; tissue++)
{
cout << "Tissue " << tissue_Names[tissue] << ": " << tissue_Migration_Totals[tissue] << endl;
for (int path = 0; path < tissue_migration_Targets_amount[tissue].size(); path++)
{
cout << tissue_migration_Targets_amount[tissue][path].second << " cells to " << tissue_Names[tissue_migration_Targets_amount[tissue][path].first] << endl;
}
}
}
// exit(-1);
if (sum_Check > 0)
{
if (terminal_status(terminal_tissues, terminal_array, source_sequence_Data_folder, enable_Folder_management, enable_Compression, terminal_Load) != 1)
{
// vector<vector<pair<int, int>>> indexed_Source_Folders = functions.index_sequence_Folders(source_sequence_Data_folder, num_Tissues, overall_Generations, multi_Read);
for (int tissue = 0; tissue < num_Tissues; tissue++)
{
cout << "Tissue: " << tissue << endl;
if (real_Particle_count_per_Tissue[tissue] > 0)
{
// get last wrtten progeny number
cout << endl;
int last_Progeny_written_this_Gen = indexed_Source_Folders[tissue][indexed_Source_Folders[tissue].size() - 1].second + 1;
string rapid_Progeny_Location = source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations) + "/" + to_string(last_Progeny_written_this_Gen) + "_rapid_Progeny.nfasta";
cout << "\nSimulating " << real_Particle_count_per_Tissue[tissue] << " particle(s) for " << tissue_Names[tissue] << " tissue\n"
<< endl;
cout << "Identifying indexes to remove\n";
set<int> check_to_Remove;
if (dead_Particle_count[tissue] > 0)
{
cout << "\nIdentifying dead viral indexe(s)\n";
fstream dead_File;
dead_File.open(source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations) + "/dead_List.txt");
if (dead_File.is_open())
{
string line;
// int index = 0;
while (getline(dead_File, line))
{
check_to_Remove.insert(stoi(line));
// index++;
}
dead_File.close();
}
else
{
cout << "ERROR: UNABLE TO OPEN DEAD LIST FILE: " << source_sequence_Data_folder << "/" << tissue << "/generation_" << overall_Generations << "/dead_List.txt" << endl;
exit(-1);
}
}
if (removed_by_Transfer_Indexes[tissue].size() > 0)
{
cout << "Identifying transferred viral indexe(s)\n";
for (auto it = removed_by_Transfer_Indexes[tissue].begin(); it != removed_by_Transfer_Indexes[tissue].end(); ++it)
{
int value = *it; // Dereference the iterator to get the value
check_to_Remove.insert(value);
}
removed_by_Transfer_Indexes[tissue].clear();
}
// cout << "\nFilling parent vector: ";
// int *parents_in_Tissue = (int *)malloc(real_Particle_count_per_Tissue[tissue] * sizeof(int));
// int fill_Count = 0;
// int index = 0;
// do
// {
// if (check_to_Remove.find(index) == check_to_Remove.end())
// {
// parents_in_Tissue[fill_Count] = index;
// fill_Count++;
// }
// index++;
// } while (fill_Count < real_Particle_count_per_Tissue[tissue]);
// cout << "Completed\n";
// cout << "Shuffling parent vector: ";
// default_random_engine rng(time(nullptr)); // Seed the random number generator with current time
// shuffle(parents_in_Tissue, parents_in_Tissue + real_Particle_count_per_Tissue[tissue], rng);
// cout << "Complete\n";
float variable_1, variable_2;
string generation_Type = get_generation_Phase(overall_Generations,
time_Ratios_per_Tissue[tissue],
phase_Type_per_tissue[tissue],
phase_paramaters_per_Tissue[tissue],
variable_1, variable_2);
int parent_population_Count = real_Particle_count_per_Tissue[tissue];
if (overall_Generations != 0)
{
int new_Parent_Count = -1;
if (generation_Type == "STATIONARY")
{
cout << "\nStationary phase\n";
if (parent_population_Count >= parents_Prev_generation[tissue])
{
normal_distribution<float> distribution(parents_Prev_generation[tissue], variable_1);
new_Parent_Count = (int)(distribution(gen) + 0.5);
if (new_Parent_Count < parent_population_Count && new_Parent_Count >= 0)
{
parent_population_Count = new_Parent_Count;
cout << "Parent population maintained at: " << parent_population_Count << endl;
}
}
}
else if (generation_Type == "DEPRICIATION")
{
cout << "\nDepriciation phase\n";
if (parent_population_Count >= parents_Prev_generation[tissue])
{
new_Parent_Count = functions.beta_Distribution(variable_1, variable_2, gen) * parents_Prev_generation[tissue];
new_Parent_Count = parents_Prev_generation[tissue] - new_Parent_Count;
parent_population_Count = new_Parent_Count;
cout << "Parent population reduced to: " << parent_population_Count << endl;
}
}
}
if (profile_tissue_Limits[tissue] != -1 && parent_population_Count > profile_tissue_Limits[tissue])
{
cout << "\nMax cell count in tissue reached\n";
parent_population_Count = profile_tissue_Limits[tissue];
}
cout << "\nFilling parent set\n";
set<int> parents_to_get;
uniform_int_distribution<> distr(0, last_Progeny_written_this_Gen - 1);
while (parents_to_get.size() < parent_population_Count)
{
int random_parent = distr(gen);
if (check_to_Remove.find(random_parent) == check_to_Remove.end())
{
parents_to_get.insert(random_parent);
}
}
cout << "\nConverting parent vetor\n";
vector<int> parents_in_Tissue(parents_to_get.begin(), parents_to_get.end());
parents_to_get.clear();
check_to_Remove.clear();
removed_by_Transfer_Indexes[tissue].clear();
dead_Particle_count[tissue] = 0;
current_cell_load_per_Tissue[tissue] = 0;
parents_Prev_generation[tissue] = parent_population_Count;
int last_index_Seq_Written = 0;
// ! check if the new generation already exists and if so update;
string intermediary_Tissue_folder = source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations + 1);
string dead_List = intermediary_Tissue_folder + "/dead_List.txt";
// fstream this_Gen_progeny_parents;
// functions.create_File(source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations) + "/" + to_string(last_Progeny_written_this_Gen) + "_rapid_Progeny.nfasta");
if (filesystem::exists(intermediary_Tissue_folder) && filesystem::is_directory(intermediary_Tissue_folder))
{
cout << "Next generation already present\n";
vector<pair<int, int>> indexed_tissue_Folder = functions.index_sequence_Folder(intermediary_Tissue_folder);
if (indexed_tissue_Folder.size() > 0)
{
last_index_Seq_Written = indexed_tissue_Folder[indexed_tissue_Folder.size() - 1].second + 1;
current_cell_load_per_Tissue[tissue] = last_index_Seq_Written;
fstream dead_File;
dead_File.open(dead_List, ios::in);
if (dead_File.is_open())
{
string line;
int index = 0;
while (getline(dead_File, line))
{
// check_to_Remove.insert(stoi(line));
index++;
}
dead_Particle_count[tissue] = index;
dead_File.close();
}
else
{
cout << "ERROR: UNABLE TO OPEN DEAD LIST FILE: " << dead_List << endl;
exit(-1);
}
}
}
else
{
functions.config_Folder(intermediary_Tissue_folder, to_string(overall_Generations + 1) + " generation Tissue " + tissue_Names[tissue] + " sequences");
functions.create_File(dead_List);
}
vector<pair<int, int>> cells_Rounds_start_stop = get_Rounds(parent_population_Count, max_Cells_at_a_time);
multiset<pair<float, int>> migration_cell_List;
for (int cell_Round = 0; cell_Round < cells_Rounds_start_stop.size(); cell_Round++)
{
int num_of_Cells = cells_Rounds_start_stop[cell_Round].second - cells_Rounds_start_stop[cell_Round].first;
cout << "\nProcessing round " << cell_Round + 1 << " of " << cells_Rounds_start_stop.size() << ": " << num_of_Cells << " cell(s)" << endl;
simulate_cell_Round(functions, multi_Read, num_Cuda_devices, CUDA_device_IDs,
num_of_Cells, cells_Rounds_start_stop[cell_Round].first, cells_Rounds_start_stop[cell_Round].second,
parents_in_Tissue, tissue, tissue_Names[tissue],
indexed_Source_Folders[tissue],
source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations),
overall_Generations,
last_index_Seq_Written,
gen,
Reference_fitness_survivability_proof_reading, Reference_cancer_parameters,
A_0_mutation,
T_1_mutation,
G_2_mutation,
C_3_mutation,
mutation_Hotspots,
mutation_hotspot_parameters,
num_effect_Segregating_sites,
sequence_Survivability_changes,
sequence_Proof_reading_changes,
num_effect_Segregating_sites_Cancer,
sequence_replication_factor_changes,
sequence_mutation_rate_changes,
sequence_generation_death_changes,
sequence_replication_prob_changes,
sequence_metastatic_prob_changes,
max_sequences_per_File, intermediary_Tissue_folder, source_sequence_Data_folder,
last_Progeny_written_this_Gen, rapid_Progeny_Location,
tissue_Migration_Totals[tissue], migration_cell_List,
tissue_selection_Position_Count,
Survivability_Positions,
Proof_Positions,
Replication_factor_Positions,
Mutation_rate_factor_Positions,
Generation_death_Positions,
Replication_prob_Positions,
Metastatic_Positions,
tissues_ATGC_positions_Survivability,
tissues_ATGC_positions_Proof,
tissues_ATGC_positions_Replication_factor,
tissues_ATGC_positions_Mutation_rate_factor,
tissues_ATGC_positions_Generation_death,
tissues_ATGC_positions_Replication_prob,
tissues_ATGC_positions_Metastatic,
viral_Migration);
}
parents_in_Tissue.clear();
// last_Progeny_written_this_Gen = indexed_Source_Folders[tissue][indexed_Source_Folders[tissue].size() - 1].second + 1;
fstream gen_Summary;
gen_Summary.open(generational_Summary, ios::app);
if (gen_Summary.is_open())
{
//"Generation\tTissue\tPhase\tnum_Parents\tnum_Progeny\tdead_Progeny\trapid_Progeny"
gen_Summary << overall_Generations << "\t" << tissue_Names[tissue] << "\t" << generation_Type << "\t" << to_string(parent_population_Count)
<< "\t" << last_index_Seq_Written << "\t" << dead_Particle_count[tissue] << "\t";
}
else
{
cout << "ERROR: UNABLE TO OPEN GENERATIONAL SUMMARY FILE: " << generational_Summary << endl;
exit(-1);
}
string rename_rapid_Progeny = source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations) +
"/" + to_string(indexed_Source_Folders[tissue][indexed_Source_Folders[tissue].size() - 1].second + 1) + "_rapid_Progeny.nfasta";
if (filesystem::exists(rename_rapid_Progeny))
{
cout << "Renaming rapid progeny file: " << rename_rapid_Progeny << endl;
try
{
filesystem::rename(rename_rapid_Progeny, source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations) +
"/" + to_string(indexed_Source_Folders[tissue][indexed_Source_Folders[tissue].size() - 1].second + 1) + "_" + to_string(last_Progeny_written_this_Gen - 1) + ".nfasta");
gen_Summary << to_string(last_Progeny_written_this_Gen - (indexed_Source_Folders[tissue][indexed_Source_Folders[tissue].size() - 1].second + 1));
}
catch (const filesystem::filesystem_error &e)
{
// Handle any errors that occur during renaming
std::cerr << "Error renaming file: " << e.what() << '\n';
}
}
else
{
gen_Summary << "0";
}
gen_Summary << endl;
gen_Summary.close();
remainder_Write_Sequences_NEXT_Generation(intermediary_Tissue_folder, functions, to_write_Sequence_Store_NEXT_Gen);
// remainder_Write_Sequences_NEXT_Generation(source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations), functions, to_write_Sequence_Store_THIS_Gen);
for (int forward = 0; forward < to_write_Sequence_Store_OTHER_Gens[tissue].size(); forward++)
{
remainder_Write_Sequences_NEXT_Generation(source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(forward), functions, to_write_Sequence_Store_OTHER_Gens[tissue][forward]);
}
// free(parents_in_Tissue);
// See which progeny qualify to migrate
cout << "\nBottleneck size for metastatsis: " << tissue_Migration_Totals[tissue] << endl;
cout << "Metastatic cells avaiable: " << migration_cell_List.size() << endl;
if (viral_Migration == "YES")
{
migration_of_Cells(source_sequence_Data_folder, tissue_Names,
tissue, tissue_migration_Targets_amount[tissue], migration_cell_List,
overall_Generations, functions);
}
// // ! Calculate Tajima's. Define parameters with gene regions for Tajima's
if (count_tajima_Regions > 0)
{
calculate_Tajima(functions,
count_tajima_Regions, tajima_regions_Start_Stop,
overall_Generations, tissue_Names[tissue], tissue,
CUDA_device_IDs,
source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(overall_Generations), max_Cells_at_a_time,
output_Tajima_File,
reference_Genome_location);
}
// exit(-1);
cout << "\nCompleted tissue: " << tissue_Names[tissue] << endl;
}
// overall_Generations++;
}
}
else
{
stop_Type = 5;
}
}
else
{
stop_Type = 4;
}
decimal_Date = decimal_Date + date_Increment;
overall_Generations++;
free(real_Particle_count_per_Tissue);
cout << "\nCompleted generation " << overall_Generations << " of time: " << decimal_Date << endl;
if (stop_gen_Mode == 0)
{
if (overall_Generations >= stop_generations_Count)
{
stop_Type = 2;
}
}
else
{
if (decimal_Date >= stop_Date)
{
stop_Type = 3;
}
}
// ! STOP after testing
// stop_Type = 1;
} while (stop_Type == 0);
cout << "\nSimulation has concluded: ";
}
__global__ void cuda_convert_Sequence(int genome_Length, char *sequence)
{
int tid = threadIdx.x + blockIdx.x * blockDim.x;
while (tid < genome_Length)
{
if (sequence[tid] == 'A' || sequence[tid] == 'a' || sequence[tid] == '0')
{
sequence[tid] = '0';
}
else if (sequence[tid] == 'T' || sequence[tid] == 't' || sequence[tid] == '1')
{
sequence[tid] = '1';
}
else if (sequence[tid] == 'G' || sequence[tid] == 'g' || sequence[tid] == '2')
{
sequence[tid] = '2';
}
else if (sequence[tid] == 'C' || sequence[tid] == 'c' || sequence[tid] == '3')
{
sequence[tid] = '3';
}
tid += blockDim.x * gridDim.x;
}
}
__global__ void addToVariable(float *cuda_a_1, int N)
{
int tid = threadIdx.x + blockIdx.x * blockDim.x;
while (tid < (N + 1))
{
if (tid > 0)
{
atomicAdd(cuda_a_1, (float)1.0 / (float)(tid));
}
tid += blockDim.x * gridDim.x;
}
}
__global__ void squared_addToVariable(float *cuda_a_2, int N)
{
int tid = threadIdx.x + blockIdx.x * blockDim.x;
while (tid < (N + 1))
{
if (tid > 0)
{
atomicAdd(cuda_a_2, (float)1.0 / (float)(tid * tid));
}
tid += blockDim.x * gridDim.x;
}
}
void cancer_Host::calculate_Tajima(functions_library &functions,
int &num_Regions, int **tajima_regions_Start_Stop,
int &overall_Generations, string &tissue_Name, int &tissue_Index,
int *CUDA_device_IDs,
string sequence_Tissue_Folder, int &max_Cells_at_a_time,
string output_Tajima_File,
string reference_Genome_location)
{
cout << "\nCalculating Tajima's for generation " << overall_Generations << " for tissue " << tissue_Name << endl;
cout << "Reading reference genome:\n";
fstream reference_Genome_file;
reference_Genome_file.open(reference_Genome_location, ios::in);
string reference_Genome = "";
if (reference_Genome_file.is_open())
{
string line;
while (getline(reference_Genome_file, line))
{
if (line.at(0) != '>')
{
reference_Genome.append(line);
}
}
}
else
{
cout << "ERROR: UNABLE TO OPEN REFERENCE GENOME FILE: " << reference_Genome_location << endl;
exit(-1);
}
if (reference_Genome.size() == genome_Length)
{
cout << "Reference genome valid\n";
}
else
{
cout << "ERROR: Size of reference genome (" << reference_Genome.size() << ") is not equal to the genome length (" << genome_Length << ").";
exit(-1);
}
cout << "Converting Reference genome to INT\n";
// exit(-1);
cudaSetDevice(CUDA_device_IDs[0]);
char *full_Char;
full_Char = (char *)malloc((reference_Genome.size() + 1) * sizeof(char));
strcpy(full_Char, reference_Genome.c_str());
char *cuda_full_Char;
cudaMallocManaged(&cuda_full_Char, (reference_Genome.size() + 1) * sizeof(char));
cudaMemcpy(cuda_full_Char, full_Char, (reference_Genome.size() + 1) * sizeof(char), cudaMemcpyHostToDevice);
free(full_Char);
reference_Genome = "";
cuda_convert_Sequence<<<functions.tot_Blocks_array[0], functions.tot_ThreadsperBlock_array[0]>>>(genome_Length, cuda_full_Char);
cudaDeviceSynchronize();
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess)
{
fprintf(stderr, "ERROR: CUDA error after synchronizing stream on GPU %d: %s\n", 0, cudaGetErrorString(err));
exit(-1);
}
cout << "Configuring common GPU memory\n";
int **cuda_tajima_regions_Start_Stop;
cudaMallocManaged(&cuda_tajima_regions_Start_Stop, num_Regions * sizeof(int *));
for (int row = 0; row < num_Regions; row++)
{
cudaMalloc((void **)&(cuda_tajima_regions_Start_Stop[row]), 2 * sizeof(int));
cudaMemcpy(cuda_tajima_regions_Start_Stop[row], tajima_regions_Start_Stop[row], 2 * sizeof(int), cudaMemcpyHostToDevice);
}
cout << "Reading folder: " << sequence_Tissue_Folder << endl;
vector<pair<int, int>> indexed_Source_Folder = functions.index_sequence_Folder(sequence_Tissue_Folder);
// cout << "\nCalculating pre-requisites: \n";
int N_total = 0;
int N_alive = 0;
// exit(-1);
string sequence_String = "";
vector<string> line_Data;
string dead_or_Alive = "";
int count_Track = 0;
int *per_Region = (int *)malloc(num_Regions * sizeof(int));
int *per_Region_ALIVE = (int *)malloc(num_Regions * sizeof(int));
for (int region = 0; region < num_Regions; region++)
{
per_Region[region] = 0;
per_Region_ALIVE[region] = 0;
}
int *cuda_per_Region;
cudaMallocManaged(&cuda_per_Region, num_Regions * sizeof(int));
cudaMemcpy(cuda_per_Region, per_Region, num_Regions * sizeof(int), cudaMemcpyHostToDevice);
int *cuda_per_Region_ALIVE;
cudaMallocManaged(&cuda_per_Region_ALIVE, num_Regions * sizeof(int));
cudaMemcpy(cuda_per_Region_ALIVE, per_Region_ALIVE, num_Regions * sizeof(int), cudaMemcpyHostToDevice);
cout << "\nReading files\n";
for (int file = 0; file < indexed_Source_Folder.size(); file++)
{
fstream nfasta;
nfasta.open(sequence_Tissue_Folder + "/" + to_string(indexed_Source_Folder[file].first) + "_" + to_string(indexed_Source_Folder[file].second) + ".nfasta");
if (nfasta.is_open())
{
string line;
while (getline(nfasta, line))
{
if (line.at(0) != '>')
{
sequence_String.append(line);
count_Track++;
if (count_Track == max_Cells_at_a_time)
{
process_Tajima_String(sequence_String, count_Track, num_Regions, cuda_tajima_regions_Start_Stop,
cuda_full_Char, cuda_per_Region, functions, dead_or_Alive,
cuda_per_Region_ALIVE);
}
}
else
{
functions.split(line_Data, line, '_');
dead_or_Alive.append(line_Data[1]);
if (line_Data[1] == "A")
{
N_alive++;
}
N_total++;
}
}
nfasta.close();
}
else
{
cout << "ERROR: UNABLE TO OPEN SEQUENCE: "
<< sequence_Tissue_Folder + "/" << indexed_Source_Folder[file].first + "_" << indexed_Source_Folder[file].second << ".nfasta\n";
exit(-1);
}
}
if (count_Track > 0)
{
process_Tajima_String(sequence_String, count_Track, num_Regions, cuda_tajima_regions_Start_Stop,
cuda_full_Char, cuda_per_Region, functions, dead_or_Alive, cuda_per_Region_ALIVE);
}
cout << "Completed reading files\n";
cout << "Total number of cells (N): " << N_total << endl;
cudaMemcpy(per_Region, cuda_per_Region, num_Regions * sizeof(int), cudaMemcpyDeviceToHost);
cudaFree(cuda_per_Region);
cudaMemcpy(per_Region_ALIVE, cuda_per_Region_ALIVE, num_Regions * sizeof(int), cudaMemcpyDeviceToHost);
cudaFree(cuda_per_Region_ALIVE);
double b1;
double b2;
double c1;
double c2;
double e1;
double e2;
double out_a_1;
double N_float = N_total;
calc_pre_Requistes(b1,
b2,
c1,
c2,
e1,
e2,
out_a_1, N_total, functions);
write_Tajima("ALL", per_Region, output_Tajima_File, overall_Generations, tissue_Name,
num_Regions, N_total, N_float, out_a_1, e1, e2);
if (N_total != N_alive)
{
cout << "Processing ALIVE only\n";
double N_float = N_alive;
calc_pre_Requistes(b1,
b2,
c1,
c2,
e1,
e2,
out_a_1, N_alive, functions);
write_Tajima("ALIVE", per_Region_ALIVE, output_Tajima_File, overall_Generations, tissue_Name,
num_Regions, N_alive, N_float, out_a_1, e1, e2);
}
// cout << "DONE\n";
for (int row = 0; row < num_Regions; row++)
{
cudaFree(cuda_tajima_regions_Start_Stop[row]);
}
// cout << "DONE\n";
cudaFree(cuda_tajima_regions_Start_Stop);
cudaFree(cuda_full_Char);
// cout << "DONE\n";
free(per_Region);
free(per_Region_ALIVE);
// cout << "DONE\n";
cout << "Completed Tajima calculation for generation: " << overall_Generations << endl;
// exit(-1);
}
void cancer_Host::calc_pre_Requistes(double &b1,
double &b2,
double &c1,
double &c2,
double &e1,
double &e2,
double &out_a_1, int &N,
functions_library &functions)
{
cout << "\nCalculating pre-requistes\n";
double N_float = N;
if (N < 50000)
{
float a_1 = 0;
float *cuda_a_1;
// Allocate memory on the device
cudaMalloc(&cuda_a_1, sizeof(float));
// Copy the initial value from host to device
cudaMemcpy(cuda_a_1, &a_1, sizeof(float), cudaMemcpyHostToDevice);
addToVariable<<<functions.tot_Blocks_array[0], functions.tot_ThreadsperBlock_array[0]>>>(cuda_a_1, (N - 1));
cudaDeviceSynchronize();
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess)
{
fprintf(stderr, "ERROR: CUDA error after synchronizing stream on GPU %d: %s\n", 0, cudaGetErrorString(err));
exit(-1);
}
// Copy the result back to the host
cudaMemcpy(&a_1, cuda_a_1, sizeof(float), cudaMemcpyDeviceToHost);
// Free device memory
cudaFree(cuda_a_1);
cout << "a1: " << a_1 << endl;
float a_2 = 0;
float *cuda_a_2;
cudaMalloc(&cuda_a_2, sizeof(float));
cudaMemcpy(cuda_a_2, &a_2, sizeof(float), cudaMemcpyHostToDevice);
squared_addToVariable<<<functions.tot_Blocks_array[0], functions.tot_ThreadsperBlock_array[0]>>>(cuda_a_2, (N - 1));
cudaDeviceSynchronize();
err = cudaGetLastError();
if (err != cudaSuccess)
{
fprintf(stderr, "ERROR: CUDA error after synchronizing stream on GPU %d: %s\n", 0, cudaGetErrorString(err));
exit(-1);
}