2 # BioPerl module for Bio::PopGen::IO
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Jason Stajich <jason-at-bioperl.org>
8 # Copyright Jason Stajich
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
16 Bio::PopGen::IO - Input individual,marker,allele information
21 my $io = Bio::PopGen::IO->new(-format => 'csv',
24 # Some IO might support reading in a population at a time
27 while( my $ind = $io->next_individual ) {
28 push @population, $ind;
34 This is a generic interface to reading in population genetic data (of
35 which there really isn't too many standard formats). This implementation
36 makes it easy to provide your own parser for the data. You need to
37 only implement one function next_individual. You can also implement
38 next_population if your data has explicit information about population
39 memberhsip for the indidviduals.
45 User feedback is an integral part of the evolution of this and other
46 Bioperl modules. Send your comments and suggestions preferably to
47 the Bioperl mailing list. Your participation is much appreciated.
49 bioperl-l@bioperl.org - General discussion
50 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
54 Please direct usage questions or support issues to the mailing list:
56 I<bioperl-l@bioperl.org>
58 rather than to the module maintainer directly. Many experienced and
59 reponsive experts will be able look at the problem and quickly
60 address it. Please include a thorough description of the problem
61 with code and data examples if at all possible.
65 Report bugs to the Bioperl bug tracking system to help us keep track
66 of the bugs and their resolution. Bug reports can be submitted via
69 https://github.com/bioperl/bioperl-live/issues
71 =head1 AUTHOR - Jason Stajich
73 Email jason-at-bioperl.org
77 The rest of the documentation details each of the object methods.
78 Internal methods are usually preceded with a _
83 # Let the code begin...
85 # Set the Individual creation as a factory rather than
88 package Bio
::PopGen
::IO
;
91 # Object preamble - inherits from Bio::Root::Root
95 use base
qw(Bio::Root::IO);
100 Usage : my $obj = Bio::PopGen::IO->new();
101 Function: Builds a new Bio::PopGen::IO object
102 Returns : an instance of Bio::PopGen::IO
109 my($class,@args) = @_;
111 if( $class =~ /Bio::PopGen::IO::(\S+)/ ) {
112 my ($self) = $class->SUPER::new
(@args);
113 $self->_initialize(@args);
117 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
118 my $format = $param{'-format'} ||
119 $class->_guess_format( $param{'-file'} || $ARGV[0] ) || 'csv';
121 # normalize capitalization to lower case
122 $format = "\L$format";
124 return unless( $class->_load_format_module($format) );
125 return "Bio::PopGen::IO::${format}"->new(@args);
133 Usage : $format = $stream->format()
134 Function: Get the PopGen format
135 Returns : PopGen format
140 # format() method inherited from Bio::Root::IO
143 # _initialize is chained for all PopGen::IO classes
146 my($self, @args) = @_;
147 # my ($indfact, $popfact) = $self->_rearrange([qw(INDIVIDUAL_FACTORY
148 # POPULATION_FACTORY)],
150 # $indfact = Bio::PopGen::IndividualBuilder->new() unless $indfact;
151 # $indfact = Bio::PopGen::PopulationBuilder->new() unless $indfact;
153 # initialize the IO part
154 $self->_initialize_io(@args);
158 =head2 next_individual
160 Title : next_individual
161 Usage : my $ind = $popgenio->next_individual;
162 Function: Retrieve the next individual from a dataset
163 Returns : L<Bio::PopGen::IndividualI> object
171 $self->throw_not_implemented();
175 =head2 next_population
177 Title : next_population
178 Usage : my $pop = $popgenio->next_population;
179 Function: Retrieve the next population from a dataset
180 Returns : L<Bio::PopGen::PopulationI> object
182 Note : Many implementation will not implement this
188 $self->throw_not_implemented();
191 =head2 write_individual
193 Title : write_individual
194 Usage : $popgenio->write_individual($ind);
195 Function: Write an individual out in the implementation format
197 Args : L<Bio::PopGen::PopulationI> object(s)
201 sub write_individual
{
203 $self->throw_not_implemented();
208 =head2 write_population
210 Title : write_population
211 Usage : $popgenio->write_population($pop);
212 Function: Write a population out in the implementation format
214 Args : L<Bio::PopGen::PopulationI> object(s)
215 Note : Many implementation will not implement this
219 sub write_population
{
221 $self->throw_not_implemented();
228 Usage : $fh = Bio::SeqIO->newFh(-file=>$filename,-format=>'Format')
229 Function: does a new() followed by an fh()
230 Example : $fh = Bio::SeqIO->newFh(-file=>$filename,-format=>'Format')
231 $sequence = <$fh>; # read a sequence object
232 print $fh $sequence; # write a sequence object
233 Returns : filehandle tied to the Bio::SeqIO::Fh class
236 See L<Bio::SeqIO::Fh>
242 return unless my $self = $class->new(@_);
251 Example : $fh = $obj->fh; # make a tied filehandle
252 $sequence = <$fh>; # read a sequence object
253 print $fh $sequence; # write a sequence object
254 Returns : filehandle tied to Bio::SeqIO class
262 my $class = ref($self) || $self;
263 my $s = Symbol
::gensym
;
264 tie
$$s,$class,$self;
268 =head2 _load_format_module
270 Title : _load_format_module
271 Usage : *INTERNAL Bio::PopGen::IO stuff*
272 Function: Loads up (like use) a module at run time on demand
279 sub _load_format_module
{
280 my ($self,$format) = @_;
281 my $module = "Bio::PopGen::IO::" . $format;
285 $ok = $self->_load_module($module);
289 $self: $format cannot be found
291 For more information about the Bio::PopGen::IO system please see the
292 Bio::PopGen::IO docs. This includes ways of checking for formats at
293 compile time, not run time
303 Title : _guess_format
304 Usage : $obj->_guess_format($filename)
307 Returns : guessed format of filename (lower case)
315 return unless $_ = shift;
316 return 'csv' if (/csv/i or /\.dat\w$/i);
321 $self->SUPER::close(@_);
331 return bless {processor
=> shift}, $class;
336 return $self->{'processor'}->next_result() || undef unless wantarray;
338 push @list, $obj while $obj = $self->{'processor'}->next_result();
344 $self->{'processor'}->write_result(@_);