lib/Bio/NEXUS/DistancesBlock.pm

filter: Statementstmt 25.5% 37 / 145 Branchbran 6.7% 5 / 74 Conditioncond 5.2% 1 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 60.0% 9 / 15 Podpod 100% 4 / 4 totaltotal 20.8% 56 / 269 CC 46 SCAR 58.4
CC
46
Cov
18%
_parse_matrix
650.0
_write_matrix
272.0
_write
6.0
CRAP 344.3
« lib/Bio/NEXUS/Tools/NexModifier.pm ↑ Summary script/nexplot.pl »
1 #################################################################
2 # DistancesBlock.pm
3 #################################################################
4 # Author: Thomas Hladish
5 # $Id: DistancesBlock.pm,v 1.18 2007/09/21 23:09:09 rvos Exp $
6  
7 #################### START POD DOCUMENTATION ##################
8  
9 =head1 NAME
10  
11 Bio::NEXUS::DistancesBlock - Represents DISTANCES block in NEXUS file
12  
13 =head1 SYNOPSIS
14  
15  
16 =head1 DESCRIPTION
17  
18 The DistancesBlock class represents a NEXUS Distances Block and provides methods for reading, writing, and accessing data within these blocks. Distances Blocks contain distance matrices, or a table of calculated distances between each possible pair of taxa.
19  
20 =head1 COMMENTS
21  
22 =head1 FEEDBACK
23  
24 All feedback (bugs, feature enhancements, etc.) are greatly appreciated.
25  
26 =head1 AUTHORS
27  
28 Tom Hladish (tjhladish at yahoo)
29  
30 =head1 VERSION
31  
32 $Revision: 1.18 $
33  
34 =head1 METHODS
35  
36 =cut
37  
38 package Bio::NEXUS::DistancesBlock;
39  
40 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 #use Data::Dumper; # XXX this is not used, might as well not import it!
42 #use Carp; # XXX this is not used, might as well not import it!
43 35 ▶use Bio::NEXUS::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 35 ▶use Bio::NEXUS::Matrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 35 ▶use Bio::NEXUS::Util::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 35 ▶use Bio::NEXUS::Util::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 35 ▶use vars qw(@ISA $VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 35 ▶use Bio::NEXUS; $VERSION = $Bio::NEXUS::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 35 @ISA = qw(Bio::NEXUS::Matrix);
51 35 my $logger = Bio::NEXUS::Util::Logger->new();
52  
53 =head2 new
54  
55 Title : new
56 Usage : block_object = new Bio::NEXUS::DistancesBlock($block_type, $commands, $verbose, $taxa);
57 Function: Creates a new Bio::NEXUS::DistancesBlock object
58 Returns : Bio::NEXUS::DistancesBlock object
59 Args : type (string), the commands/comments to parse (array ref), and a verbose flag (0 or 1)
60  
61 =cut
62  
63 1 ▶sub new {
Subroutine new: called CC=4 SCAR=15.8
Pod documented
64 1 my ( $class, $type, $commands, $verbose, $taxa ) = @_;
65 1 ▶ if ( not $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 1 1
66 0 ( $type = lc $class ) =~ s/Bio::NEXUS::(.+)Block/$1/i;
67 }
68 1 my $self = {
69 'type' => $type
70 };
71 1 bless $self, $class;
72 1 $self->set_taxlabels($taxa);
73 1 ▶ if ( ( defined $commands ) and @$commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $commands and @$commands) 1 0 1
Condition33%1/3
defined $commands and @$commands
!ll&&!rl&&r
001
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
74 1 $self->_parse_block( $commands, $verbose )
75 }
76 0 return $self;
77 }
78  
79 =begin comment
80  
81 Title : _parse_matrix
82 Usage : $self->_parse_matrix($block);
83 Function: Parses a distance matrix, stores the data
84 Returns : none
85 Args : the distance matrix to parse (string)
86  
87 =end comment
88  
89 =cut
90  
91 0 ▶sub _parse_matrix {
Subroutine _parse_matrix: not called CC=25 SCAR=64.8
92 0 my ( $self, $buffer ) = @_;
93  
94 # Set format values as already parsed, or to NEXUS-specified defaults
95 0 my %format = %{ $self->get_format() };
96  
97 0 ▶ my $triangle = defined $format{'triangle'} ? $format{'triangle'} : 'lower';
Branch: 1 branch
BranchTrueFalseTotal
defined $format{'triangle'} ? : 0 0 0
98 0 ▶ my $diagonal = defined $format{'diagonal'} ? $format{'diagonal'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $format{'diagonal'} ? : 0 0 0
99 0 ▶ my $labels = defined $format{'labels'} ? $format{'labels'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $format{'labels'} ? : 0 0 0
100 0 ▶ my $missing = defined $format{'missing'} ? $format{'missing'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $format{'missing'} ? : 0 0 0
101 0 ▶ my $interleave = defined $format{'interleave'} ? $format{'interleave'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $format{'interleave'} ? : 0 0 0
102  
103 0 ▶ if ( $triangle =~ /^both$/i && !$diagonal ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($triangle =~ /^both$/i and not $diagonal) 0 0 0
Condition0%0/3
$triangle =~ /^both$/i and not $diagonal
!ll&&!rl&&r
000
MC/DC0%0/2
$triangle =~ /^both$/i and not $diagonal
$triangle =~ /^both$/i not $diagonal
Truth table0%0/3
A and not(B)
A= $triangle =~ /^both$/iB= not $diagonal
ABresult
0X0
100
111
104 0 Bio::NEXUS::Util::Exceptions::BadFormat->throw(
105 'error' => "The Distances Block contains a matrix that has\n"
106 . "both upper and lower halves, but does not have\n"
107 . "diagonal values.\nThis is prohibited by the NEXUS standard"
108 );
109 }
110 0 ▶ if ( $interleave && !$labels ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interleave and not $labels) 0 0 0
Condition0%0/3
$interleave and not $labels
!ll&&!rl&&r
000
MC/DC0%0/2
$interleave and not $labels
$interleave not $labels
Truth table0%0/3
A and not(B)
A= $interleaveB= not $labels
ABresult
0X0
100
111
111 0 Bio::NEXUS::Util::Exceptions::BadFormat->throw(
112 'error' => "This matrix is interleaved and without row labels\n"
113 . "('unlabeled'). Please label rows or use a non-\n"
114 . "interleaved format, to allow for safer parsing"
115 );
116 }
117  
118 0 my @rows = split /\n+/, $buffer;
119 0 my @taxa_order;
120 my %row_for;
121  
122 # First, we'll deal with whether the matrix is interleaved and labeled
123 0 ▶ if ( $interleave || $labels ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interleave or $labels) { } 0 0 0
Condition0%0/3
$interleave or $labels
l!l&&r!l&&!r
000
MC/DC0%0/2
$interleave or $labels
$interleave $labels
Truth table0%0/3
A or B
A= $interleaveB= $labels
ABresult
1X1
011
000
124 0 for my $row (@rows) {
125 0 my ( $taxon, @distances ) = @{ _parse_nexus_words($row) };
126 0 push( @taxa_order, $taxon );
127 0 push @{ $row_for{$taxon} }, @distances;
128 }
129 }
130 else {
131 0 @taxa_order = @{ $self->get_taxlabels() };
132 @rows =
133 0 grep { !/^\s+$/ } @rows; # throw out rows that are just blank space
134  
135 0 ▶ for ( my $i = 0; $i < @rows; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @rows) 0 0 0
136 0 my $row = $rows[$i];
137 0 $row_for{ $taxa_order[$i] } = [ split /\s+/, $row ];
138 }
139 }
140  
141 # It's important to keep track of this so that we know what the columns
142 # are, since they're unlabeled
143 0 $self->set_taxlabels( \@taxa_order );
144  
145 # Now everything is stored in %row_for, and the original order
146 # is in @taxa_order
147 0 my $matrix;
148 0 ▶ for ( my $r = 0; $r < @taxa_order; $r++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < @taxa_order) 0 0 0
149 0 my $row_label = $taxa_order[$r];
150 0 my @cells = @{ $row_for{$row_label} };
151  
152 # If this is a full matrix (simplest to parse),
153 0 ▶ if ( $triangle =~ /^both$/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($triangle =~ /^both$/i) { } 0 0 0
elsif ($triangle =~ /^lower$/i) { } 0 0 0
elsif ($triangle =~ /^upper$/i) { } 0 0 0
154  
155 # iterate through the values
156 0 ▶ for ( my $c = 0; $c < @cells; $c++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < @cells) 0 0 0
157 0 my $cell_val = $cells[$c];
158 0 my $col_label = $taxa_order[$c];
159  
160 # and store them in $matrix.
161 0 $matrix->{$row_label}{$col_label} = $cell_val;
162 }
163 }
164  
165 # If it's a lower triangle,
166 elsif ( $triangle =~ /^lower$/i ) {
167  
168 # iterate through the values
169 0 ▶ for ( my $c = 0; $c < @cells; $c++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < @cells) 0 0 0
170 0 my $cell_val = $cells[$c];
171  
172 # and store them symmetrically in $matrix
173 0 my $col_label = $taxa_order[$c];
174 $matrix->{$row_label}{$col_label} =
175 0 ( $matrix->{$col_label}{$row_label} = $cell_val );
176 }
177  
178 # In case there are no diagonal values,
179 0 ▶ if ( !$diagonal ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($diagonal) 0 0 0
180  
181 # make sure they still get stored (as zeroes)
182 0 $matrix->{$row_label}{$row_label} = 0;
183 }
184 }
185  
186 # If this is an upper triangle
187 elsif ( $triangle =~ /^upper$/i ) {
188  
189 # iterate through the values
190 0 ▶ for ( my $c = 0; $c < @cells; $c++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < @cells) 0 0 0
191 0 my $cell_val = $cells[$c];
192  
193 # and make sure the column label is correct,
194 # since everything needs to be shifted over.
195 0 ▶ my $col_label = $diagonal
Branch: 1 branch
BranchTrueFalseTotal
$diagonal ? : 0 0 0
196 ? $taxa_order[ $r + $c ]
197 : $taxa_order[ $r + $c + 1 ];
198  
199 # Store the values symmetrically in $matrix
200 $matrix->{$row_label}{$col_label} =
201 0 ( $matrix->{$col_label}{$row_label} = $cell_val );
202 }
203  
204 # In case there are no diagonal values,
205 0 ▶ if ( !$diagonal ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($diagonal) 0 0 0
206  
207 # make sure they still get stored (as zeroes)
208 0 $matrix->{$row_label}{$row_label} = 0;
209 }
210 }
211 else {
212 0 Bio::NEXUS::Util::Exceptions::BadFormat->throw(
213 'error' => "Unknown value '$triangle' for Format:Triangle\n"
214 . "in the DistancesBlock. Expecting 'upper', 'lower', or 'both'."
215 );
216 }
217 }
218  
219 0 ▶ $self->set_ntax( scalar keys %$matrix ) unless $self->get_ntax();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_ntax 0 0 0
220 0 $self->{'matrix'} = $matrix;
221 0 return $self->{'matrix'};
222 }
223  
224 =head2 get_matrix
225  
226 Title : get_matrix
227 Usage : $matrix = $self->get_matrix();
228 Function: Retrieves the entire distance matrix
229 Returns : a hashref of hashrefs
230 Args : none
231 Note : Distance values may be retrieved by specifying the row and column keys, e.g. $dist = $matrix->{$row_taxon}{$col_taxon}
232  
233 =cut
234  
235 0 ▶sub get_matrix {
Subroutine get_matrix: not called CC=1 SCAR=6.9
Pod documented
236 0 my ( $self, $taxon ) = @_;
237 0 return $self->{'matrix'};
238 }
239  
240 =head2 get_distances_for
241  
242 Title : get_distances_for
243 Usage : %taxon1_distances = %{ $self->get_distances_for($first_taxon) };
244 Function: Retrieves a row of the distance matrix
245 Returns :
246 Args : the row label (a taxlabel) for the row desired (string)
247  
248 =cut
249  
250 0 ▶sub get_distances_for {
Subroutine get_distances_for: not called CC=1 SCAR=6.9
Pod documented
251 0 my ( $self, $taxon ) = @_;
252 0 my $matrix = $self->get_matrix();
253 0 my $row = $matrix->{$taxon};
254 0 return $row;
255 }
256  
257 =head2 get_distance_between
258  
259 Title : get_distance_between
260 Usage : $distance = $self->get_distance_between($row_taxon, $column_taxon);
261 Function: Retrieves a cell from the matrix
262 Returns : A scalar (number)
263 Args : the row and column labels (both taxa) for the cell desired
264 Note : Generally get_distance_between($A, $B) == get_distance_between($B, $A); however, this need not be true if the distance matrix is not symmetric. Make sure you are asking for the distance you want.
265  
266 =cut
267  
268 0 ▶sub get_distance_between {
Subroutine get_distance_between: not called CC=1 SCAR=6.9
Pod documented
269 0 my ( $self, $tax1, $tax2 ) = @_;
270 0 my $matrix = $self->get_matrix();
271 0 my $dist = $matrix->{$tax1}{$tax2};
272 0 return $dist;
273 }
274  
275 =begin comment
276  
277 Name : _write
278 Usage : $block->_write();
279 Function: Writes out NEXUS Distances Block
280 Returns : none
281 Args : file handle
282  
283 =end comment
284  
285 =cut
286  
287 0 ▶sub _write {
Subroutine _write: not called CC=2 SCAR=17.9
288 0 my ( $self, $fh, $verbose ) = @_;
289 0 ▶ $fh ||= \*STDOUT;
Condition0%0/2
$fh ||= \*STDOUT
l!l
00
MC/DC0%0/1
$fh ||= \*STDOUT
$fh
Truth table0%0/2
A
A= $fh
Aresult
11
00
290  
291 0 Bio::NEXUS::Block::_write( $self, $fh );
292 0 $self->_write_dimensions( $fh, $verbose );
293 0 $self->_write_format( $fh, $verbose );
294 0 $self->_write_matrix( $fh, $verbose );
295 0 print $fh "END;\n";
296 }
297  
298 =begin comment
299  
300 Name : _write_matrix
301 Usage : $block->_write_matrix();
302 Function: writes out the matrix for a NEXUS Distances Block
303 Returns : none
304 Args : file handle
305  
306 =end comment
307  
308 =cut
309  
310 0 ▶sub _write_matrix {
Subroutine _write_matrix: not called CC=16 SCAR=56.1
311 0 my ( $self, $fh, $verbose ) = @_;
312 0 ▶ $fh ||= \*STDOUT;
Condition0%0/2
$fh ||= \*STDOUT
l!l
00
MC/DC0%0/1
$fh ||= \*STDOUT
$fh
Truth table0%0/2
A
A= $fh
Aresult
11
00
313  
314 0 my %format = %{ $self->get_format() };
315  
316 0 ▶ my $triangle = defined $format{'triangle'} ? $format{'triangle'} : 'lower';
Branch: 1 branch
BranchTrueFalseTotal
defined $format{'triangle'} ? : 0 0 0
317 0 ▶ my $diagonal = defined $format{'diagonal'} ? $format{'diagonal'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $format{'diagonal'} ? : 0 0 0
318 0 ▶ my $labels = defined $format{'labels'} ? $format{'labels'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $format{'labels'} ? : 0 0 0
319 0 ▶ my $missing = defined $format{'missing'} ? $format{'missing'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $format{'missing'} ? : 0 0 0
320 0 ▶ my $interleave = defined $format{'interleave'} ? $format{'interleave'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $format{'interleave'} ? : 0 0 0
321  
322 0 ▶ if ( $triangle =~ /^both$/i && !$diagonal ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($triangle =~ /^both$/i and not $diagonal) 0 0 0
Condition0%0/3
$triangle =~ /^both$/i and not $diagonal
!ll&&!rl&&r
000
MC/DC0%0/2
$triangle =~ /^both$/i and not $diagonal
$triangle =~ /^both$/i not $diagonal
Truth table0%0/3
A and not(B)
A= $triangle =~ /^both$/iB= not $diagonal
ABresult
0X0
100
111
323 0 Bio::NEXUS::Util::Exceptions::BadFormat->throw(
324 'error' => "The Distances Block contains a matrix that has\n"
325 . "both upper and lower halves, but does not have\n"
326 . "diagonal values. This is prohibited by the NEXUS standard"
327 );
328 }
329  
330 0 print $fh "\tMATRIX\n";
331 0 my $matrix = $self->get_matrix();
332  
333 0 my @taxlabels = @{ $self->get_taxlabels };
334  
335 0 ▶ for ( my $r = 0; $r < @taxlabels; $r++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < @taxlabels) 0 0 0
336 0 my $row_taxon = $taxlabels[$r];
337 0 my $print_taxon = _nexus_formatted($row_taxon);
338 0 print $fh "\t$print_taxon";
339  
340 0 my ( $first_col, $last_col );
341  
342 # Determine which part of the matrix to iterate through,
343 # based on whether its 'upper', 'lower', or 'both'
344 0 ▶ if ( $triangle =~ /^both$/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($triangle =~ /^both$/i) { } 0 0 0
elsif ($triangle =~ /^lower$/i) { } 0 0 0
elsif ($triangle =~ /^upper$/i) { } 0 0 0
345 0 ( $first_col, $last_col ) = ( 0, scalar @taxlabels );
346 }
347 elsif ( $triangle =~ /^lower$/i ) {
348 0 ( $first_col, $last_col ) = ( 0, $r );
349 0 ▶ $last_col++ if $diagonal;
Branch: 1 branch
BranchTrueFalseTotal
if $diagonal 0 0 0
350 }
351 elsif ( $triangle =~ /^upper$/i ) {
352 0 ( $first_col, $last_col ) = ( $r, scalar @taxlabels );
353 0 ▶ $first_col++ unless $diagonal;
Branch: 1 branch
BranchTrueFalseTotal
unless $diagonal 0 0 0
354 }
355  
356 0 ▶ for ( my $c = $first_col; $c < $last_col; $c++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $last_col) 0 0 0
357 0 my $col_taxon = $taxlabels[$c];
358 0 print $fh "\t" . $matrix->{$row_taxon}{$col_taxon};
359 }
360 0 print $fh "\n";
361 }
362 0 print $fh "\t;\n";
363 }
364  
365 2 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=12.0
366 2 ▶ return if $AUTOLOAD =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Bio::NEXUS::DistancesBlock::AUTOLOAD =~ /DESTROY$/ 1 1 2
367 1 my $package_name = __PACKAGE__ . '::';
368  
369 # The following methods are deprecated and are temporarily supported
370 # via a warning and a redirection
371 1 my %synonym_for = (
372  
373 # "${package_name}parse" => "${package_name}_parse_tree", # example
374 );
375  
376 1 ▶ if ( defined $synonym_for{$AUTOLOAD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $synonym_for{$Bio::NEXUS::DistancesBlock::AUTOLOAD}) { } 0 1 1
377 0 $logger->warn("$AUTOLOAD() is deprecated; use $synonym_for{$AUTOLOAD}() instead");
378 0 goto &{ $synonym_for{$AUTOLOAD} };
379 }
380 else {
381 1 Bio::NEXUS::Util::Exceptions::UnknownMethod->throw(
382 'error' => "ERROR: Unknown method $AUTOLOAD called"
383 );
384 }
385 }
386  
387 35 1;
« lib/Bio/NEXUS/Tools/NexModifier.pm ↑ Summary script/nexplot.pl »