lib/BioX/Workflow/Command/run/Utils/WriteMeta.pm

filter: Statementstmt 10.9% 21 / 192 Branchbran 0.0% 0 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 27.2% 6 / 22 Podpod 62.5% 10 / 16 totaltotal 13.1% 37 / 282 CC 30 SCAR 30.6
CC
30
Cov
9%
write_rule_meta
72.0
get_pretty_value
30.0
write_workflow_meta
20.0
CRAP 21.4
« lib/BioX/Workflow/Command/run/Utils/Samples.pm ↑ Summary lib/BioX/Workflow/Command/run/Rules/Directives/Inspect.pm »
1 package BioX::Workflow::Command::run::Utils::WriteMeta;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use MooseX::App::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 BioX::Workflow::Command::run::Utils::WriteMeta;
12  
13 Debug information containing metadata per rule.
14  
15 Useful for tracking the evolution of an analysis
16  
17 =head2 Variables
18  
19 =head3 comment_char
20  
21 Default comment char is '#'.
22  
23 =cut
24  
25 1 option 'comment_char' => (
26 is => 'rw',
27 isa => 'Str',
28 default => '#',
29 );
30  
31 =head3 verbose
32  
33 Output some more things
34  
35 =cut
36  
37 1 option 'verbose' => (
38 is => 'rw',
39 isa => 'Bool',
40 default => 1,
41 clearer => 'clear_verbose',
42 predicate => 'has_verbose',
43 );
44  
45 =head2 Subroutines
46  
47 =cut
48  
49 =head3 print_opts
50  
51 Get the command line opts and config data - print those to cached workflow and
52 our workflow file.
53  
54 =cut
55  
56 0 ▶sub print_opts {
Subroutine print_opts: not called CC=1 SCAR=6.9
Pod documented
57 0 my $self = shift;
58  
59 0 my $cmd_opts = $self->print_cmd_line_opts;
60 0 my $config_data = $self->print_config_data;
61  
62 0 write_file( $self->cached_workflow, $cmd_opts );
63 0 write_file( $self->cached_workflow, { append => 1 }, $config_data );
64 0 write_file(
65 $self->cached_workflow,
66 { append => 1 },
67 Dump( $self->workflow_data )
68 );
69  
70 0 my $now = DateTime->now();
71 0 $self->fh->say("#!/usr/bin/env bash\n\n");
72  
73 0 $self->fh->print($cmd_opts);
74 0 $self->fh->say($config_data);
75 }
76  
77 =head3 write_workflow_meta
78  
79 Write out the global variables in the start, and the ending variables in the end
80  
81 =cut
82  
83 0 ▶sub write_workflow_meta {
Subroutine write_workflow_meta: not called CC=4 SCAR=30.0
Pod documented
84 0 my $self = shift;
85 0 my $type = shift;
86  
87 0 ▶ return unless $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->verbose 0 0 0
88  
89 0 ▶ $self->write_workflow_meta_start if $type eq 'start';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "start" 0 0 0
90 0 ▶ $self->write_workflow_meta_end if $type eq 'end';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "end" 0 0 0
91 }
92  
93 0 ▶sub write_workflow_meta_start {
Subroutine write_workflow_meta_start: not called CC=3 SCAR=24.8
Pod undocumented
94 0 my $self = shift;
95 0 $self->fh->say("$self->{comment_char}\n");
96 0 $self->fh->say("$self->{comment_char} Starting Workflow\n");
97 0 $self->fh->say("$self->{comment_char}");
98 0 $self->fh->say("$self->{comment_char}");
99  
100 0 $self->fh->say("$self->{comment_char} Samples:");
101 $self->fh->say(
102 0 "$self->{comment_char} \t" . join( ', ', @{ $self->samples } ) );
103 0 $self->fh->say("$self->{comment_char}");
104 0 $self->fh->say("$self->{comment_char}");
105  
106 0 $self->fh->say("$self->{comment_char} Global Variables:");
107  
108 0 foreach my $k ( $self->all_global_keys ) {
109 0 ▶ next unless $k;
Branch: 1 branch
BranchTrueFalseTotal
unless $k 0 0 0
110 0 my $v = $self->global_attr->$k;
111 0 $self->fh->print( $self->write_pretty_meta( $k, $v ) );
112 }
113 0 $self->fh->say("$self->{comment_char}");
114 }
115  
116 0 ▶sub write_workflow_meta_end {
Subroutine write_workflow_meta_end: not called CC=1 SCAR=6.9
Pod undocumented
117 0 my $self = shift;
118 0 $self->fh->say("$self->{comment_char}");
119 0 $self->fh->say("$self->{comment_char} Ending Workflow");
120 0 $self->fh->say("$self->{comment_char}");
121 }
122  
123 =head2 write_rule_meta
124  
125 =cut
126  
127 0 ▶sub write_rule_meta {
Subroutine write_rule_meta: not called CC=8 SCAR=42.8
Pod documented
128 0 my $self = shift;
129 0 my $meta = shift;
130  
131 0 my @meta_text = ();
132  
133 0 push( @meta_text, "\n$self->{comment_char}" );
134  
135 0 ▶ if ( $meta eq "after_meta" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta eq "after_meta") 0 0 0
136  
137 0 push( @meta_text, "$self->{comment_char} Ending $self->{key}" );
138 }
139  
140 0 push( @meta_text, "$self->{comment_char}\n" );
141  
142 0 ▶ return \@meta_text unless $meta eq "before_meta";
Branch: 1 branch
BranchTrueFalseTotal
unless $meta eq "before_meta" 0 0 0
143  
144 0 push( @meta_text, "$self->{comment_char}" );
145 0 push( @meta_text, "$self->{comment_char} Starting $self->{rule_name}" );
146 0 push( @meta_text, "$self->{comment_char}" );
147  
148 0 ▶ return \@meta_text unless $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->verbose 0 0 0
149  
150 0 push( @meta_text, "\n\n$self->{comment_char}" );
151  
152 0 push( @meta_text, "$self->{comment_char} Variables" );
153  
154 0 push( @meta_text,
155 "$self->{comment_char} Indir: " . $self->local_attr->indir );
156  
157 0 push( @meta_text,
158 "$self->{comment_char} Outdir: " . $self->local_attr->outdir );
159  
160 0 ▶ if ( exists $self->local_rule->{ $self->rule_name }->{local} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->local_rule->{$self->rule_name}{"local"}) 0 0 0
161  
162 0 push( @meta_text, "$self->{comment_char}" );
163  
164 0 push( @meta_text, "$self->{comment_char} Local Variables:\n#" );
165  
166 0 foreach my $k ( $self->all_local_rule_keys ) {
167 0 my ($v) = $self->local_attr->$k;
168  
169 0 push( @meta_text, $self->write_pretty_meta( $k, $v ) );
170 }
171 }
172  
173 0 ▶ my $t = $self->write_sample_meta if $self->resample;
Branch: 1 branch
BranchTrueFalseTotal
if $self->resample 0 0 0
174 0 ▶ push( @meta_text, $t ) if $t;
Branch: 1 branch
BranchTrueFalseTotal
if $t 0 0 0
175  
176 0 $self->write_hpc_meta;
177  
178 0 push( @meta_text, "$self->{comment_char}\n" );
179  
180 0 push( @meta_text, "$self->{comment_char}" );
181 0 my @tmp_before_meta = split( "\n", $self->local_attr->before_meta );
182  
183 0 map { push( @meta_text, $self->decide_comment( trim($_) ) ) }
184 @tmp_before_meta;
185  
186 0 push( @meta_text, "$self->{comment_char}\n" );
187  
188 0 return \@meta_text;
189 }
190  
191 =head3 decide_comment
192  
193 In order to keep backwards compatibility with beforemeta
194 If the before_meta starts with a #, don't add another one
195  
196 =cut
197  
198 0 ▶sub decide_comment {
Subroutine decide_comment: not called CC=2 SCAR=17.9
Pod documented
199 0 my $self = shift;
200 0 my $current = shift;
201 0 my $comment_char = $self->comment_char;
202  
203 0 ▶ if ( $current =~ m/^$comment_char/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current =~ /^$comment_char/) { } 0 0 0
204 0 return $current;
205 }
206 else {
207 0 return $self->comment_char . $current;
208 }
209  
210 }
211  
212 0 ▶sub trim {
Subroutine trim: not called CC=1 SCAR=6.9
Pod undocumented
213 0 my $text = shift;
214  
215 0 $text =~ s/^\s+|\s+$//g;
216 0 return $text;
217 }
218  
219 =head3 write_hpc_meta
220  
221 =cut
222  
223 0 ▶sub write_hpc_meta {
Subroutine write_hpc_meta: not called CC=3 SCAR=24.8
Pod documented
224 0 my $self = shift;
225  
226 ##TODO Fix this for mixed data types
227 ##TODO Just make them all a hash
228  
229 0 $self->local_attr->add_before_meta( ' ### HPC Directives' . "\n" );
230 0 ▶ if ( ref( $self->local_attr->HPC ) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->local_attr->HPC eq 'HASH') { } 0 0 0
elsif (ref $self->local_attr->HPC eq 'ARRAY') { } 0 0 0
231 0 $self->write_hpc_hash_meta;
232 }
233 elsif ( ref( $self->local_attr->HPC ) eq 'ARRAY' ) {
234 0 $self->write_hpc_array_meta;
235 }
236  
237 }
238  
239 =head3 write_hpc_hash_meta
240  
241 Write meta when HPC is a HashRef
242  
243 If its a hash we can merge it
244  
245 =cut
246  
247 0 ▶sub write_hpc_hash_meta {
Subroutine write_hpc_hash_meta: not called CC=2 SCAR=17.9
Pod documented
248 0 my $self = shift;
249  
250 0 my $jobname = '';
251 0 ▶ if ( !exists $self->local_attr->HPC->{jobname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->local_attr->HPC->{'jobname'}) { } 0 0 0
252 0 $self->local_attr->add_before_meta(
253 'HPC jobname=' . $self->rule_name . "\n" );
254 0 $jobname = $self->rule_name;
255 }
256 else {
257 $self->local_attr->add_before_meta(
258 0 'HPC jobname=' . $self->local_attr->HPC->{jobname} . "\n" );
259 0 $jobname = $self->local_attr->HPC->{jobname};
260 0 delete $self->local_attr->HPC->{jobname};
261 }
262  
263 0 $self->iter_hpc_hash( $self->local_attr->HPC );
264 0 $self->local_attr->HPC->{jobname} = $jobname;
265 }
266  
267 =head3 write_hpc_array_meta
268  
269 Write meta when HPC is an ArrayRef
270  
271 =cut
272  
273 0 ▶sub write_hpc_array_meta {
Subroutine write_hpc_array_meta: not called CC=4 SCAR=30.0
Pod documented
274 0 my $self = shift;
275  
276 #First we look for keys to see if we get jobname
277  
278 0 my %lookup = ();
279  
280 0 ▶ if ( ref( $self->global_attr->HPC ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->global_attr->HPC eq 'ARRAY') { } 0 0 0
elsif (ref $self->global_attr->HPC eq 'HASH') { } 0 0 0
281 %lookup =
282 0 %{ $self->iter_hpc_array( $self->global_attr->HPC, \%lookup ) };
283 }
284 elsif ( ref( $self->global_attr->HPC ) eq 'HASH' ) {
285 0 %lookup = %{ $self->global_attr->HPC };
286 }
287  
288 0 %lookup = %{ $self->iter_hpc_array( $self->local_attr->HPC, \%lookup ) };
289  
290 0 ▶ if ( !exists $lookup{jobname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $lookup{'jobname'}) { } 0 0 0
291 0 $self->local_attr->add_before_meta(
292 'HPC jobname=' . $self->rule_name . "\n" );
293 unshift(
294 0 @{ $self->local_attr->HPC },
295 { 'jobname' => $self->rule_name }
296 );
297 }
298 else {
299 $self->local_attr->add_before_meta(
300 0 'HPC jobname=' . $lookup{jobname} . "\n" );
301 0 delete $lookup{jobname};
302 }
303  
304 0 $self->iter_hpc_hash( \%lookup );
305 }
306  
307 =head3 iter_hpc_array
308  
309 =cut
310  
311 0 ▶sub iter_hpc_array {
Subroutine iter_hpc_array: not called CC=3 SCAR=24.8
Pod documented
312 0 my $self = shift;
313 0 my $aref = shift;
314 0 my $lookup = shift;
315  
316 0 foreach my $href ( @{$aref} ) {
317 0 ▶ if ( ref($href) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $href eq 'HASH') { } 0 0 0
318 0 my @keys = keys %{$href};
319 0 map { $lookup->{$_} = $href->{$_} } @keys;
320 }
321 else {
322 0 $self->warn_hpc_meta;
323 0 return;
324 }
325 }
326  
327 0 return $lookup;
328 }
329  
330 =head3 iter_hpc_hash
331  
332 =cut
333  
334 0 ▶sub iter_hpc_hash {
Subroutine iter_hpc_hash: not called CC=3 SCAR=24.8
Pod documented
335 0 my $self = shift;
336 0 my $href = shift;
337  
338 #TODO Add in a lookup here that will check deps for any undeclared rules
339  
340 0 ▶ while ( my ( $k, $v ) = each %{$href} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$href;}) 0 0 0
341 0 ▶ if ( !ref($k) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $k) { } 0 0 0
342 0 $self->local_attr->add_before_meta( 'HPC ' . $k . '=' . $v . "\n" );
343 }
344 else {
345 0 $self->warn_hpc_meta;
346 }
347 }
348 }
349  
350 0 ▶sub warn_hpc_meta {
Subroutine warn_hpc_meta: not called CC=1 SCAR=6.9
Pod undocumented
351 0 my $self = shift;
352  
353 0 my $hpc = <<EOF;
354 Key/Value:
355  
356 HPC:
357 mem: 40GB
358 walltime: '02:00:00'
359  
360 List of Key/Value:
361 HPC:
362 - mem: 40GB
363 - walltime: '02:00:00'
364 EOF
365 0 $self->app_log->warn(
366 'You are using an unsupported data structure for HPC.');
367 0 $self->app_log->warn('HPC should be key/value or a list of key/value');
368 0 $self->app_log->warn(
369 'HPC data structure should look resemble the following:' . "\n"
370 . $hpc );
371 }
372  
373 =head3 write_sample_meta
374  
375 Write the meta for samples
376  
377 =cut
378  
379 #TODO add this to app log
380  
381 0 ▶sub write_sample_meta {
Subroutine write_sample_meta: not called CC=2 SCAR=17.9
Pod documented
382 0 my $self = shift;
383  
384 0 ▶ return unless $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->verbose 0 0 0
385 0 my $meta_text = "";
386  
387 0 $meta_text .= "$self->{comment_char}\n";
388 $meta_text .= "$self->{comment_char} Samples: "
389 0 . join( ', ', @{ $self->samples } ) . "\n";
390 0 $meta_text .= "$self->{comment_char}\n\n";
391  
392 $self->app_log->info(
393 0 'Found samples: ' . join( ', ', @{ $self->samples } ) . "\n" );
394  
395 0 return $meta_text;
396 }
397  
398 0 ▶sub write_pretty_meta {
Subroutine write_pretty_meta: not called CC=2 SCAR=17.9
Pod undocumented
399 0 my $self = shift;
400 0 my $k = shift;
401 0 my $v = shift;
402  
403 0 my $t = $self->get_pretty_value( $k, $v );
404 0 ▶ if ( length($t) > 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $t > 100) 0 0 0
405 0 return "$self->{comment_char}\t$k:\n" . $t . "\n";
406 }
407 0 my $comment_char = $self->comment_char;
408 0 $t =~ s/#//;
409 0 $t =~ s/\t//;
410 0 return "$self->{comment_char}\t$k:\t" . $t . "\n";
411 }
412  
413 0 ▶sub get_pretty_value {
Subroutine get_pretty_value: not called CC=5 SCAR=34.0
Pod undocumented
414 0 my $self = shift;
415 0 my $k = shift;
416 0 my $v = shift;
417  
418 0 my $t = '';
419 0 my $ref = 0;
420 0 ▶ if ( ref($v) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) 0 0 0
421 0 $ref = 1;
422 0 $v = Dump($v);
423 }
424 0 my %seen = ();
425 0 my @uniq_array = ();
426 0 my @array = split( "\n", $v );
427 ##Get rid of the top '-----' line produced by the yaml dump
428 0 ▶ shift(@array) if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 0 0 0
429 0 ▶ for ( my $x = 0 ; $x <= $#array ; $x++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $#array) 0 0 0
430 0 my $t = $self->comment_char . "\t\t" . $array[$x];
431 0 ▶ next if $seen{$t};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$t} 0 0 0
432 0 push( @uniq_array, $t );
433 0 $seen{$t} = 1;
434 }
435 0 $v = join( "\n", @uniq_array );
436 0 return $v;
437 }
438  
439 1 1;
« lib/BioX/Workflow/Command/run/Utils/Samples.pm ↑ Summary lib/BioX/Workflow/Command/run/Rules/Directives/Inspect.pm »