| 1 | package NBI::Job; | ||||||||||||||||||||||||||||
| 2 | #ABSTRACT: A class for representing a job for NBI::Slurm | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # NBI::Job - Represents a single SLURM job to be submitted. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # DESCRIPTION: | ||||||||||||||||||||||||||||
| 7 | # Encapsulates a named job consisting of one or more shell commands | ||||||||||||||||||||||||||||
| 8 | # together with its resource options (NBI::Opts). Key responsibilities: | ||||||||||||||||||||||||||||
| 9 | # - new() : accepts -name, -command/-commands, and -opts | ||||||||||||||||||||||||||||
| 10 | # - script() : assembles the bash/sbatch script content | ||||||||||||||||||||||||||||
| 11 | # - run() : writes the script to disk and submits it via sbatch, | ||||||||||||||||||||||||||||
| 12 | # returning the numeric SLURM job ID | ||||||||||||||||||||||||||||
| 13 | # - view() : returns a human-readable summary string | ||||||||||||||||||||||||||||
| 14 | # - outputfile / errorfile : lvalue accessors for stdout/stderr paths | ||||||||||||||||||||||||||||
| 15 | # (support %j interpolation after submission) | ||||||||||||||||||||||||||||
| 16 | # - append_command / prepend_command : add commands to the job list | ||||||||||||||||||||||||||||
| 17 | # - Array-job support: if the attached NBI::Opts has a -files list, | ||||||||||||||||||||||||||||
| 18 | # the script uses a SLURM job array and replaces the -placeholder | ||||||||||||||||||||||||||||
| 19 | # token with ${selected_file}. | ||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | # RELATIONSHIPS: | ||||||||||||||||||||||||||||
| 22 | # - Depends on NBI::Opts (stored in $self->{opts}) for all #SBATCH header | ||||||||||||||||||||||||||||
| 23 | # lines, the tmpdir, and array-job configuration. | ||||||||||||||||||||||||||||
| 24 | # - $NBI::Job::VERSION is set from $NBI::Slurm::VERSION (loaded by caller). | ||||||||||||||||||||||||||||
| 25 | # - Used directly by end-users and by the runjob bin script. | ||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 28 | ▶ | use 5.012; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 28 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 28 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 28 | ▶ | use File::Spec::Functions; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 28 | $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||||||||||
| 34 | 28 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 28 | $NBI::Job::VERSION = $NBI::Slurm::VERSION; | |||||||||||||||||||||||||||
| 37 | 28 | my $DEFAULT_QUEUE = "nbi-short"; | |||||||||||||||||||||||||||
| 38 | 28 | require Exporter; | |||||||||||||||||||||||||||
| 39 | 28 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 122 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=17 SCAR=33.8
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 122 | my $class = shift @_; | |||||||||||||||||||||||||||
| 44 | 122 | my ($job_name, $commands_array, $command, $opts); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Descriptive instantiation with parameters -param => value | ||||||||||||||||||||||||||||
| 47 | 122 | ▶ | if (substr($_[0], 0, 1) eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 122 | my %data = @_; | |||||||||||||||||||||||||||
| 49 | # Try parsing | ||||||||||||||||||||||||||||
| 50 | 122 | for my $i (keys %data) { | |||||||||||||||||||||||||||
| 51 | 362 | ▶ | if ($i =~ /^-name/) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 52 | 122 | $job_name = $data{$i}; | |||||||||||||||||||||||||||
| 53 | } elsif ($i =~ /^-command$/) { | ||||||||||||||||||||||||||||
| 54 | 121 | $command = $data{$i}; | |||||||||||||||||||||||||||
| 55 | } elsif ($i =~ /^-opts$/) { | ||||||||||||||||||||||||||||
| 56 | # Check that $data{$i} is an instance of NBI::Opts | ||||||||||||||||||||||||||||
| 57 | 119 | ▶ | if ($data{$i}->isa('NBI::Opts')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | # $data{$i} is an instance of NBI::Opts | ||||||||||||||||||||||||||||
| 59 | 119 | $opts = $data{$i}; | |||||||||||||||||||||||||||
| 60 | } else { | ||||||||||||||||||||||||||||
| 61 | # $data{$i} is not an instance of NBI::Opts | ||||||||||||||||||||||||||||
| 62 | 0 | confess "ERROR NBI::Job: -opts must be an instance of NBI::Opts\n"; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | } elsif ($i =~ /^-commands$/) { | ||||||||||||||||||||||||||||
| 66 | # Check that $data{$i} is an array | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if (ref($data{$i}) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $commands_array = $data{$i}; | |||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||
| 70 | 0 | confess "ERROR NBI::Job: -commands must be an array\n"; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||||||||||
| 73 | 0 | confess "ERROR NBI::Seq: Unknown parameter $i\n"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 122 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 122 | ▶ | $self->{name} = defined $job_name ? $job_name : 'job-' . int(rand(1000000)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 122 | $self->{jobid} = 0; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Commands: if both commands_array and command are defined, append command to commands_array | ||||||||||||||||||||||||||||
| 85 | 122 | ▶ | if (defined $commands_array) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 86 | 0 | $self->{commands} = $commands_array; | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | if (defined $command) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | push @{$self->{commands}}, $command; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } elsif (defined $command) { | ||||||||||||||||||||||||||||
| 91 | 121 | $self->{commands} = [$command]; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Opts must be an instance of NBI::Opts, check first | ||||||||||||||||||||||||||||
| 95 | 122 | ▶ | if (defined $opts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | # check that $opts is an instance of NBI::Opts | ||||||||||||||||||||||||||||
| 97 | 119 | ▶ | if ($opts->isa('NBI::Opts')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | # $opts is an instance of NBI::Opts | ||||||||||||||||||||||||||||
| 99 | 119 | $self->{opts} = $opts; | |||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||
| 101 | # $opts is not an instance of NBI::Opts | ||||||||||||||||||||||||||||
| 102 | 0 | confess "ERROR NBI::Job: -opts must be an instance of NBI::Opts\n"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||||||||||
| 106 | 3 | $self->{opts} = NBI::Opts->new($DEFAULT_QUEUE); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 122 | $self->{script_path} = undef; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Check here if there is opts->placeholder in the commands. | ||||||||||||||||||||||||||||
| 112 | # If there is then replace /placeholder/ with ${selected_file} | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 122 | ▶ | if ($self->opts->is_array()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 8 | for my $cmd (@{$self->{commands}}) { | |||||||||||||||||||||||||||
| 116 | 8 | _replace_array_placeholders($self, \$cmd); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 122 | return $self; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | sub script_path : lvalue { | ||||||||||||||||||||||||||
|
Subroutine
script_path: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | # Update script_path | ||||||||||||||||||||||||||||
| 126 | 1 | my ($self, $new_val) = @_; | |||||||||||||||||||||||||||
| 127 | 1 | ▶ | $self->{script_path} = $new_val if (defined $new_val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 1 | return $self->{script_path}; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 455 | ▶ | sub name : lvalue { | ||||||||||||||||||||||||||
|
Subroutine
name: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | # Update name | ||||||||||||||||||||||||||||
| 133 | 455 | my ($self, $new_val) = @_; | |||||||||||||||||||||||||||
| 134 | 455 | ▶ | $self->{name} = $new_val if (defined $new_val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 455 | return $self->{name}; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 10 | ▶ | sub jobid : lvalue { | ||||||||||||||||||||||||||
|
Subroutine
jobid: called CC=4 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | # Update jobid | ||||||||||||||||||||||||||||
| 140 | 10 | my ($self, $new_val) = @_; | |||||||||||||||||||||||||||
| 141 | 10 | ▶ | if (defined $new_val and $new_val !~ /^-?(\d+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $new_val and not $new_val =~ /^-?(\d+)$/u
MC/DC0%0/2
defined $new_val and not $new_val =~ /^-?(\d+)$/u
defined $new_val
not $new_val =~ /^-?(\d+)$/u
Truth table33%1/3
A and not(B)
A= defined $new_valB= not $new_val =~ /^-?(\d+)$/u
| |||||||||||||||||||||||||||||
| 142 | 0 | confess "ERROR NBI::Job: jobid must be an integer ". $new_val ."\n"; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 10 | ▶ | $self->{jobid} = $new_val if (defined $new_val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 10 | return $self->{jobid}; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 216 | ▶ | sub outputfile : lvalue { | ||||||||||||||||||||||||||
|
Subroutine
outputfile: called CC=5 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | # Update name | ||||||||||||||||||||||||||||
| 150 | 216 | my ($self, $parameter) = @_; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 216 | my $interpolate = 0; | |||||||||||||||||||||||||||
| 153 | 216 | ▶ | if (defined $parameter) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ($parameter eq '-interpolate') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | $interpolate = 1; | |||||||||||||||||||||||||||
| 156 | } else { | ||||||||||||||||||||||||||||
| 157 | 0 | $self->{output_file} = $parameter; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Create a default output_file if not defined | ||||||||||||||||||||||||||||
| 162 | 216 | ▶ | if (not defined $self->{output_file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 114 | $self->{output_file} = catfile( $self->opts->tmpdir , $self->name . ".%j.out"); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 216 | ▶ | if ($interpolate) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | my $jobid = $self->jobid; | |||||||||||||||||||||||||||
| 168 | 0 | my $output_file = $self->{output_file}; | |||||||||||||||||||||||||||
| 169 | 0 | $output_file =~ s/%j/$jobid/g; | |||||||||||||||||||||||||||
| 170 | 0 | return $output_file; | |||||||||||||||||||||||||||
| 171 | } else { | ||||||||||||||||||||||||||||
| 172 | 216 | return $self->{output_file}; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 216 | ▶ | sub errorfile : lvalue { | ||||||||||||||||||||||||||
|
Subroutine
errorfile: called CC=5 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | # Update name | ||||||||||||||||||||||||||||
| 179 | 216 | my ($self, $parameter) = @_; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 216 | my $interpolate = 0; | |||||||||||||||||||||||||||
| 182 | 216 | ▶ | if (defined $parameter) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | if ($parameter eq '-interpolate') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | $interpolate = 1; | |||||||||||||||||||||||||||
| 185 | } else { | ||||||||||||||||||||||||||||
| 186 | 0 | $self->{error_file} = $parameter; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # Create a default error_file if not defined | ||||||||||||||||||||||||||||
| 191 | 216 | ▶ | if (not defined $self->{error_file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 114 | $self->{error_file} = catfile( $self->opts->tmpdir , $self->name . ".%j.err"); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 216 | ▶ | if ($interpolate) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | my $jobid = $self->jobid; | |||||||||||||||||||||||||||
| 197 | 0 | my $error_file = $self->{error_file}; | |||||||||||||||||||||||||||
| 198 | 0 | $error_file =~ s/%j/$jobid/g; | |||||||||||||||||||||||||||
| 199 | 0 | return $error_file; | |||||||||||||||||||||||||||
| 200 | } else { | ||||||||||||||||||||||||||||
| 201 | 216 | return $self->{error_file}; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 5 | ▶ | sub append_command { | ||||||||||||||||||||||||||
|
Subroutine
append_command: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 206 | 5 | my ($self, $new_command) = @_; | |||||||||||||||||||||||||||
| 207 | 5 | ▶ | if ($self->opts->is_array()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 4 | _replace_array_placeholders($self, \$new_command); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 5 | push @{$self->{commands}}, $new_command; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 0 | ▶ | sub prepend_command { | ||||||||||||||||||||||||||
|
Subroutine
prepend_command: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 214 | 0 | my ($self, $new_command) = @_; | |||||||||||||||||||||||||||
| 215 | 0 | ▶ | if ($self->opts->is_array()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | _replace_array_placeholders($self, \$new_command); | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | 0 | unshift @{$self->{commands}}, $new_command; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 0 | ▶ | sub commands { | ||||||||||||||||||||||||||
|
Subroutine
commands: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 222 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 223 | 0 | return $self->{commands}; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 11 | ▶ | sub commands_count { | ||||||||||||||||||||||||||
|
Subroutine
commands_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 227 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||
| 228 | 11 | return 0 + scalar @{$self->{commands}}; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 1 | ▶ | sub set_opts { | ||||||||||||||||||||||||||
|
Subroutine
set_opts: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 232 | 1 | my ($self, $opts) = @_; | |||||||||||||||||||||||||||
| 233 | # Check that $opts is an instance of NBI::Opts | ||||||||||||||||||||||||||||
| 234 | 1 | ▶ | if ($opts->isa('NBI::Opts')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | # $opts is an instance of NBI::Opts | ||||||||||||||||||||||||||||
| 236 | 1 | $self->{opts} = $opts; | |||||||||||||||||||||||||||
| 237 | } else { | ||||||||||||||||||||||||||||
| 238 | # $opts is not an instance of NBI::Opts | ||||||||||||||||||||||||||||
| 239 | 0 | confess "ERROR NBI::Job: -opts must be an instance of NBI::Opts\n"; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 0 | ▶ | sub get_opts { | ||||||||||||||||||||||||||
|
Subroutine
get_opts: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 244 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 245 | 0 | return $self->{opts}; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 1224 | ▶ | sub opts { | ||||||||||||||||||||||||||
|
Subroutine
opts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 249 | 1224 | my ($self) = @_; | |||||||||||||||||||||||||||
| 250 | 1224 | return $self->{opts}; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | ## Run job | ||||||||||||||||||||||||||||
| 254 | 215 | ▶ | sub script { | ||||||||||||||||||||||||||
|
Subroutine
script: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 255 | 215 | my ($self) = @_; | |||||||||||||||||||||||||||
| 256 | 215 | my $template = [ | |||||||||||||||||||||||||||
| 257 | '#SBATCH -J NBI_SLURM_JOBNAME', | ||||||||||||||||||||||||||||
| 258 | '#SBATCH -o NBI_SLURM_OUT', | ||||||||||||||||||||||||||||
| 259 | '#SBATCH -e NBI_SLURM_ERR', | ||||||||||||||||||||||||||||
| 260 | '' | ||||||||||||||||||||||||||||
| 261 | ]; | ||||||||||||||||||||||||||||
| 262 | 215 | my $header = $self->opts->header(); | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | # Replace the template | ||||||||||||||||||||||||||||
| 265 | 215 | my $script = join("\n", @{$template}); | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | # Replace the values | ||||||||||||||||||||||||||||
| 268 | 215 | my $name = $self->name; | |||||||||||||||||||||||||||
| 269 | 215 | my $file_out = $self->outputfile; | |||||||||||||||||||||||||||
| 270 | 215 | my $file_err = $self->errorfile; | |||||||||||||||||||||||||||
| 271 | 215 | $script =~ s/NBI_SLURM_JOBNAME/$name/g; | |||||||||||||||||||||||||||
| 272 | 215 | $script =~ s/NBI_SLURM_OUT/$file_out/g; | |||||||||||||||||||||||||||
| 273 | 215 | $script =~ s/NBI_SLURM_ERR/$file_err/g; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 215 | my @commands = @{$self->{commands}}; | |||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 215 | ▶ | if ($self->opts->is_array()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 8 | my @prepend = _array_prelude($self); | |||||||||||||||||||||||||||
| 279 | 8 | unshift @commands, @prepend; | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | 215 | ▶ | if ($self->opts->is_array()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 8 | my $has_array_variable = 0; | |||||||||||||||||||||||||||
| 283 | 8 | for my $cmd (@commands) { | |||||||||||||||||||||||||||
| 284 | 65 | ▶ | if ($self->opts->is_files_array() && $cmd =~ /\$\{selected_file\}/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->opts->is_files_array and $cmd =~ /\$\{selected_file\}/u
MC/DC100%2/2
$self->opts->is_files_array and $cmd =~ /\$\{selected_file\}/u
$self->opts->is_files_array
$cmd =~ /\$\{selected_file\}/u
Truth table100%3/3
A and B
A= $self->opts->is_files_arrayB= $cmd =~ /\$\{selected_file\}/u
| |||||||||||||||||||||||||||||
| 285 | 1 | $has_array_variable = 1; | |||||||||||||||||||||||||||
| 286 | 1 | last; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | 64 | ▶ | if ($self->opts->is_params_array() && $cmd =~ /\$\{param_\d+\}/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->opts->is_params_array and $cmd =~ /\$\{param_\d+\}/u
MC/DC100%2/2
$self->opts->is_params_array and $cmd =~ /\$\{param_\d+\}/u
$self->opts->is_params_array
$cmd =~ /\$\{param_\d+\}/u
Truth table100%3/3
A and B
A= $self->opts->is_params_arrayB= $cmd =~ /\$\{param_\d+\}/u
| |||||||||||||||||||||||||||||
| 289 | 6 | $has_array_variable = 1; | |||||||||||||||||||||||||||
| 290 | 6 | last; | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | 8 | ▶ | if ($has_array_variable == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 1 | ▶ | if ($self->opts->is_files_array()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | 0 | confess "ERROR NBI::Job: No command contains the placeholder:" . $self->opts->placeholder . "\n"; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | 1 | confess "ERROR NBI::Job: No command contains params-array placeholders like ##1##\n"; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 214 | $script .= join("\n", @commands); | |||||||||||||||||||||||||||
| 302 | 214 | return $header . $script . "\n"; | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 12 | ▶ | sub _replace_array_placeholders { | ||||||||||||||||||||||||||
|
Subroutine
_replace_array_placeholders: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 306 | 12 | my ($self, $command_ref) = @_; | |||||||||||||||||||||||||||
| 307 | 12 | ▶ | if ($self->opts->is_files_array()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 1 | my $placeholder = $self->opts->placeholder; | |||||||||||||||||||||||||||
| 309 | 1 | $$command_ref =~ s/\Q$placeholder\E/\${selected_file}/g; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | 12 | ▶ | if ($self->opts->is_params_array()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 11 | $$command_ref =~ s/##(\d+)##/\${param_$1}/g; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 8 | ▶ | sub _array_prelude { | ||||||||||||||||||||||||||
|
Subroutine
_array_prelude: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 317 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 318 | 8 | my @index_prelude = ('nbi_array_index=$SLURM_ARRAY_TASK_ID'); | |||||||||||||||||||||||||||
| 319 | 8 | ▶ | if ($self->opts->{array_offset} && $self->opts->{array_offset} > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->opts->{"array_offset"} and $self->opts->{"array_offset"} > 0
MC/DC50%1/2
$self->opts->{"array_offset"} and $self->opts->{"array_offset"} > 0
$self->opts->{"array_offset"}
$self->opts->{"array_offset"} > 0
Truth table66%2/3
A and B
A= $self->opts->{"array_offset"}B= $self->opts->{"array_offset"} > 0
| |||||||||||||||||||||||||||||
| 320 | 3 | @index_prelude = ("nbi_array_index=\$((SLURM_ARRAY_TASK_ID + " . $self->opts->{array_offset} . "))"); | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | 8 | ▶ | if ($self->opts->is_files_array()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 1 | my $self_files = $self->opts->files; | |||||||||||||||||||||||||||
| 324 | 1 | my @escaped_files = @{$self_files}; | |||||||||||||||||||||||||||
| 325 | 1 | for my $file (@escaped_files) { | |||||||||||||||||||||||||||
| 326 | 30 | $file =~ s/ /\\ /g; | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | 1 | my $files_list = join(" ", @escaped_files); | |||||||||||||||||||||||||||
| 329 | return ( | ||||||||||||||||||||||||||||
| 330 | 1 | "# Job array list", | |||||||||||||||||||||||||||
| 331 | @index_prelude, | ||||||||||||||||||||||||||||
| 332 | "self_files=($files_list)", | ||||||||||||||||||||||||||||
| 333 | "selected_file=\${self_files[\$nbi_array_index]}", | ||||||||||||||||||||||||||||
| 334 | ); | ||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | 7 | ▶ | if ($self->opts->is_params_array()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | 7 | my $params_file = _shell_single_quote($self->opts->params_array); | |||||||||||||||||||||||||||
| 338 | 7 | my $perl_loader = q{use strict;use warnings;my ($file,$task_id)=@ARGV;open my $fh,"<",$file or die "Cannot open $file: $!\n";my $row_idx=0;while (my $line=<$fh>) { chomp $line; $line =~ s/\r$//; next if $line =~ /^\s*$/; next if $line =~ /^\s*#/; if ($row_idx == $task_id) { my @fields = split /\t/, $line, -1; for my $field (@fields) { print $field, "\0"; } exit 0; } $row_idx++; } die "No params row for task_id=$task_id in $file\n";}; | |||||||||||||||||||||||||||
| 339 | return ( | ||||||||||||||||||||||||||||
| 340 | 7 | "# Job array params", | |||||||||||||||||||||||||||
| 341 | @index_prelude, | ||||||||||||||||||||||||||||
| 342 | "params_file=$params_file", | ||||||||||||||||||||||||||||
| 343 | "mapfile -d '' -t params < <(perl -e '$perl_loader' \"\$params_file\" \"\$nbi_array_index\")", | ||||||||||||||||||||||||||||
| 344 | "for i in \"\${!params[@]}\"; do", | ||||||||||||||||||||||||||||
| 345 | " printf -v \"param_\$((i + 1))\" '%s' \"\${params[\$i]}\"", | ||||||||||||||||||||||||||||
| 346 | "done", | ||||||||||||||||||||||||||||
| 347 | ); | ||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | 0 | return (); | |||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 7 | ▶ | sub _shell_single_quote { | ||||||||||||||||||||||||||
|
Subroutine
_shell_single_quote: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 353 | 7 | my ($value) = @_; | |||||||||||||||||||||||||||
| 354 | 7 | $value =~ s/'/'\"'\"'/g; | |||||||||||||||||||||||||||
| 355 | 7 | return "'$value'"; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 7 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=9 SCAR=28.5
Pod
documented
| |||||||||||||||||||||||||||||
| 359 | 7 | my $self = shift @_; | |||||||||||||||||||||||||||
| 360 | # Check it has some commands | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | # Check it has a queue | ||||||||||||||||||||||||||||
| 364 | 7 | ▶ | if (not defined $self->opts->queue) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 0 | confess "ERROR NBI::Job: No queue defined for job " . $self->name . "\n"; | |||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | # Check it has some opts | ||||||||||||||||||||||||||||
| 368 | 7 | ▶ | if (not defined $self->opts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | 0 | confess "ERROR NBI::Job: No opts defined for job " . $self->name . "\n"; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | # Check it has some commands | ||||||||||||||||||||||||||||
| 372 | 7 | ▶ | if ($self->commands_count == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 0 | confess "ERROR NBI::Job: No commands defined for job " . $self->name . "\n"; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | # Create the script | ||||||||||||||||||||||||||||
| 377 | 7 | my $script = $self->script(); | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | # Create the script file | ||||||||||||||||||||||||||||
| 380 | 7 | my $script_file = catfile($self->opts->tmpdir, $self->name . ".sh"); | |||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | # change suffix from .sh to .INT.sh if the file exists already | ||||||||||||||||||||||||||||
| 383 | 7 | ▶ | if (-e $script_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 0 | my $i = 1; | |||||||||||||||||||||||||||
| 385 | 0 | ▶ | while (-e $script_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | 0 | my $string_int = sprintf("%05d", $i); | |||||||||||||||||||||||||||
| 387 | 0 | $script_file = catfile($self->opts->tmpdir, $self->name . "." . $string_int . ".sh"); | |||||||||||||||||||||||||||
| 388 | 0 | $i++; | |||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 7 | $self->{"script_path"} = $script_file; | |||||||||||||||||||||||||||
| 393 | 7 | ▶ | open(my $fh, ">", $script_file) or confess "ERROR NBI::Job: Cannot open file $script_file for writing\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 7 | print $fh $script; | |||||||||||||||||||||||||||
| 395 | 7 | close($fh); | |||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | # Run the script | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 7 | ▶ | if (_has_command('sbatch') == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | 0 | $self->jobid = -1; | |||||||||||||||||||||||||||
| 401 | 0 | return 0; | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | 7 | my $job_output = `sbatch "$script_file"`; | |||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | # Check the output | ||||||||||||||||||||||||||||
| 406 | 7 | ▶ | if ($job_output =~ /Submitted batch job (\d+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | # Job submitted | ||||||||||||||||||||||||||||
| 408 | 7 | my $job_id = $1; | |||||||||||||||||||||||||||
| 409 | # Update the job id | ||||||||||||||||||||||||||||
| 410 | 7 | $self->jobid = $job_id; | |||||||||||||||||||||||||||
| 411 | 7 | return $job_id; | |||||||||||||||||||||||||||
| 412 | } else { | ||||||||||||||||||||||||||||
| 413 | # Job not submitted | ||||||||||||||||||||||||||||
| 414 | 0 | confess "ERROR NBI::Job: Job " . $self->name . " not submitted\n"; | |||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | 0 | return $self->jobid; | |||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 0 | ▶ | sub view { | ||||||||||||||||||||||||||
|
Subroutine
view: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 421 | # Return a string representation of the object | ||||||||||||||||||||||||||||
| 422 | 0 | my $self = shift @_; | |||||||||||||||||||||||||||
| 423 | 0 | my $str = " --- NBI::Job object ---\n"; | |||||||||||||||||||||||||||
| 424 | 0 | $str .= " name: " . $self->name . "\n"; | |||||||||||||||||||||||||||
| 425 | 0 | $str .= " commands: \n\t" . join("\n\t", @{$self->commands}) . "\n"; | |||||||||||||||||||||||||||
| 426 | 0 | $str .= " jobid: " . $self->jobid . "\n"; | |||||||||||||||||||||||||||
| 427 | 0 | $str .= " script: " . $self->script_path . "\n"; | |||||||||||||||||||||||||||
| 428 | 0 | $str .= " output file:" . $self->outputfile('-interpolate') . "\n"; | |||||||||||||||||||||||||||
| 429 | 0 | $str .= " error file: " . $self->errorfile('-interpolate') . "\n"; | |||||||||||||||||||||||||||
| 430 | 0 | $str .= " ---------------------------\n"; | |||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | 0 | return $str; | |||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 7 | ▶ | sub _has_command { | ||||||||||||||||||||||||||
|
Subroutine
_has_command: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 436 | 7 | my $command = shift; | |||||||||||||||||||||||||||
| 437 | 7 | my $is_available = 0; | |||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | 7 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 440 | # Windows system | ||||||||||||||||||||||||||||
| 441 | 0 | $is_available = system("where $command >nul 2>nul") == 0; | |||||||||||||||||||||||||||
| 442 | } else { | ||||||||||||||||||||||||||||
| 443 | # Unix-like system | ||||||||||||||||||||||||||||
| 444 | 7 | $is_available = system("command -v $command >/dev/null 2>&1") == 0; | |||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 7 | return $is_available; | |||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 0 | ▶ | sub _to_string { | ||||||||||||||||||||||||||
|
Subroutine
_to_string: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 451 | # Convert string to a sanitized string with alphanumeric chars and dashes | ||||||||||||||||||||||||||||
| 452 | 0 | my ($self, $string) = @_; | |||||||||||||||||||||||||||
| 453 | 0 | return $string =~ s/[^a-zA-Z0-9\-]//gr; | |||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||
| 455 | 28 | 1; | |||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | __END__ | ||||||||||||||||||||||||||||