| 1 | package Bio::Phylo::PhyloWS::Service::UbioNameBank; | ||
| 2 | use strict; | ||
| 3 | use base 'Bio::Phylo::PhyloWS::Service'; | ||
| 4 | use constant UBIO_BASE => 'http://www.ubio.org/'; | ||
| 5 | use constant UBIO_SRCH => UBIO_BASE . 'browser/search.php?search_all='; | ||
| 6 | use constant UBIO_NMBK => UBIO_BASE . 'browser/details.php?namebankID='; | ||
| 7 | use constant RDFURL => UBIO_BASE . 'authority/metadata.php?lsid=urn:lsid:ubio.org:namebank:'; | ||
| 8 | use constant UBIOWS => UBIO_BASE . 'webservices/service.php?function=namebank_search&searchName=%s&sci=1&vern=1&keyCode=%s'; | ||
| 9 | use Bio::Phylo::Util::Dependency qw'XML::Twig LWP::UserAgent'; | ||
| 10 | use Bio::Phylo::Util::CONSTANT qw'looks_like_hash'; | ||
| 11 | use Bio::Phylo::Util::Exceptions 'throw'; | ||
| 12 | use Bio::Phylo::Util::Logger; | ||
| 13 | use Bio::Phylo::IO 'parse'; | ||
| 14 | use Bio::Phylo::Factory; | ||
| 15 | |||
| 16 | { | ||
| 17 | |||
| 18 | my $fac = Bio::Phylo::Factory->new; | ||
| 19 | my $logger = Bio::Phylo::Util::Logger->new; | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | Bio::Phylo::PhyloWS::Service::UbioNameBank - PhyloWS service wrapper for uBio | ||
| 24 | NameBank records | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | # inside a CGI script: | ||
| 29 | use CGI; | ||
| 30 | use Bio::Phylo::PhyloWS::Service::UbioNameBank; | ||
| 31 | |||
| 32 | # obtain a key code from http://www.ubio.org/index.php?pagename=form | ||
| 33 | # and define it as an environment variable: | ||
| 34 | $ENV{'UBIO_KEYCODE'} = '******'; | ||
| 35 | my $cgi = CGI->new; | ||
| 36 | my $service = Bio::Phylo::PhyloWS::Service::UbioNameBank->new( '-base_uri' => $url ); | ||
| 37 | $service->handle_request($cgi); | ||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | This is an example implementation of a PhyloWS service. The service | ||
| 42 | wraps around some of the uBio XML services described at | ||
| 43 | L<http://www.ubio.org/index.php?pagename=xml_services>. | ||
| 44 | |||
| 45 | Record lookups for this service return project objects | ||
| 46 | that capture the RDF metadata for a single NameBank record as semantic | ||
| 47 | annotations to a taxon object. An example of the sort of metadata that | ||
| 48 | can be expected is shown here: | ||
| 49 | L<http://www.ubio.org/authority/metadata.php?lsid=urn:lsid:ubio.org:namebank:2481730> | ||
| 50 | |||
| 51 | Queries on this service run namebank searches and return project objects | ||
| 52 | that capture the NameBank search XML (an example is shown here: | ||
| 53 | L<http://www.ubio.org/webservices/examples/namebank_search.xml>) | ||
| 54 | as semantic annotations to taxon objects. | ||
| 55 | |||
| 56 | URLs to this service that specify format=html in the query string redirect | ||
| 57 | to web pages on the uBio site at L<http://www.ubio.org>. The redirect | ||
| 58 | URLs either point to search result listings or to NameBank record pages, | ||
| 59 | depending on whether the redirect is for a record query or a record lookup, | ||
| 60 | respectively. | ||
| 61 | |||
| 62 | =head1 UBIO KEY CODES | ||
| 63 | |||
| 64 | B<Some functionality of this service requires a key code to the uBio API>. Such | ||
| 65 | key codes can be obtained from L<http://www.ubio.org/index.php?pagename=form>. | ||
| 66 | When deploying this service on a web server (e.g. as shown in the SYNOPSIS) this | ||
| 67 | code must be provided in an environment variable called C<UBIO_KEYCODE>. | ||
| 68 | |||
| 69 | =head1 METHODS | ||
| 70 | |||
| 71 | =head2 ACCESSORS | ||
| 72 | |||
| 73 | =over | ||
| 74 | |||
| 75 | =item get_record() | ||
| 76 | |||
| 77 | Gets a uBio namebank record by its id | ||
| 78 | |||
| 79 | Type : Accessor | ||
| 80 | Title : get_record | ||
| 81 | Usage : my $record = $obj->get_record( -guid => $guid ); | ||
| 82 | Function: Gets a uBio namebank record by its id | ||
| 83 | Returns : Bio::Phylo::Project | ||
| 84 | Args : Required: -guid => $guid | ||
| 85 | Comments: For the $guid argument, this method only cares | ||
| 86 | whether the last part of the argument is a series | ||
| 87 | of integers, which are understood to be namebank | ||
| 88 | identifiers | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub get_record { | ||
| 93 | my $self = shift; | ||
| 94 | my $proj; | ||
| 95 | if ( my %args = looks_like_hash @_ ) { | ||
| 96 | if ( my $guid = $args{'-guid'} && $args{'-guid'} =~ m|(\d+)$| ) { | ||
| 97 | |||
| 98 | # fetch and parse the metadata record | ||
| 99 | my $namebank_id = $1; | ||
| 100 | $logger->info("Going to fetch metadata for record $namebank_id"); | ||
| 101 | $proj = parse( | ||
| 102 | '-url' => RDFURL . $namebank_id, | ||
| 103 | '-format' => 'ubiometa', | ||
| 104 | '-as_project' => 1, | ||
| 105 | ); | ||
| 106 | |||
| 107 | # attach links back for rss | ||
| 108 | my $prefix = $self->get_url_prefix; | ||
| 109 | my ($taxa) = @{ $proj->get_taxa }; | ||
| 110 | $taxa->visit(sub{ | ||
| 111 | my $taxon = shift; | ||
| 112 | $taxon->set_link( $prefix . $taxon->get_guid ); | ||
| 113 | }) | ||
| 114 | } | ||
| 115 | else { | ||
| 116 | throw 'BadArgs' => "No parseable GUID: '$args{-guid}'"; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | return $proj; | ||
| 120 | } | ||
| 121 | |||
| 122 | =item get_authority() | ||
| 123 | |||
| 124 | Gets the authority prefix (e.g. TB2) for the implementing service | ||
| 125 | |||
| 126 | Type : Authority | ||
| 127 | Title : get_authority | ||
| 128 | Usage : my $auth = $obj->get_authority; | ||
| 129 | Function: Gets authority prefix | ||
| 130 | Returns : 'uBioNB' | ||
| 131 | Args : None | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | sub get_authority { 'uBioNB' } | ||
| 136 | |||
| 137 | =item get_supported_formats() | ||
| 138 | |||
| 139 | Gets an array ref of supported formats | ||
| 140 | |||
| 141 | Type : Accessor | ||
| 142 | Title : get_supported_formats | ||
| 143 | Usage : my @formats = @{ $obj->get_supported_formats }; | ||
| 144 | Function: Gets an array ref of supported formats | ||
| 145 | Returns : [ qw(nexml nexus html json) ] | ||
| 146 | Args : NONE | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub get_supported_formats { [ 'nexml', 'html', 'json', 'nexus', 'rss1' ] } | ||
| 151 | |||
| 152 | =item get_redirect() | ||
| 153 | |||
| 154 | Gets a redirect URL if relevant | ||
| 155 | |||
| 156 | Type : Accessor | ||
| 157 | Title : get_redirect | ||
| 158 | Usage : my $url = $obj->get_redirect; | ||
| 159 | Function: Gets a redirect URL if relevant | ||
| 160 | Returns : String | ||
| 161 | Args : $cgi | ||
| 162 | Comments: This method is called by handle_request so that | ||
| 163 | services can 303 redirect a record lookup to | ||
| 164 | another URL. By default, this method returns | ||
| 165 | undef (i.e. no redirect), but if this implementation | ||
| 166 | is called to handle a request that specifies | ||
| 167 | 'format=html' the request is forwarded to the | ||
| 168 | appropriate page on the http://www.ubio.org website | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | sub get_redirect { | ||
| 173 | my ( $self, $cgi ) = @_; | ||
| 174 | if ( $cgi->param('format') eq 'html' ) { | ||
| 175 | if ( my $query = $cgi->param('query') ) { | ||
| 176 | return UBIO_SRCH . $query; | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | my $path_info = $cgi->path_info; | ||
| 180 | if ( $path_info =~ m/(\d+)$/ ) { | ||
| 181 | my $namebank_id = $1; | ||
| 182 | $logger->info("Getting html redirect for id: $namebank_id"); | ||
| 183 | return UBIO_NMBK . $namebank_id; | ||
| 184 | } | ||
| 185 | else { | ||
| 186 | throw 'BadArgs' => "Not a parseable guid: '$path_info'"; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | return; | ||
| 191 | } | ||
| 192 | |||
| 193 | =item get_query_result() | ||
| 194 | |||
| 195 | Gets a query result and returns it as a project object | ||
| 196 | |||
| 197 | Type : Accessor | ||
| 198 | Title : get_query_result | ||
| 199 | Usage : my $proj = $obj->get_query_result($query); | ||
| 200 | Function: Gets a query result | ||
| 201 | Returns : Bio::Phylo::Project | ||
| 202 | Args : A simple query string for a namebank search | ||
| 203 | Comments: The $query is a simple CQL level 0 term-only query | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | sub get_query_result { | ||
| 208 | my ( $self, $query ) = @_; | ||
| 209 | throw 'System' => "No UBIO_KEYCODE env var" unless $ENV{'UBIO_KEYCODE'}; | ||
| 210 | my $prefix = $self->get_url_prefix; | ||
| 211 | |||
| 212 | # do the search query | ||
| 213 | my $proj = parse( | ||
| 214 | '-url' => sprintf( UBIOWS, $query, $ENV{'UBIO_KEYCODE'} ), | ||
| 215 | '-format' => 'ubiosearch', | ||
| 216 | '-as_project' => 1, | ||
| 217 | ); | ||
| 218 | |||
| 219 | # construct links relative to the current service | ||
| 220 | my ($taxa) = @{ $proj->get_taxa }; | ||
| 221 | $taxa->visit( sub { | ||
| 222 | my $taxon = shift; | ||
| 223 | $taxon->set_link( $prefix . $taxon->get_guid ); | ||
| 224 | } ); | ||
| 225 | |||
| 226 | return $proj; | ||
| 227 | } | ||
| 228 | |||
| 229 | =back | ||
| 230 | |||
| 231 | =cut | ||
| 232 | |||
| 233 | # podinherit_insert_token | ||
| 234 | |||
| 235 | =head1 SEE ALSO | ||
| 236 | |||
| 237 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||
| 238 | for any user or developer questions and discussions. | ||
| 239 | |||
| 240 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com> | ||
| 241 | |||
| 242 | =head1 CITATION | ||
| 243 | |||
| 244 | If you use Bio::Phylo in published research, please cite it: | ||
| 245 | |||
| 246 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||
| 247 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||
| 248 | I<BMC Bioinformatics> B<12>:63. | ||
| 249 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||
| 250 | |||
| 251 | |||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | } | ||
| 256 | |||
| 257 | 1; |