| 1 | package BoutrosLab::TSVStream::IO::Role::Reader::Fixed; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # safe Perl | ||||||||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | BoutrosLab::TSVStream::IO::Role::Reader::Fixed | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 8 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 8 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 8 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 8 | ▶ | use List::MoreUtils qw(all); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 8 | enum 'ReadHeaderType', [qw(auto none check)]; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 8 | has header => ( | |||||||||||||||||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 24 | isa => 'ReadHeaderType', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | default => 'auto' | ||||||||||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | has extra_class_params => ( | ||||||||||||||||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 119 | ▶ | default => sub { [] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 8 | ); | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 8 | has pre_header_pattern => ( | |||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | isa => 'Maybe[RegexpRef]', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | default => undef | ||||||||||||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 8 | has _is_pre_header => ( | |||||||||||||||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | isa => 'CodeRef', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | builder => '_init_is_pre_header' | ||||||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 13 | ▶ | sub _init_is_pre_header { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init_is_pre_header: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 13 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 13 | ▶ | if (my $pat = $self->pre_header_pattern) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 38 | ▶ | sub { $_[0] =~ /$pat/ } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 13 | } | |||||||||||||||||||||||||||||||||||||||||
| 52 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $self->_is_comment | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | has pre_headers => ( | ||||||||||||||||||||||||||||||||||||||||||
| 58 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 59 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||||||||||||||||
| 60 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 119 | ▶ | default => sub { [] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 8 | ); | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | has _comments => ( | ||||||||||||||||||||||||||||||||||||||||||
| 65 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 66 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||||||||||||||||
| 67 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | writer => '_set_comments', | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 119 | ▶ | default => sub { [] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 8 | ); | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 143 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 143 | my $orig = shift; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 143 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 143 | ▶ | my $arg = ref($_[0]) ? $_[0] : { @_ }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 143 | my %valid_arg = ( | |||||||||||||||||||||||||||||||||||||||||
| 78 | file => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | handle => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | header => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 81 | class => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 82 | comment => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 83 | pre_comment => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 84 | pre_header => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 85 | header_fix => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 86 | extra_class_params => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | pre_header_pattern => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | comment_pattern => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 143 | $arg->{_valid_arg} = \%valid_arg; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 143 | $arg->{_open_mode} = '<'; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 143 | $class->$orig( $arg ); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 32 | ▶ | sub _read_no_header { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_no_header: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 32 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 32 | my $none = $self->header eq 'none'; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 32 | ( $none, $none ); | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 32 | ▶ | sub _fill_dyn_fields { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fill_dyn_fields: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 32 | return; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 95 | ▶ | sub _header { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_header: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 95 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 95 | my $stream_fields = shift; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 95 | my $class_fields = $self->fields; | |||||||||||||||||||||||||||||||||||||||||
| 110 | return $#$class_fields <= $#$stream_fields | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 95 | ▶ | && all { uc( $stream_fields->[$_] ) eq uc( $class_fields->[$_] ) } 0 .. $#$class_fields; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Condition100%3/3
$class_fields->$#* <= $stream_fields->$#* && &all(sub {
uc $stream_fields->[$_] eq uc $class_fields->[$_];
}
, 0 .. $class_fields->$#*)
MC/DC100%2/2
$class_fields->$#* <= $stream_fields->$#* && &all(sub {
uc $stream_fields->[$_] eq uc $class_fields->[$_];
}
, 0 .. $class_fields->$#*)
$class_fields->$#* <= $stream_fields->$#*
&all(sub {
uc $stream_fields->[$_] eq uc $class_fields->[$_];
}
, 0 .. $class_fields->$#*)
Truth table100%3/3
A && B
A= $class_fields->$#* <= $stream_fields->$#*B= &all(sub {
uc $stream_fields->[$_] eq uc $class_fields->[$_];
}
, 0 .. $class_fields->$#*)
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 119 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=25 SCAR=34.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 119 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 119 | my ( $none, $ret ) = $self->_read_no_header; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 119 | ▶ | return if $ret; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 119 | my @pre; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 119 | my $stream_fields = []; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 119 | my $is_head = undef; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 119 | ▶ | print "Starting pre-header checks\n" if $ENV{HEADER_PROCESS}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 119 | ▶ | if (!$self->_peek) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 24 | $self->_fill_dyn_fields( $none, 0, $stream_fields ); | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 95 | ▶ | while (my $line = $self->_read) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 121 | my $is_pre; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 121 | my $lline = $line->{line}; | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub check1 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check1: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my( $self, $test, $bool, $check, $line ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ($self->$bool) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | print " ",uc($test),($self->$check->($line) ? ":YES" : ":no "), "\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | print " ",lc($test), "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 121 | ▶ | print "Checking line: $lline\n" if $ENV{HEADER_PROCESS}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 121 | ▶ | check1( $self, 'PH', pre_header => _is_pre_header => $lline ) if $ENV{HEADER_PROCESS}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 121 | ▶ | check1( $self, 'PC', pre_comment => _is_comment => $lline ) if $ENV{HEADER_PROCESS}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 121 | ▶ | check1( $self, 'CO', comment => _is_comment => $lline ) if $ENV{HEADER_PROCESS}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 121 | ▶ | if ($self->pre_header) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 38 | $is_pre = $self->_is_pre_header->($lline); | |||||||||||||||||||||||||||||||||||||||||
| 148 | 38 | ▶ | $is_pre ||= $self->_is_comment->($lline) if $self->pre_comment; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$is_pre ||= $self->_is_comment->($lline)
MC/DC0%0/2
$is_pre ||= $self->_is_comment->($lline)
$is_pre
$self->_is_comment->($lline)
Truth table0%0/3
A ||= B
A= $is_preB= $self->_is_comment->($lline)
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 83 | ▶ | $is_pre = $self->_is_comment->($lline) if $self->comment; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 121 | ▶ | if ($is_pre) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 26 | ▶ | print " -> pre\n" if $ENV{HEADER_PROCESS}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 26 | push @pre, $line; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 26 | next; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 95 | $stream_fields = $self->header_fix->($line)->{fields}; | |||||||||||||||||||||||||||||||||||||||||
| 159 | # $stream_fields = $line->{fields}; | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 95 | $is_head = $self->_header($stream_fields); | |||||||||||||||||||||||||||||||||||||||||
| 161 | 95 | ▶ | print " -> NOT pre, none: $none, is_head: $is_head, header_proc: ",$self->header,"\n" if $ENV{HEADER_PROCESS}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 95 | $self->_fill_dyn_fields( $none, $is_head, $stream_fields ); | |||||||||||||||||||||||||||||||||||||||||
| 163 | 95 | ▶ | if ($none or !$is_head && $self->header eq 'auto') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$none or not $is_head and $self->header eq "auto"
not $is_head and $self->header eq "auto"
MC/DC100%3/3
$none or not $is_head and $self->header eq "auto"
$none
not $is_head
$self->header eq "auto"
Truth table100%4/4
A or not(B) and C
A= $noneB= not $is_headC= $self->header eq "auto"
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 24 | ▶ | print " *** put back\n" if $ENV{HEADER_PROCESS}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 24 | $self->_unread( @pre, $line ); | |||||||||||||||||||||||||||||||||||||||||
| 166 | 24 | return; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 71 | last; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 71 | ▶ | print " *** kept\n" if $ENV{HEADER_PROCESS}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 71 | my $die = $self->_num_fields != scalar(@$stream_fields); | |||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 71 | ▶ | if ($die || !$is_head) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$die or not $is_head
MC/DC100%2/2
$die or not $is_head
$die
not $is_head
Truth table100%3/3
A or not(B)
A= $dieB= not $is_head
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 9 | my $error = ''; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 9 | ▶ | $error = 'Headers do not match' if !$is_head; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 9 | ▶ | $error .= ' and wrong number of fields' if $die; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 9 | $error =~ s/^ and w/W/; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 9 | $self->_croak( $error, $stream_fields ); | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 62 | push @{ $self->pre_headers }, ( map { $_->{line} } @pre ); | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 6 | ▶ | sub read_comments { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_comments: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 187 | 6 | my $comments = $self->_comments; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 6 | $self->_set_comments( [] ); | |||||||||||||||||||||||||||||||||||||||||
| 189 | 6 | return $comments; | |||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 239 | ▶ | sub _load_comments { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_comments: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 239 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 239 | ▶ | return unless $self->comment; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 6 | my $comments = $self->_comments; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 6 | ▶ | while (my $line = $self->_read) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 10 | ▶ | if (! $self->_is_comment->( $line->{line} )) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 4 | $self->_unread($line); | |||||||||||||||||||||||||||||||||||||||||
| 199 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 6 | push @$comments, $line->{line}; | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | 239 | ▶ | sub read { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 239 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 239 | $self->_load_comments; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 239 | ▶ | return unless my $values = $self->_read; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 133 | my $line = $values->{line}; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 133 | $values = $values->{fields}; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 133 | my $error; | |||||||||||||||||||||||||||||||||||||||||
| 212 | my $obj; | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 133 | ▶ | $error = 'Wrong number of fields' if scalar(@$values) != $self->_num_fields; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 133 | ▶ | unless ($error) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 133 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 217 | $obj = $self->class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 218 | field_values => $values, | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 133 | @{ $self->extra_class_params }, | |||||||||||||||||||||||||||||||||||||||||
| 220 | $self->_read_config | ||||||||||||||||||||||||||||||||||||||||||
| 221 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 222 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 133 | ▶ | $error = $@ if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 133 | ▶ | $self->_croak( $error, $values ) if $error; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 133 | return $obj; | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | 2 | ▶ | sub filter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 2 | my ( $self, $filtersub ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 233 | 2 | return BoutrosLab::TSVStream::IO::Role::Reader::Filter->new( | |||||||||||||||||||||||||||||||||||||||||
| 234 | reader => $self, | ||||||||||||||||||||||||||||||||||||||||||
| 235 | filtersub => $filtersub | ||||||||||||||||||||||||||||||||||||||||||
| 236 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | package BoutrosLab::TSVStream::IO::Role::Reader::Filter; | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | # safe Perl | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 8 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 8 | has reader => ( | |||||||||||||||||||||||||||||||||||||||||
| 249 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 250 | isa => 'Object', | ||||||||||||||||||||||||||||||||||||||||||
| 251 | required => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 252 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 8 | has filtersub => ( | |||||||||||||||||||||||||||||||||||||||||
| 255 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 256 | isa => 'CodeRef', | ||||||||||||||||||||||||||||||||||||||||||
| 257 | required => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 258 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | 7 | ▶ | sub read { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 262 | 7 | ▶ | while (my $record = $self->reader->read) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 7 | ▶ | return $record if $self->filtersub->($record); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 1 | ▶ | sub filter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 1 | my ( $self, $filtersub ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 270 | 1 | return BoutrosLab::TSVStream::IO::Role::Reader::Filter->new( | |||||||||||||||||||||||||||||||||||||||||
| 271 | reader => $self, | ||||||||||||||||||||||||||||||||||||||||||
| 272 | filtersub => $filtersub | ||||||||||||||||||||||||||||||||||||||||||
| 273 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | $class->reader( ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | # ($class will use the role BoutrosLab::TSVStream which will provide | ||||||||||||||||||||||||||||||||||||||||||
| 281 | # the reader method, that method will return a Reader object with: | ||||||||||||||||||||||||||||||||||||||||||
| 282 | # ... | ||||||||||||||||||||||||||||||||||||||||||
| 283 | # return BoutrosLab::TSVStream::IO::Role::Reader::Fixed->new( | ||||||||||||||||||||||||||||||||||||||||||
| 284 | # handle => $fd, # (required) | ||||||||||||||||||||||||||||||||||||||||||
| 285 | # class => $class, # (required) class | ||||||||||||||||||||||||||||||||||||||||||
| 286 | # file => $file, # (optional) used (as filename) in error messages | ||||||||||||||||||||||||||||||||||||||||||
| 287 | # header => $str, # (optional) one of: check none (default 'check') | ||||||||||||||||||||||||||||||||||||||||||
| 288 | # ); | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | while (my $record = $reader->read) { | ||||||||||||||||||||||||||||||||||||||||||
| 291 | # ... $record is a $class object | ||||||||||||||||||||||||||||||||||||||||||
| 292 | # ... use $record->field1, $record->field2, etc. - all of the methods of $class object | ||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | This object provides an iterator to read through the lines | ||||||||||||||||||||||||||||||||||||||||||
| 298 | of a data stream (C<$fd>), converting each from a line with | ||||||||||||||||||||||||||||||||||||||||||
| 299 | tab separated fields into an object of a class (C<$classs>) | ||||||||||||||||||||||||||||||||||||||||||
| 300 | that has attributes for those fields. | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | Usually, the data stream will start with a line that has the | ||||||||||||||||||||||||||||||||||||||||||
| 303 | fieldnames in a tab separated list, and the rest of the stream | ||||||||||||||||||||||||||||||||||||||||||
| 304 | has lines that contain the field values in a tab separated list. | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | Any error diagnostics will refer to the stream using the | ||||||||||||||||||||||||||||||||||||||||||
| 307 | C<$file> filename if it is provided. | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | The C<$class> class will have a class attribute named | ||||||||||||||||||||||||||||||||||||||||||
| 310 | C<_fields>. Usually, this will be a read-only method that | ||||||||||||||||||||||||||||||||||||||||||
| 311 | returns a list of fieldnames that will be used to validate | ||||||||||||||||||||||||||||||||||||||||||
| 312 | the first line in the data stream (which should contain the | ||||||||||||||||||||||||||||||||||||||||||
| 313 | field names as the column vlues). | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | A class C<$class> object will be created for each line. | ||||||||||||||||||||||||||||||||||||||||||
| 316 | The object will be initialized with a list of names and values | ||||||||||||||||||||||||||||||||||||||||||
| 317 | matching the fields and the contents .of the line. | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | If C<header> is provided, it can be 'check', or 'none'. | ||||||||||||||||||||||||||||||||||||||||||
| 320 | This controls what is done to the handle initially. | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | If 'check' is specified, the first line of the stream is read | ||||||||||||||||||||||||||||||||||||||||||
| 323 | and it is checked to ensure that it matches the C<fields> both | ||||||||||||||||||||||||||||||||||||||||||
| 324 | in name and order. The fields list must be complete. However, | ||||||||||||||||||||||||||||||||||||||||||
| 325 | it is permitted for the field names to mismatch by having | ||||||||||||||||||||||||||||||||||||||||||
| 326 | different capitalization - the comparison is not case sensitive. | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | If 'none' is specified, the stream is not checked for a header | ||||||||||||||||||||||||||||||||||||||||||
| 329 | line. (You would use this option either if the file does not | ||||||||||||||||||||||||||||||||||||||||||
| 330 | have a header line, or if you are scanning from the middle of | ||||||||||||||||||||||||||||||||||||||||||
| 331 | a file handle that is no longer at the start of the file.) | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | =head2 handle - the filehandle to be read | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | =head2 file - the name of the stream, usually a filename, for diagnostic purposes | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | =head2 class - the class that records transformed into | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | =head2 fields - list of field names, usually provided by class | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | handle, file, class and fields are provided by the ...::Base role | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | =head2 header - 'auto', 'check', or 'none' (default 'auto') | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | The C<'check'> setting causes the first line of the stream to | ||||||||||||||||||||||||||||||||||||||||||
| 350 | be read and validated against the C<fields> list. The field | ||||||||||||||||||||||||||||||||||||||||||
| 351 | names are accepted if they match (but differences in upper/lower | ||||||||||||||||||||||||||||||||||||||||||
| 352 | case are ignored). If they do not match, an exception is thrown. | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | If the C<'none'> setting is provided, the stream should already be | ||||||||||||||||||||||||||||||||||||||||||
| 355 | positioned at a data value (i.e. the stream was previously opened and | ||||||||||||||||||||||||||||||||||||||||||
| 356 | is no longer positioned at the start, or else the stream was originally | ||||||||||||||||||||||||||||||||||||||||||
| 357 | created without a leading header line). | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | The default C<'auto'> setting causes the first line to be read and | ||||||||||||||||||||||||||||||||||||||||||
| 360 | validated as for the C<'check'> setting, but if the line does not | ||||||||||||||||||||||||||||||||||||||||||
| 361 | match the list of fields it is assumed to instead be the first data | ||||||||||||||||||||||||||||||||||||||||||
| 362 | line of a stream that has no headers, and processing continues as | ||||||||||||||||||||||||||||||||||||||||||
| 363 | if the C<'none'> setting were specified instead. | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | =head1 BUILDARGS | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | The BUILDARGS opens a handle if only a file name was provided. | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | =head1 BUILD | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | The BUILD method handles any requirements for reading and processing a | ||||||||||||||||||||||||||||||||||||||||||
| 372 | header line. | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | =head2 read - read a line from the stream end turn it into a class element | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | John Macdonald - Boutros Lab | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | Paul Boutros, Phd, PI - Boutros Lab | ||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | The Ontario Institute for Cancer Research | ||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||