Phylo/PhyloWS/Service/Timetree.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 118 CC 4 SCAR 14.4
CC
4
Cov
0%
get_redirect
12.0
get_query_result
6.0
get_supported_formats
2.0
CRAP 4.2
1 package Bio::Phylo::PhyloWS::Service::Timetree;
2 use strict;
3 use utf8;
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 looks_like_instance';
10 use Bio::Phylo::Util::Dependency qw'LWP::UserAgent HTML::TreeBuilder::XPath URI::Escape';
11 use constant URL => 'http://timetree.org/time_query.php?';
12 use constant NCBI => 'http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=';
13  
14 # http://localhost/nexml/service/timetree/phylows/tree/find?query=dcterms.identifier=9606%20and%20dcterms.identifier=9597
15 {
16 my $fac = Bio::Phylo::Factory->new;
17 my $logger = Bio::Phylo::Util::Logger->new;
18
19 $logger->VERBOSE(
20 '-level' => 4,
21 '-class' => 'Bio::Phylo::Parsers::Timetree',
22 );
23
24 my $parse_html = sub {
25 my ( $content ) = @_;
26 my $tree = HTML::TreeBuilder::XPath->new;
27 $tree->parse_content($content);
28 my ($title) = $tree->findnodes(q{//td[@class='titleBlack']/strong});
29 my $str = $title->as_text;
30 $str =~ s/^(\d+\.\d+).*$/$1/;
31 return $str;
32 };
33
34 my $get_query_result_raw = sub {
35 my ( $self, $taxon_a, $taxon_b ) = @_;
36 my $lua = LWP::UserAgent->new;
37 my $url = URL . "taxon_a=${taxon_a}&taxon_b=${taxon_b}";
38 my $response = $lua->get($url);
39 if ( $response->is_success ) {
40 my $content = $response->content;
41 utf8::decode($content);
42 return $content;
43 }
44 else {
45 throw 'NetworkError' => $response->status_line;
46 }
47 };
48
49 my $create_project = sub {
50 my ($name_a,$name_b) = @_;
51 my $project = $fac->create_project;
52 my $forest = $fac->create_forest;
53 my $taxa = $fac->create_taxa;
54 my $taxona = $fac->create_taxon(
55 '-name' => $name_a,
56 '-link' => NCBI . $name_a,
57 '-guid' => $name_a,
58 );
59 my $taxonb = $fac->create_taxon(
60 '-name' => $name_b,
61 '-link' => NCBI . $name_b,
62 '-guid' => $name_b,
63 );
64
65 $project->insert( $taxa, $forest );
66 $taxa->insert( $taxona, $taxonb );
67 $forest->set_taxa($taxa);
68 return ( $project, $taxona, $taxonb, $forest );
69 };
70  
71 =head1 NAME
72  
73 Bio::Phylo::PhyloWS::Service::Timetree - PhyloWS service wrapper for Timetree
74  
75 =head1 SYNOPSIS
76  
77 # inside a CGI script:
78 use CGI;
79 use Bio::Phylo::PhyloWS::Service::Timetree;
80  
81 my $cgi = CGI->new;
82 my $service = Bio::Phylo::PhyloWS::Service::Timetree->new( '-url' => $url );
83 $service->handle_request($cgi);
84  
85 =head1 DESCRIPTION
86  
87 This is an example implementation of a PhyloWS service. The service
88 wraps around the timetree web site (using screen scraping) and returns
89 project objects that include a tree for every search result.
90  
91 =head1 METHODS
92  
93 =head2 ACCESSORS
94  
95 =over
96  
97 =item get_query_result()
98  
99 Gets a phylows cql query result
100  
101 Type : Accessor
102 Title : get_query_result
103 Usage : my $result = $obj->get_query_result( $query );
104 Function: Gets a query result
105 Returns : Bio::Phylo::Project
106 Args : Required: $query
107  
108 =cut
109  
110 sub get_query_result {
111 my ( $self, $query ) = @_;
112 my ( $taxon_a, $taxon_b );
113
114 # clean up CQL query
115 $query = URI::Escape::uri_unescape($query);
116 if ( $query =~ m/^\D*(\d+)\D+(\d+)/ ) {
117 ( $taxon_a, $taxon_b ) = ( $1, $2 );
118 }
119  
120 # download timetree result
121 my $content = $get_query_result_raw->( $self, $taxon_a, $taxon_b );
122  
123 # parse timetree result
124 my $age = $parse_html->( $content );
125  
126 # populate project
127 my ( $project, $taxona, $taxonb, $forest ) = $create_project->($taxon_a,$taxon_b);
128 my $tree = $fac->create_tree;
129 my $tree_root = $fac->create_node;
130
131 my $node_a = $fac->create_node(
132 '-branch_length' => $age,
133 '-parent' => $tree_root,
134 '-taxon' => $taxona,
135 '-name' => $taxona->get_name,
136 '-link' => NCBI . $taxona->get_name,
137 '-guid' => $taxona->get_name,
138 );
139
140 my $node_b = $fac->create_node(
141 '-branch_length' => $age,
142 '-parent' => $tree_root,
143 '-taxon' => $taxonb,
144 '-name' => $taxonb->get_name,
145 '-link' => NCBI . $taxonb->get_name,
146 '-guid' => $taxonb->get_name,
147 );
148
149 $tree->insert( $tree_root, $node_a, $node_b );
150 $forest->insert($tree);
151  
152 # return project
153 return $project;
154 }
155  
156 =item get_supported_formats()
157  
158 Gets an array ref of supported formats
159  
160 Type : Accessor
161 Title : get_supported_formats
162 Usage : my @formats = @{ $obj->get_supported_formats };
163 Function: Gets an array ref of supported formats
164 Returns : [ qw(nexml nexus newick html) ]
165 Args : NONE
166  
167 =cut
168  
169 sub get_supported_formats { [qw(nexml nexus newick html)] }
170  
171 =item get_redirect()
172  
173 Gets a redirect URL if relevant
174  
175 Type : Accessor
176 Title : get_redirect
177 Usage : my $url = $obj->get_redirect;
178 Function: Gets a redirect URL if relevant
179 Returns : String
180 Args : $cgi
181 Comments: This method is called by handle_request so that
182 services can 303 redirect a record lookup to
183 another URL. By default, this method returns
184 undef (i.e. no redirect), but if this implementation
185 is called to handle a request that specifies
186 'format=html' the request is forwarded to the
187 appropriate page on the http://timetree.org website
188  
189 =cut
190  
191 sub get_redirect {
192 my ( $self, $cgi ) = @_;
193 if ( $cgi->param('format') eq 'html' ) {
194 my $query = $cgi->param('query');
195 $query = URI::Escape::uri_unescape($query);
196 my ( $taxon_a, $taxon_b );
197 if ( $query =~ m/^\D*(\d+)\D+(\d+)/ ) {
198 ( $taxon_a, $taxon_b ) = ( $1, $2 );
199 }
200 my $url = URL . "taxon_a=${taxon_a}&taxon_b=${taxon_b}";
201 return $url;
202 }
203 return;
204 }
205  
206 =back
207  
208 =cut
209  
210 # podinherit_insert_token
211  
212 =head1 SEE ALSO
213  
214 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
215 for any user or developer questions and discussions.
216  
217 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>
218  
219 =head1 CITATION
220  
221 If you use Bio::Phylo in published research, please cite it:
222  
223 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
224 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
225 I<BMC Bioinformatics> B<12>:63.
226 L<http://dx.doi.org/10.1186/1471-2105-12-63>
227  
228 =cut
229  
230 }
231 1;