forked from RuleWorld/bionetgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BNG2.pl
executable file
·252 lines (220 loc) · 8.43 KB
/
BNG2.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
#!/usr/bin/perl
# BioNetGen : rule-based modeling language and simulation platform
#
# Copyright (C) 2006,2009,2012 by
#
# James R. Faeder (faeder at pitt dot edu)
# Justin S. Hogg (justinshogg at gmail dot com)
# Leonard A. Harris (lh64 at cornell dot com)
# John A. P. Sekar (johnarul dot sekar at gmail dot com)
# Jose Juan Tapia
# Arshi Arora
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# pragmas
use strict;
use warnings;
# Perl Modules
use Config;
use File::Spec;
use FindBin;
use Getopt::Long;
use IO::Handle;
# get Perl2 Module directory: look for environment variables BNGPATH or BioNetGenRoot.
# If neither are defined, use RealBin module
use lib File::Spec->catdir( ( exists $ENV{'BNGPATH'}
? $ENV{'BNGPATH'}
: ( exists $ENV{'BioNetGenRoot'}
? $ENV{'BioNetGenRoot'}
: $FindBin::RealBin
)
),
'Perl2'
);
# BNG Modules
use BNGUtils;
use BNGModel;
use Console;
# Set up Signal Handlers..
# Define global variable to store PID of child process.
$::CHILD_PID = undef;
# Get signal names
my $i = 0;
my %SIGNO=();
defined($Config{sig_name}) or die "No signals defined";
foreach my $signame ( split " ", $Config{sig_name} )
{
$SIGNO{$signame} = $i;
$i++;
}
# TERM signal handler: make sure any child processes are shutdown before termination
$SIG{'TERM'} = sub
{
if (defined $::CHILD_PID)
{ # kill off child process
print "\n>>> relaying TERM signal to child with PID: ", $::CHILD_PID, " <<<\n";
kill $SIGNO{"TERM"}, $::CHILD_PID;
}
exit_error( sprintf "BioNetGen received TERM signal (%d)", $SIGNO{"TERM"} );
};
# INT signal handler: make sure any child processes are shutdown before termination
$SIG{'INT'} = sub
{
if (defined $::CHILD_PID)
{ # kill off child process
print "\n>>> relaying INT signal to child with PID: ", $::CHILD_PID, " <<<\n";
kill $SIGNO{"INT"}, $::CHILD_PID;
}
exit_error( sprintf "BioNetGen received TERM signal (%d)", $SIGNO{"INT"} );
};
# Defaults params for File mode
my %default_args = ( 'write_xml' => 0, 'write_mfile' => 0,
'write_SBML' => 0, 'generate_network' => 0,
'skip_actions' => 0, 'action_skip_warn' => 1,
'logging' => 0, 'no_exec' => 0,
'allow_perl' => 0, 'no_nfsim' => 0,
'output_dir' => File::Spec->curdir(),
'no_atomizer' => 0,
'write_autos' => 0, 'write_SBMLmulti' => 0
);
# Default params for Console mode
my %default_args_console = ( 'write_xml' => 0, 'write_mfile' => 0,
'write_SBML' => 0, 'generate_network' => 0,
'skip_actions' => 1, 'action_skip_warn' => 1,
'logging' => 0, 'no_exec' => 0,
'allow_perl' => 0, 'no_nfsim' => 0,
'output_dir' => File::Spec->curdir(),
'no_atomizer' => 0, 'write_autos' => 0,
'write_SBMLmulti' => 0
);
# variables to contain user args
my $console = 0;
my $findbin = '';
my $help = 0;
my $version = 0;
my %user_args = ( 'console' => \$console,
'findbin' => \$findbin,
'help' => \$help,
'version' => \$version
);
# parse command line arguments
GetOptions( \%user_args,
'help|h',
'version|v',
'console',
'findbin=s',
'skip_actions|check',
'no_nfsim|no-nfsim',
'no_atomizer|no-atomizer',
'output_dir|outdir=s',
'logging|log',
'generate_network|netgen',
'write_SBML|sbml',
'write_mfile|mfile',
'write_xml|xml',
'write_SBMLmulti|sbmlmulti',
'write_autos|autos'
)
or die "Error in command line arguments (try: BNG2.pl --help)";
# display help if requested
if ($help)
{
display_help();
exit(0);
}
# display version info
if ($version)
{
printf "BioNetGen version %s\n", BNGversion();
exit(0);
}
# try to find binary
if (not( $findbin eq ''))
{
# exit with value 0 if binary is found, 1 otherwise
exit( BNGModel::findExec($findbin) ? 0 : 1 );
}
if ( $console )
{
# get arguments
my %args = ();
while ( my ($opt,$val) = each %default_args_console )
{
$args{$opt} = exists $user_args{$opt} ? $user_args{$opt} : $val;
}
# check if output directory exists and is writable
unless ( -d $args{output_dir} )
{ send_warning( sprintf "Default output directory '%s' is not a directory.", $args{output_dir}); }
unless ( -w $args{output_dir} )
{ send_warning( sprintf "Not able to write to default output directory '%s'.", $args{output_dir}); }
# start console
my $err = BNGconsole( \%args );
exit_error($err) if ($err);
}
else
{
unless (@ARGV)
{ display_help(); }
# get arguments
my %args = ();
while ( my ($opt,$val) = each %default_args )
{
$args{$opt} = exists $user_args{$opt} ? $user_args{$opt} : $val;
}
# check if output directory exists and is writable
unless ( -d $args{output_dir} )
{ send_warning( sprintf "Default output directory '%s' is not a directory.", $args{output_dir}); }
unless ( -w $args{output_dir} )
{ send_warning( sprintf "Not able to write to default output directory '%s'.", $args{output_dir}); }
# Process any files
while ( my $file = shift @ARGV )
{
# create BNGModel object
my $model = BNGModel->new();
$model->initialize();
$BNGModel::GLOBAL_MODEL = $model;
# set file argument
$args{'file'} = $file;
# read and process Model file
my $err = $model->readFile( \%args );
exit_error($err) if ($err);
# undefine model
%$model = (); undef %$model;
$BNGModel::GLOBAL_MODEL = undef;
}
}
# all done!
exit(0);
# Display Help Menu
sub display_help
{
printf "\nBioNetGen version %s\n", BNGversion();
print "--------------------------------------------------/ HELP MENU /-----\n"
." SYNOPSIS \n"
." process MODEL: BNG2.pl [OPTION]... MODEL... \n"
." start BNG console: BNG2.pl --console \n"
." display help: BNG2.pl -h \n"
." display version: BNG2.pl -v \n"
." \n"
." OPTIONS \n"
." --log write log to file MODEL.log (default is STDOUT) \n"
." --xml write XML output after processing MODEL \n"
." --mfile write MATLAB M-file output after processing MODEL\n"
." --sbml write SBML output after processing MODEL \n"
." --check read MODEL, but do not execute actions \n"
." --outdir PATH change default output path \n"
." \n"
." For more information, visit bionetgen.org \n"
."--------------------------------------------------------------------\n";
}