-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation_clib.c
More file actions
3168 lines (2792 loc) · 120 KB
/
simulation_clib.c
File metadata and controls
3168 lines (2792 loc) · 120 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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<stdbool.h>
#include<string.h>
#include "pcg_basic.h"
#include "simulation_clib.h"
const double ZERO=1.0e-10;
const size_t LINE_SIZE=300;
const char CONFIG_FILE_DELIM[5]=":";
//SUBSET OF CONFIG PARAMETERS:
struct config_dict {
int NUM_RANDOM_ICS;
long double CONVERGENCE_PROXIMITY;
double SAMEPOINT_PROXIMITY;
int ITER_FOR_ODE;
int ITER_FOR_RELAXATION;
int ITER_FOR_LIMIT_CYCLE;
double EULER_SIM_TIME;
double EULER_SIM_TIME_LAST_ANN_STEP;
double STARTING_NOISE_ANN;
double STARTING_NOISE_CONSTANT;
double NOISE_SCALING_FACTOR_ANN;
double LIMIT_CYCLE_SIM_TIME;
double EULER_SIM_STEP_SIZE;
double LIMIT_CYCLE_SIM_STEP_SIZE;
int MAX_STABLE_STATES;
int MAX_LIMIT_CYCLES;
int MAX_ALLOWED_PERIODS;
int NO_OF_SAMPLED_PERIODS;
int ALLOWED_ERROR_IN_PERIODS;
};
//DEFAULT VALUES:
struct config_dict sim_config={
.NUM_RANDOM_ICS=1000,
.CONVERGENCE_PROXIMITY=1.0e-12,
.SAMEPOINT_PROXIMITY=0.1,
.ITER_FOR_ODE=200,
.ITER_FOR_RELAXATION=10,
.ITER_FOR_LIMIT_CYCLE=20,
.EULER_SIM_TIME=10,
.EULER_SIM_TIME_LAST_ANN_STEP=2000,
.STARTING_NOISE_ANN=20,
.STARTING_NOISE_CONSTANT=20,
.NOISE_SCALING_FACTOR_ANN=0.95,
.LIMIT_CYCLE_SIM_TIME=10,
.EULER_SIM_STEP_SIZE=0.01,
.LIMIT_CYCLE_SIM_STEP_SIZE=0.01,
.MAX_STABLE_STATES=20,
.MAX_LIMIT_CYCLES=10,
.MAX_ALLOWED_PERIODS=100,
.NO_OF_SAMPLED_PERIODS=3,
.ALLOWED_ERROR_IN_PERIODS=3
};
/**********************************************************************/
void find_solutions(char *WORK_DIR, \
char *FNAME_STATES, \
char *FNAME_LIMITCYCLES, \
char *FNAME_SUMMARY, \
const int MODEL_NO,\
int NUM_NODES,int NUM_EDGES,\
int NUM_RANDOM_ICS,int ITER_FOR_ODE,\
double EULER_SIM_TIME,\
double EULER_STEP_SIZE,\
long double CONVERGENCE_PROXIMITY,\
double TRANS_RATE_FACTOR,\
const double *MPR_arrv,\
const double *DNR_arrv,\
const int *node_type_arrv,\
const int *edge_source_arrv,\
const int *edge_target_arrv,\
const int *edge_type_arrv,\
const double *TSH_arrv,\
const int *HCO_arrv,\
const double *FCH_arrv,\
double *EXP_dict_arrv)
/*------------------------------------------------------------------
It carries out two main operations
(1) For each of the initial conditions, it estimates stable state
of the network (an n dimensional point) by repeatedly invoking
estimate_stable_expression function.
(2) After obtaining stable states for all initial conditions,
it clusters those stable state solutions by invoking
cluster_solutions function.
Definitions of the main data structures:
MPR_arrv: maximum production rates for each node
DNR_arrv: degradation rates for each node
node_type_arrv: type for each node
edge_source_arrv: source id of each edge. Id of a source is the
corresponding index of the arrays MPR_arrv, DNR_arrv, and
node_type_arrv
edge_target_arrv: target id of each edge
edge_type_arrv: type of each edge
TSH_arrv: threshold of each edge
HCO_arrv: hill coefficient for each edge
FCH_arrv: fold change for each edge
EXP_dict_arrv: stable expression level of all the nodes
--------------------------------------------------------------------*/
{
//CONSTANTS:
const int TOTAL_EULER_STEPS=(int) (EULER_SIM_TIME/EULER_STEP_SIZE);
//VARIABLES:
double *EXP_dict_arr=(double *) EXP_dict_arrv;
double IC_arr[NUM_NODES]; //saves ICs generated by set_ICs()
double exp_arr[NUM_NODES]; //saves exps from estimate_stable_expression()
double minEXP_arr[NUM_NODES]; //minimum expression for all nodes
double maxEXP_arr[NUM_NODES]; //maximum expression for all nodes
double EXP_arr[NUM_RANDOM_ICS][NUM_NODES];
double fX_arr_norm[NUM_RANDOM_ICS];
double fX_arr[NUM_NODES];
double zero_arr[NUM_NODES];
//INDEX VARIABLES:
int count_pos=0; //keeps track of position in EXP_dict_arr
int count_iteration; //counts iteration over IC initialization
int i=0; //general purpose
//read the simulation configations:
char fname_cfg[30]="racipe.cfg";
read_config(WORK_DIR,fname_cfg);
// calculate minimum and maximum allowable expression for each node:
cal_EXPrange(NUM_NODES,NUM_EDGES,MPR_arrv,DNR_arrv,\
edge_source_arrv, edge_target_arrv,\
edge_type_arrv, FCH_arrv, minEXP_arr, maxEXP_arr);
//set exp_arr to zeros:
for (i=0;i<NUM_NODES;++i) {
exp_arr[i]=0.0;
fX_arr[i]=0.0;
zero_arr[i]=0.0;
}
count_iteration=0;
while(count_iteration<NUM_RANDOM_ICS)
{
// randomly select initial condition:
set_ICs(NUM_NODES,minEXP_arr,maxEXP_arr,IC_arr);
// estimate the stable state for the current initial condition:
estimate_stable_expression(NUM_NODES,NUM_EDGES,ITER_FOR_ODE,\
TOTAL_EULER_STEPS,EULER_STEP_SIZE,\
CONVERGENCE_PROXIMITY,\
TRANS_RATE_FACTOR,\
MPR_arrv,DNR_arrv,node_type_arrv,
edge_source_arrv,\
edge_target_arrv,edge_type_arrv,\
TSH_arrv,HCO_arrv,FCH_arrv,\
IC_arr,exp_arr,fX_arr);
// calculate norm of fX:
fX_arr_norm[count_iteration]=cal_norm(fX_arr, NUM_NODES);
//save the exp_arr to EXP_dict_arr:
//if any value is found <= zero, assign it min value
for(i=0;i<NUM_NODES;i++){
EXP_dict_arr[count_pos+i]=exp_arr[i]>0?exp_arr[i]:minEXP_arr[i];
EXP_arr[count_iteration][i]=exp_arr[i]>0?exp_arr[i]:minEXP_arr[i];
}
count_pos+=NUM_NODES;
count_iteration+=1;
} //end of while(count_iteration<NUM_RANDOM_ICS)
// cluster the stable state solutions:
cluster_solutions(WORK_DIR, \
FNAME_STATES, FNAME_LIMITCYCLES, FNAME_SUMMARY,\
MODEL_NO, NUM_NODES,NUM_EDGES,\
NUM_RANDOM_ICS,ITER_FOR_ODE,\
EULER_SIM_TIME,\
EULER_STEP_SIZE,\
CONVERGENCE_PROXIMITY,\
TRANS_RATE_FACTOR,\
MPR_arrv,\
DNR_arrv,\
node_type_arrv,\
edge_source_arrv,\
edge_target_arrv,\
edge_type_arrv,\
TSH_arrv,\
HCO_arrv,\
FCH_arrv,\
fX_arr_norm,\
EXP_arr);
return;
}
/*-----------------------------------------------------------------------*/
void cluster_solutions(char *WORK_DIR, \
char *FNAME_STATES, char *FNAME_LIMITCYCLES, \
char *FNAME_SUMMARY,\
const int MODEL_NO,\
int NUM_NODES,int NUM_EDGES,\
int NUM_RANDOM_ICS,int ITER_FOR_ODE,\
double EULER_SIM_TIME,\
double EULER_STEP_SIZE,\
long double CONVERGENCE_PROXIMITY,\
double TRANS_RATE_FACTOR,\
const double *MPR_arrv,\
const double *DNR_arrv,\
const int *node_type_arrv,\
const int *edge_source_arrv,\
const int *edge_target_arrv,\
const int *edge_type_arrv,\
const double *TSH_arrv,\
const int *HCO_arrv,\
const double *FCH_arrv,\
double *fX_arr_norm,\
double EXP_arr[][NUM_NODES])
/*------------------------------------------------------------------
This function clusters the solutions obtained from all initial
conditions into states and limit cycles.
It invokes two functions to carry out its operation:
(1) find_states: clusters all the solutions into states
(2) find_limitcycles: for each solution not used in state calculation,
this function runs further simulation to obtain the limit cycle
for that solution.
--------------------------------------------------------------------*/
{
//LOCAL VARIABLES:
//variables for limit cycle (LC) calculation:
bool fX_arr_bool[NUM_RANDOM_ICS];
//COUNTING VARIABLES:
int state_count;
int LC_count;
int i;
clock_t time_begin, time_end;
double time_spent;
//DEFINE FILE NAME:
//char fname_summary[300]="\0";
//strcat(fname_summary,WORK_DIR);
//strcat(fname_summary,"/");
//strcat(fname_summary,"cellcycle.summary.txt");
//strcat(fname_summary,"TS.summary.txt");
state_count=0;
LC_count=0;
state_count=find_states(WORK_DIR, FNAME_STATES, MODEL_NO,\
NUM_NODES, NUM_RANDOM_ICS,\
CONVERGENCE_PROXIMITY,\
fX_arr_norm,\
EXP_arr,\
fX_arr_bool);
time_begin=clock();
LC_count=find_limitcycles(WORK_DIR, FNAME_LIMITCYCLES, MODEL_NO,\
NUM_NODES,NUM_EDGES,\
NUM_RANDOM_ICS,ITER_FOR_ODE,\
EULER_SIM_TIME,\
EULER_STEP_SIZE,\
CONVERGENCE_PROXIMITY,\
TRANS_RATE_FACTOR,\
MPR_arrv,\
DNR_arrv,\
node_type_arrv,\
edge_source_arrv,\
edge_target_arrv,\
edge_type_arrv,\
TSH_arrv,\
HCO_arrv,\
FCH_arrv,\
fX_arr_norm,\
EXP_arr,\
fX_arr_bool);
time_end=clock();
time_spent=(double) (time_end-time_begin)/CLOCKS_PER_SEC;
//write summary:
FILE *fh_summary;
//fh_summary=fopen(fname_summary,"a");
fh_summary=fopen(FNAME_SUMMARY,"a");
fprintf(fh_summary,"%d\t%d\t%d\t%8.6f\n",MODEL_NO, state_count,\
LC_count,time_spent);
fflush(fh_summary);
fclose(fh_summary);
return;
}
/*-----------------------------------------------------------------------*/
int find_states(char *WORK_DIR, char *FNAME_STATES, \
const int MODEL_NO,\
int NUM_NODES, int NUM_RANDOM_ICS,\
long double CONVERGENCE_PROXIMITY,\
double *fX_arr_norm,\
double EXP_arr[][NUM_NODES],\
bool fX_arr_bool[NUM_RANDOM_ICS])
{
//LOCAL VARIABLES:
int MAX_STABLE_STATES=sim_config.MAX_STABLE_STATES;
//solution_count_arr stores the number of solutions in each state:
int solution_count_arr[MAX_STABLE_STATES];
//solution_arr stores the average expression for all states:
double solution_arr[MAX_STABLE_STATES][NUM_NODES];
//solution_arr_avg stores the average solution for any state:
double solution_arr_avg[NUM_NODES];
double testDelta=0.0;
bool found;
//COUNTING VARIABLES:
int count_state;
int i;
//DEFINE FILE NAME:
//char fname_states[300]="\0";
//strcat(fname_states,WORK_DIR);
//strcat(fname_states,"/");
//strcat(fname_states,"cellcycle.states.txt");
//strcat(fname_states,"TS.states.txt");
//initialize the solution count for each state:
for (i=0;i<MAX_STABLE_STATES;i++) solution_count_arr[i]=0;
//SEPARATE ALL THE STABLE STATES INTO CLUSTERS:
count_state=0; //initialize state count variable
for (i=0;i<NUM_RANDOM_ICS;i++)
{
if (fX_arr_norm[i]>CONVERGENCE_PROXIMITY)
{
fX_arr_bool[i]=true;
continue;
}
fX_arr_bool[i]=false;
found = false;
int sol_no;
for (sol_no=0; sol_no<count_state;sol_no++)
{
testDelta=sum_delta(solution_arr[sol_no],EXP_arr[i],NUM_NODES);
if (testDelta<=CONVERGENCE_PROXIMITY)
{
int j;
for(j=0;j<NUM_NODES;j++)
{
solution_arr[sol_no][j]=(solution_arr[sol_no][j]*\
solution_count_arr[sol_no]+\
EXP_arr[i][j])/\
(solution_count_arr[sol_no]+1);
}
solution_count_arr[sol_no]++;
found=true;
break;
}
}
//new solution found:
if(!found){
int j;
for (j=0;j<NUM_NODES;j++) {
solution_arr[count_state][j]=EXP_arr[i][j];
}
//printf("fX_arr_norm[i]: %d\t%f\n",count_state,fX_arr_norm[i]);
solution_count_arr[count_state]=1;
count_state+=1;
if(count_state>=MAX_STABLE_STATES) break;
}
}
//SAVE SOLUTIONS: solution_arr
FILE *fh_states;
fh_states=fopen(FNAME_STATES,"a");
int j;
int k;
//write a new line to move the file pointer to the next line
//fprintf(fh_states,"\n");
for (j=0; j<count_state;j++){
fprintf(fh_states,"%d\t",MODEL_NO); //write model index
fprintf(fh_states,"%d\t",count_state); //write total solution
fprintf(fh_states,"%d\t",(j+1)); //write solution index
for(k=0;k<NUM_NODES;k++){ //write gene levels
fprintf(fh_states,"%10.6f\t",log(solution_arr[j][k])/log(2));
}
fprintf(fh_states,"\n");
}
fflush(fh_states);
fclose(fh_states);
return count_state;
}
/*-----------------------------------------------------------------------*/
int find_limitcycles(char *WORK_DIR, char *FNAME_LIMITCYCLES, \
const int MODEL_NO,\
int NUM_NODES, int NUM_EDGES,\
int NUM_RANDOM_ICS, int ITER_FOR_ODE,\
double EULER_SIM_TIME,\
double EULER_STEP_SIZE,\
long double CONVERGENCE_PROXIMITY,\
double TRANS_RATE_FACTOR,\
const double *MPR_arrv,\
const double *DNR_arrv,\
const int *node_type_arrv,\
const int *edge_source_arrv,\
const int *edge_target_arrv,\
const int *edge_type_arrv,\
const double *TSH_arrv,\
const int *HCO_arrv,\
const double *FCH_arrv,\
double *fX_arr_norm,\
double EXP_arr[][NUM_NODES],\
bool fX_arr_bool[NUM_RANDOM_ICS])
/*------------------------------------------------------------------
This function finds and saves limit cycles. It calls other functions
to carry these out:
(1) detect_limitcycle: this returns the estimated size of
the limit cycle.
(2) cal_limitcycle: this calculates expression levels along the
limit cycle.
return value: number of limit cycle found for a specific model.
--------------------------------------------------------------------*/
{
//CONSTANTS:
const double LIMIT_CYCLE_SIM_TIME=sim_config.LIMIT_CYCLE_SIM_TIME;
const double LIMIT_CYCLE_SIM_STEP_SIZE=sim_config.LIMIT_CYCLE_SIM_STEP_SIZE;
const int LIMIT_CYCLE_SIM_STEPS=(int) (LIMIT_CYCLE_SIM_TIME/\
LIMIT_CYCLE_SIM_STEP_SIZE);
const int MAX_LIMIT_CYCLES=sim_config.MAX_LIMIT_CYCLES;
const int ALLOWED_ERROR_IN_PERIODS=sim_config.ALLOWED_ERROR_IN_PERIODS;
//LOCAL VARIABLES:
int LC_period_arr[MAX_LIMIT_CYCLES];
double **LC_exp_arr;
double start_exp_arr[NUM_NODES]; //start point for calculating limit cycle
double LC_start_exp_arr[NUM_NODES];
int period=0; //period of the limit cycle
//COUNTING VARIABLE:
int count_LC;
int i;
//DEFINE FILE HEADER:
FILE *fh_LCs;
//fh_LCs=fopen(fname_LCs,"a");
fh_LCs=fopen(FNAME_LIMITCYCLES,"a");
//write a new line to move the file pointer to the next line
//fprintf(fh_LCs,"\n");
//initialize LC_period_arr with zeros:
for (i=0;i<MAX_LIMIT_CYCLES;i++){
LC_period_arr[i]=0;
}
//initialize LC_start_exp_arr
//it will be defined in detect_limitcycle()
for (i=0;i<NUM_NODES;i++){
LC_start_exp_arr[i]=0.0;
}
//initialize limit cycle count variable:
count_LC=0;
for (i=0;i<NUM_RANDOM_ICS;i++)
{
if (fX_arr_bool[i]==false) continue;
//calculate limit cycle:
fX_arr_bool[i]=false;
period=detect_limitcycle(NUM_NODES,NUM_EDGES,\
LIMIT_CYCLE_SIM_STEPS,\
LIMIT_CYCLE_SIM_STEP_SIZE,\
CONVERGENCE_PROXIMITY,\
TRANS_RATE_FACTOR,\
MPR_arrv,DNR_arrv,\
node_type_arrv,\
edge_source_arrv,\
edge_target_arrv,\
edge_type_arrv,\
TSH_arrv,\
HCO_arrv,\
FCH_arrv,\
EXP_arr[i],\
LC_start_exp_arr);
if(period>0){
//check for repeated limit cycle:
int j;
int k;
//NEW_SIM_STEPS: simulation steps for calculating expression
//levels along limit cycle:
int NEW_SIM_STEPS;
//MAX_DIST: used as an allowed error between the converged expression
//levels (based on Initial Conditions) and the expression
//level along the limit cycle:
double MAX_DIST;
double tmp_norm=0.0;
bool is_same;
is_same=false;
for(k=0;k<count_LC;k++){
if(abs(period-LC_period_arr[k])<=ALLOWED_ERROR_IN_PERIODS){
is_same=true;
break;
}
}
//if it is the same limit cycle, skip subsequent calculation:
if(is_same) continue;
LC_period_arr[count_LC]=period;
count_LC++;
LC_exp_arr=allocate_LCmemory(period+2,NUM_NODES);
NEW_SIM_STEPS=period+1;
MAX_DIST=cal_limitcycle(NUM_NODES,NUM_EDGES,\
NEW_SIM_STEPS,\
LIMIT_CYCLE_SIM_STEP_SIZE,\
CONVERGENCE_PROXIMITY,\
TRANS_RATE_FACTOR,\
MPR_arrv,DNR_arrv,\
node_type_arrv,\
edge_source_arrv,\
edge_target_arrv,\
edge_type_arrv,\
TSH_arrv,\
HCO_arrv,\
FCH_arrv,\
LC_start_exp_arr,\
LC_exp_arr);
//relax MAX_DIST by factoring it by 10:
MAX_DIST *= 10.0;
//change fX_arr_bool status to false for all the positions that
//all give to this limit cycle:
for (j=0;j<NUM_RANDOM_ICS;j++){
if (fX_arr_bool[j]==false) continue;
for(k=0;k<period+2;k++){
tmp_norm=sum_delta(EXP_arr[j],LC_exp_arr[k],NUM_NODES);
if(tmp_norm<=MAX_DIST) {
fX_arr_bool[j]=false;
break;
}
}
}
//write LC expressions to file:
write_limitcycle(fh_LCs, MODEL_NO, NUM_NODES,\
count_LC, period, NEW_SIM_STEPS,\
LC_exp_arr);
//note: NEW_SIM_STEPS is larger than period by 2
//free the memory allocated for the current LC:
free_LCmemory(LC_exp_arr,period+2,NUM_NODES);
if(count_LC>=MAX_LIMIT_CYCLES) break;
}
}
fflush(fh_LCs);
fclose(fh_LCs);
return count_LC;
}
/*-----------------------------------------------------------------------*/
int detect_limitcycle(int NUM_NODES,int NUM_EDGES,\
int SIM_STEPS,double SIM_STEP_SIZE,\
long double CONVERGENCE_PROXIMITY,\
double TRANS_RATE_FACTOR,\
const double *MPR_arrv,const double *DNR_arrv,\
const int *node_type_arrv,\
const int *edge_source_arrv,\
const int *edge_target_arrv,\
const int *edge_type_arrv,\
const double *TSH_arrv,\
const int *HCO_arrv,\
const double *FCH_arrv,\
double *start_exp_arr,\
double *LC_start_exp_arr)
{
//CONSTANTS:
const int ITER_FOR_LIMIT_CYCLE=sim_config.ITER_FOR_LIMIT_CYCLE;
const int MAX_PERIODS=sim_config.MAX_ALLOWED_PERIODS;
//variables:
double fX_arr[NUM_NODES]; //fX_arr saves fX values in each Euler step
double prev_exp_arr[NUM_NODES];
double curr_exp_arr[NUM_NODES];
double next_exp_arr[NUM_NODES];
int i;
//storage for expressions at each peak:
double max_exp_arr[MAX_PERIODS][NUM_NODES];
//storage for expressions at each valley:
double min_exp_arr[MAX_PERIODS][NUM_NODES];
//storage for index at each peak:
int max_idx_arr[MAX_PERIODS];
//storage for index at each valley:
int min_idx_arr[MAX_PERIODS];
//store for distances from the start to each valley:
double min_dist_arr[MAX_PERIODS];
//storage for differences between min_dist and prev_dist:
//double diff_dist_arr[MAX_PERIODS];
//counts for peaks and valleys:
int count_max_exp, count_min_exp;
double prev_dist, curr_dist; //keeps track of distance from the start
bool moving_uphill; //keeps track of simulation direction
int count_exp; //global count for simulation steps
int period; //period for the LC
int count_iter; //count for the outer loop
//copy the start expressions to a local variable:
for (i=0;i<NUM_NODES;++i) {
curr_exp_arr[i]=start_exp_arr[i];
prev_exp_arr[i]=curr_exp_arr[i];
//printf("%2.6f\t",curr_exp_arr[i]);
}
//initialize the control variables:
prev_dist=curr_dist=0.0;
moving_uphill=true;
count_exp=0;
count_max_exp=count_min_exp=0;
period=0;
//calculate ODE-outer loop:
for(count_iter=0; count_iter<ITER_FOR_LIMIT_CYCLE;count_iter++)
{
//calculate ODE-inner loop:
int count_step=0;
while(count_step<SIM_STEPS)
{
cal_fX(NUM_NODES,NUM_EDGES,SIM_STEP_SIZE,\
TRANS_RATE_FACTOR,\
MPR_arrv,DNR_arrv,\
node_type_arrv,edge_source_arrv,\
edge_target_arrv,edge_type_arrv,\
TSH_arrv,HCO_arrv,FCH_arrv,\
curr_exp_arr,fX_arr);
//calculate next expressions:
for (i=0;i<NUM_NODES;++i) {
next_exp_arr[i]=curr_exp_arr[i]+fX_arr[i]*SIM_STEP_SIZE;
}
curr_dist=sum_delta(start_exp_arr,next_exp_arr,NUM_NODES);
if(moving_uphill){ //while moving uphill:
if (curr_dist<prev_dist){
//hit the peak already. now, switch to downhill:
moving_uphill=false;
}
}
else { //while moving downhill:
if (curr_dist>prev_dist){
//hit the bottom already. now, switch to uphill:
moving_uphill=true;
//save information about this valley:
for (i=0;i<NUM_NODES;++i){
min_exp_arr[count_min_exp][i]=curr_exp_arr[i];
}
min_idx_arr[count_min_exp]=count_exp;
min_dist_arr[count_min_exp]=prev_dist;
//diff_dist_arr[count_min_exp]=sum_delta(curr_exp_arr,\
// next_exp_arr,\
// NUM_NODES);
count_min_exp++;
} //(curr_dist>prev_dist)
} //end of the block for moving downhill
if((count_max_exp>=MAX_PERIODS) ||
(count_min_exp>=MAX_PERIODS)) break;
prev_dist=curr_dist; //uphill motion
//copy next exp to curr exp and save curr exp
for (i=0;i<NUM_NODES;++i){
curr_exp_arr[i]=next_exp_arr[i]>=0?next_exp_arr[i]:0;
}
count_exp++;
count_step+=1;
} //end of while(count_step<SIM_STEPS)
double fX_norm=cal_norm(fX_arr,NUM_NODES);
//if a stable state is found or a boundary situation is met,
//then return -1:
if(fX_norm<=CONVERGENCE_PROXIMITY) return -1;
period=cal_period(NUM_NODES,\
MAX_PERIODS,\
count_min_exp,\
min_idx_arr,\
min_exp_arr,\
LC_start_exp_arr);
//if nonzero period found, then break of the loop:
if (period!=0) break;
}//for(count_iter=0; count_iter<ITER_FOR_LIMIT_CYCLE;count_iter++)
return period;
}
/*-----------------------------------------------------------------------*/
int cal_period(int NUM_NODES,\
int MAX_PERIODS,\
int count_min_exp,\
int *min_idx_arr,\
double min_exp_arr[][NUM_NODES],\
double *LC_start_exp_arr)
{
//CALCULATE PERIODS from all valleys
//CONSTANTS:
const int NO_OF_SAMPLED_PERIODS=sim_config.NO_OF_SAMPLED_PERIODS;
const int ALLOWED_ERROR_IN_PERIODS=sim_config.ALLOWED_ERROR_IN_PERIODS;
const double SAMEPOINT_PROXIMITY=sim_config.SAMEPOINT_PROXIMITY;
//LOCAL VARIABLES:
int period;
int count_same_min_dist;
int count_period; //counts number of periods found
int period_T[MAX_PERIODS]; //saves the periods
int idx_last_period; //keeps track of the index of the last period
double diff_exp; //saves diff between expr levels at two time points
bool found = false; //marks whether period is found
int i;
//initialize all variables:
period=0;
count_period=0;
diff_exp=0.0;
count_same_min_dist=0;
//set a marker at the index of the position of the last valley:
idx_last_period=count_min_exp-1;
//check in backward direction starting from
//the last element in min_dist_arr:
for (i=count_min_exp-2;i>=0;i--){
diff_exp=sum_delta(min_exp_arr[count_min_exp-1],\
min_exp_arr[i],NUM_NODES);
if(diff_exp<=SAMEPOINT_PROXIMITY){
count_same_min_dist++;
period_T[count_period]=min_idx_arr[idx_last_period]-min_idx_arr[i];
//save position of the valley where the last period was found:
idx_last_period=i;
count_period++; //increment count of the periods found
}
}
if(count_period>=NO_OF_SAMPLED_PERIODS){
found = true;
//check whether each of these periods are
//within the allowed error limit:
for (i=count_period-1;i>=1;i--){
if(abs(period_T[i]-period_T[i-1])>ALLOWED_ERROR_IN_PERIODS){
found = false;
break;
}
}
}
if(found){
//copy the last sampled period to period varialbe for returning:
period=period_T[count_period-1];
//copy the last minimum expression to the
//LC start variable for returning:
for(i=0;i<NUM_NODES;i++){
LC_start_exp_arr[i]=min_exp_arr[count_min_exp-1][i];
}
}
return period;
}
/*-----------------------------------------------------------------------*/
double cal_limitcycle(int NUM_NODES,int NUM_EDGES,\
int SIM_STEPS,double SIM_STEP_SIZE,\
long double CONVERGENCE_PROXIMITY,\
double TRANS_RATE_FACTOR,\
const double *MPR_arrv,const double *DNR_arrv,\
const int *node_type_arrv,\
const int *edge_source_arrv,\
const int *edge_target_arrv,\
const int *edge_type_arrv,\
const double *TSH_arrv,\
const int *HCO_arrv,\
const double *FCH_arrv,\
double *start_exp_arr,\
double **LC_exp_arr)
{
double fX_arr[NUM_NODES]; //fX_arr saves fX values in each Euler step
double curr_exp_arr[NUM_NODES];
double next_exp_arr[NUM_NODES];
int i;
double max_dist;
//copy the start expressions to a local variable:
for (i=0;i<NUM_NODES;++i) {
curr_exp_arr[i]=start_exp_arr[i];
LC_exp_arr[0][i]=curr_exp_arr[i];
}
int count_step=0;
max_dist=0.0;
while(count_step<SIM_STEPS)
{
cal_fX(NUM_NODES,NUM_EDGES,SIM_STEP_SIZE,\
TRANS_RATE_FACTOR,\
MPR_arrv,DNR_arrv,\
node_type_arrv,edge_source_arrv,\
edge_target_arrv,edge_type_arrv,\
TSH_arrv,HCO_arrv,FCH_arrv,\
curr_exp_arr,fX_arr);
//calculate next expressions:
for (i=0;i<NUM_NODES;++i) {
next_exp_arr[i]=curr_exp_arr[i]+fX_arr[i]*SIM_STEP_SIZE;
}
double tmp_dist;
tmp_dist=sum_delta(curr_exp_arr,next_exp_arr,NUM_NODES);
if(tmp_dist>max_dist) max_dist=tmp_dist;
count_step+=1;
//copy next exp to curr exp and save curr exp
for (i=0;i<NUM_NODES;++i){
curr_exp_arr[i]=next_exp_arr[i]>=0?next_exp_arr[i]:0;
LC_exp_arr[count_step][i]=curr_exp_arr[i];
}
}
//printf("%f\n",max_dist);
return max_dist;
}
/*-----------------------------------------------------------------------*/
void write_limitcycle(FILE *fh_LCs, int MODEL_NO, int NUM_NODES,\
int count_LC, int SIZE_OF_PERIOD, int SIZE_OF_LIMIT_CYCLE,\
double **LC_exp_arr)
{
//This function writes the expressions of the network states
//along the limit cycle.
int j; //outer loop counter
int k; //inner loop coutner
//for (j=0;j<NEW_SIM_STEPS+1;j++){
for (j=0;j<SIZE_OF_LIMIT_CYCLE+1;j++){
fprintf(fh_LCs,"%d\t",MODEL_NO);
//write limit cycle number:
fprintf(fh_LCs,"%d\t",count_LC);
//write period of LC:
fprintf(fh_LCs,"%d\t",SIZE_OF_PERIOD);
for(k=0;k<NUM_NODES;k++){
//save log2(gene expression):
fprintf(fh_LCs,"%10.6f\t",log(LC_exp_arr[j][k])/log(2));
}
//new line to separate the expressions from
//those of the next time step:
fprintf(fh_LCs,"%s","\n");
}
//new line after writing the whole LC
//this separates expressions between limit cycles:
fprintf(fh_LCs,"%s","\n");
return;
}
/*-----------------------------------------------------------------------*/
void cal_fX(int NUM_NODES,int NUM_EDGES,\
double EULER_STEP_SIZE,\
double TRANS_RATE_FACTOR,\
const double *MPR_arrv,
const double *DNR_arrv,\
const int *node_type_arrv,\
const int *edge_source_arrv,\
const int *edge_target_arrv,\
const int *edge_type_arrv,\
const double *TSH_arrv,\
const int *HCO_arrv,\
const double *FCH_arrv,\
double const *curr_exp_arr,\
double *fX_arr)
{
//double fX_arr[NUM_NODES]; //fX_arr saves fX values in each Euler step
double fX2_arr[NUM_NODES]; //fX2_arr saves fX values for degradation part
int i;
//fX=Gx:
//fX2=kX*X:
for (i=0;i<NUM_NODES;++i) {
fX_arr[i]=MPR_arrv[i];
fX2_arr[i]=DNR_arrv[i]*curr_exp_arr[i];
}
//update fX with regulations:
for (i=0;i<NUM_EDGES;i++){
if (edge_type_arrv[i]==1 || edge_type_arrv[i]==5) {
fX_arr[edge_target_arrv[i]] *=\
eval_shiftedHill_fn(curr_exp_arr[edge_source_arrv[i]],\
TSH_arrv[i],HCO_arrv[i],\
FCH_arrv[i])/FCH_arrv[i];
}
else if (edge_type_arrv[i]==2 || edge_type_arrv[i]==6) {
fX_arr[edge_target_arrv[i]] *=\
eval_shiftedHill_fn(curr_exp_arr[edge_source_arrv[i]],\
TSH_arrv[i],HCO_arrv[i],\
FCH_arrv[i]);
}
else if (edge_type_arrv[i]==3 || edge_type_arrv[i]==4) {
fX2_arr[edge_target_arrv[i]] *=\
eval_shiftedHill_fn(curr_exp_arr[edge_source_arrv[i]],\
TSH_arrv[i],HCO_arrv[i],\
FCH_arrv[i]);
}
}
//adjust fX by kX*X:
for (i=0;i<NUM_NODES;++i) {
fX_arr[i]-=fX2_arr[i];
if (node_type_arrv[i]==2){
fX_arr[i]*=TRANS_RATE_FACTOR;
}
}
return;
}
/*-----------------------------------------------------------------------*/
void estimate_stable_expression(int NUM_NODES,int NUM_EDGES,\
int ITER_FOR_ODE,int TOTAL_EULER_STEPS,\
double EULER_STEP_SIZE,\
long double CONVERGENCE_PROXIMITY,\
double TRANS_RATE_FACTOR,\
const double *MPR_arrv,\
const double *DNR_arrv,\
const int *node_type_arrv,
const int *edge_source_arrv,\
const int *edge_target_arrv,\
const int *edge_type_arrv,\
const double *TSH_arrv,\
const int *HCO_arrv,\
const double *FCH_arrv,\
const double *IC_arr, double *exp_arr,\
double *fX_arr)
{
//CONSTANTS:
const int ITER_FOR_RELAXATION=sim_config.ITER_FOR_RELAXATION;
double curr_exp_arr[NUM_NODES];
double next_exp_arr[NUM_NODES];
double test_delta=0.0;
int iter_count=0;
int i=0;
double fX_norm;
double fX_norm_now;
//copy ICs to a local variable:
for (i=0;i<NUM_NODES;++i) {
curr_exp_arr[i]=IC_arr[i];
}
while(iter_count<ITER_FOR_ODE)
{
cal_euler_approximation(NUM_NODES,NUM_EDGES,TOTAL_EULER_STEPS,\
EULER_STEP_SIZE,TRANS_RATE_FACTOR,\
MPR_arrv,DNR_arrv,node_type_arrv,\
edge_source_arrv,edge_target_arrv,\
edge_type_arrv,TSH_arrv,HCO_arrv,FCH_arrv,\
curr_exp_arr,next_exp_arr,fX_arr);
test_delta=sum_delta(curr_exp_arr,next_exp_arr,NUM_NODES);
//check for convergence to a state:
//if the simulation converges to a state, then break out of the loop:
if (test_delta<=CONVERGENCE_PROXIMITY){
break;
}
fX_norm_now=cal_norm(fX_arr,NUM_NODES);
//check for convergence to a limit cycle:
//after ITER_RELAXATION times of iteration, check whether limit
//cycle situation arises. If it does, break outf of the loop:
if (iter_count>ITER_FOR_RELAXATION)
{
if(fX_norm_now>fX_norm) break;
}
fX_norm=fX_norm_now;
//update curr_exp_arr:
for (i=0;i<NUM_NODES;++i) {
curr_exp_arr[i]=next_exp_arr[i];
}
iter_count+=1;
}
//copy the expression to exp_arr for returning:
for (i=0;i<NUM_NODES;i++){
exp_arr[i]=next_exp_arr[i];
}
//for (int j=0;j<NUM_NODES;j++) printf("%f\t",fX_arr[j]);
//printf("\n");
return;
}