-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcellular_activity.c
1348 lines (1262 loc) · 59.9 KB
/
cellular_activity.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 the functions to simulate gene expression and
* instantaneous fitness during development.
*
* 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 <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "cellular_activity.h"
#include "lib.h"
#include "numerical.h"
#define DO_NOTHING -2
#define INITIALIZATION -1
#define SUDDEN_SIGNAL_CHANGE 1
/*expression rate parameters*/
static const float TRANSCRIPTINIT=6.75;
static const float TRANSLATION_INITIATION_TIME=0.5; //min
static const float TRANSCRIPTION_TERMINATION_TIME=1.0; //min
static const float TRANSCRIPTION_ELONGATION_RATE=600.0; //codon/min
static const float TRANSLATION_ELONGATION_RATE=330.0; //codon/min
static const float MAX_REP_TO_INT_RATE=0.92;
static const float BASAL_REP_TO_INT_RATE=0.15;
static const float MAX_INT_TO_REP_RATE=4.11;
static const float BASAL_INT_TO_REP_RATE=0.67;
static const float MAX_INT_TO_ACT_RATE=3.3;
static const float BASAL_INT_TO_ACT_RATE=0.025;
static const float DEFAULT_UPDATE_INTERVAL=10.0; /*min*/
static const float MAX_TOLERABLE_CHANGE_IN_PROBABILITY_OF_BINDING=0.01;
/*fitness*/
static const float Ne_saturate = 10000.0;
static const float c_transl=2.0e-6;
static const float bmax=1.0;
/******************************************************************************
*
* Private function prototypes
*
*****************************************************************************/
static float calc_tprime(Genotype*, CellState*, float*, float, float, int);
static float calc_integral(Genotype *, CellState *, float *, float, float);
static void calc_fx_dfx(float, int, float, float*, float*, float*, float*, float*);
static void calc_leaping_interval(Genotype*, CellState*, float *, float, int);
static void calc_TF_dist_from_all_BS(Genotype *, CellState*, int);
static int Gillespie_event_mRNA_decay(GillespieRates *, CellState *, Genotype *, RngStream);
static void Gillespie_event_repressed_to_intermediate(GillespieRates *, CellState *, Genotype *, RngStream);
static void Gillespie_event_intermediate_to_repressed(GillespieRates *, CellState *, Genotype *, RngStream);
static void Gillespie_event_intermediate_to_active(GillespieRates *, CellState *, Genotype *, RngStream);
static void Gillespie_event_active_to_intermediate(Genotype *, CellState *, GillespieRates *, RngStream);
static void Gillespie_event_transcription_init(GillespieRates *, CellState *, Genotype *, float, RngStream);
static int fixed_event_end_translation_init(Genotype *, CellState *, GillespieRates *, float *);
static void fixed_event_end_transcription(float *, CellState *, GillespieRates *, Genotype *);
static int does_fixed_event_end(CellState*, float);
static int do_fixed_event(Genotype *, CellState *, GillespieRates *, Environment *, Phenotype *, float *, int);
static float calc_fitness(float *, Genotype *, CellState *, float*, float);
static void update_protein_number_and_fitness(Genotype *, CellState *, GillespieRates *, float);
static int do_Gillespie_event(Genotype*, CellState *, GillespieRates *, float, RngStream);
/******************************************************************************
*
* Global functions
*
*****************************************************************************/
/*
* initialize the cell state with the specified initial protein
* concentration, mean mRNA number and mRNA decay
*/
void initialize_cell( Genotype *genotype,
CellState *state,
Environment *env,
float t_burn_in,
int init_mRNA_number[MAX_GENES],
float init_protein_number[MAX_PROTEINS])
{
int i, j;
state->t=0.0;
state->cumulative_fitness = 0.0;
state->cumulative_fitness_after_burn_in = 0.0;
state->instantaneous_fitness = 0.0;
state->effect_of_effector=env->initial_effect_of_effector;
/* initialize linked tables*/
state->mRNA_transcr_time_end_head = NULL;
state->mRNA_transcr_time_end_tail = NULL;
state->mRNA_transl_init_time_end_head = NULL;
state->mRNA_transl_init_time_end_tail = NULL;
state->signal_off_head = NULL;
state->signal_off_tail = NULL;
state->signal_on_head = NULL;
state->signal_on_tail = NULL;
state->burn_in_growth_rate_head =NULL;
state->burn_in_growth_rate_tail=NULL;
state->sampling_point_end_head=NULL;
state->sampling_point_end_tail=NULL;
state->last_event_t=0.0;
state->change_signal_strength_head=NULL;
state->change_signal_strength_tail=NULL;
state->t_to_update_probability_of_binding=TIME_INFINITY;
state->cell_activated=0;
/*initialize gene state, mRNA number*/
for (i=N_SIGNAL_TF; i < genotype->ngenes; i++)
{
state->transcriptional_state[i]=REPRESSED;
state->mRNA_aft_transl_delay_num[i]=init_mRNA_number[i];
state->mRNA_under_transl_delay_num[i]=0;
state->mRNA_under_transc_num[i]=0;
state->last_P_A[i]=0.0;
state->last_P_R[i]=0.0;
state->last_P_A_no_R[i]=0.0;;
state->last_P_NotA_no_R[i]=0.0;
state->protein_synthesis_index[i]=(float)state->mRNA_aft_transl_delay_num[i]*genotype->translation_rate[i]/genotype->protein_decay_rate[i];
}
/* initiate protein concentration*/
for (i=N_SIGNAL_TF; i < genotype->ngenes; i++)
state->gene_specific_protein_number[i] = init_protein_number[i];
for(i=N_SIGNAL_TF;i<genotype->nproteins;i++)
{
state->protein_number[i]=0.0;
for(j=0;j<genotype->protein_pool[i][0][0];j++)
state->protein_number[i]+=state->gene_specific_protein_number[genotype->protein_pool[i][1][j]];
}
/* deal with the sensor tf*/
for(i=0;i<N_SIGNAL_TF;i++)
{
state->mRNA_aft_transl_delay_num[i]=0;
state->mRNA_under_transc_num[i]=0;
state->mRNA_under_transl_delay_num[i]=0;
state->gene_specific_protein_number[i]=0.0;
}
/*mark when to start calculating average fitness*/
if(t_burn_in!=0.0)
add_fixed_event(-1,t_burn_in,&(state->burn_in_growth_rate_head),&(state->burn_in_growth_rate_tail));
else
add_fixed_event(-1,(float)TIME_INFINITY,&(state->burn_in_growth_rate_head),&(state->burn_in_growth_rate_tail));
/*plot protein concentration and fitness vs time*/
#if PHENOTYPE
float t;
int N_data_points;
t=0.0+TIME_OFFSET;
N_data_points=(int)(env->t_development+t_burn_in);
for(i=0;i<N_data_points;i++)
{
add_fixed_event(-1,t,&(state->sampling_point_end_head),&(state->sampling_point_end_tail)); //get a timepoint each minute
t+=1.0;
}
#endif
}
/*
* Calculate the rates of all Gillespie events
*/
void calc_all_rates(Genotype *genotype,
CellState *state,
GillespieRates *rates,
Environment *env,
Phenotype *phenotype,
float t_burn_in,
int UPDATE_WHAT)
{
int i,cluster_id,gene_id;
int concurrent;
float t_to_update_probability_of_binding,interval_to_update_probability_of_binding;
float diff_PA,diff_PR,diff_PnotAnoR,diff_PAnoR,diff_max;
/* reset rates */
rates->total_mRNA_decay_rate=0.0;
rates->total_active_to_intermediate_rate=0.0;
rates->total_repressed_to_intermediate_rate=0.0;
rates->total_intermediate_to_repressed_rate=0.0;
rates->total_N_gene_transcript_initiated=0;
rates->total_intermediate_to_active_rate=0.0;
rates->total_Gillespie_rate=0.0;
for(i=0;i<genotype->ngenes;i++)
{
rates->repressed_to_intermediate_rate[i]=0.0;
rates->intermediate_to_repressed_rate[i]=0.0;
rates->intermediate_to_active_rate[i]=0.0;
rates->active_to_intermediate_rate[i]=0.0;
rates->mRNA_decay_rate[i]=0.0;
rates->transcript_initiation_state[i]=0;
state->P_A[i]=0.0;
state->P_R[i]=0.0;
state->P_A_no_R[i]=0.0;
state->P_NotA_no_R[i]=0.0;
}
/* update mRNA decay rates*/
for(i=N_SIGNAL_TF;i<genotype->ngenes;i++)
{
rates->mRNA_decay_rate[i] = genotype->mRNA_decay_rate[i] * (state->mRNA_aft_transl_delay_num[i] + state->mRNA_under_transl_delay_num[i]);
rates->total_mRNA_decay_rate += rates->mRNA_decay_rate[i];
}
/*update probability of binding configurations that activates expression
* and use it to update other rates*/
for(i=N_SIGNAL_TF; i < genotype->ngenes; i++)
{
cluster_id=genotype->which_cluster[i];
if(genotype->cisreg_cluster[cluster_id][0]!=i) /*if this gene does not have a unique cis-reg sequence*/
{
state->P_A[i]=state->P_A[genotype->cisreg_cluster[cluster_id][0]]; /* copy TF distribution from elsewhere*/
state->P_R[i]=state->P_R[genotype->cisreg_cluster[cluster_id][0]];
state->P_A_no_R[i]=state->P_A_no_R[genotype->cisreg_cluster[cluster_id][0]];
state->P_NotA_no_R[i]=state->P_NotA_no_R[genotype->cisreg_cluster[cluster_id][0]];
}
else /* otherwise, we need to calc the ratio*/
{
if(genotype->N_act_BS[i]!=0 || genotype->N_rep_BS[i]!=0)
calc_TF_dist_from_all_BS(genotype, state, i);
else
{
state->P_A[i]=0.0;
state->P_R[i]=0.0;
state->P_A_no_R[i] = 0.0;
state->P_NotA_no_R[i]=0.0;
}
}
/* calc other rates*/
switch (state->transcriptional_state[i])
{
case REPRESSED:
rates->repressed_to_intermediate_rate[i]=state->P_A[i]*(MAX_REP_TO_INT_RATE-BASAL_REP_TO_INT_RATE)+BASAL_REP_TO_INT_RATE;
rates->total_repressed_to_intermediate_rate+=rates->repressed_to_intermediate_rate[i];
rates->intermediate_to_repressed_rate[i]=0.0;
rates->intermediate_to_active_rate[i]=0.0;
rates->active_to_intermediate_rate[i]=0.0;
break;
case INTERMEDIATE:
rates->intermediate_to_repressed_rate[i]=state->P_R[i]*(MAX_INT_TO_REP_RATE-BASAL_INT_TO_REP_RATE)+BASAL_INT_TO_REP_RATE;
rates->total_intermediate_to_repressed_rate+=rates->intermediate_to_repressed_rate[i];
rates->intermediate_to_active_rate[i]=MAX_INT_TO_ACT_RATE*state->P_A_no_R[i]+BASAL_INT_TO_ACT_RATE*state->P_NotA_no_R[i];
rates->total_intermediate_to_active_rate+=rates->intermediate_to_active_rate[i];
rates->active_to_intermediate_rate[i]=0.0;
rates->repressed_to_intermediate_rate[i]=0.0;
break;
case ACTIVE:
rates->active_to_intermediate_rate[i]=genotype->active_to_intermediate_rate[i];
rates->total_active_to_intermediate_rate+=rates->active_to_intermediate_rate[i];
rates->transcript_initiation_state[i]= 1;
rates->total_N_gene_transcript_initiated+=1;
rates->intermediate_to_repressed_rate[i]=0.0;
rates->repressed_to_intermediate_rate[i]=0.0;
rates->intermediate_to_active_rate[i]=0.0;
break;
}
}
rates->total_Gillespie_rate+=rates->total_intermediate_to_repressed_rate;
rates->total_Gillespie_rate+=rates->total_intermediate_to_active_rate;
rates->total_Gillespie_rate+=rates->total_repressed_to_intermediate_rate;
rates->total_Gillespie_rate+=rates->total_mRNA_decay_rate;
rates->total_Gillespie_rate+=rates->total_active_to_intermediate_rate;
rates->total_Gillespie_rate+=(float)rates->total_N_gene_transcript_initiated*TRANSCRIPTINIT;
/*Check if Pact needs to be updated more or less often*/
if(UPDATE_WHAT!=INITIALIZATION && state->cell_activated==1)
{
diff_max=0.0;
cluster_id=1;
while(genotype->cisreg_cluster[cluster_id][0]!=NA) //check if Pact changes too much
{
gene_id=genotype->cisreg_cluster[cluster_id][0];
diff_PA=fabs(state->P_A[gene_id]-state->last_P_A[gene_id]);
diff_PAnoR=fabs(state->P_A_no_R[gene_id]-state->last_P_A_no_R[gene_id]);
diff_PnotAnoR=fabs(state->P_NotA_no_R[gene_id]-state->last_P_NotA_no_R[gene_id]);
diff_PR=fabs(state->P_R[gene_id]-state->last_P_R[gene_id]);
diff_max=(diff_max>diff_PA)?diff_max:diff_PA;
diff_max=(diff_max>diff_PR)?diff_max:diff_PR;
diff_max=(diff_max>diff_PAnoR)?diff_max:diff_PAnoR;
diff_max=(diff_max>diff_PnotAnoR)?diff_max:diff_PnotAnoR;
cluster_id++;
}
if(diff_max<EPSILON)
interval_to_update_probability_of_binding=DEFAULT_UPDATE_INTERVAL;
else
interval_to_update_probability_of_binding=MAX_TOLERABLE_CHANGE_IN_PROBABILITY_OF_BINDING/diff_max*(state->t-state->last_event_t);
#if PHENOTYPE
/*record the maximum change in probability of binding except when it is a sudden change in the signal*/
if(UPDATE_WHAT!=SUDDEN_SIGNAL_CHANGE)
phenotype->max_change_in_probability_of_binding=(diff_max>phenotype->max_change_in_probability_of_binding)?diff_max:phenotype->max_change_in_probability_of_binding;
#endif
if(UPDATE_WHAT!=DO_NOTHING)
calc_leaping_interval(genotype,state,&interval_to_update_probability_of_binding,env->t_development+t_burn_in,UPDATE_WHAT);
/*Update the next time that Pact will be updated mandatorily*/
t_to_update_probability_of_binding=state->t+interval_to_update_probability_of_binding;
concurrent=check_concurrence(state, t_to_update_probability_of_binding);
while(concurrent)//if the time to update overlaps with existing events, add a tiny offset
{
t_to_update_probability_of_binding+=TIME_OFFSET;
concurrent=check_concurrence(state, t_to_update_probability_of_binding);
}
state->t_to_update_probability_of_binding=t_to_update_probability_of_binding;
}
/*Keep a copy of Pact and time for comparison at next time Pact is updated*/
for(i=N_SIGNAL_TF;i<genotype->ngenes;i++)
{
state->last_P_A[i]=state->P_A[i];
state->last_P_R[i]=state->P_R[i];
state->last_P_A_no_R[i] = state->P_A_no_R[i];
state->last_P_NotA_no_R[i]=state->P_NotA_no_R[i];
}
state->last_event_t=state->t;
}
/*
* run the model for a specified cell for a single timestep:
*/
void do_single_timestep(Genotype *genotype,
CellState *state,
GillespieRates *rates,
Environment *env,
float t_burn_in,
Phenotype *timecourse,
RngStream RS)
{
int event, UPDATE_WHAT;
float fixed_time;
float dt;
float x;
float developmental_time=env->t_development+t_burn_in;
/* draw random number */
x = expdev(RS);
dt = x/rates->total_Gillespie_rate;
/* check if a fixed event occurs during dt, or in tdevelopment if running for a fixed development time */
fixed_time = (state->t+dt<developmental_time)?(state->t+dt):developmental_time;
event = does_fixed_event_end(state, fixed_time);
while(event!=0)
{
/*after doing fixed event, return a flag to indicate whether mandatorily update Pact or Prep*/
UPDATE_WHAT=do_fixed_event(genotype, state, rates, env, timecourse, &dt, event);
/* advance time by the dt */
state->t += dt;
/* we've been running with rates->total_Gillespie_rate for dt, so substract it from x*/
x -= dt*rates->total_Gillespie_rate;
/* update rates->total_Gillespie_rate and compute a new dt */
calc_all_rates(genotype, state, rates, env, timecourse, t_burn_in, UPDATE_WHAT);
dt = x/rates->total_Gillespie_rate;
/*deal with rounding error*/
if(dt<0.0)
{
#if MAKE_LOG // if enabled, error file can be huge, because this rounding error can happen very often
LOG("Negative dt!\n");
#endif
dt=TIME_OFFSET;
}
fixed_time = (state->t+dt<developmental_time)?(state->t+dt):developmental_time;
/* check to see there aren't more fixed events to do */
event = does_fixed_event_end(state, fixed_time);
}
/* no remaining fixed events to do in dt, now do stochastic events */
/* if we haven't already reached end of development with last delta-t*/
if (state->t+dt < developmental_time)
{
/*update protein concentration and fitness after dt*/
update_protein_number_and_fitness(genotype, state, rates, dt);
/*do Gillespie event*/
UPDATE_WHAT=do_Gillespie_event(genotype, state, rates, dt, RS);
/* Gillespie step: advance time to next event at dt */
state->t += dt;
calc_all_rates(genotype,state,rates,env, timecourse, t_burn_in, UPDATE_WHAT);
}
else
{
/* do remaining dt */
dt = developmental_time - state->t;
/* the final update of protein concentration */
update_protein_number_and_fitness(genotype, state, rates, dt);
/* advance to end of development (this exits the outer while loop) */
state->t = developmental_time;
}
}
/*****************************************************************************
*
* Private functions
*
*****************************************************************************/
/*
* compute t' factor used in the integration of fitness
* t' is the time the effector protein increases or decreases to a given amount
*/
static float calc_tprime(Genotype *genotype, CellState* state, float *number_of_selection_protein_bf_dt, float dt, float given_amount, int protein_id)
{
int n_copies;
int i;
n_copies=genotype->protein_pool[protein_id][0][0];
float protein_synthesis_rate[n_copies],protein_decay_rate[n_copies];
for(i=0;i<n_copies;i++)
{
protein_decay_rate[i]=genotype->protein_decay_rate[genotype->protein_pool[protein_id][1][i]];
protein_synthesis_rate[i]=state->protein_synthesis_index[genotype->protein_pool[protein_id][1][i]]*protein_decay_rate[i];
}
return rtsafe(&calc_fx_dfx, n_copies, given_amount, number_of_selection_protein_bf_dt, protein_synthesis_rate, protein_decay_rate, 0.0, dt);
}
/*
* calculate f(x)-Pp_s and f'(x),
* f(x) is the number of effector protein molecules at time x
*/
static void calc_fx_dfx(float x, int n_copies, float given_amount, float *intial_protein_number, float *protein_synthesis_rate, float *protein_decay_rate,float *fx, float *dfx)
{
int i;
*fx=0;
*dfx=0;
for(i=0;i<n_copies;i++)
{
*fx+=(intial_protein_number[i]-protein_synthesis_rate[i]/protein_decay_rate[i])*exp(-protein_decay_rate[i]*x)+protein_synthesis_rate[i]/protein_decay_rate[i];
*dfx+=(protein_synthesis_rate[i]-protein_decay_rate[i]*intial_protein_number[i])*exp(-protein_decay_rate[i]*x);
}
*fx-=given_amount;
}
/*
* calculate F(delta_t)/Ne_sat. F(x) is the integral of f(x) over delta_t.
* f(x) is the number of effector protein molecules at time x
*/
static float calc_integral(Genotype *genotype, CellState *state, float *initial_protein_number, float dt, float saturate_protein_number)
{
int i,n_copies,gene_ids[MAX_PROTEINS];
float integral=0.0,ect_minus_one;
n_copies=genotype->protein_pool[genotype->nproteins-1][0][0];
for(i=0;i<n_copies;i++)
gene_ids[i]=genotype->protein_pool[genotype->nproteins-1][1][i];
for(i=0;i<n_copies;i++)
{
ect_minus_one=exp(-genotype->protein_decay_rate[gene_ids[i]]*dt)-1.0;
integral+=(state->protein_synthesis_index[gene_ids[i]]*ect_minus_one/genotype->protein_decay_rate[gene_ids[i]]-
initial_protein_number[i]*ect_minus_one/genotype->protein_decay_rate[gene_ids[i]]+
state->protein_synthesis_index[gene_ids[i]]*dt);
}
return integral/saturate_protein_number;
}
/*
* return the instantaneous fitness given the current cell state and environment,
* also return the integrated fitness
*/
static float calc_fitness(float *integrated_fitness,
Genotype *genotype,
CellState *state,
float* number_of_selection_protein_bf_dt,
float dt)
{
int i;
float instantaneous_fitness=0.0; /* this is returned from the function */
float total_translation_rate = 0.0;
float dt_prime;
float cost_of_expression;
float Ne_next=state->protein_number[genotype->nproteins-1];
float Ne=0.0;
for(i=0;i<genotype->protein_pool[genotype->nproteins-1][0][0];i++)
Ne+=number_of_selection_protein_bf_dt[i];
/* compute the total cost of translation across all genes */
for(i=N_SIGNAL_TF; i < genotype->ngenes; i++)
{
total_translation_rate += (genotype->translation_rate[i]*(float)state->mRNA_aft_transl_delay_num[i]+
0.5*genotype->translation_rate[i]*(float)state->mRNA_under_transl_delay_num[i])*(float)genotype->locus_length[i]/236.0; //236 codon is the average length of yeast protein
}
cost_of_expression=total_translation_rate*c_transl;
switch (state->effect_of_effector)
{
case 'b': /* effector is beneficial!*/
if(Ne>Ne_next)//decrease in effector protein
{
if(Ne_next>=Ne_saturate) //too many effector throughout
{
*integrated_fitness =dt*(bmax-cost_of_expression);
}
else if(Ne<=Ne_saturate) // not enough effector throughout
{
*integrated_fitness = bmax*calc_integral(genotype, state, number_of_selection_protein_bf_dt, dt, Ne_saturate)
-cost_of_expression*dt;
}
else // bf dt_prime, the benefit saturates
{
dt_prime=calc_tprime(genotype,state,number_of_selection_protein_bf_dt,dt,Ne_saturate,genotype->nproteins-1);
*integrated_fitness = bmax*dt_prime+bmax*(calc_integral(genotype, state, number_of_selection_protein_bf_dt, dt, Ne_saturate)-
calc_integral(genotype, state, number_of_selection_protein_bf_dt, dt_prime, Ne_saturate))-
cost_of_expression*dt;
}
}
else // increase in effector protein
{
if(Ne>=Ne_saturate) //too many effector throughout
{
*integrated_fitness =dt*(bmax-cost_of_expression);
}
else if(Ne_next<=Ne_saturate)// not enough effector throughout
{
*integrated_fitness = bmax*calc_integral(genotype, state, number_of_selection_protein_bf_dt, dt, Ne_saturate)
-cost_of_expression*dt;
}
else //Aft dt_prime, the benefit saturates
{
dt_prime=calc_tprime(genotype,state,number_of_selection_protein_bf_dt,dt,Ne_saturate,genotype->nproteins-1);
*integrated_fitness = bmax*(dt-dt_prime)+bmax*calc_integral(genotype, state, number_of_selection_protein_bf_dt, dt_prime, Ne_saturate)-
cost_of_expression*dt;
}
}
/* compute instantaneous fitness at t */
if (Ne_next < Ne_saturate)
instantaneous_fitness = bmax*Ne_next/Ne_saturate;
else
instantaneous_fitness = bmax;
break;
case 'd': /* effector is deleterious! */
if(Ne>Ne_next)//decrease in effector protein
{
if(Ne_next>=Ne_saturate) //too many effector throughout
{
*integrated_fitness =0.0-dt*cost_of_expression;
}
else if(Ne<=Ne_saturate) // not enough effector throughout
{
*integrated_fitness = bmax*dt-bmax*calc_integral(genotype, state, number_of_selection_protein_bf_dt, dt, Ne_saturate)
-cost_of_expression*dt;
}
else // aft dt_prime, the benefit becomes positive
{
dt_prime=calc_tprime(genotype,state,number_of_selection_protein_bf_dt,dt,Ne_saturate,genotype->nproteins-1);
*integrated_fitness = bmax*(dt-dt_prime)-bmax*(calc_integral(genotype, state, number_of_selection_protein_bf_dt, dt, Ne_saturate)-
calc_integral(genotype, state, number_of_selection_protein_bf_dt, dt_prime, Ne_saturate))-
cost_of_expression*dt;
}
}
else // increase in effector protein
{
if(Ne>=Ne_saturate) //too many effector throughout
{
*integrated_fitness =0.0-dt*cost_of_expression;
}
else if(Ne_next<=Ne_saturate)// not enough effector throughout
{
*integrated_fitness = bmax*dt-bmax*calc_integral(genotype, state, number_of_selection_protein_bf_dt, dt, Ne_saturate)
-cost_of_expression*dt;
}
else //Aft dt_prime, the benefit becomes zero
{
dt_prime=calc_tprime(genotype,state,number_of_selection_protein_bf_dt,dt,Ne_saturate,genotype->nproteins-1);
*integrated_fitness = bmax*dt_prime-bmax*calc_integral(genotype, state, number_of_selection_protein_bf_dt, dt_prime, Ne_saturate)-
cost_of_expression*dt;
}
}
if(Ne_next<Ne_saturate)
instantaneous_fitness = bmax - bmax/Ne_saturate*Ne_next;
else
instantaneous_fitness = 0.0;
break;
case 'l':
*integrated_fitness=(bmax-cost_of_expression)*dt;
instantaneous_fitness=bmax;
}
/* and instantaneous integrated rate */
instantaneous_fitness -= cost_of_expression;
/* return the instantaneous fitness */
return instantaneous_fitness;
}
/*Calculate probability of binding configurations*/
static void calc_TF_dist_from_all_BS(Genotype *genotype, CellState *state, int gene_id)
{
int max_N_binding_act=genotype->max_unhindered_sites[gene_id][1]+1; //Binding configurations can contain at most x activators, plus 1 type of configurations that don't have activators at all.
int max_N_binding_rep=genotype->max_unhindered_sites[gene_id][2]+1; //Binding configurations can contain at most y repressors, plus 1 type of configurations that don't have repressors at all.
double ratio_matrices[genotype->binding_sites_num[gene_id]+1][max_N_binding_rep][max_N_binding_act];
double sum;
register double product_of_freq;
register float cache_Kd;
int pos_of_last_record;
int pos_of_mat_nH;
int pos_next_record;
int i,j,m,n;
double temp;
AllTFBindingSites *BS_info;
float *protein_number;
protein_number=&(state->protein_number[0]);
BS_info=genotype->all_binding_sites[gene_id];
/* initializing matrices to all zeros */
for(i=0;i<max_N_binding_rep;i++)
{
for(j=0;j<max_N_binding_act;j++)
ratio_matrices[0][i][j]=0.0;
}
/* body of the forward algorithm*/
pos_next_record=0; //where in the ratio_matrices to put the next record
ratio_matrices[pos_next_record][0][0]=BS_info[0].Kd;
/*calculate distribution based on the first BS*/
if(genotype->protein_identity[BS_info[0].tf_id]==1) // if a activator binds to BS 0
ratio_matrices[pos_next_record][0][1]=protein_number[BS_info[0].tf_id];
else
ratio_matrices[pos_next_record][1][0]=protein_number[BS_info[0].tf_id];
/*keep calculating distribution from the remaining BS*/
for(m=1;m<genotype->binding_sites_num[gene_id];m++)
{
pos_next_record++;
/*If binding of site m blocks other binding sites*/
product_of_freq = protein_number[BS_info[m].tf_id];
if(BS_info[m].N_hindered!=0)
{
for(n=m-BS_info[m].N_hindered;n<=m-1;n++)
product_of_freq*=BS_info[n].Kd;
}
cache_Kd=BS_info[m].Kd;
/*Check whether m is a site of activator or repressor*/
switch(genotype->protein_identity[BS_info[m].tf_id])
{
case ACTIVATOR: // a BS of activators
if(m-BS_info[m].N_hindered!=0)//if binding of m does not block all of the BS evaluated before
{
/*find matrix(n-H)*/
pos_of_mat_nH=pos_next_record-BS_info[m].N_hindered-1;
/*find matrix(n)*/
pos_of_last_record=pos_next_record-1;
for(i=0;i<max_N_binding_rep;i++)
for(j=1;j<max_N_binding_act;j++)
ratio_matrices[pos_next_record][i][j]=cache_Kd*ratio_matrices[pos_of_last_record][i][j]+product_of_freq*ratio_matrices[pos_of_mat_nH][i][j-1];
for(i=0;i<max_N_binding_rep;i++)
ratio_matrices[pos_next_record][i][0]=cache_Kd*ratio_matrices[pos_of_last_record][i][0];
}
else
{
/*find matrix(n)*/
pos_of_last_record=pos_next_record-1; //find last record
for(i=0;i<max_N_binding_rep;i++)
for(j=0;j<max_N_binding_act;j++)
ratio_matrices[pos_next_record][i][j]=cache_Kd*ratio_matrices[pos_of_last_record][i][j];
ratio_matrices[pos_next_record][0][1]+=product_of_freq;
}
break;
case REPRESSOR: // a BS of repressors
if(m-BS_info[m].N_hindered!=0)
{
/*find matrix(n-H)*/
pos_of_mat_nH=pos_next_record-BS_info[m].N_hindered-1;
/*find matrix(n)*/
pos_of_last_record=pos_next_record-1;
/*Similar problem when pos_of_mat_nH=pos_next_record*/
for(i=1;i<max_N_binding_rep;i++)
for(j=0;j<max_N_binding_act;j++)
ratio_matrices[pos_next_record][i][j]=cache_Kd*ratio_matrices[pos_of_last_record][i][j]+product_of_freq*ratio_matrices[pos_of_mat_nH][i-1][j];
for(j=0;j<max_N_binding_act;j++)
ratio_matrices[pos_next_record][0][j]=cache_Kd*ratio_matrices[pos_of_last_record][0][j];
}
else
{
/*find matrix(n)*/
pos_of_last_record=pos_next_record-1;
for(i=0;i<max_N_binding_rep;i++)
for(j=0;j<max_N_binding_act;j++)
ratio_matrices[pos_next_record][i][j]=cache_Kd*ratio_matrices[pos_of_last_record][i][j];
ratio_matrices[pos_next_record][1][0]+=product_of_freq;
}
break;
}
}
sum=0.0;
for(i=0;i<max_N_binding_rep;i++)
for(j=0;j<max_N_binding_act;j++)
sum+=ratio_matrices[pos_next_record][i][j];
temp=0.0;
for(i=0;i<max_N_binding_rep;i++)
for(j=genotype->min_N_activator_to_transc[gene_id];j<max_N_binding_act;j++)
temp+=ratio_matrices[pos_next_record][i][j];
state->P_A[gene_id]=(float)(temp/sum);
temp=0.0;
for(i=1;i<max_N_binding_rep;i++)
for(j=0;j<max_N_binding_act;j++)
temp+=ratio_matrices[pos_next_record][i][j];
state->P_R[gene_id]=(float)(temp/sum);
temp=0.0;
for(j=genotype->min_N_activator_to_transc[gene_id];j<max_N_binding_act;j++)
temp+=ratio_matrices[pos_next_record][0][j];
state->P_A_no_R[gene_id]=(float)(temp / sum);
temp=0.0;
for(j=0;j<genotype->min_N_activator_to_transc[gene_id];j++)
temp+=ratio_matrices[pos_next_record][0][j];
state->P_NotA_no_R[gene_id]=(float)(temp/sum);
}
/*
* update both the protein concentration and current cell size *
*
*/
static void update_protein_number_and_fitness( Genotype *genotype,
CellState *state,
GillespieRates *rates,
float dt)
{
int i,j;
float ct, ect, one_minus_ect;
float N_effector_molecules_bf_dt[genotype->protein_pool[genotype->nproteins-1][0][0]];
float instantaneous_fitness = 0.0;
float integrated_fitness = 0.0;
/* store the numbers of the effector proteins encoded by each copy of gene before updating*/
for(i=0;i<genotype->protein_pool[genotype->nproteins-1][0][0];i++)
N_effector_molecules_bf_dt[i]=state->gene_specific_protein_number[genotype->protein_pool[genotype->nproteins-1][1][i]];
/* update protein numbers*/
for (i=N_SIGNAL_TF; i < genotype->ngenes; i++)
{
ct=genotype->protein_decay_rate[i]*dt;
ect = exp(-ct);
if (fabs(ct)<EPSILON) one_minus_ect=ct;
else one_minus_ect = 1.0-ect;
/* get the new protein concentration for this gene */
state->gene_specific_protein_number[i]=ect*state->gene_specific_protein_number[i]+state->protein_synthesis_index[i]*one_minus_ect;
}
/* now, use protein_pool to pool gene specific protein number*/
for(i=N_SIGNAL_TF;i<genotype->nproteins;i++)
{
state->protein_number[i]=0.0;
for(j=0;j<genotype->protein_pool[i][0][0];j++)
state->protein_number[i]+=state->gene_specific_protein_number[genotype->protein_pool[i][1][j]];
}
/* now find out the protein numbers at end of dt interval and compute instantaneous and cumulative fitness */
instantaneous_fitness = calc_fitness(&integrated_fitness,
genotype,
state,
N_effector_molecules_bf_dt,
dt);
/* update cumulative fitness at the end of dt*/
state->cumulative_fitness += integrated_fitness;
/* update the instantaneous fitness at the end of dt */
state->instantaneous_fitness = instantaneous_fitness;
}
/*
*
* Functions that handle each possible Gillespie event
*
*/
static int Gillespie_event_mRNA_decay(GillespieRates *rates, CellState *state, Genotype *genotype, RngStream RS)
{
int gene_id;
float x;
int mRNA_id;
while(1)/*in case of numerical error*/
{
x=RngStream_RandU01(RS)*rates->total_mRNA_decay_rate;
gene_id=N_SIGNAL_TF-1;
/* loop through mRNA products, to choose the mRNA with the
proportionally higher decay rate */
while (gene_id < genotype->ngenes-1 && x > 0.0)
{
gene_id++;
x-= rates->mRNA_decay_rate[gene_id];
}
/*rarely, numerical error picks up a gene that has no mRNA at all*/
if((state->mRNA_aft_transl_delay_num[gene_id]+state->mRNA_under_transl_delay_num[gene_id])>=1)
break;
}
/* assume mRNAs are equally likely to be degraded */
x = RngStream_RandInt(RS,1,state->mRNA_aft_transl_delay_num[gene_id] + state->mRNA_under_transl_delay_num[gene_id]);
/* decay mRNA in cytoplasm */
if (x <= state->mRNA_aft_transl_delay_num[gene_id])
{
/* remove the mRNA from the cytoplasm count */
(state->mRNA_aft_transl_delay_num[gene_id])--;
/*update protein synthesis rate*/
state->protein_synthesis_index[gene_id] = (float)state->mRNA_aft_transl_delay_num[gene_id]*genotype->translation_rate[gene_id]/genotype->protein_decay_rate[gene_id];
if(genotype->which_protein[gene_id]==genotype->nproteins-1)
return DO_NOTHING;
else // an mRNA of transcription factor is degraded, which can cause fluctuation in transcription factor concentrations.
return gene_id;
}
else
{
/* decay mRNA in process of translation initialization */
mRNA_id = RngStream_RandInt(RS,0,state->mRNA_under_transl_delay_num[gene_id]-1);
/* delete this fixed event: this mRNA will never be translated */
delete_fixed_event(gene_id, mRNA_id, &(state->mRNA_transl_init_time_end_head), &(state->mRNA_transl_init_time_end_tail));
/* remove the mRNA from the count */
(state->mRNA_under_transl_delay_num[gene_id])--;
return DO_NOTHING;
}
}
static void Gillespie_event_repressed_to_intermediate(GillespieRates *rates, CellState *state, Genotype *genotype, RngStream RS)
{
int gene_id;
float x;
while(1)
{
x= RngStream_RandU01(RS)*rates->total_repressed_to_intermediate_rate;
gene_id=N_SIGNAL_TF-1;
while(gene_id<genotype->ngenes-1 && x>0.0)
{
gene_id++;
x-=rates->repressed_to_intermediate_rate[gene_id];
}
if(rates->repressed_to_intermediate_rate[gene_id]>0.0)
break;
}
/* set state */
state->transcriptional_state[gene_id]=INTERMEDIATE;
}
static void Gillespie_event_intermediate_to_repressed(GillespieRates *rates, CellState *state, Genotype *genotype, RngStream RS)
{
int gene_id;
float x;
while(1)
{
x= RngStream_RandU01(RS)*rates->total_intermediate_to_repressed_rate;
gene_id=N_SIGNAL_TF-1;
/* choose a particular gene copy to change state */
while(gene_id<genotype->ngenes-1 && x>0.0)
{
gene_id++;
x-=rates->intermediate_to_repressed_rate[gene_id];
}
if(rates->intermediate_to_repressed_rate[gene_id]>0.0)
break;
}
/* set state */
state->transcriptional_state[gene_id]=REPRESSED;
}
static void Gillespie_event_intermediate_to_active(GillespieRates *rates, CellState *state, Genotype *genotype, RngStream RS)
{
float x;
int gene_id;
while(1)
{
x= RngStream_RandU01(RS)*rates->total_intermediate_to_active_rate;
gene_id=N_SIGNAL_TF-1;
/* choose a particular gene copy to change state */
while(gene_id<genotype->ngenes-1 && x>0.0)
{
gene_id++;
x-=rates->intermediate_to_active_rate[gene_id];
}
if(rates->intermediate_to_active_rate[gene_id]>0.0)
break;
}
/* set state */
state->transcriptional_state[gene_id] =ACTIVE;
}
static void Gillespie_event_active_to_intermediate(Genotype *genotype, CellState *state,GillespieRates *rates, RngStream RS)
{
int gene_id;
float x;
while(1)
{
x=RngStream_RandU01(RS)*rates->total_active_to_intermediate_rate;
gene_id=N_SIGNAL_TF-1;
while(gene_id < genotype->ngenes-1 && x>0.0)
{
gene_id++;
x -= rates->active_to_intermediate_rate[gene_id];
}
if(rates->active_to_intermediate_rate[gene_id]>0.0)
break;
}
state->transcriptional_state[gene_id]=INTERMEDIATE;
}
static void Gillespie_event_transcription_init(GillespieRates *rates, CellState *state, Genotype *genotype, float dt, RngStream RS)
{
int gene_id;
int x;
float candidate_t;
int concurrent;
gene_id=N_SIGNAL_TF-1;
x=RngStream_RandInt(RS,1,rates->total_N_gene_transcript_initiated);
while(gene_id<genotype->ngenes-1 && x>0)
{
gene_id++;
x-=rates->transcript_initiation_state[gene_id];
}
/* now that transcription of gene has been initiated,
* we add the timepoint at which the transcription ends,
* which is dt+time-of-transcription from now */
candidate_t=state->t+dt+(float)genotype->locus_length[gene_id]/TRANSCRIPTION_ELONGATION_RATE+TRANSCRIPTION_TERMINATION_TIME;
concurrent=check_concurrence(state, candidate_t);
while(concurrent)//if the time to update overlaps with existing events, add a tiny offset
{
candidate_t+=TIME_OFFSET;
concurrent=check_concurrence(state, candidate_t);
}
add_fixed_event(gene_id, candidate_t,&(state->mRNA_transcr_time_end_head), &(state->mRNA_transcr_time_end_tail));
/* increase the number mRNAs being transcribed */
(state->mRNA_under_transc_num[gene_id])++;
}
/*
* END
* Functions that handle each possible Gillespie event
*/
/* do a fixed event that occurs in current t->dt window */
static int do_fixed_event(Genotype *genotype,
CellState *state,
GillespieRates *rates,
Environment *env,
Phenotype *timecourse,
float *dt,
int event)
{
int i, return_value;
return_value=DO_NOTHING;
switch (event)
{
case 1: /* a transcription event ends */
fixed_event_end_transcription(dt, state, rates, genotype);
break;
case 2: /* a translation initialization event ends */
return_value=fixed_event_end_translation_init(genotype, state, rates, dt);
state->cell_activated=1;
break;
case 3: /* turn signal off*/
*dt = state->signal_off_head->time - state->t;
update_protein_number_and_fitness(genotype, state, rates, *dt);
delete_fixed_event_from_head(&(state->signal_off_head),&(state->signal_off_tail));
if(env->fixed_effector_effect)
state->effect_of_effector=env->effect_of_effector_aft_burn_in;
else
state->effect_of_effector='d';
state->protein_number[N_SIGNAL_TF-1]=env->signal_off_strength;
return_value=SUDDEN_SIGNAL_CHANGE;
break;
case 4: /*turn signal on*/
*dt = state->signal_on_head->time - state->t;
update_protein_number_and_fitness(genotype, state, rates, *dt);
delete_fixed_event_from_head(&(state->signal_on_head),&(state->signal_on_tail));
state->protein_number[N_SIGNAL_TF-1]=env->signal_on_strength;
if(env->fixed_effector_effect)
state->effect_of_effector=env->effect_of_effector_aft_burn_in;
else
state->effect_of_effector='b';
return_value=SUDDEN_SIGNAL_CHANGE;
break;
case 5: /* finishing burn-in developmental simulation*/
*dt=state->burn_in_growth_rate_head->time-state->t;
update_protein_number_and_fitness(genotype, state, rates, *dt);
state->cumulative_fitness_after_burn_in=state->cumulative_fitness;
delete_fixed_event_from_head(&(state->burn_in_growth_rate_head),&(state->burn_in_growth_rate_tail));
if(env->signal_on_aft_burn_in==1)
state->protein_number[N_SIGNAL_TF-1]=env->signal_on_strength;
else
state->protein_number[N_SIGNAL_TF-1]=env->signal_off_strength;