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
;
63 use base
qw(Bio::AlignIO);
65 use Bio
::LocatableSeq
;
70 Usage : $aln = $stream->next_aln
71 Function: returns the next alignment in the stream.
72 Returns : Bio::Align::AlignI object - returns 0 on end of file
74 Args : -width => optional argument to specify the width sequence
75 will be written (60 chars by default)
77 See L<Bio::Align::AlignI>
83 my ($width) = $self->_rearrange( [qw(WIDTH)], @_ );
84 $self->width( $width || $WIDTH );
86 my ($start, $end, $name, $seqname, $seq, $seqchar,
87 $entry, $tempname, $tempdesc, %align, $desc, $maxlen
89 my $aln = Bio
::SimpleAlign
->new();
91 while ( defined( $entry = $self->_readline ) ) {
93 if ( $entry =~ s/^>\s*(\S+)\s*// ) {
97 if ( defined $name ) {
101 $end = $self->_get_len($seqchar);
102 $seq = Bio
::LocatableSeq
->new(
104 -display_id
=> $seqname,
105 -description
=> $desc,
108 -alphabet
=> $self->alphabet,
111 $self->debug("Reading $seqname\n");
120 # removed redundant symbol validation
121 # this is already done in Bio::PrimarySeq
125 # Next two lines are to silence warnings that
126 # otherwise occur at EOF when using <$fh>
127 $name = "" if ( !defined $name );
128 $seqchar = "" if ( !defined $seqchar );
131 # Put away last name and sequence
132 if ( $name =~ /(\S+\/(\d
+)-(\d
+))$/ ) {
140 $end = $self->_get_len($seqchar);
143 # This logic now also reads empty lines at the
144 # end of the file. Skip this is seqchar and seqname is null
145 unless ( length($seqchar) == 0 && length($seqname) == 0 ) {
146 $seq = Bio
::LocatableSeq
->new(
148 -display_id
=> $seqname,
149 -description
=> $desc,
152 -alphabet
=> $self->alphabet,
155 $self->debug("Reading $seqname\n");
157 my $alnlen = $aln->length;
158 foreach my $seq ( $aln->each_seq ) {
159 if ( $seq->length < $alnlen ) {
160 my ($diff) = ( $alnlen - $seq->length );
161 $seq->seq( $seq->seq() . "-" x
$diff );
165 # no sequences means empty alignment (possible EOF)
166 return $aln if $aln->num_sequences;
174 Usage : $stream->write_aln(@aln)
175 Function: writes the $aln object into the stream in fasta format
176 Returns : 1 for success and 0 for error
177 Args : L<Bio::Align::AlignI> object
179 See L<Bio::Align::AlignI>
184 my ($self,@aln) = @_;
185 my $width = $self->width;
186 my ($seq,$desc,$rseq,$name,$count,$length,$seqsub);
188 foreach my $aln (@aln) {
189 if( ! $aln || ! $aln->isa('Bio::Align::AlignI') ) {
190 $self->warn("Must provide a Bio::Align::AlignI object when calling write_aln");
193 if( $self->force_displayname_flat ) {
194 $aln->set_displayname_flat(1);
196 foreach $rseq ( $aln->each_seq() ) {
197 $name = $aln->displayname($rseq->get_nse());
199 $desc = $rseq->description || '';
200 $desc = ' '.$desc if $desc;
201 $self->_print (">$name$desc\n") or return;
203 $length = length($seq);
204 if(defined $seq && $length > 0) {
205 $seq =~ s/(.{1,$width})/$1\n/g;
212 $self->flush if $self->_flush_on_write && defined $self->_fh;
220 Function: determine number of alphabetic chars
222 Args : sequence string
227 my ($self,$seq) = @_;
228 my $chars = $Bio::LocatableSeq
::GAP_SYMBOLS
.$Bio::LocatableSeq
::FRAMESHIFT_SYMBOLS
;
229 $seq =~ s{[$chars]+}{}gi;
230 return CORE
::length($seq);
236 Usage : $obj->width($newwidth)
237 $width = $obj->width;
238 Function: Get/set width of alignment
239 Returns : integer value of width
240 Args : on set, new value (a scalar or undef, optional)
248 return $self->{'_width'} = shift if @_;
249 return $self->{'_width'} || $WIDTH;