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

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