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 Report bugs to the Bioperl bug tracking system to help us keep track
39 the bugs and their resolution. Bug reports can be submitted via the
42 http://bugzilla.open-bio.org/
50 The rest of the documentation details each of the object
51 methods. Internal methods are usually preceded with a _
55 # Let the code begin...
57 package Bio
::AlignIO
::xmfa
;
60 use base
qw(Bio::AlignIO);
66 Usage : $aln = $stream->next_aln
67 Function: returns the next alignment in the stream.
68 Returns : Bio::Align::AlignI object - returns 0 on end of file
70 Args : -width => optional argument to specify the width sequence
71 will be written (60 chars by default)
73 See L<Bio::Align::AlignI>
79 my ($width) = $self->_rearrange([qw(WIDTH)],@_);
80 $self->width($width || $WIDTH);
82 my ($name, $tempname, $seqchar);
83 my $aln = Bio
::SimpleAlign
->new();
86 while (defined (my $entry = $self->_readline) ) {
88 if ( index($entry, '=') == 0 ) {
89 if (defined $name && $seqchar) {
90 my $seq = $self->_process_seq($name, $seqchar);
93 if ($aln && $entry =~ m{score\s*=\s*(\d+)}) {
99 } elsif ( $entry =~ m{^>.+$}xms) {
100 if ( defined $name ) {
101 my $seq = $self->_process_seq($name, $seqchar);
111 # this catches last sequence if '=' is not present (Mauve)
112 if ( defined $name ) {
113 my $seq = $self->_process_seq($name, $seqchar);
116 $aln->no_sequences ?
return $aln : return;
122 Usage : $stream->write_aln(@aln)
123 Function: writes the $aln object into the stream in xmfa format
124 Returns : 1 for success and 0 for error
125 Args : L<Bio::Align::AlignI> object
127 See L<Bio::Align::AlignI>
132 my ($self,@aln) = @_;
133 my $width = $self->width;
134 my ($seq,$desc,$rseq,$name,$count,$length,$seqsub,$start,$end,$strand,$id);
136 foreach my $aln (@aln) {
137 if( ! $aln || ! $aln->isa('Bio::Align::AlignI') ) {
138 $self->warn("Must provide a Bio::Align::AlignI object when calling write_aln");
141 #if( $self->force_displayname_flat ) {
142 # $aln->set_displayname_flat(1);
145 foreach $rseq ( $aln->each_seq() ) {
146 ($start, $end, $strand, $id) = ($rseq->start, $rseq->end, $rseq->strand,
148 $strand = ($strand == 1) ?
'+' :
149 ($strand == -1) ?
'-' :
151 $name = sprintf("%d:%d-%d %s %s",$seqct,$start,$end,$strand,$id);
153 $desc = $rseq->description || '';
154 $self->_print (">$name $desc\n") or return ;
156 $length = length($seq);
157 if(defined $seq && $length > 0) {
158 $seq =~ s/(.{1,$width})/$1\n/g;
162 $self->_print($seq) || return 0;
166 $alndesc = "score = ".$aln->score if ($aln->score);
167 $self->_print("= $alndesc\n") || return 0;
170 $self->flush if $self->_flush_on_write && defined $self->_fh;
178 Function: determine number of alphabetic chars
180 Args : sequence string
185 my ($self,$seq) = @_;
186 $seq =~ s/[^A-Z]//gi;
187 return CORE
::length($seq);
193 Usage : $obj->width($newwidth)
194 $width = $obj->width;
195 Function: Get/set width of alignment
196 Returns : integer value of width
197 Args : on set, new value (a scalar or undef, optional)
205 return $self->{'_width'} = shift if @_;
206 return $self->{'_width'} || $WIDTH;
209 ####### PRIVATE #######
212 my ($self, $entry, $seq) = @_;
213 my ($start, $end, $strand, $seqname, $desc, $all);
214 # put away last name and sequence
215 if ( $entry =~ m{^>\s*\d+:(\d+)-(\d+)\s([+-]{1})(?
:\s
+(\S
+)\s
*(\S\
.*)?
)?
} ) {
216 ($start, $end, $seqname, $desc) = ($1, $2, $4, $5);
217 $strand = ($4 eq '+') ?
1 : -1;
219 $self->throw("Line does not comform to XMFA format:\n$entry");
221 my $seqobj = Bio
::LocatableSeq
->new(
225 -display_id
=> $seqname,
226 -description
=> $desc || $all,
230 $self->debug("Reading $seqname\n");