-
Notifications
You must be signed in to change notification settings - Fork 3
/
BioNetFit.pl
executable file
·5821 lines (4633 loc) · 177 KB
/
BioNetFit.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
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
#!/usr/bin/perl -w
# Copyright 2014 Translational Genomics Research Institute
# This program is free software: you can redistribute it and/or modifyd
# 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/>.
# You can contact the developer by email at [email protected]
use strict;
my $version = "1.0";
# Automatically reap forks that aren't done so with waitpid().
# Currently this in place to kill fork killer forks. Ideally
# they would be killed with a stopping waitpid(), however.
$SIG{CHLD} = 'IGNORE';
use Data::Dumper;
use Cwd qw(getcwd chdir abs_path);
use Config;
use File::Path;
use File::Copy qw(copy move);
use File::Path qw/mkpath rmtree/;
use File::Basename qw(fileparse);
use POSIX qw(floor ceil);
use POSIX qw( WNOHANG ); # In case we want to use a non-blocking waitpid()
use Safe;
use List::Util qw(sum max min);
use Scalar::Util qw(looks_like_number);
# Check to see if local machine has the libraries needed to produce graphical output. The conditional is further down.
eval {
require GD::Graph::area;
require GD::Graph::lines;
require GD::Graph::mixed;
GD::Graph::area->import();
GD::Graph::lines->import();
GD::Graph::mixed->import();
};
sub print_usage();
sub parse_file(@);
sub update_values($);
sub create_pbs_command($$$$);
sub verbose_mkdir($);
sub calculate_var_sets($$);
sub run_models_pbs($$$);
sub run_models_fork($$);
sub generate_model_set_files($$$$$);
sub load_exp($$);
sub load_data($);
sub store_factors($);
sub store_newcolumns($);
sub create_columns($$$$$$);
sub get_model_params($$);
sub pick_weighted($$$);
sub store_mutate($);
sub read_summary($);
sub run_generation($$$$$);
sub run_analyze($$$$);
sub run_genetic($$$$$);
sub create_new_config($$$$$$);
sub load_all_summaries($);
sub run_consolidate($$$);
sub consolidate($$@);
sub run_submit($$$);
sub run_resume($$);
sub zero_rand($);
sub rlnorm($$);
sub empty_sort(@);
sub write_all_summary($$$);
sub numalpha_sort;
sub combine_columns($$$@);
sub welcome_message($);
sub finished($$$);
sub graph($);
sub walltime_watch($$);
sub smooth_runs($$);
sub get_arch_info($);
sub bootstrap_run($$$);
sub show_output;
sub generate_bootstrap_file($);
sub run_monitor($);
sub check_queue($$);
# Get our root directory
my ( $filename_junk, $medir ) = fileparse( abs_path($0) );
# Path to our PBS submission script
my $pbs_generic_script = "${medir}BioNetFit.pbs";
my $verbosity; # Terminal output verbosity
my $tolerance = 1e-6; # Tolerance to round off in time comparison
my $max_retry_different_parents = 100; # Maximum number of times we will try to select different parents during breeding
my $username = $ENV{USER}; # For default email address.
my $qsub = 0; # Not using qsub by default
# Our main program commands. These are submitted on the command line as arguments to the script
my $submit_command = 'submit';
my $resume_command = 'resume';
my $generation_command = 'generation';
my $genetic_command = 'genetic';
my $analyze_command = 'analyze';
my $consolidate_command = 'results';
my $rlnorm_test = 'rlnorm';
my $monitor_command = 'monitor';
my $finish_command = 'results';
# Make sure we always know where we started
my $top_directory = getcwd;
# Used later on to store file and path names
my @filenames = qw(bng_command model exp_file net_file);
my @pathnames = qw(output_dir);
# Parameters that are used to assign model variables.
# These parameters will not be included when outputting new config file
# after running genetic algorithm because they are only used to generate the 1st
# generation's config files
my @var_variables = qw(substitute_var fixed_var list_var linear_var log_var lognormrandom_var lograndom_var random_var static_list_var loguniform_var);
# Parameters that are assigned a list of values.
# These parameters will not be included when outputting new config file
# after running genetic algorithm because variables are being set specifically.
my @list_variables = qw(newcolumn factor mutate exp_file scan_parameter);
# var_variables and sensitivity_variables are list variables as well.
push @list_variables, @var_variables;
# Parameters that should not be passed to pbs
my @skip_variables = qw(pbs_mail_options email job_name walltime analyze mail_when_analyzed job_sleep generation max_generations default_mutate skip_genetic cluster_parallel);
# Required variables.
my @required_variables = qw(job_name model bng_command max_generations permutations exp_file output_dir );
# Variable defaults.
my %var_defaults = (
parallel_count => 2,
job_sleep => 1,
force_different_parents => 1,
mail_when_aborted => "no",
mail_when_begins => "no",
mail_when_terminates => "no",
show_welcome_message => 1,
ask_create => 1,
ask_overwrite => 1,
skip_genetic => 0,
stop_when_stalled => 1,
verbosity => 1,
make_plots => 0,
max_retries => 3,
retry_count => 0,
swap_rate => 0.5,
use_cluster => 0,
min_objfunc_value => 0,
smoothing => 1,
extra_weight => 0,
keep_parents => 0,
run_job_from_worknode => 1,
objfunc => 1,
delete_old_files => 1,
bootstrap => 0,
bootstrap_num => 1,
save_cluster_output => 0,
bootstrap_retries => 3,
bootstrap_retry => 0,
first_gen_permutations => 0,
multisim => 1,
job_limit => 5000,
job_counter => 0,
max_walltime => "08:00:00",
walltime_limit => "240:00:00",
cluster_parallel => 4,
norm_to_max => 0,
);
#############################################################
### Start of main program
#############################################################
{
# Get our subcommand from the command line
my $subcommand;
my $new_max_generations;
if ( 2 == @ARGV ) {
$subcommand = shift @ARGV;
}
elsif ( 1 == @ARGV ) {
$subcommand = $submit_command;
}
elsif ( 3 == @ARGV ) {
$subcommand = shift @ARGV;
$new_max_generations = shift @ARGV;
}
else {
print_usage();
}
# Store path to our original .conf file
my ($orig_config_file) = @ARGV;
# Make sure we're using an absolute path for the config file
$orig_config_file = abs_path($orig_config_file);
unless ( -f $orig_config_file ) {
die "BioNetFit couldn't find the .conf file you specified. Please check that the path and filename to the .conf file is correct\n";
}
# Read settings from configuration file(s).
my @config_filenames;
push @config_filenames, $orig_config_file;
# Need to change to the BioNetFit directory in order to parse relative paths in .conf file.
# Save CWD so we can get back after .conf parsing
chdir $medir;
# Create our $values_ref from the config file(s). $values_ref is used throughout the script.
my $values_ref = parse_file(@config_filenames);
# Create defaults and modify values where appropriate.
update_values($values_ref);
# Chang back to cwd
chdir $top_directory;
# $qsub can be used anywhere in the script to know if we are on a cluster or not. It is used primarily to store the output filename for script output when
# running BioNetFit on a cluster.
if ( $values_ref->{use_cluster} ) {
$qsub = "$medir/$values_ref->{job_name}";
}
# If we are bootstrapping, let's save our original config for later use
$values_ref->{orig_config} = $orig_config_file
if ( $values_ref->{bootstrap} && !exists $values_ref->{generation} );
# Set global verbosity
$verbosity = $values_ref->{verbosity};
# If we did not find graphing modules ($@ is set) but user wants to make graphs, tell them they can't.
if ( $@ && $values_ref->{make_plots} ) {
print "You've set make_plots to true but we can't find the required Perl modules. Please see the manual for instructions on installing these modules.\n"
if ( $subcommand eq $submit_command );
$values_ref->{make_plots} = 0;
}
# Initialize random number seed if seed is given.
srand( $values_ref->{seed} )
if ( $values_ref->{seed} );
# Be sure our model(s) end in .txt or .bngl
my ($root) = $values_ref->{model} =~ /(.*)\.txt/;
($root) = $values_ref->{model} =~ /(.*)\.bngl/ unless ($root);
die "Error: Unrecognized model name $values_ref->{model}, needs .txt or .bngl extension.\n"
unless ($root);
# If $values_ref->{generation} exists, it means we're PAST the first generation
unless ( exists $values_ref->{generation} ) {
if ( $values_ref->{max_generations} > 0 ) {
# If multiple generations, start with 1.
$values_ref->{generation} = 1;
}
else {
# Not doing multiple generations, so start with 0.
$values_ref->{generation} = 0;
}
}
print "Getting architecture information...\n"
if ( $verbosity >= 3 );
# Get architecture information
get_arch_info($values_ref)
if ( !exists $values_ref->{command_success} && $subcommand ne $monitor_command );
if ( $values_ref->{generation} == 1 || $subcommand eq $consolidate_command ) {
my $model_full = $values_ref->{model};
# Check too see if model file exists
die "Error: Unable to find a model file. Please be sure all 'model' files in your .conf file exist.\n\n"
unless ( -f $model_full );
my ( $model, $path, $suffix );
( $model, $path, $suffix ) = fileparse( $model_full, qr/\.[^\.]*$/ );
$model = "$path$model";
my @exp_names;
# Here we convert the .exp to unix-style line endings.
# Read lines into array, first converting line endings
foreach my $exp_file ( @{ $values_ref->{exp_file} } ) {
my ( $basename, $path, $ext ) = fileparse( $exp_file, qr/\.[^\.]*$/ );
push @exp_names, $basename;
my $changes_made = 0;
die "Error: Unable to find the .exp file: $exp_file\n"
unless ( -f $exp_file );
open EXPFILE, '<', $exp_file
or show_output "die", "Error: Couldn't open .exp file for unix conversion.";
my @exp_old;
while ( my $line = <EXPFILE> ) {
my $oldline = $line;
$line =~ s/[\x0A\x0D]+/\n/g;
$changes_made = 1
if ( $oldline ne $line );
push @exp_old, $line;
}
close EXPFILE;
# Only output new .exp if we changed anything
if ($changes_made) {
# Now output the converted file
open EXPFILE, '>', $exp_file
or show_output "die", "Error: Couldn't open .exp file for unix conversion.";
foreach (@exp_old) {
print EXPFILE $_;
}
close EXPFILE;
}
}
open MODELFILE, '<', $model_full
or show_output "die", "Error: Couldn't open model file for consistency checks.";
my @orig_model = <MODELFILE>;
close MODELFILE;
my @new_model;
# Replace line break delimiters, putting split lines onto the same line
for ( my $i = 0 ; $i < @orig_model ; $i++ ) {
if ( $orig_model[$i] =~ /\\/ ) {
$orig_model[$i] =~ s/\\\W*//;
$orig_model[$i] = $orig_model[$i] . $orig_model[ $i + 1 ];
push @new_model, $orig_model[$i] unless $orig_model[$i] =~ /\\/;
$i++;
if ( $orig_model[ $i - 1 ] =~ /\\/ ) {
$orig_model[ $i - 1 ] =~ s/\\\W*//;
$orig_model[ $i - 1 ] = $orig_model[ $i - 1 ] . $orig_model[ $i + 1 ];
push @new_model, $orig_model[ $i - 1 ] unless $orig_model[ $i - 1 ] =~ /\\/;
$i++;
if ( $orig_model[ $i - 2 ] =~ /\\/ ) {
$orig_model[ $i - 2 ] =~ s/\\\W*//;
$orig_model[ $i - 2 ] = $orig_model[ $i - 2 ] . $orig_model[ $i + 1 ];
push @new_model, $orig_model[ $i - 2 ] unless $orig_model[ $i - 2 ] =~ /\\/;
$i++;
if ( $orig_model[ $i - 3 ] =~ /\\/ ) {
$orig_model[ $i - 3 ] =~ s/\\\W*//;
$orig_model[ $i - 3 ] = $orig_model[ $i - 3 ] . $orig_model[ $i + 1 ];
push @new_model, $orig_model[ $i - 3 ] unless $orig_model[ $i - 3 ] =~ /\\/;
$i++;
if ( $orig_model[ $i - 4 ] =~ /\\/ ) {
$orig_model[ $i - 4 ] =~ s/\\\W*//;
$orig_model[ $i - 4 ] = $orig_model[ $i - 4 ] . $orig_model[ $i + 1 ];
push @new_model, $orig_model[ $i - 4 ];
$i++;
}
}
}
}
}
else {
push @new_model, $orig_model[$i];
}
}
my @suffix_lines;
my @scan_parameter_lines;
# Store any model lines containing suffixes or scan parameters. Also store whether
# we are using an ode solver
print "\n";
for (@new_model) {
next if /^#/;
#print $_;
if (/suffix=>(“|’|”|’|"|')\w+(“|’|”|’|"|')/i) {
push @suffix_lines, $_;
#print 2;
}
if (/parameter_scan/i) {
push @scan_parameter_lines, $_;
#print $_;
}
$values_ref->{ode} = 1
if ( /^\s*.+method=>(“|’|”|’|")ode(“|’|”|’|")/i || /^simulate_ode/i );
$values_ref->{ssa} = 1
if ( /^\s*.+method=>(“|’|”|’|")ssa(“|’|”|’|")/i || /^simulate_ssa/i );
die "Error: It looks like you have a prefix specified in your model action command. This causes BioNetFit to get confused. Please use only suffixes, which must correspond to .exp filenames.\n\n"
if (/prefix=>(“|’|”|’|")\w+(“|’|”|’|")/i);
}
$values_ref->{ode} = 0 unless $values_ref->{ode};
my $num_name_matches = 0;
foreach my $suffix_line (@suffix_lines) {
for (@exp_names) {
#print $suffix_line;
if ( $suffix_line =~ /suffix=>(“|’|”|’|")$_(“|’|”|’|")/ ){
$num_name_matches += 1;
#print 1;
}
}
}
my $need_to_die = 0;
# See if our exp filenames have matching suffixes specified in model file
if ( $num_name_matches != @exp_names ) {
#print Dumper @exp_names;
print "\nError: It looks like the suffixes in your model action commands do not match your .exp filenames. Please be sure that they match, For example, for every .exp file used with name: \"myData_A.exp\" there should be a corresponding suffix in your model command:\n\t\"simulate({suffix=>myData_A\",...});\n\n";
$need_to_die = 1;
}
my @scan_suffixes;
my @scan_parameters;
for (@scan_parameter_lines) {
if (/suffix=>/) {
my ( $junk, $suffix, $morejunk ) = $_ =~ m/suffix=>(“|’|”|’|")(\w+)(“|’|”|’|")/g;
push @scan_suffixes, $suffix;
}
if (/parameter=>/) {
my ( $evenmorejunk, $parameter, $wowthatsalotofjunk ) = $_ =~ m/parameter=>(“|’|”|’|")(\w+)(“|’|”|’|")/g;
push @scan_parameters, $parameter;
}
}
unless ( scalar(@scan_suffixes) == scalar(@scan_parameters) ) {
print "\nError: It looks like your parameter_scan commands don't contain both a suffix and parameter. Please see the BioNetFit documentation\n\n";
$need_to_die = 1;
}
die
if $need_to_die;
for ( my $i = 0 ; $i < @scan_suffixes ; $i++ ) {
#print $scan_suffixes[$i];
$values_ref->{scan_parameter}[$i] = "$scan_suffixes[$i] $scan_parameters[$i]";
}
}
# If user didn't tell us what type of cluster they're using, let's figure it out ourselves
if ( $values_ref->{use_cluster} && !defined $values_ref->{cluster_software} && $subcommand ne $monitor_command ) {
print "Figuring out what type of cluster we're on...\n"
if ( $verbosity >= 3 );
my @slurm_tests;
my @torque_tests;
my @ge_tests;
push @slurm_tests, (`which sbatch 2> /dev/null`);
if (@slurm_tests) {
$values_ref->{cluster_software} = "slurm";
$values_ref->{run_job_from_worknode} = 1;
}
# If any of these binaries exist, we're using torque
push @torque_tests, ( `which maui 2> /dev/null`, `which moab 2> /dev/null` ) unless ( $values_ref->{cluster_software} );
if (@torque_tests) {
$values_ref->{cluster_software} = "torque";
$values_ref->{run_job_from_worknode} = 1;
}
# If any of these binaries exist, we're using grid engine
push @ge_tests, ( `which sge_execd 2> /dev/null`, `which qconf 2> /dev/null`, `which qmon 2> /dev/null`, `which qhost 2> /dev/null` ) unless ( $values_ref->{cluster_software} );
if (@ge_tests) {
$values_ref->{cluster_software} = "ge";
$values_ref->{run_job_from_worknode} = 0;
die "Error: You since you're using an SGE-based cluster, you must specify a parallel environment using the 'pe_name' option in your .conf file\n"
unless $values_ref->{pe_name};
}
# If we can't figure it out, ask the user
if ( !defined $values_ref->{cluster_software} ) {
my $answer;
do {
print "BioNetFit was unable to determine if your cluster runs SLURM, TORQUE or Grid Engine software. Please enter (S) for SLURM, (T) for a torque-based cluster, or (G) for a grid-engine based cluster. If you are unsure, try (T). You can set this option permanently using 'cluster_software={torque,ge} in your .conf file. ";
$answer = <STDIN>;
chomp $answer;
} until ( ( $answer eq 'T' ) || ( $answer eq 't' ) || ( $answer eq 'G' ) || ( $answer eq 'g' ) || ( $answer eq 'S' ) || ( $answer eq 's' ) );
if ( $answer eq "s" || $answer eq "S" ) {
$values_ref->{cluster_software} = "slurm";
$values_ref->{run_job_from_worknode} = 1;
}
elsif ( $answer eq "T" || $answer eq "t" ) {
$values_ref->{cluster_software} = "torque";
$values_ref->{run_job_from_worknode} = 1;
}
else {
$values_ref->{cluster_software} = "ge";
$values_ref->{run_job_from_worknode} = 0;
}
}
# If we're using torque, check to see if we're using maui or moab. This is necessary because our CPU requirement directives
# are different for maui.
if ( $values_ref->{cluster_software} eq "torque" ) {
my $maui_test = `which maui &> /dev/null`;
$values_ref->{maui} = 1
unless $maui_test =~ / no maui /;
}
}
# Choose the correct submission command based on cluster software
if ( $values_ref->{use_cluster} && $subcommand ne $monitor_command ) {
if ( $values_ref->{cluster_software} eq "slurm" ) {
$values_ref->{cluster_command} = "sbatch";
}
else {
$values_ref->{cluster_command} = "qsub";
}
}
# Add ending slashes to output directory if user failed to do it
if ( substr( $values_ref->{output_dir}, -1, 1 ) ne "/" ) {
$values_ref->{output_dir} .= "\/";
}
my $current_directory = $values_ref->{output_dir};
my $previous_directory = $values_ref->{output_dir};
my $next_directory;
$current_directory .= $values_ref->{job_name};
$previous_directory .= $values_ref->{job_name};
$next_directory = $current_directory;
$current_directory .= "/$values_ref->{generation}";
my $gennum = $values_ref->{generation};
$next_directory .= "/" . ( $values_ref->{generation} + 1 );
# Get our directory vars right for the current generation
if ( $values_ref->{generation} && ( $values_ref->{generation} > 1 ) ) {
$previous_directory .= "/" . ( $values_ref->{generation} - 1 );
}
else {
$previous_directory = "";
}
# Remove path from config file if it is present.
my $stripped_config_file = $orig_config_file;
my $outdir = $values_ref->{output_dir};
chop($outdir);
$stripped_config_file =~ s/$values_ref->{job_name}(\/[\d]+)?\///;
$stripped_config_file =~ s/$outdir(\/[\d]+)?\///;
if ($subcommand) {
# If we're submitting a job (the default command)
if ( $subcommand eq $submit_command ) {
print "Doing configuration consistency checks and making sure all required files are in place...\n"
if ( $verbosity >= 3 );
my @exp_files = @{ $values_ref->{exp_file} };
# Load exp file so we can check for formatting errors
load_exp( \@exp_files, $values_ref );
# If this is truly our first run, and not a bootstrap re-run
if ( -f ".lock_$values_ref->{job_name}" && $values_ref->{ask_overwrite} ) {
my $answer;
do {
print "", "You're trying to overwrite a job which might still be in progress. Do you really want to do this? ";
$answer = <STDIN>;
chomp $answer;
} until ( ( $answer eq 'y' ) || ( $answer eq 'n' ) || ( $answer eq 'Y' ) || ( $answer eq 'N' ) );
die "Please change your job name and/or starting directory.\n"
if ( ( $answer eq 'N' ) || ( $answer eq 'n' ) );
unlink(".lock_$values_ref->{job_name}");
}
if ( $values_ref->{first_gen_permutations} ) {
if ( $values_ref->{first_gen_permutations} <= $values_ref->{permutations} ) {
die "Error: You set 'first_gen_permutations' lower than 'permutations' in your .conf file but 'first_gen_permutations' must be higher than 'permutations'. Please comment or increase 'first_gen_permutations' in your .conf file.\n";
}
}
if ( $values_ref->{use_cluster} ) {
my $perms = $values_ref->{permutations};
$perms = $values_ref->{first_gen_permutations} if ( $values_ref->{first_gen_permutations} );
my $estimated_job_count = ( ( $perms * $values_ref->{smoothing} ) / ( $values_ref->{cluster_parallel} * $values_ref->{multisim} ) + 2 );
if ( $estimated_job_count >= $values_ref->{job_limit} ) {
die "Error: The current values of 'job_limit' is lower than the anticipated number of jobs needed ($estimated_job_count). Please adjust 'job_limit', 'permutations', 'smoothing', 'cluster_parallel', and/or 'multisim' to fix this\n";
}
}
# Be sure bootstrap_chi is set if we are bootstrapping
die "Error: You told BioNetFit to bootstrap but did not set 'bootstrap_chi' in your .conf file. Please turn off bootstrapping or set bootstrap_chi.\n"
if ( $values_ref->{bootstrap} && !defined $values_ref->{bootstrap_chi} );
die "Error: You told BioNetFit to email about job status, but didn't supply an email address in your .conf file. Please do this using the 'email' option.\n"
if ( ( $values_ref->{email_after_generation} || $values_ref->{email_after_run} || $values_ref->{email_on_abort} ) && !defined $values_ref->{email} );
# Make sure all the required files are in place.
warn "Can't find BioNetFit.pbs. This will prevent BioNetFit from running on a cluster.\n"
unless ( -f $pbs_generic_script );
# Check to see if BNG2.pl exists
die "Error: Unable to find BioNetGen executable. Please fix 'bng_command' in your .conf file\n\n"
unless ( -f $values_ref->{bng_command} );
# Check to see if qsub exists, but only if the user provided a full path
if ( $values_ref->{use_cluster} && ( fileparse( $values_ref->{cluster_command} ) ne $values_ref->{cluster_command} ) ) {
die "Error: Unable to find Qsub executable. Please fix 'cluster_command' in your .conf file\n\n"
unless ( -f $values_ref->{cluster_command} );
}
# If our output directory already exists, see if we should overwrite it.
if ( -d "$values_ref->{output_dir}$values_ref->{job_name}" ) {
if ( $values_ref->{ask_overwrite} ) {
my $answer;
do {
print "", "Directory $values_ref->{output_dir}$values_ref->{job_name} already exists. Overwrite? ";
$answer = <STDIN>;
chomp $answer;
} until ( ( $answer eq 'y' ) || ( $answer eq 'n' ) || ( $answer eq 'Y' ) || ( $answer eq 'N' ) );
die "Please change your job name and/or starting directory.\n"
if ( ( $answer eq 'N' ) || ( $answer eq 'n' ) );
}
if ( $verbosity >= 2 && $values_ref->{bootstrap_num} < 2 ) {
print "Deleting old files. This may take a minute.\n";
}
elsif ( $verbosity >= 2 && $values_ref->{bootstrap_num} >= 2 ) {
show_output "Deleting old files. This may take a minute.\n";
}
# Deleting a directory full of many files can take a long time. It's quicker to use a system command to move the directory,
# then fork a process that will stay to delete everything in it.
verbose_mkdir("$values_ref->{output_dir}/to_be_deleted")
unless ( -d "$values_ref->{output_dir}/to_be_deleted" );
my $command = "mv $values_ref->{output_dir}/$values_ref->{job_name} $values_ref->{output_dir}/to_be_deleted";
$command .= "&& mv values_ref->{output_dir}/$values_ref->{job_name}_cluster_output $values_ref->{output_dir}/to_be_deleted"
if ( $values_ref->{use_cluster} && $values_ref->{bootstrap_num} == 1 && -d "$values_ref->{output_dir}$values_ref->{job_name}_cluster_output" && $values_ref->{save_cluster_output} );
if ( -d "$values_ref->{output_dir}/to_be_deleted/$values_ref->{job_name}" ) {
my $pid = fork();
if ( $pid == 0 ) {
# Remove the old output directory
rmtree("$values_ref->{output_dir}/to_be_deleted")
or warn "Warning: Could not delete $values_ref->{output_dir}$values_ref->{job_name}. This may result in errors when running BioNetFit. Please be sure none of your old output files are currently in use or opened. If using qsub, check qstat -u [username] to see if you have any jobs still running.\n";
exit;
}
waitpid( $pid, WNOHANG );
}
else {
rmtree("$values_ref->{output_dir}/$values_ref->{job_name}");
rmtree("$values_ref->{output_dir}/$values_ref->{job_name}_cluster_output")
if ( $values_ref->{use_cluster} && $values_ref->{bootstrap_num} == 1 && -d "$values_ref->{output_dir}$values_ref->{job_name}_cluster_output" && $values_ref->{save_cluster_output} );
}
# Remove old bootstrap directory ( If this truly is our first run, and not a bootstrap run or redo)
if ( $values_ref->{bootstrap} && $values_ref->{bootstrap_num} == 1 && $orig_config_file !~ /bootstrap/ ) {
rmtree("$values_ref->{output_dir}$values_ref->{job_name}_bootstrap")
or warn "Warning: Could not delete $values_ref->{output_dir}$values_ref->{job_name}_bootstrap. This may result in errors when running BioNetFit. Please be sure none of your old output files are currently in use or opened. If using qsub, check qstat -u [username] to see if you have any jobs still running.\n";
}
# Make sure old output directory was actually deleted
if ( -d "$values_ref->{output_dir}$values_ref->{job_name}" ) {
warn "Warning: Could not delete $values_ref->{output_dir}$values_ref->{job_name}. This may result in errors when running BioNetFit. Please be sure none of your old output files are currently in use or opened. If using qsub, check qstat -u [username] to see if you have any jobs still running.\n";
}
}
# Create a new qsub output directory
verbose_mkdir("$values_ref->{output_dir}$values_ref->{job_name}_cluster_output")
if ( $values_ref->{use_cluster} && $values_ref->{bootstrap_num} == 1 && $values_ref->{save_cluster_output} && !-d "$values_ref->{output_dir}$values_ref->{job_name}_cluster_output" );
# Create a new bootstrap output directory
verbose_mkdir("$values_ref->{output_dir}$values_ref->{job_name}_bootstrap")
if ( $values_ref->{bootstrap} && !-d "$values_ref->{output_dir}$values_ref->{job_name}_bootstrap" );
print "Generating new bootstrap map\n"
if ( $verbosity >= 3 );
# Generate our bootstrap map and save to file
generate_bootstrap_file($values_ref)
if ( $values_ref->{bootstrap} );
print "Creating .lock file\n"
if ( $verbosity >= 3 );
open( TMP, ">>.lock_$values_ref->{job_name}" );
close TMP;
show_output "Submitting job...\n"
if ( $verbosity >= 2 );
# Finally, run the submission function
run_submit( $values_ref, $current_directory, $orig_config_file );
}
elsif ( $subcommand eq $resume_command ) {
print "Attemping to resume fitting...\n"
if ( $verbosity >= 1 );
verbose_mkdir( $values_ref->{output_dir} )
unless ( -d $values_ref->{output_dir} );
verbose_mkdir("$values_ref->{output_dir}/$values_ref->job_name}")
unless ( -d "$values_ref->{output_dir}/$values_ref->{job_name}" );
opendir( CUR, "$values_ref->{output_dir}/$values_ref->{job_name}/" )
or die "Can't open dir $values_ref->{output_dir}/$values_ref->{job_name}/: $!\n";
my @files = readdir(CUR);
closedir(CUR);
my @generations;
my $current_config;
my $resume_generation;
for (@files) {
push @generations, $_
if ( looks_like_number($_) );
}
@generations = sort { $b <=> $a } @generations;
my $orig_config;
if ( $values_ref->{bootstrap} ) {
$orig_config = fileparse( $values_ref->{orig_config} );
}
else {
$orig_config = fileparse($orig_config_file);
}
if ( !defined $orig_config ) {
die "Error: The run you're trying to resume doesn't have the data necessary to resume the run. You will need to start over.\n";
}
if (@generations) {
for ( my $i = 0 ; $i < @generations ; $i++ ) {
if ( -f "$values_ref->{output_dir}/$values_ref->{job_name}/$generations[$i]/$orig_config" || -f "$values_ref->{output_dir}/$values_ref->{job_name}/$generations[$i]/bootstrap.conf" ) {
$current_config = "$values_ref->{output_dir}/$values_ref->{job_name}/$generations[$i]/$orig_config";
$current_config = "$values_ref->{output_dir}/$values_ref->{job_name}/$generations[$i]/bootstrap.conf"
unless ( -f $current_config );
$resume_generation = $generations[$i];
last;
}
}
}
if ( $values_ref->{bootstrap} && !defined $current_config ) {
$current_config = "$values_ref->{output_dir}/$values_ref->{job_name}_bootstrap/bootstrap.conf"
if ( -f "$values_ref->{output_dir}/$values_ref->{job_name}_bootstrap/bootstrap.conf" );
$resume_generation = 1;
}
if ( $resume_generation == 1 ) {
$values_ref->{generation} = 1;
}
if ( !defined $current_config ) {
die "Error: The run you're trying to resume doesn't have the data necessary to resume the run. You will need to start over.\n";
}
my $resume_directory = "$values_ref->{output_dir}$values_ref->{job_name}/$resume_generation";
verbose_mkdir($resume_directory)
unless ( -d $current_directory );
my $output = "Found config file. Resuming run from generation $resume_generation.\n";
$output = "Found config file. Resuming run from generation $resume_generation, bootstrap #$values_ref->{bootstrap_num}.\n"
if ( $values_ref->{bootstrap} );
my @config_files = ($current_config);
my $values_ref_new = parse_file(@config_files);
update_values($values_ref_new);
generate_bootstrap_file($values_ref)
if ( $values_ref_new->{bootstrap} && $resume_generation == 1 );
$values_ref_new->{generation} = $resume_generation;
$values_ref_new->{cluster_software} = $values_ref->{cluster_software};
$values_ref_new->{run_job_from_worknode} = $values_ref->{run_job_from_worknode};
$values_ref_new->{max_generations} = $new_max_generations if $new_max_generations;
if ($new_max_generations) {
open CURR_CONF, '<', $current_config
or show_output "Warning: Can't open file: $current_config to set new max_generations value\n";
my @lines = <CURR_CONF>;
close CURR_CONF;
open CURR_CONF, ">", $current_config
or show_output "die", "Warning: Can't open file $current_config to set new max_generations value\n";
for (@lines) {
s/max_generations \d+/max_generations $new_max_generations/;
print CURR_CONF $_;
}
close CURR_CONF;
$output .= "Setting the new maximum number of generations to $new_max_generations\n";
}
print $output
if ( $verbosity >= 1 );
# Delete files in old target directory (except the config file)
opendir( CUR, "$resume_directory/" ) or show_output "Can't open dir $resume_directory/ to delete files from interrupted run.\n";
@files = readdir(CUR);
closedir(CUR);
print "Deleting old simulation files in $resume_directory\n"
if ( $verbosity >= 2 );
foreach (@files) {
if ( -f "$resume_directory/$_" && $_ !~ /.conf/ ) {
#show_output "Deleting $current_directory/$_...\n";
unlink "$resume_directory/$_" or show_output "Can't delete $resume_directory/$_: $!\n";
}
}
# Delete all generation directories past the generation we are resuming into
opendir( CUR, "$values_ref->{output_dir}$values_ref->{job_name}/" ) or show_output "Can't open $values_ref->{output_dir}$values_ref->{job_name}/ to delete data from interrupted run.\n";
@files = readdir(CUR);
closedir(CUR);
foreach (@files) {
next unless ( looks_like_number($_) && $_ > $resume_generation );
rmtree "$values_ref->{output_dir}$values_ref->{job_name}/$_"
or print "Can't remove $values_ref->{output_dir}/$_. This may cause issues with your resumed run. Be sure none of the files are being used and try again.\n";
}
# Finally, run the submission function
run_resume( $values_ref_new, $current_config );
}
elsif ( $subcommand eq $monitor_command ) {
run_monitor($values_ref);
}
# If we're running the generation command.. (this is run to begin each new generation)
elsif ( $subcommand eq $generation_command ) {
# Create the directory that this generation's output will fall into
verbose_mkdir($current_directory)
unless (-d $current_directory);
# Give the user option to run extra commands before we start the new generation
if ( $values_ref->{command_extra} ) {
system( $values_ref->{command_extra} )
or show_output "Couldn't execute command_extra. Be sure your path and permissions are correct\n";
}
# If user specifies, delete unnecessary files as we go so as to preserve disk space
if ( $values_ref->{delete_old_files} && exists $values_ref->{generation} && $values_ref->{generation} > 1 && ( -d $previous_directory ) ) {
opendir( CUR, "$previous_directory/" )
or show_output "Can't open dir $previous_directory/ to delete previous generation's files.\n";
my @files = readdir(CUR);
closedir(CUR);
for (@files) {
if (/\.BNG_OUT$|\.xml$|\.cdat$|\.finished$|\.species$/) {
unlink "$previous_directory/$_"
or show_output "Couldn't delete $_ from previous directory.\n";
}
elsif (/._\d+\.gdat$/) {
unlink "$previous_directory/$_"
or show_output "Couldn't delete $_ from previous directory.\n";
}
elsif ( -d "$previous_directory/$_" && $_ ne ".." and $_ ne "." ) {
rmtree "$previous_directory/$_";
}
}
if ( $values_ref->{smoothing} > 1 ) {
for ( 0 .. ($values_ref->{smoothing} - 1) ) {
rmtree("$previous_directory/$_")
if ( -d "$previous_directory/$_" );
}
}
}
# And start the generation function
run_generation( $values_ref, $current_directory, $next_directory, $orig_config_file, 0 );
}
# If we're running the genetic command, let's get everything prepped.
elsif ( $subcommand eq $genetic_command ) {
verbose_mkdir($current_directory);
$values_ref->{generation}++;
# Create new configuration file name.
my $old_config = fileparse( ${stripped_config_file} );
my $new_config_file = "$values_ref->{output_dir}$values_ref->{job_name}/" . "$values_ref->{generation}/$old_config";
# And run the genetic function
run_genetic( $values_ref, $current_directory, $previous_directory, $new_config_file, $orig_config_file );
}
# If none of the above commands, let's prepare for output analysis/consolidation
else {
if ( $values_ref->{first_gen_permutations} && $values_ref->{generation} == 1 ) {
$values_ref->{permutations} = $values_ref->{first_gen_permutations};
}
# If we have the analyze command, start analysis function
if ( $subcommand eq $analyze_command ) {
run_analyze( $values_ref, $current_directory, $values_ref->{permutations}, $orig_config_file );
}
elsif ( $subcommand eq $consolidate_command ) {
my $message = "Preliminary results have been placed in $values_ref->{output_dir}$values_ref->{job_name}/Results\n";
run_consolidate( $values_ref, $orig_config_file, $message );
}
}
}
else {
show_output "die", "No subcommand given for $0.\n";
}
exit 0;
}
sub welcome_message($) {
my ($values_ref) = @_;
my $parallel = ( $values_ref->{use_cluster} ) ? $values_ref->{cluster_parallel} : $values_ref->{parallel_count};
# Fill output variables with relevant info
my $max_generations = $values_ref->{max_generations};
my $max_parents = $values_ref->{max_parents};
my $model = $values_ref->{model};
my $walltime = $values_ref->{max_walltime}
if ( exists $values_ref->{max_walltime} );
my $jobpath = "$values_ref->{output_dir}$values_ref->{job_name}";
my $make_plots = ( $values_ref->{make_plots} ) ? "Yes" : "No";
my $smoothing = ( $values_ref->{smoothing} > 1 ) ? "$values_ref->{smoothing} runs" : "No";
#my $bootstrapping = ($values_ref->{bootstrap}) ? "$values_ref->{bootstrap} times" : "No";
# And output
show_output "\n===========================================================\n";
show_output "Running BioNetFit $version with the following options:\n\n";
show_output "Job path: $jobpath\n";
show_output "Model: $model\n";
for ( @{ $values_ref->{exp_file} } ) {
show_output "Data: $_\n";
}
show_output "Max walltime: $walltime\n"
if ($walltime);
show_output "Parallel count: $parallel\n";
show_output "Generations: $max_generations\n";
show_output "First Gen Permutations: $values_ref->{first_gen_permutations}\n"
if ( $values_ref->{first_gen_permutations} );
show_output "Permutations: $values_ref->{permutations}\n";
show_output "Multisim: $values_ref->{multisim}\n"
if ( $values_ref->{multisim} > 1 && $values_ref->{use_cluster} );
show_output "Max parents: $max_parents\n";
show_output "Keep $values_ref->{keep_parents} parents\n"
if ( $values_ref->{keep_parents} );
show_output "Smoothing: $smoothing\n";
#show_output "Bootstrapping? $bootstrapping\n";
show_output "Objective function: $values_ref->{objfunc}\n";
show_output "Creating graphs?: $make_plots\n";
my $timestring = "Start timestamp: " . time() . "\n";
show_output $timestring;
show_output "============================================================\n\n";
}
sub run_submit($$$) {
show_output "\n*** Function: run_submit() ***\n"
if ( $verbosity >= 4 );
my ( $values_ref, $current_directory, $orig_config_file ) = @_;
# Just submitted the job, so let's show the welcome message
welcome_message($values_ref)
if ( $values_ref->{show_welcome_message} );
# Create the command to run the next generation
my $command = "perl " . abs_path($0) . " $generation_command $orig_config_file";
# If we're chaining qsub, create the command to run the first generation
if ( $values_ref->{use_cluster} ) {
if ( $values_ref->{run_job_from_worknode} ) {
verbose_mkdir($current_directory);
$command = create_pbs_command( $pbs_generic_script, "G1", $values_ref, { command => $command } );
my $job = submit_qsub_job( $values_ref, $command );
exit 0
if ( $values_ref->{bootstrap} && $values_ref->{bootstrap_num} > 1 );