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