lib/Bio/NEXUS/CharactersBlock.pm

filter: Statementstmt 80.2% 232 / 289 Branchbran 73.4% 72 / 98 Conditioncond 68.5% 37 / 54 MC/DCmcdc 43.3% 13 / 30 Subroutinesub 90.9% 30 / 33 Podpod 100% 16 / 16 totaltotal 76.9% 400 / 520 CC 83 SCAR 29.2
CC
83
Cov
77%
_parse_matrix
40.0
_parse_statelabels
30.0
_write_labels
29.7
CRAP 18.6
« lib/Bio/NEXUS/Tree.pm ↑ Summary lib/Bio/NEXUS/UnalignedBlock.pm »
1 #######################################################################
2 # CharactersBlock.pm
3 #######################################################################
4 #
5 # $Id: CharactersBlock.pm,v 1.82 2008/04/24 19:07:25 astoltzfus Exp $
6 #
7 #################### START POD DOCUMENTATION ##########################
8  
9 =head1 NAME
10  
11 Bio::NEXUS::CharactersBlock - Represents a CHARACTERS Block (Data or Characters) of a NEXUS file
12  
13 =head1 SYNOPSIS
14  
15 $block_object = new Bio::NEXUS::CharactersBlock($type, $block, $verbose, $taxlabels_ref);
16  
17 =head1 DESCRIPTION
18  
19 This is a class representing a Characters Block in a NEXUS file. Characters Blocks generally contain state data for a set of characters for each taxon in the Taxa Block. One common use of a Characters Block is to house multiple sequence alignments.
20  
21 =head1 FEEDBACK
22  
23 All feedbacks (bugs, feature enhancements, etc.) are greatly appreciated.
24  
25 =head1 AUTHORS
26  
27 Chengzhi Liang (liangc@umbi.umd.edu)
28 Weigang Qiu (weigang@genectr.hunter.cuny.edu)
29 Eugene Melamud (melamud@carb.nist.gov)
30 Peter Yang (pyang@rice.edu)
31 Thomas Hladish (tjhladish at yahoo)
32  
33 =head1 VERSION
34  
35 $Revision: 1.82 $
36  
37 =head1 METHODS
38  
39 =cut
40  
41 package Bio::NEXUS::CharactersBlock;
42 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 # use Data::Dumper; # used for debugging only
44 # use Carp; # for debugging only
45 35 ▶use Bio::NEXUS::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 35 ▶use Bio::NEXUS::TaxUnitSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 35 ▶use Bio::NEXUS::Matrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 35 ▶use Bio::NEXUS::Util::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 35 ▶use Bio::NEXUS::Util::Exceptions 'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 35 ▶use vars qw(@ISA $VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 35 ▶use Bio::NEXUS; $VERSION = $Bio::NEXUS::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 35 @ISA = qw(Bio::NEXUS::Matrix);
55 35 my $logger = Bio::NEXUS::Util::Logger->new();
56  
57 =head2 new
58  
59 Title : new
60 Usage : block_object = new Bio::NEXUS::CharactersBlock($block_type, $commands, $verbose, $taxa);
61 Function: Creates a new Bio::NEXUS::CharactersBlock object
62 Returns : Bio::NEXUS::CharactersBlock object
63 Args : type (string), the commands/comments to parse (array ref), and a verbose flag (0 or 1)
64  
65 =cut
66  
67 73 ▶sub new {
Subroutine new: called CC=4 SCAR=14.8
Pod documented
68 73 my ( $class, $type, $commands, $verbose, $taxa ) = @_;
69 73 ▶ if ( not $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 73 73
70 0 ( $type = lc $class ) =~ s/Bio::NEXUS::(.+)Block/$1/i;
71 }
72 73 my $self = {
73 'type' => $type
74 };
75 73 bless $self, $class;
76 73 $self->set_taxlabels($taxa);
77 73 $self->{'otuset'} = Bio::NEXUS::TaxUnitSet->new();
78 73 ▶ if ( ( defined $commands ) and @$commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $commands and @$commands) 73 0 73
Condition33%1/3
defined $commands and @$commands
!ll&&!rl&&r
0073
MC/DC0%0/2
defined $commands and @$commands
defined $commands @$commands
Truth table33%1/3
A and B
A= defined $commandsB= @$commands
ABresult
0X0
100
111
79 73 $self->_parse_block( $commands, $verbose )
80 }
81  
82 70 return $self;
83 }
84  
85 72 ▶sub _post_processing {
Subroutine _post_processing: called CC=3 SCAR=11.1
86 72 my ($self) = @_;
87  
88 # We prefer using the more versatile/expressive character-state labels,
89 # rather than state labels
90 72 ▶ if ( $self->get_statelabels() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_statelabels) 72 0 72
91 72 $self->add_states_to_charstates( $self->get_statelabels() );
92 72 delete $self->get_otuset->{'statelabels'};
93 }
94  
95 # The 'ntax' subcommand is not required unless the 'newtaxa' subcommand has
96 # been used
97 72 my $dimensions = $self->get_dimensions();
98 72 ▶ if ( !$dimensions->{'newtaxa'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dimensions->{"newtaxa"}) 72 0 72
99 72 delete $dimensions->{'ntax'};
100 72 $self->set_dimensions($dimensions);
101 }
102  
103 72 return;
104 }
105  
106 =begin comment
107  
108 Title : _parse_charstatelabels
109 Usage : $self->_parse_charstatelabels($buffer);
110 Function: Parses the buffer containing character labels, stores it
111 Returns : none
112 Args : buffer (string)
113 Method : parse a charstatelabels command in Characters Block and store in hash
114  
115 =end comment
116  
117 =cut
118  
119 # NOTE: format of charstatelabel is comma-separated list of elements, where each element
120 # has the form <char_number> <opt:char_label> / <opt:statelabel> <opt:more_statelabels>
121  
122 # NOTE: the parse method below is not elegant, but its rational and so far its robust to tests
123  
124 3 ▶sub _parse_charstatelabels {
Subroutine _parse_charstatelabels: called CC=7 SCAR=19.5
125 3 my ( $self, $buffer ) = @_;
126 3 my $command_tokens = _parse_nexus_words($buffer);
127 3 my @out;
128 # print "command_tokens: ", Dumper @$command_tokens;
129  
130 3 my ($this_token, $this_element_token );
131 3 my ( @this_element_tokens, @this_half_tokens );
132 3 my ( $char_id, $char_label );
133 3 ▶ while ( $this_token = shift( @$command_tokens ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_token = shift @$command_tokens) 138 3 141
134 138 ▶ if ( $this_token eq ',' || $#$command_tokens == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_token eq ',' or $command_tokens->$#* == -1) { } 18 120 138
Condition100%3/3
$this_token eq "," or $command_tokens->$#* == -1
l!l&&r!l&&!r
162120
MC/DC100%2/2
$this_token eq "," or $command_tokens->$#* == -1
$this_token eq "," $command_tokens->$#* == -1
Truth table100%3/3
A or B
A= $this_token eq ","B= $command_tokens->$#* == -1
ABresult
1X1
011
000
135 18 ▶ if ( $#$command_tokens == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command_tokens->$#* == -1) 3 15 18
136 3 push ( @this_element_tokens, $this_token );
137 }
138 # print "processing this element . . . ", Dumper @this_element_tokens;
139 # process this_element_tokens to yield id, label, and state labels
140 18 ▶ while ( $this_element_token = shift( @this_element_tokens ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_element_token = shift @this_element_tokens) 123 18 141
141 123 ▶ if ( $this_element_token eq '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_element_token eq '/') { } 19 104 123
142 # print "processing char half. . . ", Dumper @this_half_tokens;
143 19 $char_id = shift(@this_half_tokens);
144 # label may be empty, but thats ok
145 19 $char_label = shift(@this_half_tokens);
146 # print "char_id = $char_id, char_label = $char_label\n";
147 }
148 else {
149 104 push( @this_half_tokens, $this_element_token );
150 }
151 }
152 18 push @out, $self->create_charstates( $char_id, $char_label, \@this_half_tokens );
153 18 @this_half_tokens = ();
154 # print "latest character: ", Dumper \$out[$#out];
155 18 $char_id = undef;
156 18 $char_label = undef;
157 }
158 else {
159 120 push( @this_element_tokens, $this_token );
160 }
161 }
162 3 $self->get_otuset->set_charstatelabels( \@out );
163 # print "\nout: ", Dumper @out;
164  
165 3 return;
166 }
167  
168 =begin comment
169  
170 Title : _parse_charlabels
171 Usage : $self->_parse_charlabels($buffer);
172 Function: Parses the buffer containing character labels, stores it
173 Returns : array of charstates
174 Args : buffer (string)
175 Method : Gets rid of leading blanks in the buffer and removes the
176 semicolon. Splits the buffer by whitespace into a list of
177 character labels and assigns that to charlabels.
178  
179 =end comment
180  
181 =cut
182  
183 35 ▶sub _parse_charlabels {
Subroutine _parse_charlabels: called CC=2 SCAR=6.9
184 35 my ( $self, $labels ) = @_;
185 35 my $id = 0;
186 35 my @charstates;
187 35 my @charlabels = @{ _parse_nexus_words($labels) };
188 35 for my $charlabel (@charlabels) {
189 220 push @charstates, $self->create_charstates( ++$id, $charlabel );
190 }
191 35 $self->set_charlabels( \@charlabels );
192 35 $self->set_charstatelabels( \@charstates );
193 35 return \@charstates;
194 }
195  
196 =begin comment
197  
198 Title : _parse_statelabels
199 Usage : $self->_parse_statelabels($buffer);
200 Function: Parses the buffer containing state labels, stores it
201 Returns : array of states
202 Args : buffer (string)
203 Method : parse a statelabels command in Characters Block and store in hash
204  
205 =end comment
206  
207 =cut
208  
209 0 ▶sub _parse_statelabels {
Subroutine _parse_statelabels: not called CC=5 SCAR=34.0
210 0 my ( $self, $buffer ) = @_;
211 0 my @states;
212 0 my ( $charnum, @statenames );
213 0 my @statetokens = @{ _parse_nexus_words($buffer) };
214 0 for my $token (@statetokens) {
215 0 ▶ if ( $token =~ /^\d+$/ && !$charnum > 0 ) { $charnum = $token; next; }
Branch: 2 branches
BranchTrueFalseTotal
if ($token =~ /^\d+$/ and !$charnum > 0) { } 0 0 0
elsif ($token =~ /^,$/) { } 0 0 0
Condition0%0/3
$token =~ /^\d+$/ and !$charnum > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$token =~ /^\d+$/ and !$charnum > 0
$token =~ /^\d+$/ !$charnum > 0
Truth table0%0/3
A and not(B)
A= $token =~ /^\d+$/B= !$charnum > 0
ABresult
0X0
100
111
216 elsif ( $token =~ /^,$/ ) {
217 0 push @states,
218 $self->create_charstates( $charnum, "", \@statenames );
219 0 $charnum = "";
220 0 @statenames = ();
221 }
222 else {
223 0 push @statenames, $token;
224 }
225 }
226 0 $self->set_statelabels( \@states );
227 0 return \@states;
228 }
229  
230 =head2 add_states_to_charstates
231  
232 Title : add_states_to_charstates
233 Usage : $self->add_states_to_charstates($states);
234 Function: Adds states to the character states
235 Returns : None
236 Args : states
237  
238 =cut
239  
240 72 ▶sub add_states_to_charstates {
Subroutine add_states_to_charstates: called CC=5 SCAR=19.6
Pod documented
241 72 my ( $self, $states ) = @_;
242 72 my $newstates;
243 72 my $charstates = $self->get_charstatelabels();
244 72 ▶ if ( !@$charstates ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$charstates) 34 38 72
245 34 $self->set_charstatelabels($states);
246 34 return;
247 }
248 38 STATE: for my $state (@$states) {
249 0 for my $charstate (@$charstates) {
250 0 ▶ if ( $state->{'id'} == $charstate->{'id'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"id"} == $charstate->{"id"}) 0 0 0
251 0 $charstate->{'states'} = $state->{'states'};
252 0 next STATE;
253 }
254 }
255 0 splice @$charstates, $state->{'id'} - 1, 1, $state;
256 }
257 }
258  
259 =head2 create_charstates
260  
261 Title : create_charstates
262 Usage : my $char_state_hash = $self->create_charstates($id,$label,$states);
263 Function: Converts the input id, label, states to an hash ref for processing.
264 Returns : Hash reference with (id, charlabel,states as keys)
265 Args : id, label, states
266  
267 =cut
268  
269 238 ▶sub create_charstates {
Subroutine create_charstates: called CC=3 SCAR=11.0
Pod documented
270 238 my ( $self, $id, $label, $states ) = @_;
271 238 my %states;
272 238 ▶ for ( my $i = 0; $i < @{ $states || [] }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$states or [];}) 66 238 304
Condition100%2/2
$states || []
l!l
84220
MC/DC100%1/1
$states || []
$states
Truth table100%2/2
A
A= $states
Aresult
11
00
273 66 $states{$i} = $states->[$i];
274 }
275 return {
276 238 'id' => $id,
277 'charlabel' => $label,
278 'states' => \%states
279
280 };
281 }
282  
283 =head2 find_taxon
284  
285 Title : find_taxon
286 Usage : my $is_taxon_present = $self->find_taxon($taxon_name);
287 Function: Finds whether the input taxon name is present in the taxon label.
288 Returns : 0 (not present) or 1 (if present).
289 Args : taxon label (as string)
290  
291 =cut
292  
293 533 ▶sub find_taxon {
Subroutine find_taxon: called CC=5 SCAR=16.1
Pod documented
294 533 my ( $self, $name ) = @_;
295 533 ▶ if ( @{ $self->get_taxlabels || [] } == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->get_taxlabels or [];} == 0) 147 386 533
Condition50%1/2
$self->get_taxlabels || []
l!l
5330
MC/DC0%0/1
$self->get_taxlabels || []
$self->get_taxlabels
Truth table50%1/2
A
A= $self->get_taxlabels
Aresult
11
00
296 386 for my $taxon ( @{ $self->get_taxlabels() } ) {
297 1623 ▶ if ( lc $taxon eq lc $name ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (lc $taxon eq lc $name) 376 1247 1623
298 }
299 10 return 0;
300 }
301  
302 =head2 set_otuset
303  
304 Title : set_otuset
305 Usage : $block->set_otuset($otuset);
306 Function: Set the otus
307 Returns : none
308 Args : TaxUnitSet object
309  
310 =cut
311  
312 0 ▶sub set_otuset {
Subroutine set_otuset: not called CC=1 SCAR=6.9
Pod documented
313 0 my ( $self, $otuset ) = @_;
314 0 $self->{'otuset'} = $otuset;
315 0 $self->set_taxlabels( $otuset->get_otu_names() );
316 0 return;
317 }
318  
319 =head2 add_otu_clone
320  
321 Title : add_otu_clone
322 Usage : ...
323 Function: ...
324 Returns : ...
325 Args : ...
326  
327 =cut
328  
329 8 ▶sub add_otu_clone {
Subroutine add_otu_clone: called CC=5 SCAR=16.3
Pod documented
330 8 my ( $self, $original_otu_name, $copy_otu_name ) = @_;
331 # print "Warning: Bio::NEXUS::CharactersBlock::add_otu_clone() method not fully implemented\n";
332
333 8 ▶ if ( $self->find_taxon($copy_otu_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->find_taxon($copy_otu_name)) { } 0 8 8
334 0 throw 'ObjectMismatch' => "OTU with that name [$copy_otu_name] already exists";
335 }
336 else {
337 8 $self->add_taxlabel($copy_otu_name);
338 8 my @otu_set = @{ $self->{'otuset'}->{'otus'} };
339 8 for my $otu (@otu_set) {
340 60 ▶ if (defined $otu) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $otu) 60 0 60
341 60 ▶ if ( $otu->get_name() eq $original_otu_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otu->get_name eq $original_otu_name) 8 52 60
342 8 my $otu_clone = $otu->clone();
343 8 $otu_clone->set_name($copy_otu_name);
344 8 $self->{'otuset'}->add_otu($otu_clone);
345 }
346 }
347 }
348 }
349 }
350  
351 =head2 set_charstatelabels
352  
353 Title : set_charstatelabels
354 Usage : $block->set_charstatelabels($labels);
355 Function: Set the character names and states
356 Returns : none
357 Args : array of character states
358  
359 =cut
360  
361 69 ▶sub set_charstatelabels {
Subroutine set_charstatelabels: called CC=1 SCAR=0.0
Pod documented
362 69 my ( $self, $charstatelabels ) = @_;
363 69 $self->get_otuset->set_charstatelabels($charstatelabels);
364 69 return;
365 }
366  
367 =head2 get_charstatelabels
368  
369 Title : get_charstatelabels
370 Usage : $set->get_charstatelabels();
371 Function: Returns an array of character states
372 Returns : character states
373 Args : none
374  
375 =cut
376  
377 76 ▶sub get_charstatelabels { shift->get_otuset->get_charstatelabels() }
Subroutine get_charstatelabels: called CC=1 SCAR=0.0
Pod documented
378  
379 =head2 set_charlabels
380  
381 Title : set_charlabels
382 Usage : $set->set_charlabels($labels);
383 Function: Set the character names
384 Returns : none
385 Args : array of character names
386  
387 =cut
388  
389 35 ▶sub set_charlabels {
Subroutine set_charlabels: called CC=1 SCAR=0.0
Pod documented
390 35 my ( $self, $labels ) = @_;
391 35 $self->get_otuset()->set_charlabels($labels);
392 }
393  
394 =head2 get_charlabels
395  
396 Title : get_charlabels
397 Usage : $set->get_charlabels();
398 Function: Returns an array of character labels
399 Returns : character names
400 Args : none
401  
402 =cut
403  
404 5 ▶sub get_charlabels { shift->get_otuset()->get_charlabels() }
Subroutine get_charlabels: called CC=1 SCAR=0.0
Pod documented
405  
406 =head2 set_statelabels
407  
408 Title : set_statelabels
409 Usage : $set->set_statelabels($labels);
410 Function: Set the state names
411 Returns : none
412 Args : array of state names
413  
414 =cut
415  
416 0 ▶sub set_statelabels {
Subroutine set_statelabels: not called CC=1 SCAR=6.9
Pod documented
417 0 my ( $self, $labels ) = @_;
418 0 $self->get_otuset()->set_statelabels($labels);
419 }
420  
421 =head2 get_statelabels
422  
423 Title : get_statelabels
424 Usage : $set->get_statelabels();
425 Function: Returns an array of stateacter labels
426 Returns : stateacter names
427 Args : none
428  
429 =cut
430  
431 144 ▶sub get_statelabels { shift->get_otuset()->get_statelabels() }
Subroutine get_statelabels: called CC=1 SCAR=0.0
Pod documented
432  
433 =head2 get_nchar
434  
435 Title : get_nchar
436 Usage : $block->get_nchar();
437 Function: Returns the number of characters of the block
438 Returns : # charaters
439 Args : none
440  
441 =cut
442  
443 85 ▶sub get_nchar {
Subroutine get_nchar: called CC=3 SCAR=15.0
Pod documented
444 85 my $self = shift;
445 85 my $nchar = $self->get_dimensions('nchar');
446 85 ▶ if ( not defined $nchar ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nchar) 0 85 85
447 0 my $otuset = $self->get_otuset();
448 0 ▶ $nchar = $otuset ? $otuset->get_nchar() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$otuset ? : 0 0 0
449 0 $self->set_nchar($nchar);
450 }
451 85 return $nchar;
452 }
453  
454 =begin comment
455  
456 Title : _parse_matrix
457 Usage : $self->_parse_matrix($buffer); (private)
458 Function: Processes buffer containing matrix data
459 Returns : arrayref
460 Args : buffer (string)
461 Method : parse according to if name is quoted string or single word,
462 if each state is single character or multi-character (use token keyword)
463  
464 =end comment
465  
466 =cut
467  
468 73 ▶sub _parse_matrix {
Subroutine _parse_matrix: called CC=38 SCAR=36.9
469 73 my ( $self, $matrix, $verbose ) = @_;
470 73 my $nchar = $self->get_nchar();
471 73 my @taxlabels = @{ $self->get_taxlabels() };
472  
473 73 my %format = %{ $self->get_format() };
474  
475 73 my $expect_labels = !$format{'nolabels'};
476 73 my $expect_interleave = $format{'interleave'};
477 my $expect_tokens = $format{'tokens'}
478 73 ▶ || ( lc $format{'datatype'} eq 'continuous' );
Condition66%2/3
$format{'tokens'} || lc $format{'datatype'} eq 'continuous'
l!l&&r!l&&!r
3070
MC/DC50%1/2
$format{'tokens'} || lc $format{'datatype'} eq 'continuous'
$format{'tokens'} lc $format{'datatype'} eq 'continuous'
Truth table66%2/3
A || B
A= $format{'tokens'}B= lc $format{'datatype'} eq 'continuous'
ABresult
1X1
011
000
479  
480 73 ▶ my $missing_symbol = $format{'missing'} || q{};
Condition100%2/2
$format{'missing'} || ''
l!l
5419
MC/DC100%1/1
$format{'missing'} || ''
$format{'missing'}
Truth table100%2/2
A
A= $format{'missing'}
Aresult
11
00
481 73 ▶ my $gap_symbol = $format{'gap'} || q{};
Condition100%2/2
$format{'gap'} || ''
l!l
5617
MC/DC100%1/1
$format{'gap'} || ''
$format{'gap'}
Truth table100%2/2
A
A= $format{'gap'}
Aresult
11
00
482  
483 # statesformat is the stored value (if one exists), otherwise it's
484 # the default value ('individuals' for continuous data, 'statespresent'
485 # for others).
486 my $statesformat =
487 $format{'statesformat'} ? $format{'statesformat'}
488 73 ▶ : $format{'datatype'} eq 'continuous' ? 'individuals'
Branch: 2 branches
BranchTrueFalseTotal
$format{'statesformat'} ? : 4 69 73
$format{'datatype'} eq 'continuous' ? : 0 69 69
489 : 'statespresent';
490 73 ▶ my $expect_freq =
Branch: 1 branch
BranchTrueFalseTotal
$statesformat eq 'count' || $statesformat eq 'frequency' ? : 3 70 73
Condition66%2/3
$statesformat eq "count" or $statesformat eq "frequency"
l!l&&r!l&&!r
0370
MC/DC50%1/2
$statesformat eq "count" or $statesformat eq "frequency"
$statesformat eq "count" $statesformat eq "frequency"
Truth table66%2/3
A or B
A= $statesformat eq "count"B= $statesformat eq "frequency"
ABresult
1X1
011
000
491 ( $statesformat eq 'count' || $statesformat eq 'frequency' ) ? 1 : 0;
492  
493 # '+' and '-' are not included as punctuation because they are allowed as
494 # state symbols in a matrix; colons are used to separate states from their
495 # frequencies in polymorphisms (e.g. "(A:0.9 E:0.04 N:0.06)" )
496 73 my $punctuation_regex = qr/[\/\\,;=*"`<>]/;
497  
498 73 my ( @lines, %taxa );
499  
500 73 ▶ if ($expect_interleave) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect_interleave) { } 6 67 73
501 6 @lines = split /\n+/, $matrix;
502 }
503 else {
504  
505 # This is a funny hoop we have to jump through to avoid major code
506 # duplication
507 67 @lines = ($matrix);
508 }
509  
510 73 for my $line (@lines) {
511 235 my @words = @{ _parse_nexus_words($line) };
512 235 my $name = q{};
513 235 my $in_grouping = 0;
514  
515 # my $group_position = 0;
516 235 my $saw_colon = 0;
517 235 my $last_state = q{};
518 235 my $i = 0;
519  
520 WORD:
521 235 for my $word (@words) {
522  
523 # If it's not an interleaved matrix and we've already parsed all
524 # the states for this taxon (nchar = number parsed), then move onto
525 # the next taxon
526 5728 ▶ if ( !$expect_interleave
Branch: 1 branch
BranchTrueFalseTotal
if (not $expect_interleave || $in_grouping and $taxa{$name} and scalar @{$taxa{$name};} == $nchar) 406 5322 5728
Condition100%9/9
not $expect_interleave || $in_grouping and $taxa{$name} and scalar @{$taxa{$name};} == $nchar
!ll&&!rl&&r
25122810406
not $expect_interleave || $in_grouping and $taxa{$name}
!ll&&!rl&&r
2445673216
$expect_interleave || $in_grouping
l!l&&r!l&&!r
19115343283
MC/DC100%4/4
not $expect_interleave || $in_grouping and $taxa{$name} and scalar @{$taxa{$name};} == $nchar
$expect_interleave $in_grouping $taxa{$name} scalar @{$taxa{$name};} == $nchar
Truth table100%5/5
not(A || B) and C and D
A= $expect_interleaveB= $in_groupingC= $taxa{$name}D= scalar @{$taxa{$name};} == $nchar
ABCDresult
1XXX0
01XX0
000X0
00100
00111
527 && !$in_grouping
528 && $taxa{$name}
529 3216 && scalar @{ $taxa{$name} } == $nchar )
530 {
531 406 $name = q{};
532 }
533  
534 # If $name is empty, we're looking at the beginning of a new row
535 5728 ▶ if ( $name eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "") 639 5089 5728
536 639 ▶ if ($expect_labels) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect_labels) { } 639 0 639
537 639 $name = $word;
538 639 ▶ $taxa{$name} = [] unless exists $taxa{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $taxa{$name} 521 118 639
539 639 next WORD;
540 }
541 else {
542 0 $name = $taxlabels[ $i++ ]; # (if 'NoLabels')
543 0 $taxa{$name} = [];
544  
545 # In case we're dealing with an interleaved, unlabeled matrix,
546 # reset $i if we've passed the end of the @taxlabels array
547 0 ▶ $i = $i > $#taxlabels ? 0 : $i;
Branch: 1 branch
BranchTrueFalseTotal
$i > $#taxlabels ? : 0 0 0
548 }
549 }
550  
551 5089 ▶ if ( $word ne $missing_symbol
Branch: 5 branches
BranchTrueFalseTotal
if ($word ne $missing_symbol and $word ne $gap_symbol and $word =~ /$punctuation_regex/) { } 0 5089 5089
elsif ($word eq '(') { } 97 4992 5089
elsif ($word eq '{') { } 4 4988 4992
elsif ($word eq ')' or $word eq '}') { } 101 4887 4988
elsif ($word eq ':') { } 139 4748 4887
Condition83%5/6
$word ne $missing_symbol and $word ne $gap_symbol and $word =~ /$punctuation_regex/
!ll&&!rl&&r
256325260
$word ne $missing_symbol and $word ne $gap_symbol
!ll&&!rl&&r
107314902526
MC/DC0%0/3
$word ne $missing_symbol and $word ne $gap_symbol and $word =~ /$punctuation_regex/
$word ne $missing_symbol $word ne $gap_symbol $word =~ /$punctuation_regex/
Truth table75%3/4
A and B and C
A= $word ne $missing_symbolB= $word ne $gap_symbolC= $word =~ /$punctuation_regex/
ABCresult
0XX0
10X0
1100
1111
552 && $word ne $gap_symbol
553 && $word =~ $punctuation_regex )
554 {
555 0 next WORD;
556 }
557 elsif ( $word eq '(' ) {
558 97 push @{ $taxa{$name} },
559 { 'type' => 'polymorphism', 'states' => undef };
560 97 $in_grouping = 1;
561 }
562 elsif ( $word eq '{' ) {
563 4 push @{ $taxa{$name} },
564 { 'type' => 'uncertainty', 'states' => undef };
565 4 $in_grouping = 1;
566 }
567 ▶ elsif ( $word eq ')' || $word eq '}' ) {
Condition100%3/3
$word eq ")" or $word eq "}"
l!l&&r!l&&!r
9744887
MC/DC100%2/2
$word eq ")" or $word eq "}"
$word eq ")" $word eq "}"
Truth table100%3/3
A or B
A= $word eq ")"B= $word eq "}"
ABresult
1X1
011
000
568 101 $in_grouping = 0;
569  
570 # $group_position = 0;
571 }
572 elsif ( $word eq ':' ) {
573 139 ▶ $saw_colon = 1 if ( $in_grouping && $expect_freq );
Branch: 1 branch
BranchTrueFalseTotal
if $in_grouping and $expect_freq 139 0 139
Condition33%1/3
$in_grouping and $expect_freq
!ll&&!rl&&r
00139
MC/DC0%0/2
$in_grouping and $expect_freq
$in_grouping $expect_freq
Truth table33%1/3
A and B
A= $in_groupingB= $expect_freq
ABresult
0X0
100
111
574 }
575 else {
576 4748 ▶ if ($in_grouping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_grouping) { } 294 4454 4748
577 294 ▶ if ( !$saw_colon ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $saw_colon) { } 155 139 294
578 155 ▶ if ($expect_freq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect_freq) { } 139 16 155
579 139 $taxa{$name}->[-1]{'states'}{$word} = undef;
580 139 $last_state = $word;
581 }
582 else {
583  
584 # $taxa{$name}->[-1]{'states'}{$group_position++} = $word;
585 16 push @{ $taxa{$name}->[-1]{'states'} }, $word;
586 }
587 }
588 else {
589 139 ▶ $taxa{$name}->[-1]{'states'}{$last_state} = $word
Branch: 1 branch
BranchTrueFalseTotal
if $expect_freq 139 0 139
590 if $expect_freq;
591 139 $saw_colon = 0;
592 139 $last_state = q{};
593 }
594 }
595 else {
596 4454 ▶ my @seq = $expect_tokens ? ($word) : split //, $word;
Branch: 1 branch
BranchTrueFalseTotal
$expect_tokens ? : 27 4427 4454
597 4454 push @{ $taxa{$name} }, @seq;
598 }
599 }
600 }
601 }
602  
603 73 my $title = $self->get_title();
604 73 ▶ $title = ": $title " if $title;
Branch: 1 branch
BranchTrueFalseTotal
if $title 14 59 73
605 73 my (@otus);
606  
607 73 ▶ while ( my ( $name, $seq ) = each %taxa ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %taxa) 521 72 593
608 521 ▶ unless ( $self->find_taxon($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->find_taxon($name)) 1 520 521
609 1 ▶ $title ||= '';
Condition50%1/2
$title ||= ""
l!l
01
MC/DC0%0/1
$title ||= ""
$title
Truth table50%1/2
A
A= $title
Aresult
11
00
610 1 Bio::NEXUS::Util::Exceptions::BadArgs->throw(
611 'error' => "Characters$title block error...\n"
612 . "Unknown taxon '$name\' encountered in matrix. "
613 . "Common causes include: Misspelled names, "
614 . "sequence lengths that don't match the specified number of characters (nchar), "
615 . "including a taxon that is not listed in the Taxa Block, "
616 . "and not quoting names with whitespace or punctuation"
617 );
618 }
619 520 push @otus, Bio::NEXUS::TaxUnit->new( $name, $seq );
620  
621 }
622  
623 72 my $otuset = $self->get_otuset();
624 72 $otuset->set_otus( \@otus );
625 72 $self->set_taxlabels( $otuset->get_otu_names() );
626 72 return \@otus;
627 }
628  
629 =head2 select_columns
630  
631 Title : select_columns
632 Usage : $block->select_columns($columns);
633 Function: select a subset of characters
634 Returns : new $self with subset of columns of characters
635 Args : column numbers
636  
637 =cut
638  
639 1 ▶sub select_columns {
Subroutine select_columns: called CC=1 SCAR=0.0
Pod documented
640 1 my ( $self, $columns ) = @_;
641 1 my $otuset = $self->get_otuset();
642 1 $otuset->select_columns($columns);
643 1 $self->set_nchar( $otuset->get_nchar );
644 1 return $self;
645 }
646  
647 =head2 rename_otus
648  
649 Title : rename_otus
650 Usage : $block->rename_otus(\%translation);
651 Function: Renames all the OTUs to something else
652 Returns : none
653 Args : hash containing translation
654  
655 =cut
656  
657 4 ▶sub rename_otus {
Subroutine rename_otus: called CC=1 SCAR=0.0
Pod documented
658 4 my ( $self, $translation ) = @_;
659 4 $self->get_otuset()->rename_otus($translation);
660 }
661  
662 =head2 equals
663  
664 Name : equals
665 Usage : $block->equals($another);
666 Function: compare if two Bio::NEXUS::CharactersBlock objects are equal
667 Returns : boolean
668 Args : a Bio::NEXUS::CharactersBlock object
669  
670 =cut
671  
672 9 ▶sub equals {
Subroutine equals: called CC=2 SCAR=7.6
Pod documented
673 9 my ( $self, $block ) = @_;
674 0 ▶ if ( ! $self->SUPER::equals($block) ) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->SUPER::equals($block)) 0 9 9
675 9 return $self->get_otuset()->equals( $block->get_otuset() );
676 }
677  
678 =begin comment
679  
680 Name : _write
681 Usage : $block->_write();
682 Function: Writes NEXUS block containing character data
683 Returns : none
684 Args : file handle
685  
686 =end comment
687  
688 =cut
689  
690 2 ▶sub _write {
Subroutine _write: called CC=2 SCAR=7.0
691 2 my ( $self, $fh, $verbose ) = @_;
692 2 ▶ $fh ||= \*STDOUT;
Condition50%1/2
$fh ||= \*STDOUT
l!l
20
MC/DC0%0/1
$fh ||= \*STDOUT
$fh
Truth table50%1/2
A
A= $fh
Aresult
11
00
693  
694 2 Bio::NEXUS::Block::_write( $self, $fh );
695 2 $self->_write_dimensions( $fh, $verbose );
696 2 $self->_write_format( $fh, $verbose );
697 2 $self->_write_labels( $fh, $verbose );
698 2 $self->_write_matrix( $fh, $verbose );
699 2 print $fh "END;\n";
700 }
701  
702 =begin comment
703  
704 Name : _write_labels
705 Usage : $self->_write_labels($file_handle,$verbose);
706 Function: Writes Character labels and Character-State labels to the filehandle
707 Returns : none
708 Args : $file_handle and $verbose
709  
710 =end comment
711  
712 =cut
713  
714 2 ▶sub _write_labels {
Subroutine _write_labels: called CC=8 SCAR=33.9
715 2 my ( $self, $fh, $verbose ) = @_;
716 2 ▶ $fh ||= \*STDOUT;
Condition50%1/2
$fh ||= \*STDOUT
l!l
20
MC/DC0%0/1
$fh ||= \*STDOUT
$fh
Truth table50%1/2
A
A= $fh
Aresult
11
00
717  
718 2 my @charstates = @{ $self->get_charstatelabels() };
719 2 ▶ if ( keys %{ $charstates[0]->{'states'} } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %{$charstates[0]{'states'};}) { } 0 2 2
elsif (@{$self->get_charlabels;} > 0) { } 0 2 2
720 0 print $fh "\tCHARSTATELABELS\n";
721 0 for my $label (@charstates) {
722 my ( $id, $charlabel ) =
723 0 ▶ ( $label->{'id'}, $label->{'charlabel'} || '' );
Condition0%0/2
$label->{'charlabel'} || ''
l!l
00
MC/DC0%0/1
$label->{'charlabel'} || ''
$label->{'charlabel'}
Truth table0%0/2
A
A= $label->{'charlabel'}
Aresult
11
00
724 0 $charlabel = _nexus_formatted($charlabel);
725 0 print $fh "\t$id $charlabel / ";
726 0 for my $key ( sort keys %{ $label->{'states'} } ) {
727 0 my $state = $label->{'states'}{$key};
728 0 $state = _nexus_formatted($state);
729 0 print $fh "$state ";
730 }
731 0 print $fh ",\n";
732 }
733 0 print $fh "\t;\n";
734 }
735 2 elsif ( @{ $self->get_charlabels } > 0 ) {
736 0 print $fh "\tCHARLABELS\n\t";
737 0 for my $charlabel ( @{ $self->get_charlabels } ) {
738 0 $charlabel = _nexus_formatted($charlabel);
739 0 print $fh " $charlabel";
740 }
741 0 print $fh ";\n";
742 }
743 }
744  
745 =begin comment
746  
747 Name : _write_matrix
748 Usage : $self->_write_matrix($file_handle,$verbose);
749 Function: Writes CharactersBlock matrix( The data stored in the matrix command) into the filehandle
750 Returns : none
751 Args : $file_handle and $verbose
752  
753 =end comment
754  
755 =cut
756  
757 2 ▶sub _write_matrix {
Subroutine _write_matrix: called CC=3 SCAR=11.0
758 2 my ( $self, $fh, $verbose ) = @_;
759 2 ▶ $fh ||= \*STDOUT;
Condition50%1/2
$fh ||= \*STDOUT
l!l
20
MC/DC0%0/1
$fh ||= \*STDOUT
$fh
Truth table50%1/2
A
A= $fh
Aresult
11
00
760  
761 2 my @otus = @{ $self->get_otuset()->get_otus() };
762 2 print $fh "\tMATRIX\n";
763 2 for my $otu (@otus) {
764 12 my $otu_name = _nexus_formatted( $otu->get_name() );
765 12 my $seq = $otu->get_seq_string( $self->{'format'}->{'tokens'} );
766 12 print $fh "\t", $otu_name, "\t", $seq, "\n";
767 }
768 2 print $fh "\t;\n";
769  
770 }
771  
772 79 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=12.0
773 79 ▶ return if $AUTOLOAD =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Bio::NEXUS::CharactersBlock::AUTOLOAD =~ /DESTROY$/ 77 2 79
774 2 my $package_name = __PACKAGE__ . '::';
775  
776 # The following methods are deprecated and are temporarily supported
777 # via a warning and a redirection
778 2 my %synonym_for = (
779 "${package_name}set_charstates" => "${package_name}set_charstatelabels",
780 "${package_name}get_charstates" => "${package_name}get_charstatelabels",
781 );
782  
783 2 ▶ if ( defined $synonym_for{$AUTOLOAD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $synonym_for{$Bio::NEXUS::CharactersBlock::AUTOLOAD}) { } 0 2 2
784 0 $logger->warn("$AUTOLOAD() is deprecated; use $synonym_for{$AUTOLOAD}() instead");
785 0 goto &{ $synonym_for{$AUTOLOAD} };
786 }
787 else {
788 2 Bio::NEXUS::Util::Exceptions::UnknownMethod->throw(
789 'error' => "ERROR: Unknown method $AUTOLOAD called"
790 );
791 }
792 }
793  
794 35 1;
« lib/Bio/NEXUS/Tree.pm ↑ Summary lib/Bio/NEXUS/UnalignedBlock.pm »