| 1 | # BioPerl module for FAST::Bio::SeqIO | ||
| 2 | # | ||
| 3 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 4 | # | ||
| 5 | # Cared for by Ewan Birney <birney@ebi.ac.uk> | ||
| 6 | # and Lincoln Stein <lstein@cshl.org> | ||
| 7 | # | ||
| 8 | # Copyright Ewan Birney | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | # | ||
| 12 | # _history | ||
| 13 | # October 18, 1999 Largely rewritten by Lincoln Stein | ||
| 14 | |||
| 15 | # POD documentation - main docs before the code | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | FAST::Bio::SeqIO - Handler for SeqIO Formats | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use FAST::Bio::SeqIO; | ||
| 24 | |||
| 25 | $in = FAST::Bio::SeqIO->new(-file => "inputfilename" , | ||
| 26 | -format => 'Fasta'); | ||
| 27 | $out = FAST::Bio::SeqIO->new(-file => ">outputfilename" , | ||
| 28 | -format => 'EMBL'); | ||
| 29 | |||
| 30 | while ( my $seq = $in->next_seq() ) { | ||
| 31 | $out->write_seq($seq); | ||
| 32 | } | ||
| 33 | |||
| 34 | # Now, to actually get at the sequence object, use the standard FAST::Bio::Seq | ||
| 35 | # methods (look at FAST::Bio::Seq if you don't know what they are) | ||
| 36 | |||
| 37 | use FAST::Bio::SeqIO; | ||
| 38 | |||
| 39 | $in = FAST::Bio::SeqIO->new(-file => "inputfilename" , | ||
| 40 | -format => 'genbank'); | ||
| 41 | |||
| 42 | while ( my $seq = $in->next_seq() ) { | ||
| 43 | print "Sequence ",$seq->id, " first 10 bases ", | ||
| 44 | $seq->subseq(1,10), "\n"; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | # The SeqIO system does have a filehandle binding. Most people find this | ||
| 49 | # a little confusing, but it does mean you can write the world's | ||
| 50 | # smallest reformatter | ||
| 51 | |||
| 52 | use FAST::Bio::SeqIO; | ||
| 53 | |||
| 54 | $in = FAST::Bio::SeqIO->newFh(-file => "inputfilename" , | ||
| 55 | -format => 'Fasta'); | ||
| 56 | $out = FAST::Bio::SeqIO->newFh(-format => 'EMBL'); | ||
| 57 | |||
| 58 | # World's shortest Fasta<->EMBL format converter: | ||
| 59 | print $out $_ while <$in>; | ||
| 60 | |||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | FAST::Bio::SeqIO is a handler module for the formats in the SeqIO set (eg, | ||
| 65 | FAST::Bio::SeqIO::fasta). It is the officially sanctioned way of getting at | ||
| 66 | the format objects, which most people should use. | ||
| 67 | |||
| 68 | The FAST::Bio::SeqIO system can be thought of like biological file handles. | ||
| 69 | They are attached to filehandles with smart formatting rules (eg, | ||
| 70 | genbank format, or EMBL format, or binary trace file format) and | ||
| 71 | can either read or write sequence objects (FAST::Bio::Seq objects, or | ||
| 72 | more correctly, FAST::Bio::SeqI implementing objects, of which FAST::Bio::Seq is | ||
| 73 | one such object). If you want to know what to do with a FAST::Bio::Seq | ||
| 74 | object, read L<FAST::Bio::Seq>. | ||
| 75 | |||
| 76 | The idea is that you request a stream object for a particular format. | ||
| 77 | All the stream objects have a notion of an internal file that is read | ||
| 78 | from or written to. A particular SeqIO object instance is configured | ||
| 79 | for either input or output. A specific example of a stream object is | ||
| 80 | the FAST::Bio::SeqIO::fasta object. | ||
| 81 | |||
| 82 | Each stream object has functions | ||
| 83 | |||
| 84 | $stream->next_seq(); | ||
| 85 | |||
| 86 | and | ||
| 87 | |||
| 88 | $stream->write_seq($seq); | ||
| 89 | |||
| 90 | As an added bonus, you can recover a filehandle that is tied to the | ||
| 91 | SeqIO object, allowing you to use the standard E<lt>E<gt> and print | ||
| 92 | operations to read and write sequence objects: | ||
| 93 | |||
| 94 | use FAST::Bio::SeqIO; | ||
| 95 | |||
| 96 | $stream = FAST::Bio::SeqIO->newFh(-format => 'Fasta', | ||
| 97 | -fh => \*ARGV); | ||
| 98 | # read from standard input or the input filenames | ||
| 99 | |||
| 100 | while ( $seq = <$stream> ) { | ||
| 101 | # do something with $seq | ||
| 102 | } | ||
| 103 | |||
| 104 | and | ||
| 105 | |||
| 106 | print $stream $seq; # when stream is in output mode | ||
| 107 | |||
| 108 | This makes the simplest ever reformatter | ||
| 109 | |||
| 110 | #!/usr/bin/perl | ||
| 111 | use strict; | ||
| 112 | my $format1 = shift; | ||
| 113 | my $format2 = shift || die | ||
| 114 | "Usage: reformat format1 format2 < input > output"; | ||
| 115 | |||
| 116 | use FAST::Bio::SeqIO; | ||
| 117 | |||
| 118 | my $in = FAST::Bio::SeqIO->newFh(-format => $format1, -fh => \*ARGV ); | ||
| 119 | my $out = FAST::Bio::SeqIO->newFh(-format => $format2 ); | ||
| 120 | # Note: you might want to quote -format to keep older | ||
| 121 | # perl's from complaining. | ||
| 122 | |||
| 123 | print $out $_ while <$in>; | ||
| 124 | |||
| 125 | |||
| 126 | =head1 CONSTRUCTORS | ||
| 127 | |||
| 128 | =head2 FAST::Bio::SeqIO-E<gt>new() | ||
| 129 | |||
| 130 | $seqIO = FAST::Bio::SeqIO->new(-file => 'filename', -format=>$format); | ||
| 131 | $seqIO = FAST::Bio::SeqIO->new(-fh => \*FILEHANDLE, -format=>$format); | ||
| 132 | $seqIO = FAST::Bio::SeqIO->new(-format => $format); | ||
| 133 | |||
| 134 | The new() class method constructs a new FAST::Bio::SeqIO object. The | ||
| 135 | returned object can be used to retrieve or print Seq objects. new() | ||
| 136 | accepts the following parameters: | ||
| 137 | |||
| 138 | =over 5 | ||
| 139 | |||
| 140 | =item -file | ||
| 141 | |||
| 142 | A file path to be opened for reading or writing. The usual Perl | ||
| 143 | conventions apply: | ||
| 144 | |||
| 145 | 'file' # open file for reading | ||
| 146 | '>file' # open file for writing | ||
| 147 | '>>file' # open file for appending | ||
| 148 | '+<file' # open file read/write | ||
| 149 | 'command |' # open a pipe from the command | ||
| 150 | '| command' # open a pipe to the command | ||
| 151 | |||
| 152 | =item -fh | ||
| 153 | |||
| 154 | You may provide new() with a previously-opened filehandle. For | ||
| 155 | example, to read from STDIN: | ||
| 156 | |||
| 157 | $seqIO = FAST::Bio::SeqIO->new(-fh => \*STDIN); | ||
| 158 | |||
| 159 | Note that you must pass filehandles as references to globs. | ||
| 160 | |||
| 161 | If neither a filehandle nor a filename is specified, then the module | ||
| 162 | will read from the @ARGV array or STDIN, using the familiar E<lt>E<gt> | ||
| 163 | semantics. | ||
| 164 | |||
| 165 | A string filehandle is handy if you want to modify the output in the | ||
| 166 | memory, before printing it out. The following program reads in EMBL | ||
| 167 | formatted entries from a file and prints them out in fasta format with | ||
| 168 | some HTML tags: | ||
| 169 | |||
| 170 | use FAST::Bio::SeqIO; | ||
| 171 | use IO::String; | ||
| 172 | my $in = FAST::Bio::SeqIO->new(-file => "emblfile", | ||
| 173 | -format => 'EMBL'); | ||
| 174 | while ( my $seq = $in->next_seq() ) { | ||
| 175 | # the output handle is reset for every file | ||
| 176 | my $stringio = IO::String->new($string); | ||
| 177 | my $out = FAST::Bio::SeqIO->new(-fh => $stringio, | ||
| 178 | -format => 'fasta'); | ||
| 179 | # output goes into $string | ||
| 180 | $out->write_seq($seq); | ||
| 181 | # modify $string | ||
| 182 | $string =~ s|(>)(\w+)|$1<font color="Red">$2</font>|g; | ||
| 183 | # print into STDOUT | ||
| 184 | print $string; | ||
| 185 | } | ||
| 186 | |||
| 187 | =item -format | ||
| 188 | |||
| 189 | Specify the format of the file. Supported formats include fasta, | ||
| 190 | genbank, embl, swiss (SwissProt), Entrez Gene and tracefile formats | ||
| 191 | such as abi (ABI) and scf. There are many more, for a complete listing | ||
| 192 | see the SeqIO HOWTO (L<http://bioperl.open-bio.org/wiki/HOWTO:SeqIO>). | ||
| 193 | |||
| 194 | If no format is specified and a filename is given then the module will | ||
| 195 | attempt to deduce the format from the filename suffix. If there is no | ||
| 196 | suffix that Bioperl understands then it will attempt to guess the | ||
| 197 | format based on file content. If this is unsuccessful then SeqIO will | ||
| 198 | throw a fatal error. | ||
| 199 | |||
| 200 | The format name is case-insensitive: 'FASTA', 'Fasta' and 'fasta' are | ||
| 201 | all valid. | ||
| 202 | |||
| 203 | Currently, the tracefile formats (except for SCF) require installation | ||
| 204 | of the external Staden "io_lib" package, as well as the | ||
| 205 | FAST::Bio::SeqIO::staden::read package available from the bioperl-ext | ||
| 206 | repository. | ||
| 207 | |||
| 208 | =item -alphabet | ||
| 209 | |||
| 210 | Sets the alphabet ('dna', 'rna', or 'protein'). When the alphabet is | ||
| 211 | set then Bioperl will not attempt to guess what the alphabet is. This | ||
| 212 | may be important because Bioperl does not always guess correctly. | ||
| 213 | |||
| 214 | =item -flush | ||
| 215 | |||
| 216 | By default, all files (or filehandles) opened for writing sequences | ||
| 217 | will be flushed after each write_seq() (making the file immediately | ||
| 218 | usable). If you do not need this facility and would like to marginally | ||
| 219 | improve the efficiency of writing multiple sequences to the same file | ||
| 220 | (or filehandle), pass the -flush option '0' or any other value that | ||
| 221 | evaluates as defined but false: | ||
| 222 | |||
| 223 | my $gb = FAST::Bio::SeqIO->new(-file => "<gball.gbk", | ||
| 224 | -format => "gb"); | ||
| 225 | my $fa = FAST::Bio::SeqIO->new(-file => ">gball.fa", | ||
| 226 | -format => "fasta", | ||
| 227 | -flush => 0); # go as fast as we can! | ||
| 228 | while($seq = $gb->next_seq) { $fa->write_seq($seq) } | ||
| 229 | |||
| 230 | =item -seqfactory | ||
| 231 | |||
| 232 | Provide a FAST::Bio::Factory::SequenceFactoryI object. See the sequence_factory() method. | ||
| 233 | |||
| 234 | =item -locfactory | ||
| 235 | |||
| 236 | Provide a FAST::Bio::Factory::LocationFactoryI object. See the location_factory() method. | ||
| 237 | |||
| 238 | =item -objbuilder | ||
| 239 | |||
| 240 | Provide a FAST::Bio::Factory::ObjectBuilderI object. See the object_builder() method. | ||
| 241 | |||
| 242 | =back | ||
| 243 | |||
| 244 | =head2 FAST::Bio::SeqIO-E<gt>newFh() | ||
| 245 | |||
| 246 | $fh = FAST::Bio::SeqIO->newFh(-fh => \*FILEHANDLE, -format=>$format); | ||
| 247 | $fh = FAST::Bio::SeqIO->newFh(-format => $format); | ||
| 248 | # etc. | ||
| 249 | |||
| 250 | This constructor behaves like new(), but returns a tied filehandle | ||
| 251 | rather than a FAST::Bio::SeqIO object. You can read sequences from this | ||
| 252 | object using the familiar E<lt>E<gt> operator, and write to it using | ||
| 253 | print(). The usual array and $_ semantics work. For example, you can | ||
| 254 | read all sequence objects into an array like this: | ||
| 255 | |||
| 256 | @sequences = <$fh>; | ||
| 257 | |||
| 258 | Other operations, such as read(), sysread(), write(), close(), and | ||
| 259 | printf() are not supported. | ||
| 260 | |||
| 261 | =head1 OBJECT METHODS | ||
| 262 | |||
| 263 | See below for more detailed summaries. The main methods are: | ||
| 264 | |||
| 265 | =head2 $sequence = $seqIO-E<gt>next_seq() | ||
| 266 | |||
| 267 | Fetch the next sequence from the stream, or nothing if no more. | ||
| 268 | |||
| 269 | =head2 $seqIO-E<gt>write_seq($sequence [,$another_sequence,...]) | ||
| 270 | |||
| 271 | Write the specified sequence(s) to the stream. | ||
| 272 | |||
| 273 | =head2 TIEHANDLE(), READLINE(), PRINT() | ||
| 274 | |||
| 275 | These provide the tie interface. See L<perltie> for more details. | ||
| 276 | |||
| 277 | =head1 FEEDBACK | ||
| 278 | |||
| 279 | =head2 Mailing Lists | ||
| 280 | |||
| 281 | User feedback is an integral part of the evolution of this and other | ||
| 282 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 283 | of the Bioperl mailing lists. | ||
| 284 | |||
| 285 | Your participation is much appreciated. | ||
| 286 | |||
| 287 | bioperl-l@bioperl.org - General discussion | ||
| 288 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 289 | |||
| 290 | =head2 Support | ||
| 291 | |||
| 292 | Please direct usage questions or support issues to the mailing list: | ||
| 293 | |||
| 294 | bioperl-l@bioperl.org | ||
| 295 | |||
| 296 | rather than to the module maintainer directly. Many experienced and | ||
| 297 | responsive experts will be able look at the problem and quickly | ||
| 298 | address it. Please include a thorough description of the problem | ||
| 299 | with code and data examples if at all possible. | ||
| 300 | |||
| 301 | =head2 Reporting Bugs | ||
| 302 | |||
| 303 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 304 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 305 | web: | ||
| 306 | |||
| 307 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 308 | |||
| 309 | =head1 AUTHOR - Ewan Birney, Lincoln Stein | ||
| 310 | |||
| 311 | Email birney@ebi.ac.uk | ||
| 312 | lstein@cshl.org | ||
| 313 | |||
| 314 | =head1 APPENDIX | ||
| 315 | |||
| 316 | The rest of the documentation details each of the object | ||
| 317 | methods. Internal methods are usually preceded with a _ | ||
| 318 | |||
| 319 | =cut | ||
| 320 | |||
| 321 | #' Let the code begin... | ||
| 322 | |||
| 323 | package FAST::Bio::SeqIO; | ||
| 324 | |||
| 325 | use strict; | ||
| 326 | use warnings; | ||
| 327 | |||
| 328 | use FAST::Bio::Factory::FTLocationFactory; | ||
| 329 | use FAST::Bio::Seq::SeqBuilder; | ||
| 330 | use FAST::Bio::Tools::GuessSeqFormat; | ||
| 331 | use Symbol; | ||
| 332 | |||
| 333 | use parent qw(FAST::Bio::Root::Root FAST::Bio::Root::IO FAST::Bio::Factory::SequenceStreamI); | ||
| 334 | |||
| 335 | my %valid_alphabet_cache; | ||
| 336 | |||
| 337 | |||
| 338 | =head2 new | ||
| 339 | |||
| 340 | Title : new | ||
| 341 | Usage : $stream = FAST::Bio::SeqIO->new(-file => 'sequences.fasta', | ||
| 342 | -format => 'fasta'); | ||
| 343 | Function: Returns a new sequence stream | ||
| 344 | Returns : A FAST::Bio::SeqIO stream initialised with the appropriate format | ||
| 345 | Args : Named parameters: | ||
| 346 | -file => filename | ||
| 347 | -fh => filehandle to attach to | ||
| 348 | -format => format | ||
| 349 | |||
| 350 | Additional arguments may be used. They all have reasonable defaults | ||
| 351 | and are thus optional. | ||
| 352 | -alphabet => 'dna', 'rna', or 'protein' | ||
| 353 | -flush => 0 or 1 (default, flush filehandles after each write) | ||
| 354 | -seqfactory => sequence factory | ||
| 355 | -locfactory => location factory | ||
| 356 | -objbuilder => object builder | ||
| 357 | |||
| 358 | See L<FAST::Bio::SeqIO::Handler> | ||
| 359 | |||
| 360 | =cut | ||
| 361 | |||
| 362 | my $entry = 0; | ||
| 363 | |||
| 364 | sub new { | ||
| 365 | my ($caller,@args) = @_; | ||
| 366 | my $class = ref($caller) || $caller; | ||
| 367 | |||
| 368 | # or do we want to call SUPER on an object if $caller is an | ||
| 369 | # object? | ||
| 370 | if( $class =~ /FAST::Bio::SeqIO::(\S+)/ ) { | ||
| 371 | my ($self) = $class->SUPER::new(@args); | ||
| 372 | $self->_initialize(@args); | ||
| 373 | return $self; | ||
| 374 | } else { | ||
| 375 | |||
| 376 | my %param = @args; | ||
| 377 | @param{ map { lc $_ } keys %param } = values %param; # lowercase keys | ||
| 378 | |||
| 379 | unless( defined $param{-file} || | ||
| 380 | defined $param{-fh} || | ||
| 381 | defined $param{-string} ) { | ||
| 382 | $class->throw("file argument provided, but with an undefined value") | ||
| 383 | if exists $param{'-file'}; | ||
| 384 | $class->throw("fh argument provided, but with an undefined value") | ||
| 385 | if exists $param{'-fh'}; | ||
| 386 | $class->throw("string argument provided, but with an undefined value") | ||
| 387 | if exists($param{'-string'}); | ||
| 388 | # $class->throw("No file, fh, or string argument provided"); # neither defined | ||
| 389 | } | ||
| 390 | |||
| 391 | my $format = $param{'-format'} || | ||
| 392 | $class->_guess_format( $param{-file} || $ARGV[0] ); | ||
| 393 | |||
| 394 | if( ! $format ) { | ||
| 395 | if ($param{-file}) { | ||
| 396 | $format = FAST::Bio::Tools::GuessSeqFormat->new(-file => $param{-file}||$ARGV[0] )->guess; | ||
| 397 | } elsif ($param{-fh}) { | ||
| 398 | $format = FAST::Bio::Tools::GuessSeqFormat->new(-fh => $param{-fh}||$ARGV[0] )->guess; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | # changed 1-3-11; no need to print out an empty string (only way this | ||
| 402 | # exception is triggered) - cjfields | ||
| 403 | $class->throw("Could not guess format from file/fh") unless $format; | ||
| 404 | $format = "\L$format"; # normalize capitalization to lower case | ||
| 405 | |||
| 406 | if ($format =~ /-/) { | ||
| 407 | ($format, my $variant) = split('-', $format, 2); | ||
| 408 | push @args, (-variant => $variant); | ||
| 409 | } | ||
| 410 | |||
| 411 | |||
| 412 | return unless( $class->_load_format_module($format) ); | ||
| 413 | return "FAST::Bio::SeqIO::$format"->new(@args); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | |||
| 418 | =head2 newFh | ||
| 419 | |||
| 420 | Title : newFh | ||
| 421 | Usage : $fh = FAST::Bio::SeqIO->newFh(-file=>$filename,-format=>'Format') | ||
| 422 | Function: Does a new() followed by an fh() | ||
| 423 | Example : $fh = FAST::Bio::SeqIO->newFh(-file=>$filename,-format=>'Format') | ||
| 424 | $sequence = <$fh>; # read a sequence object | ||
| 425 | print $fh $sequence; # write a sequence object | ||
| 426 | Returns : filehandle tied to the FAST::Bio::SeqIO::Fh class | ||
| 427 | Args : | ||
| 428 | |||
| 429 | See L<FAST::Bio::SeqIO::Fh> | ||
| 430 | |||
| 431 | =cut | ||
| 432 | |||
| 433 | sub newFh { | ||
| 434 | my $class = shift; | ||
| 435 | return unless my $self = $class->new(@_); | ||
| 436 | return $self->fh; | ||
| 437 | } | ||
| 438 | |||
| 439 | |||
| 440 | =head2 fh | ||
| 441 | |||
| 442 | Title : fh | ||
| 443 | Usage : $obj->fh | ||
| 444 | Function: Get or set the IO filehandle | ||
| 445 | Example : $fh = $obj->fh; # make a tied filehandle | ||
| 446 | $sequence = <$fh>; # read a sequence object | ||
| 447 | print $fh $sequence; # write a sequence object | ||
| 448 | Returns : filehandle tied to FAST::Bio::SeqIO class | ||
| 449 | Args : none | ||
| 450 | |||
| 451 | =cut | ||
| 452 | |||
| 453 | sub fh { | ||
| 454 | my $self = shift; | ||
| 455 | my $class = ref($self) || $self; | ||
| 456 | my $s = Symbol::gensym; | ||
| 457 | tie $$s,$class,$self; | ||
| 458 | return $s; | ||
| 459 | } | ||
| 460 | |||
| 461 | |||
| 462 | # _initialize is chained for all SeqIO classes | ||
| 463 | |||
| 464 | sub _initialize { | ||
| 465 | my($self, @args) = @_; | ||
| 466 | |||
| 467 | # flush is initialized by the Root::IO init | ||
| 468 | |||
| 469 | my ($seqfact,$locfact,$objbuilder, $alphabet) = | ||
| 470 | $self->_rearrange([qw(SEQFACTORY | ||
| 471 | LOCFACTORY | ||
| 472 | OBJBUILDER | ||
| 473 | ALPHABET) | ||
| 474 | ], @args); | ||
| 475 | |||
| 476 | $locfact = FAST::Bio::Factory::FTLocationFactory->new(-verbose => $self->verbose) | ||
| 477 | if ! $locfact; | ||
| 478 | $objbuilder = FAST::Bio::Seq::SeqBuilder->new(-verbose => $self->verbose) | ||
| 479 | unless $objbuilder; | ||
| 480 | $self->sequence_builder($objbuilder); | ||
| 481 | $self->location_factory($locfact); | ||
| 482 | |||
| 483 | # note that this should come last because it propagates the sequence | ||
| 484 | # factory to the sequence builder | ||
| 485 | $seqfact && $self->sequence_factory($seqfact); | ||
| 486 | |||
| 487 | #bug 2160 | ||
| 488 | $alphabet && $self->alphabet($alphabet); | ||
| 489 | |||
| 490 | # initialize the IO part | ||
| 491 | $self->_initialize_io(@args); | ||
| 492 | } | ||
| 493 | |||
| 494 | |||
| 495 | =head2 next_seq | ||
| 496 | |||
| 497 | Title : next_seq | ||
| 498 | Usage : $seq = stream->next_seq | ||
| 499 | Function: Reads the next sequence object from the stream and returns it. | ||
| 500 | |||
| 501 | Certain driver modules may encounter entries in the stream | ||
| 502 | that are either misformatted or that use syntax not yet | ||
| 503 | understood by the driver. If such an incident is | ||
| 504 | recoverable, e.g., by dismissing a feature of a feature | ||
| 505 | table or some other non-mandatory part of an entry, the | ||
| 506 | driver will issue a warning. In the case of a | ||
| 507 | non-recoverable situation an exception will be thrown. Do | ||
| 508 | not assume that you can resume parsing the same stream | ||
| 509 | after catching the exception. Note that you can always turn | ||
| 510 | recoverable errors into exceptions by calling | ||
| 511 | $stream->verbose(2). | ||
| 512 | |||
| 513 | Returns : a FAST::Bio::Seq sequence object, or nothing if no more sequences | ||
| 514 | are available | ||
| 515 | |||
| 516 | Args : none | ||
| 517 | |||
| 518 | See L<FAST::Bio::Root::RootI>, L<FAST::Bio::Factory::SeqStreamI>, L<FAST::Bio::Seq> | ||
| 519 | |||
| 520 | =cut | ||
| 521 | |||
| 522 | sub next_seq { | ||
| 523 | my ($self, $seq) = @_; | ||
| 524 | $self->throw("Sorry, you cannot read from a generic FAST::Bio::SeqIO object."); | ||
| 525 | } | ||
| 526 | |||
| 527 | |||
| 528 | =head2 write_seq | ||
| 529 | |||
| 530 | Title : write_seq | ||
| 531 | Usage : $stream->write_seq($seq) | ||
| 532 | Function: writes the $seq object into the stream | ||
| 533 | Returns : 1 for success and 0 for error | ||
| 534 | Args : FAST::Bio::Seq object | ||
| 535 | |||
| 536 | =cut | ||
| 537 | |||
| 538 | sub write_seq { | ||
| 539 | my ($self, $seq) = @_; | ||
| 540 | $self->throw("Sorry, you cannot write to a generic FAST::Bio::SeqIO object."); | ||
| 541 | } | ||
| 542 | |||
| 543 | |||
| 544 | =head2 format | ||
| 545 | |||
| 546 | Title : format | ||
| 547 | Usage : $format = $stream->format() | ||
| 548 | Function: Get the sequence format | ||
| 549 | Returns : sequence format, e.g. fasta, fastq | ||
| 550 | Args : none | ||
| 551 | |||
| 552 | =cut | ||
| 553 | |||
| 554 | # format() method inherited from FAST::Bio::Root::IO | ||
| 555 | |||
| 556 | |||
| 557 | =head2 alphabet | ||
| 558 | |||
| 559 | Title : alphabet | ||
| 560 | Usage : $self->alphabet($newval) | ||
| 561 | Function: Set/get the molecule type for the Seq objects to be created. | ||
| 562 | Example : $seqio->alphabet('protein') | ||
| 563 | Returns : value of alphabet: 'dna', 'rna', or 'protein' | ||
| 564 | Args : newvalue (optional) | ||
| 565 | Throws : Exception if the argument is not one of 'dna', 'rna', or 'protein' | ||
| 566 | |||
| 567 | =cut | ||
| 568 | |||
| 569 | sub alphabet { | ||
| 570 | my ($self, $value) = @_; | ||
| 571 | |||
| 572 | if ( defined $value) { | ||
| 573 | $value = lc $value; | ||
| 574 | unless ($valid_alphabet_cache{$value}) { | ||
| 575 | # instead of hard-coding the allowed values once more, we check by | ||
| 576 | # creating a dummy sequence object | ||
| 577 | eval { | ||
| 578 | require FAST::Bio::PrimarySeq; | ||
| 579 | my $seq = FAST::Bio::PrimarySeq->new('-verbose' => $self->verbose, | ||
| 580 | '-alphabet' => $value); | ||
| 581 | }; | ||
| 582 | if ($@) { | ||
| 583 | $self->throw("Invalid alphabet: $value\n. See FAST::Bio::PrimarySeq for allowed values."); | ||
| 584 | } | ||
| 585 | $valid_alphabet_cache{$value} = 1; | ||
| 586 | } | ||
| 587 | $self->{'alphabet'} = $value; | ||
| 588 | } | ||
| 589 | return $self->{'alphabet'}; | ||
| 590 | } | ||
| 591 | |||
| 592 | |||
| 593 | =head2 _load_format_module | ||
| 594 | |||
| 595 | Title : _load_format_module | ||
| 596 | Usage : *INTERNAL SeqIO stuff* | ||
| 597 | Function: Loads up (like use) a module at run time on demand | ||
| 598 | Example : | ||
| 599 | Returns : | ||
| 600 | Args : | ||
| 601 | |||
| 602 | =cut | ||
| 603 | |||
| 604 | sub _load_format_module { | ||
| 605 | my ($self, $format) = @_; | ||
| 606 | my $module = "FAST::Bio::SeqIO::" . $format; | ||
| 607 | my $ok; | ||
| 608 | |||
| 609 | eval { | ||
| 610 | $ok = $self->_load_module($module); | ||
| 611 | }; | ||
| 612 | if ( $@ ) { | ||
| 613 | print STDERR <<END; | ||
| 614 | $self: $format cannot be found | ||
| 615 | Exception $@ | ||
| 616 | For more information about the SeqIO system please see the SeqIO docs. | ||
| 617 | This includes ways of checking for formats at compile time, not run time | ||
| 618 | END | ||
| 619 | ; | ||
| 620 | } | ||
| 621 | return $ok; | ||
| 622 | } | ||
| 623 | |||
| 624 | |||
| 625 | =head2 _concatenate_lines | ||
| 626 | |||
| 627 | Title : _concatenate_lines | ||
| 628 | Usage : $s = _concatenate_lines($line, $continuation_line) | ||
| 629 | Function: Private. Concatenates two strings assuming that the second stems | ||
| 630 | from a continuation line of the first. Adds a space between both | ||
| 631 | unless the first ends with a dash. | ||
| 632 | |||
| 633 | Takes care of either arg being empty. | ||
| 634 | Example : | ||
| 635 | Returns : A string. | ||
| 636 | Args : | ||
| 637 | |||
| 638 | =cut | ||
| 639 | |||
| 640 | sub _concatenate_lines { | ||
| 641 | my ($self, $s1, $s2) = @_; | ||
| 642 | $s1 .= " " if($s1 && ($s1 !~ /-$/) && $s2); | ||
| 643 | return ($s1 ? $s1 : "") . ($s2 ? $s2 : ""); | ||
| 644 | } | ||
| 645 | |||
| 646 | |||
| 647 | =head2 _filehandle | ||
| 648 | |||
| 649 | Title : _filehandle | ||
| 650 | Usage : $obj->_filehandle($newval) | ||
| 651 | Function: This method is deprecated. Call _fh() instead. | ||
| 652 | Example : | ||
| 653 | Returns : value of _filehandle | ||
| 654 | Args : newvalue (optional) | ||
| 655 | |||
| 656 | =cut | ||
| 657 | |||
| 658 | sub _filehandle { | ||
| 659 | my ($self,@args) = @_; | ||
| 660 | return $self->_fh(@args); | ||
| 661 | } | ||
| 662 | |||
| 663 | |||
| 664 | =head2 _guess_format | ||
| 665 | |||
| 666 | Title : _guess_format | ||
| 667 | Usage : $obj->_guess_format($filename) | ||
| 668 | Function: guess format based on file suffix | ||
| 669 | Example : | ||
| 670 | Returns : guessed format of filename (lower case) | ||
| 671 | Args : | ||
| 672 | Notes : formats that _filehandle() will guess include fasta, | ||
| 673 | genbank, scf, pir, embl, raw, gcg, ace, bsml, swissprot, | ||
| 674 | fastq and phd/phred | ||
| 675 | |||
| 676 | =cut | ||
| 677 | |||
| 678 | sub _guess_format { | ||
| 679 | my $class = shift; | ||
| 680 | return unless $_ = shift; | ||
| 681 | return 'abi' if /\.ab[i1]$/i; | ||
| 682 | return 'ace' if /\.ace$/i; | ||
| 683 | return 'alf' if /\.alf$/i; | ||
| 684 | return 'bsml' if /\.(bsm|bsml)$/i; | ||
| 685 | return 'ctf' if /\.ctf$/i; | ||
| 686 | return 'embl' if /\.(embl|ebl|emb|dat)$/i; | ||
| 687 | return 'entrezgene' if /\.asn$/i; | ||
| 688 | return 'exp' if /\.exp$/i; | ||
| 689 | return 'fasta' if /\.(fasta|fast|fas|seq|fa|fsa|nt|aa|fna|faa)$/i; | ||
| 690 | return 'fastq' if /\.fastq$/i; | ||
| 691 | return 'gcg' if /\.gcg$/i; | ||
| 692 | return 'genbank' if /\.(gb|gbank|genbank|gbk|gbs)$/i; | ||
| 693 | return 'phd' if /\.(phd|phred)$/i; | ||
| 694 | return 'pir' if /\.pir$/i; | ||
| 695 | return 'pln' if /\.pln$/i; | ||
| 696 | return 'qual' if /\.qual$/i; | ||
| 697 | return 'raw' if /\.txt$/i; | ||
| 698 | return 'scf' if /\.scf$/i; | ||
| 699 | return 'swiss' if /\.(swiss|sp)$/i; | ||
| 700 | |||
| 701 | # from Strider 1.4 Release Notes: The file name extensions used by | ||
| 702 | # Strider 1.4 are ".xdna", ".xdgn", ".xrna" and ".xprt" for DNA, | ||
| 703 | # DNA Degenerate, RNA and Protein Sequence Files, respectively | ||
| 704 | return 'strider' if /\.(xdna|xdgn|xrna|xprt)$/i; | ||
| 705 | |||
| 706 | return 'ztr' if /\.ztr$/i; | ||
| 707 | } | ||
| 708 | |||
| 709 | |||
| 710 | sub DESTROY { | ||
| 711 | my $self = shift; | ||
| 712 | $self->close(); | ||
| 713 | } | ||
| 714 | |||
| 715 | |||
| 716 | sub TIEHANDLE { | ||
| 717 | my ($class,$val) = @_; | ||
| 718 | return bless {'seqio' => $val}, $class; | ||
| 719 | } | ||
| 720 | |||
| 721 | |||
| 722 | sub READLINE { | ||
| 723 | my $self = shift; | ||
| 724 | return $self->{'seqio'}->next_seq() unless wantarray; | ||
| 725 | my (@list, $obj); | ||
| 726 | push @list, $obj while $obj = $self->{'seqio'}->next_seq(); | ||
| 727 | return @list; | ||
| 728 | } | ||
| 729 | |||
| 730 | |||
| 731 | sub PRINT { | ||
| 732 | my $self = shift; | ||
| 733 | $self->{'seqio'}->write_seq(@_); | ||
| 734 | } | ||
| 735 | |||
| 736 | |||
| 737 | =head2 sequence_factory | ||
| 738 | |||
| 739 | Title : sequence_factory | ||
| 740 | Usage : $seqio->sequence_factory($seqfactory) | ||
| 741 | Function: Get/Set the FAST::Bio::Factory::SequenceFactoryI | ||
| 742 | Returns : FAST::Bio::Factory::SequenceFactoryI | ||
| 743 | Args : [optional] FAST::Bio::Factory::SequenceFactoryI | ||
| 744 | |||
| 745 | =cut | ||
| 746 | |||
| 747 | sub sequence_factory { | ||
| 748 | my ($self, $obj) = @_; | ||
| 749 | if( defined $obj ) { | ||
| 750 | if( ! ref($obj) || ! $obj->isa('FAST::Bio::Factory::SequenceFactoryI') ) { | ||
| 751 | $self->throw("Must provide a valid FAST::Bio::Factory::SequenceFactoryI object to ".ref($self)."::sequence_factory()"); | ||
| 752 | } | ||
| 753 | $self->{'_seqio_seqfactory'} = $obj; | ||
| 754 | my $builder = $self->sequence_builder(); | ||
| 755 | if($builder && $builder->can('sequence_factory') && | ||
| 756 | (! $builder->sequence_factory())) { | ||
| 757 | $builder->sequence_factory($obj); | ||
| 758 | } | ||
| 759 | } | ||
| 760 | $self->{'_seqio_seqfactory'}; | ||
| 761 | } | ||
| 762 | |||
| 763 | |||
| 764 | =head2 object_factory | ||
| 765 | |||
| 766 | Title : object_factory | ||
| 767 | Usage : $obj->object_factory($newval) | ||
| 768 | Function: This is an alias to sequence_factory with a more generic name. | ||
| 769 | Example : | ||
| 770 | Returns : value of object_factory (a scalar) | ||
| 771 | Args : on set, new value (a scalar or undef, optional) | ||
| 772 | |||
| 773 | =cut | ||
| 774 | |||
| 775 | sub object_factory{ | ||
| 776 | return shift->sequence_factory(@_); | ||
| 777 | } | ||
| 778 | |||
| 779 | |||
| 780 | =head2 sequence_builder | ||
| 781 | |||
| 782 | Title : sequence_builder | ||
| 783 | Usage : $seqio->sequence_builder($seqfactory) | ||
| 784 | Function: Get/Set the FAST::Bio::Factory::ObjectBuilderI used to build sequence | ||
| 785 | objects. This applies to rich sequence formats only, e.g. genbank | ||
| 786 | but not fasta. | ||
| 787 | |||
| 788 | If you do not set the sequence object builder yourself, it | ||
| 789 | will in fact be an instance of L<FAST::Bio::Seq::SeqBuilder>, and | ||
| 790 | you may use all methods documented there to configure it. | ||
| 791 | |||
| 792 | Returns : a FAST::Bio::Factory::ObjectBuilderI compliant object | ||
| 793 | Args : [optional] a FAST::Bio::Factory::ObjectBuilderI compliant object | ||
| 794 | |||
| 795 | =cut | ||
| 796 | |||
| 797 | sub sequence_builder { | ||
| 798 | my ($self, $obj) = @_; | ||
| 799 | if( defined $obj ) { | ||
| 800 | if( ! ref($obj) || ! $obj->isa('FAST::Bio::Factory::ObjectBuilderI') ) { | ||
| 801 | $self->throw("Must provide a valid FAST::Bio::Factory::ObjectBuilderI object to ".ref($self)."::sequence_builder()"); | ||
| 802 | } | ||
| 803 | $self->{'_object_builder'} = $obj; | ||
| 804 | } | ||
| 805 | $self->{'_object_builder'}; | ||
| 806 | } | ||
| 807 | |||
| 808 | |||
| 809 | =head2 location_factory | ||
| 810 | |||
| 811 | Title : location_factory | ||
| 812 | Usage : $seqio->location_factory($locfactory) | ||
| 813 | Function: Get/Set the FAST::Bio::Factory::LocationFactoryI object to be used for | ||
| 814 | location string parsing | ||
| 815 | Returns : a FAST::Bio::Factory::LocationFactoryI implementing object | ||
| 816 | Args : [optional] on set, a FAST::Bio::Factory::LocationFactoryI implementing | ||
| 817 | object. | ||
| 818 | |||
| 819 | =cut | ||
| 820 | |||
| 821 | sub location_factory { | ||
| 822 | my ($self,$obj) = @_; | ||
| 823 | if( defined $obj ) { | ||
| 824 | if( ! ref($obj) || ! $obj->isa('FAST::Bio::Factory::LocationFactoryI') ) { | ||
| 825 | $self->throw("Must provide a valid FAST::Bio::Factory::LocationFactoryI" . | ||
| 826 | " object to ".ref($self)."->location_factory()"); | ||
| 827 | } | ||
| 828 | $self->{'_seqio_locfactory'} = $obj; | ||
| 829 | } | ||
| 830 | $self->{'_seqio_locfactory'}; | ||
| 831 | } | ||
| 832 | |||
| 833 | 1; | ||
| 834 |