blib/lib/BioX/Workflow/Command/run/Rules/Directives.pm

filter: Statementstmt 15.3% 12 / 78 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 33.3% 4 / 12 Podpod n/a totaltotal 11.2% 16 / 142 CC 28 SCAR 48.3
CC
28
Cov
10%
search_registered_types
210.0
create_attr
156.0
check_type
12.0
CRAP 124.9
↑ Summary blib/lib/BioX/Workflow/Command/Exceptions.pm »
1 package BioX::Workflow::Command::run::Rules::Directives;
2  
3 1 ▶use Moose;
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 Moose::Util qw/apply_all_roles/;
Subroutine BEGIN: called CC=1 SCAR=0.0
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::Functions';
20 with 'BioX::Workflow::Command::run::Rules::Directives::Sample';
21 with 'BioX::Workflow::Command::run::Rules::Directives::Walk';
22 with 'BioX::Workflow::Command::Utils::Log';
23  
24 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =head2 Other Directives
27  
28 =cut
29  
30 has '_ERROR' => (
31 is => 'rw',
32 default => 0,
33 );
34  
35 has 'register_types' => (
36 traits => ['Hash'],
37 is => 'rw',
38 isa => 'HashRef',
39 default => sub { return {} },
40 handles => {
41 'get_register_types' => 'get',
42 'set_register_types' => 'set',
43 },
44 );
45  
46 has 'register_process_directives' => (
47 traits => ['Hash'],
48 is => 'rw',
49 isa => 'HashRef',
50 default => sub { return {} },
51 handles => {
52 'get_register_process_directives' => 'get',
53 'set_register_process_directives' => 'set',
54 },
55 );
56  
57 =head3 register_namespace
58  
59 A user can define their own custom types and processes.
60  
61 =cut
62  
63 has 'register_namespace' => (
64 traits => ['Array'],
65 is => 'rw',
66 isa => 'ArrayRef',
67 default => sub { [] },
68 handles => {
69 'all_register_namespace' => 'elements',
70 'count_register_namespace' => 'count',
71 'has_register_namespace' => 'count',
72 'has_no_register_namespace' => 'is_empty',
73 },
74 trigger => sub {
75 my $self = shift;
76 foreach my $role ( $self->all_register_namespace ) {
77 try {
78 apply_all_roles( $self, $role );
79 }
80 catch {
81 $self->app_log->warn(
82 'There was an error registering role ' . $role );
83 $self->app_log->warn( $@ . "\n" );
84 };
85 }
86 },
87 );
88  
89 has 'template_type' => (
90 is => 'rw',
91 isa => 'Str',
92 default => 'Text',
93 documentation => 'BioX supports two templating engines out of the box -'
94 . ' Text::Template and Template::Mustache. Default is Text.'
95 . 'You can register another type by registering the namespace of your templating engine.'
96 . 'It is not recommended to mix and match templating engines.'
97 );
98  
99 has 'override_process' => (
100 traits => ['Bool'],
101 is => 'rw',
102 isa => 'Bool',
103 default => 0,
104 predicate => 'has_override_process',
105 documentation =>
106 q(Instead of for my $sample (@sample){ DO STUFF } just DOSTUFF),
107 handles => {
108 set_override_process => 'set',
109 clear_override_process => 'unset',
110 },
111 );
112  
113 has 'run_stats' => (
114 is => 'rw',
115 isa => 'Bool',
116 default => 0,
117 );
118  
119 ##Add in support for chunks
120 ##This is useful for features where we want to do things like
121 ##split a file into parts
122 ##count by kmers, etc
123  
124 =head3 create_attr
125  
126 Add attributes to $self-> namespace
127  
128 =cut
129  
130 sub create_attr {
131 0 ▶ my $self = shift;
Subroutine create_attr: not called CC=12 SCAR=50.5
132 0 my $data = shift;
133  
134 0 my $meta = __PACKAGE__->meta;
135  
136 0 $meta->make_mutable;
137 0 my $seen = {};
138  
139 0 for my $attr ( $meta->get_all_attributes ) {
140 0 ▶ next if $attr->name eq 'stash';
Branch: 1 branch
BranchTrueFalseTotal
if $attr->name eq "stash" 0 0 0
141 0 $seen->{ $attr->name } = 1;
142 }
143  
144 # Get workflow_data structure
145 # Workflow is an array of hashes
146  
147 0 foreach my $href ( @{$data} ) {
148  
149 0 ▶ if ( !ref($href) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ref($href) eq "HASH") 0 0 0
150 ##TODO add more informative structure options here
151 ##TODO Add app_log
152 0 warn 'Your variable declarations should be key/value!';
153 0 return;
154 }
155  
156 0 ▶ while ( my ( $k, $v ) = each( %{$href} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$href;}) 0 0 0
157  
158 0 ▶ if ( !exists $seen->{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $seen->{$k}) 0 0 0
159  
160 0 ▶ if ( $k eq 'stash' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($k eq 'stash') { } 0 0 0
elsif ($self->can($k)) { } 0 0 0
elsif ($self->search_registered_types($meta, $k, $v)) { } 0 0 0
elsif (ref $v eq 'HASH') { } 0 0 0
elsif (ref $v eq 'ARRAY') { } 0 0 0
161 0 $self->merge_stash($v);
162 }
163 elsif ( $self->can($k) ) {
164 0 next;
165 }
166 elsif ( $self->search_registered_types( $meta, $k, $v ) ) {
167  
168 # next;
169 }
170 elsif ( ref($v) eq 'HASH' ) {
171 0 $self->create_HASH_attr( $meta, $k );
172 }
173 elsif ( ref($v) eq 'ARRAY' ) {
174 0 $self->create_ARRAY_attr( $meta, $k );
175 }
176 else {
177 0 $self->create_reg_attr( $meta, $k );
178 }
179 }
180  
181 try {
182 0 ▶ $self->$k($v) if defined $v;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
183 }
184 catch {
185 0 ▶ $self->app_log->warn( 'There was an assiging key. ' . $k );
Subroutine __ANON__: not called CC=1 SCAR=6.9
186 0 $self->app_log->warn("$_\n");
187 }
188 0 }
189  
190 }
191  
192 0 $meta->make_immutable;
193 }
194  
195 sub create_reg_attr {
196 0 ▶ my $self = shift;
Subroutine create_reg_attr: not called CC=1 SCAR=6.9
197 0 my $meta = shift;
198 0 my $k = shift;
199  
200 0 $meta->add_attribute(
201 $k => (
202 is => 'rw',
203 lazy_build => 1,
204 )
205 );
206 }
207  
208 =head3 create_blank_attr
209  
210 placeholder for some types
211  
212 =cut
213  
214 sub create_blank_attr {
215 0 ▶ my $self = shift;
Subroutine create_blank_attr: not called CC=1 SCAR=6.9
216 0 my $meta = shift;
217 0 my $k = shift;
218  
219 0 $meta->add_attribute(
220 $k => (
221 is => 'rw',
222 default => '',
223 )
224 );
225 }
226  
227 =head3 search_registered_types
228  
229 A user can register custom types through the plugin system of in the workflow with 'register_namespace'.
230  
231 Namespaces must be Moose Roles.
232  
233 See BioX::Workflow::Command::run::Rules::Types::CSV for more information
234  
235 =cut
236  
237 ##TODO Also lookup by type key
238  
239 sub search_registered_types {
240 0 ▶ my $self = shift;
Subroutine search_registered_types: not called CC=14 SCAR=53.5
241 0 my $meta = shift;
242 0 my $k = shift;
243 0 my $v = shift;
244  
245 0 foreach my $key ( keys %{ $self->register_types } ) {
246  
247 0 ▶ next unless exists $self->register_types->{$key}->{lookup};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->register_types->{$key}{"lookup"} 0 0 0
248 0 ▶ next unless exists $self->register_types->{$key}->{builder};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->register_types->{$key}{"builder"} 0 0 0
249 0 my $lookup_ref = $self->register_types->{$key}->{lookup};
250 0 my $builder = $self->register_types->{$key}->{builder};
251  
252 ## We look for keys one of two ways - either they have a pattern match or a declared type
253 0 foreach my $lookup ( @{$lookup_ref} ) {
254 0 ▶ if ( $k =~ m/$lookup/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /$lookup/) 0 0 0
255 0 $self->$builder( $meta, $k, $v );
256 0 return 1;
257 }
258 }
259  
260 ##TODO This is kind of hacky....
261 0 ▶ if ( ref($v) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $v eq 'HASH') { } 0 0 0
elsif (ref $v eq 'ARRAY') { } 0 0 0
262 0 ▶ if ( exists $v->{type} && !ref( $v->{type} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $v->{"type"} and not ref $v->{"type"}) 0 0 0
Condition0%0/3
exists $v->{"type"} and not ref $v->{"type"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $v->{"type"} and not ref $v->{"type"}
exists $v->{"type"} not ref $v->{"type"}
Truth table0%0/3
A and not(B)
A= exists $v->{"type"}B= not ref $v->{"type"}
ABresult
0X0
100
111
263 0 ▶ next unless check_type($v);
Branch: 1 branch
BranchTrueFalseTotal
unless check_type($v) 0 0 0
264 0 $self->$builder( $meta, $k, $v );
265 0 return 1;
266 }
267 }
268 elsif ( ref($v) eq 'ARRAY' ) {
269 0 my $first = $v->[0];
270 0 ▶ if ( ref($first) && ref($first) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first and ref $first eq "HASH") 0 0 0
Condition0%0/3
ref $first and ref $first eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $first and ref $first eq "HASH"
ref $first ref $first eq "HASH"
Truth table0%0/3
A and B
A= ref $firstB= ref $first eq "HASH"
ABresult
0X0
100
111
271 0 ▶ next unless check_type($first);
Branch: 1 branch
BranchTrueFalseTotal
unless check_type($first) 0 0 0
272 0 $self->$builder( $meta, $k, $v );
273 0 return 1;
274 }
275 }
276  
277 }
278  
279 0 return 0;
280 }
281  
282 sub check_type {
283 0 ▶ my $hash_ref = shift;
Subroutine check_type: not called CC=3 SCAR=24.8
284  
285 0 ▶ return unless exists $hash_ref->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash_ref->{"type"} 0 0 0
286 0 my $type = $hash_ref->{type};
287 0 ▶ return unless $type eq $hash_ref->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq $hash_ref->{"type"} 0 0 0
288  
289 0 return 1;
290  
291 }
292  
293 ▶sub BUILD { }
Subroutine BUILD: not called CC=1 SCAR=0.0
294  
295 after 'BUILD' => sub {
296 my $self = shift;
297 $self->interpol_directive_cache( {} );
298 };
299  
300 __PACKAGE__->meta->make_immutable;
301  
302 1;
↑ Summary blib/lib/BioX/Workflow/Command/Exceptions.pm »