lib/Bio/NEXUS/Matrix.pm

filter: Statementstmt 72.1% 83 / 115 Branchbran 50.0% 29 / 58 Conditioncond 70.5% 12 / 17 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 83.3% 15 / 18 Podpod 100% 6 / 6 totaltotal 66.9% 150 / 224 CC 39 SCAR 21.0
CC
39
Cov
65%
_validate_format
12.2
add_taxlabels
12.0
AUTOLOAD
12.0
CRAP 8.1
« lib/Bio/NEXUS/WeightSet.pm ↑ Summary lib/Bio/NEXUS/HistoryBlock.pm »
1 #################################################################
2 # Matrix.pm
3 #################################################################
4 # Author: Thomas Hladish
5 # $Id: Matrix.pm,v 1.23 2007/09/21 23:09:09 rvos Exp $
6  
7 #################### START POD DOCUMENTATION ##################
8  
9 =head1 NAME
10  
11 Bio::NEXUS::Matrix - Provides functions for handling blocks that have matrices
12  
13 =head1 SYNOPSIS
14  
15 This module is the super class of Characters, Unaligned, and Distances block classes, and indirectly it is a super-class of Data and History blocks, which are both sub-classes of Characters blocks. These sub-classes inherint the methods within this module. There is no constructor, as a Matrix should not exist that is not also one of the sub-class block types.
16  
17 =head1 DESCRIPTION
18  
19 Provides functions used for handling blocks that have matrices.
20  
21 =head1 COMMENTS
22  
23 =head1 FEEDBACK
24  
25 All feedback (bugs, feature enhancements, etc.) are greatly appreciated.
26  
27 =head1 AUTHORS
28  
29 Thomas Hladish (tjhladish at yahoo)
30  
31 =head1 VERSION
32  
33 $Revision: 1.23 $
34  
35 =head1 METHODS
36  
37 =cut
38  
39 package Bio::NEXUS::Matrix;
40  
41 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 #use Data::Dumper; # XXX this is not used, might as well not import it!
43 #use Carp; # XXX this is not used, might as well not import it!
44 35 ▶use Bio::NEXUS::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 35 ▶use Bio::NEXUS::Block;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 35 ▶use Bio::NEXUS::Util::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 35 ▶use Bio::NEXUS::Util::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 35 ▶use vars qw(@ISA $VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 35 ▶use Bio::NEXUS; $VERSION = $Bio::NEXUS::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 35 @ISA = qw(Bio::NEXUS::Block);
52 35 my $logger = Bio::NEXUS::Util::Logger->new();
53  
54 =head2 get_ntax
55  
56 Title : get_ntax
57 Usage : $block->get_ntax();
58 Function: Returns the number of taxa in the block
59 Returns : # taxa
60 Args : none
61  
62 =cut
63  
64 5 ▶sub get_ntax {
Subroutine get_ntax: called CC=3 SCAR=15.1
Pod documented
65 5 my $self = shift;
66  
67 5 ▶ if ( my $otuset = $self->get_otuset() ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $otuset = $self->get_otuset) { } 5 0 5
elsif (my $dimensions = $self->get_dimensions) { } 0 0 0
68 5 return $otuset->get_ntax();
69 }
70 elsif ( my $dimensions = $self->get_dimensions() ) {
71 0 return $dimensions->{'ntax'};
72 }
73 else {
74 0 return;
75 }
76 }
77  
78 =head2 set_nchar
79  
80 Title : set_nchar
81 Usage : print $block->set_nchar();
82 Function: Sets the value of Dimensions:nchar
83 Returns : none
84 Args : number of char(scalar)
85  
86 =cut
87  
88 1 ▶sub set_nchar {
Subroutine set_nchar: called CC=1 SCAR=0.0
Pod documented
89 1 my ( $self, $nchar ) = @_;
90 1 $self->{'dimensions'}{'nchar'} = $nchar;
91 1 return;
92 }
93  
94 =head2 get_nchar
95  
96 Title : get_nchar
97 Usage : $block->get_nchar();
98 Function: Returns the number of characters in the block (Note: In Distances Blocks, this is the number of characters used to infer distances.)
99 Returns : # taxa
100 Args : none
101  
102 =cut
103  
104 0 ▶sub get_nchar {
Subroutine get_nchar: not called CC=2 SCAR=17.9
Pod documented
105 0 my $self = shift;
106  
107 0 ▶ if ( my $dimensions = $self->get_dimensions() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dimensions = $self->get_dimensions) { } 0 0 0
108 0 return $dimensions->{'nchar'};
109 }
110 else {
111 0 return;
112 }
113 }
114  
115 =begin comment
116  
117 Title : _parse_format
118 Usage : $format = $self->_parse_format($buffer); (private)
119 Function: Extracts format values from line and stores in format attribute
120 Returns : none
121 Args : buffer (string)
122 Methods : Separates formats by whitespace and creates hash containing
123 key = format name and value = format value.
124  
125 =end comment
126  
127 =cut
128  
129 70 ▶sub _parse_format {
Subroutine _parse_format: called CC=7 SCAR=21.6
130 70 my ( $self, $string ) = @_;
131  
132 70 my %format = ();
133  
134 70 my @format_tokens = @{ _parse_nexus_words($string) };
135 70 ▶ while (@format_tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@format_tokens) 193 70 263
136  
137 # If the second thing in the list is a '=' (e.g. ('datatype', '=', 'standard') )
138 193 ▶ if ( $format_tokens[1] && $format_tokens[1] eq '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format_tokens[1] and $format_tokens[1] eq '=') { } 180 13 193
Condition100%3/3
$format_tokens[1] and $format_tokens[1] eq "="
!ll&&!rl&&r
85180
MC/DC100%2/2
$format_tokens[1] and $format_tokens[1] eq "="
$format_tokens[1] $format_tokens[1] eq "="
Truth table100%3/3
A and B
A= $format_tokens[1]B= $format_tokens[1] eq "="
ABresult
0X0
100
111
139 180 ▶ if ( lc($format_tokens[0]) eq 'items' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $format_tokens[0] eq 'items') { } 0 180 180
140 # process items list
141 0 my ( $key, $equals, $val ) = splice( @format_tokens, 0, 3 );
142 0 $format{ 'items' } = $val;
143 0 ▶ if ( $val eq '(' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "(") 0 0 0
144 0 ▶ while ( $format{ 'items' } !~ /\)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format{"items"} =~ /\)$/) 0 0 0
145 #print Dumper @format_tokens;
146 0 $format{ 'items' } .= " " . shift( @format_tokens );
147 }
148 }
149 }
150 else {
151 #then set the first thing equal to the third
152 180 my ( $key, $equals, $val ) = splice( @format_tokens, 0, 3 );
153 180 $format{ lc $key } = $val;
154 }
155 }
156 else {
157 13 my $key = shift @format_tokens;
158  
159 # Otherwise, just set the first thing equal to TRUE
160 13 $format{ lc $key } = 1;
161 }
162 }
163  
164 # Note: Treating flags and things with rvalues the same way is problematic--
165 # how do you know whether a given format token has a count of 1, or if it
166 # was merely present, and that's why it has a value of one. One possible
167 # way to make this more robust is to store flags in $format{'flags'},
168 # e.g. $format{'flags'} = ['tokens', 'respectcase'];
169  
170 70 $self->set_format( \%format );
171 70 return;
172 }
173  
174 =begin comment
175  
176 Title : _validate_format
177 Usage : $self->_validate_format($format_hashref); (private)
178 Function: Assigns defaults and sorts through formatting subcommands per the NEXUS standard
179 Returns : hash reference (the validated formatting)
180 Args : hash reference with format keys (the subcommands) and their values
181  
182 =end comment
183  
184 =cut
185  
186 71 ▶sub _validate_format {
Subroutine _validate_format: called CC=10 SCAR=25.0
187 71 my ( $self, $format ) = @_;
188 71 my $block_type = $self->get_type();
189  
190 # Currently, only Characters and Unaligned blocks are handled here--other
191 # matrix-type blocks are treated as though their formatting is valid
192 71 ▶ if ( $block_type !~ qr/^(?:characters|unaligned)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block_type =~ qr/^(?:characters|unaligned)$/i) 2 69 71
193 2 return $format;
194 }
195  
196 69 ▶ $format->{'datatype'} ||= 'standard'; # 'standard' is the default datatype
Condition100%2/2
$format->{"datatype"} ||= "standard"
l!l
5712
MC/DC100%1/1
$format->{"datatype"} ||= "standard"
$format->{"datatype"}
Truth table100%2/2
A
A= $format->{"datatype"}
Aresult
11
00
197  
198 # tokens always true for continuous data (p. 601 of Maddison, et al, 1997)
199 69 ▶ if ( $format->{'datatype'} =~ /^continuous$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format->{"datatype"} =~ /^continuous$/i) 0 69 69
200 0 ▶ if ( $format->{'notokens'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format->{"notokens"}) 0 0 0
201 0 $logger->warn(
202 "notokens subcommand is incompatible with"
203 . "datatype=continuous subcommand in format statement"
204 );
205 }
206 0 $format->{'tokens'} = 1;
207 }
208
209 69 ▶ if ( $format->{'datatype'} =~ /^(?:dna|rna|nucleotide|protein|continuous)$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($format->{'datatype'} =~ /^(?:dna|rna|nucleotide|protein|continuous)$/i) { } 52 17 69
elsif ($format->{'datatype'} eq 'standard') { } 17 0 17
210 52 delete $format->{'respectcase'};
211 }
212 elsif ( $format->{'datatype'} eq 'standard' ) {
213 17 ▶ if ( !$format->{'respectcase'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format->{"respectcase"}) 17 0 17
214 17 for my $sub_cmd (qw/symbols missing gap matchar/) {
215 $format->{$sub_cmd} = lc $format->{$sub_cmd}
216 68 ▶ if defined $format->{$sub_cmd};
Branch: 1 branch
BranchTrueFalseTotal
if defined $format->{$sub_cmd} 13 55 68
217 }
218 }
219 }
220 else {
221 0 $logger->warn(
222 "Unfamiliar datatype encountered in $block_type block: "
223 . "'$format->{'datatype'}' (continuing anyway)"
224 );
225 }
226  
227 69 return $format;
228 }
229  
230 =head2 set_format
231  
232 Title : set_format
233 Usage : $block->set_format(\%format);
234 Function: set the format of the characters
235 Returns : none
236 Args : hash of format values
237  
238 =cut
239  
240 71 ▶sub set_format {
Subroutine set_format: called CC=1 SCAR=0.0
Pod documented
241 71 my ( $self, $format_hashref ) = @_;
242 71 $self->{'format'} = $self->_validate_format($format_hashref);
243 }
244  
245 =head2 get_format
246  
247 Title : get_format
248 Usage : $block->get_format($attribute);
249 Function: Returns the format of the characters
250 Returns : hash of format values, or if $attribute (a string) is supplied, the value of that attribute in the hash
251 Args : none
252  
253 =cut
254  
255 81 ▶sub get_format {
Subroutine get_format: called CC=3 SCAR=11.0
Pod documented
256 81 my ( $self, $attribute ) = @_;
257 $attribute
258 ? return $self->{'format'}->{$attribute}
259 81 ▶ : return $self->{'format'} || {};
Branch: 1 branch
BranchTrueFalseTotal
$attribute ? : 6 75 81
Condition100%2/2
$self->{'format'} || {}
l!l
723
MC/DC100%1/1
$self->{'format'} || {}
$self->{'format'}
Truth table100%2/2
A
A= $self->{'format'}
Aresult
11
00
260 }
261  
262 =head2 add_taxlabels
263  
264 Title : add_taxlabels
265 Usage : $block->add_taxlabels($new_taxlabels);
266 Function: Adds new taxa to taxlabels if they aren't already there
267 Returns : none
268 Args : taxa to be added
269  
270 =cut
271  
272 0 ▶sub add_taxlabels {
Subroutine add_taxlabels: not called CC=3 SCAR=24.8
Pod documented
273 0 my ( $self, $new_taxlabels ) = @_;
274 0 my $current_taxlabels = $self->get_taxlabels();
275  
276 0 for my $new_label (@$new_taxlabels) {
277  
278 # Check to see if new_label is already in current_taxlabels
279 0 ▶ if ( !defined first {/$new_label/} @$current_taxlabels ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined do { /$new_label/ }->first(@$current_taxlabels)) 0 0 0
280 0 push @$current_taxlabels, $new_label;
281 }
282 }
283 0 return;
284 }
285  
286 =begin comment
287  
288 Title : _write_dimensions
289 Usage : $block->_write_dimensions();
290 Function: writes out the dimensions command
291 Returns : none
292 Args : filehandle to write to, a verbose flag
293  
294 =end comment
295  
296 =cut
297  
298 2 ▶sub _write_dimensions {
Subroutine _write_dimensions: called CC=6 SCAR=20.2
299 2 my ( $self, $fh, $verbose ) = @_;
300 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
301  
302 # Arlin took out all ntax stuff, ntax only used in taxa block according to standard
303 #
304 # my $ntax = $self->get_ntax();
305 2 my $nchar = $self->get_nchar();
306  
307 2 ▶ return if !defined $nchar;# && !defined $ntax;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nchar 0 2 2
308  
309 # my $ntax_text = $ntax ? " ntax=$ntax" : q{};
310 2 ▶ my $nchar_text = $nchar ? " nchar=$nchar" : q{};
Branch: 1 branch
BranchTrueFalseTotal
$nchar ? : 2 0 2
311  
312 # Tom: this code cannot be reached due to return above on !$nchar, right? -Arlin
313 #
314 2 ▶ if ( $self->get_type() eq 'characters' && !$nchar ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_type eq "characters" and not $nchar) 0 2 2
Condition33%1/3
$self->get_type eq "characters" and not $nchar
!ll&&!rl&&r
020
MC/DC0%0/2
$self->get_type eq "characters" and not $nchar
$self->get_type eq "characters" not $nchar
Truth table33%1/3
A and not(B)
A= $self->get_type eq "characters"B= not $nchar
ABresult
0X0
100
111
315 0 Bio::NEXUS::Util::Exceptions::BadFormat->throw(
316 'error' => "Characters blocks require that Dimensions:nchar be defined"
317 );
318 }
319  
320 # print $fh "\tDIMENSIONS$ntax_text$nchar_text;\n";
321 2 print $fh "\tDIMENSIONS$nchar_text;\n";
322 2 return;
323 }
324  
325 =begin comment
326  
327 Title : _write_format
328 Usage : $block->_write_format();
329 Function: writes out the format command
330 Returns : none
331 Args : filehandle to write to, a verbose flag
332  
333 =end comment
334  
335 =cut
336  
337 2 ▶sub _write_format {
Subroutine _write_format: called CC=10 SCAR=24.2
338 2 my ( $self, $fh, $verbose ) = @_;
339 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
340  
341 2 my %format_of = %{ $self->get_format() };
342 2 ▶ if ( scalar keys %format_of ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %format_of) 2 0 2
343 2 print $fh "\tFORMAT";
344  
345 print $fh " datatype=$format_of{'datatype'}"
346 2 ▶ if defined $format_of{'datatype'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $format_of{"datatype"} 2 0 2
347 2 ▶ print $fh ' respectcase' if $format_of{'respectcase'};
Branch: 1 branch
BranchTrueFalseTotal
if $format_of{"respectcase"} 0 2 2
348  
349 2 ▶ while ( my ( $key, $val ) = each %format_of ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %format_of) 6 2 8
350 6 ▶ next if ( lc($key) eq 'interleave' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $key eq "interleave" 0 6 6
351 6 ▶ if ( !$val || ( $key =~ /(?:datatype|respectcase)/i ) ) { next; }
Branch: 2 branches
BranchTrueFalseTotal
if (not $val or $key =~ /(?:datatype|respectcase)/i) { } 2 4 6
elsif ($val eq '1') { } 0 4 4
Condition66%2/3
not $val or $key =~ /(?:datatype|respectcase)/i
l!l&&r!l&&!r
024
MC/DC50%1/2
not $val or $key =~ /(?:datatype|respectcase)/i
not $val $key =~ /(?:datatype|respectcase)/i
Truth table66%2/3
not(A) or B
A= not $valB= $key =~ /(?:datatype|respectcase)/i
ABresult
1X1
011
000
352 elsif ( $val eq '1' ) {
353 0 print $fh " $key";
354 }
355 else {
356 4 print $fh " $key=$val";
357 }
358 }
359 2 print $fh ";\n";
360 }
361 2 return;
362 }
363  
364 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=24.8
365 0 ▶ return if $AUTOLOAD =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Bio::NEXUS::Matrix::AUTOLOAD =~ /DESTROY$/ 0 0 0
366 0 my $package_name = __PACKAGE__ . '::';
367  
368 # The following methods are deprecated and are temporarily supported
369 # via a warning and a redirection
370 0 my %synonym_for = (
371  
372 # "${package_name}parse" => "${package_name}_parse_tree", # example
373 );
374  
375 0 ▶ if ( defined $synonym_for{$AUTOLOAD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $synonym_for{$Bio::NEXUS::Matrix::AUTOLOAD}) { } 0 0 0
376 0 $logger->warn("$AUTOLOAD() is deprecated; use $synonym_for{$AUTOLOAD}() instead");
377 0 goto &{ $synonym_for{$AUTOLOAD} };
378 }
379 else {
380 0 Bio::NEXUS::Util::Exceptions::UnknownMethod->throw(
381 'error' => "ERROR: Unknown method $AUTOLOAD called"
382 );
383 }
384 }
385  
386 35 1;
« lib/Bio/NEXUS/WeightSet.pm ↑ Summary lib/Bio/NEXUS/HistoryBlock.pm »