lib/BoutrosLab/TSVStream/IO/Role/Base/Fixed.pm

filter: Statementstmt 97.0% 131 / 135 Branchbran 72.7% 32 / 44 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 92.8% 26 / 28 Podpod n/a totaltotal 89.7% 193 / 215 CC 26 SCAR 13.4
CC
26
Cov
90%
__ANON__
9.8
__ANON__
6.0
_read
4.0
CRAP 3.8
« lib/BoutrosLab/TSVStream/Format/AnnovarInput/Types.pm ↑ Summary lib/BoutrosLab/TSVStream/IO/Role/Fixed.pm »
1 =head1 NAME
2  
3 BoutrosLab::TSVStream:IO::Role::Base::Fixed
4  
5 =head1 SYNOPSIS
6  
7 This is a collection of base attributes and methods used internally
8 by TSVStream reader and writer role modules. It provides the
9 common parameters used to define reader and writer methods that
10 can be imported into a target class.
11  
12 =cut
13  
14 package BoutrosLab::TSVStream::IO::Role::Base::Fixed;
15  
16 # safe Perl
17 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 8 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # Base role for all reader/writer variants
27 #
28 # The BUILDARGS wrapper checks wheter a handle was proveded
29 # and, if not, opens the file provided and sets the handle
30 # to that newly opened fd.
31 #
32 # The class that consumes this role can add two extra entries
33 # to the arg list:
34 # - _open_mode - the mode to be used for an open (usually
35 # one of '<', '>', '>>')
36 # - _valid_arg - a hash of arg names to be validated, any
37 # arg key provided which does not match a
38 # key in this hash will cause an error
39 # (the _valid_arg and _open_mode args will not
40 # cause an error - they do not need to be
41 # listed in the _valid_arg hash since they
42 # are provided internally and removed before
43 # validation).
44  
45 8 has handle => ( is => 'ro', required => 1, isa => 'FileHandle' );
46  
47 8 has file => ( is => 'ro', lazy => 1, isa => 'Str', default => '[Unnamed stream]' );
48  
49 8 has class => ( is => 'ro', required => 1, isa => 'Str' );
50  
51 8 has [ qw(comment pre_comment pre_header) ] => ( is => 'ro', isa => 'Bool', default => 0 );
52  
53 has comment_pattern => (
54 is => 'ro',
55 isa => 'RegexpRef',
56 128 ▶ default => sub { qr/(?:^\s*#)|(?:^\s*$)/ }
Subroutine __ANON__: called CC=1 SCAR=0.0
57 8 );
58  
59 88 ▶sub _null_header_fix {
Subroutine _null_header_fix: called CC=1 SCAR=0.0
60 88 return $_[0];
61 }
62  
63 has header_fix => (
64 is => 'ro',
65 isa => 'CodeRef',
66 113 ▶ default => sub { \&_null_header_fix }
Subroutine __ANON__: called CC=1 SCAR=0.0
67 8 );
68  
69 153 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=9 SCAR=22.9
70 153 my $orig = shift;
71 153 my $class = shift;
72 153 ▶ my $arg = ref($_[0]) ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 153 0 153
73  
74 153 ▶ my $open_mode = delete $arg->{_open_mode} || '<';
Condition50%1/2
delete $arg->{'_open_mode'} || '<'
l!l
1530
MC/DC0%0/1
delete $arg->{'_open_mode'} || '<'
delete $arg->{'_open_mode'}
Truth table50%1/2
A
A= delete $arg->{'_open_mode'}
Aresult
11
00
75 153 ▶ if (my $valid_arg = delete $arg->{_valid_arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $valid_arg = delete $arg->{"_valid_arg"}) 153 0 153
76 153 my @unknowns = grep { !$valid_arg->{$_} } keys %$arg;
77 153 ▶ if (@unknowns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unknowns) 24 129 153
78 24 ▶ my $s = 1 == scalar(@unknowns) ? '' : 's';
Branch: 1 branch
BranchTrueFalseTotal
1 == scalar @unknowns ? : 24 0 24
79 24 confess "Unknown option$s ("
80 . join( ',', @unknowns )
81 . "), valid options are ("
82 . join( ',', keys %$valid_arg )
83 . ")\n";
84 }
85 }
86  
87 129 ▶ unless ($arg->{handle}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg->{"handle"}) 6 123 129
88 6 ▶ if ($arg->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{'file'}) { } 6 0 6
89 open my $fh, $open_mode, $arg->{file}
90 6 ▶ or croak "unable to open $open_mode ", $arg->{file}, ": $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $open_mode, $arg->{"file"} or croak("unable to open $open_mode ", $arg->{"file"}, ": $!") 6 0 6
91 6 $arg->{handle} = $fh;
92 }
93 else {
94 0 croak "one of file/handle options must be provided";
95 }
96 }
97 129 $class->$orig( $arg );
98 8 };
99  
100 8 has fields => (
101 is => 'ro',
102 lazy => 1,
103 isa => 'ArrayRef[Str]',
104 builder => '_init_fields',
105 init_arg => undef
106 );
107  
108 129 ▶sub _init_fields {
Subroutine _init_fields: called CC=1 SCAR=0.0
109 129 my $self = shift;
110 129 $self->class->_fields
111 }
112  
113 has _num_fields => (
114 is => 'rw',
115 isa => 'Int',
116 init_arg => undef,
117 lazy => 1,
118 42 ▶ default => sub { my $self = shift; scalar( @{ $self->fields } ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
119 8 );
120  
121 8 has _field_out_methods => (
122 is => 'ro',
123 lazy => 1,
124 isa => 'Ref',
125 builder => '_init_out_fields',
126 init_arg => undef
127 );
128  
129 10 ▶sub _init_out_fields {
Subroutine _init_out_fields: called CC=2 SCAR=7.0
130 10 my $self = shift;
131 return [
132 map {
133 50 my $o_meth = "_${_}_out";
134 50 ▶ $self->can($o_meth) ? $o_meth : $_
Branch: 1 branch
BranchTrueFalseTotal
$self->can($o_meth) ? : 0 50 50
135 }
136 10 @{ $self->class->_fields }
137 ];
138 }
139  
140 has _save_lines => (
141 is => 'rw',
142 isa => 'ArrayRef[Str]',
143 init_arg => undef,
144 129 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
145 8 );
146  
147 8 has _at_eof => (
148 is => 'rw',
149 isa => 'Bool',
150 init_arg => undef,
151 default => undef
152 );
153  
154 8 has _is_comment => (
155 is => 'ro',
156 isa => 'CodeRef',
157 lazy => 1,
158 builder => '_init_is_comment'
159 );
160  
161 2 ▶sub _init_is_comment {
Subroutine _init_is_comment: called CC=2 SCAR=7.6
162 2 my $self = shift;
163 2 ▶ if ($self->comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->comment) { } 2 0 2
164 2 my $pat = $self->comment_pattern;
165 12 ▶ sub { $_[0] =~ /$pat/ }
Subroutine __ANON__: called CC=1 SCAR=0.0
166 2 }
167 else {
168 0 ▶ sub { 0 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 }
170 }
171  
172 137 ▶sub _read_config {
Subroutine _read_config: called CC=1 SCAR=0.0
173 137 return ();
174 }
175  
176 465 ▶sub _peek {
Subroutine _peek: called CC=4 SCAR=13.9
177 465 my $self = shift;
178 465 ▶ return if $self->_at_eof;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_at_eof 0 465 465
179 465 my $lines = $self->_save_lines;
180 465 ▶ unless (@$lines) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$lines) 342 123 465
181 342 my $h = $self->handle;
182 342 my $line = <$h>;
183 342 ▶ if (not defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 106 236 342
184 106 $self->_at_eof(1);
185 106 return;
186 }
187 236 chomp $line;
188 236 my $hash = { line => $line };
189 236 $line =~ s/^ *//;
190 236 $line =~ s/ *$//;
191 236 $hash->{fields} = [ split "\t", $line ];
192 236 push @$lines, $hash;
193 }
194 359 return $lines->[0];
195 }
196  
197 372 ▶sub _read {
Subroutine _read: called CC=4 SCAR=13.9
198 372 my $self = shift;
199 372 ▶ return if $self->_at_eof;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_at_eof 26 346 372
200 346 my $line = $self->_peek;
201 346 ▶ return unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 82 264 346
202 264 ▶ shift @{ $self->_save_lines } if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 264 0 264
203 264 return $line;
204 }
205  
206 28 ▶sub _unread {
Subroutine _unread: called CC=2 SCAR=7.0
207 28 my $self = shift;
208 28 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 28 0 28
209 28 unshift @{ $self->_save_lines }, @_;
210 28 $self->_at_eof(0);
211 }
212 }
213  
214 9 ▶sub _croak {
Subroutine _croak: called CC=2 SCAR=6.9
215 9 my $self = shift;
216 9 my $msg = shift;
217  
218 9 my $content = '';
219  
220 9 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 0 9
221 9 my $vals = shift;
222 9 ▶ try {
Subroutine __ANON__: called CC=6 SCAR=17.9
223 9 my @flds = @{ $self->fields };
224 9 ▶ push @flds, @{ $self->dyn_fields } if $self->can('dyn_fields') && $self->_has_dyn_fields;
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("dyn_fields") and $self->_has_dyn_fields 3 6 9
Condition66%2/3
$self->can("dyn_fields") and $self->_has_dyn_fields
!ll&&!rl&&r
603
MC/DC50%1/2
$self->can("dyn_fields") and $self->_has_dyn_fields
$self->can("dyn_fields") $self->_has_dyn_fields
Truth table66%2/3
A and B
A= $self->can("dyn_fields")B= $self->_has_dyn_fields
ABresult
0X0
100
111
225 9 ▶ push @flds, '*extra*' while @flds < @$vals;
Branch: 1 branch
BranchTrueFalseTotal
@flds < @$vals and do { push @flds, "*extra*" } 8 9 17
226 9 ▶ push @$vals, '*MISSING*' while @$vals < @flds;
Branch: 1 branch
BranchTrueFalseTotal
@$vals < @flds and do { push @$vals, "*MISSING*" } 2 9 11
227 9 $content = "\n --\> content:"; # --\> without the \ Perl::Critic thinks it is an arrow operator
228 9 ▶ while (@flds) {
Branch: 1 branch
BranchTrueFalseTotal
if (@flds) 54 9 63
229 54 my $f = shift @flds;
230 54 my $v = shift @$vals;
231 54 $content .= sprintf "\n --\> %s(%s)", $f, $v;
232 }
233 }
234 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
235 0 $content = "\n --\> Secondary failure trying to dump fields ($_)";
236 }
237 9 }
238  
239 9 my $pos = $self->handle->input_line_number;
240 9 my $file = $self->file;
241 9 croak "Error: $msg\n --\> file: $file\n --\> line: $pos,$content\n";
242 }
243  
244 29 ▶sub _write_fields {
Subroutine _write_fields: called CC=1 SCAR=0.0
245 29 my $self = shift;
246 29 my $h = $self->handle;
247 29 print $h join("\t", @_), "\n";
248 }
249  
250 12 ▶sub _write_lines {
Subroutine _write_lines: called CC=3 SCAR=11.1
251 12 my $self = shift;
252 12 my $h = $self->handle;
253 12 ▶ my @lines = map { ref($_) ? @$_ : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 12 0 12
254 12 print $h "$_\n" for @lines;
255 }
256  
257 24 ▶sub _to_fields {
Subroutine _to_fields: called CC=1 SCAR=0.0
258 24 my $self = shift;
259 24 my $obj = shift;
260 24 return map { $obj->$_ } @{ $self->_field_out_methods };
261 }
262  
263 =head1 AUTHOR
264  
265 John Macdonald - Boutros Lab
266  
267 =head1 ACKNOWLEDGEMENTS
268  
269 Paul Boutros, Phd, PI - Boutros Lab
270  
271 The Ontario Institute for Cancer Research
272  
273 =cut
274  
275 8 1;
276  
« lib/BoutrosLab/TSVStream/Format/AnnovarInput/Types.pm ↑ Summary lib/BoutrosLab/TSVStream/IO/Role/Fixed.pm »