| 1 | package Bio::Phylo::Parsers::Ubiometa; | ||
| 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::Ubiometa - Parser used by Bio::Phylo::IO, no serviceable | ||
| 11 | parts inside | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This module parses RDF metadata for uBio namebank records. An example of such a | ||
| 16 | record is here: L<http://www.ubio.org/authority/metadata.php?lsid=urn:lsid:ubio.org:namebank:2481730> | ||
| 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 | # prettify the output, make sure there is a name and description | ||
| 26 | sub _rss_prettify { | ||
| 27 | my ( $self, $taxon ) = @_; | ||
| 28 | $taxon->set_name( $taxon->get_meta_object('dc:subject') ); | ||
| 29 | $taxon->set_desc( | ||
| 30 | $taxon->get_meta_object('dc:type') | ||
| 31 | . ', Rank: ' | ||
| 32 | . $taxon->get_meta_object('gla:rank') | ||
| 33 | . ', Status: ' | ||
| 34 | . $taxon->get_meta_object('ubio:lexicalStatus') | ||
| 35 | ); | ||
| 36 | } | ||
| 37 | |||
| 38 | # copy the local part of the LSID to the guid field and dc:identifier | ||
| 39 | sub _copy_identifiers { | ||
| 40 | my ( $self, $elt, $obj ) = @_; | ||
| 41 | if ( $elt->att('rdf:about') =~ /(\d+)$/ ) { | ||
| 42 | my $namebankID = $1; | ||
| 43 | $obj->set_guid($namebankID); | ||
| 44 | $obj->add_meta( $self->_factory->create_meta( | ||
| 45 | '-triple' => { 'dc:identifier' => $namebankID } | ||
| 46 | ) ); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | # attach namespaces from element to object | ||
| 51 | sub _copy_namespaces { | ||
| 52 | my ( $self, $elt, $obj ) = @_; | ||
| 53 | for my $att_name ( $elt->att_names ) { | ||
| 54 | if ( $att_name =~ /xmlns:(\S+)/ ) { | ||
| 55 | my $prefix = $1; | ||
| 56 | my $ns = $elt->att($att_name); | ||
| 57 | $obj->set_namespaces( $prefix => $ns ); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _parse { | ||
| 63 | my $self = shift; | ||
| 64 | my $fac = $self->_factory; | ||
| 65 | my $taxa = $fac->create_taxa; | ||
| 66 | XML::Twig->new( | ||
| 67 | 'twig_handlers' => { | ||
| 68 | 'rdf:RDF' => sub { | ||
| 69 | my ( $twig, $elt ) = @_; | ||
| 70 | my $taxon = $fac->create_taxon; | ||
| 71 | |||
| 72 | # attach namespaces from root element to taxon | ||
| 73 | $self->_copy_namespaces($elt,$taxon); | ||
| 74 | |||
| 75 | # attach metadata | ||
| 76 | my ($child) = $elt->children('rdf:Description'); | ||
| 77 | for my $meta_elt ( $child->children ) { | ||
| 78 | my $val = encode_entities( $meta_elt->att('rdf:resource') || $meta_elt->text ); | ||
| 79 | my $key = $meta_elt->tag; | ||
| 80 | if ( $key eq 'dc:identifier' ) { | ||
| 81 | $key = 'ubio:namebankIdentifier'; | ||
| 82 | } | ||
| 83 | $taxon->add_meta( $fac->create_meta( | ||
| 84 | '-triple' => { $key => $val } | ||
| 85 | ) ); | ||
| 86 | } | ||
| 87 | |||
| 88 | # parse the rdf:about lsid, use the numerical part as | ||
| 89 | # guid and dc:identifier | ||
| 90 | $self->_copy_identifiers($child,$taxon); | ||
| 91 | |||
| 92 | # prettify the output, make sure there is a name and description | ||
| 93 | $self->_rss_prettify($taxon); | ||
| 94 | |||
| 95 | $taxa->insert($taxon); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | )->parse($self->_string); | ||
| 99 | return $taxa; | ||
| 100 | } | ||
| 101 | |||
| 102 | # podinherit_insert_token | ||
| 103 | |||
| 104 | =head1 SEE ALSO | ||
| 105 | |||
| 106 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 107 | for any user or developer questions and discussions. | ||
| 108 | |||
| 109 | =over | ||
| 110 | |||
| 111 | =item L<Bio::Phylo::IO> | ||
| 112 | |||
| 113 | The uBio metadata parser is called by the L<Bio::Phylo::IO> object. | ||
| 114 | Look there to learn more about parsing. | ||
| 115 | |||
| 116 | =item L<Bio::Phylo::Manual> | ||
| 117 | |||
| 118 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com> | ||
| 119 | |||
| 120 | =back | ||
| 121 | |||
| 122 | =head1 CITATION | ||
| 123 | |||
| 124 | If you use Bio::Phylo in published research, please cite it: | ||
| 125 | |||
| 126 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 127 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 128 | I<BMC Bioinformatics> B<12>:63. | ||
| 129 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | 1; |