« blib/lib/BoutrosLab/TSVStream/IO/Role/Fixed.pm
↑ Summary
lib/BoutrosLab/TSVStream/Format/AnnovarInput/Types.pm »
| 1 | package BoutrosLab::TSVStream::IO::Role::Reader::Dyn; | ||
| 2 | |||
| 3 | # safe Perl | ||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use Carp; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | BoutrosLab::TSVStream:IO::Role::Reader::Dyn | ||
| 11 | |||
| 12 | =cut | ||
| 13 | |||
| 14 | use Moose::Role; | ||
| 15 | use namespace::autoclean; | ||
| 16 | |||
| 17 | with 'BoutrosLab::TSVStream::IO::Role::Reader::Fixed'; | ||
| 18 | |||
| 19 | sub _read_no_header { | ||
| 20 | my $self = shift; | ||
| 21 | my $none = $self->header eq 'none'; | ||
| 22 | ( $none, ($none && $self->_has_dyn_fields) ); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub _fill_dyn_fields { | ||
| 26 | my ($self, $none, $is_head, $stream_fields ) = @_; | ||
| 27 | my $num_fixed_fields = $#{ $self->fields }; | ||
| 28 | |||
| 29 | if (!$self->_has_dyn_fields) { | ||
| 30 | $self->_set_dyn_fields( [ | ||
| 31 | (!$none && $is_head) | ||
| 32 | ? @{$stream_fields}[ $num_fixed_fields+1 .. $#$stream_fields ] | ||
| 33 | : $self->_extra_names( $#$stream_fields - $num_fixed_fields ) | ||
| 34 | ] ); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | $class->reader( ... ); | ||
| 41 | |||
| 42 | # ($class will use the role BoutrosLab::TSVStream which will provide | ||
| 43 | # the reader method, that method will return a Reader object with: | ||
| 44 | # ... | ||
| 45 | # return BoutrosLab::TSVStream::Reader->new( | ||
| 46 | # handle => $fd, # (required) | ||
| 47 | # class => $class, # (required) class | ||
| 48 | # file => $file, # (optional) used (as filename) in error messages | ||
| 49 | # header => $str, # (optional) one of: check none (default 'check') | ||
| 50 | # ); | ||
| 51 | |||
| 52 | while (my $record = $reader->read) { | ||
| 53 | # ... $record is a $class object | ||
| 54 | # ... use $record->field1, $record->field2, etc. - all of the methods of $class object | ||
| 55 | } | ||
| 56 | |||
| 57 | =head1 DESCRIPTION | ||
| 58 | |||
| 59 | This object provides an iterator to read through the lines | ||
| 60 | of a data stream (C<$fd>), converting each from a line with | ||
| 61 | tab separated fields into an object of a class (C<$classs>) | ||
| 62 | that has attributes for those fields. | ||
| 63 | |||
| 64 | Usually, the data stream will start with a line that has the | ||
| 65 | fieldnames in a tab separated list, and the rest of the stream | ||
| 66 | has lines that contain the field values in a tab separated list. | ||
| 67 | |||
| 68 | Any error diagnostics will refer to the stream using the | ||
| 69 | C<$file> filename if it is provided. | ||
| 70 | |||
| 71 | The C<$class> class will have a class attribute named | ||
| 72 | C<_fields>. Usually, this will be a read-only method that | ||
| 73 | returns a list of fieldnames that will be used to validate | ||
| 74 | the first line in the data stream (which should contain the | ||
| 75 | field names as the column vlues). | ||
| 76 | |||
| 77 | A class C<$class> object will be created for each line. | ||
| 78 | The object will be initialized with a list of names and values | ||
| 79 | matching the fields and the contents .of the line. | ||
| 80 | |||
| 81 | If C<header> is provided, it can be 'check', or 'none'. | ||
| 82 | This controls what is done to the handle initially. | ||
| 83 | |||
| 84 | If 'check' is specified, the first line of the stream is read | ||
| 85 | and it is checked to ensure that it matches the C<fields> both | ||
| 86 | in name and order. The fields list must be complete. However, | ||
| 87 | it is permitted for the field names to mismatch by having | ||
| 88 | different capitalization - the comparison is not case sensitive. | ||
| 89 | |||
| 90 | If 'none' is specified, the stream is not checked for a header | ||
| 91 | line. (You would use this option either if the file does not | ||
| 92 | have a header line, or if you are scanning from the middle of | ||
| 93 | a file handle that is no longer at the start of the file.) | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | =head1 ATTRIBUTES | ||
| 98 | |||
| 99 | =head2 handle - the filehandle to be read | ||
| 100 | |||
| 101 | =head2 file - the name of the stream, usually a filename, for diagnostic purposes | ||
| 102 | |||
| 103 | =head2 class - the class that records transformed into | ||
| 104 | |||
| 105 | =head2 fields - list of field names, usually provided by class | ||
| 106 | |||
| 107 | handle, file, class and fields are provided by the ...::Base role | ||
| 108 | |||
| 109 | =head2 header - 'auto', 'check', or 'none' (default 'auto') | ||
| 110 | |||
| 111 | The C<'check'> setting causes the first line of the stream to | ||
| 112 | be read and validated against the C<fields> list. The field | ||
| 113 | names are accepted if they match (but differences in upper/lower | ||
| 114 | case are ignored). If they do not match, an exception is thrown. | ||
| 115 | |||
| 116 | If the C<'none'> setting is provided, the stream should already be | ||
| 117 | positioned at a data value (i.e. the stream was previously opened and | ||
| 118 | is no longer positioned at the start, or else the stream was originally | ||
| 119 | created without a leading header line). | ||
| 120 | |||
| 121 | The default C<'auto'> setting causes the first line to be read and | ||
| 122 | validated as for the C<'check'> setting, but if the line does not | ||
| 123 | match the list of fields it is assumed to instead be the first data | ||
| 124 | line of a stream that has no headers, and processing continues as | ||
| 125 | if the C<'none'> setting were specified instead. | ||
| 126 | |||
| 127 | =head1 BUILDARGS | ||
| 128 | |||
| 129 | The BUILDARGS opens a handle if only a file name was provided. | ||
| 130 | |||
| 131 | =head1 BUILD | ||
| 132 | |||
| 133 | The BUILD method handles any requirements for reading and processing a | ||
| 134 | header line. | ||
| 135 | |||
| 136 | =head1 METHODS | ||
| 137 | |||
| 138 | =head2 read - read a line from the stream end turn it into a class element | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | =head1 AUTHOR | ||
| 143 | |||
| 144 | John Macdonald - Boutros Lab | ||
| 145 | |||
| 146 | =head1 ACKNOWLEDGEMENTS | ||
| 147 | |||
| 148 | Paul Boutros, Phd, PI - Boutros Lab | ||
| 149 | |||
| 150 | The Ontario Institute for Cancer Research | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | 1; | ||
| 155 |