| 1 | package BioX::Workflow::Command::inspect; | ||
| 2 | |||
| 3 | 1 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use MooseX::App::Command; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use YAML; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Storable qw(dclone); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Try::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | extends 'BioX::Workflow::Command'; | ||
| 14 | 1 | ▶ | use BioSAILs::Utils::Traits qw(ArrayRefOfStrs); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | ▶ | use Capture::Tiny ':all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | with 'BioX::Workflow::Command::run::Rules::Directives::Walk'; | ||
| 18 | with 'BioX::Workflow::Command::run::Utils::Samples'; | ||
| 19 | with 'BioX::Workflow::Command::run::Utils::Attributes'; | ||
| 20 | with 'BioX::Workflow::Command::run::Rules::Rules'; | ||
| 21 | with 'BioX::Workflow::Command::run::Utils::WriteMeta'; | ||
| 22 | with 'BioX::Workflow::Command::run::Utils::Files::TrackChanges'; | ||
| 23 | with 'BioX::Workflow::Command::run::Utils::Files::ResolveDeps'; | ||
| 24 | with 'BioX::Workflow::Command::Utils::Files'; | ||
| 25 | |||
| 26 | 0 | ▶ | use BioX::Workflow::Command::run; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 27 | use BioX::Workflow::Command::inspect::Exceptions::Path; | ||
| 28 | |||
| 29 | command_short_description 'Inspect your workflow'; | ||
| 30 | command_long_description | ||
| 31 | 'Inspect individual variables in your workflow. Syntax is global.var for global, or rule.rulename.var for rules. Use the --all flag to inspect all variables.'; | ||
| 32 | |||
| 33 | =head1 BioX::Workflow::Command::inspect | ||
| 34 | |||
| 35 | biox inspect -h | ||
| 36 | biox inspect -w variant_calling.yml --path /rules/.*/local/indir | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | =head2 Attributes | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | option 'all' => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => 'Bool', | ||
| 47 | default => 0, | ||
| 48 | ); | ||
| 49 | |||
| 50 | option 'step_key' => ( | ||
| 51 | is => 'rw', | ||
| 52 | isa => 'Bool', | ||
| 53 | default => 0, | ||
| 54 | documentation => | ||
| 55 | 'Type any key to continue to next rule key. Type \'q\' or \'quit\' to quit.', | ||
| 56 | ); | ||
| 57 | |||
| 58 | option 'step_rule' => ( | ||
| 59 | is => 'rw', | ||
| 60 | isa => 'Bool', | ||
| 61 | default => 0, | ||
| 62 | documentation => | ||
| 63 | 'Type any key to continue to next rule. Type \'q\' or \'quit\' to quit.', | ||
| 64 | ); | ||
| 65 | |||
| 66 | option 'path' => ( | ||
| 67 | is => 'rw', | ||
| 68 | isa => 'Str', | ||
| 69 | required => 0, | ||
| 70 | predicate => 'has_path', | ||
| 71 | ); | ||
| 72 | |||
| 73 | option 'json' => ( | ||
| 74 | is => 'rw', | ||
| 75 | isa => 'Bool', | ||
| 76 | default => 0, | ||
| 77 | ); | ||
| 78 | |||
| 79 | option 'show_only_errors' => ( | ||
| 80 | is => 'rw', | ||
| 81 | isa => 'Bool', | ||
| 82 | default => 0, | ||
| 83 | ); | ||
| 84 | |||
| 85 | =head3 samples | ||
| 86 | |||
| 87 | This is our actual list of samples | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | =head2 Subroutines | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub execute { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | if ( !$self->load_yaml_workflow ) { | ||
| 99 | $self->app_log->warn('Exiting now.'); | ||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | $DB::single = 2; | ||
| 104 | if ( $self->json ) { | ||
| 105 | capture_stderr { | ||
| 106 | $self->inspect; | ||
| 107 | }; | ||
| 108 | |||
| 109 | } | ||
| 110 | else { | ||
| 111 | $self->inspect; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | sub inspect { | ||
| 116 | my $self = shift; | ||
| 117 | $self->apply_global_attributes; | ||
| 118 | $self->global_attr->create_outdir(0); | ||
| 119 | |||
| 120 | $self->return_global_as_object; | ||
| 121 | |||
| 122 | $self->get_samples; | ||
| 123 | $self->samples( ['Sample_XYZ'] ); | ||
| 124 | $self->dummy_sample('Sample_XYZ'); | ||
| 125 | $self->iterate_rules; | ||
| 126 | |||
| 127 | $self->check_for_json; | ||
| 128 | |||
| 129 | } | ||
| 130 | |||
| 131 | sub check_for_json { | ||
| 132 | my $self = shift; | ||
| 133 | ##TODO These should be too different interfaces | ||
| 134 | if ( $self->json ) { | ||
| 135 | $self->app_log->warn( | ||
| 136 | 'You have selected a path, but this is not applied with --json') | ||
| 137 | if $self->has_path; | ||
| 138 | my $json = | ||
| 139 | JSON->new->utf8->pretty->allow_blessed->encode( $self->inspect_obj ); | ||
| 140 | print $json; | ||
| 141 | } | ||
| 142 | else { | ||
| 143 | $self->comment_char(''); | ||
| 144 | $self->find_inspect_obj; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | sub find_inspect_obj { | ||
| 149 | my $self = shift; | ||
| 150 | |||
| 151 | $self->find_inspect_obj_path if $self->has_path; | ||
| 152 | |||
| 153 | if ( $self->all ) { | ||
| 154 | $self->find_inspect_obj_rules( [ '', '.*', '.*', '.*', '.*' ] ); | ||
| 155 | $self->find_inspect_obj_global( [ '', '.*', '.*', '.*' ] ); | ||
| 156 | } | ||
| 157 | |||
| 158 | $self->find_inspect_obj_select if $self->select_effect; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub find_inspect_obj_select { | ||
| 162 | my $self = shift; | ||
| 163 | |||
| 164 | return unless $self->has_select_rule_keys; | ||
| 165 | |||
| 166 | foreach my $rule ( @{ $self->select_rules } ) { | ||
| 167 | $self->find_inspect_obj_rules( [ '', '.*', $rule, '.*', '.*' ] ); | ||
| 168 | } | ||
| 169 | |||
| 170 | } | ||
| 171 | |||
| 172 | sub find_inspect_obj_path { | ||
| 173 | my $self = shift; | ||
| 174 | |||
| 175 | my @split = split( '/', $self->path ); | ||
| 176 | if ( scalar @split >= 6 ) { | ||
| 177 | my $except = | ||
| 178 | BioX::Workflow::Command::inspect::Exceptions::Path->new( | ||
| 179 | info => 'Your split path contains too many elements.' | ||
| 180 | . ' Portions may still work, but you are probably not getting what you expect.' | ||
| 181 | ); | ||
| 182 | $except->warn( $self->app_log ); | ||
| 183 | } | ||
| 184 | if ( $split[1] eq 'rules' ) { | ||
| 185 | $self->find_inspect_obj_rules( \@split ); | ||
| 186 | } | ||
| 187 | elsif ( $split[1] eq 'global' ) { | ||
| 188 | $self->find_inspect_obj_global( \@split ); | ||
| 189 | } | ||
| 190 | elsif ( $split[1] eq '*' ) { | ||
| 191 | $self->find_inspect_obj_rules( \@split ); | ||
| 192 | $self->find_inspect_obj_global( \@split ); | ||
| 193 | } | ||
| 194 | else { | ||
| 195 | my $except = | ||
| 196 | BioX::Workflow::Command::inspect::Exceptions::Path->new( | ||
| 197 | info => 'You are searching for something that does not exist.' | ||
| 198 | . ' Please see the documentation for allowed values of dpath.' ); | ||
| 199 | $except->fatal( $self->app_log ); | ||
| 200 | } | ||
| 201 | |||
| 202 | } | ||
| 203 | |||
| 204 | sub find_inspect_obj_rules { | ||
| 205 | my $self = shift; | ||
| 206 | my $split = shift; | ||
| 207 | |||
| 208 | ##TODO Apply select_rules | ||
| 209 | foreach my $rule ( @{ $self->rule_names } ) { | ||
| 210 | my $rule_name = $split->[2]; | ||
| 211 | if ( !$rule_name ) { | ||
| 212 | my $except = | ||
| 213 | BioX::Workflow::Command::inspect::Exceptions::Path->new( | ||
| 214 | info => 'You must supply a rule name ' | ||
| 215 | . $rule | ||
| 216 | . ' Examples: --path /rules/.*/local/.* or --path /rules/some_rule/local/.* or --path /rules/.*/process' | ||
| 217 | ); | ||
| 218 | $except->fatal( $self->app_log ); | ||
| 219 | } | ||
| 220 | if ( $rule =~ m/$rule_name/ ) { | ||
| 221 | print "Rule: $rule\n"; | ||
| 222 | my $wanted_key = $split->[4]; | ||
| 223 | if ( !$wanted_key ) { | ||
| 224 | my $except = | ||
| 225 | BioX::Workflow::Command::inspect::Exceptions::Path->new( | ||
| 226 | info => 'You must supply a key to rule ' | ||
| 227 | . $rule | ||
| 228 | . ' Examples: --path /rules/.*/local/.*, --path /rules/.*/process' | ||
| 229 | ); | ||
| 230 | $except->fatal( $self->app_log ); | ||
| 231 | } | ||
| 232 | elsif ( !$split->[3] ) { | ||
| 233 | my $except = | ||
| 234 | BioX::Workflow::Command::inspect::Exceptions::Path->new( | ||
| 235 | info => 'You must supply a local/process and key to rule ' | ||
| 236 | . $rule | ||
| 237 | . ' Examples: --path /rules/.*/local/.*, --path /rules/.*/process' | ||
| 238 | ); | ||
| 239 | $except->fatal( $self->app_log ); | ||
| 240 | } | ||
| 241 | elsif ( $split->[3] eq 'local' ) { | ||
| 242 | $self->find_inspect_obj_rule_keys( $rule, $wanted_key ); | ||
| 243 | } | ||
| 244 | elsif ( $split->[3] eq 'process' ) { | ||
| 245 | $self->find_inspect_obj_rule_process($rule); | ||
| 246 | } | ||
| 247 | elsif ( $split->[3] eq '.*' ) { | ||
| 248 | $self->find_inspect_obj_rule_keys( $rule, $wanted_key ); | ||
| 249 | $self->find_inspect_obj_rule_process($rule); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | &promptUser("Next rule") if $self->step_rule; | ||
| 253 | print "\n"; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | sub find_inspect_obj_rule_keys { | ||
| 258 | my $self = shift; | ||
| 259 | my $rule = shift; | ||
| 260 | my $wanted_key = shift; | ||
| 261 | my $seen = 0; | ||
| 262 | |||
| 263 | my @keys = keys %{ $self->inspect_obj->{rules}->{$rule}->{local} }; | ||
| 264 | foreach my $key (@keys) { | ||
| 265 | $seen = 1; | ||
| 266 | if ( $key =~ m/$wanted_key/ ) { | ||
| 267 | my $value = | ||
| 268 | $self->inspect_obj->{rules}->{$rule}->{local}->{$key}; | ||
| 269 | my $pp = $self->write_pretty_meta( $key, $value ); | ||
| 270 | print "Key:" . $pp . "\n"; | ||
| 271 | |||
| 272 | &promptUser("Next key") if $self->step_key; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | $self->app_log->warn( 'We were not able to find key ' . $wanted_key ) | ||
| 276 | unless $seen; | ||
| 277 | } | ||
| 278 | |||
| 279 | sub find_inspect_obj_rule_process { | ||
| 280 | my $self = shift; | ||
| 281 | my $rule = shift; | ||
| 282 | |||
| 283 | my $texts = $self->process_obj->{$rule}->{text}; | ||
| 284 | return unless $texts; | ||
| 285 | print "Process:\t" . $texts->[0] . "\n"; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub find_inspect_obj_global { | ||
| 289 | my $self = shift; | ||
| 290 | my $split = shift; | ||
| 291 | |||
| 292 | my $wanted_key = $split->[2]; | ||
| 293 | print "Global\n"; | ||
| 294 | my $seen = 0; | ||
| 295 | |||
| 296 | my @keys = keys %{ $self->inspect_obj->{global} }; | ||
| 297 | foreach my $key (@keys) { | ||
| 298 | if ( $key =~ m/$wanted_key/ ) { | ||
| 299 | $seen = 1; | ||
| 300 | my $value = $self->inspect_obj->{global}->{$key}; | ||
| 301 | my $pp = $self->write_pretty_meta( $key, $value ); | ||
| 302 | print "Key:" . $pp . "\n"; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | $self->app_log->warn( 'We were not able to find key ' . $wanted_key ) | ||
| 307 | unless $seen; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub promptUser { | ||
| 311 | my $promptString = shift; | ||
| 312 | my $defaultValue = shift; | ||
| 313 | |||
| 314 | if ($defaultValue) { | ||
| 315 | print $promptString, "[", $defaultValue, "]: "; | ||
| 316 | } | ||
| 317 | else { | ||
| 318 | $defaultValue = ""; | ||
| 319 | print $promptString, ": "; | ||
| 320 | } | ||
| 321 | |||
| 322 | $| = 1; # force a flush after our print | ||
| 323 | $_ = <STDIN>; # get the input from STDIN (presumably the keyboard) | ||
| 324 | |||
| 325 | chomp; | ||
| 326 | |||
| 327 | my $retvalue; | ||
| 328 | if ("$defaultValue") { | ||
| 329 | $retvalue = $_ ? $_ : $defaultValue; # return $_ if it has a value | ||
| 330 | } | ||
| 331 | else { | ||
| 332 | $retvalue = $_; | ||
| 333 | } | ||
| 334 | |||
| 335 | if ( $retvalue eq 'q' || $retvalue eq 'quit' ) { | ||
| 336 | exit 0; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | __PACKAGE__->meta->make_immutable; | ||
| 341 | |||
| 342 | 1; | ||