/
SDDS.pl
357 lines (245 loc) · 8.73 KB
/
SDDS.pl
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
# Authors: Seda Arat & David Murrugarra
# Name: Script for Stochastic Discrete Dynamical Systems (SDDS)
# Revision Date: August 19, 2014
#!/usr/bin/perl
use strict;
use warnings;
############################################################
###### REQUIRED PERL MODULES before running the code #######
############################################################
use Getopt::Euclid;
use JSON::Parse;
use JSON;
# use Data::Dumper;
############################################################
=head1 NAME
perl SDDS.pl - Simulate a stochastic model from a possible initialization.
=head1 USAGE
perl SDDS.pl -m <model-file> -s <simulation-file> -o <output-file>
=head1 SYNOPSIS
perl SDDS.pl -m <model-file> -s <simulation-file> -o <output-file>
=head1 DESCRIPTION
SDDS.pl - Simulate a stochastic model from a possible initialization.
=head1 REQUIRED ARGUMENTS
=over
=item -m[odel-file] <model-file>
The JSON file containing the model information (.json).
=for Euclid:
network-file.type: readable
=item -s[imulation-file] <simulation-file>
The JSON file containing the simulation information that the user has been specified (.json).
=for Euclid:
network-file.type: readable
=item -o[utput-file] <output-file>
The JSON file containing the average trajectories of all variables.
=for Euclid:
file.type: writable
=back
=head1 AUTHOR
Seda Arat
=cut
# it is for random number generator
srand ();
# inputs
my $modelFile = $ARGV{'-m'};
my $simulationFile = $ARGV{'-s'};
# output(s)
my $outputFile = $ARGV{'-o'};
# upper limits
my $max_num_simulations = 10**6;
my $max_num_interestingVariables = 10;
my $max_num_steps = 100;
# converts Model.json to Perl format
my $model = JSON::Parse::json_file_to_perl ($modelFile);
# converts Simulation.json to Perl format
my $simulation = JSON::Parse::json_file_to_perl ($simulationFile);
# sets the update rules/functions (hash)
my $updateFunctions = $model->{'model'}->{'updateRules'};
my $num_functions = scalar values %$updateFunctions;
# sets the number of variables in the model (array)
my $variables = $model->{'model'}->{'variables'};
my $num_variables = scalar @$variables;
# sets the unified (maximum prime) number that each state can take values up to (scalar)
my $num_states = $simulation->{'simulation'}->{'numberofStates'};
# sets the number of simulations that the user has specified (scalar)
my $num_simulations = $simulation->{'simulation'}->{'numberofSimulations'};
# sets the number of steps that the user has specified (scalar)
my $num_steps = $simulation->{'simulation'}->{'numberofTimeSteps'};
# sets the initial states that the user has specified for simulations (array)
my $initialState = $simulation->{'simulation'}->{'initialState'};
# sets the variables of interest that the user has specified for plots (array)
my $interestingVariables = $simulation->{'simulation'}->{'variablesofInterest'};
my $num_interestingVariables = scalar @$interestingVariables;
# sets the propensities (hash);
my $propensities = $simulation->{'simulation'}->{'propensities'};
my $num_propensities = scalar values %$propensities;
error_checking ();
my $allTrajectories = get_allTrajectories ();
my $averageTrajectories = get_averageTrajectories ();
# print Dumper ($allTrajectories);
# print ("\n*********************************\n");
# print Dumper ($averageTrajectories);
my $json = JSON->new->indent ();
open (OUT," > $outputFile") or die ("<br>ERROR: Cannot open the file for output. <br>");
print OUT $json->encode ($averageTrajectories);
close (OUT) or die ("<br>ERROR: Cannot close the file for output. <br>");
exit;
############################################################
############################################################
####################### SUBROUTINES ########################
############################################################
=pod
error_checking ();
Checks if the user enters the options/parameters correctly
=cut
sub error_checking {
# num_functions, num_variables and num_propensities
unless ($num_functions == $num_variables) {
print ("<br>INTERNAL ERROR: The number of variables, $num_variables, must be equal to the number of update rules, $num_functions. <br>");
exit;
}
unless ($num_variables == $num_propensities) {
print ("<br>ERROR: There must be propensity entries for $num_variables variables. It seems there are propensity entries for $num_propensities variables. <br>");
exit;
}
unless ($num_variables == scalar @$initialState) {
print ("<br>ERROR: There must be $num_variables variables in the initial state. Please check the initial state entry. <br>");
exit;
}
# num_simulations
if (isnot_number ($num_simulations) || $num_simulations < 1 || $num_simulations > $max_num_simulations) {
print ("<br>ERROR: The number of simulations must be a number between 1 and $max_num_simulations. <br>");
exit;
}
# num_steps
if (isnot_number ($num_steps) || $num_steps < 1 || $num_steps > $max_num_steps) {
print ("<br>ERROR: The number of steps must be a number between 1 and $max_num_steps. <br>");
exit;
}
# propensities
foreach my $v (values %$propensities) {
unless (($v->{"activation"} >= 0) && ($v->{"activation"} <= 1)) {
print ("<br>ERROR: The activation propensities for stochastic simulations must be a number between 0 and 1. <br>");
exit;
}
unless (($v->{"degradation"} >= 0) && ($v->{"degradation"} <= 1)) {
print ("<br>ERROR: The degradation propensities for stochastic simulations must be a number between 0 and 1. <br>");
exit;
}
}
}
############################################################
=pod
isnot_number ($n);
Returns true if the input is not a number, false otherwise
=cut
sub isnot_number {
my $n = shift;
if ($n =~ m/\D/) {
return 1;
}
else {
return 0;
}
}
############################################################
=pod
get_allTrajectories ();
Stores all trajectories into a hash table whose keys are the order of
the trajectories and the values are the trajectories at the initial state
and length is num_steps+1.
Returns a reference of the all trajectories hash.
=cut
sub get_allTrajectories {
my %alltrajectories = ();
for (my $i = 1; $i <= $num_simulations; $i++) {
my %table = ();
my @is = @$initialState;
for (my $k = 1; $k <= $num_variables; $k++) {
push (@{$table{"x$k"}}, $is[$k - 1]);
}
for (my $j = 1; $j <= $num_steps; $j++) {
my @ns = @{get_nextstate_stoch (\@is)};
for (my $r = 1; $r <= $num_variables; $r++) {
push (@{$table{"x$r"}}, $ns[$r - 1]);
}
@is = @ns;
}
$alltrajectories{$i} = \%table;
}
return \%alltrajectories;
}
############################################################
=pod
get_nextstate_stoch ($state);
Returns the next state (as a reference of an array) of a given state
(as a reference of an array) using update functions and propensity parameters.
=cut
sub get_nextstate_stoch {
my $state = shift;
my $z = get_nextstate_det ($state);
my @nextsstateStoch;
for (my $j = 0; $j < $num_variables; $j++) {
my $r = rand ();
my $i = $j + 1;
my $prop = $propensities->{"x$i"};
if ($state->[$j] < $z->[$j]) {
if ($r < $prop->{"activation"}) {
$nextsstateStoch[$j] = $z->[$j];
}
else{
$nextsstateStoch[$j] = $state->[$j];
}
}
elsif ($state->[$j] > $z->[$j]) {
if ($r < $prop->{"degradation"}) {
$nextsstateStoch[$j] = $z->[$j];
}
else{
$nextsstateStoch[$j] = $state->[$j];
}
}
else {
$nextsstateStoch[$j] = $state->[$j];
}
}
return \@nextsstateStoch;
}
############################################################
=pod
get_nextstate_det ($state);
Returns the next state (as a reference of an array) of a given state using
update functions.
=cut
sub get_nextstate_det {
my $state = shift;
my @nextState;
for (my $i = 1; $i <= @$state; $i++) {
my $func = $updateFunctions->{"x$i"}->{"polynomialFunction"};
for (my $j = 1; $j <= @$state; $j++) {
$func =~ s/x[$j]/\($state->[$j - 1]\)/g;
}
$nextState[$i - 1] = eval ($func) % $num_states;
}
return \@nextState;
}
############################################################
=pod
get_averageTrajectories ();
Stores average trajectories of all variables into a hash.
Returns a reference of average trajectory hash.
=cut
sub get_averageTrajectories {
my %averagetrajectories = ();
for (my $v = 1; $v <= $num_variables; $v++) {
for (my $t = 0; $t <= $num_steps; $t++) {
my $sum = 0;
for (my $s = 1; $s <= $num_simulations; $s++) {
$sum += $allTrajectories->{$s}->{"x$v"}->[$t];
}
$averagetrajectories{"x$v"}[$t] = $sum / $num_simulations;
}
}
return \%averagetrajectories;
}
############################################################