sync with trunk (to r15946)
[bioperl-live.git] / Bio / SeqIO / raw.pm
blobc37e512031403c4f8046cbb10a2fe3be42d0aac8
1 #-----------------------------------------------------------------------------
2 # PACKAGE : Bio::SeqIO::raw
3 # AUTHOR : Ewan Birney <birney@ebi.ac.uk>
4 # CREATED : Feb 16 1999
5 # REVISION: $Id$
7 # Copyright (c) 1997-9 bioperl, Ewan Birney. All Rights Reserved.
8 # This module is free software; you can redistribute it and/or
9 # modify it under the same terms as Perl itself.
11 # _History_
13 # Ewan Birney <birney@ebi.ac.uk> developed the SeqIO
14 # schema and the first prototype modules.
16 # This code is based on his Bio::SeqIO::Fasta module with
17 # the necessary minor tweaks necessary to get it to read
18 # and write raw formatted sequences made by
19 # chris dagdigian <dag@sonsorol.org>
21 # October 18, 1999 Largely rewritten by Lincoln Stein
23 # Copyright Ewan Birney
25 # You may distribute this module under the same terms as perl itself
27 # POD documentation - main docs before the code
29 =head1 NAME
31 Bio::SeqIO::raw - raw sequence file input/output stream
33 =head1 SYNOPSIS
35 Do not use this module directly. Use it via the L<Bio::SeqIO> class.
37 =head1 DESCRIPTION
39 This object can transform Bio::Seq objects to and from raw flat
40 file databases.
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 one
48 of the Bioperl mailing lists. 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 the bugs and their resolution.
68 Bug reports can be submitted via the web:
70 http://bugzilla.open-bio.org/
72 =head1 AUTHORS
74 Ewan Birney E<lt>birney@ebi.ac.ukE<gt>
75 Lincoln Stein E<lt>lstein@cshl.orgE<gt>
77 =head1 CONTRIBUTORS
79 Jason Stajich E<lt>jason@bioperl.org<gt>
81 =head1 APPENDIX
83 The rest of the documentation details each of the object methods.
84 Internal methods are usually preceded with a _
86 =cut
89 # Let the code begin...
91 package Bio::SeqIO::raw;
92 use strict;
94 use Bio::Seq::SeqFactory;
96 use base qw(Bio::SeqIO);
98 sub _initialize {
99 my($self,@args) = @_;
100 #$self->{record_sep} = (defined $gulp) ? undef : $/;
101 $self->SUPER::_initialize(@args);
102 if( ! defined $self->sequence_factory ) {
103 $self->sequence_factory(Bio::Seq::SeqFactory->new
104 (-verbose => $self->verbose(),
105 -type => 'Bio::Seq'));
107 $self->variant;
110 =head2 next_seq
112 Title : next_seq
113 Usage : $seq = $stream->next_seq()
114 Function: returns the next sequence in the stream
115 Returns : Bio::Seq object
116 Args :
119 =cut
121 sub next_seq{
122 my ($self,@args) = @_;
123 ## When its 1 sequence per line with no formatting at all,
124 ## grabbing it should be easy :)
126 ## adding an option to assume the file is one sequence
127 local $/ = $self->{record_separator};
128 my $nextline = $self->_readline();
129 return unless defined $nextline;
131 my $sequence = uc($nextline);
132 $sequence =~ s/\W//g;
133 return unless $sequence;
134 return $self->sequence_factory->create(-seq => $sequence) if $sequence;
137 =head2 write_seq
139 Title : write_seq
140 Usage : $stream->write_seq($seq)
141 Function: writes the $seq object into the stream
142 Returns : 1 for success and 0 for error
143 Args : Array of Bio::PrimarySeqI objects
146 =cut
148 sub write_seq {
149 my ($self,@seq) = @_;
150 foreach my $seq (@seq) {
151 $self->throw("Must provide a valid Bio::PrimarySeqI object")
152 unless defined $seq && ref($seq) && $seq->isa('Bio::PrimarySeqI');
153 $self->_print($seq->seq, "\n") or return;
155 $self->flush if $self->_flush_on_write && defined $self->_fh;
156 return 1;
159 =head2 write_qual
161 Title : write_qual
162 Usage : $stream->write_qual($seq)
163 Function: writes the $seq object into the stream
164 Returns : 1 for success and 0 for error
165 Args : Bio::Seq::Quality object
168 =cut
170 sub write_qual {
171 my ($self,@seq) = @_;
172 my @qual = ();
173 foreach (@seq) {
174 unless ($_->isa("Bio::Seq::Quality")){
175 warn("You cannot write raw qualities without supplying a Bio::Seq::Quality object! You passed a ", ref($_), "\n");
176 next;
178 @qual = @{$_->qual};
179 if(scalar(@qual) == 0) {
180 $qual[0] = "\n";
182 $self->_print (join " ", @qual,"\n") or return;
184 return 1;
187 =head2 variant
189 Title : variant
190 Usage : $format = $obj->variant();
191 Function: Get and set method for the sequence variant. For raw sequence, this
192 indicates whether to treat the input as multiple sequences (the
193 default) or as a single sequence.
195 Current values accepted are:
197 'single' single sequence
198 'multiple' multiple sequences (default)
199 Returns : string
200 Args : new value, string
202 =cut
204 sub variant {
205 my ($self, $var) = @_;
206 if (defined $var || !defined $self->{variant}) {
207 $var ||= 'multiple';
208 $var = lc $var;
209 $self->throw("Accepted raw format variants: 'single', 'multiple'") unless
210 $var eq 'single' || $var eq 'multiple';
211 $self->{record_separator} = $var eq 'single' ? undef : $/;
212 $self->{variant} = $var;
214 return $self->{variant};
217 # private method for testing record separator
219 sub _separator {
220 shift->{record_separator};