Phylo/Taxa.pm

filter: Statementstmt 52.5% 63 / 120 Branchbran 13.6% 3 / 22 Conditioncond 50.0% 6 / 12 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 61.9% 13 / 21 Podpod 100% 11 / 11 totaltotal 50.2% 96 / 191 CC 28 SCAR 28.4
CC
28
Cov
47%
merge_by_name
56.0
prune_taxa
42.0
merge_by_meta
9.1
CRAP 17.1
« Phylo/Treedrawer/Canvas.pm ↑ Summary Phylo/ListableRole.pm »
1 package Bio::Phylo::Taxa;
2 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use base 'Bio::Phylo::Listable';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Bio::Phylo::Util::CONSTANT qw':objecttypes /looks_like/ :namespaces';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Bio::Phylo::Util::Exceptions 'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Bio::Phylo::Mediators::TaxaMediator;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Bio::Phylo::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =begin comment
11  
12 This class has no internal state, no cleanup is necessary.
13  
14 =end comment
15  
16 =cut
17  
18 {
19 15 my $logger = __PACKAGE__->get_logger;
20 15 my $mediator = 'Bio::Phylo::Mediators::TaxaMediator';
21 15 my $factory = Bio::Phylo::Factory->new;
22 15 my $CONTAINER = _PROJECT_;
23 15 my $TYPE = _TAXA_;
24 15 my $MATRIX = _MATRIX_;
25 15 my $FOREST = _FOREST_;
26  
27 =head1 NAME
28  
29 Bio::Phylo::Taxa - Container of taxon objects
30  
31 =head1 SYNOPSIS
32  
33 use Bio::Phylo::Factory;
34 my $fac = Bio::Phylo::Factory->new;
35  
36 # A mesquite-style default
37 # taxa block for 10 taxa.
38 my $taxa = $fac->create_taxa;
39 for my $i ( 1 .. 10 ) {
40 $taxa->insert( $fac->create_taxon( '-name' => "taxon_${i}" ) );
41 }
42
43 # prints a taxa block in nexus format
44 print $taxa->to_nexus;
45  
46 =head1 DESCRIPTION
47  
48 The Bio::Phylo::Taxa object models a set of operational taxonomic units. The
49 object subclasses the Bio::Phylo::Listable object, and so the filtering
50 methods of that class are available.
51  
52 A taxa object can link to multiple forest and matrix objects.
53  
54 =head1 METHODS
55  
56 =head2 CONSTRUCTOR
57  
58 =over
59  
60 =item new()
61  
62 Taxa constructor.
63  
64 Type : Constructor
65 Title : new
66 Usage : my $taxa = Bio::Phylo::Taxa->new;
67 Function: Instantiates a Bio::Phylo::Taxa object.
68 Returns : A Bio::Phylo::Taxa object.
69 Args : none.
70  
71 =cut
72  
73 # sub new {
74 # # could be child class
75 # my $class = shift;
76 #
77 # # notify user
78 # $logger->info("constructor called for '$class'");
79 #
80 # # recurse up inheritance tree, get ID
81 # my $self = $class->SUPER::new( '-tag' => __PACKAGE__->_tag, @_ );
82 #
83 # # local fields would be set here
84 #
85 # return $self;
86 # }
87  
88 =back
89  
90 =head2 MUTATORS
91  
92 =over
93  
94 =item set_forest()
95  
96 Sets associated Bio::Phylo::Forest object.
97  
98 Type : Mutator
99 Title : set_forest
100 Usage : $taxa->set_forest( $forest );
101 Function: Associates forest with the
102 invocant taxa object (i.e.
103 creates reference).
104 Returns : Modified object.
105 Args : A Bio::Phylo::Forest object
106 Comments: A taxa object can link to multiple
107 forest and matrix objects.
108  
109 =cut
110  
111 0 ▶ sub set_forest {
Subroutine set_forest: not called CC=2 SCAR=17.9
Pod documented
112 0 my ( $self, $forest ) = @_;
113 0 $logger->debug("setting forest $forest");
114 0 ▶ if ( looks_like_object $forest, $FOREST ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_object($forest, $FOREST)) 0 0 0
115 0 $forest->set_taxa($self);
116 }
117 0 return $self;
118 }
119  
120 =item set_matrix()
121  
122 Sets associated Bio::Phylo::Matrices::Matrix object.
123  
124 Type : Mutator
125 Title : set_matrix
126 Usage : $taxa->set_matrix($matrix);
127 Function: Associates matrix with the
128 invocant taxa object (i.e.
129 creates reference).
130 Returns : Modified object.
131 Args : A Bio::Phylo::Matrices::Matrix object
132 Comments: A taxa object can link to multiple
133 forest and matrix objects.
134  
135 =cut
136  
137 0 ▶ sub set_matrix {
Subroutine set_matrix: not called CC=2 SCAR=17.9
Pod documented
138 0 my ( $self, $matrix ) = @_;
139 0 $logger->debug("setting matrix $matrix");
140 0 ▶ if ( looks_like_object $matrix, $MATRIX ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_object($matrix, $MATRIX)) 0 0 0
141 0 $matrix->set_taxa($self);
142 }
143 0 return $self;
144 }
145  
146 =item unset_forest()
147  
148 Removes association with argument Bio::Phylo::Forest object.
149  
150 Type : Mutator
151 Title : unset_forest
152 Usage : $taxa->unset_forest($forest);
153 Function: Disassociates forest from the
154 invocant taxa object (i.e.
155 removes reference).
156 Returns : Modified object.
157 Args : A Bio::Phylo::Forest object
158  
159 =cut
160  
161 0 ▶ sub unset_forest {
Subroutine unset_forest: not called CC=2 SCAR=17.9
Pod documented
162 0 my ( $self, $forest ) = @_;
163 0 $logger->debug("unsetting forest $forest");
164 0 ▶ if ( looks_like_object $forest, $FOREST ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_object($forest, $FOREST)) 0 0 0
165 0 $forest->unset_taxa();
166 }
167 0 return $self;
168 }
169  
170 =item unset_matrix()
171  
172 Removes association with Bio::Phylo::Matrices::Matrix object.
173  
174 Type : Mutator
175 Title : unset_matrix
176 Usage : $taxa->unset_matrix($matrix);
177 Function: Disassociates matrix from the
178 invocant taxa object (i.e.
179 removes reference).
180 Returns : Modified object.
181 Args : A Bio::Phylo::Matrices::Matrix object
182  
183 =cut
184  
185 0 ▶ sub unset_matrix {
Subroutine unset_matrix: not called CC=2 SCAR=17.9
Pod documented
186 0 my ( $self, $matrix ) = @_;
187 0 $logger->debug("unsetting matrix $matrix");
188 0 ▶ if ( looks_like_object $matrix, $MATRIX ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_object($matrix, $MATRIX)) 0 0 0
189 0 $matrix->unset_taxa();
190 }
191 0 return $self;
192 }
193  
194 =back
195  
196 =head2 ACCESSORS
197  
198 =over
199  
200 =item get_forests()
201  
202 Gets all associated Bio::Phylo::Forest objects.
203  
204 Type : Accessor
205 Title : get_forests
206 Usage : @forests = @{ $taxa->get_forests };
207 Function: Retrieves forests associated
208 with the current taxa object.
209 Returns : An ARRAY reference of
210 Bio::Phylo::Forest objects.
211 Args : None.
212  
213 =cut
214  
215 0 ▶ sub get_forests {
Subroutine get_forests: not called CC=1 SCAR=6.9
Pod documented
216 0 my $self = shift;
217 0 return $mediator->get_link(
218 '-source' => $self,
219 '-type' => $FOREST,
220 );
221 }
222  
223 =item get_matrices()
224  
225 Gets all associated Bio::Phylo::Matrices::Matrix objects.
226  
227 Type : Accessor
228 Title : get_matrices
229 Usage : @matrices = @{ $taxa->get_matrices };
230 Function: Retrieves matrices associated
231 with the current taxa object.
232 Returns : An ARRAY reference of
233 Bio::Phylo::Matrices::Matrix objects.
234 Args : None.
235  
236 =cut
237  
238 0 ▶ sub get_matrices {
Subroutine get_matrices: not called CC=1 SCAR=6.9
Pod documented
239 0 my $self = shift;
240 0 return $mediator->get_link(
241 '-source' => $self,
242 '-type' => $MATRIX,
243 );
244 }
245  
246 =item get_ntax()
247  
248 Gets number of contained Bio::Phylo::Taxa::Taxon objects.
249  
250 Type : Accessor
251 Title : get_ntax
252 Usage : my $ntax = $taxa->get_ntax;
253 Function: Retrieves the number of taxa for the invocant.
254 Returns : INT
255 Args : None.
256 Comments:
257  
258 =cut
259  
260 22 ▶ sub get_ntax {
Subroutine get_ntax: called CC=1 SCAR=0.0
Pod documented
261 22 my $self = shift;
262 22 return scalar @{ $self->get_entities };
263 }
264  
265 =back
266  
267 =head2 METHODS
268  
269 =over
270  
271 =item merge_by_name()
272  
273 Merges argument Bio::Phylo::Taxa object with invocant.
274  
275 Type : Method
276 Title : merge_by_name
277 Usage : $merged = $taxa->merge_by_name($other_taxa);
278 Function: Merges two or more taxa objects such that
279 internally different taxon objects
280 with the same name become a single
281 object with the combined references
282 to datum objects and node objects
283 contained by the two.
284 Returns : A merged Bio::Phylo::Taxa object.
285 Args : Bio::Phylo::Taxa objects.
286  
287 =cut
288  
289 0 ▶ sub merge_by_name {
Subroutine merge_by_name: not called CC=7 SCAR=40.3
Pod documented
290 0 my $merged = $factory->create_taxa( '-name' => 'Merged' );
291 0 for my $taxa (@_) {
292
293 # build a hash of what we have so far
294 0 my %taxon_by_name = map { $_->get_name => $_ } @{ $merged->get_entities };
295
296 # iterate over focal taxa block
297 0 for my $taxon ( @{ $taxa->get_entities } ) {
298 0 my $name = $taxon->get_name;
299
300 # retrieve or create target taxon
301 0 my $target;
302 0 ▶ if ( $taxon_by_name{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxon_by_name{$name}) { } 0 0 0
303 0 $target = $taxon_by_name{$name};
304 }
305 else {
306 0 $target = $factory->create_taxon( '-name' => $name );
307 0 $merged->insert($target);
308 0 $taxon_by_name{$name} = $target;
309 }
310
311 # copy over data, metadata and node links
312 0 $_->set_taxon($target) for @{ $taxon->get_data };
313 0 $_->set_taxon($target) for @{ $taxon->get_nodes };
314 0 $target->add_meta($_) for @{ $taxon->get_meta };
315 }
316 }
317 0 return $merged;
318 }
319  
320 =item merge_by_meta()
321  
322 Merges argument Bio::Phylo::Taxa object with invocant.
323  
324 Type : Method
325 Title : merge_by_meta
326 Usage : $taxa->merge_by_name('dc:identifier',$other_taxa);
327 Function: Merges two taxa objects such that
328 internally different taxon objects
329 with the same annotation value become
330 a single object with the combined references
331 to datum objects, node objects and
332 metadata annotations contained by
333 the two.
334 Returns : A merged Bio::Phylo::Taxa object.
335 Args : a CURIE predicate and Bio::Phylo::Taxa objects.
336  
337 =cut
338  
339 1 ▶ sub merge_by_meta {
Subroutine merge_by_meta: called CC=9 SCAR=22.1
Pod documented
340 1 my ( $self, $predicate, @others ) = @_;
341 1 push @others, $self;
342 1 my $merged = $factory->create_taxa;
343 1 for my $taxa ( @others ) {
344
345 my %object_by_value =
346 10 map { $_->get_meta_object($predicate) => $_ }
347 2 @{ $merged->get_entities };
348
349 2 for my $taxon ( @{ $taxa->get_entities } ) {
350
351 # instantiate or fetch taxon based on predicate value
352 20 my $value = $taxon->get_meta_object($predicate);
353 20 ▶ my $target = $object_by_value{$value} || $factory->create_taxon();
Condition66%2/3
$object_by_value{$value} || $factory->create_taxon
l!l&&r!l&&!r
10100
MC/DC0%0/2
$object_by_value{$value} || $factory->create_taxon
$object_by_value{$value} $factory->create_taxon
Truth table66%2/3
A || B
A= $object_by_value{$value}B= $factory->create_taxon
ABresult
1X1
011
000
354
355 # copy links and metadata
356 20 $_->set_taxon($target) for @{ $taxon->get_data };
357 20 $_->set_taxon($target) for @{ $taxon->get_nodes };
358 20 $target->add_meta($_) for @{ $taxon->get_meta };
359
360 # copy name to bp:contributing_name
361 20 ▶ if ( my $name = $taxon->get_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $taxon->get_name) 0 20 20
362 0 $target->add_meta(
363 $factory->create_meta(
364 '-namespaces' => { 'bp' => _NS_BIOPHYLO_ },
365 '-triple' => { 'bp:contributing_name' => $name }
366 )
367 );
368 }
369
370 # add to hash and block if newly created
371 20 ▶ if ( not exists $object_by_value{$value} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $object_by_value{$value}) 10 10 20
372 10 $merged->insert($target);
373 10 $object_by_value{$value} = $target;
374 }
375 }
376 }
377 1 return $merged;
378 }
379  
380 =item prune_taxa()
381  
382 Removes taxa by name or object
383  
384 Type : Method
385 Title : prune_taxa
386 Usage : $taxa->prune_taxa([$t1, $t2]);
387 Function: Prunes taxa from the taxa object
388 Returns : A pruned Bio::Phylo::Taxa object.
389 Args : An array reference of taxa, either by name or as taxon objects
390  
391 =cut
392  
393 0 ▶ sub prune_taxa {
Subroutine prune_taxa: not called CC=6 SCAR=37.4
Pod documented
394 0 my ( $self, $arrayref ) = @_;
395 0 ▶ if ( ref($arrayref) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arrayref eq 'ARRAY') { } 0 0 0
396 0 for my $t ( @{ $arrayref } ) {
397 0 ▶ if ( not ref $t ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $t) { } 0 0 0
elsif (&looks_like_object($t, 5)) { } 0 0 0
398 0 ▶ if ( my $obj = $self->get_by_name($t) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->get_by_name($t)) { } 0 0 0
399 0 $self->delete($obj);
400 }
401 else {
402 0 $logger->warn("Couldn't find taxon with name '$t'");
403 }
404 }
405 elsif ( looks_like_object $t, _TAXON_ ) {
406 0 $self->delete($t);
407 }
408 }
409 }
410 else {
411 0 throw 'BadArgs' => 'Argument is not an array reference';
412 }
413 0 return $self;
414 }
415  
416  
417 =item to_nexus()
418  
419 Serializes invocant to nexus format.
420  
421 Type : Format convertor
422 Title : to_nexus
423 Usage : my $block = $taxa->to_nexus;
424 Function: Converts $taxa into a nexus taxa block.
425 Returns : Nexus taxa block (SCALAR).
426 Args : -links => 1 (optional, adds 'TITLE' token)
427 Comments:
428  
429 =cut
430  
431 2 ▶ sub to_nexus {
Subroutine to_nexus: called CC=5 SCAR=18.1
Pod documented
432 2 my ( $self, %args ) = @_;
433 my %m = (
434 'header' => ( $args{'-header'} && '#NEXUS' ) || '',
435 'title' =>
436 ( $args{'-links'} && sprintf 'TITLE %s;', $self->get_nexus_name )
437 || '',
438 'version' => $self->VERSION,
439 'ntax' => $self->get_ntax,
440 'class' => ref $self,
441 'time' => my $time = localtime(),
442 'taxlabels' => join "\n\t\t",
443 2 ▶ map { $_->get_nexus_name } @{ $self->get_entities }
Condition44%4/9
$args{'-header'} && '#NEXUS' || ''
l!l
02
$args{"-header"} and "#NEXUS"
!ll
20
$args{'-links'} && sprintf('TITLE %s;', $self->get_nexus_name) || ''
l!l
02
$args{"-links"} and sprintf "TITLE %s;", $self->get_nexus_name
!ll&&!rl&&r
200
MC/DC0%0/1
$args{'-header'} && '#NEXUS' || ''
$args{"-header"}
MC/DC0%0/2
$args{'-links'} && sprintf('TITLE %s;', $self->get_nexus_name) || ''
$args{"-links"} sprintf "TITLE %s;", $self->get_nexus_name
Truth table50%1/2
A
A= $args{"-header"}
Aresult
11
00
Truth table33%1/3
A and B
A= $args{"-links"}B= sprintf "TITLE %s;", $self->get_nexus_name
ABresult
111
0X0
100
444 );
445 2 return <<TEMPLATE;
446 $m{header}
447 BEGIN TAXA;
448 [! Taxa block written by $m{class} $m{version} on $m{time} ]
449 $m{title}
450 DIMENSIONS NTAX=$m{ntax};
451 TAXLABELS
452 $m{taxlabels}
453 ;
454 END;
455 TEMPLATE
456 }
457  
458 =begin comment
459  
460 Type : Internal method
461 Title : _container
462 Usage : $taxa->_container;
463 Function:
464 Returns : CONSTANT
465 Args :
466  
467 =end comment
468  
469 =cut
470  
471 23 ▶ sub _container { $CONTAINER }
Subroutine _container: called CC=1 SCAR=0.0
472  
473 =begin comment
474  
475 Type : Internal method
476 Title : _type
477 Usage : $taxa->_type;
478 Function:
479 Returns : SCALAR
480 Args :
481  
482 =end comment
483  
484 =cut
485  
486 565 ▶ sub _type { $TYPE }
Subroutine _type: called CC=1 SCAR=0.0
487 1 ▶ sub _tag { 'otus' }
Subroutine _tag: called CC=1 SCAR=0.0
488  
489 =back
490  
491 =cut
492  
493 # podinherit_insert_token
494  
495 =head1 SEE ALSO
496  
497 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
498 for any user or developer questions and discussions.
499  
500 =over
501  
502 =item L<Bio::Phylo::Listable>
503  
504 The L<Bio::Phylo::Taxa> object inherits from the L<Bio::Phylo::Listable>
505 object. Look there for more methods applicable to the taxa object.
506  
507 =item L<Bio::Phylo::Manual>
508  
509 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
510  
511 =back
512  
513 =head1 CITATION
514  
515 If you use Bio::Phylo in published research, please cite it:
516  
517 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
518 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
519 I<BMC Bioinformatics> B<12>:63.
520 L<http://dx.doi.org/10.1186/1471-2105-12-63>
521  
522 =cut
523  
524 }
525 15 1;
« Phylo/Treedrawer/Canvas.pm ↑ Summary Phylo/ListableRole.pm »