lib/NBI/Launcher.pm

filter: Statementstmt 89.4% 211 / 236 Branchbran 61.2% 71 / 116 Conditioncond 57.5% 61 / 106 MC/DCmcdc 29.0% 18 / 62 Subroutinesub 94.7% 18 / 19 Podpod 100% 9 / 9 totaltotal 70.8% 388 / 548 CC 113 SCAR 33.0
CC
113
Cov
75%
_runtime_to_hours
78.7
validate
44.9
generate_script
29.3
CRAP 27.0
« bin/session ↑ Summary bin/slurm-load »
1 package NBI::Launcher;
2 #ABSTRACT: Base class for nbilaunch tool wrappers
3 #
4 # NBI::Launcher - Declarative base class for HPC tool launchers.
5 #
6 # DESCRIPTION:
7 # Every tool wrapper inherits from this class. Subclasses provide:
8 # - A constructor that calls SUPER::new() with the launcher spec
9 # - make_command(%args) - the tool invocation string (only override needed
10 # for most tools)
11 #
12 # The base class provides everything else:
13 # - Spec storage and introspection (arg_spec, input_mode, sample_name)
14 # - Input / param / output validation
15 # - Shell script generation (generate_script)
16 # - NBI::Job construction and NBI::Manifest creation (build)
17 #
18 # RELATIONSHIPS:
19 # - Subclasses live in NBI::Launcher::*, ./launchers/, or ~/.nbi/launchers/.
20 # - build() returns (NBI::Job, NBI::Manifest) - consumed by bin/nbilaunch.
21 # - NBI::Pipeline wraps multiple NBI::Job objects for multi-step launchers.
22 #
23  
24 2 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Carp qw(confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 $NBI::Launcher::VERSION = $NBI::Slurm::VERSION;
33  
34 # ── Constructor ───────────────────────────────────────────────────────────────
35 # All parameters are named (not the -param style used by NBI::Opts/NBI::Job).
36 #
37 # Required: name, activate, inputs, outputs, outdir
38 # Optional: description, version, slurm_defaults, params, scratch, success_check
39 10 ▶sub new {
Subroutine new: called CC=16 SCAR=27.9
Pod documented
40 10 my ($class, %args) = @_;
41  
42 # ── Validate activate spec ────────────────────────────────────────────────
43 my $activate = $args{activate}
44 10 ▶ or confess "ERROR NBI::Launcher: 'activate' is required\n";
Branch: 1 branch
BranchTrueFalseTotal
my $activate = $args{"activate"} or confess("ERROR NBI::Launcher: 'activate' is required\n") 9 1 10
45 9 ▶ ref $activate eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $activate eq "HASH" or confess("ERROR NBI::Launcher: 'activate' must be a hashref\n") 9 0 9
46 or confess "ERROR NBI::Launcher: 'activate' must be a hashref\n";
47 9 my @act_keys = keys %$activate;
48 confess "ERROR NBI::Launcher: 'activate' must have exactly one key (module|singularity|conda), got: @act_keys\n"
49 9 ▶ unless @act_keys == 1 && grep { /^(module|singularity|conda)$/ } @act_keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @act_keys == 1 and grep {/^(module|singularity|conda)$/u;} @act_keys 2 7 9
Condition100%3/3
@act_keys == 1 and grep {/^(module|singularity|conda)$/u;} @act_keys
!ll&&!rl&&r
117
MC/DC100%2/2
@act_keys == 1 and grep {/^(module|singularity|conda)$/u;} @act_keys
@act_keys == 1 grep {/^(module|singularity|conda)$/u;} @act_keys
Truth table100%3/3
A and B
A= @act_keys == 1B= grep {/^(module|singularity|conda)$/u;} @act_keys
ABresult
0X0
100
111
50  
51 # ── Validate slurm_defaults ───────────────────────────────────────────────
52 7 ▶ my $slurm_defaults = $args{slurm_defaults} // {};
Condition100%2/2
$args{"slurm_defaults"} // {}
l!l
34
MC/DC100%1/1
$args{"slurm_defaults"} // {}
$args{"slurm_defaults"}
Truth table100%2/2
A
A= $args{"slurm_defaults"}
Aresult
11
00
53 7 for my $k (keys %$slurm_defaults) {
54 8 ▶ confess "ERROR NBI::Launcher: unknown slurm_defaults key '$k'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^(queue|threads|memory|runtime)$/u 1 7 8
55 unless $k =~ /^(queue|threads|memory|runtime)$/;
56 }
57  
58 6 my $self = bless {}, $class;
59  
60 6 ▶ $self->{name} = $args{name} or confess "ERROR NBI::Launcher: 'name' is required\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"name"} = $args{"name"} or confess("ERROR NBI::Launcher: 'name' is required\n") 5 1 6
61 5 ▶ $self->{description} = $args{description} // '';
Condition100%2/2
$args{"description"} // ""
l!l
23
MC/DC100%1/1
$args{"description"} // ""
$args{"description"}
Truth table100%2/2
A
A= $args{"description"}
Aresult
11
00
62 5 ▶ $self->{version} = $args{version} // 'unknown';
Condition100%2/2
$args{"version"} // "unknown"
l!l
23
MC/DC100%1/1
$args{"version"} // "unknown"
$args{"version"}
Truth table100%2/2
A
A= $args{"version"}
Aresult
11
00
63 5 $self->{activate} = $activate;
64 $self->{slurm_defaults} = {
65 5 queue => 'qib-short',
66 threads => 1,
67 memory => 4, # GB
68 runtime => '01:00:00',
69 %$slurm_defaults, # caller overrides
70 };
71 5 ▶ $self->{inputs} = $args{inputs} // [];
Condition50%1/2
$args{"inputs"} // []
l!l
50
MC/DC0%0/1
$args{"inputs"} // []
$args{"inputs"}
Truth table50%1/2
A
A= $args{"inputs"}
Aresult
11
00
72 5 ▶ $self->{params} = $args{params} // [];
Condition50%1/2
$args{"params"} // []
l!l
50
MC/DC0%0/1
$args{"params"} // []
$args{"params"}
Truth table50%1/2
A
A= $args{"params"}
Aresult
11
00
73 5 ▶ $self->{outputs} = $args{outputs} // [];
Condition50%1/2
$args{"outputs"} // []
l!l
50
MC/DC0%0/1
$args{"outputs"} // []
$args{"outputs"}
Truth table50%1/2
A
A= $args{"outputs"}
Aresult
11
00
74 5 ▶ $self->{outdir} = $args{outdir} // { flag => '--outdir', short => '-o', required => 1 };
Condition50%1/2
$args{"outdir"} // {"flag", "--outdir", "short", "-o", "required", 1}
l!l
50
MC/DC0%0/1
$args{"outdir"} // {"flag", "--outdir", "short", "-o", "required", 1}
$args{"outdir"}
Truth table50%1/2
A
A= $args{"outdir"}
Aresult
11
00
75 5 ▶ $self->{scratch} = $args{scratch} // { use_tmpdir => 0, cleanup_on_failure => 0 };
Condition100%2/2
$args{"scratch"} // {"use_tmpdir", 0, "cleanup_on_failure", 0}
l!l
23
MC/DC100%1/1
$args{"scratch"} // {"use_tmpdir", 0, "cleanup_on_failure", 0}
$args{"scratch"}
Truth table100%2/2
A
A= $args{"scratch"}
Aresult
11
00
76 5 $self->{success_check} = $args{success_check}; # optional coderef
77  
78 5 return $self;
79 }
80  
81 # ── activation_lines() ────────────────────────────────────────────────────────
82 # Returns the shell snippet that loads the tool environment.
83 # For singularity: returns "" because the prefix goes into make_command()
84 # via singularity_prefix().
85 5 ▶sub activation_lines {
Subroutine activation_lines: called CC=4 SCAR=14.0
Pod documented
86 5 my ($self) = @_;
87 5 my ($type, $value) = each %{ $self->{activate} };
88  
89 5 ▶ if ($type eq 'module') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'module') { } 3 2 5
elsif ($type eq 'conda') { } 1 1 2
elsif ($type eq 'singularity') { } 1 0 1
90 3 return "module load $value\n";
91 } elsif ($type eq 'conda') {
92 # Use 'source activate' for broadest HPC compatibility.
93 # On systems with conda >= 4.4, 'conda activate' also works.
94 1 return "source activate $value\n";
95 } elsif ($type eq 'singularity') {
96 # Singularity prefix is applied per-command in make_command()
97 # via singularity_prefix(). Nothing needed here.
98 1 return '';
99 }
100 0 return '';
101 }
102  
103 # ── singularity_prefix() ──────────────────────────────────────────────────────
104 # Helper for make_command() overrides: returns "singularity exec $image "
105 # when the launcher uses singularity activation, empty string otherwise.
106 # Subclass make_command() calls this and prepends it to the tool invocation.
107 2 ▶sub singularity_prefix {
Subroutine singularity_prefix: called CC=2 SCAR=6.9
Pod documented
108 2 my ($self) = @_;
109 2 my $img = $self->{activate}{singularity};
110 2 ▶ return defined $img ? "singularity exec $img " : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $img ? : 1 1 2
111 }
112  
113 # ── sample_name(%args) ────────────────────────────────────────────────────────
114 # Derives the sample name from the first file-type required input (usually r1).
115 # Strips known FASTQ extensions in order: .gz .fastq .fq _R1 _R2 _1 _2
116 # Override with --sample-name on the nbilaunch command line.
117 6 ▶sub sample_name {
Subroutine sample_name: called CC=9 SCAR=23.1
Pod documented
118 6 my ($self, %args) = @_;
119  
120 # Explicit override takes priority
121 6 ▶ return $args{sample_name} if defined $args{sample_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"sample_name"} 1 5 6
122  
123 # Find the first required file-type input
124 5 my $source;
125 5 for my $inp (@{ $self->{inputs} }) {
126 5 ▶ if (($inp->{type} // '') eq 'file' && ($inp->{required} // 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($inp->{"type"} // "") eq "file" and $inp->{"required"} // 0) 5 0 5
Condition42%3/7
($inp->{"type"} // "") eq "file" and $inp->{"required"} // 0
!ll&&!rl&&r
005
$inp->{"type"} // ""
l!l
50
$inp->{"required"} // 0
l!l
50
MC/DC0%0/2
($inp->{"type"} // "") eq "file" and $inp->{"required"} // 0
$inp->{"type"} $inp->{"required"}
Truth table33%1/3
A and B
A= $inp->{"type"}B= $inp->{"required"}
ABresult
0X0
100
111
127 5 $source = $args{ $inp->{name} };
128 5 ▶ last if defined $source;
Branch: 1 branch
BranchTrueFalseTotal
if defined $source 5 0 5
129 }
130 }
131 5 ▶ confess "ERROR NBI::Launcher ($self->{name}): cannot derive sample name - no file input found\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 0 5 5
132 unless defined $source;
133  
134 5 my $name = basename($source);
135 # Strip extensions from right to left
136 5 $name =~ s/\.gz$//i;
137 5 $name =~ s/\.(fastq|fq)$//i;
138 5 $name =~ s/_R[12]$//;
139 5 $name =~ s/_[12]$//;
140 5 return $name;
141 }
142  
143 # ── input_mode(%args) ────────────────────────────────────────────────────────
144 # Returns "paired" if both r1 and r2 are defined, "single" otherwise.
145 # Subclasses may override for tools with different pairing logic.
146 2 ▶sub input_mode {
Subroutine input_mode: called CC=3 SCAR=11.1
Pod documented
147 2 my ($self, %args) = @_;
148 2 ▶ return (defined $args{r1} && defined $args{r2}) ? 'paired' : 'single';
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'r1'} && defined $args{'r2'} ? : 1 1 2
Condition66%2/3
defined $args{"r1"} and defined $args{"r2"}
!ll&&!rl&&r
011
MC/DC50%1/2
defined $args{"r1"} and defined $args{"r2"}
defined $args{"r1"} defined $args{"r2"}
Truth table66%2/3
A and B
A= defined $args{"r1"}B= defined $args{"r2"}
ABresult
0X0
100
111
149 }
150  
151 # ── arg_spec() ────────────────────────────────────────────────────────────────
152 # Returns the full CLI surface of this launcher for nbilaunch to use when
153 # generating --help text and parsing command-line arguments.
154 # slurm_sync params are excluded (they are derived, not user-settable).
155 2 ▶sub arg_spec {
Subroutine arg_spec: called CC=1 SCAR=0.0
Pod documented
156 2 my ($self) = @_;
157 return {
158 name => $self->{name},
159 description => $self->{description},
160 version => $self->{version},
161 activate => $self->{activate},
162 4 inputs => [ grep { !$_->{slurm_sync} } @{ $self->{inputs} } ],
163 5 params => [ grep { !$_->{slurm_sync} } @{ $self->{params} } ],
164 outputs => $self->{outputs},
165 outdir => $self->{outdir},
166 slurm_defaults => $self->{slurm_defaults},
167 2 };
168 }
169  
170 # ── validate(%args) ───────────────────────────────────────────────────────────
171 # Dies with a helpful message if required inputs/params are missing or if
172 # file/dir values do not exist on disk.
173 7 ▶sub validate {
Subroutine validate: called CC=30 SCAR=38.1
Pod documented
174 7 my ($self, %args) = @_;
175  
176 # Check inputs
177 7 for my $inp (@{ $self->{inputs} }) {
178 12 ▶ next if $inp->{slurm_sync};
Branch: 1 branch
BranchTrueFalseTotal
if $inp->{"slurm_sync"} 0 12 12
179 12 my $name = $inp->{name};
180 12 my $val = $args{$name};
181  
182 12 ▶ if ($inp->{required} && !defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inp->{"required"} and not defined $val) 1 11 12
Condition100%3/3
$inp->{"required"} and not defined $val
!ll&&!rl&&r
561
MC/DC100%2/2
$inp->{"required"} and not defined $val
$inp->{"required"} not defined $val
Truth table100%3/3
A and not(B)
A= $inp->{"required"}B= not defined $val
ABresult
0X0
100
111
183 1 confess "ERROR NBI::Launcher ($self->{name}): missing required input '--$name'\n";
184 }
185 11 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 5 6 11
186  
187 6 ▶ my $type = $inp->{type} // 'string';
Condition50%1/2
$inp->{"type"} // "string"
l!l
60
MC/DC0%0/1
$inp->{"type"} // "string"
$inp->{"type"}
Truth table50%1/2
A
A= $inp->{"type"}
Aresult
11
00
188 6 ▶ if ($type eq 'file' && !-f $val) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'file' and not -f $val) { } 1 5 6
elsif ($type eq 'dir' and not -d $val) { } 0 10 10
Condition66%2/3
$type eq "file" and not -f $val
!ll&&!rl&&r
051
MC/DC50%1/2
$type eq "file" and not -f $val
$type eq "file" not -f $val
Truth table66%2/3
A and not(B)
A= $type eq "file"B= not -f $val
ABresult
0X0
100
111
189 1 confess "ERROR NBI::Launcher ($self->{name}): input '$name' - file not found: $val\n";
190 ▶ } elsif ($type eq 'dir' && !-d $val) {
Condition33%1/3
$type eq "dir" and not -d $val
!ll&&!rl&&r
500
MC/DC0%0/2
$type eq "dir" and not -d $val
$type eq "dir" not -d $val
Truth table33%1/3
A and not(B)
A= $type eq "dir"B= not -d $val
ABresult
0X0
100
111
191 0 confess "ERROR NBI::Launcher ($self->{name}): input '$name' - directory not found: $val\n";
192 }
193 }
194  
195 # Check params (with default_env and default fallback)
196 5 for my $p (@{ $self->{params} }) {
197 11 ▶ next if $p->{slurm_sync};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"slurm_sync"} 4 7 11
198 7 my $name = $p->{name};
199 7 my $val = $args{$name};
200  
201 # Try default_env, then default
202 7 ▶ if (!defined $val && $p->{default_env}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val and $p->{"default_env"}) 4 3 7
Condition100%3/3
not defined $val and $p->{"default_env"}
!ll&&!rl&&r
214
MC/DC100%2/2
not defined $val and $p->{"default_env"}
not defined $val $p->{"default_env"}
Truth table100%3/3
not(A) and B
A= not defined $valB= $p->{"default_env"}
ABresult
0X0
100
111
203 4 $val = $ENV{ $p->{default_env} };
204 }
205 7 ▶ $val //= $p->{default};
Condition66%2/3
$val //= $p->{"default"}
l!l&&r!l&&!r
520
MC/DC0%0/2
$val //= $p->{"default"}
$val $p->{"default"}
Truth table66%2/3
A //= B
A= $valB= $p->{"default"}
ABresult
1X1
011
000
206  
207 7 ▶ if ($p->{required} && !defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"required"} and not defined $val) 0 7 7
Condition66%2/3
$p->{"required"} and not defined $val
!ll&&!rl&&r
250
MC/DC0%0/2
$p->{"required"} and not defined $val
$p->{"required"} not defined $val
Truth table66%2/3
A and not(B)
A= $p->{"required"}B= not defined $val
ABresult
0X0
100
111
208 0 confess "ERROR NBI::Launcher ($self->{name}): missing required param '--$name'\n";
209 }
210 7 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 7 7
211  
212 7 ▶ my $type = $p->{type} // 'string';
Condition50%1/2
$p->{"type"} // "string"
l!l
70
MC/DC0%0/1
$p->{"type"} // "string"
$p->{"type"}
Truth table50%1/2
A
A= $p->{"type"}
Aresult
11
00
213 7 ▶ if ($type eq 'int' && $val !~ /^\d+$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'int' and not $val =~ /^\d+$/u) { } 0 7 7
elsif ($type eq 'float' and not $val =~ /^[\d.]+$/u) { } 0 7 7
elsif ($type eq 'file' and not -f $val) { } 0 7 7
elsif ($type eq 'dir' and not -d $val) { } 1 8 9
Condition33%1/3
$type eq "int" and not $val =~ /^\d+$/u
!ll&&!rl&&r
700
MC/DC0%0/2
$type eq "int" and not $val =~ /^\d+$/u
$type eq "int" not $val =~ /^\d+$/u
Truth table33%1/3
A and not(B)
A= $type eq "int"B= not $val =~ /^\d+$/u
ABresult
0X0
100
111
214 0 confess "ERROR NBI::Launcher ($self->{name}): param '$name' must be an integer, got: $val\n";
215 ▶ } elsif ($type eq 'float' && $val !~ /^[\d.]+$/) {
Condition66%2/3
$type eq "float" and not $val =~ /^[\d.]+$/u
!ll&&!rl&&r
520
MC/DC0%0/2
$type eq "float" and not $val =~ /^[\d.]+$/u
$type eq "float" not $val =~ /^[\d.]+$/u
Truth table66%2/3
A and not(B)
A= $type eq "float"B= not $val =~ /^[\d.]+$/u
ABresult
0X0
100
111
216 0 confess "ERROR NBI::Launcher ($self->{name}): param '$name' must be a number, got: $val\n";
217 ▶ } elsif ($type eq 'file' && !-f $val) {
Condition33%1/3
$type eq "file" and not -f $val
!ll&&!rl&&r
700
MC/DC0%0/2
$type eq "file" and not -f $val
$type eq "file" not -f $val
Truth table33%1/3
A and not(B)
A= $type eq "file"B= not -f $val
ABresult
0X0
100
111
218 0 confess "ERROR NBI::Launcher ($self->{name}): param '$name' - file not found: $val\n";
219 ▶ } elsif ($type eq 'dir' && !-d $val) {
Condition100%3/3
$type eq "dir" and not -d $val
!ll&&!rl&&r
241
MC/DC100%2/2
$type eq "dir" and not -d $val
$type eq "dir" not -d $val
Truth table100%3/3
A and not(B)
A= $type eq "dir"B= not -d $val
ABresult
0X0
100
111
220 1 confess "ERROR NBI::Launcher ($self->{name}): param '$name' - directory not found: $val\n";
221 }
222 }
223  
224 # Check outdir is provided
225 4 ▶ if ($self->{outdir}{required} && !defined $args{outdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"outdir"}{"required"} and not defined $args{"outdir"}) 1 3 4
Condition66%2/3
$self->{"outdir"}{"required"} and not defined $args{"outdir"}
!ll&&!rl&&r
031
MC/DC50%1/2
$self->{"outdir"}{"required"} and not defined $args{"outdir"}
$self->{"outdir"}{"required"} not defined $args{"outdir"}
Truth table66%2/3
A and not(B)
A= $self->{"outdir"}{"required"}B= not defined $args{"outdir"}
ABresult
0X0
100
111
226 1 confess "ERROR NBI::Launcher ($self->{name}): missing required '--outdir'\n";
227 }
228  
229 3 return 1;
230 }
231  
232 # ── make_command(%args) ───────────────────────────────────────────────────────
233 # Returns the tool invocation string to embed in the job script.
234 # Subclasses SHOULD override this method.
235 # The default implementation raises an error - every launcher needs a command.
236 #
237 # %args contains all resolved inputs, params, and derived keys:
238 # $args{sample} - derived sample name
239 # $args{threads} - from slurm_sync or slurm_defaults
240 # For scratch paths, use literal \$SCRATCH (shell variable).
241 0 ▶sub make_command {
Subroutine make_command: not called CC=1 SCAR=6.9
242 0 my ($self, %args) = @_;
243 0 confess "ERROR NBI::Launcher ($self->{name}): make_command() not implemented\n";
244 }
245  
246 # ── generate_script(%args) ───────────────────────────────────────────────────
247 # Assembles the script body (everything after the #SBATCH header generated by
248 # NBI::Opts->header()). Returns a single string.
249 #
250 # Sections (in order):
251 # 1. set -euo pipefail + metadata comment
252 # 2. Manifest update shell functions + ERR trap
253 # 3. Activation (module load / conda / empty for singularity)
254 # 4. SAMPLE, OUTDIR, SCRATCH variables
255 # 5. EXIT trap (scratch cleanup)
256 # 6. Tool command from make_command()
257 # 7. Required-output validation
258 # 8. Promote from scratch to outdir + success update
259 2 ▶sub generate_script {
Subroutine generate_script: called CC=19 SCAR=33.8
Pod documented
260 2 my ($self, %args) = @_;
261  
262 2 my $tool = $self->{name};
263 2 my $version = $self->{version};
264 2 ▶ my $sample = $args{sample} or confess "generate_script: 'sample' required\n";
Branch: 1 branch
BranchTrueFalseTotal
my $sample = $args{"sample"} or confess("generate_script: 'sample' required\n") 2 0 2
265 2 ▶ my $outdir = $args{outdir} or confess "generate_script: 'outdir' required\n";
Branch: 1 branch
BranchTrueFalseTotal
my $outdir = $args{"outdir"} or confess("generate_script: 'outdir' required\n") 2 0 2
266 2 ▶ my $manifest_rel = $args{manifest_path} // ".nbilaunch/$sample.manifest.json";
Condition50%1/2
$args{"manifest_path"} // ".nbilaunch/$sample.manifest.json"
l!l
20
MC/DC0%0/1
$args{"manifest_path"} // ".nbilaunch/$sample.manifest.json"
$args{"manifest_path"}
Truth table50%1/2
A
A= $args{"manifest_path"}
Aresult
11
00
267 2 my $submitted_at = strftime("%Y-%m-%dT%H:%M:%SZ", gmtime());
268  
269 2 my $launcher_class = ref($self);
270 2 ▶ my $nbi_version = $NBI::Slurm::VERSION // 'unknown';
Condition50%1/2
$NBI::Slurm::VERSION // "unknown"
l!l
02
MC/DC0%0/1
$NBI::Slurm::VERSION // "unknown"
$NBI::Slurm::VERSION
Truth table50%1/2
A
A= $NBI::Slurm::VERSION
Aresult
11
00
271 2 ▶ my $nbi_l_version = $NBI::Launcher::VERSION // '0.1.0';
Condition50%1/2
$NBI::Launcher::VERSION // "0.1.0"
l!l
02
MC/DC0%0/1
$NBI::Launcher::VERSION // "0.1.0"
$NBI::Launcher::VERSION
Truth table50%1/2
A
A= $NBI::Launcher::VERSION
Aresult
11
00
272  
273 # ── Resolve %args for make_command ───────────────────────────────────────
274 # Apply default_env / default fallbacks for params before calling make_command
275 2 my %cmd_args = %args;
276 2 for my $p (@{ $self->{params} }) {
277 5 my $name = $p->{name};
278 5 ▶ next if defined $cmd_args{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cmd_args{$name} 5 0 5
279 0 ▶ if ($p->{default_env} && defined $ENV{ $p->{default_env} }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p->{'default_env'} and defined $ENV{$p->{'default_env'}}) { } 0 0 0
elsif (defined $p->{'default'}) { } 0 0 0
Condition0%0/3
$p->{"default_env"} and defined $ENV{$p->{"default_env"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$p->{"default_env"} and defined $ENV{$p->{"default_env"}}
$p->{"default_env"} defined $ENV{$p->{"default_env"}}
Truth table0%0/3
A and B
A= $p->{"default_env"}B= defined $ENV{$p->{"default_env"}}
ABresult
0X0
100
111
280 0 $cmd_args{$name} = $ENV{ $p->{default_env} };
281 } elsif (defined $p->{default}) {
282 0 $cmd_args{$name} = $p->{default};
283 }
284 }
285  
286 2 my $tool_command = $self->make_command(%cmd_args);
287  
288 # ── Output validation checks ──────────────────────────────────────────────
289 2 my $validation = '';
290 2 for my $out (@{ $self->{outputs} }) {
291 3 ▶ next unless $out->{required};
Branch: 1 branch
BranchTrueFalseTotal
unless $out->{"required"} 1 2 3
292 2 ▶ my $pat = $out->{pattern} // next;
Condition50%1/2
$out->{"pattern"} // (next)
l!l
20
MC/DC0%0/1
$out->{"pattern"} // (next)
$out->{"pattern"}
Truth table50%1/2
A
A= $out->{"pattern"}
Aresult
11
00
293 # Substitute {sample} with shell variable reference
294 2 (my $shell_pat = $pat) =~ s/\{sample\}/\${SAMPLE}/g;
295 2 $validation .= <<" BASH";
296 if [[ ! -s "\$SCRATCH/$shell_pat" ]]; then
297 echo "[nbilaunch] ERROR: required output not found or empty: $shell_pat" >&2
298 exit 1
299 fi
300 BASH
301 }
302  
303 2 my $activation = $self->activation_lines();
304  
305 # ── Scratch setup ─────────────────────────────────────────────────────────
306 # Priority: explicit scratch_dir arg > $TMPDIR (use_tmpdir) > /tmp
307 2 ▶ my $use_tmpdir = $self->{scratch}{use_tmpdir} // 0;
Condition50%1/2
$self->{"scratch"}{"use_tmpdir"} // 0
l!l
20
MC/DC0%0/1
$self->{"scratch"}{"use_tmpdir"} // 0
$self->{"scratch"}{"use_tmpdir"}
Truth table50%1/2
A
A= $self->{"scratch"}{"use_tmpdir"}
Aresult
11
00
308 my $scratch_base = defined $args{scratch_dir} ? $args{scratch_dir}
309 2 ▶ : $use_tmpdir ? '${TMPDIR:-/tmp}'
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'scratch_dir'} ? : 0 2 2
$use_tmpdir ? : 2 0 2
310 : '/tmp';
311 2 my $scratch_init = qq{SCRATCH=\$(mktemp -d "$scratch_base/${tool}_XXXXXXXX")};
312  
313 # ── Assemble script ───────────────────────────────────────────────────────
314 2 my $sep = '# ' . '─' x 74;
315  
316 2 my $script = <<"SCRIPT";
317 set -euo pipefail
318  
319 $sep
320 # Generated by nbilaunch v${nbi_l_version} / NBI::Slurm v${nbi_version}
321 # Tool: $tool v$version
322 # Launcher: $launcher_class
323 # Submitted: $submitted_at
324 # Manifest: $manifest_rel
325 $sep
326  
327 $sep
328 # Runtime variables - defined first so traps and manifest can reference them
329 SAMPLE="$sample"
330 OUTDIR="\$(realpath "$outdir" 2>/dev/null || echo "$outdir")"
331 $scratch_init
332 MANIFEST="\$OUTDIR/.nbilaunch/$sample.manifest.json"
333 $sep
334  
335 $sep
336 # Manifest update - called by ERR trap (failure) and at end (success).
337 # Uses a perl one-liner so there is no jq dependency on the HPC.
338 _nbi_manifest_update() {
339 local status="\$1" exit_code="\$2" completed_at
340 completed_at=\$(date -u +"%Y-%m-%dT%H:%M:%SZ")
341 perl -i -0777 -pe "
342 s{\\\"status\\\":\\\\s*\\\"[^\\\"]+\\\"}{\\\"status\\\": \\\"\$status\\\"};
343 s{\\\"exit_code\\\":\\\\s*null}{\\\"exit_code\\\": \$exit_code};
344 s{\\\"completed_at\\\":\\\\s*null}{\\\"completed_at\\\": \\\"\$completed_at\\\"};
345 " "\$MANIFEST"
346 }
347  
348 trap '_nbi_manifest_update failure \$?' ERR
349 $sep
350  
351 $sep
352 # Scratch cleanup on exit (runs even on success - scratch is empty after mv)
353 trap 'rm -rf "\$SCRATCH"' EXIT
354 $sep
355  
356 SCRIPT
357  
358 # Activation section (empty for singularity)
359 2 ▶ if ($activation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($activation) 2 0 2
360 2 $script .= <<"SCRIPT";
361 $sep
362 # Environment activation
363 $activation$sep
364  
365 SCRIPT
366 }
367  
368 2 $script .= <<"SCRIPT";
369  
370 $sep
371 # Tool command
372 $tool_command
373 $sep
374  
375 SCRIPT
376  
377 2 ▶ if ($validation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validation) 2 0 2
378 2 $script .= <<"SCRIPT";
379 $sep
380 # Output validation
381 ${validation}$sep
382  
383 SCRIPT
384 }
385  
386 2 $script .= <<"SCRIPT";
387 $sep
388 # Promote outputs from scratch to outdir and record success
389 mkdir -p "\$OUTDIR" "\$OUTDIR/.nbilaunch"
390 mv "\$SCRATCH"/* "\$OUTDIR"/
391 _nbi_manifest_update success 0
392 echo "[nbilaunch] Done. Outputs in: \$OUTDIR"
393 $sep
394 SCRIPT
395  
396 2 return $script;
397 }
398  
399 # ── _resolve_args(%args) ──────────────────────────────────────────────────────
400 # Internal: apply default_env and default fallbacks, inject slurm_sync values,
401 # and resolve absolute paths. Returns the resolved %args hash.
402 1 ▶sub _resolve_args {
Subroutine _resolve_args: called CC=14 SCAR=26.9
403 1 my ($self, %args) = @_;
404  
405 # Apply param defaults
406 1 for my $p (@{ $self->{params} }) {
407 3 my $name = $p->{name};
408 3 ▶ next if defined $args{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$name} 1 2 3
409 2 ▶ if ($p->{default_env} && defined $ENV{ $p->{default_env} }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p->{'default_env'} and defined $ENV{$p->{'default_env'}}) { } 1 1 2
elsif (defined $p->{'default'}) { } 1 0 1
Condition66%2/3
$p->{"default_env"} and defined $ENV{$p->{"default_env"}}
!ll&&!rl&&r
101
MC/DC50%1/2
$p->{"default_env"} and defined $ENV{$p->{"default_env"}}
$p->{"default_env"} defined $ENV{$p->{"default_env"}}
Truth table66%2/3
A and B
A= $p->{"default_env"}B= defined $ENV{$p->{"default_env"}}
ABresult
0X0
100
111
410 1 $args{$name} = $ENV{ $p->{default_env} };
411 } elsif (defined $p->{default}) {
412 1 $args{$name} = $p->{default};
413 }
414 }
415  
416 # Resolve absolute paths for file/dir inputs and params
417 1 for my $spec (@{ $self->{inputs} }, @{ $self->{params} }) {
418 5 my $name = $spec->{name};
419 5 ▶ next unless defined $args{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$name} 1 4 5
420 4 ▶ my $type = $spec->{type} // '';
Condition50%1/2
$spec->{"type"} // ""
l!l
40
MC/DC0%0/1
$spec->{"type"} // ""
$spec->{"type"}
Truth table50%1/2
A
A= $spec->{"type"}
Aresult
11
00
421 4 ▶ if ($type eq 'file' || $type eq 'dir') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "file" or $type eq "dir") 2 2 4
Condition100%3/3
$type eq "file" or $type eq "dir"
l!l&&r!l&&!r
112
MC/DC100%2/2
$type eq "file" or $type eq "dir"
$type eq "file" $type eq "dir"
Truth table100%3/3
A or B
A= $type eq "file"B= $type eq "dir"
ABresult
1X1
011
000
422 2 ▶ $args{$name} = realpath($args{$name}) // $args{$name};
Condition33%1/3
realpath($args{$name}) // $args{$name}
l!l&&r!l&&!r
200
MC/DC0%0/2
realpath($args{$name}) // $args{$name}
realpath($args{$name}) $args{$name}
Truth table33%1/3
A // B
A= realpath($args{$name})B= $args{$name}
ABresult
1X1
011
000
423 }
424 }
425  
426 # Absolute outdir
427 1 ▶ if (defined $args{outdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"outdir"}) 1 0 1
428 # realpath requires the path to exist; use abs_path fallback
429 1 my $abs = eval { realpath($args{outdir}) };
430 1 ▶ $args{outdir} = $abs if defined $abs;
Branch: 1 branch
BranchTrueFalseTotal
if defined $abs 1 0 1
431 }
432  
433 1 return %args;
434 }
435  
436 # ── _runtime_to_hours($str) ───────────────────────────────────────────────────
437 # Convert HH:MM:SS or simple hour strings to decimal hours for NBI::Opts.
438 1 ▶sub _runtime_to_hours {
Subroutine _runtime_to_hours: called CC=10 SCAR=43.7
439 1 my ($rt) = @_;
440 1 ▶ if ($rt =~ /^(\d+):(\d+):(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt =~ /^(\d+):(\d+):(\d+)$/u) 1 0 1
441 1 return $1 + $2 / 60 + $3 / 3600;
442 }
443 0 ▶ if ($rt =~ /^(\d+):(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt =~ /^(\d+):(\d+)$/u) 0 0 0
444 0 return $1 + $2 / 60;
445 }
446 0 ▶ if ($rt =~ /^(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt =~ /^(\d+)$/u) 0 0 0
447 0 return $1; # bare integer = hours
448 }
449 # Fall back: try NBI::Opts internal parser pattern
450 0 my $hours = 0;
451 0 my $upper = uc $rt;
452 0 ▶ while ($upper =~ /(\d+)([DHMS])/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upper =~ /(\d+)([DHMS])/gu) 0 0 0
453 0 my ($v, $u) = ($1, $2);
454 0 ▶ $hours += $v * 24 if $u eq 'D';
Branch: 1 branch
BranchTrueFalseTotal
if $u eq "D" 0 0 0
455 0 ▶ $hours += $v if $u eq 'H';
Branch: 1 branch
BranchTrueFalseTotal
if $u eq "H" 0 0 0
456 0 ▶ $hours += $v / 60 if $u eq 'M';
Branch: 1 branch
BranchTrueFalseTotal
if $u eq "M" 0 0 0
457 0 ▶ $hours += $v / 3600 if $u eq 'S';
Branch: 1 branch
BranchTrueFalseTotal
if $u eq "S" 0 0 0
458 }
459 0 ▶ return $hours || 1;
Condition0%0/2
$hours || 1
l!l
00
MC/DC0%0/1
$hours || 1
$hours
Truth table0%0/2
A
A= $hours
Aresult
11
00
460 }
461  
462 # ── build(%args) ─────────────────────────────────────────────────────────────
463 # The main entry point called by bin/nbilaunch.
464 #
465 # Validates inputs, resolves defaults, builds NBI::Job and NBI::Manifest.
466 # Returns a two-element list: ($job, $manifest).
467 #
468 # %args keys:
469 # All inputs/params by name (from nbilaunch arg parsing)
470 # outdir - output directory (required)
471 # sample_name - optional override for derived sample name
472 # slurm_queue - override slurm_defaults{queue}
473 # slurm_threads - override slurm_defaults{threads}
474 # slurm_memory - override slurm_defaults{memory} (GB)
475 # slurm_runtime - override slurm_defaults{runtime} (HH:MM:SS or hours)
476 1 ▶sub build {
Subroutine build: called CC=15 SCAR=28.2
Pod documented
477 1 my ($self, %args) = @_;
478  
479 1 require NBI::Job;
480 1 require NBI::Opts;
481 1 require NBI::Manifest;
482  
483 # ── Resolve Slurm resource values ─────────────────────────────────────────
484 1 ▶ my $queue = $args{slurm_queue} // $self->{slurm_defaults}{queue};
Condition33%1/3
$args{"slurm_queue"} // $self->{"slurm_defaults"}{"queue"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{"slurm_queue"} // $self->{"slurm_defaults"}{"queue"}
$args{"slurm_queue"} $self->{"slurm_defaults"}{"queue"}
Truth table33%1/3
A // B
A= $args{"slurm_queue"}B= $self->{"slurm_defaults"}{"queue"}
ABresult
1X1
011
000
485 1 ▶ my $threads = $args{slurm_threads} // $self->{slurm_defaults}{threads};
Condition33%1/3
$args{"slurm_threads"} // $self->{"slurm_defaults"}{"threads"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{"slurm_threads"} // $self->{"slurm_defaults"}{"threads"}
$args{"slurm_threads"} $self->{"slurm_defaults"}{"threads"}
Truth table33%1/3
A // B
A= $args{"slurm_threads"}B= $self->{"slurm_defaults"}{"threads"}
ABresult
1X1
011
000
486 1 ▶ my $mem_gb = $args{slurm_memory} // $self->{slurm_defaults}{memory};
Condition33%1/3
$args{"slurm_memory"} // $self->{"slurm_defaults"}{"memory"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{"slurm_memory"} // $self->{"slurm_defaults"}{"memory"}
$args{"slurm_memory"} $self->{"slurm_defaults"}{"memory"}
Truth table33%1/3
A // B
A= $args{"slurm_memory"}B= $self->{"slurm_defaults"}{"memory"}
ABresult
1X1
011
000
487 1 ▶ my $runtime = $args{slurm_runtime} // $self->{slurm_defaults}{runtime};
Condition33%1/3
$args{"slurm_runtime"} // $self->{"slurm_defaults"}{"runtime"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{"slurm_runtime"} // $self->{"slurm_defaults"}{"runtime"}
$args{"slurm_runtime"} $self->{"slurm_defaults"}{"runtime"}
Truth table33%1/3
A // B
A= $args{"slurm_runtime"}B= $self->{"slurm_defaults"}{"runtime"}
ABresult
1X1
011
000
488  
489 # Inject slurm_sync params (e.g. threads param mirrors Slurm --cpus)
490 1 for my $p (@{ $self->{params} }) {
491 3 ▶ next unless $p->{slurm_sync};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"slurm_sync"} 2 1 3
492 1 ▶ if ($p->{slurm_sync} eq 'threads') {
Branch: 2 branches
BranchTrueFalseTotal
if ($p->{'slurm_sync'} eq 'threads') { } 1 0 1
elsif ($p->{'slurm_sync'} eq 'memory') { } 0 0 0
493 1 $args{ $p->{name} } = $threads;
494 } elsif ($p->{slurm_sync} eq 'memory') {
495 0 $args{ $p->{name} } = $mem_gb;
496 }
497 }
498  
499 # ── Resolve and validate args ─────────────────────────────────────────────
500 1 %args = $self->_resolve_args(%args);
501 1 $self->validate(%args);
502  
503 # ── Derive sample name ────────────────────────────────────────────────────
504 1 my $sample = $self->sample_name(%args);
505 1 $args{sample} = $sample;
506  
507 # ── Paths ─────────────────────────────────────────────────────────────────
508 1 my $outdir = $args{outdir};
509 1 my $nbi_dir = "$outdir/.nbilaunch";
510 1 my $job_name = "$self->{name}_$sample";
511 1 my $manifest_path = "$nbi_dir/$sample.manifest.json";
512 1 my $script_rel = ".nbilaunch/${job_name}.script.sh";
513  
514 1 $args{manifest_path} = $manifest_path;
515  
516 # ── Generate script body ──────────────────────────────────────────────────
517 1 my $script_body = $self->generate_script(%args);
518  
519 # ── Build NBI::Opts ───────────────────────────────────────────────────────
520 1 my $hours = _runtime_to_hours($runtime);
521 1 my $mem_mb = $mem_gb * 1024;
522  
523 1 my $opts = NBI::Opts->new(
524 -queue => $queue,
525 -threads => $threads,
526 -memory => $mem_mb,
527 -time => $hours,
528 -tmpdir => $nbi_dir,
529 );
530  
531 # ── Build NBI::Job ────────────────────────────────────────────────────────
532 1 my $job = NBI::Job->new(
533 -name => $job_name,
534 -command => $script_body,
535 -opts => $opts,
536 );
537  
538 # Log/err go to provenance directory; %j is expanded by Slurm to the job ID
539 1 $job->outputfile = "$nbi_dir/${job_name}.%j.log";
540 1 $job->errorfile = "$nbi_dir/${job_name}.%j.err";
541  
542 # ── Collect resolved inputs/params/outputs for manifest ───────────────────
543 1 my %inp_snapshot;
544 1 for my $inp (@{ $self->{inputs} }) {
545 $inp_snapshot{ $inp->{name} } = $args{ $inp->{name} }
546 2 ▶ if defined $args{ $inp->{name} };
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$inp->{"name"}} 1 1 2
547 }
548  
549 1 my %par_snapshot;
550 1 for my $p (@{ $self->{params} }) {
551 $par_snapshot{ $p->{name} } = $args{ $p->{name} }
552 3 ▶ if defined $args{ $p->{name} };
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$p->{"name"}} 3 0 3
553 }
554  
555 1 my %out_snapshot;
556 1 for my $out (@{ $self->{outputs} }) {
557 2 ▶ my $pat = $out->{pattern} // next;
Condition50%1/2
$out->{"pattern"} // (next)
l!l
20
MC/DC0%0/1
$out->{"pattern"} // (next)
$out->{"pattern"}
Truth table50%1/2
A
A= $out->{"pattern"}
Aresult
11
00
558 2 (my $filename = $pat) =~ s/\{sample\}/$sample/g;
559 2 $out_snapshot{ $out->{name} } = $filename;
560 }
561  
562 # ── Build manifest ────────────────────────────────────────────────────────
563 my $manifest = NBI::Manifest->new(
564 tool => $self->{name},
565 tool_version => $self->{version},
566 1 sample => $sample,
567 outdir => $outdir,
568 inputs => \%inp_snapshot,
569 params => \%par_snapshot,
570 outputs => \%out_snapshot,
571 slurm_queue => $queue,
572 slurm_cpus => $threads,
573 slurm_mem_gb => $mem_gb,
574 script => $script_rel,
575 status => 'submitted',
576 );
577  
578 1 return ($job, $manifest);
579 }
580  
581 2 1;
582  
583 __END__
« bin/session ↑ Summary bin/slurm-load »