lib/Bio/NEXUS/NHXCmd.pm

filter: Statementstmt 85.7% 114 / 133 Branchbran 71.4% 40 / 56 Conditioncond 47.6% 10 / 21 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 86.3% 19 / 22 Podpod 100% 12 / 12 totaltotal 76.7% 198 / 258 CC 42 SCAR 17.5
CC
42
Cov
78%
_parse_nhx_command
12.4
equals
7.1
check_tag_value_present
6.7
CRAP 5.8
« lib/Bio/NEXUS/AssumptionsBlock.pm ↑ Summary lib/Bio/NEXUS/TaxUnitSet.pm »
1  
2 ######################################################
3 # NHXCmd.pm
4 ######################################################
5 # Author:
6 # $Id: NHXCmd.pm,v 1.9 2007/09/21 23:09:09 rvos Exp $
7  
8 #################### START POD DOCUMENTATION ##################
9  
10 =head1 NAME
11  
12 Bio::NEXUS::NHXCmd - Provides functions for manipulating nodes in trees
13  
14 =head1 SYNOPSIS
15  
16 new Bio::NEXUS::NHXCmd;
17  
18 =head1 DESCRIPTION
19  
20 Provides a few useful functions for nodes.
21  
22 =head1 FEEDBACK
23  
24 All feedback (bugs, feature enhancements, etc.) are all greatly appreciated. There are no mailing lists at this time for the Bio::NEXUS::Node module, so send all relevant contributions to Dr. Weigang Qiu (weigang@genectr.hunter.cuny.edu).
25  
26 =head1 AUTHORS
27  
28 Mikhail Bezruchko (bezruchk@umbi.umd.edu), Vivek Gopalan
29  
30 =head1 CONTRIBUTORS
31  
32  
33 =head1 METHODS
34  
35 =cut
36  
37 package Bio::NEXUS::NHXCmd;
38  
39 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 #use Bio::NEXUS::Functions;
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::Util::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 35 ▶use vars '$VERSION';
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 ▶sub BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.5
49 eval {
50 35 require warnings;
51 35 1;
52 }
53 35 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require warnings; 1 } } or do { no strict 'refs'; *warnings::import = *warnings::unimport = sub { } ; $INC{"warnings.pm"} = "" } 35 0 35
54 35 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
55 0 ▶ *warnings::import = *warnings::unimport = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 $INC{'warnings.pm'} = '';
57 };
58 }
59  
60 =head2 new
61  
62 Title : new
63 Usage : $nhx_cmd = new Bio::NEXUS::NHXCmd($comment_string);
64 Function: Creates a new Bio::NEXUS::NHXCmd object
65 Returns : Bio::NEXUS::NHXCmd object
66 Args : $comment_string - a string representation of the comment (w/o brackets)
67  
68 =cut
69  
70 140 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
71 140 my ( $class, $command_str ) = @_;
72 140 my $self = { '_tag_data' => undef };
73 140 bless $self, $class;
74 140 ▶ if ( defined $command_str and $self->_is_nhx_command($command_str) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $command_str and $self->_is_nhx_command($command_str)) 54 86 140
Condition100%3/3
defined $command_str and $self->_is_nhx_command($command_str)
!ll&&!rl&&r
444254
MC/DC100%2/2
defined $command_str and $self->_is_nhx_command($command_str)
defined $command_str $self->_is_nhx_command($command_str)
Truth table100%3/3
A and B
A= defined $command_strB= $self->_is_nhx_command($command_str)
ABresult
0X0
100
111
75 54 $self->_parse_nhx_command($command_str);
76 }
77 140 return $self;
78 }
79  
80 =head2 to_string
81  
82 Title : to_string
83 Usage : $comment_str = $nhx_obj->to_string
84 Function: Returns a string representation of the NHX command
85 Returns : String
86 Args : None
87  
88 =cut
89  
90 143 ▶sub to_string {
Subroutine to_string: called CC=6 SCAR=18.0
Pod documented
91 143 my ($self) = @_;
92 143 my $result = "&&NHX";
93 143 ▶ if ( not defined $self->{_tag_data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'_tag_data'}) { } 42 101 143
94 42 $result = undef;
95 42 return $result;
96 }
97 else {
98 101 for my $tag ( sort keys %{ $self->{_tag_data} } ) {
99  
100 #print $tag;
101  
102 294 ▶ if ( defined $tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) 294 0 294
103 294 my @values = $self->get_values($tag);
104  
105 294 for my $value (@values) {
106 295 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 295 295
107 295 $result .= ":$tag=$value";
108 }
109 }
110 }
111 101 return $result;
112 }
113 }
114  
115 =head2 equals
116  
117 Title : equals
118 Usage : $nhx_one->equals($nhx_two);
119 Function: compares two NHX objects for equality
120 Returns : 1 if the two objects contain the same date; 0 if they don't
121 Args : $nhx_two - a Bio::NEXUS::NHXCmd object
122  
123 =cut
124  
125 18 ▶sub equals {
Subroutine equals: called CC=7 SCAR=19.6
Pod documented
126 18 my ( $self, $other ) = @_;
127  
128 18 my @self_tags = $self->get_tags();
129 18 my @other_tags = $other->get_tags();
130  
131 18 ▶ if ( scalar @self_tags != scalar @other_tags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @self_tags != scalar @other_tags) { } 1 17 18
132 1 return 0;
133 }
134 else {
135 17 for my $tag (@self_tags) {
136 46 ▶ if ( !$other->contains_tag($tag) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($other->contains_tag($tag)) 1 45 46
137 1 return 0;
138 }
139  
140 45 my @self_values = sort $self->get_values($tag);
141 45 my @other_values = sort $other->get_values($tag);
142  
143 45 ▶ if ( scalar @self_values != scalar @other_values ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @self_values != scalar @other_values) 0 45 45
144 0 return 0;
145 }
146  
147 45 ▶ for ( my $i = 0; $i < scalar @self_values; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @self_values) 45 43 88
148 45 ▶ if ( $self_values[$i] ne $other_values[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self_values[$i] ne $other_values[$i]) 2 43 45
149 2 return 0;
150 }
151 }
152 }
153 14 return 1;
154 }
155 0 return 0;
156 }
157  
158 =head2 clone
159  
160 Title : clone
161 Usage : $new_obj = $original->clone();
162 Function: Creates a "deep copy" of a Bio::NEXUS::NHXCmd
163 Returns : A "deep copy" of a Bio::NEXUS::NHXCmd
164 Args : None
165  
166 =cut
167  
168 8 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
169 8 my ($self) = @_;
170 8 my $class = ref($self);
171  
172 #return bless( { %{$self} }, $class );
173 8 my $data;
174 8 $data->{_tag_data} = _deep_copy( $self->{_tag_data} );
175 8 return bless( { %{$data} }, $class );
176  
177 } # end of sub
178  
179 #### ?????????? Has to be added to the Bio::NEXUS::Functions package for deep copying data structures
180 ## reference : http://www.stonehenge.com/merlyn/UnixReview/col30.html
181 ####
182  
183 28 ▶sub _deep_copy {
Subroutine _deep_copy: called CC=4 SCAR=14.0
184 28 my $this = shift;
185 28 ▶ if ( not ref $this ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $this) { } 10 18 28
elsif (ref $this eq 'ARRAY') { } 10 8 18
elsif (ref $this eq 'HASH') { } 8 0 8
186 10 $this;
187 }
188 elsif ( ref $this eq "ARRAY" ) {
189 10 [ map _deep_copy($_), @$this ];
190 }
191 elsif ( ref $this eq "HASH" ) {
192 8 +{ map { $_ => _deep_copy( $this->{$_} ) } keys %$this };
193 }
194 0 else { die "what type is $_?" }
195 }
196  
197 =begin comment
198  
199 Title : _parse_nhx_command
200 Usage : N/A
201 Function: A utility (private) function that parses an NHX comment of the tree node
202 Returns : Hash of arrays, where each key is an NHX tag (i.e. 'B'), and the value is an array of values associated with that tag
203 Args : $comment - a string containing the comment
204  
205 =end comment
206  
207 =cut
208  
209 54 ▶sub _parse_nhx_command {
Subroutine _parse_nhx_command: called CC=10 SCAR=25.2
210 54 my ( $self, $command_str ) = @_;
211 54 my @command = split( //, $command_str );
212 54 my $word = "";
213 54 my @words = ();
214  
215 #
216 # 1. Split the NHX command into words (tag+value combo)
217 #
218 54 my $open_quote = 0;
219 54 for my $char (@command) {
220  
221 # try converting all dbl-quotes to sngl-quotes
222 1993 ▶ if ( !$open_quote && $char =~ /("|')/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $open_quote and $char =~ /("|')/) 0 1993 1993
Condition33%1/3
not $open_quote and $char =~ /("|')/
!ll&&!rl&&r
019930
MC/DC0%0/2
not $open_quote and $char =~ /("|')/
not $open_quote $char =~ /("|')/
Truth table33%1/3
not(A) and B
A= not $open_quoteB= $char =~ /("|')/
ABresult
0X0
100
111
223 0 $open_quote = 1;
224 0 next;
225 }
226  
227 1993 ▶ if ( $open_quote && $char =~ /("|')/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($open_quote and $char =~ /("|')/) { } 0 1993 1993
elsif (not $open_quote and $char eq ':') { } 158 1835 1993
Condition33%1/3
$open_quote and $char =~ /("|')/
!ll&&!rl&&r
199300
MC/DC0%0/2
$open_quote and $char =~ /("|')/
$open_quote $char =~ /("|')/
Truth table33%1/3
A and B
A= $open_quoteB= $char =~ /("|')/
ABresult
0X0
100
111
228 0 $open_quote = 0;
229 0 next;
230 }
231  
232 =begin comment
233  
234 warn "invalid whitespace ! - check your file\n" if ( ( !$open_quote ) && $char =~ /\s/ ); #whitespace_guardian
235  
236 =end comment
237  
238 =cut
239  
240 # The main part
241 ▶ elsif ( !$open_quote && $char eq ':' ) {
Condition66%2/3
not $open_quote and $char eq ":"
!ll&&!rl&&r
01835158
MC/DC50%1/2
not $open_quote and $char eq ":"
not $open_quote $char eq ":"
Truth table66%2/3
not(A) and B
A= not $open_quoteB= $char eq ":"
ABresult
0X0
100
111
242  
243 # start of a new tag; add the previous word to the array, reset $word
244 158 push( @words, $word );
245 158 $word = ":";
246 }
247  
248 else {
249 1835 $word .= $char;
250 }
251  
252 }
253  
254 # This is a broken solution - works, but should be re-written
255 54 push( @words, $word );
256  
257 #
258 # 2. Split each word into a _tag_ and a _value_
259 #
260 54 for my $word (@words) {
261 212 my ( $tag, $value ) = $word =~ m/^:(.*?)=(.*$)/;
262 212 ▶ next if not defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 54 158 212
263 158 push( @{ $self->{'_tag_data'}->{$tag} }, $value );
264 }
265  
266 } # end of sub
267  
268 =begin comment
269  
270 Title : _is_nhx_command
271 Usage : $foo = _is_nhx_command($command)
272 Function: A utility (private) function that checks if a given string appears to be an NHX command
273 Returns : 1 if the string is an NHX command, 0 if not
274 Args : $comment - string representation of the comment/command
275  
276 =end comment
277  
278 =cut
279  
280 96 ▶sub _is_nhx_command {
Subroutine _is_nhx_command: called CC=1 SCAR=0.0
281 96 my ( $self, $comment ) = @_;
282 96 return $comment =~ m/^\s*&&NHX/i;
283 }
284  
285 =head2 contains_tag
286  
287 Title : contains_tag
288 Usage : $nhx_obj->_contains_tag($tag_name)
289 Function: Checks if a given tag exists
290 Returns : 1 if the tax exists, 0 if it doesn't
291 Args : $tag_name - a string representation of a tag
292  
293 =cut
294  
295 583 ▶sub contains_tag {
Subroutine contains_tag: called CC=1 SCAR=0.0
Pod documented
296 583 my ( $self, $tag_name ) = @_;
297 583 return defined( $self->{'_tag_data'}->{$tag_name} );
298 }
299  
300 =head2 get_tags
301  
302 Title : get_tags
303 Usage : $nhx_obj->get_tags();
304 Function: Reads and returns an array of tags
305 Returns : An array of tags
306 Args : None
307  
308 =cut
309  
310 37 ▶sub get_tags {
Subroutine get_tags: called CC=1 SCAR=0.0
Pod documented
311 37 my ($self) = @_;
312 37 return sort keys %{ $self->{_tag_data} };
313 }
314  
315 =head2 get_values
316  
317 Title : get_values
318 Usage : $nhx_obj->get_values($tag_name);
319 Function: Returns the list of values associated with the given tag ($tag_name)
320 Returns : Array of values
321 Args : $tag_name - a string representation of the tag
322  
323 =cut
324  
325 459 ▶sub get_values {
Subroutine get_values: called CC=3 SCAR=12.3
Pod documented
326 459 my ( $self, $tag_name ) = @_;
327 459 ▶ if ( not defined $tag_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag_name) 0 459 459
328 0 Bio::NEXUS::Util::Exceptions::BadArgs->throw(
329 'error' => "Required argument tag_name not defined"
330 );
331 }
332 459 ▶ if ( $self->contains_tag($tag_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->contains_tag($tag_name)) { } 459 0 459
333 459 return @{ $self->{_tag_data}->{$tag_name} };
334 }
335 else {
336 0 return undef;
337 }
338 }
339  
340 =head2 set_tag
341  
342 Title : set_tag
343 Usage : nhx_obj->set_tag($tag_name, $tag_reference);
344 Function: Updates the list of values associated with a given tag
345 Returns : Nothing
346 Args : $tag_name - a string, $tag_reference - an array-reference
347  
348 =cut
349  
350 89 ▶sub set_tag {
Subroutine set_tag: called CC=4 SCAR=17.2
Pod documented
351 89 my ( $self, $tag_name, $tag_values ) = @_;
352 89 ▶ if ( not defined $tag_name || not defined $tag_values ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag_name or not defined $tag_values) 0 89 89
Condition33%1/3
defined $tag_name or not defined $tag_values
l!l&&r!l&&!r
8900
MC/DC0%0/2
defined $tag_name or not defined $tag_values
defined $tag_name not defined $tag_values
Truth table33%1/3
A or not(B)
A= defined $tag_nameB= not defined $tag_values
ABresult
1X1
011
000
353 0 Bio::NEXUS::Util::Exceptions::BadArgs->throw(
354 'error' => "Required arguments tag_name and/or tag_values are not defined"
355 );
356 }
357 89 ▶ if ( not ref $tag_values eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $tag_values eq "ARRAY") 0 89 89
358 0 Bio::NEXUS::Util::Exceptions::BadArgs->throw(
359 'error' => "tag_values is not an array reference"
360 );
361 }
362  
363 #croak "no such tag: $tag_name\n" unless $self->contains_tag($tag_name);
364  
365 89 $self->{'_tag_data'}->{$tag_name} = $tag_values;
366  
367 }
368  
369 =head2 check_tag_value_present
370  
371 Title : check_tag_value
372 Usage : $boolean = nhx_obj->check_tag_value($tag_name, $value);
373 Function: check whether a particular value is present in a tag
374 Returns : 0 or 1 [ true or false]
375 Args : $tag_name - a string, $value - scalar (string or number)
376  
377 =cut
378  
379 4 ▶sub check_tag_value_present {
Subroutine check_tag_value_present: called CC=6 SCAR=19.0
Pod documented
380 4 my ( $self, $tag_name, $tag_value ) = @_;
381 4 ▶ if ( not defined $tag_name || not defined $tag_value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag_name or not defined $tag_value) 0 4 4
Condition33%1/3
defined $tag_name or not defined $tag_value
l!l&&r!l&&!r
400
MC/DC0%0/2
defined $tag_name or not defined $tag_value
defined $tag_name not defined $tag_value
Truth table33%1/3
A or not(B)
A= defined $tag_nameB= not defined $tag_value
ABresult
1X1
011
000
382 0 Bio::NEXUS::Util::Exceptions::BadArgs->throw(
383 'error' => "tag_name or tag_value is not defined"
384 );
385 }
386  
387 #croak "no such tag: $tag_name\n" unless $self->contains_tag($tag_name);
388 4 my $present = 0;
389 4 for my $value ( $self->get_values($tag_name) ) {
390 7 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 7 7
391 7 ▶ if ( $value eq $tag_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq $tag_value) 3 4 7
392 3 $present = 1;
393 3 last;
394 }
395 }
396 4 return $present;
397 }
398  
399 =head2 add_tag_value
400  
401 Title : add_tag_value
402 Usage : $nhx_obj->add_tag_value($tag_name, $tag_value);
403 Function: Adds a new tag/value set to the $nhx_obj;
404 Returns : 0 if not added or 1 if added [false or true]
405 Args : $tag_name - a string, $tag_value - a string
406  
407 =cut
408  
409 3 ▶sub add_tag_value {
Subroutine add_tag_value: called CC=5 SCAR=16.8
Pod documented
410 3 my ( $self, $tag_name, $tag_value ) = @_;
411 3 ▶ if ( not defined $tag_name || not defined $tag_value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag_name or not defined $tag_value) 0 3 3
Condition33%1/3
defined $tag_name or not defined $tag_value
l!l&&r!l&&!r
300
MC/DC0%0/2
defined $tag_name or not defined $tag_value
defined $tag_name not defined $tag_value
Truth table33%1/3
A or not(B)
A= defined $tag_nameB= not defined $tag_value
ABresult
1X1
011
000
412 0 Bio::NEXUS::Util::Exceptions::BadArgs->throw(
413 'error' => "tag_name or tag_value is not defined"
414 );
415 }
416  
417 #croak "no such tag: $tag_name\n" unless $self->contains_tag($tag_name);
418  
419 3 my $is_value_present =
420 $self->check_tag_value_present( $tag_name, $tag_value );
421 3 ▶ push @{ $self->{_tag_data}->{$tag_name} }, $tag_value
Branch: 1 branch
BranchTrueFalseTotal
unless $is_value_present 1 2 3
422 unless $is_value_present;
423 3 ▶ return $is_value_present ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$is_value_present ? : 2 1 3
424  
425 }
426  
427 =head2 delete_tag
428  
429 Title : delete_tag
430 Usage : $nhx_obj->delete_tag($tag_name);
431 Function: Removes a given tag (and the associated valus) from the $nhx_obj
432 Returns : Nothing
433 Args : $tag_name - a string representation of the tag
434  
435 =cut
436  
437 0 ▶sub delete_tag {
Subroutine delete_tag: not called CC=2 SCAR=17.9
Pod documented
438 0 my ( $self, $tag_name ) = @_;
439  
440 0 ▶ delete $self->{_tag_data}->{$tag_name} if defined $tag_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag_name 0 0 0
441  
442 }
443  
444 =head2 delete_all_tags
445  
446 Title : delete_all_tags
447 Usage : $nhx_obj->delete_all_tags();
448 Function: Removes all tags from $nhx_obj
449 Returns : Nothing
450 Args : None
451  
452 =cut
453  
454 0 ▶sub delete_all_tags {
Subroutine delete_all_tags: not called CC=1 SCAR=6.9
Pod documented
455 0 my ($self) = @_;
456  
457 0 $self->{'_tag_data'} = undef;
458 }
459  
460 35 1;
461  
« lib/Bio/NEXUS/AssumptionsBlock.pm ↑ Summary lib/Bio/NEXUS/TaxUnitSet.pm »