Phylo/Parsers/Nexml.pm untested

filter: Statementstmt 0.0% 0 / 363 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 525 CC 51 SCAR 32.3
CC
51
Cov
0%
_process_listtree
72.0
_init
56.0
_process_attributes
30.0
CRAP 25.3
1 package Bio::Phylo::Parsers::Nexml;
2 use strict;
3 use base 'Bio::Phylo::Parsers::Abstract';
4 use Bio::Phylo::Util::Exceptions 'throw';
5 use Bio::Phylo::Util::CONSTANT qw'looks_like_instance _NEXML_VERSION_ :objecttypes';
6 use Bio::Phylo::Util::Dependency 'XML::Twig';
7 use Bio::Phylo::Factory;
8 use Bio::Phylo::NeXML::Writable;
9 use Bio::Phylo::NeXML::Meta::XMLLiteral;
10  
11 =head1 NAME
12  
13 Bio::Phylo::Parsers::Nexml - Parser used by Bio::Phylo::IO, no serviceable parts inside
14  
15 =head1 DESCRIPTION
16  
17 This module parses nexml data. It is called by the L<Bio::Phylo::IO> facade,
18 don't call it directly. In addition to parsing from files, handles or strings (which
19 are specified by the -file, -handle and -string arguments) this parser can also parse
20 xml directly from a url (-url => $phylows_output), provided you have L<LWP> installed.
21  
22 =cut
23  
24 # podinherit_insert_token
25  
26 =head1 SEE ALSO
27  
28 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
29 for any user or developer questions and discussions.
30  
31 =over
32  
33 =item L<Bio::Phylo::IO>
34  
35 The nexml parser is called by the L<Bio::Phylo::IO> object.
36 Look there to learn how to parse nexml (or any other data Bio::Phylo supports).
37  
38 =item L<Bio::Phylo::Manual>
39  
40 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
41  
42 =item L<http://www.nexml.org>
43  
44 For more information about the nexml data standard, visit L<http://www.nexml.org>
45  
46 =back
47  
48 =head1 CITATION
49  
50 If you use Bio::Phylo in published research, please cite it:
51  
52 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
53 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
54 I<BMC Bioinformatics> B<12>:63.
55 L<http://dx.doi.org/10.1186/1471-2105-12-63>
56  
57 =cut
58  
59 # helper method to add parser reading position to log messages
60 sub _pos {
61 my $self = shift;
62 my $t = $self->{'_twig'};
63 join ':', ( $t->current_line, $t->current_column, $t->current_byte );
64 }
65  
66 sub _process_attributes {
67 my ( $self, $elt, $obj ) = @_;
68 my $atts;
69 eval { $atts = $elt->atts };
70 if ($@) { throw API => $@ }
71 my $id = $elt->att('id');
72 if ($id) {
73 $obj->set_xml_id($id);
74 delete $atts->{$id};
75 }
76 my $label = $elt->att('label');
77 if ($label) {
78 $obj->set_name($label);
79 delete $atts->{$label};
80 }
81 for my $key ( keys %{$atts} ) {
82 if ( $key =~ /^xmlns:(.+)$/ ) {
83 my $ns = $1;
84 my $uri = $atts->{$key};
85 $obj->set_namespaces( $ns => $uri );
86 }
87 else {
88 $obj->set_attributes( $key => $atts->{$key} );
89 }
90 }
91 }
92  
93 # nice 'n' generic: we provide an element and a class,
94 # from the class we instantiate a new object, we set
95 # the element id in the generic slot of the object.
96 # If the element has a label, use that as name,
97 # otherwise use id. Additional constructor args can
98 # be specified using named arguments, e.g. -type => 'dna'
99 sub _obj_from_elt {
100 my ( $self, $elt, $class, %args ) = @_;
101  
102 # factory object handles instantiation (and class loading)
103 # see Bio::Phylo::Factory
104 my $method = "create_$class";
105 my $obj = $self->_factory->$method(%args);
106  
107 # <dict/> elements are deprecated
108 for my $dict_elt ( $elt->children('dict') ) {
109 $self->_logger->warn( $self->_pos . " dict elements are deprecated!" );
110 }
111 for my $meta_elt ( $elt->children('meta') ) {
112 my $meta = $self->_process_meta($meta_elt);
113 $obj->add_meta($meta);
114 }
115 $self->_process_attributes( $elt, $obj );
116 my $id = $elt->att('id');
117 my $tag = $elt->tag;
118 if ( defined $id ) {
119 $self->_logger->debug( $self->_pos . " processed <$tag id=\"$id\"/>" );
120 }
121 else {
122 $self->_logger->debug( $self->_pos . " processed <$tag/>" );
123 }
124 return ( $obj, $id );
125 }
126  
127 # this processes subsets of things
128 sub _process_set {
129 my ( $self, $parent_elt, $container ) = @_;
130 for my $elt ( $parent_elt->children('set') ) {
131 my ( $set, $set_id ) = $self->_obj_from_elt( $elt, 'set' );
132 $container->add_set($set);
133 my %idrefs;
134 for my $thing ( @{ $container->get_entities } ) {
135 my $tag = $thing->get_tag;
136 my $id = $thing->get_xml_id;
137 if ( not exists $idrefs{$tag} ) {
138 my @refs = grep { /\S/ } split /\s+/, $elt->att($tag);
139 if ( @refs ) {
140 my %map = map { $_ => 1 } @refs;
141 $idrefs{$tag} = \%map;
142 }
143 }
144 if ( $idrefs{$tag}->{$id} ) {
145 $container->add_to_set($thing,$set);
146 }
147 }
148 }
149 }
150  
151 # this is the constructor that gets called by Bio::Phylo::IO,
152 # here we create the object instance that will process the file/string
153 sub _init {
154 my $self = shift;
155 my ( $skipchars, $skiptrees ) = ( 0, 0 );
156 if ( $self->_args && $self->_args->{'-skip'} ) {
157 for my $skip ( @{ $self->_args->{'-skip'} } ) {
158 if ( $skip == _MATRIX_ ) {
159 $skipchars = 1;
160 $self->_logger->warn("skipping all characters elements");
161 }
162 if ( $skip == _FOREST_ ) {
163 $skiptrees = 1;
164 $self->_logger->warn("skipping all trees elements");
165 }
166 }
167 }
168 $self->_logger->debug("initializing $self");
169 $self->{'_blocks'} = [];
170 $self->{'_taxa'} = {};
171 $self->{'_taxon_in_taxa'} = {};
172  
173 # here we put the two together, i.e. create the actual XML::Twig object
174 # with its handlers, and create a reference to it in the parser object
175 $self->{'_twig'} = XML::Twig->new(
176  
177 # These handlers are called when the subtree is fully loaded, which
178 # means we can traverse it
179 'TwigHandlers' => {
180 'nex:nexml' => sub { &_handle_nexml( @_, $self ) },
181 'otus' => sub { &_handle_otus( @_, $self ) },
182 'characters' => $skipchars ? sub {} : sub { &_handle_chars( @_, $self ) },
183 'trees' => $skiptrees ? sub {} : sub { &_handle_forest( @_, $self ) },
184 },
185  
186 # These handlers are called when the element opens, that is the
187 # subtree hasn't been loaded yet - but the attributes have been,
188 # so we can read in the namespaces here.
189 'StartTagHandlers' => {
190 '_all_' => sub {
191 my ( $twig, $elt ) = @_;
192 for my $att_name ( $elt->att_names ) {
193 if ( $att_name =~ /^xmlns:(.+)$/ ) {
194 my $prefix = $1;
195 my $ns = $elt->att($att_name);
196 Bio::Phylo::NeXML::Writable->set_namespaces(
197 $prefix => $ns );
198 }
199 }
200 }
201 },
202 );
203 return $self;
204 }
205  
206 # called by Bio::Phylo::Parsers::Abstract
207 sub _parse {
208 my $self = shift;
209 $self->_init;
210 $self->_logger->debug("going to parse xml");
211 my %opt = @_;
212 $self->{'_twig'}->parse( $self->_string );
213  
214 # we're done, now order the blocks
215 my $ordered_blocks = $self->{'_blocks'};
216  
217 # prepare the requested return...
218 my $temp_project = pop( @{$ordered_blocks} ); # nexml root tag is processed last!
219 $self->{'_project_meta'} = $temp_project->get_meta;
220 return @{$ordered_blocks};
221 }
222  
223 sub _project_meta { shift->{'_project_meta'} }
224  
225 # element handler
226 sub _handle_nexml {
227 my ( $twig, $nexml_elt, $self ) = @_;
228 my ( $project_obj, $project_id ) =
229 $self->_obj_from_elt( $nexml_elt, 'project' );
230 push @{ $self->{'_blocks'} }, $project_obj;
231 $self->_logger->info( $self->_pos . " Processed nexml element" );
232 my $version = _NEXML_VERSION_;
233 if ( $nexml_elt->att('version') !~ /^\Q$version\E$/ ) {
234 throw 'BadFormat' =>
235 "Wrong version number, can only handle ${version}: "
236 . $nexml_elt->att('version');
237 }
238 }
239  
240 # element handler
241 sub _handle_otus {
242 my ( $twig, $taxa_elt, $self ) = @_;
243  
244 # instantiate taxa object, push on stack of blocks to return to user
245 my ( $taxa_obj, $taxa_id ) = $self->_obj_from_elt( $taxa_elt, 'taxa' );
246 push @{ $self->{'_blocks'} }, $taxa_obj;
247  
248 # create lookup to find taxa object for if other blocks refer to it
249 $self->{'_taxa'}->{$taxa_id} = $taxa_obj;
250  
251 # create lookup to find contained taxon objects if other elements refer to it
252 $self->{'_taxon_in_taxa'}->{$taxa_id} = {};
253  
254 # process contained otu elements
255 for my $taxon_elt ( $taxa_elt->children('otu') ) {
256  
257 # instantiate taxon object, insert in taxa object
258 my ( $taxon_obj, $taxon_id ) =
259 $self->_obj_from_elt( $taxon_elt, 'taxon' );
260 $taxa_obj->insert($taxon_obj);
261  
262 # add reference for later lookup
263 $self->{'_taxon_in_taxa'}->{$taxa_id}->{$taxon_id} = $taxon_obj;
264 }
265
266 # process taxon sets
267 $self->_process_set($taxa_elt,$taxa_obj);
268
269 $self->_logger->info( $self->_pos . " Processed block id: $taxa_id" );
270 }
271  
272 # again, nice 'n' generic: we provide an element, which must have an
273 # otu attribute; an object that is to be linked to a taxon; the otus
274 # attribute value of the containing element. Because $self->{_otus}
275 # collects a hash of hashes keyed on otus_idref => otu_idref we can
276 # then fetch the appropriate taxon
277 sub _set_otu_for_obj {
278 my ( $self, $elt, $obj, $taxa_idref ) = @_;
279  
280 # some elements (tree nodes) only optionally refer to otu elements
281 if ( my $taxon_idref = $elt->att('otu') ) {
282  
283 # referenced element must precede reference, hence $taxon_obj must be true
284 if ( my $taxon_obj =
285 $self->{'_taxon_in_taxa'}->{$taxa_idref}->{$taxon_idref} )
286 {
287 $obj->set_taxon($taxon_obj);
288 }
289  
290 # if not, throw exception - invalid data
291 else {
292 throw(
293 'API' => "no OTU '$taxon_idref' in block '$taxa_idref'",
294 'line' => $self->{'_twig'}->parser->current_line
295 );
296 }
297 }
298  
299 # notify user
300 else {
301 $self->_logger->info( $self->_pos . " no taxon idref" );
302 }
303 }
304  
305 # same thing, but for taxa objects
306 sub _set_otus_for_obj {
307 my ( $self, $elt, $obj ) = @_;
308  
309 # linking to otus elements is not optional!
310 if ( my $taxa_idref = $elt->att('otus') ) {
311  
312 # referenced element must precede reference
313 if ( my $taxa_obj = $self->{'_taxa'}->{$taxa_idref} ) {
314 $obj->set_taxa( $self->{'_taxa'}->{$taxa_idref} );
315 return $taxa_idref;
316 }
317  
318 # throw if $taxa_obj hasn't been created yet: invalid data
319 else {
320 throw(
321 'API' => "no taxa object '$taxa_idref'",
322 'line' => $self->{'_twig'}->parser->current_line
323 );
324 }
325 }
326  
327 # throw if there is no reference
328 else {
329 throw(
330 'API' => "no taxa reference",
331 'line' => $self->{'_twig'}->parser->current_line
332 );
333 }
334 }
335  
336 sub _handle_chars {
337 my ( $twig, $characters_elt, $self ) = @_;
338 $self->_logger->debug( $self->_pos . " going to parse characters element" );
339  
340 # create matrix object, send extra constructor args
341 my $type = $characters_elt->att('xsi:type');
342 my $compact = $type =~ /Seqs$/;
343 $type =~ s/^(?:.+:)?(.*?)(?:Cells|Seqs)/$1/;
344 my %args = ( '-type' => $type );
345 my ( $matrix_obj, $matrix_id ) =
346 $self->_obj_from_elt( $characters_elt, 'matrix', %args );
347 my $taxa_idref = $self->_set_otus_for_obj( $characters_elt, $matrix_obj );
348  
349 # create character definitions, if any
350 my ( $def_hash, $def_array ) = ( {}, [] );
351 my ( $lookup );
352 my $definitions_elt = $characters_elt->first_child('format');
353 ( $def_hash, $def_array, $lookup ) = $self->_process_definitions($definitions_elt);
354
355 $matrix_obj->get_type_object->set_lookup($lookup);
356 delete $args{'-type'};
357 $args{'-type_object'} = $matrix_obj->get_type_object;
358  
359 # create row objects
360 # rows are actually stored inside the <matrix/> element
361 my $matrix_elt = $characters_elt->first_child('matrix');
362 my ( $row_obj, $chars_hash );
363 for my $row_elt ( $matrix_elt->children('row') ) {
364 ( $row_obj, $chars_hash ) =
365 $self->_process_row( $row_elt, $def_hash, $def_array, %args );
366 my @chars;
367 if ( not $compact ) {
368 my $missing = $row_obj->get_missing;
369 my $i = 0;
370 for my $def_id ( @{$def_array} ) {
371 if ( exists $chars_hash->{$def_id} ) {
372 push @chars, $chars_hash->{$def_id};
373 }
374 else {
375 push @chars, $missing;
376 }
377 }
378 }
379 else {
380 my $highest_pos_for_this_row =
381 ( sort { $a <=> $b } keys %{$chars_hash} )[-1];
382 my $missing = $row_obj->get_missing;
383 for my $i ( 0 .. $highest_pos_for_this_row ) {
384 if ( exists $chars_hash->{$i} ) {
385 push @chars, $chars_hash->{$i};
386 }
387 else {
388 push @chars, $missing;
389 }
390 }
391 }
392 $self->_logger->debug( $self->_pos . " set char: '@chars'" );
393 $row_obj->set_char( \@chars );
394 $self->_set_otu_for_obj( $row_elt, $row_obj, $taxa_idref );
395 $matrix_obj->insert($row_obj);
396 }
397 my $characters = $matrix_obj->get_characters;
398
399 # assign original xml ids to character objects
400 my @char_elts = $definitions_elt->children('char');
401 for my $i ( 0 .. $#char_elts ) {
402 my ($char) = $self->_obj_from_elt( $char_elts[$i], 'character' );
403 $characters->insert_at_index($char,$i);
404 }
405
406 # now process character sets
407 $self->_process_set($definitions_elt,$characters);
408 push @{ $self->{'_blocks'} }, $matrix_obj;
409 $self->_logger->info( $self->_pos . " Processed block id: $matrix_id" );
410 }
411  
412 # here we create a hash keyed on column ids => state ids => state symbols
413 sub _process_definitions {
414 my ( $self, $format_elt ) = @_;
415 my ( $states_hash, $chars_hash, $states_array ) = ( {}, {}, [] );
416 my $lookup = {};
417  
418 # here we iterate over state set definitions, i.e. each
419 # $states_elt <states/> describes a set of mappings
420 for my $states_elt ( $format_elt->children('states') ) {
421 my $states_id = $states_elt->att('id');
422 $states_hash->{$states_id} = {};
423 my $process_state = sub {
424 my $elt = shift;
425 my ( $id, $sym ) = ( $elt->att('id'), $elt->att('symbol') );
426 $states_hash->{$states_id}->{$id} = $sym;
427 my @children = $elt->children('member');
428 if (@children) {
429 $lookup->{$sym} = [];
430 for my $child (@children) {
431 my $child_id = $child->att('state');
432 my $child_sym = $states_hash->{$states_id}->{$child_id};
433 if ( not defined $child_id ) {
434 throw(
435 'API' =>
436 "Need reference to fundamental state by set '$id'",
437 'line' => $self->{'_twig'}->parser->current_line
438 );
439 }
440 if ( not exists $states_hash->{$states_id}->{$child_id} ) {
441 throw(
442 'API' =>
443 "Couldn't find fundamental state '$child_id'",
444 'line' => $self->{'_twig'}->parser->current_line
445 );
446 }
447 push @{ $lookup->{$sym} }, $child_sym;
448 }
449 }
450 else {
451 $lookup->{$sym} = [$sym];
452 }
453 };
454  
455 # here we iterate of state definitions, i.e. each
456 # $state_elt <state/> describes what symbol that state has
457 for my $state_elt ( $states_elt->children('state') ) {
458 $process_state->($state_elt);
459 }
460 for my $polymorphic_state_set_elt (
461 $states_elt->children('polymorphic_state_set') )
462 {
463 $process_state->($polymorphic_state_set_elt);
464 }
465 for my $uncertain_state_set_elt (
466 $states_elt->children('uncertain_state_set') )
467 {
468 $process_state->($uncertain_state_set_elt);
469 }
470 }
471  
472 # finally, we iterate over column definitions which may
473 # reuse state sets.
474 for my $char_elt ( $format_elt->children('char') ) {
475 my $char_id = $char_elt->att('id');
476 my $states_idref = $char_elt->att('states');
477  
478 # $states_idref can be false (which in this case is always
479 # the same as undefined, because xml id's cannot be integers,
480 # so an id of "0" is impossible). This would be the case if
481 # the characters element is for continuous characters, which
482 # can have column definitions, but not state sets (which would
483 # have to be of infinite size).
484 if ($states_idref) {
485 $chars_hash->{$char_id} = $states_hash->{$states_idref};
486 }
487  
488 # in order to keep characters ordered (including in sparse
489 # matrices) we can't just use a hash, need an array as
490 # well
491 push @$states_array, $char_id;
492 }
493 return ( $chars_hash, $states_array, $lookup );
494 }
495  
496 sub _process_row {
497 my ( $self, $row_elt, $def_hash, $def_array, %args ) = @_;
498  
499 # create datum object
500 my ( $row_obj, $row_id ) = $self->_obj_from_elt( $row_elt, 'datum', %args );
501  
502 # check granularity, process accordingly
503 if ( $row_elt->children('cell') ) {
504 return ( $row_obj,
505 $self->_process_cells( $row_elt, $def_hash, $def_array ) );
506 }
507 else {
508 my $type;
509 if ( $args{'-type'} ) {
510 $type = $args{'-type'};
511 }
512 elsif ( $args{'-type_object'} ) {
513 $type = $args{'-type_object'}->get_type;
514 }
515 else {
516 $type = 'Standard';
517 }
518 return ( $row_obj, $self->_process_seqs( $row_elt, $def_hash, $type ) );
519 }
520 }
521  
522 sub _process_cells {
523 my ( $self, $row_elt, $def_hash, $def_array ) = @_;
524 my $chars_hash = {};
525  
526 # loop over <cell/> elements
527 my $i = 0;
528 for my $cell_elt ( $row_elt->children('cell') ) {
529 my $char_idref = $cell_elt->att('char');
530 my $state_idref = $cell_elt->att('state');
531 if ( not defined $char_idref ) {
532 $char_idref = $i++;
533 }
534 my $state;
535  
536 # may not exist for types without format block
537 if ( exists $def_hash->{$char_idref} ) {
538 my $lookup = $def_hash->{$char_idref};
539  
540 # may not be a hash for continuous states
541 if ( looks_like_instance( $lookup, 'HASH' )
542 and defined $lookup->{$state_idref} )
543 {
544 $state = $lookup->{$state_idref};
545 }
546 else {
547 $state = $state_idref;
548 }
549 }
550 else {
551 $state = $state_idref;
552 }
553 $chars_hash->{$char_idref} = $state;
554 }
555 return $chars_hash;
556 }
557  
558 sub _process_seqs {
559 my ( $self, $row_elt, $def_hash, $type ) = @_;
560 my $chars_hash = {};
561 my @seq_list;
562 if ( my $seq_string = $row_elt->first_child_text('seq') ) {
563 if ( $type =~ m/^(DNA|RNA|PROTEIN|RESTRICTION)/i ) {
564 $seq_string =~ s/\s//g;
565 @seq_list = split //, $seq_string;
566 }
567 else {
568 @seq_list = split /\s+/, $seq_string;
569 }
570 for my $i ( 0 .. $#seq_list ) {
571 $chars_hash->{$i} = $seq_list[$i];
572 }
573 }
574 return $chars_hash;
575 }
576  
577 sub _handle_forest {
578 my ( $twig, $trees_elt, $self ) = @_;
579  
580 # instantiate forest object, set id, taxa and name
581 my @args = ( $trees_elt, 'forest' );
582 my ( $forest_obj, $forest_id ) = $self->_obj_from_elt(@args);
583 my $taxa_idref = $self->_set_otus_for_obj( $trees_elt, $forest_obj );
584  
585 # loop over tree elements
586 for my $tree_elt ( $trees_elt->children('tree') ) {
587  
588 # for now we can only process true trees, not networks,
589 # which would require extensions to the Bio::Phylo API
590 my $type = $tree_elt->att('xsi:type');
591 if ( $type =~ qr/Tree$/ ) {
592  
593 # instantiate the tree object, set name and id
594 @args = ( $tree_elt, 'tree' );
595 my ( $tree_obj, $tree_id ) = $self->_obj_from_elt(@args);
596  
597 # things to pass to process methods
598 @args = ( $tree_elt, $tree_obj, $taxa_idref );
599 $forest_obj->insert( $self->_process_listtree(@args) );
600 }
601  
602 # TODO fixme
603 else {
604 $self->_logger->warn( $self->_pos . " Can't process networks yet" );
605 }
606 }
607
608 # process tree sets
609 $self->_process_set($trees_elt,$forest_obj);
610  
611 push @{ $self->{'_blocks'} }, $forest_obj;
612 }
613  
614 sub _process_listtree {
615 my ( $self, $tree_elt, $tree_obj, $taxa_idref ) = @_;
616 my $tree_id = $tree_elt->att('id');
617  
618 # this is going to be our lookup to get things back by id
619 my ( %node_by_id, %parent_of );
620  
621 # loop over nodes
622 for my $node_elt ( $tree_elt->children('node') ) {
623 my ( $node_obj, $node_id ) = $self->_obj_from_elt( $node_elt, 'node' );
624 $node_by_id{$node_id} = $node_obj;
625 $self->_set_otu_for_obj( $node_elt, $node_obj, $taxa_idref );
626 $tree_obj->insert($node_obj);
627 }
628  
629 # loop over branches
630 for my $edge_elt ( $tree_elt->children('edge') ) {
631 my $node_id = $edge_elt->att('target');
632 my $parent_id = $edge_elt->att('source');
633 my $edge_id = $edge_elt->att('id');
634  
635 # referential integrity check for target
636 if ( not exists $node_by_id{$node_id} ) {
637 throw(
638 'API' =>
639 "no target '$node_id' for edge '$edge_id' in tree '$tree_id'",
640 'line' => $self->{'_twig'}->parser->current_line
641 );
642 }
643  
644 # referential integrity check for source
645 if ( not exists $node_by_id{$parent_id} ) {
646 throw(
647 'API' =>
648 "no source '$parent_id' for edge '$edge_id' in tree '$tree_id'",
649 'line' => $self->{'_twig'}->parser->current_line
650 );
651 }
652 if ( not $node_by_id{$node_id}->get_parent ) {
653 $node_by_id{$node_id}->set_parent( $node_by_id{$parent_id} );
654 }
655 else {
656 throw(
657 'API' => sprintf(
658 "node '%s' already has parent '%s' in tree '%s'",
659 $node_id, $parent_id, $tree_id
660 ),
661 'line' => $self->{'_twig'}->parser->current_line
662 );
663 }
664 if ( defined( my $length = $edge_elt->att('length') ) ) {
665 $node_by_id{$node_id}->set_branch_length($length);
666 }
667 }
668  
669 # tree structure integrity check
670 my $orphan_count = 0;
671 for my $node_id ( keys %node_by_id ) {
672 $orphan_count++ if not $node_by_id{$node_id}->get_parent;
673 }
674 if ( $orphan_count == 0 ) {
675 throw(
676 'API' => "tree '$tree_id' has reticulations",
677 'line' => $self->{'_twig'}->parser->current_line
678 );
679 }
680 if ( $orphan_count > 1 ) {
681 throw(
682 'API' => "tree '$tree_id' has too many orphans",
683 'line' => $self->{'_twig'}->parser->current_line
684 );
685 }
686
687 # process node sets
688 $self->_process_set($tree_elt,$tree_obj);
689
690 return $tree_obj;
691 }
692  
693 sub _process_listnode {
694 my ( $self, $node_elt, $taxa_idref ) = @_;
695  
696 # instantiate internal node, set id and label
697 my ( $node_obj, $node_id ) = $self->_obj_from_elt( $node_elt, 'node' );
698 my $parent_id = $node_elt->att('parent');
699  
700 # link to taxon
701 if ( $node_elt->tag eq 'terminal' ) {
702 $self->_set_otu_for_obj( $node_elt, $node_obj, $taxa_idref );
703 }
704  
705 # always test for defined-ness on branch lengths! could be 0
706 my $branch_length;
707 if ( defined $node_elt->att('float') ) {
708 $branch_length = $node_elt->att('float');
709 }
710  
711 # TODO should really be mutually exclusive in schema, but isn't
712 elsif ( defined $node_elt->att('integer') ) {
713 $branch_length = $node_elt->att('integer');
714 }
715 if ( defined $branch_length ) {
716 $node_obj->set_branch_length($branch_length);
717 }
718 return $node_obj, $node_id, $parent_id;
719 }
720  
721 # this method is called from within _obj_from_elt
722 # to process RDFa metadata attachments embedded
723 # in an element that maps onto a Bio::Phylo object
724 sub _process_meta {
725 my ( $self, $meta_elt ) = @_;
726 my $predicate = $meta_elt->att('property') || $meta_elt->att('rel');
727 my $object = defined $meta_elt->att('content') # content can be 0
728 ? $meta_elt->att('content')
729 : $meta_elt->att('href');
730  
731 if ( $meta_elt->att('href') && $meta_elt->att('href') !~ m|http://|i ) {
732 $object = $self->_get_base_uri($meta_elt) . $object;
733 }
734 my $meta =
735 $self->_factory->create_meta( '-triple' => { $predicate => $object } );
736 for my $child_meta_elt ( $meta_elt->children() ) {
737 if ( $child_meta_elt->gi eq 'meta' ) {
738 $meta->add_meta( $self->_process_meta($child_meta_elt) );
739 }
740 else {
741 my $lit = Bio::Phylo::NeXML::Meta::XMLLiteral->new($child_meta_elt);
742 $meta->set_triple( $predicate => $lit );
743 }
744 }
745 return $meta;
746 }
747  
748 sub _get_base_uri {
749 my ( $self, $elt ) = @_;
750 while ( not $elt->att('xml:base') ) {
751 if ( $elt->parent ) {
752 $elt = $elt->parent;
753 }
754 else {
755 last;
756 }
757 }
758 return $elt->att('xml:base');
759 }
760 1;