-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_within_host.cu
3591 lines (3034 loc) · 167 KB
/
node_within_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 "node_within_host.cuh"
node_within_host::node_within_host()
{
cout << "Intializing host: ";
}
void node_within_host::setHost(int host_Index, int cave_ID, int host_ID, int profile_ID, int num_Tissues)
{
this->host_Index = host_Index;
this->cave_ID = cave_ID;
this->host_ID = host_ID;
this->profile_ID = profile_ID;
this->num_Tissues = num_Tissues;
cout << this->cave_ID << "_" << host_ID << endl;
}
void node_within_host::setNum_Generation(int num_Generation)
{
this->num_Generation = num_Generation;
}
void node_within_host::setInfectious_Load(int infectious_Load)
{
this->infectious_Load = infectious_Load;
}
void node_within_host::setTerminal_Load(int terminal_Load)
{
this->terminal_Load = terminal_Load;
}
void node_within_host::setSampling_Effect(float sampling_Effect)
{
this->sampling_Effect = sampling_Effect;
}
void node_within_host::setCell_Limit(vector<int> cell_Limit_vec)
{
num_Tissues = cell_Limit_vec.size();
this->cell_Limit = (int *)malloc(sizeof(int) * num_Tissues);
for (size_t i = 0; i < num_Tissues; i++)
{
cell_Limit[i] = cell_Limit_vec[i];
}
}
void node_within_host::print_All()
{
cout << host_Index << "\t"
<< cave_ID << "_" << host_ID << "\t"
<< profile_ID << "\t"
<< num_Generation << "\t"
<< infectious_Load << "\t"
<< terminal_Load << "\t"
<< sampling_Effect;
for (size_t i = 0; i < num_Tissues; i++)
{
cout << "\t" << cell_Limit[i];
}
cout << endl;
}
void node_within_host::begin_Infection(functions_library &functions, string &intermediary_Sequence_location,
int entry_tissues, int *entry_array, int &max_sequences_per_File,
string &output_Node_location,
vector<string> &tissue_Names, string first_Infection)
{
// FIRST NODE OF INFECTION IN THE HOST
string host_Folder = intermediary_Sequence_location + "/" + to_string(host_Index);
functions.config_Folder(host_Folder, to_string(cave_ID) + "_" + to_string(host_ID));
vector<vector<string>> tissue_Sequences;
intialize_Tissues(host_Folder, tissue_Sequences, functions);
string reference_Sequences = intermediary_Sequence_location + "/reference_Sequences";
cout << "\nFirst infection mode: " << first_Infection << endl;
if (first_Infection == "RANDOM")
{
vector<string> files;
for (const auto &entry : filesystem::directory_iterator(reference_Sequences))
{
if (entry.is_regular_file() && entry.path().extension() == ".nfasta")
{
files.push_back(entry.path().string());
}
}
vector<string> Sequences;
vector<string> Sequence_IDs;
cout << endl;
vector<char> seq_Status;
for (int file = 0; file < files.size(); file++)
{
cout << "Reading file: " << files[file] << endl;
fstream nfasta;
nfasta.open(files[file], ios::in);
if (nfasta.is_open())
{
string line;
string sequence = "";
while (getline(nfasta, line))
{
if (line.at(0) != '>')
{
sequence.append(line);
}
else
{
Sequence_IDs.push_back(line);
if (sequence != "")
{
Sequences.push_back(sequence);
sequence = "";
}
}
}
if (sequence != "")
{
Sequences.push_back(sequence);
sequence = "";
}
nfasta.close();
}
else
{
cout << "ERROR UNABLE TO OPEN NFATSA FILE: " << files[file] << endl;
exit(-1);
}
}
random_device rd; // Will be used to obtain a seed for the random number engine
mt19937 gen(rd());
uniform_int_distribution<int> entry_Tissue_select(0, entry_tissues - 1);
cout << endl;
for (int sequence = 0; sequence < Sequences.size(); sequence++)
{
int tissue_Index = entry_array[entry_Tissue_select(gen)];
cout << "Sequence " << sequence + 1 << " infects tissue: " << tissue_Index << endl;
tissue_Sequences[tissue_Index].push_back(Sequences[sequence]);
}
for (int tissue = 0; tissue < entry_tissues; tissue++)
{
if (tissue_Sequences[entry_array[tissue]].size() > 0)
{
current_Viral_load_per_Tissue[entry_array[tissue]] = tissue_Sequences[entry_array[tissue]].size();
functions.config_Folder(host_Folder + "/" + to_string(entry_array[tissue]) + "/generation_" + to_string(current_Generation), to_string(cave_ID) + "_" + to_string(host_ID) + " Tissue " + to_string(entry_array[tissue]) + " Generation 0");
if (!filesystem::exists(output_Node_location + "/" + get_Name()))
{
functions.config_Folder(output_Node_location + "/" + get_Name(), get_Name() + " node");
functions.create_File(output_Node_location + "/" + get_Name() + "/sequence_Profiles.csv", "Sequence_ID\tHost\tTissue");
functions.create_File(output_Node_location + "/" + get_Name() + "/sequence_parent_Progeny_relationships.csv", "Source\tTarget\tType");
}
vector<string> sequence_Write_Store_All;
int last_seq_Num = 0;
functions.sequence_Write_Configurator(sequence_Write_Store_All, tissue_Sequences[entry_array[tissue]],
max_sequences_per_File, host_Folder + "/" + to_string(entry_array[tissue]) + "/generation_" + to_string(current_Generation), last_seq_Num, seq_Status,
output_Node_location + "/" + get_Name() + "/sequence_Profiles.csv", get_Name(), tissue_Names[entry_array[tissue]], current_Generation);
functions.partial_Write_Check(sequence_Write_Store_All,
host_Folder + "/" + to_string(entry_array[tissue]) + "/generation_" + to_string(current_Generation), last_seq_Num, seq_Status,
output_Node_location + "/" + get_Name() + "/sequence_Profiles.csv", get_Name(), tissue_Names[entry_array[tissue]], current_Generation);
}
}
}
else
{
cout << "\nInfecting tissues\n";
vector<string> line_Data;
//// Write to sequence profiles file
for (int tissue = 0; tissue < tissue_Names.size(); tissue++)
{
string reference_Sequences_tissue = reference_Sequences + "/" + tissue_Names[tissue];
int last_seq_Num = 0;
if (filesystem::exists(reference_Sequences_tissue) && filesystem::is_directory(reference_Sequences_tissue))
{
cout << "\nTissue: " << tissue_Names[tissue] << endl;
for (const auto &entry : filesystem::directory_iterator(reference_Sequences_tissue))
{
if (entry.is_regular_file() && entry.path().extension() == ".nfasta")
{
string file_Name = entry.path().stem();
functions.split(line_Data, file_Name, '_');
int num_Particles_Tissue = stoi(line_Data[1]) - stoi(line_Data[0]) + 1;
cout << "Sequences migrating: " << num_Particles_Tissue << endl;
current_Viral_load_per_Tissue[tissue] = current_Viral_load_per_Tissue[tissue] + num_Particles_Tissue;
functions.config_Folder(host_Folder + "/" + to_string(tissue) + "/generation_" + to_string(current_Generation), to_string(cave_ID) + "_" + to_string(host_ID) + " Tissue " + to_string(tissue) + " Generation 0");
if (!filesystem::exists(output_Node_location + "/" + get_Name()))
{
functions.config_Folder(output_Node_location + "/" + get_Name(), get_Name() + " node");
functions.create_File(output_Node_location + "/" + get_Name() + "/sequence_Profiles.csv", "Sequence_ID\tHost\tTissue");
functions.create_File(output_Node_location + "/" + get_Name() + "/sequence_parent_Progeny_relationships.csv", "Source\tTarget\tType");
}
filesystem::copy_file(entry.path().string(), host_Folder + "/" + to_string(tissue) + "/generation_" + to_string(current_Generation) + "/" + file_Name + ".nfasta");
fstream sequence_Profile;
sequence_Profile.open(output_Node_location + "/" + get_Name() + "/sequence_Profiles.csv", ios::app);
for (int sequence_Num = 0; sequence_Num < num_Particles_Tissue; sequence_Num++)
{
sequence_Profile << get_Name() << "_" << tissue_Names[tissue] << "_" << current_Generation << "_" << last_seq_Num << "\t" << get_Name() << "\t" << tissue_Names[tissue] << endl;
last_seq_Num++;
}
sequence_Profile.close();
}
}
}
}
}
set_Infected();
// exit(-1);
// for (int tissue = 0; tissue < num_Tissues; tissue++)
// {
// cout << current_Viral_load_per_Tissue[tissue] << endl;
// }
// exit(-1);
}
string node_within_host::transfer_Infection(functions_library &functions, string &intermediary_Sequence_location, string &source_Target_file_Location,
int &source_Index, int &source_Generation, string &source_Name, int *source_current_Viral_load_per_Tissue,
int num_viruses_to_transfer,
int &entry_tissues, int *entry_array, int exit_Load, int &exit_tissues, int *exit_array,
vector<set<int>> &source_removed_by_Transfer_Indexes,
int &max_sequences_per_File,
vector<vector<pair<int, int>>> &indexed_Source_Folders,
float &decimal_Date,
string &Host_source_target_network_location,
string &output_Node_location,
vector<string> &tissue_Names,
mt19937 &gen)
{
/**
* First we determine of the exit tissues have a viral population to be transmitted
**/
if (exit_Load > 0)
{
cout << "\nNode " << this->cave_ID << "_" << this->host_ID << " is being infected by " << source_Name << endl;
if (num_viruses_to_transfer > exit_Load)
{
num_viruses_to_transfer = exit_Load;
}
if (num_viruses_to_transfer > 0)
{
string host_Folder = intermediary_Sequence_location + "/" + to_string(host_Index);
int year, month, day;
functions.decimal_to_Date(decimal_Date, year, month, day);
string date_String = to_string(year) + "-" + to_string(month) + "-" + to_string(day);
if (current_Generation == -1)
{
functions.config_Folder(host_Folder, to_string(cave_ID) + "_" + to_string(host_ID));
vector<vector<string>> tissue_Sequences;
intialize_Tissues(host_Folder, tissue_Sequences, functions);
}
cout << "Attempting to transfer " << num_viruses_to_transfer << " viral particle(s)\n";
uniform_int_distribution<> distribution_exit_Tissue(0, exit_tissues - 1);
vector<set<int>> unique_indexes_to_Remove_Tissues;
for (int tissue = 0; tissue < exit_tissues; tissue++)
{
set<int> init_Set;
unique_indexes_to_Remove_Tissues.push_back(init_Set);
}
// cout << "Attempting to transfer " << num_viruses_to_transfer << " viral particle(s)\n";
for (int particle = 0; particle < num_viruses_to_transfer; particle++)
{
int exit_tissue_Index = distribution_exit_Tissue(gen);
if (source_current_Viral_load_per_Tissue[exit_array[exit_tissue_Index]] > 0)
{
uniform_int_distribution<> distribution_particle(0, source_current_Viral_load_per_Tissue[exit_array[exit_tissue_Index]] - 1);
unique_indexes_to_Remove_Tissues[exit_tissue_Index].insert(distribution_particle(gen));
}
}
cout << "Viral particle(s) and their exit tissue(s) have been indentifed\n";
// vector<vector<int>> indexes_to_Remove;
vector<vector<string>> seq_to_Write;
vector<vector<string>> source_Seq_Data;
for (int init = 0; init < entry_tissues; init++)
{
vector<string> initialize;
seq_to_Write.push_back(initialize);
source_Seq_Data.push_back(initialize);
}
uniform_int_distribution<> entry_Select(0, entry_tissues - 1);
for (int tissue = 0; tissue < exit_tissues; tissue++)
{
vector<int> init_Tissue(unique_indexes_to_Remove_Tissues[tissue].begin(), unique_indexes_to_Remove_Tissues[tissue].end());
if (init_Tissue.size() > 0)
{
cout << "Exit tissue: " << exit_array[tissue] + 1 << endl;
vector<int> indexes_of_Seq_write;
for (int transfer_Cell = 0; transfer_Cell < init_Tissue.size(); transfer_Cell++)
{
auto it = source_removed_by_Transfer_Indexes[exit_array[tissue]].find(init_Tissue[transfer_Cell]);
if (it == source_removed_by_Transfer_Indexes[exit_array[tissue]].end())
{
// not present
indexes_of_Seq_write.push_back(init_Tissue[transfer_Cell]);
source_removed_by_Transfer_Indexes[exit_array[tissue]].insert(init_Tissue[transfer_Cell]);
}
}
if (indexes_of_Seq_write.size() > 0)
{
int valid_Sequences = 0;
// cout << "Collecting " << indexes_of_Seq_write.size() << " sequence(s)\n";
vector<string> collected_Sequences = functions.find_Sequences_Master(source_Target_file_Location, indexes_of_Seq_write, exit_array[tissue], indexed_Source_Folders[exit_array[tissue]], source_Generation, valid_Sequences);
cout << "Assinging sequence(s) to entry tissue(s)\n";
if (valid_Sequences != 0)
{
if (!filesystem::exists(output_Node_location + "/" + get_Name()))
{
functions.config_Folder(output_Node_location + "/" + get_Name(), get_Name() + " node");
functions.create_File(output_Node_location + "/" + get_Name() + "/sequence_Profiles.csv", "Sequence_ID\tHost\tTissue");
functions.create_File(output_Node_location + "/" + get_Name() + "/sequence_parent_Progeny_relationships.csv", "Source\tTarget\tType");
}
}
for (int check_Seq = 0; check_Seq < collected_Sequences.size(); check_Seq++)
{
if (collected_Sequences[check_Seq] != "")
{
int entry_Tissue_index = entry_Select(gen);
seq_to_Write[entry_Tissue_index].push_back(collected_Sequences[check_Seq]);
// sequence_Profile << host << "_" << tissue << "_" << last_seq_Num << "\t" << host << tissue<<endl;
source_Seq_Data[entry_Tissue_index].push_back(source_Name + "_" + tissue_Names[exit_array[tissue]] + "_" + to_string(source_Generation) + "_" + to_string(indexes_of_Seq_write[check_Seq]));
}
}
}
}
}
vector<char> seq_Status;
cout << "Writing sequence(s) to entry tissue(s)\n";
int infected_Check = 0;
for (int tissue = 0; tissue < entry_tissues; tissue++)
{
if (seq_to_Write[tissue].size() > 0)
{
if (!filesystem::exists(host_Folder + "/" + to_string(entry_array[tissue]) + "/generation_" + to_string(current_Generation)))
{
functions.config_Folder(host_Folder + "/" + to_string(entry_array[tissue]) + "/generation_" + to_string(current_Generation), to_string(cave_ID) + "_" + to_string(host_ID) + " Tissue " + to_string(entry_array[tissue]) + " Generation 0");
}
vector<string> sequence_Write_Store_All;
vector<int> indexes_Written;
functions.sequence_Write_Configurator_transfer(sequence_Write_Store_All, seq_to_Write[tissue],
max_sequences_per_File, host_Folder + "/" + to_string(entry_array[tissue]) + "/generation_" + to_string(current_Generation), current_Viral_load_per_Tissue[entry_array[tissue]], seq_Status,
output_Node_location + "/" + get_Name() + "/sequence_Profiles.csv", get_Name(), tissue_Names[entry_array[tissue]], current_Generation,
indexes_Written);
functions.partial_Write_Check_transfer(sequence_Write_Store_All,
host_Folder + "/" + to_string(entry_array[tissue]) + "/generation_" + to_string(current_Generation), current_Viral_load_per_Tissue[entry_array[tissue]], seq_Status,
output_Node_location + "/" + get_Name() + "/sequence_Profiles.csv", get_Name(), tissue_Names[entry_array[tissue]], current_Generation, indexes_Written);
infected_Check = 1;
//(output_Node_location + "/" + get_Name() + "/sequence_parent_Progeny_relationships.csv", "Source\tTarget\tType");
fstream source;
fstream target;
source.open(output_Node_location + "/" + source_Name + "/sequence_parent_Progeny_relationships.csv", ios::app);
target.open(output_Node_location + "/" + get_Name() + "/sequence_parent_Progeny_relationships.csv", ios::app);
// functions.create_File(output_Node_location + "/" + get_Name() + "/sequence_Profiles.csv", "Sequence_ID\tHost\tTissue");
fstream target_Profiles;
target_Profiles.open(output_Node_location + "/" + get_Name() + "/sequence_Profiles.csv", ios::app);
fstream source_Profiles;
source_Profiles.open(output_Node_location + "/" + source_Name + "/sequence_Profiles.csv", ios::app);
for (int transfers = 0; transfers < indexes_Written.size(); transfers++)
{
source << source_Seq_Data[tissue][transfers] << "\t" << get_Name() << "_" << tissue_Names[entry_array[tissue]] << "_" << current_Generation << "_" << to_string(indexes_Written[transfers]) << "\tTransmission" << endl;
target << source_Seq_Data[tissue][transfers] << "\t" << get_Name() << "_" << tissue_Names[entry_array[tissue]] << "_" << current_Generation << "_" << to_string(indexes_Written[transfers]) << "\tTransmission" << endl;
target_Profiles << source_Seq_Data[tissue][transfers] << "\t" << source_Name << "\t" << tissue_Names[entry_array[tissue]] << endl;
source_Profiles << get_Name() << "_" << tissue_Names[entry_array[tissue]] << "_" << current_Generation << "_" << to_string(indexes_Written[transfers]) << "\t" << get_Name() << "\t" << tissue_Names[entry_array[tissue]] << endl;
}
source.close();
target.close();
target_Profiles.close();
}
}
if (infected_Check == 1)
{
if (status == "Susceptible")
{
set_Infected();
}
fstream write_source_Target;
write_source_Target.open(Host_source_target_network_location, ios::app);
if (write_source_Target.is_open())
{
cout << "Writing host's source target relationship\n";
write_source_Target << source_Name << "\t" << this->get_Name() << "\t" << to_string(decimal_Date) << "\t" << date_String << endl;
write_source_Target.close();
}
else
{
cout << "ERROR: UNABLE TO OPEN SOURCE TARGET FILE: " << Host_source_target_network_location << "\n";
exit(-1);
}
}
}
}
else
{
cout << source_Name << " has no viral particles in the exit tissues\n";
}
return status;
}
int node_within_host::get_generation_Phase(int generation, int *num_replication_phases, float **tissue_replication_data, int *tissue_param_profile_Stride, int &tissue,
float &variable_1, float &variable_2)
{
/**
* *Extract the generation of the current tissue phase to determine the parent viral population.
**/
cout << "Getting generation phase\n";
int gen_Phase = -1;
int num_Phases = num_replication_phases[(profile_ID * num_Tissues) + tissue];
int num_phases_per_tissue = 0;
int tissue_Check = 0;
for (int param_Index = tissue_param_profile_Stride[profile_ID]; param_Index < tissue_param_profile_Stride[profile_ID + 1]; param_Index++)
{
if (tissue_Check == tissue)
{
float time_Check = 0;
float current_Generation_Ratio = (float)generation / (float)num_Generation;
for (int phases = param_Index; phases < (param_Index + num_Phases); phases++)
{
time_Check = time_Check + tissue_replication_data[phases][0];
if (current_Generation_Ratio < time_Check)
{
variable_1 = tissue_replication_data[phases][2];
variable_2 = tissue_replication_data[phases][3];
gen_Phase = tissue_replication_data[phases][1];
return gen_Phase;
// break;
}
}
// break;
}
num_phases_per_tissue++;
if (num_phases_per_tissue == num_replication_phases[(profile_ID * num_Tissues) + tissue_Check])
{
num_phases_per_tissue = 0;
tissue_Check++;
}
}
return gen_Phase;
}
void node_within_host::run_Generation(functions_library &functions, string &multi_Read, int &max_Cells_at_a_time, int &gpu_Limit, int *CUDA_device_IDs, int &num_Cuda_devices, int &genome_Length,
int &CPU_cores, int &max_sequences_per_File,
string source_sequence_Data_folder, string &output_Node_location,
vector<string> &tissue_Names,
int *num_replication_phases, float **tissue_replication_data, int *tissue_param_profile_Stride,
int terminal_tissues, int *terminal_array,
int **cell_Distribution_Type, vector<pair<float, float>> &viral_distribution_per_Tissue_param,
float *Reference_fitness_survivability_proof_reading,
int *mutation_recombination_proof_Reading_availability,
int *num_effect_Segregating_sites,
float **sequence_Fitness_changes,
float **sequence_Survivability_changes,
float **sequence_Proof_reading_changes,
int &mutation_Hotspots,
float **mutation_hotspot_parameters,
float **A_0_mutation,
float **T_1_mutation,
float **G_2_mutation,
float **C_3_mutation,
int &recombination_Hotspots,
float **recombination_hotspot_parameters,
int *tot_prob_selectivity,
int *recombination_prob_Stride,
int *recombination_select_Stride,
float **recombination_Prob_matrix,
float **recombination_Select_matrix,
float *progeny_distribution_parameters_Array,
string &viral_Migration,
float **viral_Migration_Values,
int *migration_start_Generation,
int &overall_Generations,
string &infected_to_Recovered,
string enable_Folder_management,
string enable_Compression,
mt19937 &gen)
{
/**
* ! Main function handling the processing of the within host viral infection dynamics.
**/
cout << "\nSimulating generation " << current_Generation << " of " << num_Generation << " for " << get_Name() << endl
<< endl;
if (current_Generation < num_Generation)
{
/**
* First we determine if there is a within host viral population in the host and the counts present within each tissue.
**/
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;
for (int tissue = 0; tissue < num_Tissues; tissue++)
{
real_Particle_count_per_Tissue[tissue] = current_Viral_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];
}
if (sum_Check > 0)
{
if (terminal_status(terminal_tissues, terminal_array, source_sequence_Data_folder, enable_Folder_management, enable_Compression) != 1)
{
cout << "\nInitiating simulation\n";
// cout << profile_ID << endl;
vector<vector<pair<int, int>>> indexed_Source_Folders = functions.index_sequence_Folders(source_sequence_Data_folder, num_Tissues, current_Generation, multi_Read);
string sequence_Profiles = output_Node_location + "/" + get_Name() + "/sequence_Profiles.csv";
string sequence_parent_Progeny_relationships = output_Node_location + "/" + get_Name() + "/sequence_parent_Progeny_relationships.csv";
string generational_Summary = output_Node_location + "/" + get_Name() + "/node_generational_Summary.csv";
if (!filesystem::exists(output_Node_location + "/" + get_Name()))
{
functions.config_Folder(output_Node_location + "/" + get_Name(), get_Name() + " node");
}
if (!filesystem::exists(sequence_Profiles))
{
functions.create_File(sequence_Profiles, "Sequence_ID\tHost\tTissue");
}
if (!filesystem::exists(sequence_parent_Progeny_relationships))
{
functions.create_File(sequence_parent_Progeny_relationships, "Source\tTarget\tType");
}
if (!filesystem::exists(generational_Summary))
{
functions.create_File(generational_Summary, "overall_Generation\tnode_Generation\tTissue\tnum_Parents\tnum_Progeny\tdead_Progeny");
}
for (int tissue = 0; tissue < num_Tissues; tissue++)
{
if (real_Particle_count_per_Tissue[tissue] > 0)
{
// real_Particle_count_per_Tissue[tissue] = 123;
cout << "\nSimulating " << real_Particle_count_per_Tissue[tissue] << " particle(s) for " << tissue_Names[tissue] << " tissue\n"
<< endl;
// cout << profile_ID << endl;
// for (int generation = current_Generation; generation < num_Generation; generation++)
// {
// float variable_1, variable_2;
// int gen_Phase = get_generation_Phase(generation, num_replication_phases, tissue_replication_data, tissue_param_profile_Stride, tissue,
// variable_1, variable_2);
// cout << "Gen phase " << generation << ": " << gen_Phase << endl;
// cout << variable_1 << "\t" << variable_2 << endl;
// }
// exit(-1);
cout << "Identifying indexes to remove\n";
set<int> check_to_Remove;
//// Account for dead file
if (dead_Particle_count[tissue] > 0)
{
cout << "\nIdentifying dead viral index(es)\n";
// indexes_of_Dead = (int *)malloc(sizeof(int) * dead_Particle_count[tissue]);
fstream dead_File;
dead_File.open(source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(current_Generation) + "/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_" << current_Generation << "/dead_List.txt" << endl;
exit(-1);
}
}
if (removed_by_Transfer_Indexes[tissue].size() > 0)
{
cout << "Identifying transferred viral index(es)\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);
}
}
//// clear 2d
// int *parents_in_Tissue = (int *)malloc(sizeof(int) * real_Particle_count_per_Tissue[tissue]);
//= functions.create_INT_2D_arrays(2, real_Particle_count_per_Tissue[tissue]);
/**
* @param parents_in_Tissue : 2D array storing the parent viral ID and its cell
* ROW 0: PARTICLE ID
* ROW 1: CELL ID of the cell the viral particle occupies.
**/
int **parents_in_Tissue;
parents_in_Tissue = (int **)malloc(2 * sizeof(int *));
for (int row = 0; row < 2; row++)
{
parents_in_Tissue[row] = (int *)malloc(real_Particle_count_per_Tissue[tissue] * sizeof(int));
}
// test
// check_to_Remove.insert(0);
// check_to_Remove.insert(1);
// check_to_Remove.insert(5);
// check_to_Remove.insert(99);
// cout << profile_ID << endl;
// for (int generation = current_Generation; generation < num_Generation; generation++)
//{
/**
* Extract the generation of the current tissue phase to determine the parent viral population.
**/
float variable_1, variable_2;
int gen_Phase = get_generation_Phase(current_Generation, num_replication_phases, tissue_replication_data, tissue_param_profile_Stride, tissue,
variable_1, variable_2);
// cout << "Gen phase " << generation << ": " << gen_Phase << endl;
// cout << variable_1 << "\t" << variable_2 << endl;
// cout << "Gen phase " << generation << ": " << gen_Phase << endl;
// cout << variable_1 << "\t" << variable_2 << endl;
/**
* Viral particles infect the cells.
**/
vector<int> start_Stop_cells = assign_Cells(functions, parents_in_Tissue, real_Particle_count_per_Tissue[tissue], tissue,
cell_Distribution_Type[profile_ID][tissue], viral_distribution_per_Tissue_param[tissue].first, viral_distribution_per_Tissue_param[tissue].second,
check_to_Remove,
gen_Phase, variable_1, variable_2,
gen);
check_to_Remove.clear();
removed_by_Transfer_Indexes[tissue].clear();
dead_Particle_count[tissue] = 0;
current_Viral_load_per_Tissue[tissue] = 0;
cout << "Total number of cell(s) infected: " << start_Stop_cells.size() - 1 << endl;
// if (start_Stop_cells.size() - 1 > 0)
// {
// for (int i = 0; i < start_Stop_cells.size() - 1; i++)
// {
// // cout << start_Stop_cells[i] << " : \t" << start_Stop_cells[i + 1] << endl;
// for (int particle = start_Stop_cells[i]; particle < start_Stop_cells[i + 1]; particle++)
// {
// // cout << parents_in_Tissue[0][particle] << " :\t" << parents_in_Tissue[1][particle] << endl;
// cout << parents_in_Tissue[1][particle] << "_" << parents_in_Tissue[0][particle] << ", ";
// }
// cout << endl;
// }
// }
// }
// exit(-1);
// vector<int> start_Stop_cells;
if ((start_Stop_cells.size() - 1) > 0)
{
// cout << "check\n";
// for (int i = 0; i < start_Stop_cells.size(); i++)
// {
// cout << start_Stop_cells[i] << endl;
// }
// for (int i = 0; i < start_Stop_cells.size() - 1; i++)
// {
// // cout << start_Stop_cells[i] << " : \t" << start_Stop_cells[i + 1] << endl;
// for (int particle = start_Stop_cells[i]; particle < start_Stop_cells[i + 1]; particle++)
// {
// // cout << parents_in_Tissue[0][particle] << " :\t" << parents_in_Tissue[1][particle] << endl;
// cout << parents_in_Tissue[1][particle] << "_" << parents_in_Tissue[0][particle] << ", ";
// }
// cout << endl;
// }
// exit(-1);
cout << "\nOrganising cell processing schedule\n"
<< endl;
vector<pair<int, int>> cells_Rounds_start_stop;
int full_Rounds = (start_Stop_cells.size() - 1) / max_Cells_at_a_time;
int partial_Rounds = (start_Stop_cells.size() - 1) % max_Cells_at_a_time;
for (int full = 0; full < full_Rounds; full++)
{
int start = full * max_Cells_at_a_time;
int stop = start + max_Cells_at_a_time;
cells_Rounds_start_stop.push_back(make_pair(start, stop));
}
if (partial_Rounds != 0)
{
int start = (start_Stop_cells.size() - 1) - partial_Rounds;
cells_Rounds_start_stop.push_back(make_pair(start, (start_Stop_cells.size() - 1)));
}
int sequence_Count = 0;
int index_Last_Written = 0;
// source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(current_Generation + 1),
string intermediary_Tissue_folder = source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(current_Generation + 1);
string dead_List = intermediary_Tissue_folder + "/dead_List.txt";
if (!filesystem::exists(intermediary_Tissue_folder))
{
functions.config_Folder(intermediary_Tissue_folder, to_string(current_Generation + 1) + " generation Tissue " + tissue_Names[tissue] + " sequences");
}
if (!filesystem::exists(dead_List))
{
functions.create_File(dead_List);
}
string cells_of_parents_location = output_Node_location + "/" + get_Name() + "/cells_of_Parents.csv";
string cells_of_progeny_location = output_Node_location + "/" + get_Name() + "/cells_of_Progeny.csv";
if (!filesystem::exists(cells_of_parents_location))
{
functions.create_File(cells_of_parents_location, "Sequence_ID\tParent_Cell_ID");
}
if (!filesystem::exists(cells_of_progeny_location))
{
functions.create_File(cells_of_progeny_location, "Sequence_ID\tProgeny_Cell_ID");
}
// exit(-1);
// size_t arraySize = sizeof(parents_in_Tissue) / sizeof(parents_in_Tissue[0]);
// exit(-1);
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;
int seqeunces_to_Process = start_Stop_cells[cells_Rounds_start_stop[cell_Round].second] - start_Stop_cells[cells_Rounds_start_stop[cell_Round].first];
cout << "Processing " << seqeunces_to_Process << " sequence(s) in total\n";
simulate_Cell_replication(functions, multi_Read, gpu_Limit, CUDA_device_IDs, num_Cuda_devices, source_sequence_Data_folder, indexed_Source_Folders[tissue],
CPU_cores, max_sequences_per_File,
genome_Length,
tissue, parents_in_Tissue, tissue_Names[tissue],
start_Stop_cells, cells_Rounds_start_stop[cell_Round].first, cells_Rounds_start_stop[cell_Round].second, num_of_Cells,
seqeunces_to_Process,
sequence_Count,
Reference_fitness_survivability_proof_reading,
mutation_recombination_proof_Reading_availability,
num_effect_Segregating_sites,
sequence_Fitness_changes,
sequence_Survivability_changes,
sequence_Proof_reading_changes,
mutation_Hotspots,
mutation_hotspot_parameters,
A_0_mutation,
T_1_mutation,
G_2_mutation,
C_3_mutation,
recombination_Hotspots,
recombination_hotspot_parameters,
tot_prob_selectivity,
recombination_prob_Stride,
recombination_select_Stride,
recombination_Prob_matrix,
recombination_Select_matrix,
progeny_distribution_parameters_Array,
cells_of_parents_location,
dead_List, sequence_Profiles, sequence_parent_Progeny_relationships, cells_of_progeny_location,
index_Last_Written,
gen);
}
// free(parents_in_Tissue);
write_Partial_Sequence_Progeny(functions,
intermediary_Tissue_folder,
dead_List,
index_Last_Written,
tissue);
// cout << "\nCount\n";
// cout << dead_Particle_count[tissue] << endl
// << current_Viral_load_per_Tissue[tissue] << endl;
// // TODO: COMPRESS THE PREVIOUS GENERAIONS (Current generations) SEQUENCES per tissue FOLDER
if (enable_Folder_management == "YES")
{
compress_Folder(source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(current_Generation), enable_Compression);
}
}
cout << "Clearing parent cell array: ";
functions.clear_Array_int_CPU(parents_in_Tissue, 2);
cout << "cleared" << endl;
}
else
{
removed_by_Transfer_Indexes[tissue].clear();
dead_Particle_count[tissue] = 0;
current_Viral_load_per_Tissue[tissue] = 0;
if (enable_Folder_management == "YES")
{
if (filesystem::exists(source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(current_Generation)))
{
compress_Folder(source_sequence_Data_folder + "/" + to_string(tissue) + "/generation_" + to_string(current_Generation), enable_Compression);
}
}
}
// cout << "Cell Limit: " << cell_Limit[tissue] << endl;
// cout << "Distribution type: " << cell_Distribution_Type[profile_ID][tissue] << endl;
// cout << viral_distribution_per_Tissue_param[tissue].first << "\t" << viral_distribution_per_Tissue_param[tissue].second << endl;
// // TODO: Write per node generational summary
fstream generational_Summary_File;
generational_Summary_File.open(generational_Summary, ios::app);
if (generational_Summary_File.is_open())
{
// overall_Generation\tnode_Generation\tTissue\tnum_Parents\tnum_Progeny\tdead_Progeny
generational_Summary_File << to_string(overall_Generations)
<< "\t" << to_string(current_Generation)
<< "\t" << tissue_Names[tissue]
<< "\t" << to_string(parents_Prev_generation[tissue])
<< "\t" << to_string(current_Viral_load_per_Tissue[tissue])
<< "\t" << to_string(dead_Particle_count[tissue]) << endl;
generational_Summary_File.close();
}
else
{
cout << "ERROR: UNABLE TO OPEN NODE GENERATIONAL SUMMARY FILE: " << generational_Summary << endl;
exit(-1);
}
}
// particle migration between tissues
current_Generation++;
// for (int tissue = 0; tissue < num_Tissues; tissue++)
// {
// cout << "Tissue: " << tissue << endl;
// cout << "\nCount\n";
// cout << dead_Particle_count[tissue] << endl
// << current_Viral_load_per_Tissue[tissue] << endl;
// }
//exit(-1);
if (viral_Migration == "YES")
{
// TEST SOME more
particle_Migration_between_Tissues(functions,
viral_Migration_Values,
migration_start_Generation,
source_sequence_Data_folder,
tissue_Names,
sequence_parent_Progeny_relationships, sequence_Profiles,
gen);
}
// exit(-1);
}
}
else
{
if (infected_to_Recovered == "NO")
{
set_Removed();
if (enable_Folder_management == "YES")
{
compress_Folder(source_sequence_Data_folder, enable_Compression);
}
clear_Arrays_end();
}
else
{
set_Susceptible();
}
}
free(real_Particle_count_per_Tissue);
}
else
{
set_Removed();
if (enable_Folder_management == "YES")
{
compress_Folder(source_sequence_Data_folder, enable_Compression);
}
}
// get each tissues generational phase
}
void node_within_host::compress_Folder(string path, string &enable_Compression)
{
if (filesystem::exists(path))
{
cout << "\nCompressing folder: " << path << endl;
string tar_Folder;
string command_Tar;
if (enable_Compression == "YES")
{
tar_Folder = path + ".tar.gz";
command_Tar = "tar -czf " + tar_Folder + " " + path + " && rm -R " + path;
}
else
{
tar_Folder = path + ".tar";
command_Tar = "tar -cf " + tar_Folder + " " + path + " && rm -R " + path;
}
int result = system(command_Tar.c_str());
if (result == 0)
{
cout << "Compression successful" << endl;
}
else
{
cout << "Failed to compress the folder: " << path << endl;
exit(-1);
}
}
else
{
cout << "COMPRESSION ERROR: UNABLE TO FIND THE FOLDER: " << path << endl;
exit(-1);
}
}
void node_within_host::compress_Folder(string path, string enable_Compression, int thread)
{
if (filesystem::exists(path))
{
cout << "\nCompressing folder: " << path << endl;