Phylo/PhyloWS/Service/Tolweb.pm untested

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