-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetsim.c
3354 lines (3129 loc) · 138 KB
/
netsim.c
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
/*
* This file contains functions to initialize simulation with specified
* selection condition, and to output summary of genotypes and network structure.
*
* Authors: Joanna Masel, Alex Lancaster, Kun Xiong
* Copyright (c) 2018 Arizona Board of Regents on behalf of the University of Arizona
* This file is part of network-evolution-simulator.
* network-evolution-simulator is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* network-evolution-simulator 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 Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with network-evolution-simulator. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <omp.h>
#include "netsim.h"
#include "cellular_activity.h"
#include "mutation.h"
#include "numerical.h"
#include "lib.h"
#include "RngStream.h"
#define INITIALIZATION -1
int MAX_TFBS_NUMBER=100;
static const float PROB_ACTIVATING=0.62;
static const float MEAN_PROTEIN_DECAY_RATE=-1.88;
static const float SD_PROTEIN_DECAY_RATE=0.561;
static const float MEAN_ACT_TO_INT_RATE=1.27;
static const float SD_ACT_TO_INT_RATE=0.226;
static const float MEAN_MRNA_DECAY_RATE=-1.49;
static const float SD_MRNA_DECAY_RATE=0.267;
static const float MEAN_PROTEIN_SYN_RATE=0.322;
static const float SD_PROTEIN_SYN_RATE=0.416;
const float MEAN_GENE_LENGTH=2.568; //log10(aa)
static const float SD_GENE_LENGTH=0.34;
static const float MIN_Kd=1.0e-9;
static const float MAX_Kd=1.0e-6;
static const float log_MIN_Kd=-9.0;
static const float log_MAX_Kd=-6.0;
static const float NS_Kd=1.0e-5;
const float KD2APP_KD=1.8e10;
static const float DEFAULT_UPDATE_INTERVAL=10.0; /*min*/
static const float MAX_TOLERABLE_CHANGE_IN_PROBABILITY_OF_BINDING=0.01;
static const float MIN_SELECTION_COEFFICIENT=1.0e-8;
/*Bounds*/
const float MAX_ACT_TO_INT_RATE=64.7;
const float MIN_ACT_TO_INT_RATE=0.59;
const float MAX_MRNA_DECAY=0.54;
const float MIN_MRNA_DECAY=7.5e-4;
const float MAX_PROTEIN_DECAY=0.69;
const float MIN_PROTEIN_DECAY=3.0e-6;
const float MAX_PROTEIN_SYN_RATE=61.4;
const float MIN_PROTEIN_SYN_RATE=4.5e-3;
const float MAX_KD=1.0e-5;
const float MIN_KD=0.0;
const int MAX_GENE_LENGTH=5000; //aa
const int MIN_GENE_LENGTH= 50; //aa
/*Number of genes*/
int N_TF_GENES=MAX_TF_GENES;
int N_EFFECTOR_GENES=MAX_EFFECTOR_GENES;
/******************************************************************************
*
* Private function prototypes
*
*****************************************************************************/
static void initialize_sequence(char *, int, int, RngStream);
static void initialize_genotype_fixed(Genotype *, int, int, int, RngStream);
static void set_signal(CellState *, Environment *, float, RngStream, int);
static void calc_avg_fitness(Genotype *, Selection *, int [MAX_GENES], float [MAX_PROTEINS], RngStream [N_THREADS], float *, float *);
static void calc_fitness_stats(Genotype *, Selection *, float (*)[N_REPLICATES], float (*)[N_REPLICATES], int);
static void try_replacement(Genotype *, Genotype *, int *, float*);
static void clone_genotype(Genotype *, Genotype *);
static void summarize_binding_sites(Genotype *,int);
static int evolve_N_steps(Genotype *, Genotype *, Mutation *, Selection *, Output_buffer [OUTPUT_INTERVAL], int *, int *, int [MAX_GENES], float [MAX_PROTEINS], RngStream, RngStream [N_THREADS], int);
static void run_simulation(Genotype *, Genotype *, Mutation *, Selection *, Selection *, int [MAX_GENES], float [MAX_PROTEINS], int, int, RngStream, RngStream [N_THREADS]);
static void continue_simulation(Genotype *, Genotype *, Mutation *, Selection *, Selection *, int, int [MAX_GENES], float [MAX_PROTEINS], RngStream, RngStream [N_THREADS]);
static void replay_mutations(Genotype *, Mutation *, int);
static void find_motifs(Genotype *);
static int find_TFBS_of_A_on_B(Genotype *, int, int);
static void find_activators_of_effector(Genotype *, int, int *, int [MAX_PROTEINS]);
static void build_hindrance_table(Genotype *, int, int, int [MAX_PROTEINS], int [MAX_PROTEINS][MAX_PROTEINS], int [MAX_PROTEINS][2][50]);
static void find_signal_regulated_genes(Genotype *, int, int [MAX_PROTEINS], int *, int *, int [MAX_GENES], int [MAX_GENES]);
static int find_isolated_C1FFL(Genotype *, int, int, int *, int *, int *, int *);
static int find_FFL_in_diamond(Genotype *, int, int, int *, int *, int *, int *);
static void determine_motif_logic(Genotype *, int [MAX_PROTEINS][MAX_PROTEINS], int, int, int, char, int [MAX_PROTEINS][2][50], int, int, int);
static void determine_near_AND_logic(Genotype *, int [MAX_PROTEINS][2][50], int, int, int, char, int);
static void tidy_output_files(char*, char*);
static void print_motifs(Genotype *);
static void store_mutant_info(Genotype *, Mutation *, Output_buffer *, int, int);
static void store_resident_info(Genotype *, Mutation *, Output_buffer *, int , int, int, float, int);
static void output_mutant_info(Output_buffer *, int);
static void output_resident_info(Output_buffer [OUTPUT_INTERVAL], int, int);
static void sample_motifs(Genotype *, Mutation *, int, RngStream);
static void sample_parameters(Genotype *, int, RngStream);
static void mark_genes_for_sampling(Genotype *, int, int, int, char);
static void mark_genes_to_be_perturbed(Genotype *, int, int, int, int, int);
static void modify_topology(Genotype *, Genotype *);
static void add_binding_site(Genotype *, int);
static void remove_binding_sites(Genotype *, int);
/*****************************************************************************
*
* Global functions
*
*****************************************************************************/
int evolve_under_selection(Genotype *resident,
Genotype *mutant,
Mutation *mut_record,
Selection *burn_in,
Selection *selection,
int init_mRNA[MAX_GENES],
float init_protein[MAX_GENES],
RngStream RS_main,
RngStream RS_parallel[N_THREADS])
{
int i;
int init_step;
FILE *fp;
/*create threads and rng streams*/
omp_set_num_threads(N_THREADS);
/* continue a simulation from a previously saved state?*/
fp=fopen("saving_point.txt","r");
if(fp!=NULL)
{
int replay_N_steps=0, int_buffer;
fscanf(fp,"%d %d",&replay_N_steps,&int_buffer);
fclose(fp);
fp=fopen(setup_summary,"a+");
fprintf(fp,"Continue simulation from step %d\n",replay_N_steps);
fclose(fp);
if(replay_N_steps!=0)
continue_simulation(resident,
mutant,
mut_record,
burn_in,
selection,
replay_N_steps,
init_mRNA,
init_protein,
RS_main,
RS_parallel);
}
else /* otherwise the simulation starts over from beginning*/
{
/* record the initial network topology*/
init_step=0;
summarize_binding_sites(resident,init_step); /*snapshot of the initial (0) distribution binding sites */
find_motifs(resident);
print_motifs(resident);
/*calculate the fitness of the initial genotype*/
float GR1[HI_RESOLUTION_RECALC][N_REPLICATES],GR2[HI_RESOLUTION_RECALC][N_REPLICATES];
if(burn_in->MAX_STEPS!=0)
{
for(i=0;i<HI_RESOLUTION_RECALC;i++)
calc_avg_fitness(resident, burn_in, init_mRNA, init_protein, RS_parallel, GR1[i], GR2[i]);
calc_fitness_stats(resident,burn_in,&(GR1[0]),&(GR2[0]),HI_RESOLUTION_RECALC);
}
else
{
for(i=0;i<HI_RESOLUTION_RECALC;i++)
calc_avg_fitness(resident, selection, init_mRNA, init_protein, RS_parallel, GR1[i], GR2[i]);
calc_fitness_stats(resident,selection,&(GR1[0]),&(GR2[0]),HI_RESOLUTION_RECALC);
}
/* make title of the output file*/
fp=fopen(evo_summary,"w");
fprintf(fp,"step N_tot_mut_tried N_mut_tried_this_step N_hit_bound accepted_mut selection_coeff avg_fitness fitness1 fitness2 se_avg_fitness se_fitness1 se_fitness2 N_genes N_proteins N_activator N_repressor\n");
fprintf(fp,"0 0 0 0 na na %.10f %.10f %.10f %.10f %.10f %.10f %d %d %d %d \n",
resident->avg_fitness,
resident->fitness1,
resident->fitness2,
resident->SE_avg_fitness,
resident->SE_fitness1,
resident->SE_fitness2,
resident->ngenes,
resident->nproteins,
resident->N_act,
resident->N_rep);
fclose(fp);
run_simulation( resident,
mutant,
mut_record,
burn_in,
selection,
init_mRNA,
init_protein,
0, // this is the number of total mutations that have been tried
1, // this tells the program from which step the simulation begins
RS_main,
RS_parallel);
}
print_mutatable_parameters(resident,1);
/*delete rng streams*/
for(i=0;i<N_THREADS;i++)
RngStream_DeleteStream (&RS_parallel[i]);
return 1;
}
#if NEUTRAL
void evolve_neutrally(Genotype *resident, Mutation *mut_record, Selection *burn_in, Selection *selection, RngStream RS_main)
{
int i, output_counter=0;
FILE *fp;
Output_buffer resident_info[OUTPUT_INTERVAL];
/*Set resident fitness to 0*/
resident->fitness1=0.0;
resident->fitness2=0.0;
resident->avg_fitness=0.0;
resident->SE_avg_fitness=0.0;
resident->SE_fitness1=0.0;
resident->SE_fitness2=0.0;
/*Create title for output files*/
fp=fopen(evo_summary,"a+");
fprintf(fp,"step N_tot_mut_tried N_mut_tried_this_step N_hit_bound accepted_mut selection_coeff avg_fitness fitness1 fitness2 se_avg_fitness se_fitness1 se_fitness2 N_genes N_proteins N_activator N_repressor\n");
fprintf(fp,"0 0 0 na na 0.0 0.0 0.0 0.0 0.0 0.0 0 0 0 0 \n");
fclose(fp);
/*record the initial network*/
calc_all_binding_sites(resident);
summarize_binding_sites(resident,0);
/*set BURN-IN conditions*/
DUPLICATION=burn_in->temporary_DUPLICATION;
SILENCING=burn_in->temporary_SILENCING;
N_EFFECTOR_GENES=burn_in->temporary_N_effector_genes;
N_TF_GENES=burn_in->temporary_N_tf_genes;
miu_ACT_TO_INT_RATE=burn_in->temporary_miu_ACT_TO_INT_RATE;
miu_Kd=burn_in->temporary_miu_Kd;
miu_protein_syn_rate=burn_in->temporary_miu_protein_syn_rate;
/*burn in*/
for(i=1;i<=burn_in->MAX_STEPS;i++)
{
mutate(resident,RS_main,mut_record);
calc_all_binding_sites(resident);
find_motifs(resident);
store_resident_info(resident, mut_record, &(resident_info[output_counter]), i, 1, i, 0.0, 1); //magic number 1 means store everything
output_counter++;
/*output network topology every OUTPUT_INTERVAL steps*/
if(i%OUTPUT_INTERVAL==0)
{
summarize_binding_sites(resident,i);
output_resident_info(resident_info,output_counter,1); //magic number 1 means to output everything
output_counter=0;
}
}
/*set post burn-in condition*/
DUPLICATION=selection->temporary_DUPLICATION;
SILENCING=selection->temporary_SILENCING;
N_EFFECTOR_GENES=selection->temporary_N_effector_genes;
N_TF_GENES=selection->temporary_N_tf_genes;
miu_ACT_TO_INT_RATE=selection->temporary_miu_ACT_TO_INT_RATE;
miu_Kd=selection->temporary_miu_Kd;
miu_protein_syn_rate=selection->temporary_miu_protein_syn_rate;
for(;i<=selection->MAX_STEPS;i++)
{
mutate(resident,RS_main,mut_record);
calc_all_binding_sites(resident);
find_motifs(resident);
store_resident_info(resident, mut_record, &(resident_info[output_counter]), i, 1, i, 0.0, 1); //magic number 1 means store everything
output_counter++;
/*output network topology every OUTPUT_INTERVAL steps*/
if(i%OUTPUT_INTERVAL==0 && i!=burn_in->MAX_STEPS)
{
summarize_binding_sites(resident,i);
output_resident_info(resident_info,output_counter,1); //magic number 1 means to output everything
output_counter=0;
}
}
print_mutatable_parameters(resident,1);
}
#endif
#if PHENOTYPE
void show_phenotype(Genotype *resident, Mutation *mut_record, Selection *selection, int init_mRNA[MAX_GENES], float init_protein[MAX_GENES], RngStream RS_parallel[N_THREADS])
{
/*sampling parameters of network motifs*/
if(SAMPLE_PARAMETERS)
sample_motifs(resident, mut_record, selection->MAX_STEPS, RS_parallel[0]);
/*replay mutations, output N_motifs.txt and networks.txt*/
if(REPRODUCE_GENOTYPES || SAMPLE_GENE_EXPRESSION)
{
replay_mutations(resident, mut_record, selection->MAX_STEPS);
/*output the evolved genotype*/
calc_all_binding_sites(resident);
print_mutatable_parameters(resident,1);
summarize_binding_sites(resident,selection->MAX_STEPS);
}
if(SAMPLE_GENE_EXPRESSION)
{
/*create threads*/
omp_set_num_threads(N_THREADS);
/*collection interval is 1 minute by default*/
selection->env1.t_development=91.1; //to make 90 data points
selection->env2.t_development=91.1;
calc_avg_fitness(resident, selection, init_mRNA, init_protein, RS_parallel, NULL, NULL);
}
}
/*randomly sampling motifs*/
static void sample_motifs(Genotype *genotype_ori,
Mutation *mut_record,
int max_step,
RngStream RS)
{
int i,N_samples,which_step;
Genotype genotype_copy1, genotype_copy2;
initialize_cache(&genotype_copy1);
initialize_cache(&genotype_copy2);
FILE *fp;
int buffer_int;
float buffer_float;
char buffer_char;
char buffer_string[3];
/*load mutation record*/
fp=fopen(mutation_file,"r");
if(fp!=NULL)
printf("LOAD MUTATION RECORD SUCCESSFUL!\n");
else
{
printf("Loading mutation record failed! Quit program!");
#if MAKE_LOG
LOG("Loading mutation record failed!");
#endif
exit(-2);
}
/*Don't modify genotype_ori because it will be used elsewhere*/
clone_genotype(genotype_ori,&genotype_copy1);
/*Only the last x steps will be repeated sampled. Let's keep a copy of
*the genotype at the last (x-1)th step, so re-sampling can alway start here.
*/
for(i=0;i<START_STEP_OF_SAMPLING-1;i++)
{
fscanf(fp,"%c %d %d %s %d %a\n",&(mut_record->mut_type),
&(mut_record->which_gene),
&(mut_record->which_nucleotide),
mut_record->nuc_diff,
&(mut_record->kinetic_type),
&(mut_record->kinetic_diff));
reproduce_mutate(&genotype_copy1,mut_record);
}
/*close mutation_file*/
fclose(fp);
/*reset N_samples*/
N_samples=0;
/*sampling repeatedly*/
while(N_samples<SAMPLE_SIZE)
{
/*Keep the genotype at the last (x-1)th step intact*/
clone_genotype(&genotype_copy1,&genotype_copy2);
which_step=RngStream_RandInt(RS,1,max_step-START_STEP_OF_SAMPLING+1);
/*open mutation record and skip the entries before start_step*/
fp=fopen(mutation_file,"r");
for(i=0;i<START_STEP_OF_SAMPLING-1;i++)
fscanf(fp,"%c %d %d %s %d %a\n",&buffer_char,&buffer_int,&buffer_int,&(buffer_string[0]),&buffer_int, &buffer_float);
/*reproduce the genotype at which_step*/
for(i=0;i<which_step;i++)
{
fscanf(fp,"%c %d %d %s %d %a\n",&(mut_record->mut_type),
&(mut_record->which_gene),
&(mut_record->which_nucleotide),
mut_record->nuc_diff,
&(mut_record->kinetic_type),
&(mut_record->kinetic_diff));
reproduce_mutate(&genotype_copy2,mut_record);
}
/*score motifs*/
calc_all_binding_sites(&genotype_copy2);
find_motifs(&genotype_copy2);
/*does the target motif exist*/
switch(TARGET_MOTIF)
{
case 0:
sample_parameters(&genotype_copy2,i,RS);
N_samples++;
break;
case 1:
if(genotype_copy2.N_motifs[5]!=0)
{
sample_parameters(&genotype_copy2,i,RS);
N_samples++;
}
break;
case 2:
if(genotype_copy2.N_motifs[14]!=0)
{
sample_parameters(&genotype_copy2,i,RS);
N_samples++;
}
break;
case 3:
if(genotype_copy2.N_motifs[32]!=0)
{
sample_parameters(&genotype_copy2,i,RS);
N_samples++;
}
}
/*close mutation record*/
fclose(fp);
}
printf("Sampling parameters successfully!\n");
}
void mark_genes_for_sampling(Genotype *genotype, int effector_gene_id, int fast_TF, int slow_TF, char which_motif)
{
int mark_genes;
/*set the flag to 0*/
mark_genes=0;
/*raise the flag if which_motif is the target motif*/
switch(which_motif)
{
case 'D':
if(TARGET_MOTIF==1)
mark_genes=1;
break;
case 'C':
if(TARGET_MOTIF==2)
mark_genes=1;
break;
case 'I':
if(TARGET_MOTIF==3)
mark_genes=1;
}
/*mark genes*/
if(mark_genes)
{
genotype->cis_target_to_be_perturbed[effector_gene_id]=YES;
genotype->trans_target_to_be_perturbed[effector_gene_id][fast_TF]=YES;
genotype->slow_TF[effector_gene_id][slow_TF]=YES;
}
}
/*output parameters of network motifs*/
void sample_parameters(Genotype *genotype, int step, RngStream RS)
{
int gene_id,protein_id,cluster_id,effector_gene_id;
FILE *fp;
/*make output file*/
fp=fopen("parameters.txt","a+");
/*output the Kd of the signal*/
fprintf(fp,"%d 0 0.0 0.0 0.0 0.0 0.0 %f\n",step,log10(genotype->Kd[0]));
/*sampling an effector gene*/
while(1)
{
gene_id=RngStream_RandInt(RS,1,genotype->ngenes-1);
/*if sample a neutral network*/
if(TARGET_MOTIF==0)
{
/*then just pick an effector gene*/
if(genotype->which_protein[gene_id]==genotype->nproteins-1)
break;
}
else
{
cluster_id=genotype->which_cluster[gene_id];
/*otherwise, need to pick an effector gene that's in the target motif*/
if(genotype->cis_target_to_be_perturbed[genotype->cisreg_cluster[cluster_id][0]]==1)
break;
}
}
fprintf(fp,"%d -1 %f %f %f %f %f 0.0\n",step,log10(genotype->active_to_intermediate_rate[gene_id]),
log10(genotype->mRNA_decay_rate[gene_id]),
log10(genotype->translation_rate[gene_id]),
log10(genotype->protein_decay_rate[gene_id]),
(float)genotype->locus_length[gene_id]);
/*sample a fast-TF gene from the motif that contain the chosen effector gene*/
effector_gene_id=genotype->cisreg_cluster[cluster_id][0];
if(TARGET_MOTIF!=1) //under direct regulation, the signal is the fast TF
{
while(1)
{
gene_id=RngStream_RandInt(RS,1,genotype->ngenes-1);
protein_id=genotype->which_protein[gene_id];
/*if sample a neutral network*/
if(TARGET_MOTIF==0)
{
/*then just pick a TF protein*/
if(protein_id!=genotype->nproteins-1)
break;
}
else
{ /*otherwise need to pick a TF that's in the target motif*/
if(genotype->trans_target_to_be_perturbed[effector_gene_id][protein_id]==1)
break;
}
}
}
else
{
gene_id=N_SIGNAL_TF-1;
protein_id=N_SIGNAL_TF-1;
}
fprintf(fp,"%d 1 %f %f %f %f %f %f\n",step,log10(genotype->active_to_intermediate_rate[gene_id]),
log10(genotype->mRNA_decay_rate[gene_id]),
log10(genotype->translation_rate[gene_id]),
log10(genotype->protein_decay_rate[gene_id]),
(float)genotype->locus_length[gene_id],
log10(genotype->Kd[protein_id]));
/*sample a slow-TF gene*/
if(TARGET_MOTIF!=0) //no need to sample another TF in a neutral network,
{
while(1)
{
gene_id=RngStream_RandInt(RS,1,genotype->ngenes-1);
protein_id=genotype->which_protein[gene_id];
if(genotype->slow_TF[effector_gene_id][protein_id]==1)
break;
}
}
fprintf(fp,"%d 2 %f %f %f %f %f %f\n",step,log10(genotype->active_to_intermediate_rate[gene_id]),
log10(genotype->mRNA_decay_rate[gene_id]),
log10(genotype->translation_rate[gene_id]),
log10(genotype->protein_decay_rate[gene_id]),
(float)genotype->locus_length[gene_id],
log10(genotype->Kd[protein_id]));
fclose(fp);
}
#endif
#if PERTURB
void perturbation_analysis(Genotype *resident,
Mutation *mut_record,
Selection *selection,
int init_mRNA[MAX_GENES],
float init_protein[MAX_PROTEINS],
RngStream RS_parallel[N_THREADS])
{
int i,j,k;
char buffer[600],char_buffer;
int int_buffer,step;
float float_buffer, mean_overall_fitness, mean_fitness1, mean_fitness2, se_overall_fitness, se_fitness1, se_fitness2;
float fitness1[HI_RESOLUTION_RECALC][N_REPLICATES],fitness2[HI_RESOLUTION_RECALC][N_REPLICATES];
FILE *file_mutation,*fitness_record,*f_aft_perturbation,*f_bf_perturbation;
/*load mutation record*/
file_mutation=fopen(mutation_file,"r");
if(file_mutation!=NULL)
printf("LOAD MUTATION RECORD SUCCESSFUL!\n");
else
{
printf("Loading mutation record failed! Quit program!");
#if MAKE_LOG
LOG("Loading mutation record failed!");
#endif
exit(-2);
}
/*skip first 2 rows of fitness_record*/
fitness_record=fopen(evo_summary,"r");
fgets(buffer,600,fitness_record);
fgets(buffer,600,fitness_record);
/*create threads*/
omp_set_num_threads(N_THREADS);
/*begin*/
for(i=1;i<=selection->MAX_STEPS;i++)
{
fscanf(file_mutation,"%c %d %d %s %d %a\n",&(mut_record->mut_type),
&(mut_record->which_gene),
&(mut_record->which_nucleotide),
mut_record->nuc_diff,
&(mut_record->kinetic_type),
&(mut_record->kinetic_diff));
reproduce_mutate(resident,mut_record);
fscanf(fitness_record,"%d %d %d %d %c %f %f %f %f %f %f %f %d %d %d %d\n",
&step,
&int_buffer,
&int_buffer,
&int_buffer,
&char_buffer,
&float_buffer,
&mean_overall_fitness,
&mean_fitness1,
&mean_fitness2,
&se_overall_fitness,
&se_fitness1,
&se_fitness2,
&int_buffer,
&int_buffer,
&int_buffer,
&int_buffer);
if(i>=START_STEP_OF_PERTURBATION)
{
calc_all_binding_sites(resident);
find_motifs(resident);
#if DIRECT_REG
if(resident->N_motifs[5]!=0 && resident->N_motifs[5]==resident->N_motifs[0])
#else
#if WHICH_MOTIF==0 // disturb C1-FFL
if(resident->N_motifs[14]!=0 &&
resident->N_motifs[18]==0 &&
resident->N_motifs[14]==resident->N_motifs[9] &&
resident->N_motifs[27]==0 )
#elif WHICH_MOTIF==1 // disturb FFL-in-diamond
if(resident->N_motifs[23]!=0 &&
resident->N_motifs[9]==0 &&
resident->N_motifs[23]==resident->N_motifs[18] &&
resident->N_motifs[27]==0 )
#else // disturb diamond
if(resident->N_motifs[32]!=0 &&
resident->N_motifs[9]==0 &&
resident->N_motifs[27]==resident->N_motifs[32] &&
resident->N_motifs[18]==0 )
#endif
#endif
{
for(j=0;j<HI_RESOLUTION_RECALC;j++)
calc_avg_fitness(resident, selection, init_mRNA, init_protein, RS_parallel, fitness1[j], fitness2[j]);
calc_fitness_stats(resident, selection, &(fitness1[0]), &(fitness2[0]), HI_RESOLUTION_RECALC);
f_aft_perturbation=fopen("f_aft_perturbation.txt","a+");
fprintf(f_aft_perturbation,"%d %.10f %.10f %.10f %.10f %.10f %.10f\n",i,
resident->avg_fitness,
resident->fitness1,
resident->fitness2,
resident->SE_avg_fitness,
resident->SE_fitness1,
resident->SE_fitness2);
fclose(f_aft_perturbation);
/*file to store the original fitness of the to-be-modified network*/
f_bf_perturbation=fopen("f_bf_perturbation.txt","a+");
fprintf(f_bf_perturbation,"%d %.10f %.10f %.10f %.10f %.10f %.10f\n",
step,
mean_overall_fitness,
mean_fitness1,
mean_fitness2,
se_overall_fitness,
se_fitness1,
se_fitness2);
fclose(f_bf_perturbation);
}
for(j=0;j<MAX_GENES;j++)
{
resident->cis_target_to_be_perturbed[j]=0;
for(k=0;k<MAX_PROTEINS;k++)
resident->trans_target_to_be_perturbed[j][k]=0;
}
}
}
fclose(file_mutation);
fclose(fitness_record);
}
#endif
char set_base_pair(float x)
{
char base;
if (x<0.25)
base = 'a';
else if (x<0.5)
base = 'c';
else if (x<0.75)
base = 'g';
else
base = 't';
return base;
}
/*
* initialize the genotype, this initializes random cis-regulatory
* sequences for each individual, etc. (full list below)
*/
void initialize_genotype(Genotype *genotype, int init_TF_genes, int init_N_act, int init_N_rep, int init_effector_genes, RngStream RS)
{
int i,k;
genotype->ngenes=init_effector_genes+N_SIGNAL_TF+init_TF_genes; /*including the signal genes and 1 selection gene*/
genotype->ntfgenes=N_SIGNAL_TF+init_TF_genes; /*including the signal genes*/
genotype->nproteins=genotype->ngenes; /*at initialization, each protein is encoded by one copy of gene*/
genotype->nTF_families=genotype->nproteins-1;
/*at initialization, each copy of gene should have a unique cis-regulatory sequence*/
for(i=0;i<genotype->ngenes;i++)
{
genotype->which_cluster[i]=i;
genotype->cisreg_cluster[i][0]=i;
}
/* initially, each protein has only one copy of gene*/
for(i=0;i<genotype->nproteins;i++)
{
genotype->protein_pool[i][0][0]=1;
genotype->protein_pool[i][1][0]=i;
genotype->which_protein[i]=i;
}
for(i=0;i<genotype->nTF_families;i++)
{
genotype->which_TF_family[i]=i;
genotype->TF_family_pool[i][0][0]=1;
genotype->TF_family_pool[i][1][0]=i;
}
initialize_sequence((char *)genotype->cisreg_seq, CISREG_LEN*MAX_GENES, genotype->ngenes, RS); // initialize cis-reg sequence
initialize_sequence((char *)genotype->tf_seq, CONSENSUS_SEQ_LEN*MAX_TF_GENES, genotype->ntfgenes, RS); //initialize binding sequence of TFs
/* We now generate the complementary sequence of BS that are on the non-template strand.
* The complementary sequence is used to search for BS that on the non-template strand.
* We also assume that all the TFs can work on both strands, but can induce expression in one direction.*/
for(i=0;i< genotype->ntfgenes;i++)
{
for(k=0;k<CONSENSUS_SEQ_LEN;k++)
{
switch (genotype->tf_seq[i][CONSENSUS_SEQ_LEN-k-1])
{
case 'a': genotype->tf_seq_rc[i][k]='t'; break;
case 't': genotype->tf_seq_rc[i][k]='a'; break;
case 'c': genotype->tf_seq_rc[i][k]='g'; break;
case 'g': genotype->tf_seq_rc[i][k]='c'; break;
}
}
}
initialize_genotype_fixed(genotype, init_N_act, init_N_rep, init_effector_genes, RS);
calc_all_binding_sites(genotype);
}
/*****************************************************************************
*
* Private functions
*
****************************************************************************/
static void initialize_sequence(char *Seq, int len, int num_elements, RngStream RS)
{
float x;
int i;
int current_element = len/num_elements;
int pos_n;
for (i=0; i<len; i++)
{
pos_n = (i / current_element)*current_element + i % current_element;
x = RngStream_RandU01(RS);
Seq[pos_n] = set_base_pair(x);
}
}
/*This function initialize kinetic constants for gene expression, as well the identities of TFs*/
static void initialize_genotype_fixed(Genotype *genotype, int init_N_act, int init_N_rep, int init_effector_genes, RngStream RS)
{
int i;
/* the first N_SIGNAL_TF genes encode the sensor TFs. The concentration of a sensor TF
* is determined by certain environmental signal*/
genotype->total_loci_length=0.0;
for(i=N_SIGNAL_TF; i < genotype->ngenes; i++)
{
#if RANDOM_COOPERATION_LOGIC
genotype->min_act_to_transc[i]=RngStream_RandInt(RS,1,2); //if one activator is sufficient to induce expression, the gene is regualted by OR gate.
#else
genotype->min_N_activator_to_transc[i]=1;
genotype->min_N_activator_to_transc[genotype->ngenes-1]=2;
#endif
/* tf affinity */
genotype->Kd[i]=pow(10.0,(log_MAX_Kd-log_MIN_Kd)*RngStream_RandU01(RS)+log_MIN_Kd);
/* mRNA decay */
genotype->mRNA_decay_rate[i] = pow(10.0,SD_MRNA_DECAY_RATE*gasdev(RS)+MEAN_MRNA_DECAY_RATE);
if(genotype->mRNA_decay_rate[i]>MAX_MRNA_DECAY)
genotype->mRNA_decay_rate[i]=MAX_MRNA_DECAY;
if(genotype->mRNA_decay_rate[i]<MIN_MRNA_DECAY)
genotype->mRNA_decay_rate[i]=MIN_MRNA_DECAY;
/* protein decay */
genotype->protein_decay_rate[i] = pow(10.0,SD_PROTEIN_DECAY_RATE*gasdev(RS)+MEAN_PROTEIN_DECAY_RATE);
if(genotype->protein_decay_rate[i]>MAX_PROTEIN_DECAY)
genotype->protein_decay_rate[i]=MAX_PROTEIN_DECAY;
if(genotype->protein_decay_rate[i]<MIN_PROTEIN_DECAY)
genotype->protein_decay_rate[i]=MIN_PROTEIN_DECAY;
/* translation rate */
genotype->translation_rate[i] = pow(10.0,SD_PROTEIN_SYN_RATE*gasdev(RS)+MEAN_PROTEIN_SYN_RATE);
if(genotype->translation_rate[i]>MAX_PROTEIN_SYN_RATE)
genotype->translation_rate[i]=MAX_PROTEIN_SYN_RATE;
if(genotype->translation_rate[i]<MIN_PROTEIN_SYN_RATE)
genotype->translation_rate[i]=MIN_PROTEIN_SYN_RATE;
/*ACT to INT rate*/
genotype->active_to_intermediate_rate[i]=pow(10.0,SD_ACT_TO_INT_RATE*gasdev(RS)+MEAN_ACT_TO_INT_RATE);
if(genotype->active_to_intermediate_rate[i]>MAX_ACT_TO_INT_RATE)
genotype->active_to_intermediate_rate[i]=MAX_ACT_TO_INT_RATE;
if(genotype->active_to_intermediate_rate[i]<MIN_ACT_TO_INT_RATE)
genotype->active_to_intermediate_rate[i]=MIN_ACT_TO_INT_RATE;
/*locus length*/
genotype->locus_length[i]=(int)round(pow(10.0,SD_GENE_LENGTH*gasdev(RS)+MEAN_GENE_LENGTH));
if(genotype->locus_length[i]>MAX_GENE_LENGTH)
genotype->locus_length[i]=MAX_GENE_LENGTH;
if(genotype->locus_length[i]<MIN_GENE_LENGTH)
genotype->locus_length[i]=MIN_GENE_LENGTH;
genotype->total_loci_length+=genotype->locus_length[i];
}
/* assign tf identity*/
genotype->N_act=0;
genotype->N_rep=0;
if(init_N_rep==-1 && init_N_act==-1) /*randomly generate activators and repressors*/
{
for(i=N_SIGNAL_TF;i<genotype->ntfgenes;i++)
{
if (RngStream_RandU01(RS)<PROB_ACTIVATING)
{
genotype->N_act++;
genotype->protein_identity[i] = ACTIVATOR;
}
else
{
genotype->N_rep++;
genotype->protein_identity[i]= REPRESSOR;
}
}
}
else
{
genotype->N_act=init_N_act;
genotype->N_rep=init_N_rep;
for(i=N_SIGNAL_TF;i<N_SIGNAL_TF+init_N_act;i++)
genotype->protein_identity[i]=ACTIVATOR;
for(i=N_SIGNAL_TF+init_N_act;i<genotype->ntfgenes;i++)
genotype->protein_identity[i]=REPRESSOR;
}
/* parameterize sensor TF*/
for(i=0;i<N_SIGNAL_TF;i++)
{
genotype->mRNA_decay_rate[i]=0.0; // we assume environmental signal toggles the state of sensor TF between active and inactive
genotype->protein_decay_rate[i]=0.0; // the concentration of sensor TF is constant.
genotype->translation_rate[i]=0.0;
genotype->active_to_intermediate_rate[i]=0.0;
genotype->protein_identity[i]=ACTIVATOR; /*make sensor TF an activator*/
genotype->N_act++;
genotype->Kd[i]=pow(10.0,(log_MAX_Kd-log_MIN_Kd)*RngStream_RandU01(RS)+log_MIN_Kd);
}
#if RANDOMIZE_SIGNAL2
#if N_SIGNAL_TF==2
if(RngStream_RandU01(RS)<=0.5) // we assume there is a background "on" signal, which is sensor TF 0, in the network.
genotype->protein_identity[1]=ACTIVATOR; // Other sensor TFs can be either activators or repressors.
else
{
genotype->protein_identity[1]=REPRESSOR;
genotype->N_act--;
genotype->N_rep++;
}
#endif
#endif
}
/*
* compute the list binding sites for specified gene and gene copy
*/
void calc_all_binding_sites_copy(Genotype *genotype, int gene_id)
{
int i, j, k;
int match,match_rc; // number of nucleotide that matches the binding sequence of TF, in a binding site in the coding and in the non-coding strand.
int N_hindered_BS=0;
int N_binding_sites=0;
int start_TF;
genotype->N_act_BS[gene_id]=0;
genotype->N_rep_BS[gene_id]=0;
genotype->max_hindered_sites[gene_id]=0;
//some helper pointer
char *tf_seq;
char *cis_seq;
char *tf_seq_rc;
cis_seq=&(genotype->cisreg_seq[gene_id][0]);
for(i=3; i < CISREG_LEN-CONSENSUS_SEQ_LEN-3; i++) /* scan promoter */
{
/*calc the number of BS within the hindrance range*/
N_hindered_BS=0;
if(N_binding_sites>0)
{
for(j=0;j<N_binding_sites;j++)
{
if(genotype->all_binding_sites[gene_id][j].BS_pos> i-CONSENSUS_SEQ_LEN-2*HIND_LENGTH)
N_hindered_BS++;
}
}
/* loop through TF proteins */
#if !DIRECT_REG
if(genotype->which_protein[gene_id]==genotype->nproteins-1) // if the gene is an effector gene
start_TF=N_SIGNAL_TF;// the environmental signals cannot directly regulate the selection gene
else
start_TF=0;
#else
start_TF=0;
#endif
for (k=start_TF; k < genotype->nproteins-1; k++)
{
tf_seq=&(genotype->tf_seq[k][0]);
tf_seq_rc=&(genotype->tf_seq_rc[k][0]);
/*find BS on the template strand*/
match=0;
for (j=i; j < i+CONSENSUS_SEQ_LEN; j++) /*calculate the number of nucleotides that match in each [i,i+CONSENSUS_SEQ_LEN] window. The window slides by 1 each time when scanning the promoter*/
if (cis_seq[j] == tf_seq[j-i]) match++;
if (match >= NMIN)
{
if (N_binding_sites + 1 >= genotype->N_allocated_elements)
{
while(genotype->N_allocated_elements<=N_binding_sites+1)
genotype->N_allocated_elements+=100;
for(j=0;j<MAX_GENES;j++)
{
genotype->all_binding_sites[j] = realloc(genotype->all_binding_sites[j], genotype->N_allocated_elements*sizeof(AllTFBindingSites));
if(!genotype->all_binding_sites[j])
{
#if MAKE_LOG
LOG("error in calc_all_binding_sites_copy\n");
#endif
exit(-1);
}
}
}
genotype->all_binding_sites[gene_id][N_binding_sites].tf_id = k;
genotype->all_binding_sites[gene_id][N_binding_sites].Kd=KD2APP_KD*genotype->Kd[k]*pow(NS_Kd/genotype->Kd[k],(float)(CONSENSUS_SEQ_LEN-match)/(CONSENSUS_SEQ_LEN-NMIN+1));
genotype->all_binding_sites[gene_id][N_binding_sites].BS_pos = i ;
genotype->all_binding_sites[gene_id][N_binding_sites].mis_match = CONSENSUS_SEQ_LEN-match;
genotype->all_binding_sites[gene_id][N_binding_sites].N_hindered = N_hindered_BS;
N_hindered_BS++;
N_binding_sites++;
if(genotype->protein_identity[k]==ACTIVATOR) genotype->N_act_BS[gene_id]++;
}
else /*find BS on the non-template strand.*/
{
match_rc=0;
for (j=i; j < i+CONSENSUS_SEQ_LEN; j++)
if (cis_seq[j] == tf_seq_rc[j-i]) match_rc++;
if (match_rc >= NMIN)
{
/**********************************************************************/
if (N_binding_sites + 1 >= genotype->N_allocated_elements)
{
while(genotype->N_allocated_elements<=N_binding_sites+1)
genotype->N_allocated_elements+=100;
for(j=0;j<MAX_GENES;j++)
{
genotype->all_binding_sites[j] = realloc(genotype->all_binding_sites[j], genotype->N_allocated_elements*sizeof(AllTFBindingSites));
if(!genotype->all_binding_sites[j])
{
#if MAKE_LOG
LOG("error in calc_all_binding_sites_copy\n");
#endif
exit(-1);