Phylo/Project.pm

filter: Statementstmt 43.3% 88 / 203 Branchbran 21.4% 9 / 42 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 62.9% 17 / 27 Podpod 100% 13 / 13 totaltotal 42.3% 127 / 300 CC 35 SCAR 25.5
CC
35
Cov
38%
get_document
30.0
__ANON__
23.8
reset_xml_ids
20.0
CRAP 12.8
« Phylo/Parsers/Abstract.pm ↑ Summary Phylo/Matrices/Character.pm »
1 package Bio::Phylo::Project;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use base 'Bio::Phylo::Listable';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Bio::Phylo::Util::CONSTANT qw':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Bio::Phylo::Util::Exceptions 'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Bio::Phylo::Util::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Bio::Phylo::IO 'parse';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Bio::Phylo::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 my $fac = Bio::Phylo::Factory->new;
11 13 my $logger = Bio::Phylo::Util::Logger->new;
12  
13 {
14  
15 =head1 NAME
16  
17 Bio::Phylo::Project - Container for related data
18  
19 =head1 SYNOPSIS
20  
21 use Bio::Phylo::Factory;
22 my $fac = Bio::Phylo::Factory->new;
23 my $proj = $fac->create_project;
24 my $taxa = $fac->create_taxa;
25 $proj->insert($taxa);
26 $proj->insert($fac->create_matrix->set_taxa($taxa));
27 $proj->insert($fac->create_forest->set_taxa($taxa));
28 print $proj->to_xml;
29  
30 =head1 DESCRIPTION
31  
32 The project module is used to collect taxa blocks, tree blocks and
33 matrices.
34  
35 =head1 METHODS
36  
37 =head2 MUTATORS
38  
39 =over
40  
41 =item merge()
42  
43 Project constructor.
44  
45 Type : Constructor
46 Title : merge
47 Usage : my $project = Bio::Phylo::Project->merge( @projects )
48 Function: Populates a Bio::Phylo::Project object from a list of projects
49 Returns : A Bio::Phylo::Project object.
50 Args : A list of Bio::Phylo::Project objects to be merged
51  
52 =cut
53  
54 0 ▶ sub merge {
Subroutine merge: not called CC=3 SCAR=24.8
Pod documented
55 0 my $class = shift;
56 0 my $self = $class->SUPER::new;
57 0 my @taxa = map { @{ $_->get_items(_TAXA_) } } @_;
58 0 my $taxa = $fac->create_taxa->merge_by_name(@taxa);
59 0 my $forest = $fac->create_forest( '-taxa' => $taxa );
60 0 $forest->insert($_) for map { @{ $_->get_items(_TREE_) } } @_;
61 0 $self->insert($taxa);
62 0 $self->insert($forest);
63 0 $self->insert($_) for map { $_->set_taxa($taxa) } map { @{ $_->get_items(_MATRIX_) } } @_;
64 0 return $self;
65 }
66  
67 =item set_datasource()
68  
69 Project constructor.
70  
71 Type : Constructor
72 Title : set_datasource
73 Usage : $project->set_datasource( -file => $file, -format => 'nexus' )
74 Function: Populates a Bio::Phylo::Project object from a data source
75 Returns : A Bio::Phylo::Project object.
76 Args : Arguments as must be passed to Bio::Phylo::IO::parse
77  
78 =cut
79  
80 0 ▶ sub set_datasource {
Subroutine set_datasource: not called CC=1 SCAR=6.9
Pod documented
81 0 my $self = shift;
82 0 return parse( '-project' => $self, @_ );
83 }
84  
85 =item reset_xml_ids()
86  
87 Resets all xml ids to default values
88  
89 Type : Mutator
90 Title : reset_xml_ids
91 Usage : $project->reset_xml_ids
92 Function: Resets all xml ids to default values
93 Returns : A Bio::Phylo::Project object.
94 Args : None
95  
96 =cut
97  
98 0 ▶ sub reset_xml_ids {
Subroutine reset_xml_ids: not called CC=4 SCAR=30.0
Pod documented
99 0 my $self = shift;
100 0 ▶ if ( UNIVERSAL::can($self,'set_xml_id') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($self, "set_xml_id")) 0 0 0
101 0 my $xml_id = $self->get_tag;
102 0 my $obj_id = sprintf("%x",$self->get_id);
103 0 $xml_id =~ s/^(.).+(.)$/$1$2$obj_id/;
104 0 $self->set_xml_id($xml_id);
105 }
106 0 ▶ if ( UNIVERSAL::can($self,'get_entities') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($self, "get_entities")) 0 0 0
107 0 reset_xml_ids($_) for @{ $self->get_entities };
108 }
109 0 return $self;
110 }
111  
112 =back
113  
114 =head2 ACCESSORS
115  
116 =over
117  
118 =cut
119  
120 13 my $TYPE = _PROJECT_;
121 13 my $TAXA = _TAXA_;
122 13 my $FOREST = _FOREST_;
123 13 my $MATRIX = _MATRIX_;
124 10 ▶ my $get_object = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
125 10 my ( $self, $CONSTANT ) = @_;
126 10 my @result;
127 10 for my $ent ( @{ $self->get_entities } ) {
128 11 ▶ if ( $ent->_type == $CONSTANT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ent->_type == $CONSTANT) 6 5 11
129 6 push @result, $ent;
130 }
131 }
132 10 return \@result;
133 13 };
134  
135 =item get_taxa()
136  
137 Getter for taxa objects
138  
139 Type : Accessor
140 Title : get_taxa
141 Usage : my $taxa = $proj->get_taxa;
142 Function: Getter for taxa objects
143 Returns : An array reference of taxa objects
144 Args : NONE.
145  
146 =cut
147  
148 3 ▶ sub get_taxa {
Subroutine get_taxa: called CC=1 SCAR=0.0
Pod documented
149 3 my $self = shift;
150 3 return $get_object->( $self, $TAXA );
151 }
152  
153 =item get_forests()
154  
155 Getter for forest objects
156  
157 Type : Accessor
158 Title : get_forests
159 Usage : my $forest = $proj->get_forests;
160 Function: Getter for forest objects
161 Returns : An array reference of forest objects
162 Args : NONE.
163  
164 =cut
165  
166 3 ▶ sub get_forests {
Subroutine get_forests: called CC=1 SCAR=0.0
Pod documented
167 3 my $self = shift;
168 3 return $get_object->( $self, $FOREST );
169 }
170  
171 =item get_matrices()
172  
173 Getter for matrix objects
174  
175 Type : Accessor
176 Title : get_matrices
177 Usage : my $matrix = $proj->get_matrices;
178 Function: Getter for matrix objects
179 Returns : An array reference of matrix objects
180 Args : NONE.
181  
182 =cut
183  
184 4 ▶ sub get_matrices {
Subroutine get_matrices: called CC=1 SCAR=0.0
Pod documented
185 4 my $self = shift;
186 4 return $get_object->( $self, $MATRIX );
187 }
188  
189 =item get_items()
190  
191 Gets all items of the specified type, recursively. This method can be used
192 to get things like all the trees in all the forest objects as one flat list
193 (or, indeed, all nodes, all taxon objects, etc.)
194  
195 Type : Accessor
196 Title : get_items
197 Usage : my @nodes = @{ $proj->get_items(_NODE_) };
198 Function: Getter for items of specified type
199 Returns : An array reference of objects
200 Args : A type constant as defined in Bio::Phylo::Util::CONSTANT
201  
202 =cut
203  
204 108 ▶ sub _item_finder {
Subroutine _item_finder: called CC=5 SCAR=16.1
205 108 my ( $item, $const, $array ) = @_;
206 108 ▶ if ( UNIVERSAL::can($item,'_type') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($item, "_type")) 108 0 108
207 108 ▶ if ( $item->_type == $const ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item->_type == $const) { } 55 53 108
elsif (UNIVERSAL::can($item, 'get_entities')) { } 37 16 53
208 55 push @{ $array }, $item;
209 }
210 elsif ( UNIVERSAL::can($item,'get_entities') ) {
211 37 _item_finder( $_, $const, $array ) for @{ $item->get_entities };
212 }
213 }
214 }
215
216 19 ▶ sub get_items {
Subroutine get_items: called CC=2 SCAR=7.2
Pod documented
217 19 my ( $self, $const ) = @_;
218 19 ▶ if ( $const !~ /^\d+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($const =~ /^\d+/) 0 19 19
219 0 throw 'BadArgs' => 'Constant must be an integer';
220 }
221 19 my $result = [];
222 19 _item_finder( $self, $const, $result );
223 19 return $result;
224 }
225  
226 =item get_document()
227  
228 Type : Serializer
229 Title : doc
230 Usage : $proj->get_document()
231 Function: Creates a DOM Document object, containing the
232 present state of the project by default
233 Returns : a Document object
234 Args : a DOM factory object
235 Optional: pass 1 to obtain a document node without
236 content
237  
238 =cut
239  
240 0 ▶ sub get_document {
Subroutine get_document: not called CC=5 SCAR=34.0
Pod documented
241 0 my $self = shift;
242 0 my $dom = $_[0];
243 0 my @args = @_;
244  
245 # handle dom factory object...
246 0 ▶ if ( looks_like_instance( $dom, 'SCALAR' )
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_instance($dom, 'SCALAR') and $dom->_type == 18) { } 0 0 0
Condition0%0/3
&looks_like_instance($dom, "SCALAR") and $dom->_type == 18
!ll&&!rl&&r
000
MC/DC0%0/2
&looks_like_instance($dom, "SCALAR") and $dom->_type == 18
&looks_like_instance($dom, "SCALAR") $dom->_type == 18
Truth table0%0/3
A and B
A= &looks_like_instance($dom, "SCALAR")B= $dom->_type == 18
ABresult
0X0
100
111
247 && $dom->_type == _DOMCREATOR_ )
248 {
249 0 splice( @args, 0, 1 );
250 }
251 else {
252 0 $dom = $Bio::Phylo::NeXML::DOM::DOM;
253 0 ▶ unless ($dom) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dom) 0 0 0
254 0 throw 'BadArgs' => 'DOM factory object not provided';
255 }
256 }
257 ### # make sure argument handling works here...
258 0 my $empty = shift @args;
259 0 my $doc = $dom->create_document();
260 0 my $root;
261 0 ▶ unless ($empty) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($empty) 0 0 0
262 0 $root = $self->to_dom($dom);
263 0 $doc->set_root($root);
264 }
265 0 return $doc;
266 }
267  
268 =item get_attributes()
269  
270 Retrieves attributes for the element.
271  
272 Type : Accessor
273 Title : get_attributes
274 Usage : my %attrs = %{ $obj->get_attributes };
275 Function: Gets the xml attributes for the object;
276 Returns : A hash reference
277 Args : None.
278 Comments: throws ObjectMismatch if no linked taxa object
279 can be found
280  
281 =cut
282  
283 0 ▶ sub get_attributes {
Subroutine get_attributes: not called CC=1 SCAR=6.9
Pod documented
284 0 my $self = shift;
285 0 my $class = ref($self);
286 0 my $version = $class->VERSION;
287 0 my %defaults = (
288 'version' => _NEXML_VERSION_,
289 'generator' => "$class v.$version",
290 'xmlns' => _NS_NEXML_,
291 'xsi:schemaLocation' => _NS_NEXML_ . ' '
292 . _NS_NEXML_
293 . '/nexml.xsd',
294 );
295 0 my %attrs = ( %defaults, %{ $self->SUPER::get_attributes } );
296 0 return \%attrs;
297 }
298  
299 =item is_identifiable()
300  
301 By default, all XMLWritable objects are identifiable when serialized,
302 i.e. they have a unique id attribute. However, in some cases a serialized
303 object may not have an id attribute (governed by the nexml schema). This
304 method indicates whether that is the case.
305  
306 Type : Test
307 Title : is_identifiable
308 Usage : if ( $obj->is_identifiable ) { ... }
309 Function: Indicates whether IDs are generated
310 Returns : BOOLEAN
311 Args : NONE
312  
313 =cut
314  
315 0 ▶ sub is_identifiable { 0 }
Subroutine is_identifiable: not called CC=1 SCAR=6.9
Pod documented
316  
317 =back
318  
319 =head2 SERIALIZERS
320  
321 =over
322  
323 =item to_xml()
324  
325 Serializes invocant to XML.
326  
327 Type : XML serializer
328 Title : to_xml
329 Usage : my $xml = $obj->to_xml;
330 Function: Serializes $obj to xml
331 Returns : An xml string
332 Args : Same arguments as can be passed to individual contained objects
333  
334 =cut
335  
336 0 ▶ sub _add_project_metadata {
Subroutine _add_project_metadata: not called CC=4 SCAR=30.0
337 0 my $self = shift;
338 0 $self->set_namespaces( 'dc' => _NS_DC_ );
339 0 ▶ if ( my $user = $ENV{'USER'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $user = $ENV{"USER"}) 0 0 0
340 0 $logger->debug("adding user metadata '${user}'");
341 0 $self->add_meta(
342 $fac->create_meta( '-triple' => { 'dc:creator' => $user } ) );
343 }
344 0 eval { require DateTime };
345 0 ▶ if ( not $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 0 0 0
346 0 my $now = DateTime->now();
347 0 $logger->debug("adding timestamp metadata '${now}'");
348 0 $self->add_meta(
349 $fac->create_meta( '-triple' => { 'dc:date' => $now } ) );
350 }
351 else {
352 0 undef($@);
353 }
354 0 ▶ if ( my $desc = $self->get_desc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $desc = $self->get_desc) 0 0 0
355 0 $logger->debug("adding description metadata '${desc}'");
356 0 $self->add_meta(
357 $fac->create_meta( '-triple' => { 'dc:description' => $desc } )
358 );
359 }
360 }
361  
362 0 ▶ sub to_xml {
Subroutine to_xml: not called CC=4 SCAR=30.0
Pod documented
363 0 my $self = shift;
364 0 my %args;
365 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
366 0 %args = @_;
367 0 ▶ $self->reset_xml_ids if $args{'-reset'};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"-reset"} 0 0 0
368 }
369  
370 # creating opening tags
371 0 $self->_add_project_metadata;
372 0 my $xml = $self->get_xml_tag;
373 0 $logger->debug("created opening structure ${xml}");
374  
375 # processing contents
376 0 my @linked = ( @{ $self->get_forests }, @{ $self->get_matrices } );
377 0 $logger->debug("fetched linked objects @linked");
378  
379 # writing out taxa blocks and linked objects
380 0 my %taxa = map { $_->get_id => $_ } @{ $self->get_taxa },
381 0 map { $_->make_taxa } @linked;
382 0 for ( values %taxa, @linked ) {
383 0 $logger->debug("writing $_ to xml");
384 0 $xml .= $_->to_xml(%args);
385 }
386 0 $xml .= '</' . $self->get_tag . '>';
387  
388 # done creating xml strings
389 0 $logger->debug($xml);
390 #eval { require XML::Twig };
391 #if ( not $@ ) {
392 # my $twig = XML::Twig->new( 'pretty_print' => 'indented' );
393 # eval { $twig->parse($xml) };
394 # if ($@) {
395 # throw 'API' => "Couldn't build xml: " . $@ . "\n\n$xml";
396 # }
397 # else {
398 # return $twig->sprint;
399 # }
400 #}
401 #else {
402 # undef $@;
403 # return $xml;
404 #}
405 0 return $xml;
406 }
407  
408 =item to_nexus()
409  
410 Serializes invocant to NEXUS.
411  
412 Type : NEXUS serializer
413 Title : to_nexus
414 Usage : my $nexus = $obj->to_nexus;
415 Function: Serializes $obj to nexus
416 Returns : An nexus string
417 Args : Same arguments as can be passed to individual contained objects
418  
419 =cut
420  
421 2 ▶ my $write_notes = sub {
Subroutine __ANON__: called CC=9 SCAR=31.7
422 2 my ( $self, @taxa ) = @_;
423 2 my $nexus = 'BEGIN NOTES;' . "\n";
424 2 my $version = $self->VERSION;
425 2 my $class = ref $self;
426 2 my $time = localtime();
427 2 $nexus .= "[! Notes block written by $class $version on $time ]\n";
428 2 for my $taxa ( @taxa ) {
429 2 my $name = $taxa->get_nexus_name;
430 2 my ( $i, $j ) = ( 1, 0 );
431 2 for my $taxon ( @{ $taxa->get_entities } ) {
432 22 ▶ if ( my $link = $taxon->get_link ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $link = $taxon->get_link) 0 22 22
433 0 ▶ if ( $link =~ m|/phylows/| ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link =~ m[/phylows/]) 0 0 0
434
435 # link has no query string, append one
436 0 ▶ if ( $link !~ /\?/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($link =~ /\?/) 0 0 0
437 0 $link .= '?';
438 }
439
440 # link has a format statement, replace format
441 0 ▶ if ( $link =~ /\?.*format=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link =~ /\?.*format=/) { } 0 0 0
442 0 $link =~ s/(\?.*format=)\s+/$1nexus/;
443 }
444
445 # append format statement
446 else {
447 0 ▶ $link .= '&' if $link !~ /\?$/ && $link !~ /&$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $link =~ /\?$/ or $link =~ /&$/ 0 0 0
Condition0%0/3
$link =~ /\?$/ or $link =~ /&$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$link =~ /\?$/ or $link =~ /&$/
$link =~ /\?$/ $link =~ /&$/
Truth table0%0/3
A or B
A= $link =~ /\?$/B= $link =~ /&$/
ABresult
1X1
011
000
448 0 $link .= 'format=nexus';
449 }
450 }
451 0 $nexus .= "\tSUT TAXA = $name TAXON = $i NAME = hyperlink STRING = '$link';\n";
452 0 $nexus .= "\tHYPERLINK TAXA = $name TAXON = $j URL = '$link';\n";
453 }
454 22 $i++;
455 22 $j++;
456 }
457 }
458 2 $nexus .= 'END;' . "\n";
459 13 };
460  
461 2 ▶ sub to_nexus {
Subroutine to_nexus: called CC=2 SCAR=6.9
Pod documented
462 2 my $self = shift;
463 2 my $nexus = "#NEXUS\n";
464 2 my @linked = ( @{ $self->get_forests }, @{ $self->get_matrices } );
465 2 my %taxa = map { $_->get_id => $_ } @{ $self->get_taxa },
466 2 map { $_->make_taxa } @linked;
467 2 for ( values %taxa, @linked ) {
468 3 $nexus .= $_->to_nexus(@_);
469 }
470 2 $nexus .= $write_notes->($self,values %taxa);
471 2 return $nexus;
472 }
473  
474 =item to_dom()
475  
476 Type : Serializer
477 Title : to_dom
478 Usage : $node->to_dom
479 Function: Generates a DOM subtree from the invocant
480 and its contained objects
481 Returns : an XML::LibXML::Element object
482 Args : a DOM factory object
483  
484 =cut
485  
486 0 ▶ sub to_dom {
Subroutine to_dom: not called CC=4 SCAR=30.0
Pod documented
487 0 my ( $self, $dom ) = @_;
488 0 ▶ $dom ||= Bio::Phylo::NeXML::DOM->get_dom;
Condition0%0/3
$dom ||= "Bio::Phylo::NeXML::DOM"->get_dom
l!l&&r!l&&!r
000
MC/DC0%0/2
$dom ||= "Bio::Phylo::NeXML::DOM"->get_dom
$dom "Bio::Phylo::NeXML::DOM"->get_dom
Truth table0%0/3
A ||= B
A= $domB= "Bio::Phylo::NeXML::DOM"->get_dom
ABresult
1X1
011
000
489 0 ▶ unless ( looks_like_object $dom, _DOMCREATOR_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_object($dom, 18)) 0 0 0
490 0 throw 'BadArgs' => 'DOM factory object not provided';
491 }
492 0 my $elt = $self->get_dom_elt($dom);
493 0 my @linked = ( @{ $self->get_forests }, @{ $self->get_matrices } );
494 0 my %taxa = map { $_->get_id => $_ } @{ $self->get_taxa },
495 0 map { $_->make_taxa } @linked;
496 0 for ( values %taxa, @linked ) {
497 0 $elt->set_child( $_->to_dom( $dom, @_ ) );
498 }
499 0 return $elt;
500 }
501 114 ▶ sub _type { $TYPE }
Subroutine _type: called CC=1 SCAR=0.0
502 0 ▶ sub _tag { 'nex:nexml' }
Subroutine _tag: not called CC=1 SCAR=6.9
503  
504 =back
505  
506 =cut
507  
508 # podinherit_insert_token
509  
510 =head1 SEE ALSO
511  
512 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
513 for any user or developer questions and discussions.
514  
515 =over
516  
517 =item L<Bio::Phylo::Listable>
518  
519 The L<Bio::Phylo::Project> object inherits from the L<Bio::Phylo::Listable>
520 object. Look there for more methods applicable to the project object.
521  
522 =item L<Bio::Phylo::Manual>
523  
524 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
525  
526 =back
527  
528 =head1 CITATION
529  
530 If you use Bio::Phylo in published research, please cite it:
531  
532 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
533 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
534 I<BMC Bioinformatics> B<12>:63.
535 L<http://dx.doi.org/10.1186/1471-2105-12-63>
536  
537 =cut
538  
539 }
540  
541 13 1
« Phylo/Parsers/Abstract.pm ↑ Summary Phylo/Matrices/Character.pm »