2 # BioPerl module for Bio::AlignIO::fasta
4 # Copyright Peter Schattner
6 # You may distribute this module under the same terms as perl itself
7 # POD documentation - main docs before the code
11 Bio::AlignIO::fasta - fasta MSA Sequence input/output stream
15 Do not use this module directly. Use it via the L<Bio::AlignIO>
20 This object can transform L<Bio::SimpleAlign> objects to and from
21 fasta flat files. This is for the fasta alignment format, not
22 for the FastA sequence analysis program. To process the alignments from
23 FastA (FastX, FastN, FastP, tFastA, etc) use the Bio::SearchIO module.
29 Please direct usage questions or support issues to the mailing list:
31 I<bioperl-l@bioperl.org>
33 rather than to the module maintainer directly. Many experienced and
34 reponsive experts will be able look at the problem and quickly
35 address it. Please include a thorough description of the problem
36 with code and data examples if at all possible.
40 Report bugs to the Bioperl bug tracking system to help us keep track
41 the bugs and their resolution. Bug reports can be submitted via the
44 https://github.com/bioperl/bioperl-live/issues
52 The rest of the documentation details each of the object
53 methods. Internal methods are usually preceded with a _
57 # Let the code begin...
59 package Bio
::AlignIO
::fasta
;
62 use base
qw(Bio::AlignIO);
64 use Bio
::LocatableSeq
;
69 Usage : $aln = $stream->next_aln
70 Function: returns the next alignment in the stream.
71 Returns : Bio::Align::AlignI object - returns 0 on end of file
73 Args : -width => optional argument to specify the width sequence
74 will be written (60 chars by default)
76 See L<Bio::Align::AlignI>
82 my ($width) = $self->_rearrange( [qw(WIDTH)], @_ );
83 $self->width( $width || $WIDTH );
85 my ($start, $end, $name, $seqname, $seq, $seqchar,
86 $entry, $tempname, $tempdesc, %align, $desc, $maxlen
88 my $aln = Bio
::SimpleAlign
->new();
90 while ( defined( $entry = $self->_readline ) ) {
92 if ( $entry =~ s/^>\s*(\S+)\s*// ) {
96 if ( defined $name ) {
100 $end = $self->_get_len($seqchar);
101 $seq = Bio
::LocatableSeq
->new(
103 -display_id
=> $seqname,
104 -description
=> $desc,
107 -alphabet
=> $self->alphabet,
110 $self->debug("Reading $seqname\n");
119 # removed redundant symbol validation
120 # this is already done in Bio::PrimarySeq
124 # Next two lines are to silence warnings that
125 # otherwise occur at EOF when using <$fh>
126 $name = "" if ( !defined $name );
127 $seqchar = "" if ( !defined $seqchar );
130 # Put away last name and sequence
131 if ( $name =~ /(\S+\/(\d
+)-(\d
+))$/ ) {
139 $end = $self->_get_len($seqchar);
142 # This logic now also reads empty lines at the
143 # end of the file. Skip this is seqchar and seqname is null
144 unless ( length($seqchar) == 0 && length($seqname) == 0 ) {
145 $seq = Bio
::LocatableSeq
->new(
147 -display_id
=> $seqname,
148 -description
=> $desc,
151 -alphabet
=> $self->alphabet,
154 $self->debug("Reading $seqname\n");
156 my $alnlen = $aln->length;
157 foreach my $seq ( $aln->each_seq ) {
158 if ( $seq->length < $alnlen ) {
159 my ($diff) = ( $alnlen - $seq->length );
160 $seq->seq( $seq->seq() . "-" x
$diff );
164 # no sequences means empty alignment (possible EOF)
165 return $aln if $aln->num_sequences;
173 Usage : $stream->write_aln(@aln)
174 Function: writes the $aln object into the stream in fasta format
175 Returns : 1 for success and 0 for error
176 Args : L<Bio::Align::AlignI> object
178 See L<Bio::Align::AlignI>
183 my ($self,@aln) = @_;
184 my $width = $self->width;
185 my ($seq,$desc,$rseq,$name,$count,$length,$seqsub);
187 foreach my $aln (@aln) {
188 if( ! $aln || ! $aln->isa('Bio::Align::AlignI') ) {
189 $self->warn("Must provide a Bio::Align::AlignI object when calling write_aln");
192 if( $self->force_displayname_flat ) {
193 $aln->set_displayname_flat(1);
195 foreach $rseq ( $aln->each_seq() ) {
196 $name = $aln->displayname($rseq->get_nse());
198 $desc = $rseq->description || '';
199 $desc = ' '.$desc if $desc;
200 $self->_print (">$name$desc\n") or return;
202 $length = length($seq);
203 if(defined $seq && $length > 0) {
204 $seq =~ s/(.{1,$width})/$1\n/g;
211 $self->flush if $self->_flush_on_write && defined $self->_fh;
219 Function: determine number of alphabetic chars
221 Args : sequence string
226 my ($self,$seq) = @_;
227 my $chars = $Bio::LocatableSeq
::GAP_SYMBOLS
.$Bio::LocatableSeq
::FRAMESHIFT_SYMBOLS
;
228 $seq =~ s{[$chars]+}{}gi;
229 return CORE
::length($seq);
235 Usage : $obj->width($newwidth)
236 $width = $obj->width;
237 Function: Get/set width of alignment
238 Returns : integer value of width
239 Args : on set, new value (a scalar or undef, optional)
247 return $self->{'_width'} = shift if @_;
248 return $self->{'_width'} || $WIDTH;