« blib/lib/BoutrosLab/TSVStream/Format/AnnovarInput/HumanTagNoChr/Fixed.pm
↑ Summary
blib/lib/BoutrosLab/TSVStream/Format/VCF/AsAnnovarInputChr/Dyn.pm »
| 1 | package BoutrosLab::TSVStream::Format::None::Dyn; | ||
| 2 | |||
| 3 | # safe Perl | ||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use Carp; | ||
| 7 | use BoutrosLab::TSVStream::IO::Role::Dyn; | ||
| 8 | |||
| 9 | |||
| 10 | use Moose; | ||
| 11 | use namespace::autoclean; | ||
| 12 | use MooseX::ClassAttribute; | ||
| 13 | |||
| 14 | class_has '_fields' => ( | ||
| 15 | is => 'ro', | ||
| 16 | isa => 'ArrayRef', | ||
| 17 | default => sub { [ ] } | ||
| 18 | ); | ||
| 19 | |||
| 20 | with 'BoutrosLab::TSVStream::IO::Role::Dyn'; | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | BoutrosLab::TSVStream::Format::None::Dyn | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use BoutrosLab::TSVStream::Format::None::Dyn; | ||
| 31 | |||
| 32 | # you know ./myfile1 has a valid header line | ||
| 33 | my $reader1 = BoutrosLab::TSVStream::Format::None::Dyn->reader( | ||
| 34 | file => "./myfile1" ); | ||
| 35 | |||
| 36 | my $flds; | ||
| 37 | while (my $rec1 = $reader1->read) { | ||
| 38 | $flds //= $rec->dyn_fields; # first time, get the field names | ||
| 39 | $vals = $rec->dyn_values; # get the field values | ||
| 40 | # use the record: $flds: [f1,f2,f3,...], $vals: [v1,v2,v3,...] | ||
| 41 | } | ||
| 42 | |||
| 43 | # ./myfile2 has no header line | ||
| 44 | my $reader2 = BoutrosLab::TSVStream::Format::None::Dyn->reader( | ||
| 45 | file => "./myfile2", | ||
| 46 | dyn_fields => [ qw(foo bar baz) ], | ||
| 47 | ); | ||
| 48 | while (my $rec = $reader2->read) { | ||
| 49 | $vals = $rec->dyn_values; # get the field values | ||
| 50 | # use the record: flds: [foo,bar,baz], $vals: [v1,v2,v3] | ||
| 51 | } | ||
| 52 | |||
| 53 | my $writer1 = BoutrosLab::TSVStream::Format::None::Dyn->writer( | ||
| 54 | file => "myoutfile", | ||
| 55 | dyn_fields => [ qw(name address ], | ||
| 56 | ); | ||
| 57 | |||
| 58 | $writer1->write( 'Larry Wall', 'Republic of Perl' ); | ||
| 59 | # myfile will contain 2 lines: | ||
| 60 | # name<TAB>address | ||
| 61 | # Larry Wall<TAB>Republic of Perl | ||
| 62 | |||
| 63 | my $writer2 = BoutrosLab::TSVStream::Format::None::Dyn->writer( | ||
| 64 | file => "myoutfile", | ||
| 65 | header => 'skip', | ||
| 66 | ); | ||
| 67 | |||
| 68 | $writer2->write( 'Larry Wall', 'Republic of Perl' ); | ||
| 69 | # myfile will contain 1 line: | ||
| 70 | # Larry Wall<TAB>Republic of Perl | ||
| 71 | |||
| 72 | =head1 DESCRIPTION | ||
| 73 | |||
| 74 | This class provides methods to read or write streams of tab | ||
| 75 | separated fields, with no compile-time plan for the nummber of | ||
| 76 | fields or their names. | ||
| 77 | |||
| 78 | A reader will determine the name of the fields (and thus their | ||
| 79 | number) either from an explicit list, or from the first line of | ||
| 80 | the input stream (in which case, that stream must have been written | ||
| 81 | with a header line providing the names). | ||
| 82 | |||
| 83 | The returned reader will have a B<read> method - each time it is | ||
| 84 | invoked, it returns a record for the next item in the stream (or | ||
| 85 | undef at end of the stream). The returned record will have two | ||
| 86 | attributes - B<dyn_fields> is a ArrayRef[Str] with the names of | ||
| 87 | the fields (possibly 'extra1', 'extra2', ... if no names or header | ||
| 88 | were provided), and - B<dyn_values> is a ArrayRef[Str] containing | ||
| 89 | the valules of the fields for this record. | ||
| 90 | |||
| 91 | A writer should be provided the names of the fields, otherwise it | ||
| 92 | will set the names to be 'extra1', 'extra2', ... and write those | ||
| 93 | names (unless you have specified that header writting is to be | ||
| 94 | skipped). | ||
| 95 | |||
| 96 | It will provide a B<write> method that can take an array of | ||
| 97 | strings, or a ist of strings or an Object that is a subclass of | ||
| 98 | BoutrosLab::TSVStream::Format. After the number of fields has been | ||
| 99 | determined (either from a provided B<dyn_fields> in the B<writer> | ||
| 100 | call, or by reading a header line, or by couting the number of | ||
| 101 | fields in the first B<write>; all subsequent B<write> calls must | ||
| 102 | provide the same number of fields. | ||
| 103 | |||
| 104 | =head1 SEE ALSO | ||
| 105 | |||
| 106 | =over | ||
| 107 | |||
| 108 | =item BoutrosLab::TSVStream::IO | ||
| 109 | |||
| 110 | for a description of reader and writer objects. This module only | ||
| 111 | allows for the dynamic fields, internally providing an empty list | ||
| 112 | for the fixed fields. | ||
| 113 | |||
| 114 | =item BoutrosLab::TSVStream::Format::* | ||
| 115 | |||
| 116 | for objects that include a known set of fields, and can provide | ||
| 117 | explicit type validation for those fields. If you are using the | ||
| 118 | same set of fields in many places, it would be well-advised to | ||
| 119 | define a sub-class that describes your set in better detail. | ||
| 120 | As well as providing the ability to type-check the fields, that | ||
| 121 | also gets you the ability to retrieve the field values by name. | ||
| 122 | |||
| 123 | =back | ||
| 124 | |||
| 125 | =head1 AUTHOR | ||
| 126 | |||
| 127 | John Macdonald - Boutros Lab | ||
| 128 | |||
| 129 | =head1 ACKNOWLEDGEMENTS | ||
| 130 | |||
| 131 | Paul Boutros, Phd, PI - Boutros Lab | ||
| 132 | |||
| 133 | The Ontario Institute for Cancer Research | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | 1; | ||
| 138 |