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