This repository was archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpManager.cpp
More file actions
1395 lines (1097 loc) · 50.8 KB
/
Copy pathExpManager.cpp
File metadata and controls
1395 lines (1097 loc) · 50.8 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
// ***************************************************************************************************************
//
// Mini-Aevol is a reduced version of Aevol -- An in silico experimental evolution platform
//
// ***************************************************************************************************************
//
// Copyright: See the AUTHORS file provided with the package or <https://gitlab.inria.fr/rouzaudc/mini-aevol>
// Web: https://gitlab.inria.fr/rouzaudc/mini-aevol
// E-mail: See <jonathan.rouzaud-cornabas@inria.fr>
// Original Authors : Jonathan Rouzaud-Cornabas
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ***************************************************************************************************************
#define NBTHREADS 16
#include <cmath>
#include <map>
#include <algorithm>
#include <sys/stat.h>
#include <err.h>
#include <chrono>
#include <iostream>
using namespace std::chrono;
using namespace std;
#include "ExpManager.h"
#include "Algorithms.h"
#include "AeTime.h"
#include "Promoter.h"
#include "RNA.h"
#include "Protein.h"
#include "Organism.h"
#include "Gaussian.h"
#include <utility>
/**
* Constructor for initializing a new simulation
*
* @param grid_height : Height of the grid containing the organisms
* @param grid_width : Width of the grid containing the organisms
* @param seed : Global seed for all the PRNG of the simulation
* @param mutation_rate : Mutation rates for all the organism during the simulation
* @param init_length_dna : Size of the randomly generated DNA at the initialization of the simulation
* @param w_max : Maximum width of the triangle generated by a Protein
* @param selection_pressure : Selection pressure used during the selection process
* @param backup_step : How much often checkpoint must be done
*/
ExpManager::ExpManager(int grid_height, int grid_width, int seed, double mutation_rate, int init_length_dna,
double w_max, int selection_pressure, int backup_step)
: nb_indivs_(grid_height * grid_width)
, rng_(new Threefry(grid_width, grid_height, seed))
, selection_pressure_(selection_pressure)
, grid_height_(grid_height)
, grid_width_(grid_width)
, mutation_rate_(mutation_rate)
, w_max_(w_max)
, backup_step_(backup_step)
{
internal_organisms_ = new std::shared_ptr<Organism>[nb_indivs_];
prev_internal_organisms_ = new std::shared_ptr<Organism>[nb_indivs_];
next_generation_reproducer_ = new int[nb_indivs_];
dna_mutator_array_ = new DnaMutator*[nb_indivs_];
// Building the target environment
Gaussian* g1 = new Gaussian(1.2,0.52,0.12);
Gaussian* g2 = new Gaussian(-1.4,0.5,0.07);
Gaussian* g3 = new Gaussian(0.3,0.8,0.03);
target = new double[300];
for (int i = 0; i < 300; i++) {
double pt_i = ((double)i)/300.0;
double tmp = g1->compute_y(pt_i);
tmp += g2->compute_y(pt_i);
tmp += g3->compute_y(pt_i);
tmp = tmp > Y_MAX ? Y_MAX : tmp;
tmp = tmp < Y_MIN ? Y_MIN : tmp;
target[i] = tmp;
}
delete g1;
delete g2;
delete g3;
geometric_area_ = 0;
// (fabs is float/double absolute function)
for (int i = 0; i < 298; i++) {
geometric_area_+=((target[i] + target[i+1]) / (600.0));
}
printf("Initialized environmental target %f\n",geometric_area_);
// Initializing the PRNGs
for (int indiv_id = 0; indiv_id < nb_indivs_; ++indiv_id) {
dna_mutator_array_[indiv_id] = nullptr;
}
// Generate a random organism that is better than nothing
bool found_good_random = false;
double r_best = 0;
int i_best = -1;
while(!found_good_random) {
#pragma omp parallel for
for(int i = 0; i < NBTHREADS; i++){
auto random_organism = std::make_shared<Organism>(this, init_length_dna, i);
// TODO enqueter, rarement il y a une petite différence avec ce qui est fait par l'ancien programme, comme si
// qqs rares RNA étaient non trouvés
start_stop_RNA(random_organism);
compute_RNA(random_organism);
start_protein(random_organism);
compute_protein(random_organism);
translate_protein(random_organism, w_max);
compute_phenotype(random_organism);
compute_fitness(random_organism, selection_pressure);
double r_compare = round((random_organism->metaerror-geometric_area_)* 1E10) / 1E10;
#pragma omp critical
{
if(r_compare < r_best || (r_compare == r_best && i > i_best)){
internal_organisms_[0] = random_organism;
found_good_random = true;
r_best = r_compare;
i_best = i;
}
}
}
}
printf("Populating the environment\n");
// Create a population of clones based on the randomly generated organism
for (int indiv_id = 0; indiv_id < nb_indivs_; indiv_id++) {
prev_internal_organisms_[indiv_id] = internal_organisms_[indiv_id] =
std::make_shared<Organism>(this, internal_organisms_[0]);
internal_organisms_[indiv_id]->indiv_id_ = indiv_id;
internal_organisms_[indiv_id]->parent_id_ = 0;
internal_organisms_[indiv_id]->global_id = AeTime::time()*nb_indivs_+indiv_id;
}
// Create backup and stats directory
create_directory();
}
/**
* Constructor to resume/restore a simulation from a given backup/checkpoint file
*
* @param time : resume from this generation
*/
ExpManager::ExpManager(int time) {
target = new double[300];
load(time);
geometric_area_ = 0;
for (int i = 0; i < 298; i++) {
geometric_area_+=((fabs(target[i]) + fabs(target[i+1])) / (600.0));
}
printf("Initialized environmental target %f\n", geometric_area_);
dna_mutator_array_ = new DnaMutator*[nb_indivs_];
for (int indiv_id = 0; indiv_id < nb_indivs_; ++indiv_id) {
dna_mutator_array_[indiv_id] = nullptr;
}
}
/**
* Create stats and backup directory
*/
void ExpManager::create_directory() {
// Backup
int status = mkdir("backup", 0755);
if (status == -1 && errno != EEXIST)
{
err(EXIT_FAILURE, "backup");
}
// Stats
status = mkdir("stats", 0755);
if (status == -1 && errno != EEXIST)
{
err(EXIT_FAILURE, "stats");
}
}
/**
* Checkpointing/Backup of the population of organisms
*
* @param t : simulated time of the checkpoint
*/
void ExpManager::save(int t) {
char exp_backup_file_name[255];
sprintf(exp_backup_file_name, "backup/backup_%d.zae", t);
// -------------------------------------------------------------------------
// Open backup files
// -------------------------------------------------------------------------
gzFile exp_backup_file = gzopen(exp_backup_file_name, "w");
// -------------------------------------------------------------------------
// Check that files were correctly opened
// -------------------------------------------------------------------------
if (exp_backup_file == Z_NULL)
{
printf("Error: could not open backup file %s\n",
exp_backup_file_name);
exit(EXIT_FAILURE);
}
// -------------------------------------------------------------------------
// Write the backup file
// -------------------------------------------------------------------------
printf("Save size\n");
gzwrite(exp_backup_file,&t,sizeof(t));
gzwrite(exp_backup_file,&grid_height_,sizeof(grid_height_));
gzwrite(exp_backup_file,&grid_width_,sizeof(grid_width_));
gzwrite(exp_backup_file,&nb_indivs_,sizeof(nb_indivs_));
gzwrite(exp_backup_file,&backup_step_,sizeof(backup_step_));
//rng_->save(exp_backup_file);
gzwrite(exp_backup_file,&w_max_,sizeof(w_max_));
gzwrite(exp_backup_file,&selection_pressure_,sizeof(selection_pressure_));
gzwrite(exp_backup_file,&mutation_rate_,sizeof(mutation_rate_));
for (int i = 0; i < 300; i++) {
double tmp = target[i];
gzwrite(exp_backup_file,&tmp,sizeof(tmp));
}
for (int indiv_id = 0; indiv_id < nb_indivs_; indiv_id++) {
prev_internal_organisms_[indiv_id]->save(exp_backup_file);
}
rng_->save(exp_backup_file);
gzclose(exp_backup_file);
}
/**
* Loading a simulation from a checkpoint/backup file
*
* @param t : resuming the simulation at this generation
*/
void ExpManager::load(int t) {
char exp_backup_file_name[255];
sprintf(exp_backup_file_name, "backup/backup_%d.zae", t);
// -------------------------------------------------------------------------
// Open backup files
// -------------------------------------------------------------------------
gzFile exp_backup_file = gzopen(exp_backup_file_name, "r");
// -------------------------------------------------------------------------
// Check that files were correctly opened
// -------------------------------------------------------------------------
if (exp_backup_file == Z_NULL)
{
printf("Error: could not open backup file %s\n",
exp_backup_file_name);
exit(EXIT_FAILURE);
}
// -------------------------------------------------------------------------
// Write the backup file
// -------------------------------------------------------------------------
int time;
gzread(exp_backup_file,&time,sizeof(time));
AeTime::set_time(time);
gzread(exp_backup_file,&grid_height_,sizeof(grid_height_));
gzread(exp_backup_file,&grid_width_,sizeof(grid_width_));
gzread(exp_backup_file,&nb_indivs_,sizeof(nb_indivs_));
internal_organisms_ = new std::shared_ptr<Organism> [nb_indivs_];
prev_internal_organisms_ = new std::shared_ptr<Organism> [nb_indivs_];
next_generation_reproducer_ = new int[nb_indivs_];
gzread(exp_backup_file,&backup_step_,sizeof(backup_step_));
gzread(exp_backup_file,&w_max_,sizeof(w_max_));
gzread(exp_backup_file,&selection_pressure_,sizeof(selection_pressure_));
gzread(exp_backup_file,&mutation_rate_,sizeof(mutation_rate_));
for (int i = 0; i < 300; i++) {
double tmp;
gzread(exp_backup_file,&tmp,sizeof(tmp));
target[i] = tmp;
}
for (int indiv_id = 0; indiv_id < nb_indivs_; indiv_id++) {
prev_internal_organisms_[indiv_id] = internal_organisms_[indiv_id] =
std::make_shared<Organism>(this, exp_backup_file);
}
rng_ = std::move(std::make_unique<Threefry>(grid_width_, grid_height_, exp_backup_file));
gzclose(exp_backup_file);
}
/**
* Generate mutation of an organism and applying them to its DNA
*
* @param indiv_id : Organism unique id
*/
void ExpManager::do_mutation(int indiv_id) {
auto rng = std::move(rng_->gen(indiv_id, Threefry::MUTATION));
delete dna_mutator_array_[indiv_id];
dna_mutator_array_[indiv_id] = new DnaMutator(
&rng,
prev_internal_organisms_[next_generation_reproducer_[indiv_id]]->length(),
mutation_rate_, indiv_id);
dna_mutator_array_[indiv_id]->generate_mutations();
if (dna_mutator_array_[indiv_id]->hasMutate()) {
internal_organisms_[indiv_id] =
std::make_shared<Organism>(this, prev_internal_organisms_[next_generation_reproducer_[indiv_id]]);
internal_organisms_[indiv_id]->global_id = AeTime::time() * nb_indivs_ + indiv_id;
internal_organisms_[indiv_id]->indiv_id_ = indiv_id;
internal_organisms_[indiv_id]->parent_id_ =
next_generation_reproducer_[indiv_id];
internal_organisms_[indiv_id]->apply_mutations();
} else {
int parent_id = next_generation_reproducer_[indiv_id];
internal_organisms_[indiv_id] = prev_internal_organisms_[parent_id];
internal_organisms_[indiv_id]->usage_count_++;
}
}
/**
* Destructor of the ExpManager class
*/
ExpManager::~ExpManager() {
delete stats_best;
delete stats_mean;
for (auto i = 0 ; i < nb_indivs_ ; ++i) {
delete dna_mutator_array_[i];
}
delete [] dna_mutator_array_;
delete [] internal_organisms_;
delete [] prev_internal_organisms_;
delete [] next_generation_reproducer_;
delete [] target;
}
/**
* Execute a generation of the simulation for all the Organisms
*
* @param w_max : Maximum width of the triangle generated by a Protein
* @param selection_pressure : Selection pressure used during the selection process
* @param first_gen : is it the first generation simulated ? (generation 1 or first generation after a restore)
*/
void ExpManager::run_a_step(double w_max, double selection_pressure, bool first_gen) {
// Running the simulation process for each organism
#pragma omp parallel
{
#pragma omp for
for(int indiv_id = 0; indiv_id < nb_indivs_; indiv_id++){
selection(indiv_id);
do_mutation(indiv_id);
opt_prom_compute_RNA(indiv_id);
if (dna_mutator_array_[indiv_id]->hasMutate()) {
start_protein(internal_organisms_[indiv_id]);
compute_protein(internal_organisms_[indiv_id]);
translate_protein(internal_organisms_[indiv_id], w_max);
compute_phenotype(internal_organisms_[indiv_id]);
compute_fitness(internal_organisms_[indiv_id], selection_pressure);
}
}
#pragma omp for
for(int indiv_id = 0; indiv_id < nb_indivs_; indiv_id++){
prev_internal_organisms_[indiv_id] = internal_organisms_[indiv_id];
internal_organisms_[indiv_id] = nullptr;
}
}
// Search for the best
double best_fitness = prev_internal_organisms_[0]->fitness;
int idx_best = 0;
for (int indiv_id = 1; indiv_id < nb_indivs_; indiv_id++) {
if (prev_internal_organisms_[indiv_id]->fitness > best_fitness) {
idx_best = indiv_id;
best_fitness = prev_internal_organisms_[indiv_id]->fitness;
}
}
best_indiv = prev_internal_organisms_[idx_best];
// Stats
if (first_gen) {
stats_best = new Stats(this, AeTime::time(), true);
stats_mean = new Stats(this, AeTime::time(), false);
} else {
stats_best->reinit(AeTime::time());
stats_mean->reinit(AeTime::time());
}
std::vector<int> already_seen;
for (int indiv_id = 0; indiv_id < nb_indivs_; indiv_id++) {
if (std::find(already_seen.begin(), already_seen.end(), indiv_id) == already_seen.end()) {
prev_internal_organisms_[indiv_id]->reset_stats();
for (int i = 0; i < prev_internal_organisms_[indiv_id]->rna_count_; i++) {
if (prev_internal_organisms_[indiv_id]->rnas[i] != nullptr) {
if (prev_internal_organisms_[indiv_id]->rnas[i]->is_coding_)
prev_internal_organisms_[indiv_id]->nb_coding_RNAs++;
else
prev_internal_organisms_[indiv_id]->nb_non_coding_RNAs++;
}
}
for (int i = 0; i < prev_internal_organisms_[indiv_id]->protein_count_; i++) {
if (prev_internal_organisms_[indiv_id]->rnas[i] != nullptr) {
if (prev_internal_organisms_[indiv_id]->proteins[i]->is_functional) {
prev_internal_organisms_[indiv_id]->nb_func_genes++;
} else {
prev_internal_organisms_[indiv_id]->nb_non_func_genes++;
}
if (prev_internal_organisms_[indiv_id]->proteins[i]->h > 0) {
prev_internal_organisms_[indiv_id]->nb_genes_activ++;
} else {
prev_internal_organisms_[indiv_id]->nb_genes_inhib++;
}
}
}
}
}
stats_best->write_best();
stats_mean->write_average();
}
/**
* Search for Promoters and Terminators (i.e. beginning and ending of a RNA) within the whole DNA of an Organism
*
* @param indiv_id : Unique identification number of the organism
*/
void ExpManager::start_stop_RNA(std::shared_ptr<Organism> indiv) {
for (int dna_pos = 0; dna_pos < indiv->length(); dna_pos++) {
if (indiv->length() >= PROM_SIZE) {
int dist_lead = indiv->dna_->promoter_at(dna_pos);
if (dist_lead <= 4) {
Promoter* nprom = new Promoter(dna_pos, dist_lead);
int prom_idx = indiv->count_prom;
indiv->count_prom = indiv->count_prom + 1;
indiv->promoters[prom_idx] = nprom;
indiv->prom_pos[dna_pos] = prom_idx;
}
// Computing if a terminator exists at that position
int dist_term_lead = indiv->dna_->terminator_at(dna_pos);
if (dist_term_lead == 4) {
indiv->terminators.insert(dna_pos);
}
}
}
}
/**
* Optimize version that do not need to search the whole Dna for promoters
*/
void ExpManager::opt_prom_compute_RNA(int indiv_id) {
if (dna_mutator_array_[indiv_id]->hasMutate()) {
internal_organisms_[indiv_id]->proteins.clear();
internal_organisms_[indiv_id]->rnas.clear();
internal_organisms_[indiv_id]->terminators.clear();
internal_organisms_[indiv_id]->rnas.resize(
internal_organisms_[indiv_id]->promoters.size());
for (int prom_idx = 0; prom_idx< internal_organisms_[indiv_id]->promoters.size(); prom_idx++) {
if (internal_organisms_[indiv_id]->promoters[prom_idx] != nullptr) {
int rna_idx = prom_idx;
Promoter *prom;
prom = internal_organisms_[indiv_id]->promoters[rna_idx];
if (prom != nullptr) {
int prom_pos;
double prom_error;
prom_pos = internal_organisms_[indiv_id]->promoters[rna_idx]->pos;
prom_error = fabs(
((float) internal_organisms_[indiv_id]->promoters[rna_idx]->error));
/* Search for terminators */
int cur_pos =
prom_pos + 22;
cur_pos = cur_pos >= internal_organisms_[indiv_id]->length() ? cur_pos -
internal_organisms_[indiv_id]->length() :
cur_pos;
int start_pos = cur_pos;
bool terminator_found = false;
bool no_terminator = false;
int term_dist_leading = 0;
int loop_size = 0;
while (!terminator_found) {
loop_size++;
for (int t_motif_id = 0; t_motif_id < 4; t_motif_id++)
term_dist_leading = internal_organisms_[indiv_id]->dna_->terminator_at(cur_pos);
if (term_dist_leading == 4)
terminator_found = true;
else {
cur_pos = cur_pos + 1 >= internal_organisms_[indiv_id]->length() ? cur_pos + 1 -
internal_organisms_[indiv_id]->length()
:
cur_pos + 1;
term_dist_leading = 0;
if (cur_pos == start_pos) {
no_terminator = true;
terminator_found = true;
}
}
}
if (!no_terminator) {
int32_t rna_end =
cur_pos + 10 >= internal_organisms_[indiv_id]->length() ?
cur_pos + 10 - internal_organisms_[indiv_id]->length() :
cur_pos + 10;
int32_t rna_length = 0;
if (prom_pos
> rna_end)
rna_length = internal_organisms_[indiv_id]->length() -
prom_pos
+ rna_end;
else
rna_length = rna_end - prom_pos;
rna_length -= 21;
if (rna_length > 0) {
int glob_rna_idx = internal_organisms_[indiv_id]->rna_count_;
internal_organisms_[indiv_id]->rna_count_ =
internal_organisms_[indiv_id]->rna_count_ + 1;
internal_organisms_[indiv_id]->rnas[glob_rna_idx] = new RNA(
internal_organisms_[indiv_id]->promoters[rna_idx]->pos,
rna_end,
1.0 -
std::fabs(
((float) internal_organisms_[indiv_id]->promoters[rna_idx]->error)) /
5.0, rna_length);
}
}
}
}
}
}
}
/**
* Create the list of RNAs based on the found promoters and terminators on the DNA of an Organism
*
* @param indiv_id : Unique identification number of the organism
*/
void ExpManager::compute_RNA(std::shared_ptr<Organism> indiv) {
indiv->rnas.resize(indiv->promoters.size());
for (int rna_idx = 0; rna_idx < (int) indiv->promoters.size(); rna_idx++) {
{
if (indiv->promoters[rna_idx] != nullptr) {
if (indiv->terminators.size() != 0) {
int k = indiv->promoters[rna_idx]->pos + 22;
k = k >= indiv->length() ? k - indiv->length() : k;
auto it_rna_end = indiv->terminators.lower_bound(
k);
if (it_rna_end == indiv->terminators.end()) {
it_rna_end = indiv->terminators.begin();
}
int rna_end =
*it_rna_end + 10 >= indiv->length() ?
*it_rna_end + 10 - indiv->length() :
*it_rna_end + 10;
int rna_length = 0;
if (indiv->promoters[rna_idx]->pos
> rna_end)
rna_length = indiv->length() -
indiv->promoters[rna_idx]->pos
+ rna_end;
else
rna_length = rna_end - indiv->
promoters[rna_idx]->pos;
rna_length -= 21;
if (rna_length >= 0) {
int glob_rna_idx = indiv->rna_count_;
indiv->rna_count_ = indiv->rna_count_ + 1;
indiv->rnas[glob_rna_idx] = new RNA(
indiv->promoters[rna_idx]->pos,
rna_end,
1.0 -
std::fabs(
((float) indiv->promoters[rna_idx]->error)) /
5.0, rna_length);
}
}
}
}
}
}
/**
* Search for Shine Dal sequence and Start sequence deliminating the start of genes within one of the RNA of an Organism
*
* @param indiv_id : Unique identification number of the organism
*/
void ExpManager::start_protein(std::shared_ptr<Organism> indiv) {
for (int rna_idx = 0; rna_idx < (int) indiv->rna_count_; rna_idx++) {
{
if (indiv->rnas[rna_idx]->is_init_) {
int c_pos = indiv->rnas[rna_idx]->begin;
if (indiv->rnas[rna_idx]->length >= 22) {
c_pos += 22;
c_pos =
c_pos >= indiv->length() ?
c_pos - indiv->length()
: c_pos;
while (c_pos != indiv->rnas[rna_idx]->end) {
if (indiv->dna_->shine_dal_start(c_pos)) {
indiv->rnas[rna_idx]->start_prot.
push_back(c_pos);
}
c_pos++;
c_pos =
c_pos >= indiv->length() ?
c_pos - indiv->length()
: c_pos;
}
}
}
}
}
}
/**
* Compute the list of genes/proteins of an Organism
*
* @param indiv_id : Unique identification number of the organism
*/
void ExpManager::compute_protein(std::shared_ptr<Organism> indiv) {
int resize_to = 0;
for (int rna_idx = 0; rna_idx < (int) indiv->rna_count_; rna_idx++) {
if (indiv->rnas[rna_idx]->is_init_)
resize_to += indiv->rnas[rna_idx]->start_prot.size();
}
indiv->proteins.resize(resize_to);
for (int rna_idx = 0; rna_idx < (int) indiv->rna_count_; rna_idx++) {
if (indiv->rnas[rna_idx]->is_init_) {
for (int protein_idx = 0;
protein_idx < (int) indiv->rnas[rna_idx]->start_prot.size(); protein_idx++) {
int start_protein_pos = indiv->rnas[rna_idx]->start_prot[protein_idx] + 13;
int length;
start_protein_pos = start_protein_pos >= indiv->length() ?
start_protein_pos - indiv->length()
: start_protein_pos;
if (indiv->
rnas[rna_idx]->start_prot[protein_idx] <
indiv->rnas[rna_idx]->end) {
length = indiv->rnas[rna_idx]->end -
indiv->rnas[rna_idx]->start_prot[protein_idx];
} else {
length = indiv->length() -
indiv->rnas[rna_idx]->start_prot[protein_idx] +
indiv->rnas[rna_idx]->end;
}
length -= 13;
bool is_protein = false;
length += 1;
length = length - (length % 3);
int j = 0;
int transcribed_start = 0;
transcribed_start = indiv->rnas[rna_idx]->begin + 22;
transcribed_start = transcribed_start >= indiv->length() ?
transcribed_start - indiv->length()
: transcribed_start;
if (transcribed_start <= indiv->
rnas[rna_idx]->start_prot[protein_idx]) {
j = indiv->
rnas[rna_idx]->start_prot[protein_idx] -
transcribed_start;
} else {
j = indiv->length() -
transcribed_start +
indiv->
rnas[rna_idx]->start_prot[protein_idx];
}
j += 13;
while (indiv->
rnas[rna_idx]->length - j >= 3) {
int t_k;
start_protein_pos =
start_protein_pos >= indiv->length() ?
start_protein_pos - indiv->length()
: start_protein_pos;
is_protein = indiv->dna_->protein_stop(start_protein_pos);
if (is_protein) {
int prot_length = -1;
t_k = start_protein_pos + 2 >= indiv->length() ?
start_protein_pos - indiv->length() + 2 :
start_protein_pos + 2;
if (indiv->
rnas[rna_idx]->start_prot[protein_idx] + 13 < t_k) {
prot_length = t_k -
(indiv->
rnas[rna_idx]->start_prot[protein_idx] +
13);
} else {
prot_length = indiv->length() -
(indiv->
rnas[rna_idx]->start_prot[protein_idx] +
13) + t_k;
}
if (prot_length >= 3) {
int glob_prot_idx = indiv->protein_count_;
indiv->protein_count_ =
indiv->protein_count_ + 1;
indiv->
proteins[glob_prot_idx] = new Protein(
indiv->
rnas[rna_idx]->start_prot[protein_idx], t_k,
prot_length,
indiv->rnas[rna_idx]->e
);
indiv->
rnas[rna_idx]->is_coding_ = true;
}
break;
}
start_protein_pos += 3;
start_protein_pos =
start_protein_pos >= indiv->length() ?
start_protein_pos - indiv->length()
: start_protein_pos;
j += 3;
}
}
}
}
}
/**
* Compute the pseudo-chimical model (i.e. the width, height and location in the phenotypic space) of a genes/protein
*
* @param indiv_id : Unique identification number of the organism
* @param w_max : Maximum width of the triangle generated by a Protein
*/
void ExpManager::translate_protein(std::shared_ptr<Organism> indiv, double w_max) {
for (int protein_idx = 0; protein_idx < (int) indiv->protein_count_; protein_idx++) {
{
if (indiv->proteins[protein_idx]->is_init_) {
int c_pos = indiv->proteins[protein_idx]->protein_start, t_pos;
int end_pos = indiv->proteins[protein_idx]->protein_end;
c_pos += 13;
end_pos -= 3;
c_pos =
c_pos >= indiv->length() ? c_pos -
indiv->length()
: c_pos;
end_pos = end_pos < 0 ? indiv->length() + end_pos : end_pos;
int value = 0;
int codon_list[64] = {};
int codon_idx = 0;
int count_loop = 0;
//printf("Codon list : ");
while (count_loop <
indiv->proteins[protein_idx]->protein_length /
3 &&
codon_idx < 64) {
codon_list[codon_idx] = indiv->dna_->codon_at(c_pos);
//printf("%d ",codon_list[codon_idx]);
codon_idx++;
count_loop++;
c_pos += 3;
c_pos =
c_pos >= indiv->length() ?
c_pos - indiv->length()
: c_pos;
}
//printf("\n");
double M = 0.0;
double W = 0.0;
double H = 0.0;
int nb_m = 0;
int nb_w = 0;
int nb_h = 0;
bool bin_m = false; // Initializing to false will yield a conservation of the high weight bit
bool bin_w = false; // when applying the XOR operator for the Gray to standard conversion
bool bin_h = false;
for (int i = 0; i < codon_idx; i++) {
switch (codon_list[i]) {
case CODON_M0 : {
// M codon found
nb_m++;
// Convert Gray code to "standard" binary code
bin_m ^= false; // as bin_m was initialized to false, the XOR will have no effect on the high weight bit
// A lower-than-the-previous-lowest weight bit was found, make a left bitwise shift
//~ M <<= 1;
M *= 2;
// Add this nucleotide's contribution to M
if (bin_m) M += 1;
break;
}
case CODON_M1 : {
// M codon found
nb_m++;
// Convert Gray code to "standard" binary code
bin_m ^= true; // as bin_m was initialized to false, the XOR will have no effect on the high weight bit
// A lower-than-the-previous-lowest bit was found, make a left bitwise shift
//~ M <<= 1;
M *= 2;
// Add this nucleotide's contribution to M
if (bin_m) M += 1;
break;
}
case CODON_W0 : {
// W codon found
nb_w++;
// Convert Gray code to "standard" binary code
bin_w ^= false; // as bin_m was initialized to false, the XOR will have no effect on the high weight bit
// A lower-than-the-previous-lowest weight bit was found, make a left bitwise shift
//~ W <<= 1;
W *= 2;
// Add this nucleotide's contribution to W
if (bin_w) W += 1;
break;
}
case CODON_W1 : {
// W codon found
nb_w++;
// Convert Gray code to "standard" binary code
bin_w ^= true; // as bin_m was initialized to false, the XOR will have no effect on the high weight bit
// A lower-than-the-previous-lowest weight bit was found, make a left bitwise shift
//~ W <<= 1;
W *= 2;
// Add this nucleotide's contribution to W
if (bin_w) W += 1;
break;
}
case CODON_H0 :
case CODON_START : // Start codon codes for the same amino-acid as H0 codon
{
// H codon found
nb_h++;
// Convert Gray code to "standard" binary code
bin_h ^= false; // as bin_m was initialized to false, the XOR will have no effect on the high weight bit