Bio/AlignIO/clustalw.pm

filter: Statementstmt 87.6% 106 / 121 Branchbran 70.0% 42 / 60 Conditioncond 44.4% 8 / 18 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 76.2% 170 / 223 CC 41 SCAR 29.8
CC
41
Cov
78%
write_aln
28.0
next_aln
20.3
_initialize
3.2
CRAP 19.7
« Bio/Align/PairwiseStatistics.pm ↑ Summary Bio/SeqFeature/AnnotationAdaptor.pm »
1 #
2 # BioPerl module for Bio::AlignIO::clustalw
3 #
4 # based on the Bio::SeqIO modules
5 # by Ewan Birney <birney@ebi.ac.uk>
6 # and Lincoln Stein <lstein@cshl.org>
7 # and the Bio::SimpleAlign module of Ewan Birney
8 #
9 # Copyright Peter Schattner
10 #
11 # You may distribute this module under the same terms as perl itself
12 # History
13 # September 5, 2000
14 # POD documentation - main docs before the code
15  
16 =head1 NAME
17  
18 Bio::AlignIO::clustalw - clustalw sequence input/output stream
19  
20 =head1 SYNOPSIS
21  
22 Do not use this module directly. Use it via the Bio::AlignIO class.
23  
24 =head1 DESCRIPTION
25  
26 This object can transform Bio::Align::AlignI objects to and from clustalw
27 files.
28  
29 =head1 FEEDBACK
30  
31 =head2 Mailing Lists
32  
33 User feedback is an integral part of the evolution of this and other
34 Bioperl modules. Send your comments and suggestions preferably to one
35 of the Bioperl mailing lists. Your participation is much appreciated.
36  
37 bioperl-l@bioperl.org - General discussion
38 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
39  
40 =head2 Support
41  
42 Please direct usage questions or support issues to the mailing list:
43  
44 I<bioperl-l@bioperl.org>
45  
46 rather than to the module maintainer directly. Many experienced and
47 reponsive experts will be able look at the problem and quickly
48 address it. Please include a thorough description of the problem
49 with code and data examples if at all possible.
50  
51 =head2 Reporting Bugs
52  
53 Report bugs to the Bioperl bug tracking system to help us keep track
54 the bugs and their resolution. Bug reports can be submitted via the
55 web:
56  
57 https://github.com/bioperl/bioperl-live/issues
58  
59 =head1 AUTHORS - Peter Schattner
60  
61 Email: schattner@alum.mit.edu
62  
63  
64 =head1 APPENDIX
65  
66 The rest of the documentation details each of the object
67 methods. Internal methods are usually preceded with a _
68  
69 =cut
70  
71 # Let the code begin...
72  
73 package Bio::AlignIO::clustalw;
74 8 $Bio::AlignIO::clustalw::VERSION = '1.7.7';
75 8 ▶use vars qw($LINELENGTH $CLUSTALPRINTVERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
76 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78  
79 8 $LINELENGTH = 60;
80 8 $CLUSTALPRINTVERSION = '1.81';
81 8 ▶use base qw(Bio::AlignIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 =head2 new
84  
85 Title : new
86 Usage : $alignio = Bio::AlignIO->new(-format => 'clustalw',
87 -file => 'filename');
88 Function: returns a new Bio::AlignIO object to handle clustalw files
89 Returns : Bio::AlignIO::clustalw object
90 Args : -verbose => verbosity setting (-1, 0, 1, 2)
91 -file => name of file to read in or to write, with ">"
92 -fh => alternative to -file param - provide a filehandle
93 to read from or write to
94 -format => alignment format to process or produce
95 -percentages => display a percentage of identity
96 in each line of the alignment (clustalw only)
97 -linelength=> alignment output line length (default 60)
98  
99 =cut
100  
101 sub _initialize {
102 154 ▶ my ( $self, @args ) = @_;
Subroutine _initialize: called CC=3 SCAR=11.8
103 154 $self->SUPER::_initialize(@args);
104 154 my ( $percentages, $ll ) =
105 $self->_rearrange( [qw(PERCENTAGES LINELENGTH)], @args );
106 154 ▶ defined $percentages && $self->percentages($percentages);
Branch: 1 branch
BranchTrueFalseTotal
defined $percentages and $self->percentages($percentages) 0 154 154
107 154 ▶ $self->line_length( $ll || $LINELENGTH );
Condition33%1/3
$ll || $Bio::AlignIO::clustalw::LINELENGTH
l!l&&r!l&&!r
01540
MC/DC0%0/2
$ll || $Bio::AlignIO::clustalw::LINELENGTH
$ll $Bio::AlignIO::clustalw::LINELENGTH
Truth table33%1/3
A || B
A= $llB= $Bio::AlignIO::clustalw::LINELENGTH
ABresult
1X1
011
000
108 }
109  
110 =head2 next_aln
111  
112 Title : next_aln
113 Usage : $aln = $stream->next_aln()
114 Function: returns the next alignment in the stream
115 Returns : Bio::Align::AlignI object
116 Args : NONE
117  
118 See L<Bio::Align::AlignI> for details
119  
120 =cut
121  
122 sub next_aln {
123 34 ▶ my ($self) = @_;
Subroutine next_aln: called CC=18 SCAR=30.1
Pod documented
124 34 my $first_line;
125  
126 34 ▶ while ( $first_line = $self->_readline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_line = $self->_readline) 34 0 34
127 34 ▶ last if $first_line !~ /^$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $first_line =~ /^$/ 34 0 34
128 }
129 34 $self->_pushback($first_line);
130 34 ▶ if ( defined( $first_line = $self->_readline )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($first_line = $self->_readline) and not $first_line =~ /CLUSTAL/) 0 34 34
Condition33%1/3
defined($first_line = $self->_readline) and not $first_line =~ /CLUSTAL/
!ll&&!rl&&r
0340
MC/DC0%0/2
defined($first_line = $self->_readline) and not $first_line =~ /CLUSTAL/
defined($first_line = $self->_readline) not $first_line =~ /CLUSTAL/
Truth table33%1/3
A and not(B)
A= defined($first_line = $self->_readline)B= not $first_line =~ /CLUSTAL/
ABresult
0X0
100
111
131 && $first_line !~ /CLUSTAL/ )
132 {
133 0 $self->throw(
134 "trying to parse a file which does not start with a CLUSTAL header"
135 );
136 }
137 34 my %alignments;
138 34 my $aln = Bio::SimpleAlign->new(
139 -source => 'clustalw',
140 -verbose => $self->verbose
141 );
142 34 my $order = 0;
143 34 my %order;
144 34 $self->{_lastline} = '';
145 34 my ($first_block, $seen_block) = (0,0);
146 34 ▶ while ( defined( $_ = $self->_readline ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $self->_readline)) 2363 34 2397
147 2363 ▶ next if (/^\s+$/ && !$first_block);
Branch: 1 branch
BranchTrueFalseTotal
if /^\s+$/ and not $first_block 68 2295 2363
Condition100%3/3
/^\s+$/ and not $first_block
!ll&&!rl&&r
184545068
MC/DC100%2/2
/^\s+$/ and not $first_block
/^\s+$/ not $first_block
Truth table100%3/3
A and not(B)
A= /^\s+$/B= not $first_block
ABresult
0X0
100
111
148 2295 ▶ if (/^\s$/) { # line contains no description
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s$/) 239 2056 2295
149 239 $seen_block = 1;
150 239 next;
151 }
152 2056 $first_block = 1;
153 # break the loop if we come to the end of the current alignment
154 # and push back the CLUSTAL header
155 2056 ▶ if (/CLUSTAL/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/CLUSTAL/) 0 2056 2056
156 0 $self->_pushback($_);
157 0 last;
158 }
159  
160 2056 my ( $seqname, $aln_line ) = ( '', '' );
161 2056 ▶ if (/^\s*(\S+)\s*\/\s*(\d+)-(\d+)\s+(\S+)\s*$/ox) {
Branch: 2 branches
BranchTrueFalseTotal
if (m[^\s*(\S+)\s*/\s*(\d+)-(\d+)\s+(\S+)\s*$]ox) { } 48 2008 2056
elsif (/^\s*(\S+)\s+(\S+)\s*\d*\s*$/ox) { } 1744 264 2008
162  
163 # clustal 1.4 format
164 48 ( $seqname, $aln_line ) = ( "$1:$2-$3", $4 );
165  
166 # } elsif( /^\s*(\S+)\s+(\S+)\s*$/ox ) { without trailing numbers
167 }
168 elsif (/^\s*(\S+)\s+(\S+)\s*\d*\s*$/ox) { # with numbers
169 1744 ( $seqname, $aln_line ) = ( $1, $2 );
170 1744 ▶ if ( $seqname =~ /^[\*\.\+\:]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seqname =~ /^[\*\.\+\:]+$/) 0 1744 1744
171 0 $self->{_lastline} = $_;
172 0 next;
173 }
174 }
175 else {
176 264 $self->{_lastline} = $_;
177 264 next;
178 }
179  
180 1792 ▶ if ( !$seen_block ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_block) 232 1560 1792
181 232 ▶ if (exists $order{$seqname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $order{$seqname}) { } 0 232 232
182 0 $self->warn("Duplicate sequence : $seqname\n".
183 "Can't guarantee alignment quality");
184 }
185 else {
186 232 $order{$seqname} = $order++;
187 }
188 }
189  
190 1792 $alignments{$seqname} .= $aln_line;
191 }
192  
193 34 my ( $sname, $start, $end );
194 34 foreach my $name ( sort { $order{$a} <=> $order{$b} } keys %alignments ) {
195 232 ▶ if ( $name =~ /(\S+):(\d+)-(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /(\S+):(\d+)-(\d+)/) { } 6 226 232
196 6 ( $sname, $start, $end ) = ( $1, $2, $3 );
197 }
198 else {
199 226 ( $sname, $start ) = ( $name, 1 );
200 226 my $str = $alignments{$name};
201 226 $str =~ s/[^A-Za-z]//g;
202 226 $end = length($str);
203 }
204 my $seq = Bio::LocatableSeq->new
205 (
206 232 '-seq' => $alignments{$name},
207 '-display_id' => $sname,
208 '-start' => $start,
209 '-end' => $end,
210 '-alphabet' => $self->alphabet,
211 );
212 232 $aln->add_seq($seq);
213 }
214  
215 # not sure if this should be a default option - or we can pass in
216 # an option to do this in the future? --jason stajich
217 # $aln->map_chars('\.','-');
218
219 # no sequences added, so just return
220 34 ▶ return $aln if $aln->num_sequences;
Branch: 1 branch
BranchTrueFalseTotal
if $aln->num_sequences 34 0 34
221 0 return;
222 }
223  
224 =head2 write_aln
225  
226 Title : write_aln
227 Usage : $stream->write_aln(@aln)
228 Function: writes the clustalw-format object (.aln) into the stream
229 Returns : 1 for success and 0 for error
230 Args : Bio::Align::AlignI object
231  
232 =cut
233  
234 sub write_aln {
235 2 ▶ my ( $self, @aln ) = @_;
Subroutine write_aln: called CC=20 SCAR=33.3
Pod documented
236 2 my ( $count, $length, $seq, @seq, $tempcount, $line_len );
237 2 ▶ $line_len = $self->line_length || $LINELENGTH;
Condition33%1/3
$self->line_length || $Bio::AlignIO::clustalw::LINELENGTH
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->line_length || $Bio::AlignIO::clustalw::LINELENGTH
$self->line_length $Bio::AlignIO::clustalw::LINELENGTH
Truth table33%1/3
A || B
A= $self->line_lengthB= $Bio::AlignIO::clustalw::LINELENGTH
ABresult
1X1
011
000
238 2 foreach my $aln (@aln) {
239 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
240 0 $self->warn(
241 "Must provide a Bio::Align::AlignI object when calling write_aln"
242 );
243 0 next;
244 }
245 2 my $matchline = $aln->match_line;
246 2 ▶ if ( $self->force_displayname_flat ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->force_displayname_flat) 0 2 2
247 0 $aln->set_displayname_flat(1);
248 }
249 $self->_print(
250 2 ▶ sprintf( "CLUSTAL W (%s) multiple sequence alignment\n\n\n",
Branch: 1 branch
BranchTrueFalseTotal
$self->_print(sprintf("CLUSTAL W (%s) multiple sequence alignment\n\n\n", $Bio::AlignIO::clustalw::CLUSTALPRINTVERSION)) or return 0 2 2
251 $CLUSTALPRINTVERSION )
252 ) or return;
253 2 $length = $aln->length();
254 2 $count = $tempcount = 0;
255 2 @seq = $aln->each_seq();
256 2 my $max = 22;
257 2 foreach $seq (@seq) {
258 12 ▶ $max = length( $aln->displayname( $seq->get_nse() ) )
Branch: 1 branch
BranchTrueFalseTotal
if length $aln->displayname($seq->get_nse) > $max 0 12 12
259 if ( length( $aln->displayname( $seq->get_nse() ) ) > $max );
260 }
261  
262 2 ▶ while ( $count < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $length) 16 2 18
263 16 my ( $linesubstr, $first ) = ( '', 1 );
264 16 foreach $seq (@seq) {
265  
266 #
267 # Following lines are to suppress warnings
268 # if some sequences in the alignment are much longer than others.
269  
270 96 my ($substring);
271 96 my $seqchars = $seq->seq();
272 SWITCH: {
273 96 ▶ if ( length($seqchars) >= ( $count + $line_len ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $seqchars >= $count + $line_len) { } 84 12 96
elsif (length $seqchars >= $count) { } 12 0 12
274 84 $substring = substr( $seqchars, $count, $line_len );
275 84 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 14 70 84
276 14 $linesubstr =
277 substr( $matchline, $count, $line_len );
278 14 $first = 0;
279 }
280 84 last SWITCH;
281 }
282 elsif ( length($seqchars) >= $count ) {
283 12 $substring = substr( $seqchars, $count );
284 12 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 2 10 12
285 2 $linesubstr = substr( $matchline, $count );
286 2 $first = 0;
287 }
288 12 last SWITCH;
289 }
290 0 $substring = "";
291 }
292 $self->_print(
293 96 ▶ sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$self->_print(sprintf('%-' . $max . "s %s\n", $aln->displayname($seq->get_nse), $substring)) or return 0 96 96
294 "%-" . $max . "s %s\n",
295 $aln->displayname( $seq->get_nse() ), $substring
296 )
297 ) or return;
298 }
299  
300 16 my $percentages = '';
301 16 ▶ if ( $self->percentages ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->percentages) 0 16 16
302 0 my ($strcpy) = ($linesubstr);
303 0 my $count = ( $strcpy =~ tr/\*// );
304 0 $percentages =
305 sprintf( "\t%d%%", 100 * ( $count / length($linesubstr) ) );
306 }
307 $self->_print(
308 16 sprintf(
309 "%-" . $max . "s %s%s\n",
310 '', $linesubstr, $percentages
311 )
312 );
313 16 ▶ $self->_print( sprintf("\n\n") ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print("\n\n") or return 0 16 16
314 16 $count += $line_len;
315 }
316 }
317 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
318 2 return 1;
319 }
320  
321 =head2 percentages
322  
323 Title : percentages
324 Usage : $obj->percentages($newval)
325 Function: Set the percentages flag - whether or not to show percentages in
326 each output line
327 Returns : value of percentages
328 Args : newvalue (optional)
329  
330  
331 =cut
332  
333 sub percentages {
334 16 ▶ my ( $self, $value ) = @_;
Subroutine percentages: called CC=2 SCAR=7.6
Pod documented
335 16 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 16 16
336 0 $self->{'_percentages'} = $value;
337 }
338 16 return $self->{'_percentages'};
339 }
340  
341 =head2 line_length
342  
343 Title : line_length
344 Usage : $obj->line_length($newval)
345 Function: Set the alignment output line length
346 Returns : value of line_length
347 Args : newvalue (optional)
348  
349  
350 =cut
351  
352 sub line_length {
353 156 ▶ my ( $self, $value ) = @_;
Subroutine line_length: called CC=2 SCAR=6.9
Pod documented
354 156 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 154 2 156
355 154 $self->{'_line_length'} = $value;
356 }
357 156 return $self->{'_line_length'};
358 }
359  
360 8 1;
« Bio/Align/PairwiseStatistics.pm ↑ Summary Bio/SeqFeature/AnnotationAdaptor.pm »