| 1 | package Bio::Phylo::Parsers::Ubiosearch; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Bio::Phylo::Parsers::Abstract'; | ||
| 5 | use Bio::Phylo::Util::Dependency 'XML::Twig'; | ||
| 6 | use Bio::Phylo::Util::Logger; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Bio::Phylo::Parsers::Ubiosearch - Parser used by Bio::Phylo::IO, no serviceable parts inside | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | This module parses the XML that is returned by a uBio namebank search. An | ||
| 15 | example of such a record is here: L<http://www.ubio.org/webservices/examples/namebank_search.xml> | ||
| 16 | |||
| 17 | The parser creates a single L<Bio::Phylo::Taxa> object to which the metadata | ||
| 18 | on the search (time, date stamp, uBio version number) are attached. This taxa | ||
| 19 | object is then populated with L<Bio::Phylo::Taxa::Taxon> objects that are | ||
| 20 | annotated with the metadata for each search result. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | my $DCID = 'dc:identifier'; | ||
| 25 | my $DCSUB = 'dc:subject'; | ||
| 26 | |||
| 27 | my %predicate_for = ( | ||
| 28 | 'namebankID' => $DCID, | ||
| 29 | 'packageName' => $DCSUB, | ||
| 30 | 'rankName' => 'gla:rank', | ||
| 31 | ); | ||
| 32 | |||
| 33 | my %object_for = ( | ||
| 34 | 'namebankID' => sub { 'urn:lsid:ubio.org:namebank:' . shift }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | my %namespaces = ( | ||
| 38 | 'dc' => 'http://purl.org/dc/elements/1.1/', | ||
| 39 | 'ubio' => 'urn:lsid:ubio.org:predicates:', | ||
| 40 | 'gla' => 'urn:lsid:lsid.zoology.gla.ac.uk:predicates:', | ||
| 41 | ); | ||
| 42 | |||
| 43 | my $logger = Bio::Phylo::Util::Logger->new; | ||
| 44 | |||
| 45 | sub _parse { | ||
| 46 | my $self = shift; | ||
| 47 | my $fac = $self->_factory; | ||
| 48 | my $taxa = $fac->create_taxa( '-namespaces' => \%namespaces ); | ||
| 49 | XML::Twig->new( | ||
| 50 | 'twig_handlers' => { | ||
| 51 | 'serviceData' => sub { | ||
| 52 | my ( $twig, $elt ) = @_; | ||
| 53 | $self->_elt_handler( $elt, $taxa ); | ||
| 54 | }, | ||
| 55 | 'value' => sub { | ||
| 56 | my ( $twig, $elt ) = @_; | ||
| 57 | my $taxon = $fac->create_taxon; | ||
| 58 | $self->_elt_handler( $elt, $taxon ); | ||
| 59 | |||
| 60 | # copy record title to name field | ||
| 61 | if ( my $name = $taxon->get_meta_object($DCSUB) ) { | ||
| 62 | $taxon->set_name( $name ); | ||
| 63 | $logger->info("Copied $DCSUB to name field: $name"); | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | $logger->warn("Couldn't find $DCSUB"); | ||
| 67 | } | ||
| 68 | |||
| 69 | # copy namebank LSID to guid field and ubio:namebankIdentifier, | ||
| 70 | # set local namebank ID as value of dc:identifier | ||
| 71 | if ( my ($meta) = @{ $taxon->get_meta($DCID) } ) { | ||
| 72 | $logger->info("Meta object for $DCID is $meta"); | ||
| 73 | my $lsid = $meta->get_object; | ||
| 74 | $taxon->add_meta( $fac->create_meta( | ||
| 75 | '-triple' => { 'ubio:namebankIdentifier' => $lsid } | ||
| 76 | ) ); | ||
| 77 | my $id = $lsid; | ||
| 78 | $id =~ s/.+://; | ||
| 79 | $taxon->set_guid( $id ); | ||
| 80 | $taxon->remove_meta( $meta ); | ||
| 81 | $taxon->add_meta( $fac->create_meta( '-triple' => { $DCID => $id } ) ); | ||
| 82 | $logger->info("Processed $DCID annotation: $id"); | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | $logger->warn("Couldn't find $DCID annotation"); | ||
| 86 | } | ||
| 87 | $taxa->insert($taxon); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | )->parse( $self->_string ); | ||
| 91 | return $taxa; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub _elt_handler { | ||
| 95 | my ( $self, $elt, $obj ) = @_; | ||
| 96 | for my $child ( $elt->children ) { | ||
| 97 | my ( $key, $val ) = ( $child->tag, $child->text ); | ||
| 98 | my $predicate = $predicate_for{$key} || "ubio:${key}"; | ||
| 99 | my $object = $object_for{$key} ? $object_for{$key}->($val) : $val; | ||
| 100 | $logger->debug("Setting annotation $predicate => $object"); | ||
| 101 | $obj->add_meta( | ||
| 102 | $self->_factory->create_meta( | ||
| 103 | '-triple' => { $predicate => $object } | ||
| 104 | ) | ||
| 105 | ) | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | # podinherit_insert_token | ||
| 110 | |||
| 111 | =head1 SEE ALSO | ||
| 112 | |||
| 113 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 114 | for any user or developer questions and discussions. | ||
| 115 | |||
| 116 | =over | ||
| 117 | |||
| 118 | =item L<Bio::Phylo::IO> | ||
| 119 | |||
| 120 | The uBio search result parser is called by the L<Bio::Phylo::IO> object. | ||
| 121 | Look there to learn more about parsing. | ||
| 122 | |||
| 123 | =item L<Bio::Phylo::Manual> | ||
| 124 | |||
| 125 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com> | ||
| 126 | |||
| 127 | =back | ||
| 128 | |||
| 129 | =head1 CITATION | ||
| 130 | |||
| 131 | If you use Bio::Phylo in published research, please cite it: | ||
| 132 | |||
| 133 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 134 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 135 | I<BMC Bioinformatics> B<12>:63. | ||
| 136 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | 1; |