| 1 | package Bio::Phylo::Parsers::Ubiocbmeta; | ||
| 2 | use base 'Bio::Phylo::Parsers::Abstract'; | ||
| 3 | use Bio::Phylo::Util::Dependency 'XML::Twig'; | ||
| 4 | use Bio::Phylo::NeXML::Entities '/entities/'; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Bio::Phylo::Parsers::Ubiocbmeta - Parser used by Bio::Phylo::IO, no serviceable parts inside | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | This module parses RDF metadata for uBio classification bank records. An example | ||
| 15 | of such a record is here: | ||
| 16 | L<http://www.ubio.org/authority/metadata.php?lsid=urn:lsid:ubio.org:classificationbank:11168417> | ||
| 17 | |||
| 18 | The parser creates a single L<Bio::Phylo::Taxa::Taxon> object to which all | ||
| 19 | metadata are attached as L<Bio::Phylo::NeXML::Meta> objects. This taxon is | ||
| 20 | embedded in a taxa block, or optionally in a L<Bio::Phylo::Project> object | ||
| 21 | if the C<-as_project> flag was provided to the call to C<parse()>. | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | # attach namespaces from element to object | ||
| 26 | sub _copy_namespaces { | ||
| 27 | my ( $self, $elt, $obj ) = @_; | ||
| 28 | for my $att_name ( $elt->att_names ) { | ||
| 29 | if ( $att_name =~ /xmlns:(\S+)/ ) { | ||
| 30 | my $prefix = $1; | ||
| 31 | my $ns = $elt->att($att_name); | ||
| 32 | $obj->set_namespaces( $prefix => $ns ); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | # prettify the output, make sure there is a name and description | ||
| 38 | sub _rss_prettify { | ||
| 39 | my ( $self, $obj ) = @_; | ||
| 40 | $obj->set_name( $obj->get_meta_object('dc:title') ); | ||
| 41 | $obj->set_desc( | ||
| 42 | 'Rank: ' | ||
| 43 | . $obj->get_meta_object('gla:rank') | ||
| 44 | . ', Classification: ' | ||
| 45 | . $obj->get_meta_object('ubio:classificationName') | ||
| 46 | ); | ||
| 47 | } | ||
| 48 | |||
| 49 | # copy the local part of the LSID to the guid field and dc:identifier | ||
| 50 | sub _copy_identifiers { | ||
| 51 | my ( $self, $elt, $obj ) = @_; | ||
| 52 | if ( $elt->att('rdf:about') =~ /(\d+)$/ ) { | ||
| 53 | my $namebankID = $1; | ||
| 54 | $obj->set_guid($namebankID); | ||
| 55 | $obj->add_meta( $self->_factory->create_meta( | ||
| 56 | '-triple' => { 'dc:identifier' => $namebankID } | ||
| 57 | ) ); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | # process annotation values: xml escape strings and urls, return | ||
| 62 | # child nodes as is | ||
| 63 | sub _process_value { | ||
| 64 | my ( $self, $elt ) = @_; | ||
| 65 | my $val; | ||
| 66 | if ( $val = $elt->att('rdf:resource') ) { | ||
| 67 | return encode_entities($val); | ||
| 68 | } | ||
| 69 | elsif ( ($val) = $elt->children('rdf:Seq') ) { | ||
| 70 | return $val; | ||
| 71 | } | ||
| 72 | else { | ||
| 73 | return encode_entities($elt->text); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _parse { | ||
| 78 | my $self = shift; | ||
| 79 | my $fac = $self->_factory; | ||
| 80 | my $taxa = $fac->create_taxa; | ||
| 81 | XML::Twig->new( | ||
| 82 | 'twig_handlers' => { | ||
| 83 | 'rdf:RDF' => sub { | ||
| 84 | my ( $twig, $elt ) = @_; | ||
| 85 | my $taxon = $fac->create_taxon; | ||
| 86 | |||
| 87 | # attach namespaces from root element to taxon | ||
| 88 | $self->_copy_namespaces($elt,$taxon); | ||
| 89 | |||
| 90 | # $child contains all the metadata to parse | ||
| 91 | my ($child) = $elt->children('rdf:Description'); | ||
| 92 | KEY: for my $meta_elt ( $child->children ) { | ||
| 93 | my $key = $meta_elt->tag; | ||
| 94 | next KEY if $key eq '#PCDATA'; | ||
| 95 | |||
| 96 | # process annotation value | ||
| 97 | my $val = $self->_process_value($meta_elt); | ||
| 98 | |||
| 99 | # we will use the numerical part as dc:identifier | ||
| 100 | if ( $key eq 'dc:identifier' ) { | ||
| 101 | $key = 'ubio:classificationbankIdentifier'; | ||
| 102 | } | ||
| 103 | $taxon->add_meta( $fac->create_meta( | ||
| 104 | '-triple' => { $key => $val } | ||
| 105 | ) ); | ||
| 106 | } | ||
| 107 | |||
| 108 | # parse the rdf:about lsid, use the numerical part as | ||
| 109 | # guid and dc:identifier | ||
| 110 | $self->_copy_identifiers($child,$taxon); | ||
| 111 | |||
| 112 | # prettify the output, make sure there is a name and description | ||
| 113 | $self->_rss_prettify($taxon); | ||
| 114 | |||
| 115 | $taxa->insert($taxon); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | )->parse($self->_string); | ||
| 119 | return $taxa; | ||
| 120 | } | ||
| 121 | |||
| 122 | # podinherit_insert_token | ||
| 123 | |||
| 124 | =head1 SEE ALSO | ||
| 125 | |||
| 126 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 127 | for any user or developer questions and discussions. | ||
| 128 | |||
| 129 | =over | ||
| 130 | |||
| 131 | =item L<Bio::Phylo::IO> | ||
| 132 | |||
| 133 | The uBio classification bank metadata parser is called by the L<Bio::Phylo::IO> | ||
| 134 | object. Look there to learn more about parsing. | ||
| 135 | |||
| 136 | =item L<Bio::Phylo::Manual> | ||
| 137 | |||
| 138 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com> | ||
| 139 | |||
| 140 | =back | ||
| 141 | |||
| 142 | =head1 CITATION | ||
| 143 | |||
| 144 | If you use Bio::Phylo in published research, please cite it: | ||
| 145 | |||
| 146 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 147 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 148 | I<BMC Bioinformatics> B<12>:63. | ||
| 149 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | 1; |