File Coverage

blib/lib/BioX/Workflow/Command/run/Rules/Directives.pm
Criterion Covered Total %
statement 12 63 19.0
branch 0 24 0.0
condition n/a
subroutine 4 10 40.0
pod n/a
total 16 97 16.4


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