| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::SeqIO::MultiFile | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Ewan Birney <birney@ebi.ac.uk> | ||
| 7 | # | ||
| 8 | # Copyright Ewan Birney | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | |||
| 12 | # POD documentation - main docs before the code | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | FAST::Bio::SeqIO::MultiFile - Treating a set of files as a single input stream | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | $seqin = FAST::Bio::SeqIO::MultiFile( '-format' => 'Fasta', | ||
| 21 | '-files' => ['file1','file2'] ); | ||
| 22 | while((my $seq = $seqin->next_seq)) { | ||
| 23 | # do something with $seq | ||
| 24 | } | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | FAST::Bio::SeqIO::MultiFile provides a simple way of bundling a whole | ||
| 29 | set of identically formatted sequence input files as a single stream. | ||
| 30 | |||
| 31 | =head1 FEEDBACK | ||
| 32 | |||
| 33 | =head2 Mailing Lists | ||
| 34 | |||
| 35 | User feedback is an integral part of the evolution of this and other | ||
| 36 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 37 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 38 | |||
| 39 | bioperl-l@bioperl.org - General discussion | ||
| 40 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 41 | |||
| 42 | =head2 Support | ||
| 43 | |||
| 44 | Please direct usage questions or support issues to the mailing list: | ||
| 45 | |||
| 46 | I<bioperl-l@bioperl.org> | ||
| 47 | |||
| 48 | rather than to the module maintainer directly. Many experienced and | ||
| 49 | reponsive experts will be able look at the problem and quickly | ||
| 50 | address it. Please include a thorough description of the problem | ||
| 51 | with code and data examples if at all possible. | ||
| 52 | |||
| 53 | =head2 Reporting Bugs | ||
| 54 | |||
| 55 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 56 | the bugs and their resolution. | ||
| 57 | Bug reports can be submitted via the web: | ||
| 58 | |||
| 59 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 60 | |||
| 61 | =head1 AUTHOR - Ewan Birney | ||
| 62 | |||
| 63 | Email birney@ebi.ac.uk | ||
| 64 | |||
| 65 | =head1 APPENDIX | ||
| 66 | |||
| 67 | The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _ | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | |||
| 72 | # Let the code begin... | ||
| 73 | |||
| 74 | |||
| 75 | package FAST::Bio::SeqIO::MultiFile; | ||
| 76 | use strict; | ||
| 77 | |||
| 78 | use base qw(FAST::Bio::SeqIO); | ||
| 79 | |||
| 80 | |||
| 81 | # _initialize is where the heavy stuff will happen when new is called | ||
| 82 | |||
| 83 | sub _initialize { | ||
| 84 | my($self,@args) = @_; | ||
| 85 | |||
| 86 | $self->SUPER::_initialize(@args); | ||
| 87 | |||
| 88 | my ($file_array,$format) = $self->_rearrange([qw( | ||
| 89 | FILES | ||
| 90 | FORMAT | ||
| 91 | )], | ||
| 92 | @args, | ||
| 93 | ); | ||
| 94 | if( !defined $file_array || ! ref $file_array ) { | ||
| 95 | $self->throw("Must have an array files for MultiFile"); | ||
| 96 | } | ||
| 97 | |||
| 98 | if( !defined $format ) { | ||
| 99 | $self->throw("Must have a format for MultiFile"); | ||
| 100 | } | ||
| 101 | |||
| 102 | $self->{'_file_array'} = []; | ||
| 103 | |||
| 104 | $self->_set_file(@$file_array); | ||
| 105 | $self->_format($format); | ||
| 106 | if( $self->_load_file() == 0 ) { | ||
| 107 | $self->throw("Unable even to initialise the first file"); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | =head2 next_seq | ||
| 112 | |||
| 113 | Title : next_seq | ||
| 114 | Usage : | ||
| 115 | Function: | ||
| 116 | Example : | ||
| 117 | Returns : | ||
| 118 | Args : | ||
| 119 | |||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub next_seq{ | ||
| 124 | my ($self,@args) = @_; | ||
| 125 | |||
| 126 | my $seq = $self->_current_seqio->next_seq(); | ||
| 127 | if( !defined $seq ) { | ||
| 128 | if( $self->_load_file() == 0) { | ||
| 129 | return; | ||
| 130 | } else { | ||
| 131 | return $self->next_seq(); | ||
| 132 | } | ||
| 133 | } else { | ||
| 134 | return $seq; | ||
| 135 | } | ||
| 136 | |||
| 137 | } | ||
| 138 | |||
| 139 | =head2 next_primary_seq | ||
| 140 | |||
| 141 | Title : next_primary_seq | ||
| 142 | Usage : | ||
| 143 | Function: | ||
| 144 | Example : | ||
| 145 | Returns : | ||
| 146 | Args : | ||
| 147 | |||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub next_primary_seq{ | ||
| 152 | my ($self,@args) = @_; | ||
| 153 | |||
| 154 | my $seq = $self->_current_seqio->next_primary_seq(); | ||
| 155 | if( !defined $seq ) { | ||
| 156 | if( $self->_load_file() == 0) { | ||
| 157 | return; | ||
| 158 | } else { | ||
| 159 | return $self->next_primary_seq(); | ||
| 160 | } | ||
| 161 | } else { | ||
| 162 | return $seq; | ||
| 163 | } | ||
| 164 | |||
| 165 | } | ||
| 166 | |||
| 167 | =head2 _load_file | ||
| 168 | |||
| 169 | Title : _load_file | ||
| 170 | Usage : | ||
| 171 | Function: | ||
| 172 | Example : | ||
| 173 | Returns : | ||
| 174 | Args : | ||
| 175 | |||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub _load_file{ | ||
| 180 | my ($self,@args) = @_; | ||
| 181 | |||
| 182 | my $file = shift(@{$self->{'_file_array'}}); | ||
| 183 | if( !defined $file ) { | ||
| 184 | return 0; | ||
| 185 | } | ||
| 186 | my $seqio = FAST::Bio::SeqIO->new( '-format' => $self->_format(), -file => $file); | ||
| 187 | # should throw an exception - but if not... | ||
| 188 | if( !defined $seqio) { | ||
| 189 | $self->throw("no seqio built for $file!"); | ||
| 190 | } | ||
| 191 | |||
| 192 | $self->_current_seqio($seqio); | ||
| 193 | return 1; | ||
| 194 | } | ||
| 195 | |||
| 196 | =head2 _set_file | ||
| 197 | |||
| 198 | Title : _set_file | ||
| 199 | Usage : | ||
| 200 | Function: | ||
| 201 | Example : | ||
| 202 | Returns : | ||
| 203 | Args : | ||
| 204 | |||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | sub _set_file{ | ||
| 209 | my ($self,@files) = @_; | ||
| 210 | |||
| 211 | push(@{$self->{'_file_array'}},@files); | ||
| 212 | |||
| 213 | } | ||
| 214 | |||
| 215 | =head2 _current_seqio | ||
| 216 | |||
| 217 | Title : _current_seqio | ||
| 218 | Usage : $obj->_current_seqio($newval) | ||
| 219 | Function: | ||
| 220 | Example : | ||
| 221 | Returns : value of _current_seqio | ||
| 222 | Args : newvalue (optional) | ||
| 223 | |||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | sub _current_seqio{ | ||
| 228 | my ($obj,$value) = @_; | ||
| 229 | if( defined $value) { | ||
| 230 | $obj->{'_current_seqio'} = $value; | ||
| 231 | } | ||
| 232 | return $obj->{'_current_seqio'}; | ||
| 233 | |||
| 234 | } | ||
| 235 | |||
| 236 | =head2 _format | ||
| 237 | |||
| 238 | Title : _format | ||
| 239 | Usage : $obj->_format($newval) | ||
| 240 | Function: | ||
| 241 | Example : | ||
| 242 | Returns : value of _format | ||
| 243 | Args : newvalue (optional) | ||
| 244 | |||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | sub _format{ | ||
| 249 | my ($obj,$value) = @_; | ||
| 250 | if( defined $value) { | ||
| 251 | $obj->{'_format'} = $value; | ||
| 252 | } | ||
| 253 | return $obj->{'_format'}; | ||
| 254 | |||
| 255 | } | ||
| 256 | |||
| 257 | 1; |