|
| 1 | +package BioX::Workflow::Command::run::Rules::Directives; |
| 2 | + |
| 3 | +use Moose; |
| 4 | +use namespace::autoclean; |
| 5 | +use Moose::Util qw/apply_all_roles/; |
| 6 | + |
| 7 | +with 'BioX::Workflow::Command::run::Rules::Directives::Types::HPC'; |
| 8 | +with 'BioX::Workflow::Command::run::Rules::Directives::Types::Path'; |
| 9 | +with 'BioX::Workflow::Command::run::Rules::Directives::Types::List'; |
| 10 | +with 'BioX::Workflow::Command::run::Rules::Directives::Types::Stash'; |
| 11 | +with 'BioX::Workflow::Command::run::Rules::Directives::Types::Hash'; |
| 12 | +with 'BioX::Workflow::Command::run::Rules::Directives::Types::Array'; |
| 13 | +with 'BioX::Workflow::Command::run::Rules::Directives::Types::CSV'; |
| 14 | +with 'BioX::Workflow::Command::run::Rules::Directives::Types::Config'; |
| 15 | +with 'BioX::Workflow::Command::run::Rules::Directives::Interpolate'; |
| 16 | +with 'BioX::Workflow::Command::run::Rules::Directives::Sample'; |
| 17 | +with 'BioX::Workflow::Command::run::Rules::Directives::Walk'; |
| 18 | +with 'BioX::Workflow::Command::Utils::Log'; |
| 19 | + |
| 20 | +use Try::Tiny; |
| 21 | + |
| 22 | +=head2 Other Directives |
| 23 | +
|
| 24 | +=cut |
| 25 | + |
| 26 | +has 'register_types' => ( |
| 27 | + traits => ['Hash'], |
| 28 | + is => 'rw', |
| 29 | + isa => 'HashRef', |
| 30 | + default => sub { return {} }, |
| 31 | + handles => { |
| 32 | + 'get_register_types' => 'get', |
| 33 | + 'set_register_types' => 'set', |
| 34 | + }, |
| 35 | +); |
| 36 | + |
| 37 | +has 'register_process_directives' => ( |
| 38 | + traits => ['Hash'], |
| 39 | + is => 'rw', |
| 40 | + isa => 'HashRef', |
| 41 | + default => sub { return {} }, |
| 42 | + handles => { |
| 43 | + 'get_register_process_directives' => 'get', |
| 44 | + 'set_register_process_directives' => 'set', |
| 45 | + }, |
| 46 | +); |
| 47 | + |
| 48 | +##TODO add this so that we can have user defined types |
| 49 | +has 'register_namespace' => ( |
| 50 | + traits => ['Array'], |
| 51 | + is => 'rw', |
| 52 | + isa => 'ArrayRef', |
| 53 | + default => sub { [] }, |
| 54 | + handles => { |
| 55 | + 'all_register_namespace' => 'elements', |
| 56 | + 'count_register_namespace' => 'count', |
| 57 | + 'has_register_namespace' => 'count', |
| 58 | + 'has_no_register_namespace' => 'is_empty', |
| 59 | + }, |
| 60 | + trigger => sub { |
| 61 | + my $self = shift; |
| 62 | + foreach my $role ( $self->all_register_namespace ) { |
| 63 | + try { |
| 64 | + apply_all_roles( $self, $role ); |
| 65 | + } |
| 66 | + catch { |
| 67 | + $self->app_log->warn( |
| 68 | + 'There was an error registering role ' . $role ); |
| 69 | + $self->app_log->warn("$_\n"); |
| 70 | + } |
| 71 | + } |
| 72 | + }, |
| 73 | +); |
| 74 | + |
| 75 | +has 'override_process' => ( |
| 76 | + traits => ['Bool'], |
| 77 | + is => 'rw', |
| 78 | + isa => 'Bool', |
| 79 | + default => 0, |
| 80 | + predicate => 'has_override_process', |
| 81 | + documentation => |
| 82 | + q(Instead of for my $sample (@sample){ DO STUFF } just DOSTUFF), |
| 83 | + handles => { |
| 84 | + set_override_process => 'set', |
| 85 | + clear_override_process => 'unset', |
| 86 | + }, |
| 87 | +); |
| 88 | + |
| 89 | +##Add in support for chunks |
| 90 | +##This is useful for features where we want to do things like |
| 91 | +##split a file into parts |
| 92 | +##count by kmers, etc |
| 93 | + |
| 94 | +=head3 create_attr |
| 95 | +
|
| 96 | +Add attributes to $self-> namespace |
| 97 | +
|
| 98 | +=cut |
| 99 | + |
| 100 | +sub create_attr { |
| 101 | + my $self = shift; |
| 102 | + my $data = shift; |
| 103 | + |
| 104 | + my $meta = __PACKAGE__->meta; |
| 105 | + |
| 106 | + $meta->make_mutable; |
| 107 | + my $seen = {}; |
| 108 | + |
| 109 | + for my $attr ( $meta->get_all_attributes ) { |
| 110 | + next if $attr->name eq 'stash'; |
| 111 | + $seen->{ $attr->name } = 1; |
| 112 | + } |
| 113 | + |
| 114 | + # Get workflow_data structure |
| 115 | + # Workflow is an array of hashes |
| 116 | + |
| 117 | + foreach my $href ( @{$data} ) { |
| 118 | + |
| 119 | + if ( !ref($href) eq 'HASH' ) { |
| 120 | + ##TODO add more informative structure options here |
| 121 | + ##TODO Add app_log |
| 122 | + warn 'Your variable declarations should be key/value!'; |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + while ( my ( $k, $v ) = each( %{$href} ) ) { |
| 127 | + |
| 128 | + if ( !exists $seen->{$k} ) { |
| 129 | + |
| 130 | + if ( $k eq 'stash' ) { |
| 131 | + $self->merge_stash($v); |
| 132 | + } |
| 133 | + elsif ( $self->can($k) ) { |
| 134 | + ##Should this be next of just skip ? |
| 135 | + next; |
| 136 | + } |
| 137 | + elsif ( $self->search_registered_types( $meta, $k, $v ) ) { |
| 138 | + |
| 139 | + # next; |
| 140 | + } |
| 141 | + elsif ( ref($v) eq 'HASH' ) { |
| 142 | + $self->create_HASH_attr( $meta, $k ); |
| 143 | + } |
| 144 | + elsif ( ref($v) eq 'ARRAY' ) { |
| 145 | + $self->create_ARRAY_attr( $meta, $k ); |
| 146 | + } |
| 147 | + else { |
| 148 | + $self->create_reg_attr( $meta, $k ); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + try{ |
| 153 | + $self->$k($v) if defined $v; |
| 154 | + } |
| 155 | + catch{ |
| 156 | + $self->app_log->warn( |
| 157 | + 'There was an assiging key. ' . $k ); |
| 158 | + $self->app_log->warn("$_\n"); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + $meta->make_immutable; |
| 165 | +} |
| 166 | + |
| 167 | +sub search_registered_types { |
| 168 | + my $self = shift; |
| 169 | + my $meta = shift; |
| 170 | + my $k = shift; |
| 171 | + my $v = shift; |
| 172 | + |
| 173 | + foreach my $key ( keys %{ $self->register_types } ) { |
| 174 | + next unless exists $self->register_types->{$key}->{lookup}; |
| 175 | + next unless exists $self->register_types->{$key}->{builder}; |
| 176 | + my $lookup_ref = $self->register_types->{$key}->{lookup}; |
| 177 | + my $builder = $self->register_types->{$key}->{builder}; |
| 178 | + |
| 179 | + foreach my $lookup ( @{$lookup_ref} ) { |
| 180 | + if ( $k =~ m/$lookup/ ) { |
| 181 | + $self->$builder( $meta, $k, $v ); |
| 182 | + return 1; |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return 0; |
| 188 | +} |
| 189 | + |
| 190 | +sub create_reg_attr { |
| 191 | + my $self = shift; |
| 192 | + my $meta = shift; |
| 193 | + my $k = shift; |
| 194 | + |
| 195 | + $meta->add_attribute( |
| 196 | + $k => ( |
| 197 | + is => 'rw', |
| 198 | + lazy_build => 1, |
| 199 | + ) |
| 200 | + ); |
| 201 | +} |
| 202 | + |
| 203 | +=head3 create_blank_attr |
| 204 | +
|
| 205 | +placeholder for some types |
| 206 | +
|
| 207 | +=cut |
| 208 | + |
| 209 | +sub create_blank_attr { |
| 210 | + my $self = shift; |
| 211 | + my $meta = shift; |
| 212 | + my $k = shift; |
| 213 | + |
| 214 | + $meta->add_attribute( |
| 215 | + $k => ( |
| 216 | + is => 'rw', |
| 217 | + default => '', |
| 218 | + ) |
| 219 | + ); |
| 220 | +} |
| 221 | + |
| 222 | +sub BUILD { } |
| 223 | + |
| 224 | +after 'BUILD' => sub { |
| 225 | + my $self = shift; |
| 226 | + $self->interpol_directive_cache( {} ); |
| 227 | +}; |
| 228 | + |
| 229 | +no Moose; |
| 230 | +__PACKAGE__->meta->make_immutable; |
| 231 | + |
| 232 | +1; |
0 commit comments