3 # BioPerl module for Bio::AlignIO::xmfa
5 # Copyright Chris Fields
7 # You may distribute this module under the same terms as perl itself
8 # POD documentation - main docs before the code
12 Bio::AlignIO::xmfa - XMFA MSA Sequence input/output stream
16 Do not use this module directly. Use it via the L<Bio::AlignIO>
21 This object can transform L<Bio::SimpleAlign> objects from
22 XMFA flat file databases. For more information, see:
24 http://gel.ahabs.wisc.edu/docserver/mauve/files.stx
26 This module is based on the AlignIO::fasta parser written by
31 Finish write_aln(), clean up code, allow LargeLocatableSeq (ie for
32 very large sequences a'la Mauve)
38 Please direct usage questions or support issues to the mailing list:
40 L<bioperl-l@bioperl.org>
42 rather than to the module maintainer directly. Many experienced and
43 reponsive experts will be able look at the problem and quickly
44 address it. Please include a thorough description of the problem
45 with code and data examples if at all possible.
49 Report bugs to the Bioperl bug tracking system to help us keep track
50 the bugs and their resolution. Bug reports can be submitted via the
53 http://bugzilla.open-bio.org/
61 The rest of the documentation details each of the object
62 methods. Internal methods are usually preceded with a _
66 # Let the code begin...
68 package Bio
::AlignIO
::xmfa
;
71 use base
qw(Bio::AlignIO);
77 Usage : $aln = $stream->next_aln
78 Function: returns the next alignment in the stream.
79 Returns : Bio::Align::AlignI object - returns 0 on end of file
81 Args : -width => optional argument to specify the width sequence
82 will be written (60 chars by default)
84 See L<Bio::Align::AlignI>
90 my ($width) = $self->_rearrange([qw(WIDTH)],@_);
91 $self->width($width || $WIDTH);
93 my ($name, $tempname, $seqchar);
94 my $aln = Bio
::SimpleAlign
->new();
97 while (defined (my $entry = $self->_readline) ) {
99 if ( index($entry, '=') == 0 ) {
100 if (defined $name && $seqchar) {
101 my $seq = $self->_process_seq($name, $seqchar);
104 if ($aln && $entry =~ m{score\s*=\s*(\d+)}) {
110 } elsif ( $entry =~ m{^>.+$}xms) {
111 if ( defined $name ) {
112 my $seq = $self->_process_seq($name, $seqchar);
122 # this catches last sequence if '=' is not present (Mauve)
123 if ( defined $name ) {
124 my $seq = $self->_process_seq($name, $seqchar);
127 $aln->no_sequences ?
return $aln : return;
133 Usage : $stream->write_aln(@aln)
134 Function: writes the $aln object into the stream in xmfa format
135 Returns : 1 for success and 0 for error
136 Args : L<Bio::Align::AlignI> object
138 See L<Bio::Align::AlignI>
143 my ($self,@aln) = @_;
144 my $width = $self->width;
145 my ($seq,$desc,$rseq,$name,$count,$length,$seqsub,$start,$end,$strand,$id);
147 foreach my $aln (@aln) {
148 if( ! $aln || ! $aln->isa('Bio::Align::AlignI') ) {
149 $self->warn("Must provide a Bio::Align::AlignI object when calling write_aln");
152 #if( $self->force_displayname_flat ) {
153 # $aln->set_displayname_flat(1);
156 foreach $rseq ( $aln->each_seq() ) {
157 ($start, $end, $strand, $id) = ($rseq->start, $rseq->end, $rseq->strand || 0,
159 $strand = ($strand == 1) ?
'+' :
160 ($strand == -1) ?
'-' :
162 $name = sprintf("%d:%d-%d %s %s",$seqct,$start,$end,$strand,$id);
164 $desc = $rseq->description || '';
165 $self->_print (">$name $desc\n") or return ;
167 $length = length($seq);
168 if(defined $seq && $length > 0) {
169 $seq =~ s/(.{1,$width})/$1\n/g;
173 $self->_print($seq) || return 0;
177 $alndesc = "score = ".$aln->score if ($aln->score);
178 $self->_print("= $alndesc\n") || return 0;
181 $self->flush if $self->_flush_on_write && defined $self->_fh;
189 Function: determine number of alphabetic chars
191 Args : sequence string
196 my ($self,$seq) = @_;
197 $seq =~ s/[^A-Z]//gi;
198 return CORE
::length($seq);
204 Usage : $obj->width($newwidth)
205 $width = $obj->width;
206 Function: Get/set width of alignment
207 Returns : integer value of width
208 Args : on set, new value (a scalar or undef, optional)
216 return $self->{'_width'} = shift if @_;
217 return $self->{'_width'} || $WIDTH;
220 ####### PRIVATE #######
223 my ($self, $entry, $seq) = @_;
224 my ($start, $end, $strand, $seqname, $desc, $all);
225 # put away last name and sequence
226 if ( $entry =~ m{^>\s*\d+:(\d+)-(\d+)\s([+-]{1})(?
:\s
+(\S
+)\s
*(\S\
.*)?
)?
} ) {
227 ($start, $end, $seqname, $desc) = ($1, $2, $4, $5);
228 $strand = ($4 eq '+') ?
1 : -1;
230 $self->throw("Line does not comform to XMFA format:\n$entry");
232 my $seqobj = Bio
::LocatableSeq
->new(
236 -display_id
=> $seqname,
237 -description
=> $desc || $all,
241 $self->debug("Reading $seqname\n");