-
Notifications
You must be signed in to change notification settings - Fork 8
/
run_parallel_cmmds.pl
executable file
·76 lines (60 loc) · 2.1 KB
/
run_parallel_cmmds.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
#!/usr/bin/env perl
# AUTHORS: Bruno Contreras Moreira, Pablo Vinuesa
# AIM: run commands in parallel to process multiple files using GNU parallel
use FindBin '$Bin';
my $VERBOSE = 0;
# check whether an attached parallel binary is available to avoid errors caused by older,
# non-compatible parallel binaries in the system
my $parallelEXE = "$Bin/bin/";
if($ENV{'OSTYPE'} =~ /darwin/){ $parallelEXE .= "macosx-intel/parallel" }
else{ $parallelEXE .= "linux/parallel" }
if(! -s $parallelEXE){
$parallelEXE = `which parallel`;
chomp $parallelEXE;
if($parallelEXE eq ''){
print "# ERROR: parallel not in place!\n";
print "# ... you will need to install \"parallel\" first or include it in \$PATH\n";
print "# ... exiting\n";
exit(1)
}
}
if($VERBOSE == 1){ print "# parallelEXE=$parallelEXE\n" }
if(!$ARGV[1]){
print << "HELP";
$0 usage: requires 2 or max 3 args
$0 <file extension name> <'command with \$file interpolations'> [no_of_cores/threads]
example1: $0 faa 'muscle < \$file > \${file%.faa}_musAln.FAA'
example2: $0 faa 'mafft-linsi \$file > \${file%.faa}_mlinsi.FAA'
example3: $0 FAA 'FastTree -slow -sprlength 12 -log \${file%.FAA}_FT.log < \$file > \${file%.FAA}_FT.ph'
HELP
exit(2);
}
my ( $ext, $command ) = ($ARGV[0], $ARGV[1]);
my $total_files = `ls -1 *$ext | wc -l`;
chomp($total_files);
# prepare command for parallel syntax, which is different to pexec's
$command =~ s/\$file/{}/g;
$command =~ s/\$\{/{/g;
$command =~ s/file//g;
# eliminate indicated extensions
$command =~ s/\%\.\*/./g;
$command =~ s/\%([\.\-\_\w]+)/=s\/$1\/\/=/g;
# added --gnu flag for compatibility
if($ARGV[2] && $ARGV[2] > 0){
my $n_of_cores = $ARGV[2];
$command = "ls -1 *$ext | $parallelEXE --gnu --willcite -j $n_of_cores \"$command\"";
}
else{
$command = "ls -1 *$ext | $parallelEXE --gnu --willcite \"$command\"";
}
warn "# $command" if($VERBOSE);
open(RUN,"$command |") || die "# cannot run $command\n";
while(<RUN>){
if(/Thread creation failed/){
print "\n >>> ERROR: Thread creation failed, stop ...\n";
exit(3);
}
}
close(RUN);
print "\ndone processing $total_files files ...\n";
exit(4);