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 file databases. 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://redmine.open-bio.org/projects/bioperl/
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, $entry,
86 $tempname, $tempdesc, %align, $desc, $maxlen);
87 my $aln = Bio
::SimpleAlign
->new();
89 while (defined ($entry = $self->_readline) ) {
91 if ( $entry =~ s/^>\s*(\S+)\s*// ) {
95 if ( defined $name ) {
97 # put away last name and sequence
98 if ( $name =~ /(\S+)\/(\d
+)-(\d
+)/ ) {
105 $end = $self->_get_len($seqchar);
107 $seq = Bio
::LocatableSeq
->new
110 '-display_id' => $seqname,
111 '-description' => $desc,
114 '-alphabet' => $self->alphabet,
117 $self->debug("Reading $seqname\n");
125 # removed redundant symbol validation
126 # this is already done in Bio::PrimarySeq
130 # Next two lines are to silence warnings that
131 # otherwise occur at EOF when using <$fh>
132 $name = "" if (!defined $name);
133 $seqchar="" if (!defined $seqchar);
136 # Put away last name and sequence
137 if ( $name =~ /(\S+)\/(\d
+)-(\d
+)/ ) {
144 $end = $self->_get_len($seqchar);
147 # This logic now also reads empty lines at the
148 # end of the file. Skip this is seqchar and seqname is null
149 unless ( length($seqchar) == 0 && length($seqname) == 0 ) {
150 $seq = Bio
::LocatableSeq
->new
152 '-display_id' => $seqname,
153 '-description' => $desc,
156 '-alphabet' => $self->alphabet,
159 $self->debug("Reading $seqname\n");
161 my $alnlen = $aln->length;
162 foreach my $seq ( $aln->each_seq ) {
163 if ( $seq->length < $alnlen ) {
164 my ($diff) = ($alnlen - $seq->length);
165 $seq->seq( $seq->seq() . "-" x
$diff);
169 # no sequences means empty alignment (possible EOF)
170 return $aln if $aln->num_sequences;
176 Usage : $stream->write_aln(@aln)
177 Function: writes the $aln object into the stream in fasta format
178 Returns : 1 for success and 0 for error
179 Args : L<Bio::Align::AlignI> object
181 See L<Bio::Align::AlignI>
186 my ($self,@aln) = @_;
187 my $width = $self->width;
188 my ($seq,$desc,$rseq,$name,$count,$length,$seqsub);
190 foreach my $aln (@aln) {
191 if( ! $aln || ! $aln->isa('Bio::Align::AlignI') ) {
192 $self->warn("Must provide a Bio::Align::AlignI object when calling write_aln");
195 if( $self->force_displayname_flat ) {
196 $aln->set_displayname_flat(1);
198 foreach $rseq ( $aln->each_seq() ) {
199 $name = $aln->displayname($rseq->get_nse());
201 $desc = $rseq->description || '';
202 $desc = ' '.$desc if $desc;
203 $self->_print (">$name$desc\n") or return;
205 $length = length($seq);
206 if(defined $seq && $length > 0) {
207 $seq =~ s/(.{1,$width})/$1\n/g;
214 $self->flush if $self->_flush_on_write && defined $self->_fh;
222 Function: determine number of alphabetic chars
224 Args : sequence string
229 my ($self,$seq) = @_;
230 my $chars = $Bio::LocatableSeq
::RESIDUE_SYMBOLS
;
231 $seq =~ s{[^$chars]+}{}gi;
232 return CORE
::length($seq);
238 Usage : $obj->width($newwidth)
239 $width = $obj->width;
240 Function: Get/set width of alignment
241 Returns : integer value of width
242 Args : on set, new value (a scalar or undef, optional)
250 return $self->{'_width'} = shift if @_;
251 return $self->{'_width'} || $WIDTH;