lib/Bio/NEXUS/SetsBlock.pm

filter: Statementstmt 35.4% 61 / 172 Branchbran 16.6% 7 / 42 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 58.3% 14 / 24 Podpod 100% 14 / 14 totaltotal 35.8% 99 / 276 CC 44 SCAR 32.6
CC
44
Cov
31%
equals
90.0
_write
42.0
print_all_taxsets
30.0
CRAP 26.2
« script/nex2text_tree.pl ↑ Summary lib/Bio/NEXUS/TaxUnit.pm »
1 ######################################################
2 # SetsBlock.pm
3 ######################################################
4 # Author: Thomas Hladish
5 # $Id: SetsBlock.pm,v 1.32 2007/09/21 23:09:09 rvos Exp $
6 #################### START POD DOCUMENTATION ##################
7  
8 =head1 NAME
9  
10 Bio::NEXUS::SetsBlock - Represents SETS block of a NEXUS file
11  
12 =head1 SYNOPSIS
13  
14 $block_object = new Bio::NEXUS::SetsBlock($block_type, $block, $verbose);
15  
16 =head1 DESCRIPTION
17  
18 Parses Sets block of NEXUS file and stores Sets data.
19  
20 =head1 FEEDBACK
21  
22 All feedback (bugs, feature enhancements, etc.) are greatly appreciated.
23  
24 =head1 AUTHORS
25  
26 Thomas Hladish (tjhladish at yahoo)
27  
28 =head1 VERSION
29  
30 $Revision: 1.32 $
31  
32 =head1 METHODS
33  
34 =cut
35  
36 package Bio::NEXUS::SetsBlock;
37  
38 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 #use Carp; # XXX this is not used, might as well not import it!
40 #use Data::Dumper; # XXX this is not used, might as well not import it!
41 35 ▶use Bio::NEXUS::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 35 ▶use Bio::NEXUS::Block;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 35 ▶use Bio::NEXUS::Util::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 35 ▶use Bio::NEXUS::Util::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 35 ▶use vars qw(@ISA $VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 35 ▶use Bio::NEXUS; $VERSION = $Bio::NEXUS::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 35 @ISA = qw(Bio::NEXUS::Block);
49 35 my $logger = Bio::NEXUS::Util::Logger->new();
50  
51 =head2 new
52  
53 Title : new
54 Usage : $block_object = new Bio::NEXUS::SetsBlock($block_type, $commands, $verbose)
55 Function: Creates a new Bio::NEXUS::SetsBlock object
56 Returns : Bio::NEXUS::SetsBlock object
57 Args : type (string), the commands/comments to parse (array ref), and a verbose flag (0 or 1)
58  
59 =cut
60  
61 2 ▶sub new {
Subroutine new: called CC=4 SCAR=15.5
Pod documented
62 2 my ( $class, $type, $commands, $verbose, $taxlabels ) = @_;
63 0 ▶ unless ($type) { ( $type = lc $class ) =~ s/Bio::NEXUS::(.+)Block/$1/i; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 2 2
64 2 my $self = { type => $type };
65 2 bless $self, $class;
66 2 ▶ $self->_parse_block( $commands, $verbose, $taxlabels )
Branch: 1 branch
BranchTrueFalseTotal
if defined $commands and @$commands 2 0 2
Condition33%1/3
defined $commands and @$commands
!ll&&!rl&&r
002
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
67 if ( ( defined $commands ) and @$commands );
68 1 return $self;
69 }
70  
71 =begin comment
72  
73 Title : _parse_taxset
74 Usage :
75  
76 =end comment
77  
78 =cut
79  
80 5 ▶sub _parse_taxset {
Subroutine _parse_taxset: called CC=1 SCAR=0.0
81 5 my ( $self, $buffer ) = @_;
82 5 my ( $setname, $equals_symb, @taxa ) = @{ _parse_nexus_words($buffer) };
83  
84 5 my $taxsets;
85 5 $taxsets->{$setname} = \@taxa;
86  
87 #$self->set_taxsets($taxsets);
88 5 $self->add_taxsets( { $setname, \@taxa } );
89  
90 5 return $taxsets;
91 }
92  
93 =head2 set_taxsets
94  
95 Title : set_taxsets
96 Usage : $block->set_taxsets($taxsets);
97 Function: Set the taxsets hash
98 Returns : none
99 Args : hash of set name keys and element arrays
100  
101 =cut
102  
103 0 ▶sub set_taxsets {
Subroutine set_taxsets: not called CC=1 SCAR=6.9
Pod documented
104 0 my ( $self, $taxsets ) = @_;
105 0 $self->{'taxsets'} = $taxsets;
106 }
107  
108 =head2 add_taxsets
109  
110 Title : add_taxsets
111 Usage : $block->add_taxsets($taxsets);
112 Function: add taxa sets
113 Returns : none
114 Args : a reference to a hash of taxa sets
115  
116 =cut
117  
118 6 ▶sub add_taxsets {
Subroutine add_taxsets: called CC=2 SCAR=6.9
Pod documented
119 6 my ( $self, $taxsets ) = @_;
120 6 for my $setname ( keys %{$taxsets} ) {
121 6 ${ $self->{'taxsets'} }{$setname} = ( $$taxsets{$setname} );
122 }
123 }
124  
125 =head2 get_taxsets
126  
127 Title : get_taxsets
128 Usage : $block->get_taxsets();
129 Function: Returns a hash of taxa sets
130 Returns : taxa sets
131 Args : none
132  
133 =cut
134  
135 8 ▶sub get_taxsets {
Subroutine get_taxsets: called CC=2 SCAR=7.2
Pod documented
136 8 my ($self) = @_;
137 8 ▶ return $self->{'taxsets'} || {};
Condition50%1/2
$self->{'taxsets'} || {}
l!l
80
MC/DC0%0/1
$self->{'taxsets'} || {}
$self->{'taxsets'}
Truth table50%1/2
A
A= $self->{'taxsets'}
Aresult
11
00
138 }
139  
140 =head2 get_taxset
141  
142 Title : get_taxset
143 Usage : $block->get_taxset($setname);
144 Function: Returns a list of OTU's
145 Returns : OTU's
146 Args : none
147  
148 =cut
149  
150 3 ▶sub get_taxset {
Subroutine get_taxset: called CC=2 SCAR=7.2
Pod documented
151 3 my ( $self, $setname ) = @_;
152 3 ▶ return $self->{'taxsets'}->{$setname} || [];
Condition50%1/2
$self->{'taxsets'}{$setname} || []
l!l
30
MC/DC0%0/1
$self->{'taxsets'}{$setname} || []
$self->{'taxsets'}{$setname}
Truth table50%1/2
A
A= $self->{'taxsets'}{$setname}
Aresult
11
00
153 }
154  
155 =head2 get_taxset_names
156  
157 Title : get_taxset_names
158 Usage : $block->get_taxset_names()
159 Function: gets the names of all sets
160 Returns : array of names
161 Args : none
162
163 =cut
164  
165 0 ▶sub get_taxset_names {
Subroutine get_taxset_names: not called CC=1 SCAR=6.9
Pod documented
166 0 my ($self) = @_;
167 0 return [ sort keys %{ $self->{'taxsets'} } ];
168 }
169  
170 =head2 print_all_taxsets
171  
172 Title : print_all_taxsets
173 Usage : $block->print_all_taxsets($outfile)
174 Function: prints set names and elements
175 Returns : none
176 Args : filename or filehandle
177
178 =cut
179  
180 0 ▶sub print_all_taxsets {
Subroutine print_all_taxsets: not called CC=5 SCAR=34.0
Pod documented
181 0 my ( $self, $outfile ) = @_;
182 0 my $fh;
183 0 ▶ if ( $outfile eq "-" || $outfile eq \*STDOUT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile eq '-' or $outfile eq \*STDOUT) { } 0 0 0
Condition0%0/3
$outfile eq "-" or $outfile eq \*STDOUT
l!l&&r!l&&!r
000
MC/DC0%0/2
$outfile eq "-" or $outfile eq \*STDOUT
$outfile eq "-" $outfile eq \*STDOUT
Truth table0%0/3
A or B
A= $outfile eq "-"B= $outfile eq \*STDOUT
ABresult
1X1
011
000
184 0 $fh = \*STDOUT;
185 }
186 else {
187 0 ▶ open( $fh, ">$outfile" )
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">$outfile" or "Bio::NEXUS::Util::Exceptions::FileError"->throw("error", "Could not open $outfile for writing") 0 0 0
188 || Bio::NEXUS::Util::Exceptions::FileError->throw(
189 'error' => "Could not open $outfile for writing"
190 );
191 }
192  
193 0 for my $setname ( sort keys %{ $self->{'taxsets'} } ) {
194 0 print $fh "$setname = [@{$self->{'taxsets'}->{$setname}}]\n\n";
195 }
196 }
197  
198 =head2 delete_taxsets
199  
200 Title : delete_taxsets
201 Usage : $block->delete_taxsets($set1 [$set2 $set3 ...])
202 Function: Removes the named sets from the Sets block
203 Returns : none
204 Args : Names of sets to be deleted
205  
206 =cut
207  
208 0 ▶sub delete_taxsets {
Subroutine delete_taxsets: not called CC=2 SCAR=17.9
Pod documented
209 0 my ( $self, @setnames ) = @_;
210 0 for my $setname (@setnames) {
211 0 delete ${ $self->{'taxsets'} }{$setname};
212 }
213 }
214  
215 =head2 exclude_otus
216  
217 Title : exclude_otus
218 Usage : $block->exclude_otus($otu_array_ref)
219 Function: Finds and deletes each of the given otus from any sets they appear in
220 Returns : none
221 Args : Names of otus to be removed
222
223 =cut
224  
225 0 ▶sub exclude_otus {
Subroutine exclude_otus: not called CC=5 SCAR=34.0
Pod documented
226 0 my ( $self, $otus_to_remove ) = @_;
227 0 for my $setname ( keys %{ $self->{'taxsets'} } ) {
228 0 ▶ for ( my $i = 0; $i < @{ $self->{'taxsets'}{$setname} }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"taxsets"}{$setname};}) 0 0 0
229 0 for my $otu_to_remove (@$otus_to_remove) {
230 0 ▶ if ( $self->{'taxsets'}->{$setname}[$i] eq $otu_to_remove ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"taxsets"}{$setname}[$i] eq $otu_to_remove) 0 0 0
231 0 splice( @{ $self->{'taxsets'}{$setname} }, $i, 1 );
232 }
233 }
234 }
235 }
236 }
237  
238 =head2 select_otus
239  
240 Title : select_otus
241 Usage : $block->select_otus($otu_array_ref)
242 Function: Finds the given otus and removes all others from any sets they appear in
243 Returns : none
244 Args : Names of otus to be removed
245
246 =cut
247  
248 0 ▶sub select_otus {
Subroutine select_otus: not called CC=5 SCAR=34.0
Pod documented
249 0 my ( $self, $otus_to_keep ) = @_;
250 0 my $newsets;
251 0 for my $setname ( keys %{ $self->{'taxsets'} } ) {
252 0 $$newsets{$setname} = [];
253 0 for my $otu_element ( @{ $self->{'taxsets'}{$setname} } ) {
254 0 for my $otu_to_keep (@$otus_to_keep) {
255 0 ▶ if ( $otu_element eq $otu_to_keep ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otu_element eq $otu_to_keep) 0 0 0
256 0 push( @{ $$newsets{$setname} }, $otu_to_keep );
257 }
258 }
259 }
260 }
261 0 $self->set_taxsets($newsets);
262 }
263  
264 =head2 rename_otus
265  
266 Title : rename_otus
267 Usage : $block->rename_otus($names);
268 Function: rename all OTUs
269 Returns : none
270 Args : hash of OTU names
271  
272 =cut
273  
274 0 ▶sub rename_otus {
Subroutine rename_otus: not called CC=4 SCAR=30.0
Pod documented
275 0 my ( $self, $translation ) = @_;
276 0 for my $setname ( @{ $self->get_taxset_names() } ) {
277 0 my @otu_names = @{ $self->get_taxset($setname) };
278 0 my @new_otu_names;
279 0 for my $otu_name (@otu_names) {
280 0 ▶ if ( my $new_name = $$translation{$otu_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $new_name = $translation->{$otu_name}) { } 0 0 0
281 0 push( @new_otu_names, $new_name );
282 }
283 else {
284 0 push( @new_otu_names, $otu_name );
285 }
286 }
287 0 $self->add_taxsets( { $setname, \@new_otu_names } );
288 }
289 }
290  
291 =head2 add_otu_clone
292  
293 Title : add_otu_clone
294 Usage : ...
295 Function: ...
296 Returns : ...
297 Args : ...
298  
299 =cut
300  
301 1 ▶sub add_otu_clone {
Subroutine add_otu_clone: called CC=4 SCAR=13.9
Pod documented
302 1 my ( $self, $original_otu_name, $copy_otu_name ) = @_;
303 # print "Warning: Bio::NEXUS::SetsBlock::add_otu_clone() method not fully implemented\n";
304
305 # add the cloned otu to those sets that contain the original otu
306 1 foreach my $set_id (keys %{ $self->get_taxsets() }) {
307 #print "> set ", $set_id, "\n";
308 6 my @set = @{ $self->get_taxsets()->{$set_id} };
309 6 foreach my $otu (@set) {
310 12 ▶ if ($otu eq $original_otu_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otu eq $original_otu_name) 2 10 12
311 #print "> found the original otu in ", $set_id, "\n";
312 2 push (@{$self->{'taxsets'}{$set_id}}, $copy_otu_name);
313 }
314 }
315 }
316 }
317  
318 =head2 rename_taxsets
319  
320 Title : rename_taxsets
321 Usage : $block->rename_taxsets($oldsetname1, $newsetname1, ...)
322 Function: Renames sets
323 Returns : none
324 Args : Oldname, newname pairs
325  
326 =cut
327  
328 0 ▶sub rename_taxsets {
Subroutine rename_taxsets: not called CC=4 SCAR=30.0
Pod documented
329 0 my ( $self, @old_and_new ) = @_;
330 0 my ( @old, @new );
331 0 ▶ while (@old_and_new) {
Branch: 1 branch
BranchTrueFalseTotal
if (@old_and_new) 0 0 0
332 0 push( @old, shift(@old_and_new) );
333 0 push( @new, shift(@old_and_new) );
334 }
335 0 ▶ for ( my $i = 0; $i < scalar(@old); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @old) 0 0 0
336 0 ▶ if ( $self->{'taxsets'}{ $old[$i] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'taxsets'}{$old[$i]}) { } 0 0 0
337 0 $self->{'taxsets'}{ $new[$i] } = $self->{'taxsets'}{ $old[$i] };
338 0 delete $self->{'taxsets'}{ $old[$i] };
339 }
340 else {
341 0 print "$old[$i] is not the name of a set in this NEXUS file.\n";
342 }
343 }
344 }
345  
346 =head2 equals
347  
348 Name : equals
349 Usage : $setsblock->equals($another);
350 Function: compare if two Bio::NEXUS::SetsBlock objects are equal
351 Returns : boolean
352 Args : a Bio::NEXUS::SetsBlock object
353  
354 =cut
355  
356 0 ▶sub equals {
Subroutine equals: not called CC=9 SCAR=45.0
Pod documented
357 0 my ( $block1, $block2 ) = @_;
358 0 ▶ if ( !Bio::NEXUS::Block::equals( $block1, $block2 ) ) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (Bio::NEXUS::Block::equals($block1, $block2)) 0 0 0
359 0 my $sets1 = $block1->get_taxsets();
360 0 my $sets2 = $block2->get_taxsets();
361 0 ▶ if ( keys %$sets1 != keys %$sets2 ) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$sets1 != keys %$sets2) 0 0 0
362 0 for my $setname1 ( keys %$sets1 ) {
363 0 ▶ unless ( ( defined $$sets2{$setname1} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sets2->{$setname1} and @{$sets1->{$setname1};} == @{$sets2->{$setname1};}) 0 0 0
Condition0%0/3
defined $sets2->{$setname1} and @{$sets1->{$setname1};} == @{$sets2->{$setname1};}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sets2->{$setname1} and @{$sets1->{$setname1};} == @{$sets2->{$setname1};}
defined $sets2->{$setname1} @{$sets1->{$setname1};} == @{$sets2->{$setname1};}
Truth table0%0/3
A and B
A= defined $sets2->{$setname1}B= @{$sets1->{$setname1};} == @{$sets2->{$setname1};}
ABresult
0X0
100
111
364 0 && ( @{ $$sets1{$setname1} } == @{ $$sets2{$setname1} } ) )
365 {
366 0 return 0;
367 }
368 }
369 0 for my $setname1 ( keys %$sets1 ) {
370 0 @{ $$sets1{$setname1} } = sort @{ $$sets1{$setname1} };
371 0 @{ $$sets2{$setname1} } = sort @{ $$sets2{$setname1} };
372 0 ▶ for ( my $i = 0; $i < @{ $$sets1{$setname1} }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$sets1->{$setname1};}) 0 0 0
373 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (do { $$sets1{$setname1} }->[$i] eq do { $$sets2{$setname1} }->[$i]) 0 0 0
374 0 ${ $$sets1{$setname1} }[$i] eq ${ $$sets2{$setname1} }[$i] )
375 {
376 0 return 0;
377 }
378 }
379 }
380 0 return 1;
381 }
382  
383 =begin comment
384  
385 Name : _write
386 Usage : $sets -> _write($filehandle, $verbose);
387 Function: Writes NEXUS Sets block from stored data
388 Returns : none
389 Args : none
390  
391 =end comment
392  
393 =cut
394  
395 0 ▶sub _write {
Subroutine _write: not called CC=6 SCAR=37.4
396 0 my ( $self, $fh, $verbose ) = @_;
397 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
398  
399 0 Bio::NEXUS::Block::_write( $self, $fh );
400 0 for my $setname ( sort keys %{ $self->{'taxsets'} } ) {
401 0 my @set_elements = sort @{ ${ $self->{'taxsets'} }{$setname} };
402 0 my $i = 0;
403 0 ▶ for ( my $j = 0; $j + 1 < @set_elements; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j + 1 < @set_elements) 0 0 0
404 0 ▶ if ( $set_elements[$i] eq $set_elements[ $i + 1 ] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set_elements[$i] eq $set_elements[$i + 1]) { } 0 0 0
405 0 splice( @set_elements, $i, 1 );
406 }
407 else {
408 0 $i++;
409 }
410 }
411 0 $setname = _nexus_formatted($setname);
412 0 print $fh "\tTAXSET $setname =";
413 0 for my $element (@set_elements) {
414 0 $element = _nexus_formatted($element);
415 0 print $fh " $element";
416 }
417 0 print $fh ";\n";
418 }
419 0 print $fh "END;\n";
420 }
421  
422 3 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=12.6
423 3 ▶ return if $AUTOLOAD =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Bio::NEXUS::SetsBlock::AUTOLOAD =~ /DESTROY$/ 2 1 3
424 1 my $package_name = __PACKAGE__ . '::';
425  
426 # The following methods are deprecated and are temporarily supported
427 # via a warning and a redirection
428 1 my %synonym_for = (
429  
430 # "${package_name}parse" => "${package_name}_parse_tree", # example
431 );
432  
433 1 ▶ if ( defined $synonym_for{$AUTOLOAD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $synonym_for{$Bio::NEXUS::SetsBlock::AUTOLOAD}) { } 0 1 1
434 0 $logger->warn("$AUTOLOAD() is deprecated; use $synonym_for{$AUTOLOAD}() instead");
435 0 goto &{ $synonym_for{$AUTOLOAD} };
436 }
437 else {
438 1 Bio::NEXUS::Util::Exceptions::UnknownMethod->throw(
439 'error' => "ERROR: Unknown method $AUTOLOAD called"
440 );
441 }
442 0 return;
443 }
444  
445 35 1;
« script/nex2text_tree.pl ↑ Summary lib/Bio/NEXUS/TaxUnit.pm »