Phylo/PhyloWS/Client.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 180 CC 7 SCAR 18.5
CC
7
Cov
0%
get_query_result
20.0
new
6.0
set_ua
6.0
CRAP 6.4
1 package Bio::Phylo::PhyloWS::Client;
2 use strict;
3 use base 'Bio::Phylo::PhyloWS';
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 'looks_like_hash';
9 use Bio::Phylo::Util::Dependency qw'LWP::UserAgent XML::Twig';
10 {
11 my @fields = \( my (%ua) );
12 my $logger = Bio::Phylo::Util::Logger->new;
13 my $fac = Bio::Phylo::Factory->new;
14  
15 =head1 NAME
16  
17 Bio::Phylo::PhyloWS::Client - Base class for phylogenetic web service clients
18  
19 =head1 SYNOPSIS
20  
21 #!/usr/bin/perl
22 use strict;
23 use warnings;
24 use Bio::Phylo::Factory;
25
26 my $fac = Bio::Phylo::Factory->new;
27 my $client = $fac->create_client(
28 '-base_uri' => 'http://nexml-dev.nescent.org/nexml/phylows/tolweb/phylows/',
29 '-authority' => 'uBioNB',
30 );
31 my $desc = $client->get_query_result(
32 '-query' => 'Homo sapiens',
33 '-section' => 'taxon',
34 );
35 for my $res ( @{ $desc->get_entities } ) {
36 my $proj = $client->get_record( '-guid' => $res->get_guid );
37 print $proj->to_nexus, "\n";
38 }
39  
40 =head1 DESCRIPTION
41  
42 This is the base class for clients connecting to services that implement
43 the PhyloWS (L<http://evoinfo.nescent.org/PhyloWS>) recommendations.
44  
45 =head1 METHODS
46  
47 =head2 CONSTRUCTOR
48  
49 =over
50  
51 =item new()
52  
53 Type : Constructor
54 Title : new
55 Usage : my $phylows = Bio::Phylo::PhyloWS::Client->new( -url => $url );
56 Function: Instantiates Bio::Phylo::PhyloWS::Client object
57 Returns : a Bio::Phylo::PhyloWS::Client object
58 Args : Required: -url => $url
59 Optional: any number of setters. For example,
60 Bio::Phylo::PhyloWS->new( -name => $name )
61 will call set_name( $name ) internally
62  
63 =cut
64  
65 sub new {
66  
67 # could be child class
68 my $class = shift;
69  
70 # go up inheritance tree, eventually get an ID
71 my $self = $class->SUPER::new(@_);
72  
73 # store a user agent object to delegate http stuff to
74 if ( not $self->get_ua ) {
75 my $ua = LWP::UserAgent->new;
76 $ua->timeout(300);
77 $ua->env_proxy;
78 $self->set_ua($ua);
79 }
80 return $self;
81 }
82 my $ua = sub {
83 return $ua{ shift->get_id };
84 };
85  
86 =back
87  
88 =head2 MUTATORS
89  
90 =over
91  
92 =item set_ua()
93  
94 Assigns a new L<LWP::UserAgent> object that the client uses to communicate
95 with the service. Typically you don't have to use this unless you have to
96 configure a user agent for things such as proxies. Normally a default user
97 agent is instantiated when the client constructor is called.
98  
99 Type : Mutator
100 Title : set_ua
101 Usage : $obj->set_ua( LWP::UserAgent->new );
102 Function: Assigns another (non-default) user agent
103 Returns : $self
104 Args : An LWP::UserAgent object (or child class)
105  
106 =cut
107  
108 sub set_ua {
109 my $self = shift;
110 my $arg = shift;
111 if ( UNIVERSAL::isa( $arg, 'LWP::UserAgent' ) ) {
112 $ua{ $self->get_id } = $arg;
113 }
114 else {
115 throw 'BadArgs' => "'$arg' is not an LWP::UserAgent";
116 }
117 return $self;
118 }
119  
120 =back
121  
122 =head2 ACCESSORS
123  
124 =over
125  
126 =item get_query_result()
127  
128 Gets search query result
129  
130 Type : Accessor
131 Title : get_query_result
132 Usage : my $res = $obj->get_query_result( -query => $query );
133 Function: Returns Bio::Phylo::PhyloWS::Description object
134 Returns : A string
135 Args : Required: -query => $cql_query
136 Optional: -section, -recordSchema
137  
138 =cut
139  
140 my $rss_handler = sub {
141 my ($create_method,$self,$twig,$elt) = @_;
142 my %known = (
143 'title' => '-name',
144 'description' => '-desc',
145 'link' => '-link',
146 );
147 my ( %args, @meta );
148 for my $child ( $elt->children ) {
149 my $tag = $child->tag;
150 if ( my $key = $known{$tag} ) {
151 $args{$key} = $child->text;
152 }
153 elsif ( $tag ne 'items' ) {
154 my $predicate = $tag;
155 my ( $prefix, $namespace, $object );
156 if ( $tag =~ /(.+?):/ ) {
157 $prefix = $1;
158 $namespace = $child->namespace;
159 }
160 if ( ! ( $object = $child->att('rdf:about') ) ) {
161 $object = $child->text;
162 }
163 push @meta, $fac->create_meta(
164 '-namespaces' => { $prefix => $namespace },
165 '-triple' => { $predicate => $object },
166 );
167 }
168 }
169 my $obj = $fac->$create_method(%args);
170 $obj->add_meta($_) for @meta;
171 my $pre = $self->get_url_prefix;
172 my $link = $obj->get_link;
173 $link =~ s/^\Q$pre\E(.+?)?/$1/i;
174 $obj->set_guid($link);
175 return $obj;
176 };
177  
178 sub get_query_result {
179 my $self = shift;
180 $logger->debug("going to get query result");
181 if ( my %args = looks_like_hash @_ ) {
182
183 # these fields need to be set first before get_url returns
184 # a sane response
185 $self->set_query( $args{'-query'} || throw 'BadArgs' => "Need query argument" );
186 $logger->debug("set query ".$args{'-query'});
187
188 $self->set_section( $args{'-section'} || 'taxon' );
189 $self->set_format( 'rss1' );
190 my $rs = $args{'-recordSchema'} || $args{'-section'} || 'taxon';
191 my $url = $self->get_url( '-recordSchema' => $rs );
192 $url =~ s/&amp;/&/g;
193 $logger->debug("URL: $url");
194
195 # do the request
196 my $response = $ua->($self)->get($url);
197 if ( $response->is_success ) {
198 $logger->debug("request succeeded");
199 my $content = $response->content;
200 use Data::Dumper;
201 print Dumper($response);
202
203 $self->set_section($rs);
204 my $desc = $self->parse_query_result($content);
205 if ( $@ ) {
206 $logger->fatal("Error fetching from $url");
207 $logger->fatal($content);
208 throw 'NetworkError' => $@;
209 }
210 else {
211 $self->set_section( $args{'-section'} || 'taxon' );
212 return $desc;
213 }
214 }
215 else {
216 throw 'NetworkError' => "Error fetching from $url: "
217 . $response->status_line;
218 }
219 }
220 }
221  
222 =item parse_query_result()
223  
224 Parses a raw query result
225  
226 Type : Accessor
227 Title : parse_query_result
228 Usage : my $desc = $obj->parse_query_result($content);
229 Function: Parses a raw query result
230 Returns : Bio::Phylo::PhyloWS::Resource::Description object
231 Args : Raw result content
232  
233 =cut
234
235 sub parse_query_result {
236 my ( $self, $content ) = @_;
237 my $desc;
238 eval {
239 XML::Twig->new(
240 'TwigHandlers' => {
241 'channel' => sub {
242 $desc = $rss_handler->('create_description',$self,@_);
243 },
244 'item' => sub {
245 my $res = $rss_handler->('create_resource',$self,@_);
246 $desc->insert($res);
247 },
248 }
249 )->parse($content);
250 };
251 return $desc;
252 }
253  
254 =item get_record()
255  
256 Gets a PhyloWS database record
257  
258 Type : Accessor
259 Title : get_record
260 Usage : my $rec = $obj->get_record( -guid => $guid );
261 Function: Gets a PhyloWS database record
262 Returns : Bio::Phylo::Project object
263 Args : Required: -guid => $guid
264  
265 =cut
266  
267 sub get_record {
268 my $self = shift;
269 if ( my %args = looks_like_hash @_ ) {
270 $self->set_guid( $args{'-guid'} || throw 'BadArgs' => "Need -guid argument" );
271 $self->set_query();
272 my $url = $self->get_url( '-format' => 'nexml' );
273 $logger->debug($url);
274 return parse(
275 '-format' => 'nexml',
276 '-url' => $url,
277 '-as_project' => 1,
278 );
279 }
280 }
281  
282 =item get_ua()
283  
284 Gets the underlying L<LWP::UserAgent> object that the client uses to communicate with the service
285  
286 Type : Accessor
287 Title : get_ua
288 Usage : my $ua = $obj->get_ua;
289 Function: Gets user agent
290 Returns : LWP::UserAgent object
291 Args : None
292  
293 =cut
294  
295 sub get_ua { $ua{ shift->get_id } }
296  
297 sub _cleanup {
298 my $self = shift;
299 my $id = $self->get_id;
300 for my $field (@fields) {
301 delete $field->{$id};
302 }
303 }
304  
305 =back
306  
307 =cut
308  
309 # podinherit_insert_token
310  
311 =head1 SEE ALSO
312  
313 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
314 for any user or developer questions and discussions.
315  
316 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>
317  
318 =head1 CITATION
319  
320 If you use Bio::Phylo in published research, please cite it:
321  
322 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
323 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
324 I<BMC Bioinformatics> B<12>:63.
325 L<http://dx.doi.org/10.1186/1471-2105-12-63>
326  
327 =cut
328  
329 }
330 1;