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