| 1 | #----------------------------------------------------------------------------- | ||
| 2 | # PACKAGE : FAST::Bio::SeqIO::raw | ||
| 3 | # AUTHOR : Ewan Birney <birney@ebi.ac.uk> | ||
| 4 | # CREATED : Feb 16 1999 | ||
| 5 | # | ||
| 6 | # Copyright (c) 1997-9 bioperl, Ewan Birney. All Rights Reserved. | ||
| 7 | # This module is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | # | ||
| 10 | # _History_ | ||
| 11 | # | ||
| 12 | # Ewan Birney <birney@ebi.ac.uk> developed the SeqIO | ||
| 13 | # schema and the first prototype modules. | ||
| 14 | # | ||
| 15 | # This code is based on his FAST::Bio::SeqIO::Fasta module with | ||
| 16 | # the necessary minor tweaks necessary to get it to read | ||
| 17 | # and write raw formatted sequences made by | ||
| 18 | # chris dagdigian <dag@sonsorol.org> | ||
| 19 | # | ||
| 20 | # October 18, 1999 Largely rewritten by Lincoln Stein | ||
| 21 | # | ||
| 22 | # Copyright Ewan Birney | ||
| 23 | # | ||
| 24 | # You may distribute this module under the same terms as perl itself | ||
| 25 | |||
| 26 | # POD documentation - main docs before the code | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | FAST::Bio::SeqIO::raw - raw sequence file input/output stream | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | Do not use this module directly. Use it via the L<FAST::Bio::SeqIO> class. | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | This object can transform FAST::Bio::Seq objects to and from raw flat | ||
| 39 | file databases. | ||
| 40 | |||
| 41 | =head1 FEEDBACK | ||
| 42 | |||
| 43 | =head2 Mailing Lists | ||
| 44 | |||
| 45 | User feedback is an integral part of the evolution of this and other | ||
| 46 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 47 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 48 | |||
| 49 | bioperl-l@bioperl.org - General discussion | ||
| 50 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 51 | |||
| 52 | =head2 Support | ||
| 53 | |||
| 54 | Please direct usage questions or support issues to the mailing list: | ||
| 55 | |||
| 56 | I<bioperl-l@bioperl.org> | ||
| 57 | |||
| 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. | ||
| 62 | |||
| 63 | =head2 Reporting Bugs | ||
| 64 | |||
| 65 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 66 | the bugs and their resolution. | ||
| 67 | Bug reports can be submitted via the web: | ||
| 68 | |||
| 69 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 70 | |||
| 71 | =head1 AUTHORS | ||
| 72 | |||
| 73 | Ewan Birney E<lt>birney@ebi.ac.ukE<gt> | ||
| 74 | Lincoln Stein E<lt>lstein@cshl.orgE<gt> | ||
| 75 | |||
| 76 | =head1 CONTRIBUTORS | ||
| 77 | |||
| 78 | Jason Stajich E<lt>jason@bioperl.org<gt> | ||
| 79 | |||
| 80 | =head1 APPENDIX | ||
| 81 | |||
| 82 | The rest of the documentation details each of the object methods. | ||
| 83 | Internal methods are usually preceded with a _ | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | |||
| 88 | # Let the code begin... | ||
| 89 | |||
| 90 | package FAST::Bio::SeqIO::raw; | ||
| 91 | use strict; | ||
| 92 | |||
| 93 | use FAST::Bio::Seq::SeqFactory; | ||
| 94 | |||
| 95 | use base qw(FAST::Bio::SeqIO); | ||
| 96 | |||
| 97 | our %variant = ( 'multiple' => undef, # default | ||
| 98 | 'single' => undef ); | ||
| 99 | |||
| 100 | sub _initialize { | ||
| 101 | my($self,@args) = @_; | ||
| 102 | $self->SUPER::_initialize(@args); | ||
| 103 | my ($variant) = $self->_rearrange([qw(VARIANT)], @args); | ||
| 104 | $variant ||= 'multiple'; | ||
| 105 | $self->variant($variant); | ||
| 106 | $self->{record_separator} = $variant eq 'single' ? undef : $/; | ||
| 107 | if( ! defined $self->sequence_factory ) { | ||
| 108 | $self->sequence_factory(FAST::Bio::Seq::SeqFactory->new | ||
| 109 | (-verbose => $self->verbose(), | ||
| 110 | -type => 'FAST::Bio::Seq')); | ||
| 111 | } | ||
| 112 | $self->variant; | ||
| 113 | } | ||
| 114 | |||
| 115 | =head2 next_seq | ||
| 116 | |||
| 117 | Title : next_seq | ||
| 118 | Usage : $seq = $stream->next_seq() | ||
| 119 | Function: returns the next sequence in the stream | ||
| 120 | Returns : FAST::Bio::Seq object | ||
| 121 | Args : | ||
| 122 | |||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub next_seq{ | ||
| 127 | my ($self,@args) = @_; | ||
| 128 | ## When its 1 sequence per line with no formatting at all, | ||
| 129 | ## grabbing it should be easy :) | ||
| 130 | |||
| 131 | ## adding an option to assume the file is one sequence | ||
| 132 | local $/ = $self->{record_separator}; | ||
| 133 | my $nextline = $self->_readline(); | ||
| 134 | return unless defined $nextline; | ||
| 135 | |||
| 136 | my $sequence = uc($nextline); | ||
| 137 | $sequence =~ s/\W//g; | ||
| 138 | return unless $sequence; | ||
| 139 | return $self->sequence_factory->create(-seq => $sequence) if $sequence; | ||
| 140 | } | ||
| 141 | |||
| 142 | =head2 write_seq | ||
| 143 | |||
| 144 | Title : write_seq | ||
| 145 | Usage : $stream->write_seq($seq) | ||
| 146 | Function: writes the $seq object into the stream | ||
| 147 | Returns : 1 for success and 0 for error | ||
| 148 | Args : Array of FAST::Bio::PrimarySeqI objects | ||
| 149 | |||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | sub write_seq { | ||
| 154 | my ($self,@seq) = @_; | ||
| 155 | foreach my $seq (@seq) { | ||
| 156 | $self->throw("Must provide a valid FAST::Bio::PrimarySeqI object") | ||
| 157 | unless defined $seq && ref($seq) && $seq->isa('FAST::Bio::PrimarySeqI'); | ||
| 158 | $self->_print($seq->seq, "\n") or return; | ||
| 159 | } | ||
| 160 | $self->flush if $self->_flush_on_write && defined $self->_fh; | ||
| 161 | return 1; | ||
| 162 | } | ||
| 163 | |||
| 164 | =head2 write_qual | ||
| 165 | |||
| 166 | Title : write_qual | ||
| 167 | Usage : $stream->write_qual($seq) | ||
| 168 | Function: writes the $seq object into the stream | ||
| 169 | Returns : 1 for success and 0 for error | ||
| 170 | Args : FAST::Bio::Seq::Quality object | ||
| 171 | |||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | sub write_qual { | ||
| 176 | my ($self,@seq) = @_; | ||
| 177 | my @qual = (); | ||
| 178 | foreach (@seq) { | ||
| 179 | unless ($_->isa("FAST::Bio::Seq::Quality")){ | ||
| 180 | warn("You cannot write raw qualities without supplying a FAST::Bio::Seq::". | ||
| 181 | "Quality object! You passed a ".ref($_)."\n"); | ||
| 182 | next; | ||
| 183 | } | ||
| 184 | @qual = @{$_->qual}; | ||
| 185 | if(scalar(@qual) == 0) { | ||
| 186 | $qual[0] = "\n"; | ||
| 187 | } | ||
| 188 | $self->_print (join " ", @qual,"\n") or return; | ||
| 189 | } | ||
| 190 | return 1; | ||
| 191 | } | ||
| 192 | |||
| 193 | =head2 variant | ||
| 194 | |||
| 195 | Title : variant | ||
| 196 | Usage : $format = $obj->variant(); | ||
| 197 | Function: Get and set method for the sequence variant. For raw sequence, this | ||
| 198 | indicates whether to treat the input as multiple sequences (the | ||
| 199 | default) or as a single sequence. | ||
| 200 | |||
| 201 | Current values accepted are: | ||
| 202 | |||
| 203 | 'single' single sequence | ||
| 204 | 'multiple' multiple sequences (default) | ||
| 205 | Returns : string | ||
| 206 | Args : new value, string | ||
| 207 | |||
| 208 | =cut | ||
| 209 | |||
| 210 | # variant() method inherited from FAST::Bio::Root::IO | ||
| 211 | |||
| 212 | # private method for testing record separator | ||
| 213 | |||
| 214 | sub _separator { | ||
| 215 | shift->{record_separator}; | ||
| 216 | } | ||
| 217 | |||
| 218 | 1; |