Merge branch 'master' of github.com:bioperl/bioperl-live
[bioperl-live.git] / Bio / SeqIO / MultiFile.pm
blobfc328ed609c247aef554daee5464209015b2e9da
2 # BioPerl module for Bio::SeqIO::MultiFile
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Ewan Birney <birney@ebi.ac.uk>
8 # Copyright Ewan Birney
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
14 =head1 NAME
16 Bio::SeqIO::MultiFile - Treating a set of files as a single input stream
18 =head1 SYNOPSIS
20 my $seqin = Bio::SeqIO::MultiFile->new( -format => 'Fasta',
21 -files => ['file1','file2'] );
22 while (my $seq = $seqin->next_seq) {
23 # do something with $seq
26 =head1 DESCRIPTION
28 Bio::SeqIO::MultiFile provides a simple way of bundling a whole
29 set of identically formatted sequence input files as a single stream.
30 File format is automatically determined by C<Bio::SeqIO>.
32 =head1 FEEDBACK
34 =head2 Mailing Lists
36 User feedback is an integral part of the evolution of this and other
37 Bioperl modules. Send your comments and suggestions preferably to one
38 of the Bioperl mailing lists. Your participation is much appreciated.
40 bioperl-l@bioperl.org - General discussion
41 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
43 =head2 Support
45 Please direct usage questions or support issues to the mailing list:
47 I<bioperl-l@bioperl.org>
49 rather than to the module maintainer directly. Many experienced and
50 reponsive experts will be able look at the problem and quickly
51 address it. Please include a thorough description of the problem
52 with code and data examples if at all possible.
54 =head2 Reporting Bugs
56 Report bugs to the Bioperl bug tracking system to help us keep track
57 the bugs and their resolution.
58 Bug reports can be submitted via the web:
60 https://github.com/bioperl/bioperl-live/issues
62 =head1 AUTHOR - Ewan Birney
64 Email birney@ebi.ac.uk
66 =head1 APPENDIX
68 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
70 =cut
73 # Let the code begin...
76 package Bio::SeqIO::MultiFile;
77 use strict;
79 use base qw(Bio::SeqIO);
82 # _initialize is where the heavy stuff will happen when new is called
84 sub _initialize {
85 my($self, @args) = @_;
87 $self->SUPER::_initialize(@args);
89 my ($file_array, $format) = $self->_rearrange([qw(FILES FORMAT)], @args);
90 if( !defined $file_array || ! ref $file_array ) {
91 $self->throw("Must have an array files for MultiFile");
94 $self->{'_file_array'} = [];
95 $self->_set_file(@$file_array);
97 $self->format($format) if defined $format;
99 if( $self->_load_file() == 0 ) {
100 $self->throw("Unable to initialise the first file");
105 =head2 next_seq
107 Title : next_seq
108 Usage :
109 Function:
110 Example :
111 Returns :
112 Args :
114 =cut
116 sub next_seq{
117 my ($self, @args) = @_;
118 my $seq = $self->_current_seqio->next_seq();
119 if( !defined $seq ) {
120 if( $self->_load_file() == 0) {
121 return;
122 } else {
123 return $self->next_seq();
125 } else {
126 return $seq;
131 =head2 next_primary_seq
133 Title : next_primary_seq
134 Usage :
135 Function:
136 Example :
137 Returns :
138 Args :
140 =cut
142 sub next_primary_seq{
143 my ($self, @args) = @_;
144 my $seq = $self->_current_seqio->next_primary_seq();
145 if( !defined $seq ) {
146 if( $self->_load_file() == 0) {
147 return;
148 } else {
149 return $self->next_primary_seq();
151 } else {
152 return $seq;
157 =head2 _load_file
159 Title : _load_file
160 Usage :
161 Function:
162 Example :
163 Returns :
164 Args :
166 =cut
168 sub _load_file{
169 my ($self, @args) = @_;
170 my $file = shift @{$self->{'_file_array'}};
171 if( !defined $file ) {
172 return 0;
174 my $seqio;
175 my $format = $self->format;
176 if ($format) {
177 $seqio = Bio::SeqIO->new( -file => $file, -format => $format );
178 } else {
179 $seqio = Bio::SeqIO->new( -file => $file );
180 $self->format($seqio->format) if not $format;
183 # should throw an exception - but if not...
184 if( !defined $seqio) {
185 $self->throw("Could not build SeqIO object for $file!");
187 $self->_current_seqio($seqio);
188 return 1;
192 =head2 _set_file
194 Title : _set_file
195 Usage :
196 Function:
197 Example :
198 Returns :
199 Args :
201 =cut
203 sub _set_file{
204 my ($self, @files) = @_;
205 push @{$self->{'_file_array'}}, @files;
209 =head2 _current_seqio
211 Title : _current_seqio
212 Usage : $obj->_current_seqio($newval)
213 Function:
214 Example :
215 Returns : value of _current_seqio
216 Args : newvalue (optional)
218 =cut
220 sub _current_seqio{
221 my ($obj, $value) = @_;
222 if( defined $value) {
223 $obj->{'_current_seqio'} = $value;
225 return $obj->{'_current_seqio'};
229 # We overload the format() method of Bio::Root::IO by a simple get/set
231 sub format{
232 my ($obj, $value) = @_;
233 if( defined $value) {
234 $obj->{'_format'} = $value;
236 return $obj->{'_format'};