Bio/AlignIO/xmfa.pm

filter: Statementstmt 87.3% 69 / 79 Branchbran 66.6% 28 / 42 Conditioncond 39.2% 11 / 28 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 85.7% 6 / 7 Podpod 100% 3 / 3 totaltotal 66.6% 118 / 177 CC 34 SCAR 27.2
CC
34
Cov
72%
write_aln
23.1
next_aln
14.2
_process_seq
6.3
CRAP 15.2
« Bio/DB/Flat.pm ↑ Summary Bio/DB/Taxonomy.pm »
1 #
2 # BioPerl module for Bio::AlignIO::xmfa
3 #
4 # Copyright Chris Fields
5 #
6 # You may distribute this module under the same terms as perl itself
7 # POD documentation - main docs before the code
8  
9 =head1 NAME
10  
11 Bio::AlignIO::xmfa - XMFA MSA Sequence input/output stream
12  
13 =head1 SYNOPSIS
14  
15 Do not use this module directly. Use it via the L<Bio::AlignIO>
16 class.
17  
18 =head1 DESCRIPTION
19  
20 This object can transform L<Bio::SimpleAlign> objects from
21 XMFA flat file databases. For more information, see:
22  
23 http://asap.ahabs.wisc.edu/mauve-aligner/mauve-user-guide/mauve-output-file-formats.html
24  
25 This module is based on the AlignIO::fasta parser written by
26 Peter Schattner
27  
28 =head1 TODO
29  
30 Finish write_aln(), clean up code, allow LargeLocatableSeq (e.g. for
31 very large sequences from Mauve).
32  
33 =head1 FEEDBACK
34  
35 =head2 Support
36  
37 Please direct usage questions or support issues to the mailing list:
38  
39 I<bioperl-l@bioperl.org>
40  
41 rather than to the module maintainer directly. Many experienced and
42 reponsive experts will be able look at the problem and quickly
43 address it. Please include a thorough description of the problem
44 with code and data examples if at all possible.
45  
46 =head2 Reporting Bugs
47  
48 Report bugs to the Bioperl bug tracking system to help us keep track
49 the bugs and their resolution. Bug reports can be submitted via the
50 web:
51  
52 https://github.com/bioperl/bioperl-live/issues
53  
54 =head1 AUTHORS
55  
56 Chris Fields
57  
58 =head1 APPENDIX
59  
60 The rest of the documentation details each of the object
61 methods. Internal methods are usually preceded with a _
62  
63 =cut
64  
65 # Let the code begin...
66  
67 package Bio::AlignIO::xmfa;
68 3 $Bio::AlignIO::xmfa::VERSION = '1.7.8';
69 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 3 ▶use base qw(Bio::AlignIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
72 3 our $WIDTH = 60;
73  
74 =head2 next_aln
75  
76 Title : next_aln
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
80 or on error
81 Args : -width => optional argument to specify the width sequence
82 will be written (60 chars by default)
83  
84 See L<Bio::Align::AlignI>
85  
86 =cut
87  
88 9 ▶sub next_aln {
Subroutine next_aln: called CC=12 SCAR=26.6
Pod documented
89 9 my $self = shift;
90 9 my ($width) = $self->_rearrange([qw(WIDTH)],@_);
91 9 ▶ $self->width($width || $WIDTH);
Condition33%1/3
$width || $Bio::AlignIO::xmfa::WIDTH
l!l&&r!l&&!r
090
MC/DC0%0/2
$width || $Bio::AlignIO::xmfa::WIDTH
$width $Bio::AlignIO::xmfa::WIDTH
Truth table33%1/3
A || B
A= $widthB= $Bio::AlignIO::xmfa::WIDTH
ABresult
1X1
011
000
92  
93 9 my ($name, $tempname, $seqchar);
94 9 my $aln = Bio::SimpleAlign->new();
95 9 my $seqs = 0;
96 # alignments
97 9 ▶ while (defined (my $entry = $self->_readline) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = $self->_readline)) 279 0 279
98 279 chomp $entry;
99 279 ▶ if ( index($entry, '=') == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($entry, '=') == 0) { } 9 270 279
elsif ($entry =~ /^>.+$/msx) { } 27 243 270
100 9 ▶ if (defined $name && $seqchar) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and $seqchar) 9 0 9
Condition33%1/3
defined $name and $seqchar
!ll&&!rl&&r
009
MC/DC0%0/2
defined $name and $seqchar
defined $name $seqchar
Truth table33%1/3
A and B
A= defined $nameB= $seqchar
ABresult
0X0
100
111
101 9 my $seq = $self->_process_seq($name, $seqchar);
102 9 $aln->add_seq($seq);
103 }
104 9 ▶ if ($aln && $entry =~ m{score\s*=\s*(\d+)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aln and $entry =~ /score\s*=\s*(\d+)/) 9 0 9
Condition33%1/3
$aln and $entry =~ /score\s*=\s*(\d+)/
!ll&&!rl&&r
009
MC/DC0%0/2
$aln and $entry =~ /score\s*=\s*(\d+)/
$aln $entry =~ /score\s*=\s*(\d+)/
Truth table33%1/3
A and B
A= $alnB= $entry =~ /score\s*=\s*(\d+)/
ABresult
0X0
100
111
105 9 $aln->score($1);
106 }
107 9 $seqchar = '';
108 9 undef $name;
109 9 last;
110 } elsif ( $entry =~ m{^>.+$}xms) {
111 27 ▶ if ( defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 18 9 27
112 18 my $seq = $self->_process_seq($name, $seqchar);
113 18 $aln->add_seq($seq);
114 }
115 27 $seqchar = '';
116 27 $name = $entry;
117 } else {
118 243 $seqchar .= $entry;
119 }
120 }
121  
122 # this catches last sequence if '=' is not present (Mauve)
123 9 ▶ if ( defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 9 9
124 0 my $seq = $self->_process_seq($name, $seqchar);
125 0 $aln->add_seq($seq);
126 }
127 9 ▶ $aln->num_sequences ? return $aln : return;
Branch: 1 branch
BranchTrueFalseTotal
$aln->num_sequences ? : 9 0 9
128 }
129  
130 =head2 write_aln
131  
132 Title : write_aln
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
137  
138 See L<Bio::Align::AlignI>
139  
140 =cut
141  
142 2 ▶sub write_aln {
Subroutine write_aln: called CC=17 SCAR=31.4
Pod documented
143 2 my ($self,@aln) = @_;
144 2 my $width = $self->width;
145 2 my ($seq,$desc,$rseq,$name,$count,$length,$seqsub,$start,$end,$strand,$id);
146  
147 2 foreach my $aln (@aln) {
148 2 ▶ if( ! $aln || ! $aln->isa('Bio::Align::AlignI') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aln and $aln->isa("Bio::Align::AlignI")) 0 2 2
Condition33%1/3
$aln and $aln->isa("Bio::Align::AlignI")
!ll&&!rl&&r
002
MC/DC0%0/2
$aln and $aln->isa("Bio::Align::AlignI")
$aln $aln->isa("Bio::Align::AlignI")
Truth table33%1/3
A and B
A= $alnB= $aln->isa("Bio::Align::AlignI")
ABresult
0X0
100
111
149 0 $self->warn("Must provide a Bio::Align::AlignI object when calling write_aln");
150 0 next;
151 }
152 #if( $self->force_displayname_flat ) {
153 # $aln->set_displayname_flat(1);
154 #}
155 2 my $seqct = 1;
156 2 foreach $rseq ( $aln->each_seq() ) {
157 9 ▶ ($start, $end, $strand, $id) = ($rseq->start, $rseq->end, $rseq->strand || 0,
Condition100%2/2
$rseq->strand || 0
l!l
36
MC/DC100%1/1
$rseq->strand || 0
$rseq->strand
Truth table100%2/2
A
A= $rseq->strand
Aresult
11
00
158 $rseq->display_id);
159 9 ▶ $strand = ($strand == 1) ? '+' :
Branch: 2 branches
BranchTrueFalseTotal
$strand == 1 ? : 2 7 9
$strand == -1 ? : 1 6 7
160 ($strand == -1) ? '-' :
161 '';
162 9 $name = sprintf("%d:%d-%d %s %s",$seqct,$start,$end,$strand,$id);
163 9 $seq = $rseq->seq();
164 9 ▶ $desc = $rseq->description || '';
Condition50%1/2
$rseq->description || ''
l!l
09
MC/DC0%0/1
$rseq->description || ''
$rseq->description
Truth table50%1/2
A
A= $rseq->description
Aresult
11
00
165 9 ▶ $self->_print (">$name $desc\n") or return ;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print(">$name $desc\n") or return 9 0 9
166 9 $count = 0;
167 9 $length = length($seq);
168 9 ▶ if(defined $seq && $length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seq and $length > 0) { } 9 0 9
Condition33%1/3
defined $seq and $length > 0
!ll&&!rl&&r
009
MC/DC0%0/2
defined $seq and $length > 0
defined $seq $length > 0
Truth table33%1/3
A and B
A= defined $seqB= $length > 0
ABresult
0X0
100
111
169 9 $seq =~ s/(.{1,$width})/$1\n/g;
170 } else {
171 0 $seq = "\n";
172 }
173 9 ▶ $self->_print($seq) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print($seq) or return 0 9 0 9
174 9 $seqct++;
175 }
176 2 my $alndesc = '';
177 2 ▶ $alndesc = "score = ".$aln->score if ($aln->score);
Branch: 1 branch
BranchTrueFalseTotal
if $aln->score 1 1 2
178 2 ▶ $self->_print("= $alndesc\n") || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print("= $alndesc\n") or return 0 2 0 2
179  
180 }
181 2 ▶ $self->flush if $self->_flush_on_write && defined $self->_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_flush_on_write and defined $self->_fh 2 0 2
Condition33%1/3
$self->_flush_on_write and defined $self->_fh
!ll&&!rl&&r
002
MC/DC0%0/2
$self->_flush_on_write and defined $self->_fh
$self->_flush_on_write defined $self->_fh
Truth table33%1/3
A and B
A= $self->_flush_on_writeB= defined $self->_fh
ABresult
0X0
100
111
182 2 return 1;
183 }
184  
185 =head2 _get_len
186  
187 Title : _get_len
188 Usage :
189 Function: determine number of alphabetic chars
190 Returns : integer
191 Args : sequence string
192  
193 =cut
194  
195 0 ▶sub _get_len {
Subroutine _get_len: not called CC=1 SCAR=6.9
196 0 my ($self,$seq) = @_;
197 0 $seq =~ s/[^A-Z]//gi;
198 0 return CORE::length($seq);
199 }
200  
201 =head2 width
202  
203 Title : width
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)
209  
210  
211 =cut
212  
213 11 ▶sub width{
Subroutine width: called CC=3 SCAR=11.4
Pod documented
214 11 my $self = shift;
215  
216 11 ▶ return $self->{'_width'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 2 11
217 2 ▶ return $self->{'_width'} || $WIDTH;
Condition33%1/3
$self->{'_width'} || $Bio::AlignIO::xmfa::WIDTH
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{'_width'} || $Bio::AlignIO::xmfa::WIDTH
$self->{'_width'} $Bio::AlignIO::xmfa::WIDTH
Truth table33%1/3
A || B
A= $self->{'_width'}B= $Bio::AlignIO::xmfa::WIDTH
ABresult
1X1
011
000
218 }
219  
220 ####### PRIVATE #######
221  
222 27 ▶sub _process_seq {
Subroutine _process_seq: called CC=5 SCAR=18.3
223 27 my ($self, $entry, $seq) = @_;
224 27 my ($start, $end, $strand, $seqname, $desc, $all);
225 # put away last name and sequence
226 27 ▶ if ( $entry =~ m{^>\s*\d+:(\d+)-(\d+)\s+([+-]{1})(?:\s+(\S+)\s*(\S\.*)?)?} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($entry =~ /^>\s*\d+:(\d+)-(\d+)\s+([+-]{1})(?:\s+(\S+)\s*(\S\.*)?)?/) { } 27 0 27
elsif (not $entry =~ /^>\s*\d+:/) { } 0 0 0
227 27 ($start, $end, $seqname, $desc) = ($1, $2, $4, $5);
228 27 ▶ $strand = ($3 eq '+') ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$3 eq '+' ? : 10 17 27
229 } elsif ( $entry !~ /^>\s*\d+:/ ) {
230 0 $self->throw("First field after '>' must be a number:\n$entry");
231 } else {
232 0 $self->throw("Line does not comform to XMFA format:\n$entry");
233 }
234 27 ▶ my $seqobj = Bio::LocatableSeq->new(
Condition33%1/3
$desc || $all
l!l&&r!l&&!r
0027
MC/DC0%0/2
$desc || $all
$desc $all
Truth table33%1/3
A || B
A= $descB= $all
ABresult
1X1
011
000
235 -nowarnonempty => 1,
236 -strand => $strand,
237 -seq => $seq,
238 -display_id => $seqname,
239 -description => $desc || $all,
240 -start => $start,
241 -end => $end,
242 -alphabet => $self->alphabet,
243 );
244 27 $self->debug("Reading $seqname\n");
245 27 return $seqobj;
246 }
247  
248 3 1;
« Bio/DB/Flat.pm ↑ Summary Bio/DB/Taxonomy.pm »