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.8';
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 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
77 3 %valid_type = map {$_, 1} qw( dna rna protein standard );
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 36 ▶sub _initialize {
Subroutine _initialize: called CC=3 SCAR=11.0
105 36 my ($self, @args) = @_;
106 36 $self->SUPER::_initialize(@args);
107 36 my ($show_symbols, $endblock) =
108 $self->_rearrange([qw(SHOW_SYMBOLS SHOW_ENDBLOCK)], @args);
109 36 my @names = qw(symbols endblock);
110 36 for my $v ( $show_symbols, $endblock ) {
111 72 ▶ $v = 1 unless defined $v; # default value is 1
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 72 0 72
112 72 my $n = shift @names;
113 72 $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 33 ▶sub next_aln {
Subroutine next_aln: called CC=72 SCAR=45.5
Pod documented
144 33 my $self = shift;
145 33 my $entry;
146 33 my ($aln_name, $seqcount, $residuecount, %hash, $alphabet,
147 $match, $gap, $missing, $equate, $interleave,
148 $name,$str,@names,$seqname,$start,$end,$count,$seq);
149 33 local $Bio::LocatableSeq::OTHER_SYMBOLS = '\*\?\.';
150 33 local $Bio::LocatableSeq::GAP_SYMBOLS = '\-';
151 33 my $aln = Bio::SimpleAlign->new(-source => 'nexus');
152  
153 # file starts with '#NEXUS' but we allow white space only lines before it
154 33 $entry = $self->_readline;
155 33 ▶ $entry = $self->_readline while defined $entry && $entry =~ /^\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
defined $entry and $entry =~ /^\s+$/ and do { $entry = $self->_readline } 0 33 33
Condition33%1/3
defined $entry and $entry =~ /^\s+$/
!ll&&!rl&&r
0330
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 33 ▶ return unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 33 33
158 33 ▶ $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 33 33
Condition33%1/3
$entry and $entry =~ /^#NEXUS/i
!ll&&!rl&&r
0033
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 33 ▶ while (defined($entry = $self->_readline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($entry = $self->_readline)) 97 0 97
164 97 local ($_) = $entry;
165 97 ▶ /\[TITLE. *([^\]]+)]\s+/i and $aln_name = $1;
Branch: 1 branch
BranchTrueFalseTotal
/\[TITLE. *([^\]]+)]\s+/i and $aln_name = $1 14 83 97
166 97 ▶ last if /^begin +data/i || /^begin +taxa/i;
Branch: 1 branch
BranchTrueFalseTotal
if /^begin +data/i or /^begin +taxa/i 33 64 97
Condition100%3/3
/^begin +data/i or /^begin +taxa/i
l!l&&r!l&&!r
201364
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 33 ▶ $aln_name =~ s/\s/_/g and $aln->id($aln_name) if $aln_name;
Branch: 1 branch
BranchTrueFalseTotal
if $aln_name 14 19 33
Condition50%3/6
$aln_name && ($aln_name =~ s/\s/_/g && $aln->id($aln_name))
!ll&&!rl&&r
19140
$aln_name =~ s/\s/_/g and $aln->id($aln_name)
!ll&&!rl&&r
0140
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 33 my $incomment;
172 33 ▶ while (defined ($entry = $self->_readline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($entry = $self->_readline)) 446 0 446
173 446 local ($_) = $entry;
174 446 ▶ next if s/\[[^\]]+\]//g; # remove comments
Branch: 1 branch
BranchTrueFalseTotal
if s/\[[^\]]+\]//g 182 264 446
175 264 ▶ if( s/\[[^\]]+$// ) {
Branch: 3 branches
BranchTrueFalseTotal
if (s/\[[^\]]+$//) { } 6 258 264
elsif ($incomment) { } 39 219 258
elsif (/taxlabels/i) { } 13 206 219
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 206 ▶ /ntax\s*=\s*(\d+)/i and $seqcount = $1;
Branch: 1 branch
BranchTrueFalseTotal
/ntax\s*=\s*(\d+)/i and $seqcount = $1 33 173 206
191 206 ▶ /nchar\s*=\s*(\d+)/i and $residuecount = $1;
Branch: 1 branch
BranchTrueFalseTotal
/nchar\s*=\s*(\d+)/i and $residuecount = $1 33 173 206
192 206 ▶ /matchchar\s*=\s*(.)/i and $match = $1;
Branch: 1 branch
BranchTrueFalseTotal
/matchchar\s*=\s*(.)/i and $match = $1 2 204 206
193 206 ▶ /gap\s*=\s*(.)/i and $gap = $1;
Branch: 1 branch
BranchTrueFalseTotal
/gap\s*=\s*(.)/i and $gap = $1 33 173 206
194 206 ▶ /missing\s*=\s*(.)/i and $missing = $1;
Branch: 1 branch
BranchTrueFalseTotal
/missing\s*=\s*(.)/i and $missing = $1 32 174 206
195 206 ▶ /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 206 206
196 206 ▶ /datatype\s*=\s*(\w+)/i and $alphabet = lc $1;
Branch: 1 branch
BranchTrueFalseTotal
/datatype\s*=\s*(\w+)/i and $alphabet = lc $1 33 173 206
197 206 ▶ /interleave/i and $interleave = 1 ;
Branch: 1 branch
BranchTrueFalseTotal
/interleave/i and $interleave = 1 18 188 206
198 206 ▶ last if /matrix/io;
Branch: 1 branch
BranchTrueFalseTotal
if /matrix/io 33 173 206
199 }
200 }
201 33 ▶ $self->throw("Not a valid NEXUS sequence file. Datatype not specified.")
Branch: 1 branch
BranchTrueFalseTotal
unless $alphabet 0 33 33
202 unless $alphabet;
203 $self->throw("Not a valid NEXUS sequence file. Datatype should not be [$alphabet]")
204 33 ▶ unless $valid_type{$alphabet};
Branch: 1 branch
BranchTrueFalseTotal
unless $Bio::AlignIO::nexus::valid_type{$alphabet} 0 33 33
205 33 ▶ $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 33 33
Condition33%1/3
$gap and $gap =~ m"[\(\)\[\]\{\}/\\\,\;\:\=\*\'\`\<\>\^]"
!ll&&!rl&&r
0330
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 33 ▶ $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 33 33
Condition66%2/3
$missing and $missing =~ m"[\(\)\[\]\{\}/\\\,\;\:\=\*\'\`\<\>\^]"
!ll&&!rl&&r
1320
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 33 $aln->gap_char($gap);
211 33 $aln->missing_char($missing);
212  
213 #
214 # if data is not right after the matrix line
215 # read the empty lines out
216 #
217 33 ▶ while ($entry = $self->_readline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry = $self->_readline) 36 0 36
218 36 ▶ unless ($entry =~ /^\s+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry =~ /^\s+$/) 33 3 36
219 33 $self->_pushback($entry);
220 33 last;
221 }
222 }
223  
224 #
225 # matrix command
226 #
227 # first alignment section
228 33 ▶ if (@names == 0) { # taxa block did not exist
Branch: 1 branch
BranchTrueFalseTotal
if (@names == 0) 33 0 33
229 33 ▶ while ($entry = $self->_readline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry = $self->_readline) 522 0 522
230 522 local ($_) = $entry;
231 522 ▶ if( s/\[[^\]]+\]//g ) { #] remove comments
Branch: 1 branch
BranchTrueFalseTotal
if (s/\[[^\]]+\]//g) 205 317 522
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 514 ▶ if ($interleave && defined$count && ($count <= $seqcount)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interleave and defined $count and $count <= $seqcount) { } 122 392 514
Condition83%5/6
$interleave and defined $count and $count <= $seqcount
!ll&&!rl&&r
3920122
$interleave and defined $count
!ll&&!rl&&r
37220122
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 122 ▶ /^\s+$/ and last;
Branch: 1 branch
BranchTrueFalseTotal
/^\s+$/ and last 18 104 122
237 } else {
238 392 ▶ /^\s+$/ and next;
Branch: 1 branch
BranchTrueFalseTotal
/^\s+$/ and next 4 388 392
239 }
240 492 ▶ /^\s*;/ and last; # stop if colon at end of matrix is on it's own line
Branch: 1 branch
BranchTrueFalseTotal
/^\s*;/ and last 6 486 492
241 #/^\s*;\s*$/ and last;
242 486 ▶ if ( /^\s*([\"\'](.+?)[\"\']|(\S+))\s+(.*)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*([\"\'](.+?)[\"\']|(\S+))\s+(.*)\s*$/) 486 0 486
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 486 ▶ $name = ($2 || $3);
Condition66%2/3
$2 || $3
l!l&&r!l&&!r
414450
MC/DC0%0/2
$2 || $3
$2 $3
Truth table66%2/3
A || B
A= $2B= $3
ABresult
1X1
011
000
247 486 ▶ if ($4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($4) { } 476 10 486
248 # seq is on same line as name
249 # this is the usual NEXUS format
250 476 $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 486 $name =~ s/ /_/g;
263 486 push @names, $name;
264  
265 486 $str =~ s/[\s;]//g;
266 486 $count = @names;
267 486 $hash{$count} = $str;
268 }
269 486 ▶ $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 486 486
270 486 ▶ /;/ 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 477 486
271 }
272 }
273  
274 # interleaved sections
275 33 $count = 0;
276 33 ▶ if ( $interleave ) { # only read next section if file is interleaved
Branch: 1 branch
BranchTrueFalseTotal
if ($interleave) 18 15 33
277 18 ▶ while( $entry = $self->_readline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry = $self->_readline) 1058 0 1058
278 1058 local ($_) = $entry;
279 1058 ▶ if( s/\[[^\]]+\]//g ) { #] remove comments
Branch: 1 branch
BranchTrueFalseTotal
if (s/\[[^\]]+\]//g) 826 232 1058
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 1000 ▶ /^\s*;/ and last; # stop if colon at end of matrix is on it's own line
Branch: 1 branch
BranchTrueFalseTotal
/^\s*;/ and last 17 983 1000
283 983 ▶ $count = 0, next if $entry =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /^\s*$/ 67 916 983
284 916 ▶ if (/^\s*(\'([^\']*?)\'|([^\']\S*))\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(\'([^\']*?)\'|([^\']\S*))\s+(.*)$/) 916 0 916
285 916 $str = $4;
286 916 $str =~ s/[\s;]//g;
287 916 $count++;
288 916 $hash{$count} .= $str;
289 };
290 916 ▶ $self->throw("Not a valid interleaved NEXUS file!
Branch: 1 branch
BranchTrueFalseTotal
if $count > $seqcount 0 916 916
291 seqcount [$count] > predeclared [$seqcount] ") if $count > $seqcount;
292 916 ▶ /;/ 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 915 916
293 }
294 }
295
296 33 ▶ return if @names < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @names < 1 0 33 33
297
298 # sequence creation
299 33 $count = 0;
300 33 foreach $name ( @names ) {
301 486 $count++;
302 486 ▶ if( $name =~ /(\S+)\/(\d+)-(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[(\S+)/(\d+)-(\d+)]) { } 0 486 486
303 0 ($seqname,$start,$end) = ($1,$2,$3);
304 } else {
305 486 ($seqname,$start,$str) = ($name,1,$hash{$count});
306 486 $str =~ s/[$Bio::LocatableSeq::GAP_SYMBOLS]//g;
307 486 $end = length($str);
308 }
309
310 # consistency test
311 $self->throw("Length of sequence [$seqname] is not [$residuecount]; got".CORE::length($hash{$count}))
312 486 ▶ unless CORE::length($hash{$count}) == $residuecount;
Branch: 1 branch
BranchTrueFalseTotal
unless length $hash{$count} == $residuecount 0 486 486
313
314 486 $seq = Bio::LocatableSeq->new('-seq' => $hash{$count},
315 '-display_id' => $seqname,
316 '-start' => $start,
317 '-end' => $end,
318 '-alphabet' => $alphabet
319 );
320 486 $aln->add_seq($seq);
321 }
322  
323 # if matchchar is used
324 33 ▶ $aln->unmatch($match) if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 2 31 33
325  
326 # if equate ( e.g. equate="T=C G=A") is used
327 33 ▶ if ($equate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($equate) 0 33 33
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 33 ▶ while (defined $entry &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry and not $entry =~ /endblock/i) 475 33 508
Condition100%3/3
defined $entry and not $entry =~ /endblock/i
!ll&&!rl&&r
1914475
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 475 $entry = $self->_readline;
334 }
335  
336 33 ▶ return $aln if $aln->num_sequences;
Branch: 1 branch
BranchTrueFalseTotal
if $aln->num_sequences 33 0 33
337 0 return;
338 }
339  
340 0 ▶sub _read_taxlabels {
Subroutine _read_taxlabels: not called CC=4 SCAR=30.0
341 0 my ($self) = @_;
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 2 ▶sub write_aln {
Subroutine write_aln: called CC=20 SCAR=31.2
Pod documented
369 2 my ($self,@aln) = @_;
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 76 ▶sub flag{
Subroutine flag: called CC=2 SCAR=6.9
Pod documented
465 76 my ($self,$name,$val) = @_;
466 76 ▶ return $self->{'flag'}->{$name} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 72 4 76
467 4 return $self->{'flag'}->{$name};
468 }
469  
470 3 1;
« Bio/Index/EMBL.pm ↑ Summary Bio/Root/Utilities.pm »