sync with main trunk completely (a few tests failing)
[bioperl-live.git] / Bio / SeqIO / raw.pm
blob506290f9a940e88de4928b4bb66d2155ec78152f
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->SUPER::_initialize(@args);
101 my ($variant) = $self->_rearrange([qw(VARIANT)], @args);
102 $variant ||= 'multiple';
103 $self->variant($variant);
104 if( ! defined $self->sequence_factory ) {
105 $self->sequence_factory(Bio::Seq::SeqFactory->new
106 (-verbose => $self->verbose(),
107 -type => 'Bio::Seq'));
109 $self->variant;
112 =head2 next_seq
114 Title : next_seq
115 Usage : $seq = $stream->next_seq()
116 Function: returns the next sequence in the stream
117 Returns : Bio::Seq object
118 Args :
121 =cut
123 sub next_seq{
124 my ($self,@args) = @_;
125 ## When its 1 sequence per line with no formatting at all,
126 ## grabbing it should be easy :)
128 ## adding an option to assume the file is one sequence
129 local $/ = $self->{record_separator};
130 my $nextline = $self->_readline();
131 return unless defined $nextline;
133 my $sequence = uc($nextline);
134 $sequence =~ s/\W//g;
135 return unless $sequence;
136 return $self->sequence_factory->create(-seq => $sequence) if $sequence;
139 =head2 write_seq
141 Title : write_seq
142 Usage : $stream->write_seq($seq)
143 Function: writes the $seq object into the stream
144 Returns : 1 for success and 0 for error
145 Args : Array of Bio::PrimarySeqI objects
148 =cut
150 sub write_seq {
151 my ($self,@seq) = @_;
152 foreach my $seq (@seq) {
153 $self->throw("Must provide a valid Bio::PrimarySeqI object")
154 unless defined $seq && ref($seq) && $seq->isa('Bio::PrimarySeqI');
155 $self->_print($seq->seq, "\n") or return;
157 $self->flush if $self->_flush_on_write && defined $self->_fh;
158 return 1;
161 =head2 write_qual
163 Title : write_qual
164 Usage : $stream->write_qual($seq)
165 Function: writes the $seq object into the stream
166 Returns : 1 for success and 0 for error
167 Args : Bio::Seq::Quality object
170 =cut
172 sub write_qual {
173 my ($self,@seq) = @_;
174 my @qual = ();
175 foreach (@seq) {
176 unless ($_->isa("Bio::Seq::Quality")){
177 warn("You cannot write raw qualities without supplying a Bio::Seq::Quality object! You passed a ", ref($_), "\n");
178 next;
180 @qual = @{$_->qual};
181 if(scalar(@qual) == 0) {
182 $qual[0] = "\n";
184 $self->_print (join " ", @qual,"\n") or return;
186 return 1;
189 =head2 variant
191 Title : variant
192 Usage : $format = $obj->variant();
193 Function: Get and set method for the sequence variant. For raw sequence, this
194 indicates whether to treat the input as multiple sequences (the
195 default) or as a single sequence.
197 Current values accepted are:
199 'single' single sequence
200 'multiple' multiple sequences (default)
201 Returns : string
202 Args : new value, string
204 =cut
206 sub variant {
207 my ($self, $var) = @_;
208 if (defined $var || !defined $self->{variant}) {
209 $var ||= 'multiple';
210 $var = lc $var;
211 $self->throw("Accepted raw format variants: 'single', 'multiple'") unless
212 $var eq 'single' || $var eq 'multiple';
213 $self->{record_separator} = $var eq 'single' ? undef : $/;
214 $self->{variant} = $var;
216 return $self->{variant};
219 # private method for testing record separator
221 sub _separator {
222 shift->{record_separator};