Phylo.pm

filter: Statementstmt 86.2% 201 / 233 Branchbran 56.2% 45 / 80 Conditioncond 47.6% 10 / 21 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 94.8% 37 / 39 Podpod 100% 14 / 14 totaltotal 77.8% 312 / 401 CC 53 SCAR 20.8
CC
53
Cov
77%
new
20.6
_json_data
20.0
import
16.2
CRAP 8.0
« Phylo/Treedrawer.pm ↑ Summary Phylo/Parsers/Phyloxml.pm »
1 package Bio::Phylo;
2 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 57 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 57 ▶use Bio::PhyloRole;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 57 ▶use base 'Bio::PhyloRole';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # don't use Scalar::Util::looks_like_number directly, use wrapped version
8 57 ▶use Scalar::Util qw'weaken blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 57 ▶use Bio::Phylo::Util::CONSTANT '/looks_like/';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 57 ▶use Bio::Phylo::Util::IDPool; # creates unique object IDs
Subroutine BEGIN: called CC=1 SCAR=0.0
11 57 ▶use Bio::Phylo::Util::Exceptions 'throw'; # defines exception classes and throws
Subroutine BEGIN: called CC=1 SCAR=0.0
12 57 ▶use Bio::Phylo::Util::Logger; # for logging, like log4perl/log4j
Subroutine BEGIN: called CC=1 SCAR=0.0
13 57 ▶use Bio::Phylo::Util::MOP; # for traversing inheritance trees
Subroutine BEGIN: called CC=1 SCAR=0.0
14 57 ▶use Bio::Phylo::Identifiable; # for storing unique IDs inside an instance
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 57 our ( $logger, $COMPAT ) = Bio::Phylo::Util::Logger->new;
17 57 ▶use version 0.77; our $VERSION = qv("v2.0.2");
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # mediates one-to-many relationships between taxon and nodes,
20 # taxon and sequences, taxa and forests, taxa and matrices.
21 # Read up on the Mediator design pattern to learn how this works.
22 57 require Bio::Phylo::Mediators::TaxaMediator;
23  
24  
25 {
26 57 my $taxamediator = 'Bio::Phylo::Mediators::TaxaMediator';
27 57 my $mop = 'Bio::Phylo::Util::MOP';
28  
29 556 ▶ sub import {
Subroutine import: called CC=5 SCAR=27.8
30 556 my $class = shift;
31 556 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 556 556
32 0 my %opt = looks_like_hash @_;
33 0 ▶ while ( my ( $key, $value ) = each %opt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %opt) 0 0 0
34 0 ▶ if ( $key =~ qr/^VERBOSE$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ qr/^VERBOSE$/i) { } 0 0 0
elsif ($key =~ qr/^COMPAT$/i) { } 0 0 0
35 0 $logger->VERBOSE( '-level' => $value, '-class' => $class );
36 }
37 elsif ( $key =~ qr/^COMPAT$/i ) {
38 0 $COMPAT = ucfirst( lc($value) );
39 }
40 else {
41 0 throw 'BadArgs' => "'$key' is not a valid argument for import";
42 }
43 }
44 }
45 556 return 1;
46 }
47  
48 # the following hashes are used to hold state of inside-out objects. For
49 # example, $obj->set_name("name") is implemented as $name{ $obj->get_id }
50 # = $name. To avoid memory leaks (and subtle bugs, should a new object by
51 # the same id appear (though that shouldn't happen)), the hash slots
52 # occupied by $obj->get_id need to be reclaimed in the destructor. This
53 # is done by recursively calling the $obj->_cleanup methods in all of $obj's
54 # superclasses. To make that method easier to write, we create an array
55 # with the local inside-out hashes here, so that we can just iterate over
56 # them anonymously during destruction cleanup. Other classes do something
57 # like this as well.
58 57 my @fields = \(
59 my (
60 %guid,
61 %desc,
62 %score,
63 %generic,
64 %cache,
65 %container, # XXX weak reference
66 %objects # XXX weak reference
67 )
68 );
69  
70 =head1 NAME
71  
72 Bio::Phylo - Phylogenetic analysis using perl
73  
74 =head1 SYNOPSIS
75  
76 # Actually, you would almost never use this module directly. This is
77 # the base class for other modules.
78 use Bio::Phylo;
79
80 # sets global verbosity to 'error'
81 Bio::Phylo->VERBOSE( -level => Bio::Phylo::Util::Logger::ERROR );
82
83 # sets verbosity for forest ojects to 'debug'
84 Bio::Phylo->VERBOSE(
85 -level => Bio::Phylo::Util::Logger::DEBUG,
86 -class => 'Bio::Phylo::Forest'
87 );
88
89 # prints version, including SVN revision number
90 print Bio::Phylo->VERSION;
91
92 # prints suggested citation
93 print Bio::Phylo->CITATION;
94  
95 =head1 DESCRIPTION
96  
97 This is the base class for the Bio::Phylo package for phylogenetic analysis using
98 object-oriented perl5. In this file, methods are defined that are performed by other
99 objects in the Bio::Phylo release that inherit from this base class (which you normally
100 wouldn't use directly).
101  
102 For general information on how to use Bio::Phylo, consult the manual
103 (L<Bio::Phylo::Manual>).
104  
105 If you come here because you are trying to debug a problem you run into in
106 using Bio::Phylo, you may be interested in the "exceptions" system as discussed
107 in L<Bio::Phylo::Util::Exceptions>. In addition, you may find the logging system
108 in L<Bio::Phylo::Util::Logger> of use to localize problems.
109  
110 =head1 METHODS
111  
112 =head2 CONSTRUCTOR
113  
114 =over
115  
116 =item new()
117  
118 The Bio::Phylo root constructor is rarely used directly. Rather, many other
119 objects in Bio::Phylo internally go up the inheritance tree to this constructor.
120 The arguments shown here can therefore also be passed to any of the child
121 classes' constructors, which will pass them on up the inheritance tree. Generally,
122 constructors in Bio::Phylo subclasses can process as arguments all methods that
123 have set_* in their names. The arguments are named for the methods, but "set_"
124 has been replaced with a dash "-", e.g. the method "set_name" becomes the
125 argument "-name" in the constructor.
126  
127 Type : Constructor
128 Title : new
129 Usage : my $phylo = Bio::Phylo->new;
130 Function: Instantiates Bio::Phylo object
131 Returns : a Bio::Phylo object
132 Args : Optional, any number of setters. For example,
133 Bio::Phylo->new( -name => $name )
134 will call set_name( $name ) internally
135  
136 =cut
137  
138 12924 ▶ sub new : Constructor {
Subroutine new: called CC=13 SCAR=30.2
Pod documented
139  
140 # $class could be a child class, called from $class->SUPER::new(@_)
141 # or an object, e.g. $node->new(%args) in which case we create a new
142 # object that's bless into the same class as the invocant. No, that's
143 # not the same thing as a clone.
144 12924 my $class = shift;
145 12924 ▶ if ( my $reference = ref $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reference = ref $class) 0 12924 12924
146 0 $class = $reference;
147 }
148  
149 # happens only and exactly once because this
150 # root class is visited from every constructor
151 12924 my $self = $class->SUPER::new();
152  
153 # register for get_obj_by_id
154 12924 my $id = $self->get_id;
155 12924 $objects{$id} = $self;
156 12924 weaken( $objects{$id} );
157
158 # notify user
159 12924 $logger->info("constructor called for '$class' - $id");
160  
161 # processing arguments
162 12924 ▶ if ( @_ and @_ = looks_like_hash @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and @_ = &looks_like_hash(@_)) 2945 9979 12924
Condition66%2/3
@_ and @_ = &looks_like_hash(@_)
!ll&&!rl&&r
997902945
MC/DC50%1/2
@_ and @_ = &looks_like_hash(@_)
@_ @_ = &looks_like_hash(@_)
Truth table66%2/3
A and B
A= @_B= @_ = &looks_like_hash(@_)
ABresult
0X0
100
111
163 2945 $logger->info("processing arguments");
164  
165 # process all arguments
166 2945 ▶ ARG: while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5799 2945 8744
167 5799 my $key = shift @_;
168 5799 my $value = shift @_;
169  
170 # this is a bioperl arg, meant to set
171 # verbosity at a per class basis. In
172 # bioperl, the $verbose argument is
173 # subsequently carried around in that
174 # class, here we delegate that to the
175 # logger, which has roughly the same
176 # effect.
177 5799 ▶ if ( $key eq '-verbose' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "-verbose") 0 5799 5799
178 0 $logger->VERBOSE(
179 '-level' => $value,
180 '-class' => $class,
181 );
182 0 next ARG;
183 }
184  
185 # notify user
186 5799 $logger->debug("processing constructor arg '${key}' => '${value}'");
187  
188 # don't access data structures directly, call mutators
189 # in child classes or __PACKAGE__
190 5799 my $mutator = $key;
191 5799 $mutator =~ s/^-/set_/;
192  
193 # backward compat fixes:
194 5799 $mutator =~ s/^set_pos$/set_position/;
195 5799 $mutator =~ s/^set_matrix$/set_raw/;
196 5799 eval { $self->$mutator($value); };
197 5799 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5799 5799
198 0 ▶ if ( blessed $@ and $@->can('rethrow') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($@) and $@->can('rethrow')) { } 0 0 0
elsif (not ref $@ and $@ =~ /^Can't locate object method /) { } 0 0 0
Condition0%0/3
builtin::blessed($@) and $@->can("rethrow")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($@) and $@->can("rethrow")
builtin::blessed($@) $@->can("rethrow")
Truth table0%0/3
A and B
A= builtin::blessed($@)B= $@->can("rethrow")
ABresult
0X0
100
111
199 0 $@->rethrow;
200 }
201 ▶ elsif ( not ref($@) and $@ =~ /^Can't locate object method / ) {
Condition0%0/3
not ref $@ and $@ =~ /^Can't locate object method /
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $@ and $@ =~ /^Can't locate object method /
not ref $@ $@ =~ /^Can't locate object method /
Truth table0%0/3
not(A) and B
A= not ref $@B= $@ =~ /^Can't locate object method /
ABresult
0X0
100
111
202 0 throw 'BadArgs' => "The named argument '${key}' cannot be passed to the constructor of ${class}";
203 }
204 else {
205 0 throw 'Generic' => $@;
206 }
207 }
208 }
209 }
210 12924 $logger->info("done processing constructor arguments");
211  
212 # register with mediator
213 # TODO this is irrelevant for some child classes,
214 # so should be re-factored into somewhere nearer the
215 # tips of the inheritance tree. The hack where we
216 # skip over direct instances of Writable is so that
217 # we don't register things like <format> and <matrix> tags
218 12924 ▶ if ( ref $self ne 'Bio::Phylo::NeXML::Writable' && ! $self->isa('Bio::Phylo::Matrices::Datatype') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne "Bio::Phylo::NeXML::Writable" and not $self->isa("Bio::Phylo::Matrices::Datatype")) 12088 836 12924
Condition66%2/3
ref $self ne "Bio::Phylo::NeXML::Writable" and not $self->isa("Bio::Phylo::Matrices::Datatype")
!ll&&!rl&&r
083612088
MC/DC50%1/2
ref $self ne "Bio::Phylo::NeXML::Writable" and not $self->isa("Bio::Phylo::Matrices::Datatype")
ref $self ne "Bio::Phylo::NeXML::Writable" not $self->isa("Bio::Phylo::Matrices::Datatype")
Truth table66%2/3
A and not(B)
A= ref $self ne "Bio::Phylo::NeXML::Writable"B= not $self->isa("Bio::Phylo::Matrices::Datatype")
ABresult
0X0
100
111
219 12088 $logger->info("going to register $self with $taxamediator");
220 12088 $taxamediator->register($self);
221 }
222 12924 $logger->info("done building object");
223 12924 return $self;
224 57 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
225  
226 =back
227  
228 =head2 MUTATORS
229  
230 =over
231  
232 =item set_guid()
233  
234 Sets invocant GUID.
235  
236 Type : Mutator
237 Title : set_guid
238 Usage : $obj->set_guid($guid);
239 Function: Assigns an object's GUID.
240 Returns : Modified object.
241 Args : A scalar
242 Notes : This field can be used for storing an identifier that is
243 unambiguous within a given content. For example, an LSID,
244 a genbank accession number, etc.
245  
246 =cut
247  
248 109 ▶ sub set_guid : Clonable {
Subroutine set_guid: called CC=2 SCAR=7.4
Pod documented
249 109 my ( $self, $guid ) = @_;
250 109 ▶ if ( defined $guid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $guid) { } 0 109 109
251 0 $guid{ $self->get_id } = $guid;
252 }
253 else {
254 109 delete $guid{ $self->get_id };
255 }
256 109 return $self;
257 57 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
258  
259  
260 =item set_desc()
261  
262 Sets invocant description.
263  
264 Type : Mutator
265 Title : set_desc
266 Usage : $obj->set_desc($desc);
267 Function: Assigns an object's description.
268 Returns : Modified object.
269 Args : Argument must be a string.
270  
271 =cut
272  
273 111 ▶ sub set_desc : Clonable {
Subroutine set_desc: called CC=2 SCAR=6.9
Pod documented
274 111 my ( $self, $desc ) = @_;
275 111 ▶ if ( defined $desc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desc) { } 2 109 111
276 2 $desc{ $self->get_id } = $desc;
277 }
278 else {
279 109 delete $desc{ $self->get_id };
280 }
281 111 return $self;
282 57 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
283  
284 =item set_score()
285  
286 Sets invocant score.
287  
288 Type : Mutator
289 Title : set_score
290 Usage : $obj->set_score($score);
291 Function: Assigns an object's numerical score.
292 Returns : Modified object.
293 Args : Argument must be any of
294 perl's number formats, or undefined
295 to reset score.
296  
297 =cut
298  
299 109 ▶ sub set_score : Clonable {
Subroutine set_score: called CC=3 SCAR=14.8
Pod documented
300 109 my ( $self, $score ) = @_;
301  
302 # $score must be a number (or undefined)
303 109 ▶ if ( defined $score ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $score) { } 0 109 109
304 0 ▶ if ( !looks_like_number($score) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($score)) 0 0 0
305 0 throw 'BadNumber' => "score \"$score\" is a bad number";
306 }
307  
308 # notify user
309 0 $logger->info("setting score '$score'");
310 0 $score{ $self->get_id } = $score;
311 }
312 else {
313 109 $logger->info("unsetting score");
314 109 delete $score{ $self->get_id };
315 }
316  
317 109 return $self;
318 57 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
319  
320 =item set_generic()
321  
322 Sets generic key/value pair(s).
323  
324 Type : Mutator
325 Title : set_generic
326 Usage : $obj->set_generic( %generic );
327 Function: Assigns generic key/value pairs to the invocant.
328 Returns : Modified object.
329 Args : Valid arguments constitute:
330  
331 * key/value pairs, for example:
332 $obj->set_generic( '-lnl' => 0.87565 );
333  
334 * or a hash ref, for example:
335 $obj->set_generic( { '-lnl' => 0.87565 } );
336  
337 * or nothing, to reset the stored hash, e.g.
338 $obj->set_generic( );
339  
340 =cut
341  
342 2188 ▶ sub set_generic : Clonable {
Subroutine set_generic: called CC=7 SCAR=19.6
Pod documented
343 2188 my $self = shift;
344  
345 # retrieve id just once, don't call $self->get_id in loops, inefficient
346 2188 my $id = $self->get_id;
347  
348 # this initializes the hash if it didn't exist yet, or resets it if no args
349 2188 ▶ if ( !defined $generic{$id} || !@_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $generic{$id} and @_) 1618 570 2188
Condition66%2/3
defined $generic{$id} and @_
!ll&&!rl&&r
16180570
MC/DC50%1/2
defined $generic{$id} and @_
defined $generic{$id} @_
Truth table66%2/3
A and B
A= defined $generic{$id}B= @_
ABresult
0X0
100
111
350 1618 $generic{$id} = {};
351 }
352  
353 # have args
354 2188 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2188 0 2188
355 2188 my %args;
356  
357 # have a single arg, a hash ref
358 2188 ▶ if ( scalar @_ == 1 && looks_like_instance( $_[0], 'HASH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and &looks_like_instance($_[0], 'HASH')) { } 128 2060 2188
Condition66%2/3
scalar @_ == 1 and &looks_like_instance($_[0], "HASH")
!ll&&!rl&&r
20600128
MC/DC50%1/2
scalar @_ == 1 and &looks_like_instance($_[0], "HASH")
scalar @_ == 1 &looks_like_instance($_[0], "HASH")
Truth table66%2/3
A and B
A= scalar @_ == 1B= &looks_like_instance($_[0], "HASH")
ABresult
0X0
100
111
359 128 %args = %{ $_[0] };
360 }
361  
362 # multiple args, hopefully even size key/value pairs
363 else {
364 2060 %args = looks_like_hash @_;
365 }
366  
367 # notify user
368 2188 $logger->info("setting generic key/value pairs %{args}");
369  
370 # fill up the hash
371 2188 for my $key ( keys %args ) {
372 2079 $generic{$id}->{$key} = $args{$key};
373 }
374 }
375 2188 return $self;
376 57 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
377  
378 =back
379  
380 =head2 ACCESSORS
381  
382 =over
383  
384 =item get_guid()
385  
386 Gets invocant GUID.
387  
388 Type : Accessor
389 Title : get_guid
390 Usage : my $guid = $obj->get_guid;
391 Function: Assigns an object's GUID.
392 Returns : Scalar.
393 Args : None
394 Notes : This field can be used for storing an identifier that is
395 unambiguous within a given content. For example, an LSID,
396 a genbank accession number, etc.
397  
398 =cut
399  
400 109 ▶ sub get_guid { $guid{ shift->get_id } }
Subroutine get_guid: called CC=1 SCAR=0.0
Pod documented
401  
402 =item get_desc()
403  
404 Gets invocant description.
405  
406 Type : Accessor
407 Title : get_desc
408 Usage : my $desc = $obj->get_desc;
409 Function: Returns the object's description (if any).
410 Returns : A string
411 Args : None
412  
413 =cut
414  
415 111 ▶ sub get_desc { $desc{ shift->get_id } }
Subroutine get_desc: called CC=1 SCAR=0.0
Pod documented
416  
417 =item get_score()
418  
419 Gets invocant's score.
420  
421 Type : Accessor
422 Title : get_score
423 Usage : my $score = $obj->get_score;
424 Function: Returns the object's numerical score (if any).
425 Returns : A number
426 Args : None
427  
428 =cut
429  
430 109 ▶ sub get_score { $score{ shift->get_id } }
Subroutine get_score: called CC=1 SCAR=0.0
Pod documented
431  
432 =item get_generic()
433  
434 Gets generic hashref or hash value(s).
435  
436 Type : Accessor
437 Title : get_generic
438 Usage : my $value = $obj->get_generic($key);
439 or
440 my %hash = %{ $obj->get_generic() };
441 Function: Returns the object's generic data. If an
442 argument is used, it is considered a key
443 for which the associated value is returned.
444 Without arguments, a reference to the whole
445 hash is returned.
446 Returns : A value or an array reference of values
447 Args : A key (string) or an array reference of keys
448  
449 =cut
450  
451 2991 ▶ sub get_generic {
Subroutine get_generic: called CC=4 SCAR=14.0
Pod documented
452 2991 my ( $self, $key ) = @_;
453  
454 # retrieve just once
455 2991 my $id = $self->get_id;
456  
457 # might not even have a generic hash yet, make one on-the-fly
458 2991 ▶ if ( not defined $generic{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $generic{$id}) 64 2927 2991
459 64 $generic{$id} = {};
460 }
461  
462 # have an argument
463 2991 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 2882 109 2991
464  
465 2882 ▶ if ( ref($key) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key eq 'ARRAY') { } 0 2882 2882
466 0 my @result = @generic{@$key};
467 0 return \@result;
468 }
469 else {
470 # notify user
471 2882 $logger->debug("getting value for key '$key'");
472 2882 return $generic{$id}->{$key};
473 }
474 }
475  
476 # no argument, wants whole hash
477 else {
478  
479 # notify user
480 109 $logger->debug("retrieving generic hash");
481 109 return $generic{$id};
482 }
483 }
484  
485 =back
486  
487 =head2 PACKAGE METHODS
488  
489 =over
490  
491 =item get_obj_by_id()
492  
493 Attempts to fetch an in-memory object by its UID
494  
495 Type : Accessor
496 Title : get_obj_by_id
497 Usage : my $obj = Bio::Phylo->get_obj_by_id($uid);
498 Function: Fetches an object from the IDPool cache
499 Returns : A Bio::Phylo object
500 Args : A unique id
501  
502 =cut
503  
504 42 ▶ sub get_obj_by_id {
Subroutine get_obj_by_id: called CC=1 SCAR=0.0
Pod documented
505 42 my ( $class, $id ) = @_;
506 42 return $objects{$id};
507 }
508  
509 =item get_logger()
510  
511 Returns a singleton reference to a Bio::Phylo::Util::Logger object
512  
513 Type : Accessor
514 Title : get_logger
515 Usage : my $logger = Bio::Phylo->get_logger
516 Function: Returns logger
517 Returns : A Bio::Phylo::Util::Logger object
518 Args : None
519  
520 =cut
521
522 414 ▶ sub get_logger { $logger }
Subroutine get_logger: called CC=1 SCAR=0.0
Pod documented
523  
524 =item VERSION()
525  
526 Returns the $VERSION string of this Bio::Phylo release
527  
528 Type : Accessor
529 Title : VERSION
530 Usage : my $version = Bio::Phylo->VERSION
531 Function: Returns version string
532 Returns : A string
533 Args : None
534  
535 =cut
536
537 9 ▶ sub VERSION { $VERSION }
Subroutine VERSION: called CC=1 SCAR=0.0
Pod documented
538  
539 =item clone()
540  
541 Clones invocant.
542  
543 Type : Utility method
544 Title : clone
545 Usage : my $clone = $object->clone;
546 Function: Creates a copy of the invocant object.
547 Returns : A copy of the invocant.
548 Args : None.
549 Comments: Cloning is currently experimental, use with caution.
550  
551 =cut
552  
553 109 ▶ sub clone {
Subroutine clone: called CC=11 SCAR=24.0
Pod documented
554 109 my ( $self, $deep ) = @_;
555 109 ▶ $deep = 1 unless defined $deep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $deep 6 103 109
556
557 # compute and instantiate the constructor nearest to the tips of
558 # the inheritance tree
559 109 my $constructors = $mop->get_constructors($self); my $clone =
560 109 $constructors->[0]->{'code'}->(ref $self);
561  
562 # keep track of which methods we've done, including overrides
563 109 my %seen;
564
565 # do the deep cloning first
566 109 ▶ if ( $deep ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep) 108 1 109
567
568 # get the deeply clonable methods
569 108 my $clonables = $mop->get_deep_clonables($self);
570 108 for my $setter ( @{ $clonables } ) {
571 115 my $setter_name = $setter->{'name'};
572
573 # only do this for the shallowest method with
574 # the same name: the others are overrided
575 115 ▶ if ( not $seen{$setter_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$setter_name}) 115 0 115
576 115 $seen{$setter_name}++;
577
578 # pass the output of the getter to the
579 # input of the setter
580 115 my $output = $self->_get_clonable_output($setter);
581 115 my $input;
582 115 ▶ if ( ref $output eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $output eq 'ARRAY') { } 30 85 115
elsif ($output and ref $output) { } 56 58 114
583 $input = [
584 123 ▶ map { ref $_ ? $_->clone($deep) : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 43 80 123
585 30 @{ $output }
586 ];
587 }
588 ▶ elsif ( $output and ref $output ) {
Condition66%2/3
$output and ref $output
!ll&&!rl&&r
29056
MC/DC50%1/2
$output and ref $output
$output ref $output
Truth table66%2/3
A and B
A= $outputB= ref $output
ABresult
0X0
100
111
589 56 $input = $output->clone($deep);
590 }
591 115 $setter->{'code'}->($clone,$input);
592 }
593 }
594 }
595
596 # get the clonable methods
597 109 my $clonables = $mop->get_clonables($self);
598 109 for my $setter ( @{ $clonables } ) {
599 1852 my $setter_name = $setter->{'name'};
600
601 # only do this for the shallowest method with the
602 # same name: the others are overrided
603 1852 ▶ if ( not $seen{$setter_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$setter_name}) 1737 115 1852
604 1737 $seen{$setter_name}++;
605 1737 my $output = $self->_get_clonable_output($setter);
606 1737 $setter->{'code'}->($clone,$output);
607 }
608 }
609 109 return $clone;
610 }
611
612 1852 ▶ sub _get_clonable_output {
Subroutine _get_clonable_output: called CC=1 SCAR=0.0
613 1852 my ( $self, $setter ) = @_;
614 1852 my $setter_name = $setter->{'name'};
615
616 # assume getter/setter symmetry
617 1852 my $getter_name = $setter_name;
618 1852 $getter_name =~ s/^(_?)set_/$1get_/;
619 1852 my $fqn = $setter->{'package'} . '::' . $getter_name;
620  
621 # get the code reference for the fully qualified name of the getter
622 1852 my $getter = $mop->get_method($fqn);
623  
624 # pass the output of the getter to the input of the setter
625 1852 my $output = $getter->($self);
626 1852 return $output;
627 }
628  
629 =begin comment
630  
631 Invocant destructor.
632  
633 Type : Destructor
634 Title : DESTROY
635 Usage : $phylo->DESTROY
636 Function: Destroys Phylo object
637 Alias :
638 Returns : TRUE
639 Args : none
640 Comments: You don't really need this,
641 it is called automatically when
642 the object goes out of scope.
643  
644 =end comment
645  
646 =cut
647  
648 12924 ▶ sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=13.9
649 12924 my $self = shift;
650  
651 # delete from get_obj_by_id
652 12924 my $id;
653 12924 ▶ if ( defined( $id = $self->get_id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($id = $self->get_id)) 12924 0 12924
654 12924 delete $objects{$id};
655 }
656  
657 # do the cleanups
658 # my @destructors = @{ $mop->get_destructors( $self ) };
659 # for my $d ( @destructors ) {
660 # $d->{'code'}->( $self );
661 # }
662 12924 my @classes = @{ $mop->get_classes($self) };
663 12924 for my $class ( @classes ) {
664 119609 my $cleanup = "${class}::_cleanup";
665 119609 ▶ if ( $class->can($cleanup) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can($cleanup)) 83917 35692 119609
666 83917 $self->$cleanup;
667 }
668 }
669
670 # unregister from mediator
671 12924 $taxamediator->unregister( $self );
672  
673 # done cleaning up, id can be reclaimed
674 12924 Bio::Phylo::Util::IDPool->_reclaim( $self );
675 }
676  
677  
678 # child classes probably should have a method like this,
679 # if their objects hold internal state anyway (b/c they'll
680 # be inside-out objects).
681 12924 ▶ sub _cleanup : Destructor {
Subroutine _cleanup: called CC=3 SCAR=11.1
682 12924 my $self = shift;
683 12924 my $id = $self->get_id;
684  
685 # cleanup local fields
686 12924 ▶ if ( defined $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 12924 0 12924
687 12924 for my $field (@fields) {
688 90468 delete $field->{$id};
689 }
690 }
691 57 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
692  
693 =begin comment
694  
695 Type : Internal method
696 Title : _get_container
697 Usage : $phylo->_get_container;
698 Function: Retrieves the object that contains the invocant (e.g. for a node,
699 returns the tree it is in).
700 Returns : Bio::Phylo::* object
701 Args : None
702  
703 =end comment
704  
705 =cut
706  
707 # this is the converse of $listable->get_entities, i.e.
708 # every entity in a listable object holds a reference
709 # to its container. We actually use this surprisingly
710 # rarely, and because I read somewhere (heh) it's bad
711 # to have the objects of a has-a relationship fiddle with
712 # their container we hide this method from abuse. Then
713 # again, sometimes it's handy ;-)
714 907 ▶ sub _get_container { $container{ shift->get_id } }
Subroutine _get_container: called CC=1 SCAR=0.0
715  
716 =begin comment
717  
718 Type : Internal method
719 Title : _set_container
720 Usage : $phylo->_set_container($obj);
721 Function: Creates a reference from the invocant to the object that contains
722 it (e.g. for a node, creates a reference to the tree it is in).
723 Returns : Bio::Phylo::* object
724 Args : A Bio::Phylo::Listable object
725  
726 =end comment
727  
728 =cut
729  
730 19078 ▶ sub _set_container {
Subroutine _set_container: called CC=5 SCAR=18.5
731 19078 my ( $self, $container ) = @_;
732 19078 my $id = $self->get_id;
733 19078 ▶ if ( blessed $container ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($container)) { } 19078 0 19078
734 19078 ▶ if ( $container->can('can_contain') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($container->can('can_contain')) { } 19078 0 19078
735 19078 ▶ if ( $container->can_contain($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($container->can_contain($self)) { } 19078 0 19078
736 19078 ▶ if ( $container->contains($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($container->contains($self)) { } 19078 0 19078
737 19078 $container{$id} = $container;
738 19078 weaken( $container{$id} );
739 }
740 else {
741 0 throw 'ObjectMismatch' => "'$self' not in '$container'";
742 }
743 }
744 else {
745 0 throw 'ObjectMismatch' =>
746 "'$container' cannot contain '$self'";
747 }
748 }
749 else {
750 0 throw 'ObjectMismatch' => "Invalid objects";
751 }
752 }
753 else {
754 0 delete $container{$id};
755 #throw 'BadArgs' => "Argument not an object";
756 }
757 19078 return $self;
758 }
759
760 =item to_js()
761  
762 Serializes to simple JSON. For a conversion to NeXML/JSON, use C<to_json>.
763  
764 Type : Serializer
765 Title : to_js
766 Usage : my $json = $object->to_js;
767 Function: Serializes to JSON
768 Returns : A JSON string
769 Args : None.
770 Comments:
771  
772 =cut
773  
774 0 ▶ sub to_js {JSON::to_json(shift->_json_data,{'pretty'=>1}) if looks_like_class 'JSON'}
Subroutine to_js: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_class("JSON") 0 0 0
775
776 0 ▶ sub _json_data {
Subroutine _json_data: not called CC=4 SCAR=30.0
777 0 my $self = shift;
778 0 my %data = %{ $self->get_generic };
779 0 ▶ $data{'guid'} = $self->get_guid if $self->get_guid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_guid 0 0 0
780 0 ▶ $data{'desc'} = $self->get_desc if $self->get_desc;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_desc 0 0 0
781 0 ▶ $data{'score'} = $self->get_score if $self->get_score;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_score 0 0 0
782 0 return \%data;
783 }
784  
785 =back
786  
787 =head1 SEE ALSO
788  
789 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
790 for any user or developer questions and discussions.
791  
792 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>
793  
794 =head1 CITATION
795  
796 If you use Bio::Phylo in published research, please cite it:
797  
798 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
799 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
800 I<BMC Bioinformatics> B<12>:63.
801 L<http://dx.doi.org/10.1186/1471-2105-12-63>
802  
803 =cut
804  
805 }
806 57 1;
« Phylo/Treedrawer.pm ↑ Summary Phylo/Parsers/Phyloxml.pm »