lib/ClarID/Tools/Command/code.pm

filter: Statementstmt 96.9% 600 / 619 Branchbran 58.8% 200 / 340 Conditioncond 51.3% 94 / 183 MC/DCmcdc 17.3% 18 / 104 Subroutinesub 100% 51 / 51 Podpod 50.0% 1 / 2 totaltotal 74.2% 964 / 1299 CC 255 SCAR 31.7
CC
255
Cov
78%
_decode_stub_biosample
51.1
_parse_from_codebook
48.6
_run_bulk
40.9
CRAP 23.7
↑ Summary blib/lib/ClarID/Tools/Command/qrcode.pm »
1 package ClarID::Tools::Command::code;
2 49 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 49 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 49 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 49 ▶use ClarID::Tools;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 49 ▶use ClarID::Tools::Util qw(load_yaml_file load_json_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 49 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 use MooX::Options
9 49 ▶ auto_help => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
10 version => $ClarID::Tools::VERSION,
11 usage => 'pod',
12 49 config_from_hash => {};
13 49 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 49 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 49 ▶use Types::Standard qw(Str Int Enum HashRef Undef ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 49 ▶use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 49 ▶use IO::Compress::Gzip qw(gzip $GzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 49 ▶use JSON::XS qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 49 ▶use File::Spec::Functions qw(catdir catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Tell App::Cmd this is a command
22 49 ▶use App::Cmd::Setup -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 49 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # Set development mode
26 49 ▶use constant DEVEL_MODE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # CLI options
29 # NB: Invalid parameter values (e.g., --format=foo) trigger App::Cmd usage/help
30 # This hides the detailed Types::Standard error
31 # Fix by overriding usage_error/options_usage
32 #
33 # Example (Types::Standard):
34 # perl -Ilib -MClarID::Tools::Command::code -we \
35 # 'ClarID::Tools::Command::code->new(format=>"stube", action=>"encode", entity=>"biosample")'
36 #
37 # Value "stube" did not pass type constraint "Enum["human","stub"]" (in $args->{"format"}) at -e line 1
38 # "Enum["human","stub"]" requires that the value is equal to "human" or "stub"
39 option entity => (
40 is => 'ro',
41 format => 's',
42 isa => Enum [qw/biosample subject biospecimen individual/],
43 48 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
44 48 ▶ return 'biosample' if $_[0] eq 'biospecimen';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "biospecimen" 0 48 48
45 48 ▶ return 'subject' if $_[0] eq 'individual';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "individual" 0 48 48
46 48 return $_[0];
47 },
48 49 doc =>
49 'biosample | subject (accepts synonyms: biospecimen -> biosample; individual -> subject)',
50 required => 1,
51 );
52 49 option format => (
53 is => 'ro',
54 format => 's',
55 isa => Enum [qw/human stub/],
56 doc => 'human | stub',
57 required => 1,
58 );
59 49 option action => (
60 is => 'ro',
61 format => 's',
62 isa => Enum [qw/encode decode/],
63 doc => 'encode | decode',
64 required => 1,
65 );
66 option codebook => (
67 is => 'ro',
68 format => 's',
69 isa => HashRef,
70 49 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
71 49 my $cb = load_yaml_file( $_[0] );
72 48 _apply_defaults($cb);
73 48 return $cb;
74 },
75 doc => 'path to codebook.yaml',
76 default =>
77 6 ▶ sub { catfile( $ClarID::Tools::share_dir, 'clarid-codebook.yaml' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 49 required => 1,
79 );
80 49 option infile => (
81 is => 'ro',
82 format => 's',
83 isa => Undef | Str,
84 doc => 'bulk input CSV/TSV',
85 );
86 49 option outfile => (
87 is => 'ro',
88 format => 's',
89 isa => Undef | Str,
90 doc => 'bulk output file',
91 );
92 option sep => (
93 is => 'ro',
94 format => 's',
95 isa => Str,
96 37 ▶ default => sub { ',' },
Subroutine __ANON__: called CC=1 SCAR=0.0
97 49 doc => 'separator',
98 );
99 option icd10_map => (
100 is => 'ro',
101 format => 's',
102 isa => Str,
103 48 ▶ default => sub { catfile( $ClarID::Tools::share_dir, 'icd10.json' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
104 49 doc => 'path to ICD-10 map JSON',
105 );
106 49 option with_condition_name => (
107 is => 'ro',
108 is_flag => 1,
109 doc => 'append human-readable condition_name on decode',
110 );
111 option subject_id_base62_width => (
112 is => 'ro',
113 format => 'i',
114 40 ▶ default => sub { 3 },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 49 doc => 'number of Base-62 characters to use for subject ID stubs',
116 );
117 option subject_id_pad_length => (
118 is => 'ro',
119 format => 'i',
120 48 ▶ default => sub { 5 },
Subroutine __ANON__: called CC=1 SCAR=0.0
121 49 doc =>
122 'decimal padding width for subject IDs in biosample/subject human format',
123 );
124 49 option clar_id => (
125 is => 'ro',
126 format => 's',
127 isa => Undef | Str,
128 doc => 'ID to decode (use --clar_id or --stub_id)',
129 short => 'stub_id',
130 );
131  
132 # Always load the ICD-10 order map at startup (JSON::XS parses ~70K entries in ~100 ms on modern hardware), simplifying stub logic.
133 # This one-time cost (~0.1 s, ~10 MB RAM) is negligible compared to the complexity of conditional or lazy loading.
134 option icd10_order => (
135 is => 'ro',
136 format => 's',
137 isa => HashRef,
138 coerce => \&load_json_file,
139 48 ▶ default => sub { catfile( $ClarID::Tools::share_dir, 'icd10_order.json' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
140 49 doc => 'path to icd10_order.json',
141 );
142  
143 49 has icd10_by_order => (
144 is => 'ro',
145 isa => ArrayRef,
146 lazy => 1,
147 builder => '_build_icd10_by_order',
148 );
149  
150 option max_conditions => (
151 is => 'ro',
152 format => 'i',
153 isa => Int,
154 48 ▶ default => sub { 10 },
Subroutine __ANON__: called CC=1 SCAR=0.0
155 49 doc => 'maximum number of ICD-10 codes allowed',
156 );
157  
158 # biosample fields
159 49 option project =>
160 ( is => 'ro', format => 's', isa => Undef | Str, doc => 'project key' );
161 49 option species =>
162 ( is => 'ro', format => 's', isa => Undef | Str, doc => 'species key' );
163 49 option tissue =>
164 ( is => 'ro', format => 's', isa => Undef | Str, doc => 'tissue key' );
165 49 option sample_type => (
166 is => 'ro',
167 format => 's',
168 isa => Undef | Str,
169 doc => 'sample_type key'
170 );
171 49 option assay =>
172 ( is => 'ro', format => 's', isa => Undef | Str, doc => 'assay key' );
173 49 option timepoint =>
174 ( is => 'ro', format => 's', isa => Undef | Str, doc => 'timepoint' );
175 49 option duration => (
176 is => 'ro',
177 format => 's',
178 isa => Undef | Str,
179 doc => 'duration: P<digit><D|W|M|Y> or P0N (Not Available)'
180 );
181 49 option batch =>
182 ( is => 'ro', format => 's', isa => Undef | Int, doc => 'batch' );
183 49 option replicate => (
184 is => 'ro',
185 format => 'i',
186 isa => Undef | Int,
187 doc => 'replicate number'
188 );
189  
190 # subject fields
191 49 option study =>
192 ( is => 'ro', format => 's', isa => Undef | Str, doc => 'study' );
193 49 option type => ( is => 'ro', format => 's', isa => Undef | Str, doc => 'type' );
194 49 option sex => ( is => 'ro', format => 's', isa => Undef | Str, doc => 'sex' );
195 49 option age_group =>
196 ( is => 'ro', format => 's', isa => Undef | Str, doc => 'age_group' );
197  
198 # Biosample + subject
199 49 option subject_id =>
200 ( is => 'ro', format => 'i', isa => Undef | Int, doc => 'subject_id' );
201 49 option condition => (
202 is => 'ro',
203 format => 's',
204 isa => Undef | Str,
205 doc => 'comma-separated ICD-10 codes (e.g. C22.0,C18.1)'
206 );
207  
208 # declare once, reuse everywhere:
209 49 my %FIELDS = (
210 biosample => {
211 encode => [
212 qw(
213 project species subject_id tissue sample_type assay
214 condition timepoint duration batch replicate
215 )
216 ],
217 decode => [
218 qw(
219 project species subject_id tissue sample_type assay
220 condition timepoint duration batch replicate
221 )
222 ],
223 },
224 subject => {
225 encode => [
226 qw(
227 study subject_id type condition sex age_group
228 )
229 ],
230 decode => [
231 qw(
232 study subject_id type condition sex age_group
233 )
234 ],
235 },
236 );
237  
238 65 ▶sub _encode_fields {
Subroutine _encode_fields: called CC=2 SCAR=7.0
239 65 my $self = shift;
240 65 my $e = $self->entity;
241 65 ▶ Carp::croak "Unknown entity '$e'" unless exists $FIELDS{$e};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $FIELDS{$e} 0 65 65
242 65 return @{ $FIELDS{$e}{encode} };
243 }
244  
245 34 ▶sub _decode_fields {
Subroutine _decode_fields: called CC=2 SCAR=7.0
246 34 my $self = shift;
247 34 my $e = $self->entity;
248 34 ▶ Carp::croak "Unknown entity '$e'" unless exists $FIELDS{$e};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $FIELDS{$e} 0 34 34
249 34 return @{ $FIELDS{$e}{decode} };
250 }
251  
252 # Validate required options
253 48 ▶sub BUILD {
Subroutine BUILD: called CC=20 SCAR=32.2
Pod undocumented
254 48 my $self = shift;
255  
256 # If we're in bulk mode, skip ALL of the single-record checks,
257 # including the CLI --condition parsing.
258 48 ▶ return if defined $self->infile;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->infile 11 37 48
259  
260 # Only from here on is single-record mode…
261  
262 # 1) Ensure --with-condition-name only on decode
263 37 ▶ if ( $self->with_condition_name && $self->action ne 'decode' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->with_condition_name and $self->action ne "decode") 0 37 37
Condition66%2/3
$self->with_condition_name and $self->action ne "decode"
!ll&&!rl&&r
3520
MC/DC0%0/2
$self->with_condition_name and $self->action ne "decode"
$self->with_condition_name $self->action ne "decode"
Truth table66%2/3
A and B
A= $self->with_condition_nameB= $self->action ne "decode"
ABresult
0X0
100
111
264 0 croak "--with-condition-name only makes sense when --action decode";
265 }
266  
267 # 2) Single-record encode: parse & validate the CLI --condition
268 37 ▶ if ( $self->action eq 'encode' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->action eq 'encode') { } 21 16 37
elsif ($self->action eq 'decode') { } 16 0 16
269  
270 # split into individual codes (profiled on Aug-09-25)
271 21 ▶ my @conds = split /\s*,\s*/, ( $self->condition // '' );
Condition50%1/2
$self->condition // ""
l!l
210
MC/DC0%0/1
$self->condition // ""
$self->condition
Truth table50%1/2
A
A= $self->condition
Aresult
11
00
272 21 ▶ croak "--condition must not be empty" unless @conds;
Branch: 1 branch
BranchTrueFalseTotal
unless @conds 0 21 21
273  
274 # enforce max
275 21 ▶ if ( @conds > $self->max_conditions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@conds > $self->max_conditions) 0 21 21
276 0 croak sprintf
277 "You passed %d conditions but max is %d",
278 scalar(@conds), $self->max_conditions;
279 }
280  
281 # pull regex from your codebook
282 my $pat_cfg =
283 $self->codebook->{entities}{ $self->entity }{condition_pattern}
284 21 ▶ or croak "No condition_pattern defined in your codebook";
Branch: 1 branch
BranchTrueFalseTotal
my $pat_cfg = $self->codebook->{"entities"}{$self->entity}{"condition_pattern"} or croak("No condition_pattern defined in your codebook") 21 0 21
285 my $re = $pat_cfg->{regex}
286 21 ▶ or croak "condition_pattern has no regex";
Branch: 1 branch
BranchTrueFalseTotal
my $re = $pat_cfg->{"regex"} or croak("condition_pattern has no regex") 21 0 21
287  
288 # validate each
289 21 for my $c (@conds) {
290 24 ▶ croak "Invalid condition '$c'"
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /^$re$/ 0 24 24
291 unless $c =~ /^$re$/;
292 }
293  
294 # stash for the encoder
295 21 $self->{_conds} = \@conds;
296  
297 # 3) Now enforce the usual “required field” checks
298 21 for my $field ( $self->_encode_fields ) {
299 211 ▶ next if $field eq 'batch' && !defined $self->batch;
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "batch" and not defined $self->batch 6 205 211
Condition100%3/3
$field eq "batch" and not defined $self->batch
!ll&&!rl&&r
194116
MC/DC100%2/2
$field eq "batch" and not defined $self->batch
$field eq "batch" not defined $self->batch
Truth table100%3/3
A and not(B)
A= $field eq "batch"B= not defined $self->batch
ABresult
0X0
100
111
300 205 ▶ next if $field eq 'replicate' && !defined $self->replicate;
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "replicate" and not defined $self->replicate 6 199 205
Condition100%3/3
$field eq "replicate" and not defined $self->replicate
!ll&&!rl&&r
188116
MC/DC100%2/2
$field eq "replicate" and not defined $self->replicate
$field eq "replicate" not defined $self->replicate
Truth table100%3/3
A and not(B)
A= $field eq "replicate"B= not defined $self->replicate
ABresult
0X0
100
111
301 199 ▶ croak "--$field is required for single-record encode"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->$field 0 199 199
302 unless defined $self->$field;
303 }
304 }
305 elsif ( $self->action eq 'decode' ) {
306  
307 # 4) Single-record decode needs a clar_id
308 16 ▶ croak "--clar_id is required for decode"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->clar_id 0 16 16
309 unless defined $self->clar_id;
310 }
311 }
312  
313 # lazy load
314 7 ▶sub _build_icd10_by_order {
Subroutine _build_icd10_by_order: called CC=2 SCAR=6.9
315 7 my $self = shift;
316  
317 # force-load the order hash if not done yet
318 7 my $ord_hash = $self->icd10_order;
319 7 my @by;
320 7 $by[ $ord_hash->{$_} ] = $_ for keys %$ord_hash;
321 7 return \@by;
322 }
323  
324 # Main dispatch
325 48 ▶sub execute {
Subroutine execute: called CC=19 SCAR=33.1
Pod documented
326 48 my $self = shift;
327  
328 # lazy load ICD-10 map if requested
329 48 my $code2name;
330 48 ▶ if ( $self->action eq 'decode' && $self->with_condition_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "decode" and $self->with_condition_name) 4 44 48
Condition100%3/3
$self->action eq "decode" and $self->with_condition_name
!ll&&!rl&&r
27174
MC/DC100%2/2
$self->action eq "decode" and $self->with_condition_name
$self->action eq "decode" $self->with_condition_name
Truth table100%3/3
A and B
A= $self->action eq "decode"B= $self->with_condition_name
ABresult
0X0
100
111
331 4 ▶ croak "ICD-10 map not found at '" . $self->icd10_map . "'"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->icd10_map 0 4 4
332 unless -e $self->icd10_map;
333 4 ▶ open my $jh, '<', $self->icd10_map or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $jh, "<", $self->icd10_map or croak($!) 4 0 4
334 4 local $/;
335 4 my $jtxt = <$jh>;
336 4 close $jh;
337 4 $code2name = decode_json($jtxt);
338 }
339  
340 # bulk vs single-record
341 48 my ( $in_fh, $out_fh );
342 48 ▶ if ( defined( my $infile = $self->infile ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $infile = $self->infile)) 11 37 48
343  
344 # Input handle (auto‐gunzip for .gz)
345 11 ▶ if ( $infile =~ /\.gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($infile =~ /\.gz$/) { } 0 11 11
346 0 ▶ $in_fh = IO::Uncompress::Gunzip->new($infile)
Branch: 1 branch
BranchTrueFalseTotal
$in_fh = "IO::Uncompress::Gunzip"->new($infile) or croak("gunzip failed on '${infile}': $ClarID::Tools::Command::code::GunzipError") 0 0 0
347 or croak "gunzip failed on '$infile': $GunzipError";
348 }
349 else {
350 11 ▶ open my $fh_in, '<', $infile
Branch: 1 branch
BranchTrueFalseTotal
open my $fh_in, "<", $infile or croak("Could not open '${infile}': $!") 11 0 11
351 or croak "Could not open '$infile': $!";
352 11 $in_fh = $fh_in;
353 }
354  
355 # Output handle (auto‐gzip for .gz, else file or STDOUT)
356 11 ▶ if ( defined $self->outfile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->outfile) { } 0 11 11
357 0 ▶ if ( $self->outfile =~ /\.gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->outfile =~ /\.gz$/) { } 0 0 0
358 0 ▶ $out_fh = IO::Compress::Gzip->new( $self->outfile )
Branch: 1 branch
BranchTrueFalseTotal
$out_fh = "IO::Compress::Gzip"->new($self->outfile) or croak("gzip failed on '$self->outfile': $ClarID::Tools::Command::code::GzipError") 0 0 0
359 or croak "gzip failed on '$self->outfile': $GzipError";
360 }
361 else {
362 0 ▶ open my $fh_out, '>', $self->outfile
Branch: 1 branch
BranchTrueFalseTotal
open my $fh_out, ">", $self->outfile or croak("Could not open '$self->outfile' for writing: $!") 0 0 0
363 or croak "Could not open '$self->outfile' for writing: $!";
364 0 $out_fh = $fh_out;
365 }
366 }
367 else {
368 11 $out_fh = *STDOUT;
369 }
370  
371 # Delegate to bulk processor
372 11 return $self->_run_bulk( $in_fh, $self->sep, $out_fh, $code2name );
373 }
374  
375 # single-record mode — unwrap either top-level or entities-wrapped codebook
376 37 my $full = $self->codebook;
377  
378 # if the YAML has an "entities:" wrapper, drill into it; otherwise assume old style
379 my $root =
380 exists $full->{entities}
381 ? $full->{entities}
382 37 ▶ : $full;
Branch: 1 branch
BranchTrueFalseTotal
exists $full->{'entities'} ? : 37 0 37
383 37 ▶ my $cb = $root->{ $self->entity }
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $$root{$self->entity} or croak(q[No codebook for '] . $self->entity . q[' in your YAML]) 37 0 37
384 or croak "No codebook for '" . $self->entity . "' in your YAML";
385 37 ▶ if ( $self->action eq 'encode' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq 'encode') { } 21 16 37
386 21 my @vals = map { $self->$_ } $self->_encode_fields;
387 21 my $meth = sprintf '_encode_%s_%s', $self->format, $self->entity;
388 21 say $self->$meth( $cb, @vals );
389 }
390 else {
391 16 my $meth = sprintf '_decode_%s_%s', $self->format, $self->entity;
392 16 my $res = $self->$meth( $cb, $self->clar_id );
393  
394 # Print each field in order
395 12 say "$_: $res->{$_}" for $self->_decode_fields;
396  
397 # Optionally append human-readable condition_name
398 12 ▶ if ( $self->with_condition_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->with_condition_name) 2 10 12
399  
400 # split whatever separator you're using in $res->{condition}
401 2 my @keys = split /[+;]/, $res->{condition};
402  
403 # normalize to dot‐free lookup key and fetch each name
404 my @names = map {
405 2 ( my $clean = $_ ) =~ s/\W//g;
406 4 ▶ $code2name->{$clean} // ''
Condition50%1/2
$code2name->{$clean} // ""
l!l
40
MC/DC0%0/1
$code2name->{$clean} // ""
$code2name->{$clean}
Truth table50%1/2
A
A= $code2name->{$clean}
Aresult
11
00
407 } @keys;
408  
409 # join multiple names with semicolons
410 2 say "condition_name: " . join( ';', @names );
411 }
412 }
413 }
414  
415 # Bulk processing for single or multiple records
416 11 ▶sub _run_bulk {
Subroutine _run_bulk: called CC=34 SCAR=37.1
417 11 my ( $self, $in_fh, $sep, $out_fh, $code2name ) = @_;
418 11 ▶ $sep ||= ',';
Condition50%1/2
$sep ||= ","
l!l
110
MC/DC0%0/1
$sep ||= ","
$sep
Truth table50%1/2
A
A= $sep
Aresult
11
00
419  
420 # set up CSV parser/writer
421 11 my $csv = Text::CSV_XS->new( { sep_char => $sep } );
422 11 my $csv_out = Text::CSV_XS->new(
423 {
424 sep_char => $sep,
425 binary => 1,
426 eol => "\n",
427 }
428 );
429 11 ▶ croak "Failed to initialize CSV writer" unless $csv_out;
Branch: 1 branch
BranchTrueFalseTotal
unless $csv_out 0 11 11
430  
431 # read header row and set column names
432 11 ▶ my $hdr = $csv->getline($in_fh)
Branch: 1 branch
BranchTrueFalseTotal
my $hdr = $csv->getline($in_fh) or croak("Failed to read header") 11 0 11
433 or croak "Failed to read header";
434 11 $csv->column_names(@$hdr);
435  
436 # unwrap entity wrapper in the codebook for bulk mode
437 11 my $full = $self->codebook;
438 11 ▶ my $root = exists $full->{entities} ? $full->{entities} : $full;
Branch: 1 branch
BranchTrueFalseTotal
exists $full->{'entities'} ? : 11 0 11
439 11 ▶ my $cb = $root->{ $self->entity }
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $$root{$self->entity} or croak(q[No codebook for '] . $self->entity . q[']) 11 0 11
440 or croak "No codebook for '" . $self->entity . "'";
441  
442 # write output header
443 11 ▶ if ( $self->action eq 'encode' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq 'encode') { } 6 5 11
444 6 ▶ my $label = $self->format eq 'stub' ? 'stub_id' : 'clar_id';
Branch: 1 branch
BranchTrueFalseTotal
$self->format eq 'stub' ? : 3 3 6
445 6 ▶ $csv_out->print( $out_fh, [ @$hdr, $label ] )
Branch: 1 branch
BranchTrueFalseTotal
$csv_out->print($out_fh, [@$hdr, $label]) or croak('Failed to write output header: ' . $csv_out->error_diag) 6 0 6
446 or croak "Failed to write output header: " . $csv_out->error_diag;
447 }
448 else {
449 5 my @cols = ( @$hdr, $self->_decode_fields );
450 5 ▶ push @cols, 'condition_name' if $self->with_condition_name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->with_condition_name 2 3 5
451 5 ▶ $csv_out->print( $out_fh, \@cols )
Branch: 1 branch
BranchTrueFalseTotal
$csv_out->print($out_fh, \@cols) or croak('Failed to write output header: ' . $csv_out->error_diag) 5 0 5
452 or croak "Failed to write output header: " . $csv_out->error_diag;
453 }
454  
455 # process each row
456 11 ▶ while ( my $row = $csv->getline_hr($in_fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $csv->getline_hr($in_fh)) 40 11 51
457  
458 40 ▶ if ( $self->action eq 'encode' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq 'encode') { } 23 17 40
459  
460 # ——— per-row multi‐condition support (CSV uses ';') ———
461 23 ▶ my @conds = split /\s*;\s*/, $row->{condition} // '';
Condition50%1/2
$row->{"condition"} // ""
l!l
230
MC/DC0%0/1
$row->{"condition"} // ""
$row->{"condition"}
Truth table50%1/2
A
A= $row->{"condition"}
Aresult
11
00
462 23 ▶ croak "Missing or empty condition in row" unless @conds;
Branch: 1 branch
BranchTrueFalseTotal
unless @conds 0 23 23
463 23 ▶ if ( @conds > $self->max_conditions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@conds > $self->max_conditions) 0 23 23
464 0 croak sprintf
465 "You passed %d conditions but max is %d",
466 scalar(@conds), $self->max_conditions;
467 }
468  
469 # validate each against the codebook regex
470 my $pat_cfg = $cb->{condition_pattern}
471 23 ▶ or croak "No condition_pattern in codebook";
Branch: 1 branch
BranchTrueFalseTotal
my $pat_cfg = $cb->{"condition_pattern"} or croak("No condition_pattern in codebook") 23 0 23
472 my $re = $pat_cfg->{regex}
473 23 ▶ or croak "condition_pattern has no regex";
Branch: 1 branch
BranchTrueFalseTotal
my $re = $pat_cfg->{"regex"} or croak("condition_pattern has no regex") 23 0 23
474 23 for my $c (@conds) {
475 26 ▶ croak "Invalid condition '$c'"
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /^$re$/ 0 26 26
476 unless $c =~ /^$re$/;
477 }
478  
479 # stash for the encoder
480 23 $self->{_conds} = \@conds;
481  
482 my @args = map {
483 23 my $value = $row->{$_};
484 # Empty optional CSV cells are absent, like omitted CLI options.
485 213 ▶ ( ( $_ eq 'batch' || $_ eq 'replicate' )
Branch: 1 branch
BranchTrueFalseTotal
($_ eq 'batch' || $_ eq 'replicate') && defined $value && $value =~ /^\s*$/ ? : 12 201 213
Condition88%8/9
$_ eq "batch" || $_ eq "replicate" and defined $value and $value =~ /^\s*$/
!ll&&!rl&&r
1831812
$_ eq "batch" || $_ eq "replicate" and defined $value
!ll&&!rl&&r
183030
$_ eq "batch" or $_ eq "replicate"
l!l&&r!l&&!r
1515183
MC/DC75%3/4
$_ eq "batch" || $_ eq "replicate" and defined $value and $value =~ /^\s*$/
$_ eq "batch" $_ eq "replicate" defined $value $value =~ /^\s*$/
Truth table71%5/7
A or B and C and D
A= $_ eq "batch"B= $_ eq "replicate"C= defined $valueD= $value =~ /^\s*$/
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
486 && defined $value && $value =~ /^\s*$/ ) ? undef : $value;
487 } $self->_encode_fields;
488 23 my $meth = sprintf '_encode_%s_%s', $self->format, $self->entity;
489 23 my $out = $self->$meth( $cb, @args );
490 23 ▶ $csv_out->print( $out_fh, [ ( map { $row->{$_} // '' } @$hdr ), $out ] )
Branch: 1 branch
BranchTrueFalseTotal
$csv_out->print($out_fh, [map({$row->{$_} // "";} @$hdr), $out]) or croak('Failed to write output row: ' . $csv_out->error_diag) 23 0 23
Condition50%1/2
$row->{$_} // ""
l!l
2360
MC/DC0%0/1
$row->{$_} // ""
$row->{$_}
Truth table50%1/2
A
A= $row->{$_}
Aresult
11
00
491 or croak "Failed to write output row: " . $csv_out->error_diag;
492 }
493 else {
494 # enforce column based on --format
495 17 my ( $meth, $id_col );
496 17 ▶ if ( $self->format eq 'human' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format eq 'human') { } 13 4 17
497 13 $meth = sprintf '_decode_human_%s', $self->entity;
498 13 $id_col = 'clar_id';
499 }
500 else { # format eq 'stub'
501 4 $meth = sprintf '_decode_stub_%s', $self->entity;
502 4 $id_col = 'stub_id';
503 }
504  
505 croak "Missing $id_col in input row"
506 17 ▶ unless defined $row->{$id_col};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row->{$id_col} 0 17 17
507  
508 17 my $res = $self->$meth( $cb, $row->{$id_col} );
509  
510 17 ▶ my @out = map { $res->{$_} // '' } $self->_decode_fields;
Condition50%1/2
$res->{$_} // ""
l!l
1470
MC/DC0%0/1
$res->{$_} // ""
$res->{$_}
Truth table50%1/2
A
A= $res->{$_}
Aresult
11
00
511 17 ▶ if ( $self->with_condition_name && $code2name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->with_condition_name and $code2name) 5 12 17
Condition66%2/3
$self->with_condition_name and $code2name
!ll&&!rl&&r
1205
MC/DC50%1/2
$self->with_condition_name and $code2name
$self->with_condition_name $code2name
Truth table66%2/3
A and B
A= $self->with_condition_nameB= $code2name
ABresult
0X0
100
111
512 5 my @codes = split /[+;]/, $res->{condition};
513 my @names = map {
514 5 ( my $c = $_ ) =~ s/\W//g;
515 8 ▶ $code2name->{$c} // ''
Condition100%2/2
$code2name->{$c} // ""
l!l
71
MC/DC100%1/1
$code2name->{$c} // ""
$code2name->{$c}
Truth table100%2/2
A
A= $code2name->{$c}
Aresult
11
00
516 } @codes;
517 5 push @out, join( ';', @names );
518 }
519  
520 17 ▶ $csv_out->print( $out_fh, [ ( map { $row->{$_} // '' } @$hdr ), @out ] )
Branch: 1 branch
BranchTrueFalseTotal
$csv_out->print($out_fh, [map({$row->{$_} // "";} @$hdr), @out]) or croak('Failed to write output row: ' . $csv_out->error_diag) 17 0 17
Condition50%1/2
$row->{$_} // ""
l!l
450
MC/DC0%0/1
$row->{$_} // ""
$row->{$_}
Truth table50%1/2
A
A= $row->{$_}
Aresult
11
00
521 or croak "Failed to write output row: " . $csv_out->error_diag;
522 }
523 }
524  
525 11 return;
526 }
527  
528 210 ▶sub _validate_field {
Subroutine _validate_field: called CC=3 SCAR=13.1
529 210 my ( $fmap, $f, $v ) = @_;
530 210 ▶ croak "Invalid $f '$v'" unless defined $v && exists $fmap->{$v};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and exists $fmap->{$v} 0 210 210
Condition33%1/3
defined $v and exists $fmap->{$v}
!ll&&!rl&&r
00210
MC/DC0%0/2
defined $v and exists $fmap->{$v}
defined $v exists $fmap->{$v}
Truth table33%1/3
A and B
A= defined $vB= exists $fmap->{$v}
ABresult
0X0
100
111
531 }
532  
533 # Cache compiled regex + placeholder counts per pattern config
534 111 ▶sub _prep_pattern {
Subroutine _prep_pattern: called CC=7 SCAR=19.7
535 111 my ( $pcfg, $mode ) = @_;
536 111 ▶ $pcfg->{_re} ||= qr/^(?:$pcfg->{regex})$/;
Condition100%2/2
$pcfg->{"_re"} ||= qr/^(?:$pcfg->{"regex"})$/
l!l
4071
MC/DC100%1/1
$pcfg->{"_re"} ||= qr/^(?:$pcfg->{"regex"})$/
$pcfg->{"_re"}
Truth table100%2/2
A
A= $pcfg->{"_re"}
Aresult
11
00
537  
538 my $fmt =
539 $mode eq 'human'
540 ? ( $pcfg->{code_format} // '%s' )
541 111 ▶ : ( $pcfg->{stub_format} // '%s' );
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'human' ? : 70 41 111
Condition50%2/4
$pcfg->{"code_format"} // "%s"
l!l
700
$pcfg->{"stub_format"} // "%s"
l!l
410
MC/DC0%0/1
$pcfg->{"code_format"} // "%s"
$pcfg->{"code_format"}
MC/DC0%0/1
$pcfg->{"stub_format"} // "%s"
$pcfg->{"stub_format"}
Truth table50%1/2
A
A= $pcfg->{"code_format"}
Aresult
11
00
Truth table50%1/2
A
A= $pcfg->{"stub_format"}
Aresult
11
00
542  
543 111 ▶ my $need_key = $mode eq 'human' ? '_need_human' : '_need_stub';
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'human' ? : 70 41 111
544 111 ▶ $pcfg->{$need_key} //= do {
Condition66%2/3
$pcfg->{$need_key} //= do { (my $t = $fmt) =~ s/%%//g; scalar(() = $t =~ /%/g) }
l!l&&r!l&&!r
35760
MC/DC0%0/2
$pcfg->{$need_key} //= do { (my $t = $fmt) =~ s/%%//g; scalar(() = $t =~ /%/g) }
$pcfg->{$need_key} do { (my $t = $fmt) =~ s/%%//g; scalar(() = $t =~ /%/g) }
Truth table66%2/3
A //= B
A= $pcfg->{$need_key}B= do { (my $t = $fmt) =~ s/%%//g; scalar(() = $t =~ /%/g) }
ABresult
1X1
011
000
545 76 ( my $t = $fmt ) =~ s/%%//g; # ignore literal %%
546 76 scalar( () = $t =~ /%/g ) # count % directives
547 };
548  
549 111 return ( $pcfg->{_re}, $fmt, $pcfg->{$need_key} );
550 }
551  
552 # Generic parser (grab captures; safe for alternation like P0N)
553 98 ▶sub _parse_field {
Subroutine _parse_field: called CC=10 SCAR=24.8
554 98 my ( $val, $map, $pcfg, $mode, $field ) = @_;
555  
556 98 ▶ croak "Missing value for $field" unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 98 98
557 98 ▶ croak "No pattern for $field" unless $pcfg && $pcfg->{regex};
Branch: 1 branch
BranchTrueFalseTotal
unless $pcfg and $pcfg->{"regex"} 0 98 98
Condition33%1/3
$pcfg and $pcfg->{"regex"}
!ll&&!rl&&r
0098
MC/DC0%0/2
$pcfg and $pcfg->{"regex"}
$pcfg $pcfg->{"regex"}
Truth table33%1/3
A and B
A= $pcfgB= $pcfg->{"regex"}
ABresult
0X0
100
111
558  
559 # Duration width is structural, not a codebook extension point. The
560 # published 0.02/0.03 codebooks used \d+ here by mistake, so enforce the
561 # specification before applying any supported codebook's pattern.
562 98 ▶ croak "Invalid duration '$val'"
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "duration" and not $val =~ /^P?(?:[0-9][DWMY]|0N)$/ 8 90 98
Condition100%3/3
$field eq "duration" and not $val =~ /^P?(?:[0-9][DWMY]|0N)$/
!ll&&!rl&&r
40508
MC/DC100%2/2
$field eq "duration" and not $val =~ /^P?(?:[0-9][DWMY]|0N)$/
$field eq "duration" not $val =~ /^P?(?:[0-9][DWMY]|0N)$/
Truth table100%3/3
A and not(B)
A= $field eq "duration"B= not $val =~ /^P?(?:[0-9][DWMY]|0N)$/
ABresult
0X0
100
111
563 if $field eq 'duration' && $val !~ /^P?(?:[0-9][DWMY]|0N)$/;
564  
565 90 my ( $re, $fmt, $need ) = _prep_pattern( $pcfg, $mode );
566 90 ▶ croak "Invalid $field '$val'" unless $val =~ $re;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /$re/ 0 90 90
567  
568 90 my @caps = grep { defined } ( $val =~ $re ); # drop undef from alternation
569 90 ▶ splice @caps, $need if @caps > $need; # never pass extra args
Branch: 1 branch
BranchTrueFalseTotal
if @caps > $need 0 90 90
570  
571 90 ▶ return $need ? sprintf( $fmt, @caps ? @caps : ($val) ) : $fmt;
Branch: 2 branches
BranchTrueFalseTotal
$need ? : 90 0 90
@caps ? : 90 0 90
572 }
573  
574 # Static-or-pattern parser (uses codebook map first, then pattern)
575 21 ▶sub _parse_from_codebook {
Subroutine _parse_from_codebook: called CC=15 SCAR=38.8
576 21 my ( $val, $map, $pcfg, $mode ) = @_;
577  
578 # 1) static map entry (e.g., tokens)
579 21 ▶ if ( $map && ref $map eq 'HASH' && exists $map->{$val} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map and ref $map eq "HASH" and exists $map->{$val}) 0 21 21
Condition33%2/6
$map and ref $map eq "HASH" and exists $map->{$val}
!ll&&!rl&&r
2100
$map and ref $map eq "HASH"
!ll&&!rl&&r
2100
MC/DC0%0/3
$map and ref $map eq "HASH" and exists $map->{$val}
$map ref $map eq "HASH" exists $map->{$val}
Truth table25%1/4
A and B and C
A= $mapB= ref $map eq "HASH"C= exists $map->{$val}
ABCresult
0XX0
10X0
1100
1111
580 0 ▶ my $k = $mode eq 'human' ? 'code' : 'stub_code';
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'human' ? : 0 0 0
581 0 ▶ return $map->{$val}{$k} if defined $map->{$val}{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $map->{$val}{$k} 0 0 0
582 }
583  
584 # 2) require a value
585 21 ▶ croak "Missing value for field" unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 21 21
586  
587 # 3) pattern path (captures-aware) or raw fallback
588 21 ▶ if ( $pcfg && $pcfg->{regex} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pcfg and $pcfg->{"regex"}) 21 0 21
Condition33%1/3
$pcfg and $pcfg->{"regex"}
!ll&&!rl&&r
0021
MC/DC0%0/2
$pcfg and $pcfg->{"regex"}
$pcfg $pcfg->{"regex"}
Truth table33%1/3
A and B
A= $pcfgB= $pcfg->{"regex"}
ABresult
0X0
100
111
589 21 my ( $re, $fmt, $need ) = _prep_pattern( $pcfg, $mode );
590 21 ▶ croak "Invalid value '$val' for field" unless $val =~ $re;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /$re/ 0 21 21
591  
592 21 my @caps = grep { defined } ( $val =~ $re );
593 21 ▶ splice @caps, $need if @caps > $need;
Branch: 1 branch
BranchTrueFalseTotal
if @caps > $need 0 21 21
594  
595 # for stub mode, if no captures, normalize raw by stripping non-word
596 21 ▶ if ( !@caps && $need == 1 && $mode eq 'stub' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @caps and $need == 1 and $mode eq "stub") 0 21 21
Condition33%2/6
not @caps and $need == 1 and $mode eq "stub"
!ll&&!rl&&r
2100
not @caps and $need == 1
!ll&&!rl&&r
2100
MC/DC0%0/3
not @caps and $need == 1 and $mode eq "stub"
not @caps $need == 1 $mode eq "stub"
Truth table25%1/4
not(A) and B and C
A= not @capsB= $need == 1C= $mode eq "stub"
ABCresult
0XX0
10X0
1100
1111
597 0 ( my $raw = $val ) =~ s/\W//g;
598 0 return sprintf( $fmt, $raw );
599 }
600  
601 21 ▶ return @caps ? sprintf( $fmt, @caps ) : sprintf( $fmt, $val );
Branch: 1 branch
BranchTrueFalseTotal
@caps ? : 21 0 21
602 }
603  
604 0 return $val;
605 }
606  
607 #----------------------------------------------------------------
608 # Human biosample encoder
609 #----------------------------------------------------------------
610 # Human biosample encoder
611 17 ▶sub _encode_human_biosample {
Subroutine _encode_human_biosample: called CC=13 SCAR=27.5
612 my (
613 17 $self, $cb, $pr_raw, $sp, $sid, $ti, $st,
614 $as, $co, $tp, $du, $ba, $re
615 ) = @_;
616  
617 # normalize project key
618 17 my $pr = $pr_raw;
619 17 ▶ unless ( exists $cb->{project}{$pr} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cb->{"project"}{$pr}) 0 17 17
620 0 ( my $alt = $pr_raw ) =~ tr/-/_/;
621 0 ▶ $pr = $alt if exists $cb->{project}{$alt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cb->{"project"}{$alt} 0 0 0
622 }
623 17 ▶ croak "Invalid project '$pr_raw'" unless exists $cb->{project}{$pr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cb->{"project"}{$pr} 0 17 17
624  
625 # basic field validation
626 17 _validate_field( $cb->{project}, 'project', $pr );
627 17 _validate_field( $cb->{species}, 'species', $sp );
628 17 _validate_field( $cb->{tissue}, 'tissue', $ti );
629 17 _validate_field( $cb->{sample_type}, 'sample_type', $st );
630 17 _validate_field( $cb->{assay}, 'assay', $as );
631  
632 # subject_id
633 17 my $pad = $self->subject_id_pad_length;
634 17 ▶ croak "Invalid pad length '$pad'" unless $pad =~ /^\d+$/ && $pad > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $pad =~ /^\d+$/ and $pad > 0 0 17 17
Condition33%1/3
$pad =~ /^\d+$/ and $pad > 0
!ll&&!rl&&r
0017
MC/DC0%0/2
$pad =~ /^\d+$/ and $pad > 0
$pad =~ /^\d+$/ $pad > 0
Truth table33%1/3
A and B
A= $pad =~ /^\d+$/B= $pad > 0
ABresult
0X0
100
111
635 17 my $max = 10**$pad - 1;
636 17 ▶ croak "Bad subject_id '$sid' (0-$max)"
Branch: 1 branch
BranchTrueFalseTotal
unless $sid =~ /^\d+$/ and $sid >= 0 and $sid <= $max 0 17 17
Condition33%2/6
$sid =~ /^\d+$/ and $sid >= 0 and $sid <= $max
!ll&&!rl&&r
0017
$sid =~ /^\d+$/ and $sid >= 0
!ll&&!rl&&r
0017
MC/DC0%0/3
$sid =~ /^\d+$/ and $sid >= 0 and $sid <= $max
$sid =~ /^\d+$/ $sid >= 0 $sid <= $max
Truth table25%1/4
A and B and C
A= $sid =~ /^\d+$/B= $sid >= 0C= $sid <= $max
ABCresult
0XX0
10X0
1100
1111
637 unless $sid =~ /^\d+$/ && $sid >= 0 && $sid <= $max;
638  
639 # ------ multi‐condition support ------
640 17 my @conds = @{ delete $self->{_conds} }; # pull in the validated list
641 my @human = map {
642 17 _parse_from_codebook( $_, $cb->{condition}, $cb->{condition_pattern},
643 21 'human' )
644 } @conds;
645 17 my $cond_code = join '+', @human;
646  
647 # -------------------------------------
648 # timepoint (strict codebook lookup)
649 17 _validate_field( $cb->{timepoint}, 'timepoint', $tp );
650 17 my $pt_code = $cb->{timepoint}{$tp}{code};
651  
652 # duration (pattern-drivem only)
653 my $dur_code =
654 17 _parse_field( $du, undef, $cb->{duration_pattern}, 'human', 'duration' );
655  
656 # batch (pattern-driven only)
657 my $batch_code =
658 defined $ba
659 13 ▶ ? _parse_field( $ba, undef, $cb->{batch_pattern}, 'human', 'batch' )
Branch: 1 branch
BranchTrueFalseTotal
defined $ba ? : 9 4 13
660 : ();
661  
662 # replicate (pattern-driven only)
663 my $rep_code =
664 defined $re
665 13 ▶ ? _parse_field( $re, undef, $cb->{replicate_pattern}, 'human',
Branch: 1 branch
BranchTrueFalseTotal
defined $re ? : 9 4 13
666 'replicate' )
667 : ();
668  
669 # assemble
670 my @parts = (
671 $cb->{project}{$pr}{code}, $cb->{species}{$sp}{code},
672 sprintf( "%0${pad}d", $sid ), $cb->{tissue}{$ti}{code},
673 $cb->{sample_type}{$st}{code}, $cb->{assay}{$as}{code},
674 13 $cond_code, $pt_code,
675 $dur_code,
676 );
677 13 ▶ push @parts, $batch_code if defined $batch_code;
Branch: 1 branch
BranchTrueFalseTotal
if defined $batch_code 9 4 13
678 13 ▶ push @parts, $rep_code if defined $rep_code;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rep_code 9 4 13
679  
680 13 return join( '-', @parts );
681 }
682  
683 # Human biosample decoder
684 14 ▶sub _decode_human_biosample {
Subroutine _decode_human_biosample: called CC=21 SCAR=32.3
685 14 my ( $self, $cb, $id ) = @_;
686 14 my @p = split /-/, $id;
687 14 ▶ croak "Bad biosample ID" unless @p >= 9;
Branch: 1 branch
BranchTrueFalseTotal
unless @p >= 9 0 14 14
688 14 my ( $prc, $sc, $sid, $tc, $stc, $ac, $cn, $ptc, $du, @rest ) = @p;
689  
690 my ($project) =
691 14 grep { $cb->{project}{$_}{code} eq $prc } keys %{ $cb->{project} };
692 14 ▶ croak "Unknown project code '$prc'" unless defined $project;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $project 0 14 14
693 my ($species) =
694 14 grep { $cb->{species}{$_}{code} eq $sc } keys %{ $cb->{species} };
695 14 ▶ croak "Unknown species code '$sc'" unless defined $species;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $species 0 14 14
696 my ($tissue) =
697 14 grep { $cb->{tissue}{$_}{code} eq $tc } keys %{ $cb->{tissue} };
698 14 ▶ croak "Unknown tissue code '$tc'" unless defined $tissue;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tissue 0 14 14
699 84 my ($stype) = grep { $cb->{sample_type}{$_}{code} eq $stc }
700 14 keys %{ $cb->{sample_type} };
701 14 ▶ croak "Unknown sample_type code '$stc'" unless defined $stype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stype 0 14 14
702 14 my ($assay) = grep { $cb->{assay}{$_}{code} eq $ac } keys %{ $cb->{assay} };
703 14 ▶ croak "Unknown assay code '$ac'" unless defined $assay;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $assay 0 14 14
704  
705 # parse subject_id using dynamic pad length
706 14 my $pad = $self->subject_id_pad_length;
707 14 ▶ croak "Invalid pad length '$pad'" unless $pad =~ /^\d+$/ && $pad > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $pad =~ /^\d+$/ and $pad > 0 0 14 14
Condition33%1/3
$pad =~ /^\d+$/ and $pad > 0
!ll&&!rl&&r
0014
MC/DC0%0/2
$pad =~ /^\d+$/ and $pad > 0
$pad =~ /^\d+$/ $pad > 0
Truth table33%1/3
A and B
A= $pad =~ /^\d+$/B= $pad > 0
ABresult
0X0
100
111
708 14 ▶ croak "Bad subject_id in ID '$sid'"
Branch: 1 branch
BranchTrueFalseTotal
unless $sid =~ /^\d{$pad}$/ 0 14 14
709 unless $sid =~ /^\d{$pad}$/;
710 14 my $subject_id = int($sid);
711  
712 # Extract batch and replicate (just the integer)
713 14 my ( $batch, $replicate );
714 14 ▶ if (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) 11 3 14
715  
716 # last element R## -> replicate
717 11 ▶ if ( $rest[-1] =~ /^R(\d{2})$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest[-1] =~ /^R(\d{2})$/) 11 0 11
718 11 $replicate = int $1;
719 11 pop @rest;
720 }
721  
722 # now maybe B## -> batch
723 11 ▶ if ( @rest && $rest[-1] =~ /^B(\d{2})$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest and $rest[-1] =~ /^B(\d{2})$/) 11 0 11
Condition33%1/3
@rest and $rest[-1] =~ /^B(\d{2})$/
!ll&&!rl&&r
0011
MC/DC0%0/2
@rest and $rest[-1] =~ /^B(\d{2})$/
@rest $rest[-1] =~ /^B(\d{2})$/
Truth table33%1/3
A and B
A= @restB= $rest[-1] =~ /^B(\d{2})$/
ABresult
0X0
100
111
724 11 $batch = int $1;
725 11 pop @rest;
726 }
727 }
728  
729 # timepoint decode (strict codebook reverse lookup)
730 my ($timepoint) =
731 14 grep { $cb->{timepoint}{$_}{code} eq $ptc } keys %{ $cb->{timepoint} };
732 14 ▶ croak "Unknown timepoint code '$ptc'" unless defined $timepoint;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timepoint 0 14 14
733  
734 14 $du = _parse_field( $du, undef, $cb->{duration_pattern}, 'human',
735 'duration' );
736  
737 # condition decode (allow multiple codes separated by '+')
738 13 my @parts = split /\+/, $cn;
739 13 my @norm;
740 13 for my $code (@parts) {
741  
742 # if it exists verbatim, use it
743 17 ▶ if ( exists $cb->{condition}{$code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cb->{'condition'}{$code}) { } 0 17 17
744 0 push @norm, $code;
745 }
746 else {
747 # insert dot after 3rd char if missing
748 17 ▶ if ( $code !~ /\./ && length($code) > 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $code =~ /\./ and length $code > 3) 0 17 17
Condition66%2/3
not $code =~ /\./ and length $code > 3
!ll&&!rl&&r
1340
MC/DC0%0/2
not $code =~ /\./ and length $code > 3
not $code =~ /\./ length $code > 3
Truth table66%2/3
not(A) and B
A= not $code =~ /\./B= length $code > 3
ABresult
0X0
100
111
749 0 $code = substr( $code, 0, 3 ) . '.' . substr( $code, 3 );
750 }
751 17 push @norm, $code;
752 }
753 }
754  
755 # join with semicolons for human readability
756 13 my $condition = join ';', @norm;
757  
758 # ensure we don't get warnings when printing
759 13 ▶ $batch = '' unless defined $batch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $batch 2 11 13
760 13 ▶ $replicate = '' unless defined $replicate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $replicate 2 11 13
761  
762 return {
763 13 project => $project,
764 species => $species,
765 subject_id => $subject_id,
766 tissue => $tissue,
767 sample_type => $stype,
768 assay => $assay,
769 condition => $condition,
770 timepoint => $timepoint,
771 duration => $du,
772 batch => $batch,
773 replicate => $replicate,
774 };
775 }
776  
777 #----------------------------------------------------------------
778 # Stub biosample encoder
779 #----------------------------------------------------------------
780 15 ▶sub _encode_stub_biosample {
Subroutine _encode_stub_biosample: called CC=13 SCAR=27.8
781 15 my ( $self, $cb, $pr, $sp, $sid, $ti, $st, $as, $co, $tp, $du, $ba, $re ) =
782 @_;
783  
784 # 1) static validations
785 15 _validate_field( $cb->{project}, 'project', $pr );
786 15 _validate_field( $cb->{species}, 'species', $sp );
787 15 _validate_field( $cb->{tissue}, 'tissue', $ti );
788 15 _validate_field( $cb->{sample_type}, 'sample_type', $st );
789 15 _validate_field( $cb->{assay}, 'assay', $as );
790  
791 # 2) subject_id -> stub
792 15 my $w = $self->subject_id_base62_width;
793 15 ▶ croak "Invalid stub width '$w'" unless $w =~ /^\d+$/ && $w > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $w =~ /^\d+$/ and $w > 0 0 15 15
Condition33%1/3
$w =~ /^\d+$/ and $w > 0
!ll&&!rl&&r
0015
MC/DC0%0/2
$w =~ /^\d+$/ and $w > 0
$w =~ /^\d+$/ $w > 0
Truth table33%1/3
A and B
A= $w =~ /^\d+$/B= $w > 0
ABresult
0X0
100
111
794 15 ▶ croak "Bad subject_id '$sid' (0–" . ( 62**$w - 1 ) . ")"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sid and $sid =~ /^\d+$/ and $sid <= 62 ** $w - 1 0 15 15
Condition33%2/6
defined $sid and $sid =~ /^\d+$/ and $sid <= 62 ** $w - 1
!ll&&!rl&&r
0015
defined $sid and $sid =~ /^\d+$/
!ll&&!rl&&r
0015
MC/DC0%0/3
defined $sid and $sid =~ /^\d+$/ and $sid <= 62 ** $w - 1
defined $sid $sid =~ /^\d+$/ $sid <= 62 ** $w - 1
Truth table25%1/4
A and B and C
A= defined $sidB= $sid =~ /^\d+$/C= $sid <= 62 ** $w - 1
ABCresult
0XX0
10X0
1100
1111
795 unless defined $sid && $sid =~ /^\d+$/ && $sid <= 62**$w - 1;
796 15 my $sid_stub = $self->_subject_id_to_stub( $sid, $w );
797  
798 # 3) pull in previously-parsed conditions (bulk) or split the raw string
799 15 my $conds_aref = delete $self->{_conds};
800 15 ▶ my @conds =
Branch: 2 branches
BranchTrueFalseTotal
$conds_aref ? : 15 0 15
defined $co ? : 0 0 0
801 $conds_aref
802 ? @$conds_aref
803 : split /\s*[+,;]\s*/, ( defined $co ? $co : '' );
804 15 ▶ croak "No conditions provided" unless @conds;
Branch: 1 branch
BranchTrueFalseTotal
unless @conds 0 15 15
805 15 ▶ croak sprintf "You passed %d conditions but max is %d",
Branch: 1 branch
BranchTrueFalseTotal
if @conds > $self->max_conditions 0 15 15
806 scalar(@conds), $self->max_conditions
807 if @conds > $self->max_conditions;
808  
809 # 4) map each ICD-10 -> 3-char stub
810 my @stubs = map {
811 15 ( my $c = $_ ) =~ s/\.//g; # strip dots
812 croak "Unknown ICD-10 '$_'"
813 15 ▶ unless exists $self->icd10_order->{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->icd10_order->{$c} 0 15 15
814 15 $self->_subject_id_to_stub( $self->icd10_order->{$c}, 3 )
815 } @conds;
816 15 my $cond_stub = join '', @stubs;
817 15 my $count_prefix = sprintf( "%02d", scalar @stubs ); # << count added
818  
819 # 5) timepoint stub (strict codebook lookup)
820 15 _validate_field( $cb->{timepoint}, 'timepoint', $tp );
821 15 my $tp_stub = $cb->{timepoint}{$tp}{stub_code};
822  
823 # 6) duration stub (pattern-driven only)
824 my $du_stub =
825 15 _parse_field( $du, undef, $cb->{duration_pattern}, 'stub', 'duration' );
826  
827 # 7) batch & replicate (optional)
828 my $ba_stub =
829 defined $ba
830 14 ▶ ? _parse_field( $ba, undef, $cb->{batch_pattern}, 'stub', 'batch' )
Branch: 1 branch
BranchTrueFalseTotal
defined $ba ? : 11 3 14
831 : '';
832 my $re_stub =
833 defined $re
834 14 ▶ ? _parse_field( $re, undef, $cb->{replicate_pattern}, 'stub',
Branch: 1 branch
BranchTrueFalseTotal
defined $re ? : 11 3 14
835 'replicate' )
836 : '';
837  
838 # 8) assemble and return
839 return join '', (
840 $cb->{project}{$pr}{stub_code}, $cb->{species}{$sp}{stub_code},
841 $sid_stub, $cb->{tissue}{$ti}{stub_code},
842 $cb->{sample_type}{$st}{stub_code}, $cb->{assay}{$as}{stub_code},
843 14 $cond_stub, $count_prefix, # << here
844 $tp_stub, $du_stub,
845 $ba_stub, $re_stub,
846 );
847 }
848  
849 # Stub biosample decoder
850 7 ▶sub _decode_stub_biosample {
Subroutine _decode_stub_biosample: called CC=41 SCAR=39.3
851 7 my ( $self, $cb, $id ) = @_;
852 7 ▶ croak "Bad stub ID" unless defined $id && length $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and length $id 0 7 7
Condition33%1/3
defined $id and length $id
!ll&&!rl&&r
007
MC/DC0%0/2
defined $id and length $id
defined $id length $id
Truth table33%1/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
853  
854 # Use codebook-driven tail peel for replicate & batch
855 7 ▶ my $repl_fmt = $cb->{replicate_pattern}{stub_format} // '%02d';
Condition50%1/2
$cb->{"replicate_pattern"}{"stub_format"} // "%02d"
l!l
70
MC/DC0%0/1
$cb->{"replicate_pattern"}{"stub_format"} // "%02d"
$cb->{"replicate_pattern"}{"stub_format"}
Truth table50%1/2
A
A= $cb->{"replicate_pattern"}{"stub_format"}
Aresult
11
00
856 7 ▶ my $batch_fmt = $cb->{batch_pattern}{stub_format} // '%02d';
Condition50%1/2
$cb->{"batch_pattern"}{"stub_format"} // "%02d"
l!l
70
MC/DC0%0/1
$cb->{"batch_pattern"}{"stub_format"} // "%02d"
$cb->{"batch_pattern"}{"stub_format"}
Truth table50%1/2
A
A= $cb->{"batch_pattern"}{"stub_format"}
Aresult
11
00
857  
858 # 1) strip off replicate using stub_format (e.g. 'R%02d' or '%02d')
859 7 my $replicate = _strip_tail_using_fmt( \$id, $repl_fmt );
860  
861 # 2) strip off batch using stub_format
862 7 my $batch = _strip_tail_using_fmt( \$id, $batch_fmt );
863  
864 # Legacy fallback for old (unprefixed) stubs: peel bare 2 digits if still present
865 0 ▶ if ( !defined $replicate && $id =~ s/(\d{2})$// ) { $replicate = 0 + $1 }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $replicate and $id =~ s/(\d{2})$//) 0 7 7
Condition66%2/3
not defined $replicate and $id =~ s/(\d{2})$//
!ll&&!rl&&r
610
MC/DC0%0/2
not defined $replicate and $id =~ s/(\d{2})$//
not defined $replicate $id =~ s/(\d{2})$//
Truth table66%2/3
not(A) and B
A= not defined $replicateB= $id =~ s/(\d{2})$//
ABresult
0X0
100
111
866 0 ▶ if ( !defined $batch && $id =~ s/(\d{2})$// ) { $batch = 0 + $1 }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $batch and $id =~ s/(\d{2})$//) 0 7 7
Condition66%2/3
not defined $batch and $id =~ s/(\d{2})$//
!ll&&!rl&&r
610
MC/DC0%0/2
not defined $batch and $id =~ s/(\d{2})$//
not defined $batch $id =~ s/(\d{2})$//
Truth table66%2/3
not(A) and B
A= not defined $batchB= $id =~ s/(\d{2})$//
ABresult
0X0
100
111
867  
868 # 3) strip and validate duration using the selected codebook pattern
869 7 ▶ croak "Bad stub ID (duration)" unless $id =~ s/(\d+)([DWMYN])$//;
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ s/(\d+)([DWMYN])$// 0 7 7
870 7 my ( $d_num, $d_unit ) = ( $1, $2 );
871 7 my $duration = 'P' . $d_num . $d_unit;
872 7 _parse_field( $duration, undef, $cb->{duration_pattern}, 'stub',
873 'duration' );
874 $duration = _parse_field( $duration, undef, $cb->{duration_pattern},
875 5 'human', 'duration' );
876  
877 # 4) strip off timepoint (variable-length stub_code from codebook)
878 my %tp_by =
879 5 map { $cb->{timepoint}{$_}{stub_code} => $_ } keys %{ $cb->{timepoint} };
880 5 my $timepoint;
881 5 for my $stub ( sort { length($b) <=> length($a) } keys %tp_by ) {
882 21 ▶ if ( $id =~ s/\Q$stub\E$// ) { $timepoint = $tp_by{$stub}; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ s/\Q$stub\E$//) 5 16 21
883 }
884 5 ▶ croak "Unknown timepoint stub at end of ID" unless defined $timepoint;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timepoint 0 5 5
885  
886 # 4b) peel 2-digit condition COUNT (immediately before timepoint)
887 5 ▶ croak "Missing condition count" unless $id =~ s/(\d{2})$//;
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ s/(\d{2})$// 0 5 5
888 5 my $cond_count = int $1;
889 5 ▶ croak "Invalid condition count '$cond_count'" unless $cond_count > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $cond_count > 0 0 5 5
890  
891 # now $id is the head: project + species + sid + tissue + sample_type + assay + COND_SEGMENT
892 5 my $head = $id;
893  
894 # 5) project (match longest stub_code at start)
895 25 my %proj_by = map { $cb->{project}{$_}{stub_code} => $_ }
896 5 keys %{ $cb->{project} };
897 5 my ($project);
898 5 for my $stub ( sort { length($b) <=> length($a) } keys %proj_by ) {
899 11 ▶ if ( $head =~ s/^\Q$stub\E// ) { $project = $proj_by{$stub}; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($head =~ s/^\Q$stub\E//) 5 6 11
900 }
901 5 ▶ croak "Unknown project stub" unless defined $project;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $project 0 5 5
902  
903 # 6) species (fixed width inferred from codebook)
904 5 my $spec_w = _species_stub_width( $cb->{species} );
905 4 ▶ croak "Stub too short to contain species" unless length($head) >= $spec_w;
Branch: 1 branch
BranchTrueFalseTotal
unless length $head >= $spec_w 0 4 4
906 4 my $spec_stub = substr( $head, 0, $spec_w );
907 96 my %sp_by = map { $cb->{species}{$_}{stub_code} => $_ }
908 4 keys %{ $cb->{species} };
909 # The global Not Available token is exempt from the species-width rule.
910 4 my $nav = $cb->{species}{'Not Available'}{stub_code};
911 4 ▶ if ( !exists $sp_by{$spec_stub} && defined $nav && length $nav
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $sp_by{$spec_stub} and defined $nav and length $nav and substr($head, 0, length $nav) eq $nav) 1 3 4
Condition66%6/9
not exists $sp_by{$spec_stub} and defined $nav and length $nav and substr($head, 0, length $nav) eq $nav
!ll&&!rl&&r
301
not exists $sp_by{$spec_stub} and defined $nav and length $nav
!ll&&!rl&&r
301
not exists $sp_by{$spec_stub} and defined $nav
!ll&&!rl&&r
301
MC/DC25%1/4
not exists $sp_by{$spec_stub} and defined $nav and length $nav and substr($head, 0, length $nav) eq $nav
not exists $sp_by{$spec_stub} defined $nav length $nav substr($head, 0, length $nav) eq $nav
Truth table40%2/5
not(A) and B and C and D
A= not exists $sp_by{$spec_stub}B= defined $navC= length $navD= substr($head, 0, length $nav) eq $nav
ABCDresult
0XXX0
10XX0
110X0
11100
11111
912 && substr( $head, 0, length $nav ) eq $nav ) {
913 1 $spec_stub = $nav;
914 1 $spec_w = length $nav;
915 }
916 4 ▶ croak "Unknown species stub '$spec_stub'" unless exists $sp_by{$spec_stub};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $sp_by{$spec_stub} 0 4 4
917 4 substr( $head, 0, $spec_w ) = '';
918 4 my $species = $sp_by{$spec_stub};
919  
920 # 7) subject_id (base-62 width)
921 4 my $w = $self->subject_id_base62_width;
922 4 ▶ croak "Bad stub width" unless $w =~ /^\d+$/ && $w > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $w =~ /^\d+$/ and $w > 0 0 4 4
Condition33%1/3
$w =~ /^\d+$/ and $w > 0
!ll&&!rl&&r
004
MC/DC0%0/2
$w =~ /^\d+$/ and $w > 0
$w =~ /^\d+$/ $w > 0
Truth table33%1/3
A and B
A= $w =~ /^\d+$/B= $w > 0
ABresult
0X0
100
111
923 4 my $sid_stub = substr( $head, 0, $w );
924 4 substr( $head, 0, $w ) = '';
925 4 my $subject_id = $self->_stub_to_subject_id($sid_stub);
926  
927 # 8) tissue (match longest)
928 84 my %ti_by = map { $cb->{tissue}{$_}{stub_code} => $_ }
929 4 keys %{ $cb->{tissue} };
930 4 my $tissue;
931 4 for my $stub ( sort { length($b) <=> length($a) } keys %ti_by ) {
932 63 ▶ if ( $head =~ s/^\Q$stub\E// ) { $tissue = $ti_by{$stub}; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($head =~ s/^\Q$stub\E//) 4 59 63
933 }
934 4 ▶ croak "Unknown tissue stub '$head'" unless defined $tissue;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tissue 0 4 4
935  
936 # 9) sample_type (match longest)
937 24 my %st_by = map { $cb->{sample_type}{$_}{stub_code} => $_ }
938 4 keys %{ $cb->{sample_type} };
939 4 my $stype;
940 4 for my $stub ( sort { length($b) <=> length($a) } keys %st_by ) {
941 21 ▶ if ( $head =~ s/^\Q$stub\E// ) { $stype = $st_by{$stub}; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($head =~ s/^\Q$stub\E//) 4 17 21
942 }
943 4 ▶ croak "Unknown sample_type stub" unless defined $stype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stype 0 4 4
944  
945 # 10) assay (match longest)
946 84 my %as_by = map { $cb->{assay}{$_}{stub_code} => $_ }
947 4 keys %{ $cb->{assay} };
948 4 my $assay;
949 4 for my $stub ( sort { length($b) <=> length($a) } keys %as_by ) {
950 43 ▶ if ( $head =~ s/^\Q$stub\E// ) { $assay = $as_by{$stub}; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($head =~ s/^\Q$stub\E//) 4 39 43
951 }
952 4 ▶ croak "Unknown assay stub" unless defined $assay;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $assay 0 4 4
953  
954 # 11) remaining $head must be exactly cond_count * 3 chars (ICD ordinals stubs)
955 4 ▶ croak "Bad condition stub length" unless length($head) % 3 == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless length($head) % 3 == 0 0 4 4
956 4 my @c_stubs = $head =~ /(.{3})/g;
957 4 ▶ croak "Condition count mismatch (have "
Branch: 1 branch
BranchTrueFalseTotal
unless @c_stubs == $cond_count 0 4 4
958 . scalar(@c_stubs)
959 . ", expected $cond_count)"
960 unless @c_stubs == $cond_count;
961  
962 my @conds = map {
963 4 my $ord = $self->_stub_to_subject_id($_);
964 croak "Invalid condition ordinal '$ord'"
965 4 ▶ unless $ord >= 1 && $ord < @{ $self->icd10_by_order };
Branch: 1 branch
BranchTrueFalseTotal
unless $ord >= 1 and $ord < @{$self->icd10_by_order;} 0 4 4
Condition33%1/3
$ord >= 1 and $ord < @{$self->icd10_by_order;}
!ll&&!rl&&r
004
MC/DC0%0/2
$ord >= 1 and $ord < @{$self->icd10_by_order;}
$ord >= 1 $ord < @{$self->icd10_by_order;}
Truth table33%1/3
A and B
A= $ord >= 1B= $ord < @{$self->icd10_by_order;}
ABresult
0X0
100
111
966 4 _format_icd10( $self->icd10_by_order->[$ord] );
967 } @c_stubs;
968  
969 4 my $condition = join ';', @conds;
970  
971 4 if (DEVEL_MODE) {
972 say "DEBUG biosample cond_count = $cond_count";
973 say "DEBUG biosample cond_stubs = [" . join( ',', @c_stubs ) . "]";
974 }
975  
976 return {
977 4 ▶ project => $project,
Condition100%4/4
$batch // ""
l!l
31
$replicate // ""
l!l
31
MC/DC100%1/1
$batch // ""
$batch
MC/DC100%1/1
$replicate // ""
$replicate
Truth table100%2/2
A
A= $batch
Aresult
11
00
Truth table100%2/2
A
A= $replicate
Aresult
11
00
978 species => $species,
979 subject_id => $subject_id,
980 tissue => $tissue,
981 sample_type => $stype,
982 assay => $assay,
983 condition => $condition, # ';' joined
984 timepoint => $timepoint,
985 duration => $duration,
986 batch => $batch // '',
987 replicate => $replicate // '',
988 };
989 }
990  
991 # Human-mode subject encoder
992 6 ▶sub _encode_human_subject {
Subroutine _encode_human_subject: called CC=11 SCAR=27.2
993 my (
994 6 $self, $cb,
995 $study, # study
996 $sid, # subject_id
997 $ty, # type
998 $co, # raw condition string
999 $sx, # sex
1000 $ag # age_group
1001 ) = @_;
1002  
1003 # 1) Validate type, sex & age_group
1004 6 _validate_field( $cb->{type}, 'type', $ty );
1005 6 _validate_field( $cb->{sex}, 'sex', $sx );
1006 6 _validate_field( $cb->{age_group}, 'age_group', $ag );
1007  
1008 # 2) Pad subject_id
1009 6 my $pad = $self->subject_id_pad_length;
1010 6 ▶ croak "Invalid pad length '$pad'"
Branch: 1 branch
BranchTrueFalseTotal
unless $pad =~ /^\d+$/ and $pad > 0 0 6 6
Condition33%1/3
$pad =~ /^\d+$/ and $pad > 0
!ll&&!rl&&r
006
MC/DC0%0/2
$pad =~ /^\d+$/ and $pad > 0
$pad =~ /^\d+$/ $pad > 0
Truth table33%1/3
A and B
A= $pad =~ /^\d+$/B= $pad > 0
ABresult
0X0
100
111
1011 unless $pad =~ /^\d+$/ && $pad > 0;
1012 6 my $max = 10**$pad - 1;
1013 6 ▶ croak "Bad subject_id '$sid' (must be 0-$max)"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sid and $sid =~ /^\d+$/ and $sid <= $max 0 6 6
Condition33%2/6
defined $sid and $sid =~ /^\d+$/ and $sid <= $max
!ll&&!rl&&r
006
defined $sid and $sid =~ /^\d+$/
!ll&&!rl&&r
006
MC/DC0%0/3
defined $sid and $sid =~ /^\d+$/ and $sid <= $max
defined $sid $sid =~ /^\d+$/ $sid <= $max
Truth table25%1/4
A and B and C
A= defined $sidB= $sid =~ /^\d+$/C= $sid <= $max
ABCresult
0XX0
10X0
1100
1111
1014 unless defined $sid && $sid =~ /^\d+$/ && $sid <= $max;
1015 6 $study =~ tr/-/_/; # normalize study key
1016  
1017 # 3) Split & validate multiple ICD-10 codes
1018 6 ▶ my @conds = split /\s*[+,;]\s*/, ( defined $co ? $co : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $co ? : 6 0 6
1019 6 ▶ croak "No conditions provided" unless @conds;
Branch: 1 branch
BranchTrueFalseTotal
unless @conds 0 6 6
1020 6 for my $c (@conds) {
1021 my $pat_cfg = $cb->{condition_pattern}
1022 7 ▶ or croak "No condition_pattern in codebook";
Branch: 1 branch
BranchTrueFalseTotal
my $pat_cfg = $cb->{"condition_pattern"} or croak("No condition_pattern in codebook") 7 0 7
1023 7 ▶ croak "Invalid condition '$c'"
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /^$pat_cfg->{"regex"}$/ 0 7 7
1024 unless $c =~ /^$pat_cfg->{regex}$/;
1025 }
1026  
1027 # 4) Re-join with '+' for the final code
1028 6 my $cond_code = join '+', @conds;
1029  
1030 # 5) Assemble the ID
1031 return join( '-',
1032 $study,
1033 sprintf( "%0${pad}d", $sid ),
1034 $cb->{type}{$ty}{code},
1035 $cond_code,
1036 $cb->{sex}{$sx}{code},
1037 $cb->{age_group}{$ag}{code},
1038 6 );
1039 }
1040  
1041 # Human-mode subject decoder
1042 6 ▶sub _decode_human_subject {
Subroutine _decode_human_subject: called CC=3 SCAR=11.1
1043 6 my ( $self, $cb, $id ) = @_;
1044  
1045 6 my @p = split /-/, $id;
1046 6 ▶ croak "Bad subject ID" unless @p == 6;
Branch: 1 branch
BranchTrueFalseTotal
unless @p == 6 0 6 6
1047 6 my ( $study, $sid, $type_c, $co, $sex_c, $ag_code ) = @p;
1048  
1049 72 my ($ag_key) = grep { $cb->{age_group}{$_}{code} eq $ag_code }
1050 6 keys %{ $cb->{age_group} };
1051 6 ▶ croak "Unknown age_group code '$ag_code'" unless defined $ag_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ag_key 0 6 6
1052  
1053 return {
1054 6 study => $study,
1055 type => $type_c,
1056 sex => $sex_c,
1057 age_group => $ag_key,
1058 condition => $co,
1059 subject_id => int($sid),
1060 };
1061 }
1062  
1063 # Stub-mode subject encoder (with 2-digit condition count prefix)
1064 # Stub-mode subject **encoder**, with COUNT just before sex
1065 6 ▶sub _encode_stub_subject {
Subroutine _encode_stub_subject: called CC=10 SCAR=24.8
1066 my (
1067 6 $self, $cb,
1068 $study, # study key
1069 $subject_id, # integer
1070 $type, # type key
1071 $condition, # comma-separated ICD list
1072 $sex, # sex key
1073 $age_group # age_group key
1074 ) = @_;
1075  
1076 # study stub
1077 6 ▶ my $study_stub = $cb->{study}{$study}{stub_code} // $study;
Condition33%1/3
$cb->{"study"}{$study}{"stub_code"} // $study
l!l&&r!l&&!r
060
MC/DC0%0/2
$cb->{"study"}{$study}{"stub_code"} // $study
$cb->{"study"}{$study}{"stub_code"} $study
Truth table33%1/3
A // B
A= $cb->{"study"}{$study}{"stub_code"}B= $study
ABresult
1X1
011
000
1078  
1079 # validate & get stubs for type/sex/age_group
1080 6 ▶ croak "Unknown type '$type'" unless exists $cb->{type}{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cb->{"type"}{$type} 0 6 6
1081 6 ▶ croak "Unknown sex '$sex'" unless exists $cb->{sex}{$sex};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cb->{"sex"}{$sex} 0 6 6
1082 croak "Unknown age_group '$age_group'"
1083 6 ▶ unless exists $cb->{age_group}{$age_group};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cb->{"age_group"}{$age_group} 0 6 6
1084 6 my $type_stub = $cb->{type}{$type}{stub_code};
1085 6 my $sex_stub = $cb->{sex}{$sex}{stub_code};
1086 6 my $age_group_stub = $cb->{age_group}{$age_group}{stub_code};
1087  
1088 # subject_id -> base-62
1089 6 my $w = $self->subject_id_base62_width;
1090 6 ▶ croak "Invalid stub width '$w'" unless $w =~ /^\d+$/ && $w > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $w =~ /^\d+$/ and $w > 0 0 6 6
Condition33%1/3
$w =~ /^\d+$/ and $w > 0
!ll&&!rl&&r
006
MC/DC0%0/2
$w =~ /^\d+$/ and $w > 0
$w =~ /^\d+$/ $w > 0
Truth table33%1/3
A and B
A= $w =~ /^\d+$/B= $w > 0
ABresult
0X0
100
111
1091 6 my $sid_stub = $self->_subject_id_to_stub( $subject_id, $w );
1092  
1093 # split and encode ICDs
1094 6 my @conds = split /\s*,\s*/, $condition;
1095 6 ▶ croak "No conditions provided" unless @conds;
Branch: 1 branch
BranchTrueFalseTotal
unless @conds 0 6 6
1096 6 ▶ croak "Too many conditions" if @conds > 99;
Branch: 1 branch
BranchTrueFalseTotal
if @conds > 99 0 6 6
1097 my @cond_stubs = map {
1098 6 ( my $c = $_ ) =~ s/\.//g;
1099 7 ▶ croak "Unknown ICD-10 '$_'" unless exists $self->icd10_order->{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->icd10_order->{$c} 0 7 7
1100 7 $self->_subject_id_to_stub( $self->icd10_order->{$c}, 3 )
1101 } @conds;
1102  
1103 # build count + conds string
1104 6 my $count_prefix = sprintf( "%02d", scalar @cond_stubs );
1105 6 my $conds_part = join "", @cond_stubs;
1106  
1107 # final: STUDY + SID + TYPE + CONDS + COUNT + SEX + AGE
1108 6 return join "",
1109 (
1110 $study_stub, $sid_stub, $type_stub, $conds_part,
1111 $count_prefix, $sex_stub, $age_group_stub,
1112 );
1113 }
1114  
1115 # Stub-mode subject **decoder**, reading backwards with COUNT before sex
1116 6 ▶sub _decode_stub_subject {
Subroutine _decode_stub_subject: called CC=15 SCAR=28.3
1117 6 my ( $self, $cb, $stub ) = @_;
1118  
1119 # 1) sanity on base-62 width
1120 6 my $w = $self->subject_id_base62_width;
1121 6 ▶ croak "Invalid stub length '$w'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w and $w =~ /^\d+$/ and $w > 0 0 6 6
Condition33%2/6
defined $w and $w =~ /^\d+$/ and $w > 0
!ll&&!rl&&r
006
defined $w and $w =~ /^\d+$/
!ll&&!rl&&r
006
MC/DC0%0/3
defined $w and $w =~ /^\d+$/ and $w > 0
defined $w $w =~ /^\d+$/ $w > 0
Truth table25%1/4
A and B and C
A= defined $wB= $w =~ /^\d+$/C= $w > 0
ABCresult
0XX0
10X0
1100
1111
1122 unless defined $w && $w =~ /^\d+$/ && $w > 0;
1123  
1124 # 2) reverse the entire stub
1125 6 my $rev = reverse $stub;
1126  
1127 # 3) pull off age_group (2 chars) + sex (1 char)
1128 6 my $rev_age = substr( $rev, 0, 2 );
1129 6 my $rev_sex = substr( $rev, 2, 1 );
1130 6 my $rest = substr( $rev, 3 );
1131  
1132 # 4) next two chars are the **reversed** condition count
1133 6 ▶ croak "Bad condition count in stub" unless length($rest) >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless length $rest >= 2 0 6 6
1134 6 my $count_rev = substr( $rest, 0, 2 );
1135  
1136 # restore correct order, parse as integer
1137 6 my $cond_count = int reverse $count_rev;
1138 6 ▶ croak "Invalid condition count '$cond_count'" unless $cond_count > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $cond_count > 0 0 6 6
1139 6 $rest = substr( $rest, 2 );
1140  
1141 # 5) peel off exactly cond_count * 3 chars for all condition stubs
1142 6 my $conds_len = $cond_count * 3;
1143 6 ▶ croak "Bad condition stub length"
Branch: 1 branch
BranchTrueFalseTotal
unless length $rest >= $conds_len 0 6 6
1144 unless length($rest) >= $conds_len;
1145 6 my $rev_conds_rev = substr( $rest, 0, $conds_len );
1146 6 $rest = substr( $rest, $conds_len );
1147  
1148 # 6) next is the type stub (1 char)
1149 6 ▶ croak "Missing type stub" unless length($rest) >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $rest >= 1 0 6 6
1150 6 my $rev_type = substr( $rest, 0, 1 );
1151 6 $rest = substr( $rest, 1 );
1152  
1153 # 7) then the subject_id stub (width $w)
1154 6 ▶ croak "Missing subject_id stub" unless length($rest) >= $w;
Branch: 1 branch
BranchTrueFalseTotal
unless length $rest >= $w 0 6 6
1155 6 my $rev_id_rev = substr( $rest, 0, $w );
1156 6 $rest = substr( $rest, $w );
1157  
1158 # 8) and whatever remains is the study stub
1159 6 my $rev_study_rev = $rest;
1160  
1161 # 9) DEBUG dumps
1162 6 if (DEVEL_MODE) {
1163 say "DEBUG rev = '$rev'";
1164 say "DEBUG rev_age = '$rev_age'";
1165 say "DEBUG rev_sex = '$rev_sex'";
1166 say "DEBUG count_rev = '"
1167 . ( reverse $count_rev )
1168 . "' -> cond_count=$cond_count";
1169 say "DEBUG rev_conds_rev = '$rev_conds_rev'";
1170 say "DEBUG rev_type = '$rev_type'";
1171 say "DEBUG rev_id_rev = '$rev_id_rev'";
1172 say "DEBUG rev_study_rev = '$rev_study_rev'";
1173 }
1174  
1175 # 10) reverse back simple fields
1176 6 my $age_s = reverse $rev_age; # stub for age_group
1177 6 my $sex_s = reverse $rev_sex; # stub for sex
1178 6 my $type_s = reverse $rev_type; # stub for type
1179 6 my $sid_stub = reverse $rev_id_rev; # base62 subject_id
1180 6 my $study = reverse $rev_study_rev;
1181  
1182 # 11) decode subject_id
1183 6 my $subject_id = $self->_stub_to_subject_id($sid_stub);
1184  
1185 # 12) map back type, sex, and age_group via codebook
1186 my ($type) =
1187 6 grep { $cb->{type}{$_}{stub_code} eq $type_s } keys %{ $cb->{type} };
1188 6 ▶ croak "Unknown type stub '$type_s'" unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 6 6
1189  
1190 my ($sex_key) =
1191 6 grep { $cb->{sex}{$_}{stub_code} eq $sex_s } keys %{ $cb->{sex} };
1192 6 ▶ croak "Unknown sex stub '$sex_s'" unless defined $sex_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sex_key 0 6 6
1193  
1194 72 my ($age_group_key) = grep { $cb->{age_group}{$_}{stub_code} eq $age_s }
1195 6 keys %{ $cb->{age_group} };
1196 6 ▶ croak "Unknown age_group stub '$age_s'" unless defined $age_group_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $age_group_key 0 6 6
1197  
1198 # 13) split condition segment into reversed 3-char chunks
1199 6 my @rev_chunks = $rev_conds_rev =~ /(.{3})/g;
1200 6 ▶ croak "Condition stub parsing mismatch"
Branch: 1 branch
BranchTrueFalseTotal
unless @rev_chunks == $cond_count 0 6 6
1201 unless @rev_chunks == $cond_count;
1202  
1203 # *** restore original left-to-right order ***
1204 6 @rev_chunks = reverse @rev_chunks;
1205  
1206 # 14) reverse each back, then decode ordinal -> ICD-10
1207 my @conds = map {
1208 6 my $stub3 = reverse $_; # un-reverse it
1209 7 my $ord = $self->_stub_to_subject_id($stub3); # base62 -> ordinal
1210 croak "Invalid condition ordinal '$ord'"
1211 7 ▶ unless $ord >= 1 && $ord < @{ $self->icd10_by_order };
Branch: 1 branch
BranchTrueFalseTotal
unless $ord >= 1 and $ord < @{$self->icd10_by_order;} 0 7 7
Condition33%1/3
$ord >= 1 and $ord < @{$self->icd10_by_order;}
!ll&&!rl&&r
007
MC/DC0%0/2
$ord >= 1 and $ord < @{$self->icd10_by_order;}
$ord >= 1 $ord < @{$self->icd10_by_order;}
Truth table33%1/3
A and B
A= $ord >= 1B= $ord < @{$self->icd10_by_order;}
ABresult
0X0
100
111
1212 7 _format_icd10( $self->icd10_by_order->[$ord] );
1213 } @rev_chunks;
1214  
1215 # 15) assemble final hash
1216 return {
1217 6 study => $study,
1218 subject_id => $subject_id,
1219 type => $type,
1220 condition => join( ';', @conds ),
1221 sex => $sex_key,
1222 age_group => $age_group_key, # <-- now mapped via codebook
1223 };
1224 }
1225  
1226 # ------------------------------------------------------------------------
1227 # Helper functions for subject_id ↔ stub conversion
1228 #
1229 # _subject_id_to_stub($id, $width):
1230 # - Converts a non-negative integer $id into a fixed-width base‑62 string.
1231 # - Pads with '0' on the left to exactly $width characters.
1232 #
1233 # _stub_to_subject_id($stub):
1234 # - Parses a base‑62 string $stub back into the original integer.
1235 # - Validates characters against the 0-9, A-Z, a-z alphabet.
1236 #
1237 # These allow you to shrink a 5‑digit decimal ID into 3 base‑62 chars in stub mode,
1238 # and recover the original integer when decoding.
1239 # ------------------------------------------------------------------------
1240  
1241 # Base‑62 alphabet
1242 49 my @BASE62 = ( '0' .. '9', 'A' .. 'Z', 'a' .. 'z' );
1243 49 my %BASE62_REV = map { $BASE62[$_] => $_ } 0 .. $#BASE62;
1244  
1245 # ------------------------------------------------------------------------
1246 # Convert a non‑negative integer into a fixed-width base‑62 stub
1247 # $id : integer subject ID (>=0)
1248 # $width : desired stub length (default 3)
1249 # Returns a $width-char string in [0-9A-Za-z], zero-padded on the left.
1250 # ------------------------------------------------------------------------
1251 43 ▶sub _subject_id_to_stub {
Subroutine _subject_id_to_stub: called CC=7 SCAR=21.1
1252 43 my ( $self, $id, $width ) = @_;
1253 43 ▶ $width ||= 3; # default stub length
Condition50%1/2
$width ||= 3
l!l
430
MC/DC0%0/1
$width ||= 3
$width
Truth table50%1/2
A
A= $width
Aresult
11
00
1254  
1255 43 ▶ croak "Bad subject_id '$id'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and $id =~ /^\d+$/ and $id >= 0 0 43 43
Condition33%2/6
defined $id and $id =~ /^\d+$/ and $id >= 0
!ll&&!rl&&r
0043
defined $id and $id =~ /^\d+$/
!ll&&!rl&&r
0043
MC/DC0%0/3
defined $id and $id =~ /^\d+$/ and $id >= 0
defined $id $id =~ /^\d+$/ $id >= 0
Truth table25%1/4
A and B and C
A= defined $idB= $id =~ /^\d+$/C= $id >= 0
ABCresult
0XX0
10X0
1100
1111
1256 unless defined $id && $id =~ /^\d+$/ && $id >= 0;
1257  
1258 43 ▶ return '0' x $width if $id == 0; # special‑case zero
Branch: 1 branch
BranchTrueFalseTotal
if $id == 0 0 43 43
1259  
1260 43 my $s = '';
1261 43 my $num = $id;
1262 43 ▶ while ( $num > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 0) 86 43 129
1263  
1264 # prepend the next base‑62 digit
1265 86 $s = $BASE62[ $num % 62 ] . $s;
1266 86 $num = int( $num / 62 );
1267 }
1268  
1269 # left‑pad with '0' to exactly $width chars
1270 43 return substr( ( '0' x $width ) . $s, -$width );
1271 }
1272  
1273 # Convert a base‑62 string back to an integer
1274 21 ▶sub _stub_to_subject_id {
Subroutine _stub_to_subject_id: called CC=5 SCAR=17.2
1275 21 my ( $self, $stub ) = @_;
1276 21 ▶ croak "Bad stub '$stub'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stub and $stub =~ /^[0-9A-Za-z]+$/ 0 21 21
Condition33%1/3
defined $stub and $stub =~ /^[0-9A-Za-z]+$/
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $stub and $stub =~ /^[0-9A-Za-z]+$/
defined $stub $stub =~ /^[0-9A-Za-z]+$/
Truth table33%1/3
A and B
A= defined $stubB= $stub =~ /^[0-9A-Za-z]+$/
ABresult
0X0
100
111
1277 unless defined $stub && $stub =~ /^[0-9A-Za-z]+$/;
1278 21 my $id = 0;
1279 21 for my $char ( split //, $stub ) {
1280 64 ▶ croak "Invalid base62 char '$char'" unless exists $BASE62_REV{$char};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $BASE62_REV{$char} 0 64 64
1281 64 $id = $id * 62 + $BASE62_REV{$char};
1282 }
1283 21 return $id;
1284 }
1285  
1286 # Private helper: if an ICD‑10 code has no dot but is >3 chars,
1287 # stick a dot after the third character.
1288 11 ▶sub _format_icd10 {
Subroutine _format_icd10: called CC=3 SCAR=12.5
1289 11 my ($code) = @_;
1290 11 ▶ return $code if $code =~ /\./ || length($code) <= 3;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /\./ or length $code <= 3 0 11 11
Condition33%1/3
$code =~ /\./ or length $code <= 3
l!l&&r!l&&!r
0011
MC/DC0%0/2
$code =~ /\./ or length $code <= 3
$code =~ /\./ length $code <= 3
Truth table33%1/3
A or B
A= $code =~ /\./B= length $code <= 3
ABresult
1X1
011
000
1291 11 return substr( $code, 0, 3 ) . '.' . substr( $code, 3 );
1292 }
1293  
1294 5 ▶sub _species_stub_width {
Subroutine _species_stub_width: called CC=9 SCAR=23.7
1295 5 my ($species_map) = @_;
1296 croak "Missing species codebook section"
1297 5 ▶ unless $species_map && ref $species_map eq 'HASH' && %{$species_map};
Branch: 1 branch
BranchTrueFalseTotal
unless $species_map and ref $species_map eq "HASH" and %{$species_map;} 0 5 5
Condition33%2/6
$species_map and ref $species_map eq "HASH" and %{$species_map;}
!ll&&!rl&&r
005
$species_map and ref $species_map eq "HASH"
!ll&&!rl&&r
005
MC/DC0%0/3
$species_map and ref $species_map eq "HASH" and %{$species_map;}
$species_map ref $species_map eq "HASH" %{$species_map;}
Truth table25%1/4
A and B and C
A= $species_mapB= ref $species_map eq "HASH"C= %{$species_map;}
ABCresult
0XX0
10X0
1100
1111
1298  
1299 5 my %widths;
1300 5 for my $key ( keys %{$species_map} ) {
1301 120 ▶ next if $key eq 'Not Available';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "Not Available" 5 115 120
1302 115 my $stub = $species_map->{$key}{stub_code};
1303 115 ▶ croak "Missing species stub_code for '$key'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stub and length $stub 0 115 115
Condition33%1/3
defined $stub and length $stub
!ll&&!rl&&r
00115
MC/DC0%0/2
defined $stub and length $stub
defined $stub length $stub
Truth table33%1/3
A and B
A= defined $stubB= length $stub
ABresult
0X0
100
111
1304 unless defined $stub && length $stub;
1305 115 $widths{ length $stub } = 1;
1306 }
1307  
1308 5 ▶ croak "Species stub_code values must all have the same length"
Branch: 1 branch
BranchTrueFalseTotal
unless keys %widths == 1 1 4 5
1309 unless keys(%widths) == 1;
1310  
1311 4 return ( keys %widths )[0];
1312 }
1313  
1314 48 ▶sub _apply_defaults {
Subroutine _apply_defaults: called CC=9 SCAR=22.4
1315 48 my ($doc) = @_;
1316 48 ▶ my $ents = $doc->{entities} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ents = $doc->{"entities"} or return 48 0 48
1317 48 ▶ my $defaults = delete $ents->{_defaults} || {};
Condition50%1/2
delete $ents->{'_defaults'} || {}
l!l
480
MC/DC0%0/1
delete $ents->{'_defaults'} || {}
delete $ents->{'_defaults'}
Truth table50%1/2
A
A= delete $ents->{'_defaults'}
Aresult
11
00
1318  
1319 48 for my $entity (qw/biosample subject/) {
1320 96 ▶ next unless my $cat = $ents->{$entity};
Branch: 1 branch
BranchTrueFalseTotal
unless my $cat = $ents->{$entity} 0 96 96
1321 96 for my $slot ( grep { ref $cat->{$_} eq 'HASH' } keys %$cat ) {
1322 720 my $map = $cat->{$slot};
1323  
1324 # skip the default‐holder itself
1325 720 ▶ next if $slot eq '_defaults';
Branch: 1 branch
BranchTrueFalseTotal
if $slot eq "_defaults" 0 720 720
1326  
1327 # for each of your two default keys, only add if missing:
1328 # NB: 'age_group' already has its own so it is skipped here
1329 720 for my $k ( keys %$defaults ) {
1330 1440 ▶ $map->{$k} //= { %{ $defaults->{$k} } };
Condition100%2/2
$map->{$k} //= {%{$defaults->{$k};}}
l!l
3361104
MC/DC100%1/1
$map->{$k} //= {%{$defaults->{$k};}}
$map->{$k}
Truth table100%2/2
A
A= $map->{$k}
Aresult
11
00
1331 }
1332 }
1333 }
1334  
1335 warn
1336 48 "! Note: injected global 'Unknown' + 'Not Available' defaults into each category\n"
1337 if DEVEL_MODE;
1338 }
1339  
1340 # Turn a stub_format like 'R%02d' or '%02d' into a tail regex with one capture
1341 14 ▶sub _fmt_to_tail_regex {
Subroutine _fmt_to_tail_regex: called CC=1 SCAR=0.0
1342 14 my ($fmt) = @_;
1343 14 my $re = quotemeta($fmt); # escape literal chars
1344 14 $re =~ s/\\%0?(\d+)d/(\\d{$1})/g; # %02d -> (\d{2}), %3d -> (\d{3})
1345 14 return qr/$re$/; # anchor at end
1346 }
1347  
1348 # Try to strip a value from the *end* of a string using stub_format
1349 # Returns the captured integer or undef if no match; modifies $$sref
1350 14 ▶sub _strip_tail_using_fmt {
Subroutine _strip_tail_using_fmt: called CC=2 SCAR=6.9
1351 14 my ( $sref, $fmt ) = @_;
1352 14 my $re = _fmt_to_tail_regex($fmt);
1353 14 ▶ if ( $$sref =~ s/$re// ) { return 0 + $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($$sref =~ s/$re//) 12 2 14
1354 2 return undef;
1355 }
1356  
1357 49 1;
↑ Summary blib/lib/ClarID/Tools/Command/qrcode.pm »