« blib/lib/BioX/Workflow/Command/run/Rules/Directives/Types/Stash.pm
↑ Summary
blib/lib/BioX/Workflow/Command/run.pm »
| 1 | package BioX::Workflow::Command::run::Rules::Rules; | ||
| 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 Storable qw(dclone); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Data::Merger qw(merger); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Data::Dumper; | ||
| 9 | use File::Path qw(make_path remove_tree); | ||
| 10 | use Try::Tiny; | ||
| 11 | use Path::Tiny; | ||
| 12 | |||
| 13 | use BioSAILs::Utils::Traits qw(ArrayRefOfStrs); | ||
| 14 | |||
| 15 | use BioX::Workflow::Command::run::Rules::Exceptions::KeyDeclaration; | ||
| 16 | use BioX::Workflow::Command::run::Rules::Exceptions::OneRuleName; | ||
| 17 | |||
| 18 | with 'BioX::Workflow::Command::run::Rules::Directives::Inspect'; | ||
| 19 | |||
| 20 | =head1 Name | ||
| 21 | |||
| 22 | BioX::Workflow::Command::run::Rules::Rules | ||
| 23 | |||
| 24 | =head2 Description | ||
| 25 | |||
| 26 | Role for Rules | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | =head2 Command Line Options | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | option 'select_rules' => ( | ||
| 35 | traits => ['Array'], | ||
| 36 | is => 'rw', | ||
| 37 | required => 0, | ||
| 38 | isa => ArrayRefOfStrs, | ||
| 39 | documentation => 'Select rules to process', | ||
| 40 | default => sub { [] }, | ||
| 41 | cmd_split => qr/,/, | ||
| 42 | handles => { | ||
| 43 | all_select_rules => 'elements', | ||
| 44 | has_select_rules => 'count', | ||
| 45 | join_select_rules => 'join', | ||
| 46 | }, | ||
| 47 | cmd_aliases => ['sr'], | ||
| 48 | ); | ||
| 49 | |||
| 50 | option 'select_after' => ( | ||
| 51 | is => 'rw', | ||
| 52 | isa => 'Str', | ||
| 53 | required => 0, | ||
| 54 | predicate => 'has_select_after', | ||
| 55 | clearer => 'clear_select_after', | ||
| 56 | documentation => 'Select rules after and including a particular rule.', | ||
| 57 | cmd_aliases => ['sa'], | ||
| 58 | ); | ||
| 59 | |||
| 60 | option 'select_before' => ( | ||
| 61 | is => 'rw', | ||
| 62 | isa => 'Str', | ||
| 63 | required => 0, | ||
| 64 | predicate => 'has_select_before', | ||
| 65 | clearer => 'clear_select_before', | ||
| 66 | documentation => 'Select rules before and including a particular rule.', | ||
| 67 | cmd_aliases => ['sb'], | ||
| 68 | ); | ||
| 69 | |||
| 70 | option 'select_between' => ( | ||
| 71 | traits => ['Array'], | ||
| 72 | is => 'rw', | ||
| 73 | isa => ArrayRefOfStrs, | ||
| 74 | documentation => 'select rules to process', | ||
| 75 | cmd_split => qr/,/, | ||
| 76 | required => 0, | ||
| 77 | default => sub { [] }, | ||
| 78 | documentation => 'Select sets of rules. Ex: rule1-rule2,rule4-rule5', | ||
| 79 | cmd_aliases => ['sbtwn'], | ||
| 80 | handles => { | ||
| 81 | all_select_between => 'elements', | ||
| 82 | has_select_between => 'count', | ||
| 83 | join_select_between => 'join', | ||
| 84 | }, | ||
| 85 | ); | ||
| 86 | |||
| 87 | option 'omit_rules' => ( | ||
| 88 | traits => ['Array'], | ||
| 89 | is => 'rw', | ||
| 90 | required => 0, | ||
| 91 | isa => ArrayRefOfStrs, | ||
| 92 | documentation => 'Omit rules to process', | ||
| 93 | default => sub { [] }, | ||
| 94 | cmd_split => qr/,/, | ||
| 95 | handles => { | ||
| 96 | all_omit_rules => 'elements', | ||
| 97 | has_omit_rules => 'count', | ||
| 98 | join_omit_rules => 'join', | ||
| 99 | }, | ||
| 100 | cmd_aliases => ['or'], | ||
| 101 | ); | ||
| 102 | |||
| 103 | option 'omit_after' => ( | ||
| 104 | is => 'rw', | ||
| 105 | isa => 'Str', | ||
| 106 | required => 0, | ||
| 107 | predicate => 'has_omit_after', | ||
| 108 | clearer => 'clear_omit_after', | ||
| 109 | documentation => 'Omit rules after and including a particular rule.', | ||
| 110 | cmd_aliases => ['oa'], | ||
| 111 | ); | ||
| 112 | |||
| 113 | option 'omit_before' => ( | ||
| 114 | is => 'rw', | ||
| 115 | isa => 'Str', | ||
| 116 | required => 0, | ||
| 117 | predicate => 'has_omit_before', | ||
| 118 | clearer => 'clear_omit_before', | ||
| 119 | documentation => 'Omit rules before and including a particular rule.', | ||
| 120 | cmd_aliases => ['ob'], | ||
| 121 | ); | ||
| 122 | |||
| 123 | option 'omit_between' => ( | ||
| 124 | traits => ['Array'], | ||
| 125 | is => 'rw', | ||
| 126 | isa => ArrayRefOfStrs, | ||
| 127 | documentation => 'omit rules to process', | ||
| 128 | cmd_split => qr/,/, | ||
| 129 | required => 0, | ||
| 130 | default => sub { [] }, | ||
| 131 | documentation => 'Omit sets of rules. Ex: rule1-rule2,rule4-rule5', | ||
| 132 | cmd_aliases => ['obtwn'], | ||
| 133 | handles => { | ||
| 134 | all_omit_between => 'elements', | ||
| 135 | has_omit_between => 'count', | ||
| 136 | join_omit_between => 'join', | ||
| 137 | }, | ||
| 138 | ); | ||
| 139 | |||
| 140 | option 'select_match' => ( | ||
| 141 | traits => ['Array'], | ||
| 142 | is => 'rw', | ||
| 143 | required => 0, | ||
| 144 | isa => ArrayRefOfStrs, | ||
| 145 | documentation => 'Match rules to select', | ||
| 146 | default => sub { [] }, | ||
| 147 | cmd_split => qr/,/, | ||
| 148 | handles => { | ||
| 149 | all_select_match => 'elements', | ||
| 150 | has_select_match => 'count', | ||
| 151 | join_select_match => 'join', | ||
| 152 | }, | ||
| 153 | cmd_aliases => ['sm'], | ||
| 154 | ); | ||
| 155 | |||
| 156 | option 'omit_match' => ( | ||
| 157 | traits => ['Array'], | ||
| 158 | is => 'rw', | ||
| 159 | required => 0, | ||
| 160 | isa => ArrayRefOfStrs, | ||
| 161 | documentation => 'Match rules to omit', | ||
| 162 | default => sub { [] }, | ||
| 163 | cmd_split => qr/,/, | ||
| 164 | handles => { | ||
| 165 | all_omit_match => 'elements', | ||
| 166 | has_omit_match => 'count', | ||
| 167 | join_omit_match => 'join', | ||
| 168 | }, | ||
| 169 | cmd_aliases => ['om'], | ||
| 170 | ); | ||
| 171 | |||
| 172 | # TODO Change this to rules? | ||
| 173 | has 'rule_keys' => ( | ||
| 174 | is => 'rw', | ||
| 175 | isa => 'ArrayRef', | ||
| 176 | default => sub { return [] }, | ||
| 177 | ); | ||
| 178 | |||
| 179 | has 'local_rule_keys' => ( | ||
| 180 | traits => ['Array'], | ||
| 181 | is => 'rw', | ||
| 182 | isa => 'ArrayRef', | ||
| 183 | default => sub { return [] }, | ||
| 184 | handles => { | ||
| 185 | all_local_rule_keys => 'elements', | ||
| 186 | has_local_rule_keys => 'count', | ||
| 187 | }, | ||
| 188 | ); | ||
| 189 | |||
| 190 | has 'global_keys' => ( | ||
| 191 | traits => ['Array'], | ||
| 192 | is => 'rw', | ||
| 193 | isa => 'ArrayRef', | ||
| 194 | default => sub { return [] }, | ||
| 195 | handles => { | ||
| 196 | all_global_keys => 'elements', | ||
| 197 | has_global_keys => 'count', | ||
| 198 | first_index_global_keys => 'first_index', | ||
| 199 | }, | ||
| 200 | ); | ||
| 201 | |||
| 202 | has [ 'select_effect', 'omit_effect' ] => ( | ||
| 203 | is => 'rw', | ||
| 204 | isa => 'Bool', | ||
| 205 | default => 0, | ||
| 206 | ); | ||
| 207 | |||
| 208 | has 'dummy_sample' => ( | ||
| 209 | is => 'rw', | ||
| 210 | isa => 'Str', | ||
| 211 | default => '__DUMMYSAMPLE123456789__' | ||
| 212 | ); | ||
| 213 | |||
| 214 | has 'dummy_iterable' => ( | ||
| 215 | is => 'rw', | ||
| 216 | isa => 'Str', | ||
| 217 | default => '__DUMMYITER123456789__' | ||
| 218 | ); | ||
| 219 | |||
| 220 | #This should be in its own role | ||
| 221 | sub iterate_rules { | ||
| 222 | my $self = shift; | ||
| 223 | |||
| 224 | $self->set_rule_names; | ||
| 225 | my $rules = $self->workflow_data->{rules}; | ||
| 226 | |||
| 227 | $self->filter_rule_keys; | ||
| 228 | |||
| 229 | foreach my $rule (@$rules) { | ||
| 230 | $self->local_rule($rule); | ||
| 231 | my $except = $self->process_rule; | ||
| 232 | next if $except; | ||
| 233 | $self->p_rule_name( $self->rule_name ); | ||
| 234 | $self->p_local_attr( dclone( $self->local_attr ) ); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | =head3 filter_rule_keys | ||
| 239 | |||
| 240 | First option is to use --use_timestamps | ||
| 241 | The user can also override the timestamps with --select_* --omit_* | ||
| 242 | |||
| 243 | Use the --select_rules and --omit_rules options to choose rules. | ||
| 244 | |||
| 245 | By default all rules are selected | ||
| 246 | |||
| 247 | =cut | ||
| 248 | |||
| 249 | sub filter_rule_keys { | ||
| 250 | my $self = shift; | ||
| 251 | |||
| 252 | $self->select_rule_keys( dclone( $self->rule_names ) ); | ||
| 253 | |||
| 254 | $self->set_rule_keys('select'); | ||
| 255 | $self->set_rule_keys('omit'); | ||
| 256 | |||
| 257 | $self->app_log->info( 'Selected rules:' . "\t" | ||
| 258 | . join( ', ', @{ $self->select_rule_keys } ) | ||
| 259 | . "\n" ); | ||
| 260 | } | ||
| 261 | |||
| 262 | =head3 set_rule_names | ||
| 263 | |||
| 264 | Iterate over the rule names and add them to our array | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | sub set_rule_names { | ||
| 269 | my $self = shift; | ||
| 270 | my $rules = $self->workflow_data->{rules}; | ||
| 271 | |||
| 272 | my @rule_names = map { my ($key) = keys %{$_}; $key } @{$rules}; | ||
| 273 | @rule_names = map { my $t = $_; $t =~ s/^\s+|\s+$//g; $t } @rule_names; | ||
| 274 | $self->rule_names( \@rule_names ); | ||
| 275 | $self->app_log->info( 'Found rules:' . "\t" . join( ', ', @rule_names ) ); | ||
| 276 | } | ||
| 277 | |||
| 278 | #TODO This is confusing change names | ||
| 279 | |||
| 280 | =head3 set_rule_keys | ||
| 281 | |||
| 282 | If we have any select_* or select_match, get those rules before we start processing | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | sub set_rule_keys { | ||
| 287 | my $self = shift; | ||
| 288 | my $cond = shift || 'select'; | ||
| 289 | |||
| 290 | my @rules = (); | ||
| 291 | my $rule_exists = 1; | ||
| 292 | my @rule_name_exists = (); | ||
| 293 | |||
| 294 | my $effect = $cond . '_effect'; | ||
| 295 | |||
| 296 | my ( $has_rules, $has_bf, $has_af, $has_btw, $has_match ) = | ||
| 297 | map { 'has_' . $cond . '_' . $_ } | ||
| 298 | ( 'rules', 'before', 'after', 'between', 'match' ); | ||
| 299 | |||
| 300 | my ( $bf, $af ) = ( $cond . '_before', $cond . '_after' ); | ||
| 301 | |||
| 302 | my ( $btw, $all_rules, $all_matches ) = | ||
| 303 | map { 'all_' . $cond . '_' . $_ } ( 'between', 'rules', 'match' ); | ||
| 304 | |||
| 305 | my ($rule_keys) = ( $cond . '_rule_keys' ); | ||
| 306 | |||
| 307 | if ( $self->$has_rules ) { | ||
| 308 | $self->$effect(1); | ||
| 309 | foreach my $r ( $self->$all_rules ) { | ||
| 310 | if ( $self->first_index_rule_names( sub { $_ eq $r } ) != -1 ) { | ||
| 311 | push( @rules, $r ); | ||
| 312 | } | ||
| 313 | else { | ||
| 314 | $self->app_log->warn( | ||
| 315 | "You selected a rule $r that does not exist"); | ||
| 316 | $rule_exists = 0; | ||
| 317 | push( @rule_name_exists, $r ); | ||
| 318 | } | ||
| 319 | } | ||
| 320 | } | ||
| 321 | elsif ( $self->$has_bf ) { | ||
| 322 | $self->$effect(1); | ||
| 323 | my $index = $self->first_index_rule_names( sub { $_ eq $self->$bf } ); | ||
| 324 | if ( $index == -1 ) { | ||
| 325 | $self->app_log->warn( "You " | ||
| 326 | . $cond | ||
| 327 | . "ed a rule " | ||
| 328 | . $self->$bf | ||
| 329 | . " that does not exist" ); | ||
| 330 | $rule_exists = 0; | ||
| 331 | push( @rule_name_exists, $self->$bf ); | ||
| 332 | } | ||
| 333 | for ( my $x = 0 ; $x <= $index ; $x++ ) { | ||
| 334 | push( @rules, $self->rule_names->[$x] ); | ||
| 335 | } | ||
| 336 | } | ||
| 337 | elsif ( $self->$has_af ) { | ||
| 338 | $self->$effect(1); | ||
| 339 | my $index = $self->first_index_rule_names( sub { $_ eq $self->$af } ); | ||
| 340 | if ( $index == -1 ) { | ||
| 341 | $self->app_log->warn( "You " | ||
| 342 | . $cond | ||
| 343 | . "ed a rule " | ||
| 344 | . $self->$af | ||
| 345 | . " that does not exist" ); | ||
| 346 | $rule_exists = 0; | ||
| 347 | push( @rule_name_exists, $self->$af ); | ||
| 348 | } | ||
| 349 | for ( my $x = $index ; $x < $self->has_rule_names ; $x++ ) { | ||
| 350 | push( @rules, $self->rule_names->[$x] ); | ||
| 351 | } | ||
| 352 | } | ||
| 353 | elsif ( $self->$has_btw ) { | ||
| 354 | $self->$effect(1); | ||
| 355 | foreach my $rule ( $self->$btw ) { | ||
| 356 | my (@array) = split( '-', $rule ); | ||
| 357 | |||
| 358 | my $index1 = | ||
| 359 | $self->first_index_rule_names( sub { $_ eq $array[0] } ); | ||
| 360 | my $index2 = | ||
| 361 | $self->first_index_rule_names( sub { $_ eq $array[1] } ); | ||
| 362 | |||
| 363 | if ( $index1 == -1 || $index2 == -1 ) { | ||
| 364 | $self->app_log->warn( "You " | ||
| 365 | . $cond | ||
| 366 | . "ed a set of rules " | ||
| 367 | . join( ',', $self->$btw ) | ||
| 368 | . " that does not exist" ); | ||
| 369 | $rule_exists = 0; | ||
| 370 | push( @rule_name_exists, $rule ); | ||
| 371 | } | ||
| 372 | |||
| 373 | for ( my $x = $index1 ; $x <= $index2 ; $x++ ) { | ||
| 374 | push( @rules, $self->rule_names->[$x] ); | ||
| 375 | } | ||
| 376 | } | ||
| 377 | } | ||
| 378 | elsif ( $self->$has_match ) { | ||
| 379 | $self->$effect(1); | ||
| 380 | foreach my $match_rule ( $self->$all_matches ) { | ||
| 381 | my @t_rules = $self->grep_rule_names( sub { /$match_rule/ } ); | ||
| 382 | map { push( @rules, $_ ) } @t_rules; | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | $self->$rule_keys( \@rules ) if @rules; | ||
| 387 | |||
| 388 | # return ( $rule_exists, @rule_name_exists ); | ||
| 389 | } | ||
| 390 | |||
| 391 | =head3 check_select | ||
| 392 | |||
| 393 | See if the the current rule_name exists in either select_* or omit_* | ||
| 394 | |||
| 395 | =cut | ||
| 396 | |||
| 397 | sub check_select { | ||
| 398 | my $self = shift; | ||
| 399 | my $cond = shift || 'select'; | ||
| 400 | |||
| 401 | my $findex = 'first_index_' . $cond . '_rule_keys'; | ||
| 402 | my $index = $self->$findex( sub { $_ eq $self->rule_name } ); | ||
| 403 | |||
| 404 | return 0 if $index == -1; | ||
| 405 | return 1; | ||
| 406 | } | ||
| 407 | |||
| 408 | =head3 process_rule | ||
| 409 | |||
| 410 | This function is just a placeholder for the other functions we need to process a rule | ||
| 411 | |||
| 412 | 1. Do a sanity check of the rule - it could be yaml/json friendly but not biox friendly | ||
| 413 | 2. Clone the local attr | ||
| 414 | 3. Check for carrying indir/outdir INPUT/OUTPUT | ||
| 415 | 4. Apply the local attr - Add all the local: keys to our attr | ||
| 416 | 5. Get the keys of the rule | ||
| 417 | 6. Finally, process the template, or the process: key | ||
| 418 | |||
| 419 | =cut | ||
| 420 | |||
| 421 | sub process_rule { | ||
| 422 | my $self = shift; | ||
| 423 | |||
| 424 | my $except = $self->sanity_check_rule; | ||
| 425 | return $except if $except; | ||
| 426 | |||
| 427 | $self->local_attr( dclone( $self->global_attr ) ); | ||
| 428 | |||
| 429 | $self->carry_directives; | ||
| 430 | |||
| 431 | $self->apply_local_attr; | ||
| 432 | |||
| 433 | $self->get_keys; | ||
| 434 | $self->template_process; | ||
| 435 | return 0; | ||
| 436 | } | ||
| 437 | |||
| 438 | =head3 sanity_check_rule | ||
| 439 | |||
| 440 | Check the rule to make sure it only has 1 key | ||
| 441 | |||
| 442 | =cut | ||
| 443 | |||
| 444 | #TODO make this into a type Instead | ||
| 445 | |||
| 446 | sub sanity_check_rule { | ||
| 447 | my $self = shift; | ||
| 448 | |||
| 449 | my $except; | ||
| 450 | |||
| 451 | $except = $self->sanity_check_rule_one_rule_name; | ||
| 452 | return $except if $except; | ||
| 453 | |||
| 454 | if ( !exists $self->local_rule->{ $self->rule_name }->{process} ) { | ||
| 455 | $self->local_rule->{ $self->rule_name }->{process} = ''; | ||
| 456 | $self->app_log->warn( | ||
| 457 | 'Rule does not have a process. If you have registered a namespace please ignore this error.' | ||
| 458 | ); | ||
| 459 | } | ||
| 460 | |||
| 461 | if ( !exists $self->local_rule->{ $self->rule_name }->{local} ) { | ||
| 462 | $self->local_rule->{ $self->rule_name }->{local} = []; | ||
| 463 | } | ||
| 464 | else { | ||
| 465 | $except = $self->sanity_check_rule_local_structure; | ||
| 466 | return $except if $except; | ||
| 467 | } | ||
| 468 | |||
| 469 | $self->app_log->info( | ||
| 470 | 'Rule: ' . $self->rule_name . ' passes sanity check' ); | ||
| 471 | return 0; | ||
| 472 | } | ||
| 473 | |||
| 474 | sub sanity_check_rule_one_rule_name { | ||
| 475 | my $self = shift; | ||
| 476 | |||
| 477 | my @keys = keys %{ $self->local_rule }; | ||
| 478 | $self->rule_name( $keys[0] ); | ||
| 479 | |||
| 480 | return if $#keys == 0; | ||
| 481 | |||
| 482 | my $except = | ||
| 483 | BioX::Workflow::Command::run::Rules::Exceptions::OneRuleName->new(); | ||
| 484 | $self->app_log->info( 'Rule: ' . $self->rule_name . ' fails sanity check' ); | ||
| 485 | if(exists $self->local_rule->{process}){ | ||
| 486 | $self->app_log->warn('\'Process\' found at same indentation as rule name. Increase the indentation for this key!'); | ||
| 487 | } | ||
| 488 | if(exists $self->local_rule->{local}){ | ||
| 489 | $self->app_log->warn('\'Local\' found at same indentation as rule name. Increase the indentation for this key!'); | ||
| 490 | } | ||
| 491 | |||
| 492 | $except->warn( $self->app_log ); | ||
| 493 | $self->sanity_check_fail; | ||
| 494 | |||
| 495 | $self->inspect_obj->{errors}->{rules}->{ $self->rule_name }->{structure} = | ||
| 496 | 'OneRuleName'; | ||
| 497 | return $except; | ||
| 498 | } | ||
| 499 | |||
| 500 | sub sanity_check_rule_local_structure { | ||
| 501 | my $self = shift; | ||
| 502 | my $ref = $self->local_rule->{ $self->rule_name }->{local}; | ||
| 503 | |||
| 504 | return if ref($ref) eq 'ARRAY'; | ||
| 505 | |||
| 506 | my $except = | ||
| 507 | BioX::Workflow::Command::run::Rules::Exceptions::KeyDeclaration->new(); | ||
| 508 | $self->app_log->info( 'Rule: ' . $self->rule_name . ' fails sanity check' ); | ||
| 509 | $except->warn( $self->app_log ); | ||
| 510 | $self->sanity_check_fail; | ||
| 511 | $self->inspect_obj->{errors}->{rules}->{ $self->rule_name }->{structure} = | ||
| 512 | 'KeyDeclaration'; | ||
| 513 | |||
| 514 | return $except; | ||
| 515 | } | ||
| 516 | |||
| 517 | =head3 template_process | ||
| 518 | |||
| 519 | Do the actual processing of the rule->process | ||
| 520 | |||
| 521 | =cut | ||
| 522 | |||
| 523 | sub template_process { | ||
| 524 | my $self = shift; | ||
| 525 | my $texts = []; | ||
| 526 | |||
| 527 | $self->process_obj->{ $self->rule_name } = {}; | ||
| 528 | |||
| 529 | my $dummy_sample = $self->dummy_sample; | ||
| 530 | my $dummy_texts = $self->check_iterables( $dummy_sample, [] ); | ||
| 531 | |||
| 532 | if ( !$self->local_attr->override_process ) { | ||
| 533 | foreach my $sample ( $self->all_samples ) { | ||
| 534 | foreach my $text ( @{$dummy_texts} ) { | ||
| 535 | my $new_text = $text; | ||
| 536 | $new_text =~ s/$dummy_sample/$sample/g; | ||
| 537 | push( @$texts, $new_text ); | ||
| 538 | } | ||
| 539 | } | ||
| 540 | |||
| 541 | $self->process_obj->{ $self->rule_name }->{text} = $texts; | ||
| 542 | $self->process_obj->{ $self->rule_name }->{run_stats} = | ||
| 543 | $self->local_attr->run_stats; | ||
| 544 | } | ||
| 545 | else { | ||
| 546 | $self->process_obj->{ $self->rule_name }->{text} = $dummy_texts; | ||
| 547 | } | ||
| 548 | |||
| 549 | $self->process_obj->{ $self->rule_name }->{meta} = | ||
| 550 | $self->write_rule_meta('before_meta'); | ||
| 551 | } | ||
| 552 | |||
| 553 | =head3 use_iterables | ||
| 554 | |||
| 555 | Check the global and local keys to see if we are using any iterables | ||
| 556 | |||
| 557 | use_chroms: 1 | ||
| 558 | use_chunks: 1 | ||
| 559 | |||
| 560 | This is still here, but its use is discouraged | ||
| 561 | |||
| 562 | Instead check out the Mustache Template for easier loops | ||
| 563 | |||
| 564 | =cut | ||
| 565 | |||
| 566 | sub use_iterables { | ||
| 567 | my $self = shift; | ||
| 568 | |||
| 569 | my $iter = ''; | ||
| 570 | my $use_iter = 0; | ||
| 571 | my @use = (); | ||
| 572 | map { | ||
| 573 | if ( $_ =~ m/^use_/ ) { push( @use, $_ ) } | ||
| 574 | } @{ $self->rule_keys }; | ||
| 575 | map { | ||
| 576 | if ( $_ =~ m/^use_/ ) { push( @use, $_ ) } | ||
| 577 | } @{ $self->local_rule_keys }; | ||
| 578 | |||
| 579 | my $use = pop(@use); | ||
| 580 | |||
| 581 | return 0 if !$use; | ||
| 582 | |||
| 583 | my $base = $use; | ||
| 584 | $base =~ s/use_//; | ||
| 585 | |||
| 586 | my $no = 'no_' . $base; | ||
| 587 | |||
| 588 | return 0 if $self->local_attr->$no; | ||
| 589 | |||
| 590 | my $elem = $base; | ||
| 591 | $elem =~ s/s$//; | ||
| 592 | my $all = 'all_' . $elem . '_lists'; | ||
| 593 | |||
| 594 | return [ $all, $elem ]; | ||
| 595 | } | ||
| 596 | |||
| 597 | sub check_iterables { | ||
| 598 | my $self = shift; | ||
| 599 | my $sample = shift; | ||
| 600 | my $texts = shift; | ||
| 601 | |||
| 602 | #First check the global for any lists | ||
| 603 | my $use_iters = $self->use_iterables; | ||
| 604 | |||
| 605 | if ( !$use_iters ) { | ||
| 606 | $texts = $self->in_template_process( $sample, $texts ); | ||
| 607 | return $texts; | ||
| 608 | } | ||
| 609 | |||
| 610 | my $all = $use_iters->[0]; | ||
| 611 | my $elem = $use_iters->[1]; | ||
| 612 | |||
| 613 | ##TODO This should be a separate function | ||
| 614 | my $dummy_iter = $self->dummy_iterable; | ||
| 615 | $self->local_attr->$elem($dummy_iter); | ||
| 616 | |||
| 617 | my $dummy_texts = $self->in_template_process( $sample, [] ); | ||
| 618 | |||
| 619 | foreach my $chunk ( $self->local_attr->$all ) { | ||
| 620 | foreach my $text ( @{$dummy_texts} ) { | ||
| 621 | my $new_text = $text; | ||
| 622 | $new_text =~ s/$dummy_iter/$chunk/g; | ||
| 623 | push( @$texts, $new_text ); | ||
| 624 | } | ||
| 625 | } | ||
| 626 | |||
| 627 | return $texts; | ||
| 628 | } | ||
| 629 | |||
| 630 | sub in_template_process { | ||
| 631 | my $self = shift; | ||
| 632 | my $sample = shift; | ||
| 633 | my $texts = shift; | ||
| 634 | |||
| 635 | $self->local_attr->sample($sample); | ||
| 636 | $self->sample($sample); | ||
| 637 | my $text = $self->eval_process(); | ||
| 638 | |||
| 639 | push( @{$texts}, $text ) if $self->print_within_rule; | ||
| 640 | |||
| 641 | return $texts; | ||
| 642 | } | ||
| 643 | |||
| 644 | sub walk_attr { | ||
| 645 | my $self = shift; | ||
| 646 | |||
| 647 | my $attr = dclone( $self->local_attr ); | ||
| 648 | $attr->sample( $self->sample ) if $self->has_sample; | ||
| 649 | $self->check_indir_outdir($attr); | ||
| 650 | |||
| 651 | $attr->walk_process_data( $self->rule_keys ); | ||
| 652 | |||
| 653 | return $attr; | ||
| 654 | } | ||
| 655 | |||
| 656 | sub eval_process { | ||
| 657 | my $self = shift; | ||
| 658 | |||
| 659 | my $attr = $self->walk_attr; | ||
| 660 | $self->walk_indir_outdir($attr); | ||
| 661 | |||
| 662 | my $text = $self->eval_rule($attr); | ||
| 663 | $text = clean_text($text); | ||
| 664 | |||
| 665 | $self->walk_FILES($attr); | ||
| 666 | $self->clear_files; | ||
| 667 | |||
| 668 | ##Carry stash when not in template | ||
| 669 | my $sample = $attr->sample; | ||
| 670 | |||
| 671 | $self->return_rule_as_obj($attr); | ||
| 672 | |||
| 673 | if ( $text =~ m/The following errors/ ) { | ||
| 674 | $self->app_log->warn( 'Error for \'process\' Line #: ' | ||
| 675 | . $self->inspect_obj->{line_numbers}->{rules} | ||
| 676 | ->{ $self->rule_name }->{process}->{line} ); | ||
| 677 | my $dummytext = $text; | ||
| 678 | $dummytext =~ s/__DUMMYSAMPLE123456789__/Sample_XYZ/g; | ||
| 679 | $self->inspect_obj->{errors}->{rules}->{ $self->rule_name }->{process} | ||
| 680 | ->{msg} = $dummytext; | ||
| 681 | |||
| 682 | $self->inspect_obj->{errors}->{rules}->{ $self->rule_name }->{process} | ||
| 683 | ->{error_types} = $self->get_error_types( 'process', $dummytext ); | ||
| 684 | } | ||
| 685 | |||
| 686 | $attr->sample($sample); | ||
| 687 | $self->local_attr->stash( dclone( $attr->stash ) ); | ||
| 688 | |||
| 689 | return $text; | ||
| 690 | } | ||
| 691 | |||
| 692 | =head3 eval_rule | ||
| 693 | |||
| 694 | Check to see if there is a custom method registered. | ||
| 695 | |||
| 696 | Otherwise process the template as normal. | ||
| 697 | |||
| 698 | =cut | ||
| 699 | |||
| 700 | sub eval_rule { | ||
| 701 | my $self = shift; | ||
| 702 | my $attr = shift; | ||
| 703 | |||
| 704 | my $process = $self->local_rule->{ $self->rule_name }->{process}; | ||
| 705 | my $text; | ||
| 706 | |||
| 707 | my $eval_rule = 'eval_rule_' . $self->rule_name; | ||
| 708 | if ( $attr->can($eval_rule) ) { | ||
| 709 | try { | ||
| 710 | $text = $attr->$eval_rule($process); | ||
| 711 | } | ||
| 712 | catch { | ||
| 713 | $self->app_log->warn( | ||
| 714 | 'There was a problem evaluating rule. Error is:'); | ||
| 715 | $self->app_log->warn($_); | ||
| 716 | }; | ||
| 717 | } | ||
| 718 | else { | ||
| 719 | $text = $attr->interpol_directive($process); | ||
| 720 | } | ||
| 721 | |||
| 722 | if ( $attr->_ERROR == 1 ) { | ||
| 723 | $self->app_log->warn( | ||
| 724 | 'There were 1 or more errors evaluating rule: ' | ||
| 725 | . $self->rule_name . '. ' | ||
| 726 | . 'Please check the output file for details.' | ||
| 727 | . "\n" ); | ||
| 728 | } | ||
| 729 | $attr->_ERROR(0); | ||
| 730 | |||
| 731 | return $text; | ||
| 732 | } | ||
| 733 | |||
| 734 | sub get_global_keys { | ||
| 735 | my $self = shift; | ||
| 736 | my @global_keys = (); | ||
| 737 | |||
| 738 | map { my ($key) = keys %{$_}; push( @global_keys, $key ) } | ||
| 739 | @{ $self->workflow_data->{global} }; | ||
| 740 | |||
| 741 | $self->global_keys( \@global_keys ); | ||
| 742 | } | ||
| 743 | |||
| 744 | sub get_keys { | ||
| 745 | my $self = shift; | ||
| 746 | |||
| 747 | my %seen = (); | ||
| 748 | my @local_keys = map { my ($key) = keys %{$_}; $seen{$key} = 1; $key } | ||
| 749 | @{ $self->local_rule->{ $self->rule_name }->{local} }; | ||
| 750 | |||
| 751 | my @global_keys = (); | ||
| 752 | map { my ($key) = keys %{$_}; push( @global_keys, $key ) if !$seen{$key} } | ||
| 753 | @{ $self->workflow_data->{global} }; | ||
| 754 | |||
| 755 | $self->local_rule_keys( dclone( \@local_keys ) ); | ||
| 756 | |||
| 757 | my @special_keys = ( 'indir', 'outdir', 'INPUT', 'OUTPUT' ); | ||
| 758 | foreach my $key (@special_keys) { | ||
| 759 | if ( !$seen{$key} ) { | ||
| 760 | unshift( @local_keys, $key ); | ||
| 761 | } | ||
| 762 | } | ||
| 763 | |||
| 764 | map { push( @global_keys, $_ ) } @local_keys; | ||
| 765 | |||
| 766 | $self->rule_keys( \@global_keys ); | ||
| 767 | } | ||
| 768 | |||
| 769 | ##TODO Write more tests | ||
| 770 | sub walk_indir_outdir { | ||
| 771 | my $self = shift; | ||
| 772 | my $attr = shift; | ||
| 773 | |||
| 774 | my $text = $attr->interpol_directive( $attr->outdir ); | ||
| 775 | $self->walk_indir_outdir_sample( $attr, $text ); | ||
| 776 | } | ||
| 777 | |||
| 778 | sub walk_indir_outdir_sample { | ||
| 779 | my $self = shift; | ||
| 780 | my $attr = shift; | ||
| 781 | my $text = shift; | ||
| 782 | |||
| 783 | my $use_iters = $self->use_iterables; | ||
| 784 | my $dummy_sample = $self->dummy_sample; | ||
| 785 | |||
| 786 | foreach my $sample ( $attr->all_samples ) { | ||
| 787 | my $new_text = $text; | ||
| 788 | $new_text =~ s/$dummy_sample/$sample/g; | ||
| 789 | |||
| 790 | if ($use_iters) { | ||
| 791 | $self->walk_indir_outdir_iters( $use_iters, $attr, $new_text ); | ||
| 792 | } | ||
| 793 | else { | ||
| 794 | $new_text = path($new_text)->absolute if $attr->coerce_abs_dir; | ||
| 795 | $new_text = path($new_text) if !$attr->coerce_abs_dir; | ||
| 796 | $self->decide_create_outdir( $attr, $new_text ); | ||
| 797 | } | ||
| 798 | } | ||
| 799 | } | ||
| 800 | |||
| 801 | sub walk_indir_outdir_iters { | ||
| 802 | my $self = shift; | ||
| 803 | my $use_iters = shift; | ||
| 804 | my $attr = shift; | ||
| 805 | my $text = shift; | ||
| 806 | |||
| 807 | return unless $use_iters; | ||
| 808 | |||
| 809 | my $all = $use_iters->[0]; | ||
| 810 | my $elem = $use_iters->[1]; | ||
| 811 | |||
| 812 | my $dummy_iter = $self->dummy_iterable; | ||
| 813 | $attr->$elem($dummy_iter); | ||
| 814 | |||
| 815 | foreach my $chunk ( $self->local_attr->$all ) { | ||
| 816 | my $new_text = $text; | ||
| 817 | $new_text =~ s/$dummy_iter/$chunk/g; | ||
| 818 | $new_text = path($new_text)->absolute if $attr->coerce_abs_dir; | ||
| 819 | $new_text = path($new_text) if !$attr->coerce_abs_dir; | ||
| 820 | $self->decide_create_outdir( $attr, $new_text ); | ||
| 821 | } | ||
| 822 | } | ||
| 823 | |||
| 824 | sub decide_create_outdir { | ||
| 825 | my $self = shift; | ||
| 826 | my $attr = shift; | ||
| 827 | my $dir = shift; | ||
| 828 | |||
| 829 | return unless $attr->create_outdir; | ||
| 830 | return unless $dir; | ||
| 831 | |||
| 832 | try { | ||
| 833 | $dir->mkpath; | ||
| 834 | } | ||
| 835 | catch { | ||
| 836 | $self->app_log->fatal( "We were not able to make the directory.\n\t" | ||
| 837 | . $attr->outdir | ||
| 838 | . "\n\tError: $!" ); | ||
| 839 | }; | ||
| 840 | } | ||
| 841 | |||
| 842 | sub clean_text { | ||
| 843 | my $text = shift; | ||
| 844 | my @text = split( "\n", $text ); | ||
| 845 | my @new_text = (); | ||
| 846 | |||
| 847 | foreach my $t (@text) { | ||
| 848 | $t =~ s/^\s+|\s+$//g; | ||
| 849 | if ( $t !~ /^\s*$/ ) { | ||
| 850 | push( @new_text, $t ); | ||
| 851 | } | ||
| 852 | } | ||
| 853 | |||
| 854 | $text = join( "\n", @new_text ); | ||
| 855 | return $text; | ||
| 856 | } | ||
| 857 | |||
| 858 | =head3 print_rule | ||
| 859 | |||
| 860 | Decide if we print the rule | ||
| 861 | |||
| 862 | There are 3 main decision trees | ||
| 863 | |||
| 864 | 1. User specifies --select_* | ||
| 865 | 2. User specified --omit_* | ||
| 866 | 3. User specified --use_timestamps | ||
| 867 | |||
| 868 | select_* and omit_* take precedence over use_timestamps | ||
| 869 | |||
| 870 | =cut | ||
| 871 | |||
| 872 | sub print_rule { | ||
| 873 | my $self = shift; | ||
| 874 | my $print_rule = 1; | ||
| 875 | |||
| 876 | my $select_index = $self->check_select('select'); | ||
| 877 | my $omit_index = $self->check_select('omit'); | ||
| 878 | |||
| 879 | if ( !$select_index ) { | ||
| 880 | $self->app_log->info( | ||
| 881 | 'Select rules in place. Skipping rule ' . $self->rule_name ); | ||
| 882 | $print_rule = 0; | ||
| 883 | } | ||
| 884 | |||
| 885 | if ($omit_index) { | ||
| 886 | $self->app_log->info( | ||
| 887 | 'Omit rules in place. Skipping rule ' . $self->rule_name ); | ||
| 888 | $print_rule = 0; | ||
| 889 | } | ||
| 890 | |||
| 891 | $self->app_log->info( 'Processing rule ' . $self->rule_name . "\n" ) | ||
| 892 | if $print_rule; | ||
| 893 | |||
| 894 | return $print_rule; | ||
| 895 | } | ||
| 896 | |||
| 897 | ##This is not necessary without the use_timestamps | ||
| 898 | ##But I will leave it in as a placeholder | ||
| 899 | sub print_within_rule { | ||
| 900 | my $self = shift; | ||
| 901 | |||
| 902 | #TODO May not need this without use_timestamps | ||
| 903 | my $select_index = $self->check_select('select'); | ||
| 904 | |||
| 905 | return 1; | ||
| 906 | } | ||
| 907 | |||
| 908 | =head3 check_indir_outdir | ||
| 909 | |||
| 910 | If by_sample_outdir we pop the last dirname, append {$sample} to the base dir, and then add back on the popped value | ||
| 911 | |||
| 912 | There are 2 cases we do not do this | ||
| 913 | |||
| 914 | 1. The indir of the first rule | ||
| 915 | 2. If the user specifies indir/outdir in the local vars | ||
| 916 | |||
| 917 | =cut | ||
| 918 | |||
| 919 | sub check_indir_outdir { | ||
| 920 | my $self = shift; | ||
| 921 | my $attr = shift; | ||
| 922 | |||
| 923 | # $DB::single = 2; | ||
| 924 | return unless $attr->by_sample_outdir; | ||
| 925 | return unless $self->has_sample; | ||
| 926 | return if $attr->override_process; | ||
| 927 | |||
| 928 | # If indir/outdir is specified in the local config | ||
| 929 | # then we don't evaluate it | ||
| 930 | my %keys = (); | ||
| 931 | map { $keys{$_} = 1 } @{ $self->local_rule_keys }; | ||
| 932 | |||
| 933 | foreach my $dir ( ( 'indir', 'outdir' ) ) { | ||
| 934 | if ( exists $keys{$dir} ) { | ||
| 935 | next; | ||
| 936 | } | ||
| 937 | |||
| 938 | if ( $dir eq 'indir' && !$self->has_p_rule_name ) { | ||
| 939 | my $new_dir = File::Spec->catdir( $attr->$dir, '{$sample}' ); | ||
| 940 | $attr->$dir($new_dir); | ||
| 941 | next; | ||
| 942 | } | ||
| 943 | |||
| 944 | my @dirs = File::Spec->splitdir( $attr->$dir ); | ||
| 945 | my $last = ''; | ||
| 946 | if ($#dirs) { | ||
| 947 | $last = pop(@dirs); | ||
| 948 | } | ||
| 949 | |||
| 950 | my $base_dir = File::Spec->catdir(@dirs); | ||
| 951 | my $new_dir = File::Spec->catdir( $base_dir, '{$sample}', $last ); | ||
| 952 | $attr->$dir($new_dir); | ||
| 953 | } | ||
| 954 | |||
| 955 | } | ||
| 956 | |||
| 957 | =head3 carry_directives | ||
| 958 | |||
| 959 | At the beginning of each rule the previous outdir should be the new indir, and the previous OUTPUT should be the new INPUT | ||
| 960 | |||
| 961 | Stash should be carried over | ||
| 962 | |||
| 963 | Outdir should be global_attr->outdir/rule_name | ||
| 964 | |||
| 965 | =cut | ||
| 966 | |||
| 967 | sub carry_directives { | ||
| 968 | my $self = shift; | ||
| 969 | |||
| 970 | # $DB::single = 2; | ||
| 971 | $self->local_attr->outdir( | ||
| 972 | $self->global_attr->outdir . '/' . $self->rule_name ); | ||
| 973 | |||
| 974 | return unless $self->has_p_rule_name; | ||
| 975 | |||
| 976 | $self->local_attr->indir( dclone( $self->p_local_attr->outdir ) ); | ||
| 977 | |||
| 978 | if ( $self->p_local_attr->has_OUTPUT ) { | ||
| 979 | if ( ref( $self->p_local_attr->OUTPUT ) ) { | ||
| 980 | $self->local_attr->INPUT( dclone( $self->p_local_attr->OUTPUT ) ); | ||
| 981 | } | ||
| 982 | else { | ||
| 983 | $self->local_attr->INPUT( $self->p_local_attr->OUTPUT ); | ||
| 984 | } | ||
| 985 | } | ||
| 986 | |||
| 987 | $self->local_attr->stash( dclone( $self->p_local_attr->stash ) ); | ||
| 988 | } | ||
| 989 | |||
| 990 | sub sanity_check_fail { | ||
| 991 | my $self = shift; | ||
| 992 | |||
| 993 | my $rule_example = <<EOF; | ||
| 994 | global: | ||
| 995 | - indir: data/raw | ||
| 996 | - outdir: data/processed | ||
| 997 | - sample_rule: (sample.*)\$ | ||
| 998 | - by_sample_outdir: 1 | ||
| 999 | - find_sample_bydir: 1 | ||
| 1000 | - copy1: | ||
| 1001 | local: | ||
| 1002 | - indir: '{\$self->my_dir}' | ||
| 1003 | - INPUT: '{\$self->indir}/{\$sample}.csv' | ||
| 1004 | - HPC: | ||
| 1005 | - mem: '40GB' | ||
| 1006 | - walltime: '40GB' | ||
| 1007 | process: | | ||
| 1008 | echo 'MyDir on {\$self->my_dir}' | ||
| 1009 | echo 'Indir on {\$self->indir}' | ||
| 1010 | echo 'Outdir on {\$self->outdir}' | ||
| 1011 | echo 'INPUT on {\$self->INPUT}' | ||
| 1012 | EOF | ||
| 1013 | $self->app_log->fatal('Skipping this rule.'); | ||
| 1014 | $self->app_log->fatal( | ||
| 1015 | 'Here is an example workflow. For more information please see biox new --help.' | ||
| 1016 | ); | ||
| 1017 | $self->app_log->fatal($rule_example); | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | sub print_process_workflow { | ||
| 1021 | my $self = shift; | ||
| 1022 | |||
| 1023 | $self->app_log->info('Post processing rules and printing workflow...'); | ||
| 1024 | foreach my $rule ( $self->all_rule_names ) { | ||
| 1025 | |||
| 1026 | #TODO This should be named select_rule_names | ||
| 1027 | my $index = $self->first_index_select_rule_keys( sub { $_ eq $rule } ); | ||
| 1028 | next if $index == -1; | ||
| 1029 | |||
| 1030 | my $meta = $self->process_obj->{$rule}->{meta} || []; | ||
| 1031 | my $text = $self->process_obj->{$rule}->{text} || []; | ||
| 1032 | |||
| 1033 | map { $self->fh->say($_) } @{$meta}; | ||
| 1034 | $self->fh->say(''); | ||
| 1035 | map { $self->fh->say($_); $self->fh->say('') } @{$text}; | ||
| 1036 | |||
| 1037 | $self->print_stats_rules($rule); | ||
| 1038 | |||
| 1039 | } | ||
| 1040 | } | ||
| 1041 | |||
| 1042 | |||
| 1043 | no Moose::Role; | ||
| 1044 | |||
| 1045 | 1; | ||