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