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>
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
16 Bio::FeatureIO - Handler for FeatureIO
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');
35 $out = Bio::FeatureIO->new(-file => ">outputfilename" ,
39 while ( my $feature = $in->next_feature() ) {
40 $out->write_feature($feature);
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,
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
75 -----------------------------------
79 InterPro (IPRScan 4.0) interpro.pm
80 PTT (NCBI protein table) ptt.pm
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:
99 A file path to be opened for reading or writing. The usual Perl
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
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>
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
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,
136 # output goes into $string
137 $out->write_feature($f);
139 $string =~ s|(>)(\w+)|$1<font color="Red">$2</font>|g;
146 Specify the format of the file. See above for list of supported formats
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",
159 my $f2 = Bio::FeatureIO->new -file => ">a.f2",
161 -flush => 0; # go as fast as we can!
163 while($feature = $f1->next_feature) { $f2->write_feature($feature) }
167 =head2 Bio::FeatureIO-E<gt>newFh()
169 $fh = Bio::FeatureIO->newFh(-fh => \*FILEHANDLE, -format=>$format);
170 $fh = Bio::FeatureIO->newFh(-format => $format);
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:
181 Other operations, such as read(), sysread(), write(), close(), and printf()
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.
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
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
230 https://redmine.open-bio.org/projects/bioperl/
232 =head1 AUTHOR - Allen Day
234 Email allenday@ucla.edu
238 The rest of the documentation details each of the object
239 methods. Internal methods are usually preceded with a _
243 #' Let the code begin...
245 package Bio
::FeatureIO
;
251 use base
qw(Bio::Root::Root Bio::Root::IO);
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:
261 -fh => filehandle to attach to
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
274 if( $class =~ /Bio::FeatureIO::(\S+)/ ) {
276 my ($self) = $class->SUPER::new
(@args);
277 $self->_initialize(@args);
284 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
285 my $format = $param{'-format'} ||
286 $class->_guess_format( $param{-file
} || $ARGV[0] );
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);
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
313 See L<Bio::FeatureIO::Fh>
319 return unless my $self = $class->new(@_);
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
339 my $class = ref($self) || $self;
340 my $s = Symbol
::gensym
;
341 tie
$$s,$class,$self;
345 # _initialize is chained for all FeatureIO classes
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);
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
375 Returns : a Bio::SeqFeatureI feature object
378 See L<Bio::Root::RootI>, L<Bio::SeqFeatureI>
383 my ($self, $seq) = @_;
384 $self->throw("Sorry, you cannot read from a generic Bio::FeatureIO object.");
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
398 my ($self, $seq) = @_;
399 if(ref($self) eq __PACKAGE__
){
400 $self->throw("Sorry, you cannot write to a generic Bio::FeatureIO object.");
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
417 sub _load_format_module
{
418 my ($self, $format) = @_;
419 my $class = ref($self) || $self;
420 my $module = $class."::$format";#"Bio::Feature::" . $format;
424 $ok = $self->_load_module($module);
428 $self: $format cannot be found
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
441 Usage : $obj->seq() OR $obj->seq($newSeq)
443 Returns : Bio::SeqI object
444 Args : newSeq (optional)
452 $self->{'seq'} = $val if defined($val);
453 return $self->{'seq'};
459 Usage : $obj->_filehandle($newval)
460 Function: This method is deprecated. Call _fh() instead.
462 Returns : value of _filehandle
463 Args : newvalue (optional)
469 my ($self,@args) = @_;
470 return $self->_fh(@args);
475 Title : _guess_format
476 Usage : $obj->_guess_format($filename)
477 Function: guess format based on file suffix
479 Returns : guessed format of filename (lower case)
481 Notes : See "SUPPORTED FORMATS"
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
502 my ($class,$val) = @_;
503 return bless {'featio' => $val}, $class;
508 return $self->{'featio'}->next_feature() unless wantarray;
510 push @list, $obj while $obj = $self->{'featio'}->next_feature();
516 $self->{'featio'}->write_feature(@_);