lib/Bio/NEXUS/TaxUnitSet.pm

filter: Statementstmt 74.5% 149 / 200 Branchbran 52.5% 21 / 40 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 90.6% 29 / 32 Podpod 100% 24 / 24 totaltotal 75.4% 230 / 305 CC 36 SCAR 16.7
CC
36
Cov
71%
equals
15.0
get_seq_string_hash
12.0
subset
12.0
CRAP 5.3
« lib/Bio/NEXUS/NHXCmd.pm ↑ Summary lib/Bio/NEXUS/DataBlock.pm »
1 #################################################################
2 # TaxUnitSet.pm
3 #################################################################
4 # Author: Chengzhi Liang, Peter Yang, Thomas Hladish
5 # $Id: TaxUnitSet.pm,v 1.30 2007/09/24 04:52:14 rvos Exp $
6  
7 #################### START POD DOCUMENTATION ##################
8  
9 =head1 NAME
10  
11 Bio::NEXUS::TaxUnitSet - Represents a sets of OTUS (Bio::NEXUS::TaxUnits objects) in a NEXUS file
12  
13 =head1 SYNOPSIS
14  
15 $otuset = new Bio::NEXUS::TaxUnitSet(\@otus);
16  
17 =head1 DESCRIPTION
18  
19 This module represents a set of OTUs (Bio::NEXUS::TaxUnit objects) in a NEXUS file (in characters block or History block)
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 Chengzhi Liang (liangc@umbi.umd.edu)
30 Peter Yang (pyang@rice.edu)
31 Thomas Hladish (tjhladish at yahoo)
32  
33 =head1 VERSION
34  
35 $Revision: 1.30 $
36  
37 =head1 METHODS
38  
39 =cut
40  
41 package Bio::NEXUS::TaxUnitSet;
42  
43 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 35 ▶use Bio::NEXUS::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 35 ▶use Bio::NEXUS::TaxUnit;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 #use Data::Dumper; # XXX this is not used, might as well not import it!
47 #use Carp; # XXX this is not used, might as well not import it!
48 35 ▶use Bio::NEXUS::Util::Exceptions 'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 35 ▶use Bio::NEXUS::Util::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 35 ▶use vars qw($VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 35 ▶use Bio::NEXUS; $VERSION = $Bio::NEXUS::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 35 my $logger = Bio::NEXUS::Util::Logger->new;
54  
55 =head2 new
56  
57 Title : new
58 Usage : $otuset = new Bio::NEXUS::TaxUnitSet(\@otus);
59 Function: Creates a new Bio::NEXUS::TaxUnitSet object
60 Returns : Bio::NEXUS::TaxUnitSet object
61 Args : ref to an array of TaxUnit objects
62  
63 =cut
64  
65 78 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
66 78 my ( $class, $otus ) = @_;
67 78 my $self = { otus => $otus, };
68 78 bless( $self, $class );
69 78 return $self;
70 }
71  
72 =head2 clone
73  
74 Title : clone
75 Usage : my $newset = $set->clone();
76 Function: clone an TaxUnitSet object
77 Returns : TaxUnitSet object
78 Args : none
79  
80 =cut
81  
82 1 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
83 1 my ($self) = @_;
84 1 my $class = ref($self);
85 1 my $newset = bless( { %{$self} }, $class );
86 1 my @otus = @{ $newset->get_otus() };
87 1 my @newotus = ();
88 1 for my $otu (@otus) {
89 2 push @newotus, $otu->clone();
90 }
91 1 $newset->set_otus( \@newotus );
92 1 return $newset;
93 }
94  
95 =head2 add_otu
96  
97 Title : add_otu
98 Usage : $block->add_otu($otu);
99 Function: add a taxon
100 Returns : none
101 Args : a taxon
102  
103 =cut
104  
105 12 ▶sub add_otu {
Subroutine add_otu: called CC=1 SCAR=0.0
Pod documented
106 12 my ( $self, $otu ) = @_;
107 12 push @{ $self->{'otus'} }, $otu;
108 }
109  
110 =head2 set_otus
111  
112 Title : set_otus
113 Usage : $set->set_otus($otus);
114 Function: sets the list of OTUs
115 Returns : none
116 Args : array of OTUs
117  
118 =cut
119  
120 74 ▶sub set_otus {
Subroutine set_otus: called CC=1 SCAR=0.0
Pod documented
121 74 my ( $self, $otus ) = @_;
122 74 $self->{'otus'} = $otus;
123 }
124  
125 =head2 get_otus
126  
127 Title : get_otus
128 Usage : $set->get_otus();
129 Function: Returns array of otus
130 Returns : all otus
131 Args : none
132  
133 =cut
134  
135 144 ▶sub get_otus {
Subroutine get_otus: called CC=1 SCAR=0.0
Pod documented
136 144 my ($self) = @_;
137 144 return $self->{'otus'};
138 }
139  
140 =head2 get_otu
141  
142 Title : get_otu
143 Usage : $set->get_otu(name);
144 Function: Returns an OTU with a specified name
145 Returns : an OTU (Bio::NEXUS::TaxUnit)
146 Args : OTU name as scalar string
147  
148 =cut
149  
150 13 ▶sub get_otu {
Subroutine get_otu: called CC=3 SCAR=11.1
Pod documented
151 13 my ( $self, $name ) = @_;
152 13 for my $otu ( @{ $self->get_otus() } ) {
153 40 ▶ return $otu if ( lc($name) eq lc($otu->get_name()) );
Branch: 1 branch
BranchTrueFalseTotal
if lc $name eq lc $otu->get_name 13 27 40
154 }
155 0 return undef;
156 }
157  
158 =head2 get_otu_names
159  
160 Title : get_otu_names
161 Usage : $set->get_otu_names();
162 Function: Returns array of OTU names
163 Returns : all OTU names
164 Args : none
165  
166 =cut
167  
168 77 ▶sub get_otu_names {
Subroutine get_otu_names: called CC=2 SCAR=6.9
Pod documented
169 77 my ($self) = @_;
170 77 my @names = ();
171 77 for my $otu ( @{ $self->get_otus() } ) {
172 532 push @names, $otu->get_name();
173 }
174  
175 # @names = sort {lc $a cmp lc $b} @names;
176 77 return \@names;
177 }
178  
179 =head2 get_seq_string_hash
180  
181 Title : get_seq_string_hash
182 Usage : $set->get_seq_string_hash($delimiter);
183 Function: gets sequence string delimited by $delimiter (default is "")
184 Returns : hashref
185 Args : scalar
186  
187 =cut
188  
189 0 ▶sub get_seq_string_hash {
Subroutine get_seq_string_hash: not called CC=3 SCAR=24.8
Pod documented
190 0 my ( $self, $delimiter ) = @_;
191 0 my %sequences;
192 0 ▶ $delimiter = '' unless $delimiter;
Branch: 1 branch
BranchTrueFalseTotal
unless $delimiter 0 0 0
193 0 for my $otu ( @{ $self->get_otus() } ) {
194 0 $sequences{ $otu->get_name() } = join $delimiter, @{ $otu->get_seq() };
195 }
196 0 return \%sequences;
197 }
198  
199 =head2 get_seq_array_hash
200  
201 Title : get_seq_array_hash
202 Usage : $set->get_seq_array_hash();
203 Function: gets sequences as arrays
204 Returns : hashref
205 Args : scalar
206  
207 =cut
208  
209 8 ▶sub get_seq_array_hash {
Subroutine get_seq_array_hash: called CC=2 SCAR=6.9
Pod documented
210 8 my ($self) = @_;
211 8 my %sequences;
212 8 for my $otu ( @{ $self->get_otus() } ) {
213 47 $sequences{ $otu->get_name() } = $otu->get_seq();
214 }
215 8 return \%sequences;
216 }
217  
218 =head2 rename_otus
219  
220 Title : rename_otus
221 Usage : $set->rename_otus($names);
222 Function: rename all OTUs
223 Returns : none
224 Args : hash of OTU names
225  
226 =cut
227  
228 4 ▶sub rename_otus {
Subroutine rename_otus: called CC=3 SCAR=11.0
Pod documented
229 4 my ( $self, $translate ) = @_;
230 4 for my $otu ( @{ $self->get_otus() } ) {
231 14 my $name = $otu->get_name();
232 14 my $newname = $translate->{$name};
233 14 ▶ if ($newname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newname) 4 10 14
234 4 $otu->set_name($newname);
235 }
236 }
237 }
238  
239 =head2 subset
240  
241 Title : subset
242 Usage : $block->subset($otunames);
243 Function: select a subset of OTUs
244 Returns : new TaxUnitSet object
245 Args : OTU names
246  
247 =cut
248  
249 0 ▶sub subset {
Subroutine subset: not called CC=3 SCAR=24.8
Pod documented
250 0 my ( $self, $otunames ) = @_;
251 0 my $names = " @{$otunames} ";
252 0 my @newarray;
253 0 for my $otu ( @{ $self->get_otus() } ) {
254 0 my $name = $otu->get_name();
255 0 ▶ if ( $names =~ /\s+$name\s+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($names =~ /\s+$name\s+/) 0 0 0
256 0 push @newarray, $otu;
257 }
258 }
259 0 my $newset = new Bio::NEXUS::TaxUnitSet( \@newarray );
260 0 $newset->set_charlabels( $self->get_charlabels );
261 0 $newset->set_charstatelabels( $self->get_charstatelabels );
262 0 return $newset;
263 }
264  
265 =head2 select_columns
266  
267 Title : select_columns
268 Usage : $set->select_columns($columns);
269 Function: select a subset of characters
270 Returns : new $self with subset of columns of characters
271 Args : column numbers
272  
273 =cut
274  
275 1 ▶sub select_columns {
Subroutine select_columns: called CC=1 SCAR=0.0
Pod documented
276 1 my ( $self, $columns ) = @_;
277 1 $self->select_charlabels($columns);
278 1 $self->select_charstatelabels($columns);
279 1 $self->select_chars($columns);
280 1 return $self;
281 }
282  
283 =head2 select_chars
284  
285 Title : select_chars
286 Usage : $set->select_chars($columns);
287 Function: select a subset of characters
288 Returns : new self with subset of characters
289 Args : column numbers
290  
291 =cut
292  
293 1 ▶sub select_chars {
Subroutine select_chars: called CC=4 SCAR=13.9
Pod documented
294 1 my ( $self, $columns ) = @_;
295 1 my @otus = @{ $self->get_otus() };
296 1 for my $otu (@otus) {
297 1 my @seq = @{ $otu->get_seq() };
298 1 my @newseq;
299 1 for my $i ( @{$columns} ) {
300 1 ▶ if ( $i >= scalar @seq ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= scalar @seq) 0 1 1
301 0 throw 'BadArgs' => "invalid column number: " . ( $i + 1 );
302 }
303 1 push @newseq, $seq[$i];
304 }
305 1 $otu->set_seq( \@newseq );
306 }
307 1 return $self;
308 }
309  
310 =head2 set_charlabels
311  
312 Title : set_charlabels
313 Usage : $set->set_charlabels($labels);
314 Function: Set the character names
315 Returns : none
316 Args : array of character names
317  
318 =cut
319  
320 35 ▶sub set_charlabels {
Subroutine set_charlabels: called CC=2 SCAR=6.9
Pod documented
321 35 my ( $self, $labels ) = @_;
322 35 my $charstates;
323 35 ▶ for ( my $i = 0; $i < @$labels; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$labels) 220 35 255
324 220 push @$charstates,
325 { id => $i + 1, charlabel => $$labels[$i], states => {} }
326  
327 }
328 35 $self->{'charstates'} = $charstates;
329 }
330  
331 =head2 get_charlabels
332  
333 Title : get_charlabels
334 Usage : $set->get_charlabels();
335 Function: Returns an array of character labels
336 Returns : character names
337 Args : none
338  
339 =cut
340  
341 6 ▶sub get_charlabels {
Subroutine get_charlabels: called CC=3 SCAR=11.0
Pod documented
342 6 my ($self) = @_;
343 6 my $charlabels;
344 6 for my $charstate ( @{ $self->{'charstates'} } ) {
345 15 push @$charlabels, $charstate->{'charlabel'};
346 }
347 6 ▶ return $charlabels || [];
Condition100%2/2
$charlabels || []
l!l
33
MC/DC100%1/1
$charlabels || []
$charlabels
Truth table100%2/2
A
A= $charlabels
Aresult
11
00
348 }
349  
350 =head2 set_statelabels
351  
352 Title : set_statelabels
353 Usage : $set->set_statelabels($labels);
354 Function: Set the state names
355 Returns : none
356 Args : array of state names
357  
358 =cut
359  
360 0 ▶sub set_statelabels {
Subroutine set_statelabels: not called CC=1 SCAR=6.9
Pod documented
361 0 my ( $self, $labels ) = @_;
362 0 $self->{'statelabels'} = $labels;
363 }
364  
365 =head2 get_statelabels
366  
367 Title : get_statelabels
368 Usage : $set->get_statelabels();
369 Function: Returns an array of state labels
370 Returns : state names
371 Args : none
372  
373 =cut
374  
375 144 ▶sub get_statelabels {
Subroutine get_statelabels: called CC=2 SCAR=7.2
Pod documented
376 144 my ($self) = @_;
377 144 ▶ return $self->{'statelabels'} || [];
Condition50%1/2
$self->{'statelabels'} || []
l!l
0144
MC/DC0%0/1
$self->{'statelabels'} || []
$self->{'statelabels'}
Truth table50%1/2
A
A= $self->{'statelabels'}
Aresult
11
00
378 }
379  
380 =head2 set_charstatelabels
381  
382 Title : set_charstatelabels
383 Usage : $set->set_charstatelabels($labels);
384 Function: Set the character names and states
385 Returns : none
386 Args : array of character states
387  
388 =cut
389  
390 73 ▶sub set_charstatelabels {
Subroutine set_charstatelabels: called CC=1 SCAR=0.0
Pod documented
391 73 my ( $self, $states ) = @_;
392 73 $self->{'charstatelabels'} = $states;
393 }
394  
395 =head2 get_charstatelabels
396  
397 Title : get_charstatelabels
398 Usage : $set->get_charstatelabels();
399 Function: Returns an array of character states
400 Returns : character states
401 Args : none
402  
403 =cut
404  
405 77 ▶sub get_charstatelabels {
Subroutine get_charstatelabels: called CC=2 SCAR=6.9
Pod documented
406 77 my ($self) = @_;
407 77 ▶ return $self->{'charstatelabels'} || [];
Condition100%2/2
$self->{'charstatelabels'} || []
l!l
4334
MC/DC100%1/1
$self->{'charstatelabels'} || []
$self->{'charstatelabels'}
Truth table100%2/2
A
A= $self->{'charstatelabels'}
Aresult
11
00
408 }
409  
410 =head2 get_ntax
411  
412 Title : get_ntax
413 Usage : $set->get_ntax();
414 Function: Returns the number of taxa of the block
415 Returns : # taxa
416 Args : none
417  
418 =cut
419  
420 5 ▶sub get_ntax {
Subroutine get_ntax: called CC=2 SCAR=7.2
Pod documented
421 5 my $self = shift;
422 5 my $otus = $self->get_otus();
423 5 ▶ if ( ref $otus ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $otus) { } 5 0 5
424 5 return scalar @{ $self->get_otus() };
425 }
426 else {
427 0 $logger->warn("No otus found\n")
428 }
429 }
430  
431 =head2 get_nchar
432  
433 Title : get_nchar
434 Usage : $set->get_nchar();
435 Function: Returns the number of characters of the block
436 Returns : # charaters
437 Args : none
438  
439 =cut
440  
441 2 ▶sub get_nchar {
Subroutine get_nchar: called CC=1 SCAR=0.0
Pod documented
442 2 my $self = shift;
443 2 return scalar @{ $self->get_otus()->[0]->get_seq() };
444 }
445  
446 =head2 select_charlabels
447  
448 Title : select_charlabels
449 Usage : $set->select_charlabels($columns);
450 Function: select a subset of charlabels
451 Returns : new self with subset of charlabels
452 Args : column numbers
453  
454 =cut
455  
456 1 ▶sub select_charlabels {
Subroutine select_charlabels: called CC=3 SCAR=14.8
Pod documented
457 1 my ( $self, $columns ) = @_;
458 1 my @labels = @{ $self->get_charlabels() };
459 1 ▶ if ( @labels == 0 ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (@labels == 0) 1 0 1
460  
461 0 my @newlabels = ();
462 0 for my $i ( @{$columns} ) {
463 0 push @newlabels, $labels[$i];
464 }
465  
466 0 $self->set_charlabels( \@newlabels );
467 0 return $self;
468 }
469  
470 =head2 select_charstatelabels
471  
472 Title : select_charstatelabels
473 Usage : $set->select_charstatelabels($columns);
474 Function: select a subset of charstates
475 Returns : new self with subset of charstates
476 Args : column numbers
477  
478 =cut
479  
480 1 ▶sub select_charstatelabels {
Subroutine select_charstatelabels: called CC=3 SCAR=11.1
Pod documented
481 1 my ( $self, $columns ) = @_;
482 1 my @labels = @{ $self->get_charstatelabels() };
483 0 ▶ if ( @labels == 0 ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (@labels == 0) 0 1 1
484  
485 1 my @newlabels = ();
486 1 for my $i ( @{$columns} ) {
487 1 push @newlabels, $labels[$i];
488 }
489  
490 1 $self->set_charstatelabels( \@newlabels );
491 1 return $self;
492 }
493  
494 =head2 equals
495  
496 Name : equals
497 Usage : $set->equals($another);
498 Function: compare if two TaxUnitSet objects are equal
499 Returns : boolean
500 Args : an TaxUnitSet object
501  
502 =cut
503  
504 9 ▶sub equals {
Subroutine equals: called CC=10 SCAR=27.1
Pod documented
505 9 my ( $self, $set ) = @_;
506 9 my @otus1 = @{ $self->get_otus() };
507 9 my @otus2 = @{ $set->get_otus() };
508 0 ▶ if ( @otus1 != @otus2 ) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (@otus1 != @otus2) 0 9 9
509 9 @otus1 = sort { $a->get_name() cmp $b->get_name() } @otus1;
510 9 @otus2 = sort { $a->get_name() cmp $b->get_name() } @otus2;
511 9 ▶ for ( my $i = 0; $i < @otus1; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @otus1) 35 6 41
512  
513 # check names
514 35 ▶ if ( $otus1[$i]->get_name() ne $otus2[$i]->get_name() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otus1[$i]->get_name ne $otus2[$i]->get_name) 0 35 35
515 #carp "OTU names not equal: " . $otus1[$i]->get_name() . " ne " . $otus2[$i]->get_name() . "\n";
516 0 return 0;
517 }
518  
519 # check seq's
520 35 my @seqs1 = @{ $otus1[$i]->get_seq() };
521 35 my @seqs2 = @{ $otus2[$i]->get_seq() };
522  
523 0 ▶ if ( @seqs1 != @seqs2 ) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (@seqs1 != @seqs2) 0 35 35
524 35 ▶ for ( my $j = 0; $j < @seqs1; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @seqs1) 1160 32 1192
525  
526 # entry is an array ref of probability values
527 1160 ▶ if ( ref( $seqs1[$j] ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $seqs1[$j] eq 'ARRAY') { } 0 1160 1160
elsif ($seqs1[$j] ne $seqs2[$j]) { } 3 1157 1160
528 0 my @prob1 = @{ $seqs1[$j] };
529 0 my @prob2 = @{ $seqs2[$j] };
530 0 ▶ for ( my $k = 0; $k < @prob1; $k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < @prob1) 0 0 0
531 0 ▶ if ( $prob1[$k] != $prob2[$k] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prob1[$k] != $prob2[$k]) 0 0 0
532 0 return 0;
533 }
534 }
535 }
536  
537 # entry is a character datum
538 elsif ( $seqs1[$j] ne $seqs2[$j] ) {
539 #carp "Character values not equal: $seqs1[$j] != $seqs2[$j]\n";
540 3 return 0;
541 }
542 }
543  
544 }
545 6 return 1;
546 }
547  
548 79 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=21.3
549 79 ▶ return if $AUTOLOAD =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Bio::NEXUS::TaxUnitSet::AUTOLOAD =~ /DESTROY$/ 79 0 79
550 0 my $package_name = __PACKAGE__ . '::';
551  
552 # The following methods are deprecated and are temporarily supported
553 # via a warning and a redirection
554 0 my %synonym_for = (
555 "${package_name}set_charstates" => "${package_name}set_charstatelabels",
556 "${package_name}get_charstates" => "${package_name}get_charstatelabels",
557 "${package_name}select_charstates" =>
558 "${package_name}select_charstatelabels",
559 "${package_name}get_otu_sequences" =>
560 "${package_name}get_seq_string_hash",
561 );
562  
563 0 ▶ if ( defined $synonym_for{$AUTOLOAD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $synonym_for{$Bio::NEXUS::TaxUnitSet::AUTOLOAD}) { } 0 0 0
564 0 $logger->warn("$AUTOLOAD() is deprecated; use $synonym_for{$AUTOLOAD}() instead");
565 0 goto &{ $synonym_for{$AUTOLOAD} };
566 }
567 else {
568 0 throw 'UnknownMethod' => "ERROR: Unknown method $AUTOLOAD called";
569 }
570 0 return;
571 }
572  
573 35 1;
« lib/Bio/NEXUS/NHXCmd.pm ↑ Summary lib/Bio/NEXUS/DataBlock.pm »