-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid-crawler.pl
1156 lines (863 loc) · 36.4 KB
/
grid-crawler.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/env perl
###Copyright 2016 Henrik Stranneheim
use Modern::Perl '2014';
use warnings qw( FATAL utf8 );
use autodie;
use v5.18; #Require at least perl 5.18
use utf8; #Allow unicode characters in this script
use open qw( :encoding(UTF-8) :std );
use charnames qw( :full :short );
use Getopt::Long;
use Params::Check qw[check allow last_error];
$Params::Check::PRESERVE_CASE = 1; #Do not convert to lower case
use Time::Piece;
use Cwd;
use File::Path qw(make_path);
use File::Spec::Functions qw(catdir catfile devnull);
use List::Util qw(any);
use IPC::Cmd qw[can_run run];
## Third party module(s)
use YAML;
$YAML::QuoteNumericStrings = 1; #Force numeric values to strings in YAML representation
use Log::Log4perl;
our $USAGE;
BEGIN {
$USAGE =
qq{grid-crawler.pl grid.yaml -s sampleID > outfile.vcf
-s/--sampleIDs SampleIDs to analyze
-p/--positions Search at region (A,B:C,X:Y-Z)
-en/--exclude Exclude sites for which the expression is true
-i/--include Include sites for which the expression is true
-g/--genoType Require one or more hom/het/missing genotype or, if prefixed with "^", exclude sites with hom/het/missing genotypes
-e/environment Execute search via SHELL or SBATCH (defaults to "SHELL")
-pro/--projectID The project ID (mandatory if using SBATCH)
-em/--email E-mail (defaults to "")
-mc/--maximumCores The maximum number of cores per node used in the analysis (defaults to "16")
-qos/--slurmQualityofService SLURM quality of service command in sbatch scripts (defaults to "low")
-sen/--sourceEnvironmentCommand Source environment command in sbatch scripts (defaults to "")
-x/--xargs Use xargs in sbatch for parallel execution (defaults to "1" (=yes))
-od/--outDataDir The data files output directory
-ot/--outputType The output type b: compressed BCF, z: compressed VCF (defaults to "b")
-l/--logFile Log file (defaults to "grid-crawler-log/{date}/{scriptname}_{timestamp}.log")
-h/--help Display this help message
-v/--version Display version
verb/--verbose Verbose
};
}
my ($gridfile, $exclude, $include, $genoType, $outDataDir, $logfile, $verbose);
my ($environment, $outputType) = ("shell", "b");
my @positions;
my @sampleIDs;
my @programs = ("bcftools");
my @sourceEnvironmentCommand;
##Sbatch parameters
my %sbatchParameter;
$sbatchParameter{xargs} = 1;
$sbatchParameter{maximumCores} = 16;
my $logger; #Will hold the logger object
my $gridCrawlerVersion = "0.0.0";
## Add dateTimestamp for later use in log
my $dateTime = localtime;
my $dateTimeStamp = $dateTime->datetime;
my $date = $dateTime->ymd;
my $script = (`basename $0`); #Catches script name
chomp($dateTimeStamp, $date, $script); #Remove \n;
## Enables cmd to print usage help
if(!@ARGV) {
&Help({USAGE => $USAGE,
exitCode => 0,
});
}
else { #Collect potential infile - otherwise read from STDIN
$gridfile = $ARGV[0];
}
###User Options
GetOptions('s|sampleIDs:s' => \@sampleIDs,
'p|positions:s' => \@positions,
'e|exclude:s' => \$exclude,
'i|include:s' => \$include,
'g|genoType:s' => \$genoType,
'en|environment:s' => \$environment,
'pro|projectID:s' => \$sbatchParameter{projectID},
'mc|maximumCores=n' => \$sbatchParameter{maximumCores}, #Per node
'em|email:s' => \$sbatchParameter{email}, #Email adress
'qos|slurmQualityofService=s' => \$sbatchParameter{slurmQualityofService},
'sen|sourceEnvironmentCommand=s{,}' => \@sourceEnvironmentCommand,
'x|xargs=n' => \$sbatchParameter{xargs},
'od|outDataDir:s' => \$outDataDir,
'ot|outputType:s' => \$outputType,
'l|logFile:s' => \$logfile,
'h|help' => sub { say STDOUT $USAGE; exit;}, #Display help text
'v|version' => sub { say STDOUT "\ngrid-crawler.pl ".$gridCrawlerVersion, "\n"; exit;}, #Display version number
'verb|verbose' => \$verbose,
) or &Help({USAGE => $USAGE,
exitCode => 1,
});
## Create log directory, default logfile path, initiate logger object
$logfile = &InitiateLog({logfileRef => \$logfile,
scriptRef => \$script,
dateRef => \$date,
dateTimeStampRef => \$dateTimeStamp,
});
if(! @sampleIDs) {
$logger->fatal("Please provide sampleIDs");
exit 1;
}
$logger->info("Including sampleIDs(s): ".join(", ", @sampleIDs), "\n");
if(@sourceEnvironmentCommand) {
if ($sourceEnvironmentCommand[-1] !~ /\;$/) {
push(@sourceEnvironmentCommand, ";");
}
}
if($environment =~/SBATCH/i) {
## Required sbatch parameters
my @sbatchParameters = ("projectID", "xargs");
foreach my $parameter (@sbatchParameters) {
if ( ! $sbatchParameter{$parameter}) {
$logger->fatal("Please provide ".$parameter." when submitting via sbatch");
exit 1;
}
if ($parameter eq "xargs") {
push(@programs, "xargs");
}
}
## Check email adress format
if ($sbatchParameter{email}) { #Allow no malformed email adress
&CheckEmailAddress({emailRef => \$sbatchParameter{email},
});
}
}
## Check program(s) can run and log version if so. Otherwise exit
&CheckProgram({programsArrayRef => \@programs,
verbose => $verbose,
});
## Set fileending depending on outputType
my $outfileEnding .= ".bcf.gz";
if ($outputType eq "z") {
$outfileEnding = ".vcf.gz"
}
if (! $outDataDir) {
$outDataDir = catfile(cwd(), "gc_analysis", $dateTimeStamp);
}
############
####MAIN####
############
## Create outDataDir
make_path($outDataDir);
## Loads a YAML file into an arbitrary hash and returns it.
my %grid = &LoadYAML({yamlFile => $gridfile,
});
my %path = &SearchGrid({gridHashRef => \%grid,
sampleIDsArrayRef => \@sampleIDs,
});
my %cmd = &BcfToolsViewCmd({pathHashRef => \%path,
positionsArrayRef => \@positions,
outDataDir => $outDataDir,
exclude => $exclude,
include => $include,
genoType => $genoType,
outputType => $outputType,
outfileEnding => $outfileEnding,
});
my @mergecmds = &BcfToolsMergeCmd({pathHashRef => \%path,
outDataDir => $outDataDir,
outputType => $outputType,
outfileEnding => $outfileEnding,
});
&SubmitCommand({pathHashRef => \%path,
cmdHashRef => \%cmd,
sbatchParameterHashRef => \%sbatchParameter,
mergecmdsArrayRef => \@mergecmds,
sourceEnvironmentCommandArrayRef => \@sourceEnvironmentCommand,
environment => $environment,
verbose => $verbose
});
######################
####SubRoutines#######
######################
sub Help {
##Help
##Function : Print help text and exit with supplied exit code
##Returns : ""
##Arguments: $USAGE, $exitCode
## : $USAGE => Help text
## : $exitCode => Exit code
my ($argHashRef) = @_;
## Flatten argument(s)
my $USAGE;
my $exitCode;
my $tmpl = {
USAGE => {required => 1, defined => 1, strict_type => 1, store => \$USAGE},
exitCode => { default => 0, strict_type => 1, store => \$exitCode},
};
check($tmpl, $argHashRef, 1) or die qw[Could not parse arguments!];
say STDOUT $USAGE;
exit $exitCode;
}
sub InitiateLog {
##InitiateLog
##Function : Create log directory, default logfile path, initiate logger object
##Returns : ""
##Arguments: $logfileRef, $scriptRef, $dateRef, $dateTimeStampRef
## : $logfileRef => User supplied info on cmd for logFile option {REF}
## : $scriptRef => The script that is executed {REF}
## : $dateRef => The date {REF}
## : $dateTimeStampRef => The date and time {REF}
my ($argHashRef) = @_;
## Flatten argument(s)
my $logfileRef;
my $scriptRef;
my $dateRef;
my $dateTimeStampRef;
my $tmpl = {
logfileRef => { default => \$$, strict_type => 1, store => \$logfileRef},
scriptRef => { required => 1, defined => 1, default => \$$, strict_type => 1, store => \$scriptRef},
dateRef => { required => 1, defined => 1, default => \$$, strict_type => 1, store => \$dateRef},
dateTimeStampRef => { required => 1, defined => 1, default => \$$, strict_type => 1, store => \$dateTimeStampRef},
};
check($tmpl, $argHashRef, 1) or die qw[Could not parse arguments!];
if(! $$logfileRef) { #No input from cmd i.e. create default logging directory and set default
## Create default logfile dir and file path
make_path(catdir(cwd(), "gc_analysis", "gc_log", $$dateRef));
$logfile = catfile(cwd(), "gc_analysis", "gc_log", $$dateRef, $$scriptRef."_".$$dateTimeStampRef.".log");
}
## Creates log for the master script
my $config = &CreateLog4perlCongfig({filePathRef => \$$logfileRef,
});
Log::Log4perl->init(\$config);
$logger = Log::Log4perl->get_logger("gcLogger");
}
sub CreateLog4perlCongfig {
##CreateLog4perlCongfig
##Function : Create log4perl config file.
##Returns : "$config"
##Arguments: $filePathRef
## : $filePathRef => log4perl config file path {REF}
my ($argHashRef) = @_;
## Flatten argument(s)
my $filePathRef;
my $tmpl = {
filePathRef => { required => 1, defined => 1, default => \$$, strict_type => 1, store => \$filePathRef},
};
check($tmpl, $argHashRef, 1) or die qw[Could not parse arguments!];
my $config = q?
log4perl.category.gcLogger = TRACE, LogFile, ScreenApp
log4perl.appender.LogFile = Log::Log4perl::Appender::File
log4perl.appender.LogFile.filename = ?.$$filePathRef.q?
log4perl.appender.LogFile.layout=PatternLayout
log4perl.appender.LogFile.layout.ConversionPattern = [%p] %d %c - %m%n
log4perl.appender.ScreenApp = Log::Log4perl::Appender::Screen
log4perl.appender.ScreenApp.layout = PatternLayout
log4perl.appender.ScreenApp.layout.ConversionPattern = [%p] %d %c - %m%n
?;
return $config;
}
sub CheckProgram {
##CheckProgram
##Function : Check program(s) can run and log version if so. Otherwise exit.
##Returns : ""
##Arguments: $programsArrayRef, $verbose
## : $programsArrayRef => Programs to check
## : $verbose => Verbose
my ($argHashRef) = @_;
## Flatten argument(s)
my $programsArrayRef;
my $verbose;
my $tmpl = {
programsArrayRef => { required => 1, defined => 1, default => [], strict_type => 1, store => \$programsArrayRef},
verbose => { strict_type => 1, store => \$verbose},
};
check($tmpl, $argHashRef, 1) or die qw[Could not parse arguments!];
foreach my $program (@{${programsArrayRef}}) {
&CheckCommandinPath({program => $program,
});
my $cmdsArrayRef = [$program, "--version"];
my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
run( command => $cmdsArrayRef, verbose => $verbose );
if ($error_message) {
my $ret = join(" ", @$stderr_buf);
if( $ret =~/(Version:\s+\S+)/) {
$logger->info($program." ".$1, "\n");
}
if( $ret =~/(\d+.\d+.\d+)/) { #Find version - assume semantic versioning major.minor.patch
$logger->info("Version: ".$program." ".$1, "\n");
}
}
if($success) {
my $ret = join(" ", @$full_buf);
if( $ret =~/(\d+.\d+.\d+)/) { #Find version - assume semantic versioning major.minor.patch
$logger->info("Version: ". $program." ".$1, "\n");
}
}
}
}
sub CheckCommandinPath {
##CheckCommandinPath
##Function : Checking command in your path and executable
##Returns : ""
##Arguments: $program
## : $program => Program to check
my ($argHashRef) = @_;
## Flatten argument(s)
my $program;
my $tmpl = {
program => { required => 1, defined => 1, strict_type => 1, store => \$program},
};
check($tmpl, $argHashRef, 1) or die qw[Could not parse arguments!];
if(can_run($program)) { #IPC::Cmd
$logger->info("ProgramCheck: ".$program." installed\n");
}
else {
$logger->fatal("Could not detect ".$program." in your Path\n");
exit 1;
}
}
sub LoadYAML {
##LoadYAML
##Function : Loads a YAML file into an arbitrary hash and returns it. Note: Currently only supports hashreferences and hashes and no mixed entries.
##Returns : %yamlHash
##Arguments: $yamlFile
## : $yamlFile => The yaml file to load
my ($argHashRef) = @_;
##Flatten argument(s)
my $yamlFile;
my $tmpl = {
yamlFile => { required => 1, defined => 1, strict_type => 1, store => \$yamlFile},
};
check($tmpl, $argHashRef, 1) or die qw[Could not parse arguments!];
my %yamlHash;
open (my $YAML, "<", $yamlFile) or $logger->logdie("Can't open '".$yamlFile."':".$!, "\n");
local $YAML::QuoteNumericStrings = 1; #Force numeric values to strings in YAML representation
%yamlHash = %{ YAML::LoadFile($yamlFile) }; #Load hashreference as hash
close($YAML);
$logger->info("Read YAML file: ".$yamlFile,"\n");
return %yamlHash;
}
sub SearchGrid {
##SearchGrid
##Function : Search grid for input samples and add to paths
##Returns : ""
##Arguments: $gridHashRef, sampleIDsArrayRef
## : $gridHashRef => Grid to search
## : $sampleIDsArrayRef => Cohort from cmd
my ($argHashRef) = @_;
## Flatten argument(s)
my $gridHashRef;
my $sampleIDsArrayRef;
my $tmpl = {
gridHashRef => { required => 1, defined => 1, default => {}, strict_type => 1, store => \$gridHashRef},
sampleIDsArrayRef => { required => 1, defined => 1, default => [], strict_type => 1, store => \$sampleIDsArrayRef},
};
check($tmpl, $argHashRef, 1) or die qw[Could not parse arguments!];
my @foundSampleIDs;
my @paths; #Temporary array for paths
my %path; #Collection of paths to analyse
foreach my $sampleID (@sampleIDs) {
if(${$gridHashRef}{$sampleID}) {
my $pathRef = \${$gridHashRef}{$sampleID}; #Alias
push(@{$path{$$pathRef}}, $sampleID); #Collapse sampleIDs per path
push(@foundSampleIDs, $sampleID);
}
else {
$logger->fatal("Could not detect sampleID: ".$sampleID." in grid", "\n");
exit 1;
}
}
if (@foundSampleIDs) {
$logger->info("Found sampleID(s): ".join(", ", @foundSampleIDs)." in grid", "\n");
}
return %path;
}
sub BcfToolsViewCmd {
##BcfToolsViewCmd
##Function : Generate command line instructions for bcfTools view
##Returns : ""
##Arguments: $pathHashRef, $positionsArrayRef, $outDataDir, $exclude, $include, $genoType, $outputType, $outfileEnding
## : $pathHashRef => PathHashRef
## : $positionsArrayRef => Positions to analyse
## : $outDataDir => Outdata directory
## : $exclude => Filters to exclude variant
## : $include => Filters to include variant
## : $genoType => Require or exclude genotype
## : $outputType => The output data type
## : $outfileEnding => Outfile ending depending on outputType
my ($argHashRef) = @_;
## Flatten argument(s)
my $pathHashRef;
my $positionsArrayRef;
my $outDataDir;
my $exclude;
my $include;
my $genoType;
my $outputType;
my $outfileEnding;
my $tmpl = {
pathHashRef => { required => 1, defined => 1, default => {}, strict_type => 1, store => \$pathHashRef},
positionsArrayRef => { required => 1, defined => 1, default => [], strict_type => 1, store => \$positionsArrayRef},
outDataDir => { required => 1, defined => 1, strict_type => 1, store => \$outDataDir},
exclude => { strict_type => 1, store => \$exclude},
include => { strict_type => 1, store => \$include},
genoType => { strict_type => 1, store => \$genoType},
outputType => { strict_type => 1, store => \$outputType},
outfileEnding => { strict_type => 1, store => \$outfileEnding},
};
check($tmpl, $argHashRef, 1) or die qw[Could not parse arguments!];
foreach my $path (keys %{$pathHashRef}) {
## Create array ref for cmd
my $cmdsArrayRef;
my $cmd;
push (@{$cmdsArrayRef}, "bcftools");
push (@{$cmdsArrayRef}, "view");
### Filters
## Samples
push (@{$cmdsArrayRef}, "--samples");
push (@{$cmdsArrayRef}, join(",", @{${$pathHashRef}{$path}}) );
## GenoType
if ($genoType) {
push (@{$cmdsArrayRef}, "--genotype");
push (@{$cmdsArrayRef}, $genoType);
}
## VCF data
if($exclude) {
my @filter = split(/\s+/, $exclude); #Seperate for whitespace to enable correct encoding to run
push (@{$cmdsArrayRef}, "-e", "'",);
foreach my $filterElement (@filter) {
push (@{$cmdsArrayRef}, $filterElement);
}
push (@{$cmdsArrayRef}, "'",);
}
elsif ($include) {
my @filter = split(/\s+/, $include); #Seperate for whitespace to enable correct encoding to run
push (@{$cmdsArrayRef}, "-i", "'",);
foreach my $filterElement (@filter) {
push (@{$cmdsArrayRef}, $filterElement);
}
push (@{$cmdsArrayRef}, "'",);
}
## Position(s)
if (@{$positionsArrayRef}) {
push(@{$cmdsArrayRef}, "--regions");
push (@{$cmdsArrayRef}, join(",", @{$positionsArrayRef}));
}
## OutputType
if($outputType) {
push(@{$cmdsArrayRef}, "--output-type");
push(@{$cmdsArrayRef}, $outputType);
}
## Add vcf path
push (@{$cmdsArrayRef}, $path);
push (@{$cmdsArrayRef}, ">", catfile($outDataDir, join("_", @{${$pathHashRef}{$path}}).$outfileEnding)); #Outdata
## Index
push (@{$cmdsArrayRef}, ";");
push (@{$cmdsArrayRef}, "bcftools");
push (@{$cmdsArrayRef}, "index");
push (@{$cmdsArrayRef}, catfile($outDataDir, join("_", @{${$pathHashRef}{$path}}).$outfileEnding));
$cmd{$path} = $cmdsArrayRef;
}
return %cmd;
}
sub BcfToolsMergeCmd {
##BcfToolsMergeCmd
##Function : Generate command line instructions for bcfTools merge
##Returns : ""
##Arguments: $pathHashRef, $outDataDir, $outputType, $outfileEnding
## : $pathHashRef => PathHashRef
## : $outDataDir => Outdata directory
## : $outputType => The output data type
## : $outfileEnding => Outfile ending depending on outputType
my ($argHashRef) = @_;
## Flatten argument(s)
my $pathHashRef;
my $outDataDir;
my $outputType;
my $outfileEnding;
my $tmpl = {
pathHashRef => { required => 1, defined => 1, default => {}, strict_type => 1, store => \$pathHashRef},
outDataDir => { required => 1, defined => 1, strict_type => 1, store => \$outDataDir},
outputType => { strict_type => 1, store => \$outputType},
outfileEnding => { strict_type => 1, store => \$outfileEnding},
};
check($tmpl, $argHashRef, 1) or die qw[Could not parse arguments!];
## Create array ref for cmd
my $cmdsArrayRef;
## Merge files
push (@{$cmdsArrayRef}, "bcftools");
push (@{$cmdsArrayRef}, "merge");
## OutputType
if($outputType) {
push(@{$cmdsArrayRef}, "--output-type");
push(@{$cmdsArrayRef}, $outputType);
}
## Add outdata paths
foreach my $path (keys %{$pathHashRef}) {
push (@{$cmdsArrayRef}, catfile($outDataDir, join("_", @{${$pathHashRef}{$path}}).$outfileEnding));
}
push (@{$cmdsArrayRef}, ">", catfile($outDataDir, "gc_merged".$outfileEnding)); #Outdata
return @{$cmdsArrayRef};
}
sub SubmitCommand {
##SubmitCommand
##Function : Submit command via SHELL or SBATCH
##Returns : ""
##Arguments: $pathHashRef, $cmdHashRef, $sbatchParameterHashRef, $mergecmdsArrayRef, $sourceEnvironmentCommandArrayRef, $email, $projectID, $nrofCores, $slurmQualityofService, verbose, $environment
## : $pathHashRef => PathHashRef {REF}
## : $cmdHashRef => Command to execute {REF}
## : $sbatchParameterHashRef => The sbatch parameter hash {REF}
## : $mergecmdsArrayRef => Merge command {REF}
## : $sourceEnvironmentCommandArrayRef => Source environment command {REF}
## : $verbose => Verbose
## : $environment => Shell or SBATCH
my ($argHashRef) = @_;
## Default(s)
my $environment = ${$argHashRef}{environment} //= "SHELL";
## Flatten argument(s)
my $pathHashRef;
my $cmdHashRef;
my $sbatchParameterHashRef;
my $mergecmdsArrayRef;
my $sourceEnvironmentCommandArrayRef;
my $verbose;
my $tmpl = {
pathHashRef => { required => 1, defined => 1, default => {}, strict_type => 1, store => \$pathHashRef},
cmdHashRef => { required => 1, defined => 1, default => {}, strict_type => 1, store => \$cmdHashRef},
sbatchParameterHashRef => { required => 1, defined => 1, default => {}, strict_type => 1, store => \$sbatchParameterHashRef},
mergecmdsArrayRef => { required => 1, defined => 1, default => [], strict_type => 1, store => \$mergecmdsArrayRef},
sourceEnvironmentCommandArrayRef => { default => [],
strict_type => 1, store => \$sourceEnvironmentCommandArrayRef},
environment => { required => 1, defined => 1, strict_type => 1, store => \$environment},
verbose => { strict_type => 1, store => \$verbose},
};
check($tmpl, $argHashRef, 1) or die qw[Could not parse arguments!];
if ($environment =~/SHELL/i) {
$logger->info("Executing grid search", "\n");
foreach my $cmd (keys %{$cmdHashRef}) {
if(@sourceEnvironmentCommand) {
## Add source environment command
unshift(@{$cmd{$cmd}}, @{$sourceEnvironmentCommandArrayRef});
}
my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
run( command => $cmd{$cmd}, verbose => $verbose );
}
## Bcftools merge
if (keys %{$cmdHashRef} > 1) {
$logger->info("Merging grid search results", "\n");
if(@sourceEnvironmentCommand) {
## Add source environment command
unshift(@{$mergecmdsArrayRef}, @{$sourceEnvironmentCommandArrayRef});
}
my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
run( command => $mergecmdsArrayRef, verbose => $verbose );
}
}
if ($environment =~/SBATCH/i) {
my $FILEHANDLE = IO::Handle->new(); #Create anonymous filehandle
my $XARGSFILEHANDLE = IO::Handle->new(); #Create anonymous filehandle
my $fileName = &ProgramPreRequisites({sourceEnvironmentCommandArrayRef => $sourceEnvironmentCommandArrayRef,
dateTimeStampRef => \$dateTimeStamp,
FILEHANDLE => $FILEHANDLE,
projectID => ${$sbatchParameterHashRef}{projectID},
email => ${$sbatchParameterHashRef}{email},
nrofCores => ${$sbatchParameterHashRef}{maximumCores},
slurmQualityofService => ${$sbatchParameterHashRef}{slurmQualityofService},
});
if(@sourceEnvironmentCommand) {
## Add source environment command
say $FILEHANDLE join(" ", @{$sourceEnvironmentCommandArrayRef}), "\n";
}
###Method of parallelization
## Use standard bash print/wait statements
if (! ${$sbatchParameterHashRef}{xargs}) {
my $coreCounter = 1;
my $cmdCounter = 0;
foreach my $cmd (keys %{$cmdHashRef}) {
## Calculates when to prints "wait" statement and prints "wait" to supplied FILEHANDLE when adequate.
&PrintWait({counterRef => \$cmdCounter,
nrCoresRef => \${$sbatchParameterHashRef}{maximumCores},
coreCounterRef => \$coreCounter,
FILEHANDLE => $FILEHANDLE,
});
map { print $FILEHANDLE $_." "} (@{$cmd{$cmd}}); #Write command instruction to sbatch
say $FILEHANDLE "& \n";
$cmdCounter++;
}
say $FILEHANDLE "wait", "\n";
if (keys %{$cmdHashRef} > 1) {
##BcfTools merge
map { print $FILEHANDLE $_." "} (@{$mergecmdsArrayRef}); #Write command instruction to sbatch
}
}
else { # Use Xargs
## Create file commands for xargs
my ($xargsFileCounter, $xargsFileName) = &XargsCommand({FILEHANDLE => $FILEHANDLE,
XARGSFILEHANDLE => $XARGSFILEHANDLE,
fileName => $fileName,
nrCores => ${$sbatchParameterHashRef}{maximumCores},
firstCommand => "bcftools",
verbose => $verbose,
});
foreach my $cmd (keys %{$cmdHashRef}) {
my @escapeCharacters = qw(' ");
## Add escape characters for xargs shell expansion
for(my $elementCounter=0;$elementCounter<scalar(@{$cmd{$cmd}});$elementCounter++) {
foreach my $escapeCharacter (@escapeCharacters) {
$cmd{$cmd}[$elementCounter] =~ s/$escapeCharacter/\\$escapeCharacter/g;
}
}
shift(@{$cmd{$cmd}}); #Since this command is handled in the xargs
map { print $XARGSFILEHANDLE $_." "} (@{$cmd{$cmd}}); #Write command instruction to sbatch
print $XARGSFILEHANDLE "\n";
}
if (keys %{$cmdHashRef} > 1) {
## BcfTools merge
map { print $FILEHANDLE $_." "} (@{$mergecmdsArrayRef}); #Write command instruction to sbatch
}
}
my @slurmSubmit = ["sbatch", $fileName];
my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
run( command => @slurmSubmit, verbose => $verbose );
$logger->info(join(" ", @{$stdout_buf}));
}
}
sub ProgramPreRequisites {
##ProgramPreRequisites
##Function : Creates program directories (info & programData & programScript), program script filenames and writes sbatch header.
##Returns : Path to stdout
##Arguments: $sourceEnvironmentCommandArrayRef, $FILEHANDLE, $projectID, $email, $dateTimeStampRef, $emailType, $slurmQualityofService, $nrofCores, $processTime, $pipefail, $errorTrap
## : $sourceEnvironmentCommandArrayRef => Source environment command {REF}
## : $FILEHANDLE => FILEHANDLE to write to
## : $projectID => Sbatch projectID
## : $email => Send email from sbatch
## : $dateTimeStampRef => The date and time {REF}
## : $emailType => The email type
## : $slurmQualityofService => SLURM quality of service priority {Optional}
## : $nrofCores => The number of cores to allocate {Optional}
## : $processTime => Allowed process time (Hours) {Optional}
## : $errorTrap => Error trap switch {Optional}
## : $pipefail => Pipe fail switch {Optional}
my ($argHashRef) = @_;
## Default(s)
my $emailType = ${$argHashRef}{emailType} //= "F";
my $slurmQualityofService = ${$argHashRef}{slurmQualityofService} //= "low";
my $nrofCores = ${$argHashRef}{nrofCores} //= 16;
my $processTime;
my $pipefail;
my $errorTrap;
## Flatten argument(s)
my $sourceEnvironmentCommandArrayRef;
my $FILEHANDLE;
my $projectID;
my $email;
my $dateTimeStampRef;
my $tmpl = {
sourceEnvironmentCommandArrayRef => { default => [],
strict_type => 1, store => \$sourceEnvironmentCommandArrayRef},
FILEHANDLE => { required => 1, defined => 1, store => \$FILEHANDLE},
projectID => { required => 1, defined => 1, strict_type => 1, store => \$projectID},
email => { strict_type => 1, store => \$email},
dateTimeStampRef => { required => 1, defined => 1, default => \$$, strict_type => 1, store => \$dateTimeStampRef},
emailType => { allow => ["B", "F", "E"],
strict_type => 1, store => \$emailType},
slurmQualityofService => { default => "low",
allow => ["low", "high", "normal"],
strict_type => 1, store => \$slurmQualityofService},
nrofCores => { allow => qr/^\d+$/,
strict_type => 1, store => \$nrofCores},
processTime => { default => 1,
allow => qr/^\d+$/,
strict_type => 1, store => \$processTime},
pipefail => { default => 1,
allow => [0, 1],
strict_type => 1, store => \$pipefail},
errorTrap => { default => 1,
allow => [0, 1],
strict_type => 1, store => \$errorTrap},
};
check($tmpl, $argHashRef, 1) or die qw[Could not parse arguments!];
### Sbatch script names and directory creation
my $outDataDir = catfile(cwd(), "gc_analysis");
my $outScriptDir = catfile(cwd(), "gc_analysis", "scripts");
my $programName = "grid-crawler";
my $fileNameEnd = ".sh";
my $fileNameStub = $$dateTimeStampRef; #The sbatch script
my $fileNamePath = catfile($outScriptDir, $programName."_".$fileNameStub.$fileNameEnd);
my $fileInfoPath = catfile($outDataDir, "info", $programName."_".$fileNameStub);
## Create directories
make_path(catfile($outDataDir, "info"), #Creates the outdata directory folder and info data file directory
catfile($outScriptDir), #Creates the folder script file directory
);
###Info and Log
$logger->info("Creating sbatch script for ".$programName." and writing script file(s) to: ".$fileNamePath."\n");
$logger->info("Sbatch script ".$programName." data files will be written to: ".$outDataDir."\n");
###Sbatch header
open ($FILEHANDLE, ">",$fileNamePath) or $logger->logdie("Can't write to '".$fileNamePath."' :".$!."\n");
say $FILEHANDLE "#! /bin/bash -l";
if ($pipefail) {
say $FILEHANDLE "set -o pipefail"; #Detect errors within pipes
}
say $FILEHANDLE "#SBATCH -A ".$projectID;
say $FILEHANDLE "#SBATCH -n ".$nrofCores;
say $FILEHANDLE "#SBATCH -t ".$processTime.":00:00";
say $FILEHANDLE "#SBATCH --qos=".$slurmQualityofService;
say $FILEHANDLE "#SBATCH -J ".$programName;
say $FILEHANDLE "#SBATCH -e ".catfile($fileInfoPath.".stderr.txt");
say $FILEHANDLE "#SBATCH -o ".catfile($fileInfoPath.".stdout.txt");
if ($email) {
if ($emailType =~/B/i) {
say $FILEHANDLE "#SBATCH --mail-type=BEGIN";
}
if ($emailType =~/E/i) {
say $FILEHANDLE "#SBATCH --mail-type=END";
}
if ($emailType =~/F/i) {
say $FILEHANDLE "#SBATCH --mail-type=FAIL";
}
say $FILEHANDLE "#SBATCH --mail-user=".$email, "\n";
}
say $FILEHANDLE q?echo "Running on: $(hostname)"?;
say $FILEHANDLE q?PROGNAME=$(basename $0)?,"\n";
if (@{$sourceEnvironmentCommandArrayRef}) {
say $FILEHANDLE "##Activate environment";
say $FILEHANDLE join(' ', @{$sourceEnvironmentCommandArrayRef}), "\n";
}
if ($errorTrap) {
## Create error handling function and trap
say $FILEHANDLE q?error() {?, "\n";
say $FILEHANDLE "\t".q?## Display error message and exit?;
say $FILEHANDLE "\t".q{ret="$?"};
say $FILEHANDLE "\t".q?echo "${PROGNAME}: ${1:-"Unknown Error - ExitCode="$ret}" 1>&2?, "\n";
say $FILEHANDLE "\t".q?exit 1?;
say $FILEHANDLE q?}?;
say $FILEHANDLE q?trap error ERR?, "\n";
}
return $fileNamePath;
}
sub CheckEmailAddress {
##CheckEmailAddress
##Function : Check the syntax of the email adress is valid not that it is actually exists.
##Returns : ""
##Arguments: $emailRef
## : $emailRef => The email adress
my ($argHashRef) = @_;
## Flatten argument(s)
my $emailRef;
my $tmpl = {