« lib/BoutrosLab/TSVStream/IO/Role/Base/Fixed.pm
↑ Summary
lib/BoutrosLab/TSVStream/Format/VCF/Types.pm »
| 1 | package BoutrosLab::TSVStream::IO::Role::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 | 8 | ▶ | use Moose::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 8 | ▶ | use namespace::autoclean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 8 | ▶ | use Moose::Util qw(find_meta); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 8 | ▶ | use BoutrosLab::TSVStream::IO::Reader::Fixed; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 8 | ▶ | use BoutrosLab::TSVStream::IO::Writer::Fixed; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | BoutrosLab::TSVStream::IO::Role::Fixed | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | # in a Moose class definition... | ||||||||||||||||||||
| 23 | use MooseX::ClassAttribute | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | class_has '_fields' => ( | ||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||
| 27 | isa => 'ArrayRef', | ||||||||||||||||||||
| 28 | default => sub { [qw(foo bar)] } | ||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | # # or, without using MooseX::ClassAttribute | ||||||||||||||||||||
| 32 | # my $_fields = [ qw(foo bar) ]; | ||||||||||||||||||||
| 33 | # sub _fields { return $_fields } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | with 'BoutrosLab::TSVStream::IO::Role::Fixed'; | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | has 'foo' => ( ... ); | ||||||||||||||||||||
| 38 | has 'bar' => ( ... ); | ||||||||||||||||||||
| 39 | ... | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | This role provides methods to create a file/iostream reader/writer | ||||||||||||||||||||
| 44 | for a class, using a stream of lines with tab separated fields for | ||||||||||||||||||||
| 45 | each record, converting to/from an object of the class. Usually, | ||||||||||||||||||||
| 46 | the stream will start with an initial line that has the field names | ||||||||||||||||||||
| 47 | as a tab separated record. | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | This role is also provided a BUILDARGS wrapper that alows the | ||||||||||||||||||||
| 50 | constructor to be given one element pair in the parameter list: | ||||||||||||||||||||
| 51 | (field_values=>[val1,val2,...]) instead of providing each field | ||||||||||||||||||||
| 52 | explicitly by name as (fld1=>val1, fld2=>val2, ...). In such a | ||||||||||||||||||||
| 53 | case, the values in the B<field_values> array must be in the same | ||||||||||||||||||||
| 54 | order as they are listed in the B<_fields> class attribute. | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | has [qw(_tsvinternal_pre_header _tsvinternal_pre_comments _tsvinternal_post_comments)] => ( | ||||||||||||||||||||
| 59 | is => 'ro', | ||||||||||||||||||||
| 60 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||
| 61 | 468 | ▶ | default => sub { [] } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | 8 | ); | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 32 | ▶ | sub _reader_class { | ||||||||||||||||||
|
Subroutine
_reader_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 65 | 32 | return 'BoutrosLab::TSVStream::IO::Reader::Fixed'; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 10 | ▶ | sub _writer_class { | ||||||||||||||||||
|
Subroutine
_writer_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 69 | 10 | return 'BoutrosLab::TSVStream::IO::Writer::Fixed'; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 153 | ▶ | sub _hashlist_opt_attr { | ||||||||||||||||||
|
Subroutine
_hashlist_opt_attr: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 73 | 153 | my $self = shift; | |||||||||||||||||||
| 74 | 153 | my $attr = shift; | |||||||||||||||||||
| 75 | 153 | my $can = $self->can($attr); | |||||||||||||||||||
| 76 | 153 | ▶ | return $can ? %{ $self->$attr } : (); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 143 | ▶ | sub reader { | ||||||||||||||||||
|
Subroutine
reader: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||
| 80 | 143 | my $self = shift; | |||||||||||||||||||
| 81 | 143 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 82 | 143 | return $self->_reader_class() | |||||||||||||||||||
| 83 | ->new( { $self->_hashlist_opt_attr('_reader_args'), @_, class => $class } ); | ||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 10 | ▶ | sub writer { | ||||||||||||||||||
|
Subroutine
writer: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||
| 87 | 10 | my $self = shift; | |||||||||||||||||||
| 88 | 10 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 89 | 10 | return $self->_writer_class() | |||||||||||||||||||
| 90 | ->new( { $self->_hashlist_opt_attr('_writer_args'), @_, class => $class } ); | ||||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 156 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 94 | 156 | my $orig = shift; | |||||||||||||||||||
| 95 | 156 | my $class = shift; | |||||||||||||||||||
| 96 | 156 | ▶ | my $arg = ref($_[0]) ? $_[0] : { @_ }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 156 | ▶ | if (my $field_values = delete $arg->{field_values}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 53 | my @v = @$field_values; | |||||||||||||||||||
| 100 | 53 | $arg->{$_} = shift @v for @{ $class->_fields }; | |||||||||||||||||||
| 101 | 53 | ▶ | $arg->{dyn_values} = \@v if scalar(@v); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | 156 | $class->$orig( $arg ); | |||||||||||||||||||
| 104 | 8 | }; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 55 | ▶ | sub BUILD { | ||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 107 | 55 | my $self = shift; | |||||||||||||||||||
| 108 | 55 | $self->_check_dups( @{ $self->_fields } ); | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 139 | ▶ | sub _check_dups { | ||||||||||||||||||
|
Subroutine
_check_dups: called CC=5 SCAR=20.1
| |||||||||||||||||||||
| 112 | 139 | my $self = shift; | |||||||||||||||||||
| 113 | 139 | my %seen; | |||||||||||||||||||
| 114 | my @dups; | ||||||||||||||||||||
| 115 | 139 | for my $hdr (@_) { | |||||||||||||||||||
| 116 | 305 | ▶ | push @dups, $hdr if $seen{$hdr}++; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | 139 | ▶ | if (@dups) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 0 | ▶ | my $s = (@dups == 1) ? '' : 's'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 0 | croak "field name$s (" | |||||||||||||||||||
| 121 | . join( ', ', @dups) | ||||||||||||||||||||
| 122 | . ") seen multiple times in headers (" | ||||||||||||||||||||
| 123 | . join( ', ', @_) | ||||||||||||||||||||
| 124 | . ")"; | ||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =head1 AUTHOR | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | John Macdonald - Boutros Lab | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | Paul Boutros, Phd, PI - Boutros Lab | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | The Ontario Institute for Cancer Research | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 8 | 1; | |||||||||||||||||||
| 142 | |||||||||||||||||||||