sync w/ main trunk
[bioperl-live.git] / Bio / PopGen / IO.pm
blobda258986f58a5e116a56bb29cfddaf81c0b88ccf
1 # $Id$
3 # BioPerl module for Bio::PopGen::IO
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Jason Stajich <jason-at-bioperl.org>
9 # Copyright Jason Stajich
11 # You may distribute this module under the same terms as perl itself
13 # POD documentation - main docs before the code
15 =head1 NAME
17 Bio::PopGen::IO - Input individual,marker,allele information
19 =head1 SYNOPSIS
21 use Bio::PopGen::IO;
22 my $io = Bio::PopGen::IO->new(-format => 'csv',
23 -file => 'data.csv');
25 # Some IO might support reading in a population at a time
27 my @population;
28 while( my $ind = $io->next_individual ) {
29 push @population, $ind;
33 =head1 DESCRIPTION
35 This is a generic interface to reading in population genetic data (of
36 which there really isn't too many standard formats). This implementation
37 makes it easy to provide your own parser for the data. You need to
38 only implement one function next_individual. You can also implement
39 next_population if your data has explicit information about population
40 memberhsip for the indidviduals.
42 =head1 FEEDBACK
44 =head2 Mailing Lists
46 User feedback is an integral part of the evolution of this and other
47 Bioperl modules. Send your comments and suggestions preferably to
48 the Bioperl mailing list. Your participation is much appreciated.
50 bioperl-l@bioperl.org - General discussion
51 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
53 =head2 Support
55 Please direct usage questions or support issues to the mailing list:
57 L<bioperl-l@bioperl.org>
59 rather than to the module maintainer directly. Many experienced and
60 reponsive experts will be able look at the problem and quickly
61 address it. Please include a thorough description of the problem
62 with code and data examples if at all possible.
64 =head2 Reporting Bugs
66 Report bugs to the Bioperl bug tracking system to help us keep track
67 of the bugs and their resolution. Bug reports can be submitted via
68 the web:
70 http://bugzilla.open-bio.org/
72 =head1 AUTHOR - Jason Stajich
74 Email jason-at-bioperl.org
76 =head1 APPENDIX
78 The rest of the documentation details each of the object methods.
79 Internal methods are usually preceded with a _
81 =cut
84 # Let the code begin...
85 #TODO
86 # Set the Individual creation as a factory rather than
87 # hardcoded
89 package Bio::PopGen::IO;
90 use strict;
92 # Object preamble - inherits from Bio::Root::Root
94 use Bio::Root::Root;
96 use base qw(Bio::Root::IO);
98 =head2 new
100 Title : new
101 Usage : my $obj = Bio::PopGen::IO->new();
102 Function: Builds a new Bio::PopGen::IO object
103 Returns : an instance of Bio::PopGen::IO
104 Args :
107 =cut
109 sub new {
110 my($class,@args) = @_;
112 if( $class =~ /Bio::PopGen::IO::(\S+)/ ) {
113 my ($self) = $class->SUPER::new(@args);
114 $self->_initialize(@args);
115 return $self;
116 } else {
117 my %param = @args;
118 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
119 my $format = $param{'-format'} ||
120 $class->_guess_format( $param{'-file'} || $ARGV[0] ) || 'csv';
122 # normalize capitalization to lower case
123 $format = "\L$format";
125 return unless( $class->_load_format_module($format) );
126 return "Bio::PopGen::IO::${format}"->new(@args);
130 # _initialize is chained for all PopGen::IO classes
132 sub _initialize {
133 my($self, @args) = @_;
134 # my ($indfact, $popfact) = $self->_rearrange([qw(INDIVIDUAL_FACTORY
135 # POPULATION_FACTORY)],
136 # @args);
137 # $indfact = Bio::PopGen::IndividualBuilder->new() unless $indfact;
138 # $indfact = Bio::PopGen::PopulationBuilder->new() unless $indfact;
140 # initialize the IO part
141 $self->_initialize_io(@args);
142 return 1;
145 =head2 next_individual
147 Title : next_individual
148 Usage : my $ind = $popgenio->next_individual;
149 Function: Retrieve the next individual from a dataset
150 Returns : L<Bio::PopGen::IndividualI> object
151 Args : none
154 =cut
156 sub next_individual{
157 my ($self) = @_;
158 $self->throw_not_implemented();
162 =head2 next_population
164 Title : next_population
165 Usage : my $pop = $popgenio->next_population;
166 Function: Retrieve the next population from a dataset
167 Returns : L<Bio::PopGen::PopulationI> object
168 Args : none
169 Note : Many implementation will not implement this
171 =cut
173 sub next_population{
174 my ($self) = @_;
175 $self->throw_not_implemented();
178 =head2 write_individual
180 Title : write_individual
181 Usage : $popgenio->write_individual($ind);
182 Function: Write an individual out in the implementation format
183 Returns : none
184 Args : L<Bio::PopGen::PopulationI> object(s)
186 =cut
188 sub write_individual{
189 my ($self) = @_;
190 $self->throw_not_implemented();
195 =head2 write_population
197 Title : write_population
198 Usage : $popgenio->write_population($pop);
199 Function: Write a population out in the implementation format
200 Returns : none
201 Args : L<Bio::PopGen::PopulationI> object(s)
202 Note : Many implementation will not implement this
204 =cut
206 sub write_population{
207 my ($self) = @_;
208 $self->throw_not_implemented();
212 =head2 newFh
214 Title : newFh
215 Usage : $fh = Bio::SeqIO->newFh(-file=>$filename,-format=>'Format')
216 Function: does a new() followed by an fh()
217 Example : $fh = Bio::SeqIO->newFh(-file=>$filename,-format=>'Format')
218 $sequence = <$fh>; # read a sequence object
219 print $fh $sequence; # write a sequence object
220 Returns : filehandle tied to the Bio::SeqIO::Fh class
221 Args :
223 See L<Bio::SeqIO::Fh>
225 =cut
227 sub newFh {
228 my $class = shift;
229 return unless my $self = $class->new(@_);
230 return $self->fh;
233 =head2 fh
235 Title : fh
236 Usage : $obj->fh
237 Function:
238 Example : $fh = $obj->fh; # make a tied filehandle
239 $sequence = <$fh>; # read a sequence object
240 print $fh $sequence; # write a sequence object
241 Returns : filehandle tied to Bio::SeqIO class
242 Args : none
244 =cut
247 sub fh {
248 my $self = shift;
249 my $class = ref($self) || $self;
250 my $s = Symbol::gensym;
251 tie $$s,$class,$self;
252 return $s;
255 =head2 _load_format_module
257 Title : _load_format_module
258 Usage : *INTERNAL Bio::PopGen::IO stuff*
259 Function: Loads up (like use) a module at run time on demand
260 Example :
261 Returns :
262 Args :
264 =cut
266 sub _load_format_module {
267 my ($self,$format) = @_;
268 my $module = "Bio::PopGen::IO::" . $format;
269 my $ok;
271 eval {
272 $ok = $self->_load_module($module);
274 if ( $@ ) {
275 print STDERR <<END;
276 $self: $format cannot be found
277 Exception $@
278 For more information about the Bio::PopGen::IO system please see the
279 Bio::PopGen::IO docs. This includes ways of checking for formats at
280 compile time, not run time
284 return $ok;
288 =head2 _guess_format
290 Title : _guess_format
291 Usage : $obj->_guess_format($filename)
292 Function:
293 Example :
294 Returns : guessed format of filename (lower case)
295 Args :
297 =cut
300 sub _guess_format {
301 my $class = shift;
302 return unless $_ = shift;
303 return 'csv' if (/csv/i or /\.dat\w$/i);
306 sub close {
307 my $self = shift;
308 $self->SUPER::close(@_);
311 sub DESTROY {
312 my $self = shift;
313 $self->close();
316 sub TIEHANDLE {
317 my $class = shift;
318 return bless {processor => shift}, $class;
321 sub READLINE {
322 my $self = shift;
323 return $self->{'processor'}->next_result() unless wantarray;
324 my (@list, $obj);
325 push @list, $obj while $obj = $self->{'processor'}->next_result();
326 return @list;
329 sub PRINT {
330 my $self = shift;
331 $self->{'processor'}->write_result(@_);