File Coverage

blib/lib/Bio/Phylo.pm
Criterion Covered Total %
statement 193 225 85.7
branch 43 76 56.5
condition 10 21 47.6
subroutine 37 39 94.8
pod 14 14 100.0
total 297 375 79.2


line stmt bran cond sub pod time code
1             package Bio::Phylo;
2 57     57   542725 use strict;
  57         144  
  57         1378  
3 57     57   243 use warnings;
  57         88  
  57         1149  
4 57     57   14334 use Bio::PhyloRole;
  57         157  
  57         2362  
5 57     57   1168 use base 'Bio::PhyloRole';
  57         100  
  57         5536  
6              
7             # don't use Scalar::Util::looks_like_number directly, use wrapped version
8 57     57   345 use Scalar::Util qw'weaken blessed';
  57         99  
  57         2527  
9 57     57   286 use Bio::Phylo::Util::CONSTANT '/looks_like/';
  57         99  
  57         6725  
10 57     57   345 use Bio::Phylo::Util::IDPool; # creates unique object IDs
  57         104  
  57         1213  
11 57     57   239 use Bio::Phylo::Util::Exceptions 'throw'; # defines exception classes and throws
  57         101  
  57         1964  
12 57     57   279 use Bio::Phylo::Util::Logger; # for logging, like log4perl/log4j
  57         113  
  57         1570  
13 57     57   14182 use Bio::Phylo::Util::MOP; # for traversing inheritance trees
  57         123  
  57         973  
14 57     57   319 use Bio::Phylo::Identifiable; # for storing unique IDs inside an instance
  57         120  
  57         2495  
15              
16             our ( $logger, $COMPAT ) = Bio::Phylo::Util::Logger->new;
17 57     57   15262 use version 0.77; our $VERSION = qv("v2.0.1");
  57         85766  
  57         459  
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             require Bio::Phylo::Mediators::TaxaMediator;
23              
24              
25             {
26             my $taxamediator = 'Bio::Phylo::Mediators::TaxaMediator';
27             my $mop = 'Bio::Phylo::Util::MOP';
28              
29             sub import {
30 556     556   1666 my $class = shift;
31 556 50       1633 if (@_) {
32 0         0 my %opt = looks_like_hash @_;
33 0         0 while ( my ( $key, $value ) = each %opt ) {
34 0 0       0 if ( $key =~ qr/^VERBOSE$/i ) {
    0          
35 0         0 $logger->VERBOSE( '-level' => $value, '-class' => $class );
36             }
37             elsif ( $key =~ qr/^COMPAT$/i ) {
38 0         0 $COMPAT = ucfirst( lc($value) );
39             }
40             else {
41 0         0 throw 'BadArgs' => "'$key' is not a valid argument for import";
42             }
43             }
44             }
45 556         31853 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             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             sub new : Constructor {
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 13384     13384 1 23281 my $class = shift;
145 13384 50       27166 if ( my $reference = ref $class ) {
146 0         0 $class = $reference;
147             }
148              
149             # happens only and exactly once because this
150             # root class is visited from every constructor
151 13384         33424 my $self = $class->SUPER::new();
152              
153             # register for get_obj_by_id
154 13384         27435 my $id = $self->get_id;
155 13384         36642 $objects{$id} = $self;
156 13384         39839 weaken( $objects{$id} );
157            
158             # notify user
159 13384         54948 $logger->info("constructor called for '$class' - $id");
160              
161             # processing arguments
162 13384 100 66     35280 if ( @_ and @_ = looks_like_hash @_ ) {
163 2950         7108 $logger->info("processing arguments");
164              
165             # process all arguments
166 2950         5740 ARG: while (@_) {
167 5804         8471 my $key = shift @_;
168 5804         7944 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 5804 50       9959 if ( $key eq '-verbose' ) {
178 0         0 $logger->VERBOSE(
179             '-level' => $value,
180             '-class' => $class,
181             );
182 0         0 next ARG;
183             }
184              
185             # notify user
186 5804         22133 $logger->debug("processing constructor arg '${key}' => '${value}'");
187              
188             # don't access data structures directly, call mutators
189             # in child classes or __PACKAGE__
190 5804         8603 my $mutator = $key;
191 5804         20247 $mutator =~ s/^-/set_/;
192              
193             # backward compat fixes:
194 5804         9139 $mutator =~ s/^set_pos$/set_position/;
195 5804         7142 $mutator =~ s/^set_matrix$/set_raw/;
196 5804         7487 eval { $self->$mutator($value); };
  5804         17111  
197 5804 50       16050 if ($@) {
198 0 0 0     0 if ( blessed $@ and $@->can('rethrow') ) {
    0 0        
199 0         0 $@->rethrow;
200             }
201             elsif ( not ref($@) and $@ =~ /^Can't locate object method / ) {
202 0         0 throw 'BadArgs' => "The named argument '${key}' cannot be passed to the constructor of ${class}";
203             }
204             else {
205 0         0 throw 'Generic' => $@;
206             }
207             }
208             }
209             }
210 13384         36560 $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 13384 100 66     89570 if ( ref $self ne 'Bio::Phylo::NeXML::Writable' && ! $self->isa('Bio::Phylo::Matrices::Datatype') ) {
219 12548         50038 $logger->info("going to register $self with $taxamediator");
220 12548         40246 $taxamediator->register($self);
221             }
222 13384         35316 $logger->info("done building object");
223 13384         31112 return $self;
224 57     57   38701 }
  57         748  
  57         977  
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             sub set_guid : Clonable {
249 109     109 1 144 my ( $self, $guid ) = @_;
250 109 50       138 if ( defined $guid ) {
251 0         0 $guid{ $self->get_id } = $guid;
252             }
253             else {
254 109         200 delete $guid{ $self->get_id };
255             }
256 109         170 return $self;
257 57     57   15791 }
  57         108  
  57         1796  
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             sub set_desc : Clonable {
274 111     111 1 152 my ( $self, $desc ) = @_;
275 111 100       161 if ( defined $desc ) {
276 2         8 $desc{ $self->get_id } = $desc;
277             }
278             else {
279 109         178 delete $desc{ $self->get_id };
280             }
281 111         186 return $self;
282 57     57   13793 }
  57         113  
  57         993  
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             sub set_score : Clonable {
300 109     109 1 156 my ( $self, $score ) = @_;
301              
302             # $score must be a number (or undefined)
303 109 50       164 if ( defined $score ) {
304 0 0       0 if ( !looks_like_number($score) ) {
305 0         0 throw 'BadNumber' => "score \"$score\" is a bad number";
306             }
307              
308             # notify user
309 0         0 $logger->info("setting score '$score'");
310 0         0 $score{ $self->get_id } = $score;
311             }
312             else {
313 109         285 $logger->info("unsetting score");
314 109         223 delete $score{ $self->get_id };
315             }
316              
317 109         194 return $self;
318 57     57   15921 }
  57         1545  
  57         213  
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             sub set_generic : Clonable {
343 2241     2241 1 3196 my $self = shift;
344              
345             # retrieve id just once, don't call $self->get_id in loops, inefficient
346 2241         4412 my $id = $self->get_id;
347              
348             # this initializes the hash if it didn't exist yet, or resets it if no args
349 2241 100 66     7759 if ( !defined $generic{$id} || !@_ ) {
350 1664         4629 $generic{$id} = {};
351             }
352              
353             # have args
354 2241 50       4454 if (@_) {
355 2241         2619 my %args;
356              
357             # have a single arg, a hash ref
358 2241 100 66     5332 if ( scalar @_ == 1 && looks_like_instance( $_[0], 'HASH' ) ) {
359 128         161 %args = %{ $_[0] };
  128         287  
360             }
361              
362             # multiple args, hopefully even size key/value pairs
363             else {
364 2113         5553 %args = looks_like_hash @_;
365             }
366              
367             # notify user
368 2241         8131 $logger->info("setting generic key/value pairs %{args}");
369              
370             # fill up the hash
371 2241         5300 for my $key ( keys %args ) {
372 2132         6440 $generic{$id}->{$key} = $args{$key};
373             }
374             }
375 2241         4885 return $self;
376 57     57   17525 }
  57         101  
  57         1741  
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     109 1 182 sub get_guid { $guid{ shift->get_id } }
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     111 1 195 sub get_desc { $desc{ shift->get_id } }
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     109 1 205 sub get_score { $score{ shift->get_id } }
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             sub get_generic {
452 3060     3060 1 4801 my ( $self, $key ) = @_;
453              
454             # retrieve just once
455 3060         5722 my $id = $self->get_id;
456              
457             # might not even have a generic hash yet, make one on-the-fly
458 3060 100       6888 if ( not defined $generic{$id} ) {
459 64         104 $generic{$id} = {};
460             }
461              
462             # have an argument
463 3060 100       4833 if ( defined $key ) {
464              
465 2951 50       5036 if ( ref($key) eq 'ARRAY' ) {
466 0         0 my @result = @generic{@$key};
467 0         0 return \@result;
468             }
469             else {
470             # notify user
471 2951         9348 $logger->debug("getting value for key '$key'");
472 2951         10224 return $generic{$id}->{$key};
473             }
474             }
475              
476             # no argument, wants whole hash
477             else {
478              
479             # notify user
480 109         279 $logger->debug("retrieving generic hash");
481 109         188 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             sub get_obj_by_id {
505 42     42 1 8934 my ( $class, $id ) = @_;
506 42         176 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     414 1 1340 sub get_logger { $logger }
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     9 1 400 sub VERSION { $VERSION }
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             sub clone {
554 109     109 1 184 my ( $self, $deep ) = @_;
555 109 100       184 $deep = 1 unless defined $deep;
556            
557             # compute and instantiate the constructor nearest to the tips of
558             # the inheritance tree
559 109         241 my $constructors = $mop->get_constructors($self); my $clone =
560 109         288 $constructors->[0]->{'code'}->(ref $self);
561              
562             # keep track of which methods we've done, including overrides
563 109         148 my %seen;
564            
565             # do the deep cloning first
566 109 100       184 if ( $deep ) {
567            
568             # get the deeply clonable methods
569 108         245 my $clonables = $mop->get_deep_clonables($self);
570 108         138 for my $setter ( @{ $clonables } ) {
  108         159  
571 115         175 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 50       206 if ( not $seen{$setter_name} ) {
576 115         171 $seen{$setter_name}++;
577            
578             # pass the output of the getter to the
579             # input of the setter
580 115         243 my $output = $self->_get_clonable_output($setter);
581 115         148 my $input;
582 115 100 66     389 if ( ref $output eq 'ARRAY' ) {
    100          
583             $input = [
584 123 100       307 map { ref $_ ? $_->clone($deep) : $_ }
585 30         44 @{ $output }
  30         58  
586             ];
587             }
588             elsif ( $output and ref $output ) {
589 56         146 $input = $output->clone($deep);
590             }
591 115         286 $setter->{'code'}->($clone,$input);
592             }
593             }
594             }
595            
596             # get the clonable methods
597 109         296 my $clonables = $mop->get_clonables($self);
598 109         130 for my $setter ( @{ $clonables } ) {
  109         166  
599 1852         2364 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 100       2760 if ( not $seen{$setter_name} ) {
604 1737         2201 $seen{$setter_name}++;
605 1737         2355 my $output = $self->_get_clonable_output($setter);
606 1737         3247 $setter->{'code'}->($clone,$output);
607             }
608             }
609 109         939 return $clone;
610             }
611            
612             sub _get_clonable_output {
613 1852     1852   2350 my ( $self, $setter ) = @_;
614 1852         2065 my $setter_name = $setter->{'name'};
615            
616             # assume getter/setter symmetry
617 1852         2055 my $getter_name = $setter_name;
618 1852         7199 $getter_name =~ s/^(_?)set_/$1get_/;
619 1852         3574 my $fqn = $setter->{'package'} . '::' . $getter_name;
620              
621             # get the code reference for the fully qualified name of the getter
622 1852         3433 my $getter = $mop->get_method($fqn);
623              
624             # pass the output of the getter to the input of the setter
625 1852         3025 my $output = $getter->($self);
626 1852         3259 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             sub DESTROY {
649 13384     13384   115526 my $self = shift;
650              
651             # delete from get_obj_by_id
652 13384         14529 my $id;
653 13384 50       22819 if ( defined( $id = $self->get_id ) ) {
654 13384         27255 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 13384         16225 my @classes = @{ $mop->get_classes($self) };
  13384         28704  
663 13384         20864 for my $class ( @classes ) {
664 124204         186905 my $cleanup = "${class}::_cleanup";
665 124204 100       390035 if ( $class->can($cleanup) ) {
666 87137         171223 $self->$cleanup;
667             }
668             }
669            
670             # unregister from mediator
671 13384         34466 $taxamediator->unregister( $self );
672              
673             # done cleaning up, id can be reclaimed
674 13384         25987 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             sub _cleanup : Destructor {
682 13384     13384   15618 my $self = shift;
683 13384         21905 my $id = $self->get_id;
684              
685             # cleanup local fields
686 13384 50       22623 if ( defined $id ) {
687 13384         18730 for my $field (@fields) {
688 93688         121487 delete $field->{$id};
689             }
690             }
691 57     57   49970 }
  57         110  
  57         224  
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     907   1767 sub _get_container { $container{ shift->get_id } }
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             sub _set_container {
731 19943     19943   31422 my ( $self, $container ) = @_;
732 19943         32780 my $id = $self->get_id;
733 19943 50       52450 if ( blessed $container ) {
734 19943 50       47655 if ( $container->can('can_contain') ) {
735 19943 50       36846 if ( $container->can_contain($self) ) {
736 19943 50       43416 if ( $container->contains($self) ) {
737 19943         39114 $container{$id} = $container;
738 19943         46636 weaken( $container{$id} );
739             }
740             else {
741 0         0 throw 'ObjectMismatch' => "'$self' not in '$container'";
742             }
743             }
744             else {
745 0         0 throw 'ObjectMismatch' =>
746             "'$container' cannot contain '$self'";
747             }
748             }
749             else {
750 0         0 throw 'ObjectMismatch' => "Invalid objects";
751             }
752             }
753             else {
754 0         0 delete $container{$id};
755             #throw 'BadArgs' => "Argument not an object";
756             }
757 19943         45844 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 0   0 1   sub to_js {JSON::to_json(shift->_json_data,{'pretty'=>1}) if looks_like_class 'JSON'}
775            
776             sub _json_data {
777 0     0     my $self = shift;
778 0           my %data = %{ $self->get_generic };
  0            
779 0 0         $data{'guid'} = $self->get_guid if $self->get_guid;
780 0 0         $data{'desc'} = $self->get_desc if $self->get_desc;
781 0 0         $data{'score'} = $self->get_score if $self->get_score;
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             1;