see if we can make PAUSE skip indexing these modules
[bioperl-live.git] / Bio / FeatureIO.pm
blobadd4dd3ef85e38edb05944bbbafdeb18046c35e6
2 # BioPerl module for Bio::FeatureIO
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Allen Day <allenday@ucla.edu>
8 # Copyright Allen Day
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::FeatureIO - Handler for FeatureIO
18 =head1 SYNOPSIS
20 use Bio::FeatureIO;
22 #read from a file
23 $in = Bio::FeatureIO->new(-file => "my.gff" , -format => 'GFF');
25 #read from a filehandle
26 $in = Bio::FeatureIO->new(-fh => \*GFF , -format => 'GFF');
28 #read features already attached to a sequence
29 my $feat = Bio::FeatureIO->new(-seq => $seq , -format => 'features');
31 #read new features for existing sequence
32 my $seq = Bio::FeatureIO->new(-seq => $seq , -format => 'Das');
34 #write out features
35 $out = Bio::FeatureIO->new(-file => ">outputfilename" ,
36 -format => 'GFF' ,
37 -version => 3);
39 while ( my $feature = $in->next_feature() ) {
40 $out->write_feature($feature);
43 =head1 DESCRIPTION
45 NOTE: This is a highly experimental I/O iterator subsystem for genomic sequence
46 features. It is not complete, and is now undergoing a significant refactoring in
47 a separate branch of BioPerl to address specific issues regarding the current
48 implementation. Use at your own risk.
50 Bio::FeatureIO is a handler module for the formats in the FeatureIO set (eg,
51 Bio::FeatureIO::GFF).
53 The Bio::FeatureIO system can be thought of like biological file handles.
54 They are attached to filehandles with smart formatting rules (eg,
55 GFF format, or BED format) and
56 can either read or write feature objects (Bio::SeqFeature objects, or
57 more correctly, Bio::FeatureHolderI implementing objects, of which
58 Bio::SeqFeature is one such object). If you want to know what to
59 do with a Bio::SeqFeatureI object, read L<Bio::SeqFeatureI>.
61 The idea is that you request a stream object for a particular format.
62 All the stream objects have a notion of an internal file that is read
63 from or written to. A particular FeatureIO object instance is configured
64 for either input or output. A specific example of a stream object is
65 the Bio::FeatureIO::gff object.
67 Each stream object has functions:
69 $stream->next_feature();
70 $stream->write_feature($feature);
72 =head1 SUPPORTED FORMATS
74 name module
75 -----------------------------------
76 BED bed.pm
77 GFF gff.pm
78 GTF gtf.pm
79 InterPro (IPRScan 4.0) interpro.pm
80 PTT (NCBI protein table) ptt.pm
83 =head1 CONSTRUCTORS
85 =head2 Bio::FeatureIO-E<gt>new()
87 $featureIO = Bio::FeatureIO->new(-file => 'filename', -format=>$format);
88 $featureIO = Bio::FeatureIO->new(-fh => \*FILEHANDLE, -format=>$format);
89 $featureIO = Bio::FeatureIO->new(-seq => $seq, -format=>$format);
91 The new() class method constructs a new Bio::FeatureIO object. The
92 returned object can be used to retrieve or print Seq objects. new()
93 accepts the following parameters:
95 =over 4
97 =item -file
99 A file path to be opened for reading or writing. The usual Perl
100 conventions apply:
102 'file' # open file for reading
103 '>file' # open file for writing
104 '>>file' # open file for appending
105 '+<file' # open file read/write
106 'command |' # open a pipe from the command
107 '| command' # open a pipe to the command
109 =item -fh
111 You may provide new() with a previously-opened filehandle. For
112 example, to read from STDIN:
114 $featio = Bio::FeatureIO->new(-fh => \*STDIN);
116 Note that you must pass filehandles as references to globs.
118 If neither a filehandle nor a filename is specified, then the module
119 will read from the @ARGV array or STDIN, using the familiar E<lt>E<gt>
120 semantics.
122 A string filehandle is handy if you want to modify the output in the
123 memory, before printing it out. The following program reads in EMBL
124 formatted entries from a file and prints them out in fasta format with
125 some HTML tags:
127 use Bio::FeatureIO;
128 use IO::String;
129 my $in = Bio::FeatureIO->new('-file' => "my.gff" ,
130 '-format' => 'EMBL');
131 while ( my $f = $in->next_feature() ) {
132 # the output handle is reset for every file
133 my $stringio = IO::String->new($string);
134 my $out = Bio::FeatureIO->new('-fh' => $stringio,
135 '-format' => 'gtf');
136 # output goes into $string
137 $out->write_feature($f);
138 # modify $string
139 $string =~ s|(>)(\w+)|$1<font color="Red">$2</font>|g;
140 # print into STDOUT
141 print $string;
144 =item -format
146 Specify the format of the file. See above for list of supported formats
148 =item -flush
150 By default, all files (or filehandles) opened for writing sequences
151 will be flushed after each write_seq() (making the file immediately
152 usable). If you don't need this facility and would like to marginally
153 improve the efficiency of writing multiple sequences to the same file
154 (or filehandle), pass the -flush option '0' or any other value that
155 evaluates as defined but false:
157 my $f1 = Bio::FeatureIO->new -file => "<a.f1",
158 -format => "f1";
159 my $f2 = Bio::FeatureIO->new -file => ">a.f2",
160 -format => "f2",
161 -flush => 0; # go as fast as we can!
163 while($feature = $f1->next_feature) { $f2->write_feature($feature) }
165 =back
167 =head2 Bio::FeatureIO-E<gt>newFh()
169 $fh = Bio::FeatureIO->newFh(-fh => \*FILEHANDLE, -format=>$format);
170 $fh = Bio::FeatureIO->newFh(-format => $format);
171 # etc.
173 This constructor behaves like new(), but returns a tied filehandle
174 rather than a Bio::FeatureIO object. You can read sequences from this
175 object using the familiar E<lt>E<gt> operator, and write to it using
176 print(). The usual array and $_ semantics work. For example, you can
177 read all sequence objects into an array like this:
179 @features = <$fh>;
181 Other operations, such as read(), sysread(), write(), close(), and printf()
182 are not supported.
184 =head1 OBJECT METHODS
186 See below for more detailed summaries. The main methods are:
188 =head2 $feature = $featureIO-E<gt>next_feature()
190 Fetch the next feature from the stream.
192 =head2 $featureIO-E<gt>write_feature($feature [,$another_feature,...])
194 Write the specified feature(s) to the stream.
196 =head2 TIEHANDLE(), READLINE(), PRINT()
198 These provide the tie interface. See L<perltie> for more details.
200 =head1 FEEDBACK
202 =head2 Mailing Lists
204 User feedback is an integral part of the evolution of this
205 and other Bioperl modules. Send your comments and suggestions preferably
206 to one of the Bioperl mailing lists.
208 Your participation is much appreciated.
210 bioperl-l@bioperl.org - General discussion
211 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
213 =head2 Support
215 Please direct usage questions or support issues to the mailing list:
217 I<bioperl-l@bioperl.org>
219 rather than to the module maintainer directly. Many experienced and
220 reponsive experts will be able look at the problem and quickly
221 address it. Please include a thorough description of the problem
222 with code and data examples if at all possible.
224 =head2 Reporting Bugs
226 Report bugs to the Bioperl bug tracking system to help us keep track
227 the bugs and their resolution. Bug reports can be submitted via the
228 web:
230 https://redmine.open-bio.org/projects/bioperl/
232 =head1 AUTHOR - Allen Day
234 Email allenday@ucla.edu
236 =head1 APPENDIX
238 The rest of the documentation details each of the object
239 methods. Internal methods are usually preceded with a _
241 =cut
243 #' Let the code begin...
245 package Bio::FeatureIO;
247 use strict;
249 use Symbol;
251 use base qw(Bio::Root::Root Bio::Root::IO);
253 =head2 new
255 Title : new
256 Usage : $stream = Bio::FeatureIO->new(-file => $filename, -format => 'Format')
257 Function: Returns a new feature stream
258 Returns : A Bio::FeatureIO stream initialised with the appropriate format
259 Args : Named parameters:
260 -file => $filename
261 -fh => filehandle to attach to
262 -format => format
264 =cut
266 my $entry = 0;
268 sub new {
269 my ($caller,@args) = @_;
270 my $class = ref($caller) || $caller;
272 # or do we want to call SUPER on an object if $caller is an
273 # object?
274 if( $class =~ /Bio::FeatureIO::(\S+)/ ) {
276 my ($self) = $class->SUPER::new(@args);
277 $self->_initialize(@args);
278 return $self;
280 } else {
282 my %param = @args;
284 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
285 my $format = $param{'-format'} ||
286 $class->_guess_format( $param{-file} || $ARGV[0] );
288 if( ! $format ) {
289 if ($param{-file}) {
290 $format = $class->_guess_format($param{-file});
291 } elsif ($param{-fh}) {
292 $format = $class->_guess_format(undef);
295 $format = "\L$format"; # normalize capitalization to lower case
296 return unless( $class->_load_format_module($format) );
297 return "Bio::FeatureIO::$format"->new(@args);
302 =head2 newFh
304 Title : newFh
305 Usage : $fh = Bio::FeatureIO->newFh(-file=>$filename,-format=>'Format')
306 Function: does a new() followed by an fh()
307 Example : $fh = Bio::FeatureIO->newFh(-file=>$filename,-format=>'Format')
308 $feature = <$fh>; # read a feature object
309 print $fh $feature; # write a feature object
310 Returns : filehandle tied to the Bio::FeatureIO::Fh class
311 Args :
313 See L<Bio::FeatureIO::Fh>
315 =cut
317 sub newFh {
318 my $class = shift;
319 return unless my $self = $class->new(@_);
320 return $self->fh;
323 =head2 fh
325 Title : fh
326 Usage : $obj->fh
327 Function:
328 Example : $fh = $obj->fh; # make a tied filehandle
329 $feature = <$fh>; # read a feature object
330 print $fh $feature; # write a feature object
331 Returns : filehandle tied to Bio::FeatureIO class
332 Args : none
334 =cut
337 sub fh {
338 my $self = shift;
339 my $class = ref($self) || $self;
340 my $s = Symbol::gensym;
341 tie $$s,$class,$self;
342 return $s;
345 # _initialize is chained for all FeatureIO classes
347 sub _initialize {
348 my($self, %arg) = @_;
350 # flush is initialized by the Root::IO init
352 # initialize the IO part
353 $self->seq($arg{-seq});
354 $self->_initialize_io(%arg);
357 =head2 next_feature
359 Title : next_feature
360 Usage : $feature = stream->next_feature
361 Function: Reads the next feature object from the stream and returns it.
363 Certain driver modules may encounter entries in the stream
364 that are either misformatted or that use syntax not yet
365 understood by the driver. If such an incident is
366 recoverable, e.g., by dismissing a feature of a feature
367 table or some other non-mandatory part of an entry, the
368 driver will issue a warning. In the case of a
369 non-recoverable situation an exception will be thrown. Do
370 not assume that you can resume parsing the same stream
371 after catching the exception. Note that you can always turn
372 recoverable errors into exceptions by calling
373 $stream->verbose(2).
375 Returns : a Bio::SeqFeatureI feature object
376 Args : none
378 See L<Bio::Root::RootI>, L<Bio::SeqFeatureI>
380 =cut
382 sub next_feature {
383 my ($self, $seq) = @_;
384 $self->throw("Sorry, you cannot read from a generic Bio::FeatureIO object.");
387 =head2 write_feature
389 Title : write_feature
390 Usage : $stream->write_feature($feature)
391 Function: writes the $feature object into the stream
392 Returns : 1 for success and 0 for error
393 Args : Bio::SeqFeature object
395 =cut
397 sub write_feature {
398 my ($self, $seq) = @_;
399 if(ref($self) eq __PACKAGE__){
400 $self->throw("Sorry, you cannot write to a generic Bio::FeatureIO object.");
401 } else {
402 $self->throw_not_implemented();
406 =head2 _load_format_module
408 Title : _load_format_module
409 Usage : *INTERNAL FeatureIO stuff*
410 Function: Loads up (like use) a module at run time on demand
411 Example :
412 Returns :
413 Args :
415 =cut
417 sub _load_format_module {
418 my ($self, $format) = @_;
419 my $class = ref($self) || $self;
420 my $module = $class."::$format";#"Bio::Feature::" . $format;
421 my $ok;
423 eval {
424 $ok = $self->_load_module($module);
426 if ( $@ ) {
427 print STDERR <<END;
428 $self: $format cannot be found
429 Exception $@
430 For more information about the FeatureIO system please see the FeatureIO docs.
431 This includes ways of checking for formats at compile time, not run time
435 return $ok;
438 =head2 seq
440 Title : seq
441 Usage : $obj->seq() OR $obj->seq($newSeq)
442 Example :
443 Returns : Bio::SeqI object
444 Args : newSeq (optional)
446 =cut
448 sub seq {
449 my $self = shift;
450 my $val = shift;
452 $self->{'seq'} = $val if defined($val);
453 return $self->{'seq'};
456 =head2 _filehandle
458 Title : _filehandle
459 Usage : $obj->_filehandle($newval)
460 Function: This method is deprecated. Call _fh() instead.
461 Example :
462 Returns : value of _filehandle
463 Args : newvalue (optional)
466 =cut
468 sub _filehandle {
469 my ($self,@args) = @_;
470 return $self->_fh(@args);
473 =head2 _guess_format
475 Title : _guess_format
476 Usage : $obj->_guess_format($filename)
477 Function: guess format based on file suffix
478 Example :
479 Returns : guessed format of filename (lower case)
480 Args :
481 Notes : See "SUPPORTED FORMATS"
483 =cut
485 sub _guess_format {
486 my $class = shift;
487 return unless $_ = shift;
488 return 'gff' if /\.gff3?$/i;
489 return 'gff' if /\.gtf$/i;
490 return 'bed' if /\.bed$/i;
491 return 'ptt' if /\.ptt$/i;
493 return 'gff'; #the default
496 sub DESTROY {
497 my $self = shift;
498 $self->close();
501 sub TIEHANDLE {
502 my ($class,$val) = @_;
503 return bless {'featio' => $val}, $class;
506 sub READLINE {
507 my $self = shift;
508 return $self->{'featio'}->next_feature() unless wantarray;
509 my (@list, $obj);
510 push @list, $obj while $obj = $self->{'featio'}->next_feature();
511 return @list;
514 sub PRINT {
515 my $self = shift;
516 $self->{'featio'}->write_feature(@_);