| 1 | package Bio::Phylo::Parsers::Cdao; | ||
| 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 :namespaces :objecttypes'; | ||
| 7 | use Bio::Phylo::Util::Dependency qw'RDF::Trine::Node::Resource RDF::Query'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Bio::Phylo::Parsers::Cdao - Parser used by Bio::Phylo::IO, no serviceable parts inside | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This module is used for parsing CDAO RDF. The implementation is incomplete, | ||
| 16 | especially for character state matrices (trees and OTUs work fine). | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | my $ns_cdao = _NS_CDAO_; | ||
| 21 | my $ns_rdf = _NS_RDF_; | ||
| 22 | my $ns_rdfs = _NS_RDFS_; | ||
| 23 | |||
| 24 | my %prefix_for_ns = ( | ||
| 25 | $ns_cdao => 'cdao', | ||
| 26 | $ns_rdf => 'rdf', | ||
| 27 | $ns_rdfs => 'rdfs', | ||
| 28 | ); | ||
| 29 | |||
| 30 | my %objects; | ||
| 31 | |||
| 32 | my $prefixes = <<"PREFIXES"; | ||
| 33 | PREFIX rdf: <${ns_rdf}> | ||
| 34 | PREFIX cdao: <${ns_cdao}> | ||
| 35 | PREFIX rdfs: <${ns_rdfs}> | ||
| 36 | PREFIXES | ||
| 37 | |||
| 38 | my $query = <<"QUERY"; | ||
| 39 | ${prefixes} | ||
| 40 | SELECT | ||
| 41 | ?subject | ||
| 42 | WHERE { | ||
| 43 | ?subject rdf:type cdao:%s | ||
| 44 | } | ||
| 45 | QUERY | ||
| 46 | |||
| 47 | my $subclass = <<"SUBCLASS"; | ||
| 48 | ${prefixes} | ||
| 49 | SELECT | ||
| 50 | ?subject | ||
| 51 | WHERE { | ||
| 52 | ?subject rdfs:subClassOf cdao:%s | ||
| 53 | } | ||
| 54 | SUBCLASS | ||
| 55 | |||
| 56 | my $states = <<"STATES"; | ||
| 57 | ${prefixes} | ||
| 58 | SELECT | ||
| 59 | ?subject ?stateset ?state ?label | ||
| 60 | WHERE { | ||
| 61 | ?subject cdao:belongs_to_TU <%s> . | ||
| 62 | ?subject cdao:belongs_to_Character <%s> . | ||
| 63 | ?subject cdao:has_%sState ?state . | ||
| 64 | ?state rdfs:label ?label . | ||
| 65 | ?state rdf:type ?stateset | ||
| 66 | } | ||
| 67 | STATES | ||
| 68 | |||
| 69 | sub _parse { | ||
| 70 | warn "***Please note: implementation is incomplete, character data are not yet read correctly.***\n"; | ||
| 71 | my $self = shift; | ||
| 72 | %objects = (); | ||
| 73 | $self->_args->{'-opts'} = { | ||
| 74 | 'lang' => 'sparql', | ||
| 75 | 'base' => $self->_args->{'-base'}, | ||
| 76 | 'update' => 0, | ||
| 77 | 'load_data' => 0, | ||
| 78 | }; | ||
| 79 | $self->_project->set_base_uri($self->_args->{'-base'}); | ||
| 80 | $self->_process_tus; | ||
| 81 | $self->_process_trees; | ||
| 82 | $self->_process_nodes; | ||
| 83 | $self->_process_edges; | ||
| 84 | $self->_process_matrices; | ||
| 85 | |||
| 86 | my $proj = $self->_project; | ||
| 87 | my @objects = ( @{ $proj->get_taxa }, @{ $proj->get_forests }, @{ $proj->get_matrices } ); | ||
| 88 | $proj->clear; | ||
| 89 | return @objects; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub _object_from_resource { | ||
| 93 | my ( $self, $resource, $creator ) = @_; | ||
| 94 | my $fac = $self->_factory; | ||
| 95 | my $base = $self->_args->{'-base'}; | ||
| 96 | my $uri = $resource->value; | ||
| 97 | my $id = $uri; | ||
| 98 | $id =~ s/^\Q$base\E#?//; | ||
| 99 | my $object = $fac->$creator( '-guid' => $id, '-xml_id' => $id ); | ||
| 100 | my $iterator = $self->_args->{'-model'}->get_statements($resource,undef,undef); | ||
| 101 | while ( my $inner = $iterator->next ) { | ||
| 102 | my ( $predicate, $value ) = ( $inner->predicate, $inner->object ); | ||
| 103 | $self->_process_annotation( $predicate->value, $value->value, $object ); | ||
| 104 | |||
| 105 | } | ||
| 106 | $objects{$uri} = $object; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _parse_predicate { | ||
| 110 | my ( $self, $predicate ) = @_; | ||
| 111 | # attempt to split URI in namespace and term | ||
| 112 | my ( $ns, $term ); | ||
| 113 | |||
| 114 | # this is for cases where the term is referenced as somewhere inside | ||
| 115 | # an ontology using an anchor '#', e.g. in CDAO | ||
| 116 | if ( $predicate =~ m/^(.+#)(.+?)$/ ) { | ||
| 117 | ( $ns, $term ) = ( $1, $2 ); | ||
| 118 | } | ||
| 119 | |||
| 120 | # this is for cases where the term is a path fragment inside a namespace, | ||
| 121 | # i.e. preceded by a '/', as in dublin core | ||
| 122 | elsif ( $predicate =~ m/^(.+\/)([^\/]+?)$/ ) { | ||
| 123 | ( $ns, $term ) = ( $1, $2 ); | ||
| 124 | } | ||
| 125 | |||
| 126 | # this is for cases where the term is relative to a urn:, i.e. preceded | ||
| 127 | # by a ':', as in the uBio predicates | ||
| 128 | elsif ( $predicate =~ m/^(.+:)([^:]+?)$/ ) { | ||
| 129 | ( $ns, $term ) = ( $1, $2 ); | ||
| 130 | } | ||
| 131 | |||
| 132 | else { | ||
| 133 | $self->_logger->warn("Can't parse URI $predicate"); | ||
| 134 | } | ||
| 135 | return $ns, $term; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _process_annotation { | ||
| 139 | my ( $self, $predicate, $value, $object ) = @_; | ||
| 140 | my $fac = $self->_factory; | ||
| 141 | $predicate =~ s/^<(.+)>$/$1/; | ||
| 142 | return if $predicate eq _NS_RDF_ . 'type'; | ||
| 143 | |||
| 144 | # attempt to split URI in namespace and term | ||
| 145 | my ( $ns, $term ) = $self->_parse_predicate( $predicate ); | ||
| 146 | |||
| 147 | # check to see if we have a prefix for that namespace, or make one | ||
| 148 | my $prefix = $prefix_for_ns{$ns} || 'ns' . scalar(keys %prefix_for_ns); | ||
| 149 | $prefix_for_ns{$prefix} = $ns; | ||
| 150 | |||
| 151 | # maybe we know how to deal with this in the API | ||
| 152 | if ( "${prefix}:${term}" eq 'rdfs:label' ) { | ||
| 153 | $object->set_name( $value ); | ||
| 154 | return; | ||
| 155 | } | ||
| 156 | if ( "${prefix}:${term}" eq 'cdao:represents_TU' ) { | ||
| 157 | $object->set_taxon( $objects{$value} ); | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | if ( "${prefix}:${term}" eq 'cdao:has_Ancestor' ) { | ||
| 161 | return; # don't need this, will reconstruct from edge links | ||
| 162 | } | ||
| 163 | if ( "${prefix}:${term}" eq 'cdao:has_Root' ) { | ||
| 164 | return; # don't need this, will be obvious from whether tree is rooted | ||
| 165 | } | ||
| 166 | |||
| 167 | # attach annotation | ||
| 168 | $object->set_namespaces( $prefix => $ns ); | ||
| 169 | $object->add_meta( | ||
| 170 | $fac->create_meta( | ||
| 171 | '-triple' => { "${prefix}:${term}" => $value } | ||
| 172 | ) | ||
| 173 | ); | ||
| 174 | } | ||
| 175 | |||
| 176 | sub _do_query { | ||
| 177 | my ( $self, $type, $type_query ) = @_; | ||
| 178 | $type_query = $query unless $type_query; | ||
| 179 | my $sth = RDF::Query->new( sprintf($type_query, $type), $self->_args->{'-opts'} ); | ||
| 180 | return $sth->execute( $self->_args->{'-model'} ); | ||
| 181 | } | ||
| 182 | |||
| 183 | sub _process_matrices { | ||
| 184 | my $self = shift; | ||
| 185 | my $fac = $self->_factory; | ||
| 186 | my $model = $self->_args->{'-model'}; | ||
| 187 | my $iter = $self->_do_query('CharacterStateDataMatrix'); | ||
| 188 | while( my $row = $iter->next ) { | ||
| 189 | my $subject = $row->{'subject'}; | ||
| 190 | my $matrix = $self->_object_from_resource( $subject, 'create_matrix' ); | ||
| 191 | my ($taxa) = @{ $self->_project->get_taxa }; | ||
| 192 | $matrix->set_taxa($taxa); | ||
| 193 | |||
| 194 | # create rows for taxa | ||
| 195 | my ( $rowlist, %row ) = $self->_create_rows($matrix); | ||
| 196 | |||
| 197 | # create columns | ||
| 198 | my ( $charlist, %char ) = $self->_create_characters($matrix); | ||
| 199 | |||
| 200 | # maps CDAO state type predicates to Bio::Phylo matrix types | ||
| 201 | my %types = ( | ||
| 202 | 'Nucleotide_' => 'dna', | ||
| 203 | 'Continuous_' => 'continuous', | ||
| 204 | 'Standard_' => 'standard', | ||
| 205 | '' => 'standard', | ||
| 206 | ); | ||
| 207 | my $datatype; | ||
| 208 | |||
| 209 | for my $row_uri ( @{ $rowlist } ) { | ||
| 210 | for my $col_uri ( @{ $charlist } ) { | ||
| 211 | if ( not $datatype ) { | ||
| 212 | TYPE_SEARCH : for my $predicate ( keys %types ) { | ||
| 213 | my $state_query = sprintf($states, $row_uri, $col_uri, $predicate ); | ||
| 214 | my $sth = RDF::Query->new( $state_query, $self->_args->{'-opts'} ); | ||
| 215 | my $state_iterator = $sth->execute( $self->_args->{'-model'} ); | ||
| 216 | if ( my $state = $state_iterator->next ) { | ||
| 217 | $datatype = $predicate; | ||
| 218 | $matrix->set_type($types{$predicate}); | ||
| 219 | last TYPE_SEARCH; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | else { | ||
| 224 | my $state_query = sprintf($states, $row_uri, $col_uri, $datatype ); | ||
| 225 | my $sth = RDF::Query->new( $state_query, $self->_args->{'-opts'} ); | ||
| 226 | my $state_iterator = $sth->execute( $self->_args->{'-model'} ); | ||
| 227 | while ( my $state = $state_iterator->next ) { | ||
| 228 | if ( my $val = $state->{label}->value ) { | ||
| 229 | $row{$row_uri}->insert($val); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | } | ||
| 233 | } | ||
| 234 | } | ||
| 235 | $self->_logger->debug($matrix->to_nexus); | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | sub _create_rows { | ||
| 240 | my ( $self, $matrix ) = @_; | ||
| 241 | my $fac = $self->_factory; | ||
| 242 | my ( %row, @rowlist ); | ||
| 243 | my $tu_metas = $matrix->get_meta('cdao:has_TU'); | ||
| 244 | for my $tu_meta ( @{ $tu_metas } ) { | ||
| 245 | my $tu_uri = $tu_meta->get_object; | ||
| 246 | my $row = $fac->create_datum( | ||
| 247 | '-taxon' => $objects{$tu_uri}, | ||
| 248 | '-name' => $objects{$tu_uri}->get_name, | ||
| 249 | ); | ||
| 250 | $row{$tu_uri} = $row; | ||
| 251 | $matrix->insert( $row ); | ||
| 252 | push @rowlist, $tu_uri; | ||
| 253 | } | ||
| 254 | return \@rowlist, %row; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub _create_characters { | ||
| 258 | my ( $self, $matrix ) = @_; | ||
| 259 | my ( %char, @charlist ); | ||
| 260 | my $characters = $matrix->get_characters; | ||
| 261 | my $char_metas = $matrix->get_meta('cdao:has_Character'); | ||
| 262 | for my $char_meta ( @{ $char_metas } ) { | ||
| 263 | my $char_uri = $char_meta->get_object; | ||
| 264 | my $char_resource = RDF::Trine::Node::Resource->new( $char_uri ); | ||
| 265 | my $char = $self->_object_from_resource( $char_resource, 'create_character' ); | ||
| 266 | $char{$char_uri} = $char; | ||
| 267 | $characters->insert($char); | ||
| 268 | push @charlist, $char_uri; | ||
| 269 | } | ||
| 270 | return \@charlist, %char; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub _process_tus { | ||
| 274 | my $self = shift; | ||
| 275 | my $fac = $self->_factory; | ||
| 276 | my $taxa = $fac->create_taxa; | ||
| 277 | my $model = $self->_args->{'-model'}; | ||
| 278 | my $iter = $self->_do_query('TU'); | ||
| 279 | while ( my $row = $iter->next ) { | ||
| 280 | my $subject = $row->{'subject'}; | ||
| 281 | my $taxon = $self->_object_from_resource( $subject, 'create_taxon' ); | ||
| 282 | $taxa->insert($taxon); | ||
| 283 | } | ||
| 284 | $self->_project->insert($taxa); | ||
| 285 | } | ||
| 286 | |||
| 287 | sub _process_trees { | ||
| 288 | my $self = shift; | ||
| 289 | my $fac = $self->_factory; | ||
| 290 | my ($taxa) = @{ $self->_project->get_items(_TAXA_) }; | ||
| 291 | my $forest = $fac->create_forest( '-taxa' => $taxa ); | ||
| 292 | my $model = $self->_args->{'-model'}; | ||
| 293 | |||
| 294 | # process rooted trees | ||
| 295 | my $rooted_iter = $self->_do_query('RootedTree'); | ||
| 296 | while( my $row = $rooted_iter->next ) { | ||
| 297 | my $subject = $row->{'subject'}; | ||
| 298 | my $tree = $self->_object_from_resource( $subject, 'create_tree' ); | ||
| 299 | $forest->insert($tree); | ||
| 300 | } | ||
| 301 | |||
| 302 | # process unrooted trees | ||
| 303 | my $unrooted_iter = $self->_do_query('UnrootedTree'); | ||
| 304 | while( my $row = $unrooted_iter->next ) { | ||
| 305 | my $subject = $row->{'subject'}; | ||
| 306 | my $tree = $self->_object_from_resource( $subject, 'create_tree' ); | ||
| 307 | $tree->set_as_unrooted; | ||
| 308 | $forest->insert($tree); | ||
| 309 | } | ||
| 310 | |||
| 311 | $self->_project->insert($forest); | ||
| 312 | } | ||
| 313 | |||
| 314 | sub _process_nodes { | ||
| 315 | my $self = shift; | ||
| 316 | my $model = $self->_args->{'-model'}; | ||
| 317 | my $logger = $self->_logger; | ||
| 318 | |||
| 319 | # this only assigns nodes to a tree object but doesn't resolve | ||
| 320 | # topology, that's done in _process_edges | ||
| 321 | my $node_iter = $self->_do_query('Node'); | ||
| 322 | while( my $row = $node_iter->next ) { | ||
| 323 | my $subject = $row->{'subject'}; | ||
| 324 | my $node = $self->_object_from_resource( $subject, 'create_node' ); | ||
| 325 | my ($value) = @{ $node->get_meta('cdao:belongs_to_Tree') }; | ||
| 326 | $objects{$value->get_object}->insert($node) if $objects{$value->get_object}; | ||
| 327 | $node->remove_meta($value); | ||
| 328 | } | ||
| 329 | } | ||
| 330 | |||
| 331 | sub _process_edges { | ||
| 332 | my $self = shift; | ||
| 333 | my $model = $self->_args->{'-model'}; | ||
| 334 | my $logger = $self->_logger; | ||
| 335 | |||
| 336 | my $edge_iter = $self->_do_query('DirectedEdge'); | ||
| 337 | while( my $row = $edge_iter->next ) { | ||
| 338 | my $subject = $row->{'subject'}; | ||
| 339 | my $edge_statements = $model->get_statements($subject); | ||
| 340 | my ( $parent_uri, $child_uri, $branch_length ); | ||
| 341 | LINK: while( my $st = $edge_statements->next ) { | ||
| 342 | my $predicate = $st->predicate->value; | ||
| 343 | $logger->debug($predicate); | ||
| 344 | if ( $predicate eq "${ns_cdao}has_Parent_Node" ) { | ||
| 345 | $parent_uri = $st->object->value; | ||
| 346 | } | ||
| 347 | elsif ( $predicate eq "${ns_cdao}has_Child_Node" ) { | ||
| 348 | $child_uri = $st->object->value; | ||
| 349 | } | ||
| 350 | elsif ( $predicate eq "${ns_cdao}has_Annotation" ) { | ||
| 351 | my $annotation_statements = $model->get_statements($st->object); | ||
| 352 | ANNO: while(my $anno = $annotation_statements->next) { | ||
| 353 | my $anno_pre = $anno->predicate->value; | ||
| 354 | if ( $anno_pre =~ /^\Q${ns_cdao}\Ehas_(?:Int|Float)_Value/ ) { | ||
| 355 | $branch_length = $anno->object->value; | ||
| 356 | last ANNO; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | } | ||
| 360 | last LINK if $parent_uri && $child_uri; | ||
| 361 | } | ||
| 362 | $logger->debug("Parent: $parent_uri Child: $child_uri"); | ||
| 363 | $objects{$parent_uri}->set_child($objects{$child_uri}); | ||
| 364 | $objects{$child_uri}->set_branch_length($branch_length) if defined $branch_length; | ||
| 365 | } | ||
| 366 | } | ||
| 367 | |||
| 368 | # podinherit_insert_token | ||
| 369 | |||
| 370 | =head1 SEE ALSO | ||
| 371 | |||
| 372 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 373 | for any user or developer questions and discussions. | ||
| 374 | |||
| 375 | =over | ||
| 376 | |||
| 377 | =item L<Bio::Phylo::IO> | ||
| 378 | |||
| 379 | The CDAO parser is called by the L<Bio::Phylo::IO> object. | ||
| 380 | Look there for examples. | ||
| 381 | |||
| 382 | =item L<Bio::Phylo::Manual> | ||
| 383 | |||
| 384 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>. | ||
| 385 | |||
| 386 | =back | ||
| 387 | |||
| 388 | =head1 CITATION | ||
| 389 | |||
| 390 | If you use Bio::Phylo in published research, please cite it: | ||
| 391 | |||
| 392 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 393 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 394 | I<BMC Bioinformatics> B<12>:63. | ||
| 395 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 396 | |||
| 397 | =cut | ||
| 398 | |||
| 399 | 1; |