Bio/AlignIO/nexus.pm

filter: Statementstmt 91.5% 174 / 190 Branchbran 73.3% 113 / 154 Conditioncond 60.0% 27 / 45 MC/DCmcdc 28.5% 8 / 28 Subroutinesub 90.0% 9 / 10 Podpod 100% 3 / 3 totaltotal 77.6% 334 / 430 CC 97 SCAR 42.4
CC
97
Cov
81%
next_aln
94.6
write_aln
22.7
_read_taxlabels
20.0
CRAP 69.5
« Bio/Index/EMBL.pm ↑ Summary Bio/Root/Utilities.pm »
1 #
2 # BioPerl module for Bio::AlignIO::nexus
3 #
4 # Copyright Heikki Lehvaslaiho
5 #
6  
7 =head1 NAME
8  
9 Bio::AlignIO::nexus - NEXUS format sequence input/output stream
10  
11 =head1 SYNOPSIS
12  
13 Do not use this module directly. Use it via the L<Bio::AlignIO> class.
14  
15 use Bio::AlignIO;
16  
17 my $in = Bio::AlignIO->new(-format => 'nexus',
18 -file => 'aln.nexus');
19 while( my $aln = $in->next_aln ) {
20 # do something with the alignment
21 }
22  
23 =head1 DESCRIPTION
24  
25 This object can transform L<Bio::Align::AlignI> objects to and from NEXUS
26 data blocks. See method documentation for supported NEXUS features.
27  
28 =head1 ACKNOWLEDGEMENTS
29  
30 Will Fisher has written an excellent standalone NEXUS format parser in
31 Perl, readnexus. A number of tricks were adapted from it.
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 - Heikki Lehvaslaiho
55  
56 Email: heikki-at-bioperl-dot-org
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::nexus;
68 3 $Bio::AlignIO::nexus::VERSION = '1.7.7';
69 3 ▶use vars qw(%valid_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
70 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 3 ▶no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73  
74 3 ▶use base qw(Bio::AlignIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 BEGIN {
77 3 ▶ %valid_type = map {$_, 1} qw( dna rna protein standard );
Subroutine BEGIN: called CC=1 SCAR=0.0
78 # standard throws error: inherited from Bio::PrimarySeq
79 }
80  
81 =head2 new
82  
83 Title : new
84 Usage : $alignio = Bio::AlignIO->new(-format => 'nexus', -file => 'filename');
85 Function: returns a new Bio::AlignIO object to handle clustalw files
86 Returns : Bio::AlignIO::clustalw object
87 Args : -verbose => verbosity setting (-1,0,1,2)
88 -file => name of file to read in or with ">" - writeout
89 -fh => alternative to -file param - provide a filehandle
90 to read from/write to
91 -format => type of Alignment Format to process or produce
92  
93 Customization of nexus flavor output
94  
95 -show_symbols => print the symbols="ATGC" in the data definition
96 (MrBayes does not like this)
97 boolean [default is 1]
98 -show_endblock => print an 'endblock;' at the end of the data
99 (MyBayes does not like this)
100 boolean [default is 1]
101  
102 =cut
103  
104 sub _initialize {
105 33 ▶ my ($self, @args) = @_;
Subroutine _initialize: called CC=3 SCAR=11.0
106 33 $self->SUPER::_initialize(@args);
107 33 my ($show_symbols, $endblock) =
108 $self->_rearrange([qw(SHOW_SYMBOLS SHOW_ENDBLOCK)], @args);
109 33 my @names = qw(symbols endblock);
110 33 for my $v ( $show_symbols, $endblock ) {
111 66 ▶ $v = 1 unless defined $v; # default value is 1
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 66 0 66
112 66 my $n = shift @names;
113 66 $self->flag($n, $v);
114 }
115 }
116  
117  
118 =head2 next_aln
119  
120 Title : next_aln
121 Usage : $aln = $stream->next_aln()
122 Function: Returns the next alignment in the stream.
123  
124 Supports the following NEXUS format features:
125 - The file has to start with '#NEXUS'
126 - Reads in the name of the alignment from a comment
127 (anything after 'TITLE: ') .
128 - Sequence names can be given in a taxa block, too.
129 - If matchchar notation is used, converts
130 them back to sequence characters.
131 - Does character conversions specified in the
132 NEXUS equate command.
133 - Sequence names of type 'Homo sapiens' and
134 Homo_sapiens are treated identically.
135  
136 Returns : L<Bio::Align::AlignI> object
137 Args :
138  
139  
140 =cut
141  
142  
143 sub next_aln {
144 30 ▶ my $self = shift;
Subroutine next_aln: called CC=72 SCAR=45.5
Pod documented
145 30 my $entry;
146 30 my ($aln_name, $seqcount, $residuecount, %hash, $alphabet,
147 $match, $gap, $missing, $equate, $interleave,
148 $name,$str,@names,$seqname,$start,$end,$count,$seq);
149 30 local $Bio::LocatableSeq::OTHER_SYMBOLS = '\*\?\.';
150 30 local $Bio::LocatableSeq::GAP_SYMBOLS = '\-';
151 30 my $aln = Bio::SimpleAlign->new(-source => 'nexus');
152  
153 # file starts with '#NEXUS' but we allow white space only lines before it
154 30 $entry = $self->_readline;
155 30 ▶ $entry = $self->_readline while defined $entry && $entry =~ /^\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
defined $entry and $entry =~ /^\s+$/ and do { $entry = $self->_readline } 0 30 30
Condition33%1/3
defined $entry and $entry =~ /^\s+$/
!ll&&!rl&&r
0300
MC/DC0%0/2
defined $entry and $entry =~ /^\s+$/
defined $entry $entry =~ /^\s+$/
Truth table33%1/3
A and B
A= defined $entryB= $entry =~ /^\s+$/
ABresult
0X0
100
111
156  
157 30 ▶ return unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 30 30
158 30 ▶ $self->throw("Not a valid interleaved NEXUS file! [#NEXUS] not starting the file\n$entry")
Branch: 1 branch
BranchTrueFalseTotal
unless $entry and $entry =~ /^#NEXUS/i 0 30 30
Condition33%1/3
$entry and $entry =~ /^#NEXUS/i
!ll&&!rl&&r
0030
MC/DC0%0/2
$entry and $entry =~ /^#NEXUS/i
$entry $entry =~ /^#NEXUS/i
Truth table33%1/3
A and B
A= $entryB= $entry =~ /^#NEXUS/i
ABresult
0X0
100
111
159 unless ($entry && $entry =~ /^#NEXUS/i);
160  
161 # skip anything before either the taxa or data block
162 # but read in the optional title in a comment
163 30 ▶ while (defined($entry = $self->_readline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($entry = $self->_readline)) 88 0 88
164 88 local ($_) = $entry;
165 88 ▶ /\[TITLE. *([^\]]+)]\s+/i and $aln_name = $1;
Branch: 1 branch
BranchTrueFalseTotal
/\[TITLE. *([^\]]+)]\s+/i and $aln_name = $1 11 77 88
166 88 ▶ last if /^begin +data/i || /^begin +taxa/i;
Branch: 1 branch
BranchTrueFalseTotal
if /^begin +data/i or /^begin +taxa/i 30 58 88
Condition100%3/3
/^begin +data/i or /^begin +taxa/i
l!l&&r!l&&!r
171358
MC/DC100%2/2
/^begin +data/i or /^begin +taxa/i
/^begin +data/i /^begin +taxa/i
Truth table100%3/3
A or B
A= /^begin +data/iB= /^begin +taxa/i
ABresult
1X1
011
000
167 }
168 30 ▶ $aln_name =~ s/\s/_/g and $aln->id($aln_name) if $aln_name;
Branch: 1 branch
BranchTrueFalseTotal
if $aln_name 11 19 30
Condition50%3/6
$aln_name && ($aln_name =~ s/\s/_/g && $aln->id($aln_name))
!ll&&!rl&&r
19110
$aln_name =~ s/\s/_/g and $aln->id($aln_name)
!ll&&!rl&&r
0110
MC/DC0%0/3
$aln_name && ($aln_name =~ s/\s/_/g && $aln->id($aln_name))
$aln_name $aln_name =~ s/\s/_/g $aln->id($aln_name)
Truth table0%0/4
A && B and C
A= $aln_nameB= $aln_name =~ s/\s/_/gC= $aln->id($aln_name)
ABCresult
0XX0
10X0
1100
1111
169  
170 # data and taxa blocks
171 30 my $incomment;
172 30 ▶ while (defined ($entry = $self->_readline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($entry = $self->_readline)) 434 0 434
173 434 local ($_) = $entry;
174 434 ▶ next if s/\[[^\]]+\]//g; # remove comments
Branch: 1 branch
BranchTrueFalseTotal
if s/\[[^\]]+\]//g 182 252 434
175 252 ▶ if( s/\[[^\]]+$// ) {
Branch: 3 branches
BranchTrueFalseTotal
if (s/\[[^\]]+$//) { } 6 246 252
elsif ($incomment) { } 39 207 246
elsif (/taxlabels/i) { } 13 194 207
176 6 $incomment = 1;
177 # skip line if it is now empty or contains only whitespace
178 6 ▶ next if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 6 0 6
179 } elsif($incomment) {
180 39 ▶ if( s/^[^\]]*\]// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^[^\]]*\]//) { } 6 33 39
181 6 $incomment = 0;
182 } else {
183 33 next;
184 }
185 } elsif( /taxlabels/i ) {
186 # doesn't deal with taxlabels adequately and can mess things up!
187 # @names = $self->_read_taxlabels;
188 } else {
189  
190 194 ▶ /ntax\s*=\s*(\d+)/i and $seqcount = $1;
Branch: 1 branch
BranchTrueFalseTotal
/ntax\s*=\s*(\d+)/i and $seqcount = $1 30 164 194
191 194 ▶ /nchar\s*=\s*(\d+)/i and $residuecount = $1;
Branch: 1 branch
BranchTrueFalseTotal
/nchar\s*=\s*(\d+)/i and $residuecount = $1 30 164 194
192 194 ▶ /matchchar\s*=\s*(.)/i and $match = $1;
Branch: 1 branch
BranchTrueFalseTotal
/matchchar\s*=\s*(.)/i and $match = $1 2 192 194
193 194 ▶ /gap\s*=\s*(.)/i and $gap = $1;
Branch: 1 branch
BranchTrueFalseTotal
/gap\s*=\s*(.)/i and $gap = $1 30 164 194
194 194 ▶ /missing\s*=\s*(.)/i and $missing = $1;
Branch: 1 branch
BranchTrueFalseTotal
/missing\s*=\s*(.)/i and $missing = $1 29 165 194
195 194 ▶ /equate\s*=\s*\"([^\"]+)/i and $equate = $1; # "e.g. equate="T=C G=A";
Branch: 1 branch
BranchTrueFalseTotal
/equate\s*=\s*\"([^\"]+)/i and $equate = $1 0 194 194
196 194 ▶ /datatype\s*=\s*(\w+)/i and $alphabet = lc $1;
Branch: 1 branch
BranchTrueFalseTotal
/datatype\s*=\s*(\w+)/i and $alphabet = lc $1 30 164 194
197 194 ▶ /interleave/i and $interleave = 1 ;
Branch: 1 branch
BranchTrueFalseTotal
/interleave/i and $interleave = 1 15 179 194
198 194 ▶ last if /matrix/io;
Branch: 1 branch
BranchTrueFalseTotal
if /matrix/io 30 164 194
199 }
200 }
201 30 ▶ $self->throw("Not a valid NEXUS sequence file. Datatype not specified.")
Branch: 1 branch
BranchTrueFalseTotal
unless $alphabet 0 30 30
202 unless $alphabet;
203 $self->throw("Not a valid NEXUS sequence file. Datatype should not be [$alphabet]")
204 30 ▶ unless $valid_type{$alphabet};
Branch: 1 branch
BranchTrueFalseTotal
unless $Bio::AlignIO::nexus::valid_type{$alphabet} 0 30 30
205 30 ▶ $self->throw("\"$gap\" is not a valid gap character. For compatability, gap char can not be one of: ()[]{}/\,;:=*'`\"<>^")
Branch: 1 branch
BranchTrueFalseTotal
if $gap and $gap =~ m"[\(\)\[\]\{\}/\\\,\;\:\=\*\'\`\<\>\^]" 0 30 30
Condition33%1/3
$gap and $gap =~ m"[\(\)\[\]\{\}/\\\,\;\:\=\*\'\`\<\>\^]"
!ll&&!rl&&r
0300
MC/DC0%0/2
$gap and $gap =~ m"[\(\)\[\]\{\}/\\\,\;\:\=\*\'\`\<\>\^]"
$gap $gap =~ m"[\(\)\[\]\{\}/\\\,\;\:\=\*\'\`\<\>\^]"
Truth table33%1/3
A and B
A= $gapB= $gap =~ m"[\(\)\[\]\{\}/\\\,\;\:\=\*\'\`\<\>\^]"
ABresult
0X0
100
111
206 if $gap && $gap =~ /[\(\)\[\]\{\}\/\\\,\;\:\=\*\'\`\<\>\^]/;
207 30 ▶ $self->throw("\"$missing\" is not a valid missing character. For compatability, missing char can not be one of: ()[]{}/\,;:=*'`\"<>^")
Branch: 1 branch
BranchTrueFalseTotal
if $missing and $missing =~ m"[\(\)\[\]\{\}/\\\,\;\:\=\*\'\`\<\>\^]" 0 30 30
Condition66%2/3
$missing and $missing =~ m"[\(\)\[\]\{\}/\\\,\;\:\=\*\'\`\<\>\^]"
!ll&&!rl&&r
1290
MC/DC0%0/2
$missing and $missing =~ m"[\(\)\[\]\{\}/\\\,\;\:\=\*\'\`\<\>\^]"
$missing $missing =~ m"[\(\)\[\]\{\}/\\\,\;\:\=\*\'\`\<\>\^]"
Truth table66%2/3
A and B
A= $missingB= $missing =~ m"[\(\)\[\]\{\}/\\\,\;\:\=\*\'\`\<\>\^]"
ABresult
0X0
100
111
208 if $missing && $missing =~ /[\(\)\[\]\{\}\/\\\,\;\:\=\*\'\`\<\>\^]/;
209  
210 30 $aln->gap_char($gap);
211 30 $aln->missing_char($missing);
212  
213 #
214 # if data is not right after the matrix line
215 # read the empty lines out
216 #
217 30 ▶ while ($entry = $self->_readline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry = $self->_readline) 33 0 33
218 33 ▶ unless ($entry =~ /^\s+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry =~ /^\s+$/) 30 3 33
219 30 $self->_pushback($entry);
220 30 last;
221 }
222 }
223  
224 #
225 # matrix command
226 #
227 # first alignment section
228 30 ▶ if (@names == 0) { # taxa block did not exist
Branch: 1 branch
BranchTrueFalseTotal
if (@names == 0) 30 0 30
229 30 ▶ while ($entry = $self->_readline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry = $self->_readline) 507 0 507
230 507 local ($_) = $entry;
231 507 ▶ if( s/\[[^\]]+\]//g ) { #] remove comments
Branch: 1 branch
BranchTrueFalseTotal
if (s/\[[^\]]+\]//g) 205 302 507
232 205 ▶ next if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 8 197 205
233 # skip line if it is now empty or contains only whitespace
234 }
235 499 ▶ if ($interleave && defined$count && ($count <= $seqcount)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interleave and defined $count and $count <= $seqcount) { } 110 389 499
Condition83%5/6
$interleave and defined $count and $count <= $seqcount
!ll&&!rl&&r
3890110
$interleave and defined $count
!ll&&!rl&&r
37217110
MC/DC66%2/3
$interleave and defined $count and $count <= $seqcount
$interleave defined $count $count <= $seqcount
Truth table75%3/4
A and B and C
A= $interleaveB= defined $countC= $count <= $seqcount
ABCresult
0XX0
10X0
1100
1111
236 110 ▶ /^\s+$/ and last;
Branch: 1 branch
BranchTrueFalseTotal
/^\s+$/ and last 15 95 110
237 } else {
238 389 ▶ /^\s+$/ and next;
Branch: 1 branch
BranchTrueFalseTotal
/^\s+$/ and next 4 385 389
239 }
240 480 ▶ /^\s*;/ and last; # stop if colon at end of matrix is on it's own line
Branch: 1 branch
BranchTrueFalseTotal
/^\s*;/ and last 6 474 480
241 #/^\s*;\s*$/ and last;
242 474 ▶ if ( /^\s*([\"\'](.+?)[\"\']|(\S+))\s+(.*)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*([\"\'](.+?)[\"\']|(\S+))\s+(.*)\s*$/) 474 0 474
243 # get single and double quoted names, or all the first
244 # nonwhite word as the name, and remained is seq
245 #if (/^\s*('([^']*?)'|([^']\S*))\s+(.*)$/) { #'
246 474 ▶ $name = ($2 || $3);
Condition66%2/3
$2 || $3
l!l&&r!l&&!r
384360
MC/DC0%0/2
$2 || $3
$2 $3
Truth table66%2/3
A || B
A= $2B= $3
ABresult
1X1
011
000
247 474 ▶ if ($4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($4) { } 464 10 474
248 # seq is on same line as name
249 # this is the usual NEXUS format
250 464 $str = $4;
251 } else {
252 # otherwise get seq from following lines. No comments allowed
253 # a less common matrix format, usually used for very long seqs
254 10 $str='';
255 10 ▶ while (local ($_) = $self->_readline) {
Branch: 1 branch
BranchTrueFalseTotal
if (local($_) = $self->_readline) 1860 0 1860
256 1860 my $str_tmp = $_;
257 1860 $str_tmp =~ s/[\s;]//g;
258 1860 $str .= $str_tmp;
259 1860 ▶ last if length$str == $residuecount;
Branch: 1 branch
BranchTrueFalseTotal
if length $str == $residuecount 10 1850 1860
260 }
261 }
262 474 $name =~ s/ /_/g;
263 474 push @names, $name;
264  
265 474 $str =~ s/[\s;]//g;
266 474 $count = @names;
267 474 $hash{$count} = $str;
268 }
269 474 ▶ $self->throw("Not a valid interleaved NEXUS file! seqcount [$count] > predeclared [$seqcount] in the first section") if $count > $seqcount;
Branch: 1 branch
BranchTrueFalseTotal
if $count > $seqcount 0 474 474
270 474 ▶ /;/ and last; # stop if colon at end of matrix is on the same line as the last seq
Branch: 1 branch
BranchTrueFalseTotal
/;/ and last 9 465 474
271 }
272 }
273  
274 # interleaved sections
275 30 $count = 0;
276 30 ▶ if ( $interleave ) { # only read next section if file is interleaved
Branch: 1 branch
BranchTrueFalseTotal
if ($interleave) 15 15 30
277 15 ▶ while( $entry = $self->_readline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry = $self->_readline) 1043 0 1043
278 1043 local ($_) = $entry;
279 1043 ▶ if( s/\[[^\]]+\]//g ) { #] remove comments
Branch: 1 branch
BranchTrueFalseTotal
if (s/\[[^\]]+\]//g) 826 217 1043
280 826 ▶ next if /^\s*$/; # skip line if it is now empty or contains only whitespace
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 58 768 826
281 }
282 985 ▶ /^\s*;/ and last; # stop if colon at end of matrix is on it's own line
Branch: 1 branch
BranchTrueFalseTotal
/^\s*;/ and last 14 971 985
283 971 ▶ $count = 0, next if $entry =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /^\s*$/ 67 904 971
284 904 ▶ if (/^\s*(\'([^\']*?)\'|([^\']\S*))\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(\'([^\']*?)\'|([^\']\S*))\s+(.*)$/) 904 0 904
285 904 $str = $4;
286 904 $str =~ s/[\s;]//g;
287 904 $count++;
288 904 $hash{$count} .= $str;
289 };
290 904 ▶ $self->throw("Not a valid interleaved NEXUS file!
Branch: 1 branch
BranchTrueFalseTotal
if $count > $seqcount 0 904 904
291 seqcount [$count] > predeclared [$seqcount] ") if $count > $seqcount;
292 904 ▶ /;/ and last; # stop if colon at end of matrix is on the same line as the last seq
Branch: 1 branch
BranchTrueFalseTotal
/;/ and last 1 903 904
293 }
294 }
295
296 30 ▶ return if @names < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @names < 1 0 30 30
297
298 # sequence creation
299 30 $count = 0;
300 30 foreach $name ( @names ) {
301 474 $count++;
302 474 ▶ if( $name =~ /(\S+)\/(\d+)-(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[(\S+)/(\d+)-(\d+)]) { } 0 474 474
303 0 ($seqname,$start,$end) = ($1,$2,$3);
304 } else {
305 474 ($seqname,$start,$str) = ($name,1,$hash{$count});
306 474 $str =~ s/[$Bio::LocatableSeq::GAP_SYMBOLS]//g;
307 474 $end = length($str);
308 }
309
310 # consistency test
311 $self->throw("Length of sequence [$seqname] is not [$residuecount]; got".CORE::length($hash{$count}))
312 474 ▶ unless CORE::length($hash{$count}) == $residuecount;
Branch: 1 branch
BranchTrueFalseTotal
unless length $hash{$count} == $residuecount 0 474 474
313
314 474 $seq = Bio::LocatableSeq->new('-seq' => $hash{$count},
315 '-display_id' => $seqname,
316 '-start' => $start,
317 '-end' => $end,
318 '-alphabet' => $alphabet
319 );
320 474 $aln->add_seq($seq);
321 }
322  
323 # if matchchar is used
324 30 ▶ $aln->unmatch($match) if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 2 28 30
325  
326 # if equate ( e.g. equate="T=C G=A") is used
327 30 ▶ if ($equate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($equate) 0 30 30
328 0 ▶ $aln->map_chars($1, $2) while $equate =~ /(\S)=(\S)/g;
Branch: 1 branch
BranchTrueFalseTotal
$equate =~ /(\S)=(\S)/g and do { $aln->map_chars($1, $2) } 0 0 0
329 }
330  
331 30 ▶ while (defined $entry &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry and not $entry =~ /endblock/i) 469 30 499
Condition100%3/3
defined $entry and not $entry =~ /endblock/i
!ll&&!rl&&r
1911469
MC/DC100%2/2
defined $entry and not $entry =~ /endblock/i
defined $entry not $entry =~ /endblock/i
Truth table100%3/3
A and not(B)
A= defined $entryB= not $entry =~ /endblock/i
ABresult
0X0
100
111
332 $entry !~ /endblock/i) {
333 469 $entry = $self->_readline;
334 }
335  
336 30 ▶ return $aln if $aln->num_sequences;
Branch: 1 branch
BranchTrueFalseTotal
if $aln->num_sequences 30 0 30
337 0 return;
338 }
339  
340 sub _read_taxlabels {
341 0 ▶ my ($self) = @_;
Subroutine _read_taxlabels: not called CC=4 SCAR=30.0
342 0 my ($name, @names);
343 0 ▶ while (my $entry = $self->_readline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->_readline) 0 0 0
344 0 ▶ last if $entry =~ m/^\s*(END)?;/i;
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /^\s*(END)?;/i 0 0 0
345 0 ▶ if( $entry =~ m/\s*(\S+)\s+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /\s*(\S+)\s+/) 0 0 0
346 0 ($name) = ($1);
347 0 $name =~ s/\[[^\[]+\]//g;
348 0 $name =~ s/\W/_/g;
349 0 push @names, $name;
350 }
351 }
352 0 return @names;
353 }
354  
355 =head2 write_aln
356  
357 Title : write_aln
358 Usage : $stream->write_aln(@aln)
359 Function: Writes the $aln object into the stream in interleaved NEXUS
360 format. Everything is written into a data block.
361 SimpleAlign methods match_char, missing_char and gap_char must be set
362 if you want to see them in the output.
363 Returns : 1 for success and 0 for error
364 Args : L<Bio::Align::AlignI> object
365  
366 =cut
367  
368 sub write_aln {
369 2 ▶ my ($self,@aln) = @_;
Subroutine write_aln: called CC=20 SCAR=31.2
Pod documented
370 2 my $count = 0;
371 2 my $wrapped = 0;
372 2 my $maxname;
373 2 my ($length,$date,$name,$seq,$miss,$pad,%hash,@arr,$tempcount,$index );
374 2 my ($match, $missing, $gap,$symbols) = ('', '', '','');
375  
376 2 foreach my $aln (@aln) {
377 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
378 0 $self->warn("Must provide a Bio::Align::AlignI object when calling write_aln");
379 0 next;
380 }
381 2 ▶ $self->throw("All sequences in the alignment must be the same length")
Branch: 1 branch
BranchTrueFalseTotal
unless $aln->is_flush($self->verbose) 0 2 2
382 unless $aln->is_flush($self->verbose);
383  
384 2 $length = $aln->length();
385  
386 2 $self->_print (sprintf("#NEXUS\n[TITLE: %s]\n\nbegin data;\ndimensions ntax=%s nchar=%s;\n",
387 $aln->id, $aln->num_sequences, $length));
388 2 ▶ $match = "match=". $aln->match_char if $aln->match_char;
Branch: 1 branch
BranchTrueFalseTotal
if $aln->match_char 0 2 2
389 2 ▶ $missing = "missing=". $aln->missing_char if $aln->missing_char;
Branch: 1 branch
BranchTrueFalseTotal
if $aln->missing_char 1 1 2
390 2 ▶ $gap = "gap=". $aln->gap_char if $aln->gap_char;
Branch: 1 branch
BranchTrueFalseTotal
if $aln->gap_char 2 0 2
391  
392 2 ▶ $symbols = 'symbols="'.join('',$aln->symbol_chars). '"'
Branch: 1 branch
BranchTrueFalseTotal
if $self->flag("symbols") and $aln->symbol_chars 2 0 2
Condition33%1/3
$self->flag("symbols") and $aln->symbol_chars
!ll&&!rl&&r
002
MC/DC0%0/2
$self->flag("symbols") and $aln->symbol_chars
$self->flag("symbols") $aln->symbol_chars
Truth table33%1/3
A and B
A= $self->flag("symbols")B= $aln->symbol_chars
ABresult
0X0
100
111
393 if( $self->flag('symbols') && $aln->symbol_chars);
394 2 $self->_print
395 (sprintf("format interleave datatype=%s %s %s %s %s;\n\nmatrix\n",
396 $aln->get_seq_by_pos(1)->alphabet, $match,
397 $missing, $gap, $symbols));
398  
399 # account for single quotes round names
400 2 my $indent = $aln->maxdisplayname_length+2;
401  
402 2 $aln->set_displayname_flat();
403 2 foreach $seq ( $aln->each_seq() ) {
404 10 my $nmid = $aln->displayname($seq->get_nse());
405 10 ▶ if( $nmid =~ /[^\w\d\.]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmid =~ /[^\w\d\.]/) { } 1 9 10
406 # put name in single quotes incase it contains any of
407 # the following chars: ()[]{}/\,;:=*'"`+-<> that are not
408 # allowed in PAUP* and possible other software
409  
410 1 $name = sprintf("%-${indent}s", "\'" . $nmid . "\'");
411 } else {
412 9 $name = sprintf("%-${indent}s", $nmid);
413 }
414 10 $hash{$name} = $seq->seq;
415 10 push(@arr,$name);
416 }
417  
418 2 ▶ while( $count < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $length) 10 2 12
419 # there is another block to go!
420 10 foreach $name ( @arr ) {
421 58 my $dispname = $name;
422 # $dispname = '' if $wrapped;
423 58 $self->_print (sprintf("%${indent}s ",$dispname));
424 58 $tempcount = $count;
425 58 $index = 0;
426 58 ▶ while( ($tempcount + 10 < $length) && ($index < 5) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tempcount + 10 < $length and $index < 5) 268 58 326
Condition100%3/3
$tempcount + 10 < $length and $index < 5
!ll&&!rl&&r
1048268
MC/DC100%2/2
$tempcount + 10 < $length and $index < 5
$tempcount + 10 < $length $index < 5
Truth table100%3/3
A and B
A= $tempcount + 10 < $lengthB= $index < 5
ABresult
0X0
100
111
427 268 $self->_print (sprintf("%s ",substr($hash{$name},$tempcount,10)));
428 268 $tempcount += 10;
429 268 $index++;
430 }
431 # last
432 58 ▶ if( $index < 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < 5) 10 48 58
433 # space to print!
434 10 $self->_print (sprintf("%s ",substr($hash{$name},$tempcount)));
435 10 $tempcount += 10;
436 }
437 58 $self->_print ("\n");
438 }
439 10 $self->_print ("\n\n");
440 10 $count = $tempcount;
441 10 $wrapped = 1;
442 }
443 2 ▶ if( $self->flag('endblock') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->flag('endblock')) { } 2 0 2
444 2 $self->_print (";\n\nendblock;\n");
445 } else {
446 0 $self->_print (";\n\nend;\n");
447 }
448 }
449 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
450 2 return 1;
451 }
452  
453 =head2 flag
454  
455 Title : flag
456 Usage : $obj->flag($name,$value)
457 Function: Get/Set a flag value
458 Returns : value of flag (a scalar)
459 Args : on set, new value (a scalar or undef, optional)
460  
461  
462 =cut
463  
464 sub flag{
465 70 ▶ my ($self,$name,$val) = @_;
Subroutine flag: called CC=2 SCAR=6.9
Pod documented
466 70 ▶ return $self->{'flag'}->{$name} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 66 4 70
467 4 return $self->{'flag'}->{$name};
468 }
469  
470 3 1;
« Bio/Index/EMBL.pm ↑ Summary Bio/Root/Utilities.pm »