Phylo/PhyloWS/Service/Tolweb.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 141 CC 6 SCAR 18.6
CC
6
Cov
0%
get_redirect
20.0
get_record
12.0
get_query_result
2.0
CRAP 6.4
1 package Bio::Phylo::PhyloWS::Service::Tolweb;
2 use strict;
3 use warnings;
4 use base 'Bio::Phylo::PhyloWS::Service';
5 use Bio::Phylo::IO 'parse';
6 use Bio::Phylo::Factory;
7 use Bio::Phylo::Util::Logger;
8 use Bio::Phylo::Util::Exceptions 'throw';
9 use Bio::Phylo::Util::CONSTANT qw'looks_like_hash :namespaces';
10 use Bio::Phylo::Util::Dependency qw'XML::Twig';
11 use constant TOL_BASE => 'http://tolweb.org/';
12 use constant WEB_SRCH => TOL_BASE . 'tree/home.pages/searchTOL?taxon=';
13 use constant XML_NODE => TOL_BASE . 'onlinecontributors/app?service=external&page=xml/TreeStructureService&page_depth=1&node_id=';
14 use constant XML_SRCH => TOL_BASE . 'onlinecontributors/app?service=external&page=xml/GroupSearchService&group=';
15  
16 {
17 my $fac = Bio::Phylo::Factory->new;
18 my $logger = Bio::Phylo::Util::Logger->new;
19  
20 =head1 NAME
21  
22 Bio::Phylo::PhyloWS::Service::Tolweb - PhyloWS service wrapper for Tree of Life
23  
24 =head1 SYNOPSIS
25  
26 # inside a CGI script:
27 use CGI;
28 use Bio::Phylo::PhyloWS::Service::Tolweb;
29  
30 my $cgi = CGI->new;
31 my $service = Bio::Phylo::PhyloWS::Service::Tolweb->new( '-url' => $url );
32 $service->handle_request($cgi);
33  
34 =head1 DESCRIPTION
35  
36 This is an example implementation of a PhyloWS service. The service
37 wraps around the Tree of Life XML services described at
38 L<http://tolweb.org/tree/home.pages/downloadtree.html>.
39  
40 When doing a record lookup this service returns project objects
41 that include the focal node (identified by its PhyloWS ID) and the
42 nearest child and parent nodes that have web pages.
43  
44 When querying, this service returns a project object with one taxa
45 block containing zero or more taxon objects that matched the query.
46  
47 When URLs to this service specify format=html in the query string, this
48 service returns redirect URLs to web pages on the Tree of Life web project
49 site at L<http://tolweb.org>. The redirect URLs either point to search result
50 listings or to node pages, depending on whether the redirect is for a record
51 query or a record lookup, respectively.
52  
53 =head1 METHODS
54  
55 =head2 ACCESSORS
56  
57 =over
58  
59 =item get_record()
60  
61 Gets a tolweb record by its id
62  
63 Type : Accessor
64 Title : get_record
65 Usage : my $record = $obj->get_record( -guid => $guid );
66 Function: Gets a tolweb record by its id
67 Returns : Bio::Phylo::Project
68 Args : Required: -guid => $guid
69 Comments: For the $guid argument, this method only cares
70 whether the last part of the argument is a series
71 of integers, which are understood to be the node
72 identifier in the Tree of Life
73  
74 =cut
75  
76 sub get_record {
77 my $self = shift;
78 if ( my %args = looks_like_hash @_ ) {
79 if ( $args{'-guid'} && $args{'-guid'} =~ m|(\d+)$| ) {
80 my $tolweb_id = $1;
81 $logger->info("Getting nexml record for id: $tolweb_id");
82
83 # fetch and parse the output
84 my $proj = parse(
85 '-format' => 'tolweb',
86 '-url' => XML_NODE . $tolweb_id,
87 '-as_project' => 1,
88 );
89 $proj->set_link($self->get_url);
90 $proj->set_name('Tree of Life web project lookup service');
91 $proj->set_desc("Results for ID $tolweb_id");
92
93 # post processing to make nice local links back to this service
94 my $prefix = $self->get_url_prefix;
95 my ($forest) = @{ $proj->get_forests };
96 my ($tree) = @{ $forest->get_entities };
97 my $taxa = $forest->make_taxa;
98 $proj->insert($taxa);
99 $tree->visit( sub {
100 my $node = shift;
101 $node->set_link( $prefix . $node->get_guid );
102 } );
103 $taxa->visit( sub {
104 my $taxon = shift;
105 my ($node) = @{ $taxon->get_nodes };
106 $taxon->set_link($node->get_link);
107 $taxon->set_desc($node->get_desc);
108 $taxon->add_meta($_) for @{ $node->get_meta };
109 } );
110
111 # done!
112 return $proj;
113 }
114 else {
115 throw 'BadArgs' => "Not a parseable guid: '$args{-guid}'";
116 }
117 }
118 }
119  
120 =item get_redirect()
121  
122 Gets a redirect URL if relevant
123  
124 Type : Accessor
125 Title : get_redirect
126 Usage : my $url = $obj->get_redirect;
127 Function: Gets a redirect URL if relevant
128 Returns : String
129 Args : $cgi
130 Comments: This method is called by handle_request so that
131 services can 303 redirect a record lookup to
132 another URL. By default, this method returns
133 undef (i.e. no redirect), but if this implementation
134 is called to handle a request that specifies
135 'format=html' the request is forwarded to the
136 appropriate page on the http://tolweb.org website
137  
138 =cut
139  
140 sub get_redirect {
141 my ( $self, $cgi ) = @_;
142 if ( $cgi->param('format') eq 'html' ) {
143 if ( my $query = $cgi->param('query') ) {
144 return WEB_SRCH . $query;
145 }
146 else {
147 my $path_info = $cgi->path_info;
148 if ( $path_info =~ m/(\d+)$/ ) {
149 my $tolweb_id = $1;
150 $logger->info("Getting html redirect for id: $tolweb_id");
151 return TOL_BASE . $tolweb_id;
152 }
153 else {
154 throw 'BadArgs' => "Not a parseable guid: '$path_info'";
155 }
156 }
157 }
158 return;
159 }
160  
161 =item get_query_result()
162  
163 Gets a query result and returns it as a project object
164  
165 Type : Accessor
166 Title : get_query_result
167 Usage : my $proj = $obj->get_query_result($query);
168 Function: Gets a query result
169 Returns : Bio::Phylo::Project
170 Args : A simple query string for a group search
171 Comments: The $query is a simple CQL level 0 term-only query
172  
173 =cut
174  
175 sub get_query_result {
176 my ( $self, $query ) = @_;
177 my $proj = $fac->create_project(
178 '-link' => $self->get_url,
179 '-namespaces' => { 'dc' => _NS_DC_ },
180 '-desc' => 'Results for query: ' . $self->get_query,
181 '-name' => 'Tree of Life web project PhyloWS search service',
182 );
183 my $taxa = $fac->create_taxa;
184 my $prefix = $self->get_url_prefix;
185 $proj->insert( $taxa );
186 XML::Twig->new(
187 'twig_handlers' => {
188 'NODE' => sub {
189 my ( $twig, $node_elt ) = @_;
190 my $id = $node_elt->att('ID');
191 my ($name_elt) = $node_elt->children('NAME');
192 $taxa->insert(
193 $fac->create_taxon(
194 '-name' => $name_elt->text,
195 '-guid' => $id,
196 '-link' => $prefix . $id,
197 )->add_meta(
198 $fac->create_meta( '-triple' => { 'dc:identifier' => $id } )
199 )
200 );
201 }
202 }
203 )->parseurl( XML_SRCH . $query );
204 return $proj;
205 }
206  
207 =item get_supported_formats()
208  
209 Gets an array ref of supported formats
210  
211 Type : Accessor
212 Title : get_supported_formats
213 Usage : my @formats = @{ $obj->get_supported_formats };
214 Function: Gets an array ref of supported formats
215 Returns : [ qw(nexml nexus newick html json phyloxml rss1) ]
216 Args : NONE
217  
218 =cut
219  
220 sub get_supported_formats { [qw(nexml nexus newick html json phyloxml rss1)] }
221  
222 =item get_authority()
223  
224 Gets the authority prefix (e.g. TB2) for the implementing service
225  
226 Type : Abstract Accessor
227 Title : get_authority
228 Usage : my $auth = $obj->get_authority;
229 Function: Gets authority prefix
230 Returns : 'ToL'
231 Args : None
232  
233 =cut
234  
235 sub get_authority { 'ToL' }
236  
237 =back
238  
239 =cut
240  
241 # podinherit_insert_token
242  
243 =head1 SEE ALSO
244  
245 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
246 for any user or developer questions and discussions.
247  
248 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>
249  
250 =head1 CITATION
251  
252 If you use Bio::Phylo in published research, please cite it:
253  
254 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
255 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
256 I<BMC Bioinformatics> B<12>:63.
257 L<http://dx.doi.org/10.1186/1471-2105-12-63>
258  
259 =cut
260  
261 }
262 1;