Bio/DB/Query/WebQuery.pm

filter: Statementstmt 18.5% 21 / 113 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 30.4% 7 / 23 Podpod 100% 8 / 8 totaltotal 15.6% 36 / 230 CC 33 SCAR 30.4
CC
33
Cov
11%
new
56.0
proxy
42.0
ids
30.0
CRAP 20.9
« Bio/Tools/Sim4/Results.pm ↑ Summary Bio/DB/Taxonomy/list.pm »
1 #
2 # BioPerl module for Bio::DB::WebQuery.pm
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Lincoln Stein <lstein@cshl.org>
7 #
8 # Copyright Lincoln Stein
9 #
10 # You may distribute this module under the same terms as perl itself
11 #
12 # POD documentation - main docs before the code
13 #
14  
15 =head1 NAME
16  
17 Bio::DB::Query::WebQuery - Helper class for web-based sequence queryies
18  
19 =head1 SYNOPSIS
20  
21 # Do not use this class directly. See Bio::DB::QueryI and one of
22 # the implementor classes (such as Bio::DB::GenBankQuery) for
23 # information.
24  
25 See L<Bio::DB::QueryI>, L<Bio::DB::GenBankQuery>
26  
27  
28 =head1 DESCRIPTION
29  
30 Do not use this class directly. See Bio::DB::QueryI and one of the
31 implementor classes (such as Bio::DB::Query::GenBank) for information.
32  
33 Those writing subclasses must define _get_params() and
34 _parse_response(), and possibly override _request_method().
35  
36 =head1 FEEDBACK
37  
38 =head2 Mailing Lists
39  
40 User feedback is an integral part of the
41 evolution of this and other Bioperl modules. Send
42 your comments and suggestions preferably to one
43 of the Bioperl mailing lists. Your participation
44 is much appreciated.
45  
46 bioperl-l@bioperl.org - General discussion
47 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
48  
49 =head2 Support
50  
51 Please direct usage questions or support issues to the mailing list:
52  
53 I<bioperl-l@bioperl.org>
54  
55 rather than to the module maintainer directly. Many experienced and
56 reponsive experts will be able look at the problem and quickly
57 address it. Please include a thorough description of the problem
58 with code and data examples if at all possible.
59  
60 =head2 Reporting Bugs
61  
62 Report bugs to the Bioperl bug tracking system to help us keep track
63 the bugs and their resolution. Bug reports can be submitted via the
64 web:
65  
66 https://github.com/bioperl/bioperl-live/issues
67  
68 =head1 AUTHOR - Lincoln Stein
69  
70 Email lstein@cshl.org
71  
72 =head1 APPENDIX
73  
74 The rest of the documentation details each of the
75 object methods. Internal methods are usually
76 preceded with a _
77  
78 =cut
79  
80 # Let the code begin...
81  
82 package Bio::DB::Query::WebQuery;
83 1 $Bio::DB::Query::WebQuery::VERSION = '1.7.8';
84 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 1 ▶use vars qw($LAST_INVOCATION_TIME);
Subroutine BEGIN: called CC=1 SCAR=0.0
90 1 ▶use base qw(Bio::Root::Root Bio::DB::QueryI);
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1 $LAST_INVOCATION_TIME = 0;
94 }
95  
96 =head2 new
97  
98 Title : new
99 Usage : $db = Bio::DB::WebQuery->new(@args)
100 Function: create new query object
101 Returns : new query object
102 Args : -db database (e.g. 'protein')
103 -ids array ref of ids (overrides query)
104 -verbose turn on verbose debugging
105  
106 This method creates a new query object. Typically you will specify a
107 -db and a -query argument. The value of -query is a database-specific
108 string.
109  
110 If you provide an array reference of IDs in -ids, the query will be
111 ignored and the list of IDs will be used when the query is passed to
112 the database.
113  
114 =cut
115  
116 # Borrowed shamelessly from WebDBSeqI. Some of this code should be
117 # refactored.
118 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
119 0 my $class = shift;
120 0 my $self = $class->SUPER::new(@_);
121  
122 0 my ($query,$ids,$verbose,$delay) = $self->_rearrange(['QUERY','IDS','VERBOSE','DELAY'],@_);
123 0 ▶ $self->throw('must provide one of the the -query or -ids arguments')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query or defined $ids 0 0 0
Condition0%0/3
defined $query or defined $ids
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $query or defined $ids
defined $query defined $ids
Truth table0%0/3
A or B
A= defined $queryB= defined $ids
ABresult
1X1
011
000
124 unless defined($query) || defined($ids);
125 0 ▶ if ($ids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ids) 0 0 0
126 0 $query = $self->_generate_id_string($ids);
127 }
128 0 $self->query($query);
129 0 ▶ $verbose && $self->verbose($verbose);
Branch: 1 branch
BranchTrueFalseTotal
$verbose and $self->verbose($verbose) 0 0 0
130 0 ▶ $delay = $self->delay_policy unless defined $delay;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delay 0 0 0
131 0 $self->delay($delay);
132  
133 0 my $ua = LWP::UserAgent->new(env_proxy => 1);
134 0 ▶ $ua->agent(ref($self) ."/".($Bio::DB::Query::WebQuery::VERSION || '0.1'));
Condition0%0/2
$Bio::DB::Query::WebQuery::VERSION || '0.1'
l!l
00
MC/DC0%0/1
$Bio::DB::Query::WebQuery::VERSION || '0.1'
$Bio::DB::Query::WebQuery::VERSION
Truth table0%0/2
A
A= $Bio::DB::Query::WebQuery::VERSION
Aresult
11
00
135 0 $self->ua($ua);
136 0 $self->{'_authentication'} = [];
137 0 $self;
138 }
139  
140 =head2 ua
141  
142 Title : ua
143 Usage : my $ua = $self->ua or
144 $self->ua($ua)
145 Function: Get/Set a LWP::UserAgent for use
146 Returns : reference to LWP::UserAgent Object
147 Args : $ua - must be a LWP::UserAgent
148  
149 =cut
150  
151 0 ▶sub ua {
Subroutine ua: not called CC=3 SCAR=24.8
Pod documented
152 0 my ($self, $ua) = @_;
153 0 my $d = $self->{'_ua'};
154 0 ▶ if( defined $ua && $ua->isa("LWP::UserAgent") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ua and $ua->isa("LWP::UserAgent")) 0 0 0
Condition0%0/3
defined $ua and $ua->isa("LWP::UserAgent")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ua and $ua->isa("LWP::UserAgent")
defined $ua $ua->isa("LWP::UserAgent")
Truth table0%0/3
A and B
A= defined $uaB= $ua->isa("LWP::UserAgent")
ABresult
0X0
100
111
155 0 $self->{'_ua'} = $ua;
156 }
157 0 $d;
158 }
159  
160 =head2 proxy
161  
162 Title : proxy
163 Usage : $httpproxy = $db->proxy('http') or
164 $db->proxy(['http','ftp'], 'http://myproxy' )
165 Function: Get/Set a proxy for use of proxy
166 Returns : a string indicating the proxy
167 Args : $protocol : an array ref of the protocol(s) to set/get
168 $proxyurl : url of the proxy to use for the specified protocol
169 $username : username (if proxy requires authentication)
170 $password : password (if proxy requires authentication)
171  
172 =cut
173  
174 0 ▶sub proxy {
Subroutine proxy: not called CC=6 SCAR=37.4
Pod documented
175 0 my ($self,$protocol,$proxy,$username,$password) = @_;
176 0 ▶ return undef if ( !defined $self->ua || !defined $protocol
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->ua and defined $protocol and defined $proxy 0 0 0
Condition0%0/6
defined $self->ua and defined $protocol and defined $proxy
!ll&&!rl&&r
000
defined $self->ua and defined $protocol
!ll&&!rl&&r
000
MC/DC0%0/3
defined $self->ua and defined $protocol and defined $proxy
defined $self->ua defined $protocol defined $proxy
Truth table0%0/4
A and B and C
A= defined $self->uaB= defined $protocolC= defined $proxy
ABCresult
0XX0
10X0
1100
1111
177 || !defined $proxy );
178 0 ▶ $self->authentication($username, $password)
Branch: 1 branch
BranchTrueFalseTotal
if $username and $password 0 0 0
Condition0%0/3
$username and $password
!ll&&!rl&&r
000
MC/DC0%0/2
$username and $password
$username $password
Truth table0%0/3
A and B
A= $usernameB= $password
ABresult
0X0
100
111
179 if ($username && $password);
180 0 return $self->ua->proxy($protocol,$proxy);
181 }
182  
183 =head2 authentication
184  
185 Title : authentication
186 Usage : $db->authentication($user,$pass)
187 Function: Get/Set authentication credentials
188 Returns : Array of user/pass
189 Args : Array or user/pass
190  
191  
192 =cut
193  
194 0 ▶sub authentication{
Subroutine authentication: not called CC=3 SCAR=24.8
Pod documented
195 0 my ($self,$u,$p) = @_;
196  
197 0 ▶ if( defined $u && defined $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $u and defined $p) 0 0 0
Condition0%0/3
defined $u and defined $p
!ll&&!rl&&r
000
MC/DC0%0/2
defined $u and defined $p
defined $u defined $p
Truth table0%0/3
A and B
A= defined $uB= defined $p
ABresult
0X0
100
111
198 0 $self->{'_authentication'} = [ $u,$p];
199 }
200 0 return @{$self->{'_authentication'}};
201 }
202  
203 =head2 ids
204  
205 Title : ids
206 Usage : @ids = $db->ids([@ids])
207 Function: get/set matching ids
208 Returns : array of sequence ids
209 Args : (optional) array ref with new set of ids
210  
211 =cut
212  
213 0 ▶sub ids {
Subroutine ids: not called CC=5 SCAR=34.0
Pod documented
214 0 my $self = shift;
215 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
216 0 my $d = $self->{'_ids'};
217 0 my $arg = shift;
218 0 ▶ $self->{'_ids'} = ref $arg ? $arg : [$arg];
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 0 0 0
219 0 ▶ return $d ? @$d : ();
Branch: 1 branch
BranchTrueFalseTotal
$d ? : 0 0 0
220 } else {
221 0 $self->_fetch_ids;
222 0 ▶ return @{$self->{'_ids'} || []};
Condition0%0/2
$self->{'_ids'} || []
l!l
00
MC/DC0%0/1
$self->{'_ids'} || []
$self->{'_ids'}
Truth table0%0/2
A
A= $self->{'_ids'}
Aresult
11
00
223 }
224 }
225  
226 =head2 query
227  
228 Title : query
229 Usage : $query = $db->query([$query])
230 Function: get/set query string
231 Returns : string
232 Args : (optional) new query string
233  
234 =cut
235  
236 0 ▶sub query {
Subroutine query: not called CC=2 SCAR=17.9
Pod documented
237 0 my $self = shift;
238 0 my $d = $self->{'_query'};
239 0 ▶ $self->{'_query'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
240 0 $d;
241 }
242  
243 =head2 delay
244  
245 Title : delay
246 Usage : $secs = $self->delay([$secs])
247 Function: get/set number of seconds to delay between fetches
248 Returns : number of seconds to delay
249 Args : new value
250  
251 NOTE: the default is to use the value specified by delay_policy().
252 This can be overridden by calling this method, or by passing the
253 -delay argument to new().
254  
255 =cut
256  
257 0 ▶sub delay {
Subroutine delay: not called CC=2 SCAR=17.9
Pod documented
258 0 my $self = shift;
259 0 my $d = $self->{'_delay'};
260 0 ▶ $self->{'_delay'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
261 0 $d;
262 }
263  
264 =head2 delay_policy
265  
266 Title : delay_policy
267 Usage : $secs = $self->delay_policy
268 Function: return number of seconds to delay between calls to remote db
269 Returns : number of seconds to delay
270 Args : none
271  
272 NOTE: The default delay policy is 0s. Override in subclasses to
273 implement delays. The timer has only second resolution, so the delay
274 will actually be +/- 1s.
275  
276 =cut
277  
278 0 ▶sub delay_policy {
Subroutine delay_policy: not called CC=1 SCAR=6.9
Pod documented
279 0 my $self = shift;
280 0 return 0;
281 }
282  
283 =head2 _sleep
284  
285 Title : _sleep
286 Usage : $self->_sleep
287 Function: sleep for a number of seconds indicated by the delay policy
288 Returns : none
289 Args : none
290  
291 NOTE: This method keeps track of the last time it was called and only
292 imposes a sleep if it was called more recently than the delay_policy()
293 allows.
294  
295 =cut
296  
297 0 ▶sub _sleep {
Subroutine _sleep: not called CC=3 SCAR=24.8
298 0 my $self = shift;
299 0 my $last_invocation = $LAST_INVOCATION_TIME;
300 0 ▶ if (time - $LAST_INVOCATION_TIME < $self->delay) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $Bio::DB::Query::WebQuery::LAST_INVOCATION_TIME < $self->delay) 0 0 0
301 0 my $delay = $self->delay - (time - $LAST_INVOCATION_TIME);
302 0 ▶ warn "sleeping for $delay seconds\n" if $self->verbose > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 0 0 0 0
303 0 sleep $delay;
304 }
305 0 $LAST_INVOCATION_TIME = time;
306 }
307  
308 =head2 _fetch_ids
309  
310 Title : _fetch_ids
311 Usage : @ids = $db->_fetch_ids
312 Function: run query, get ids
313 Returns : array of sequence ids
314 Args : none
315  
316 =cut
317  
318 0 ▶sub _fetch_ids {
Subroutine _fetch_ids: not called CC=4 SCAR=30.0
319 0 my $self = shift;
320 0 $self->_run_query;
321 0 ▶ $self->_run_query(1) if $self->_truncated;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_truncated 0 0 0
322 0 ▶ $self->throw('Id list has been truncated even after maxids requested')
Branch: 1 branch
BranchTrueFalseTotal
if $self->_truncated 0 0 0
323 if $self->_truncated;
324 0 ▶ return @{$self->{'_ids'}} if $self->{'_ids'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_ids"} 0 0 0
325 }
326  
327 =head2 _run_query
328  
329 Title : _run_query
330 Usage : $success = $db->_run_query
331 Function: run query, parse results
332 Returns : true if successful
333 Args : none
334  
335 =cut
336  
337 0 ▶sub _run_query {
Subroutine _run_query: not called CC=4 SCAR=30.0
338 0 my $self = shift;
339 0 my $force = shift;
340  
341 # allow the query to be run one extra time if truncated
342 0 ▶ return $self->{'_ran_query'} if $self->{'_ran_query'}++ && !$force;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_ran_query"}++ and not $force 0 0 0
Condition0%0/3
$self->{"_ran_query"}++ and not $force
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_ran_query"}++ and not $force
$self->{"_ran_query"}++ not $force
Truth table0%0/3
A and not(B)
A= $self->{"_ran_query"}++B= not $force
ABresult
0X0
100
111
343  
344 0 my $request = $self->_get_request;
345 0 $self->debug("request is ".$request->url."\n");
346 0 my $response = $self->ua->request($request);
347 0 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
348 0 $self->debug("response is ".$response->content."\n");
349 0 $self->_parse_response($response->content);
350 0 1;
351 }
352  
353 =head2 _truncated
354  
355 Title : _truncated
356 Usage : $flag = $db->_truncated([$newflag])
357 Function: get/set truncation flag
358 Returns : boolean
359 Args : new flag
360  
361 Some databases will truncate output unless explicitly asked
362 not to. This flag allows a "two probe" attempt.
363  
364 =cut
365  
366 0 ▶sub _truncated {
Subroutine _truncated: not called CC=2 SCAR=17.9
367 0 my $self = shift;
368 0 my $d = $self->{'_truncated'};
369 0 ▶ $self->{'_truncated'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
370 0 $d;
371 }
372  
373 =head2 _get_request
374  
375 Title : _get_request
376 Usage : $http_request = $db->_get_request(@params)
377 Function: create an HTTP::Request with indicated parameters
378 Returns : HTTP::Request object
379 Args : CGI parameter list
380  
381 =cut
382  
383 0 ▶sub _get_request {
Subroutine _get_request: not called CC=3 SCAR=24.8
384 0 my $self = shift;
385 0 my ($method,$base,@params) = $self->_request_parameters;
386 0 my $uri = URI->new($base);
387 0 my $request;
388 0 ▶ if ($method eq 'get') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'get') { } 0 0 0
389 0 $uri->query_form(@params);
390 0 $request = GET $uri;
391 } else {
392 0 $request = POST $uri,\@params;
393 }
394  
395 0 ▶ $request->proxy_authorization_basic($self->authentication)
Branch: 1 branch
BranchTrueFalseTotal
if $self->authentication 0 0 0
396 if $self->authentication;
397 0 $request;
398 }
399  
400 =head2 _parse_response
401  
402 Title : _parse_response
403 Usage : $db->_parse_response($content)
404 Function: parse out response
405 Returns : empty
406 Args : none
407 Throws : 'unparseable output exception'
408  
409 NOTE: This method must be implemented by subclass.
410  
411 =cut
412  
413 0 ▶sub _parse_response {
Subroutine _parse_response: not called CC=1 SCAR=6.9
414 0 my $self = shift;
415 0 my $content = shift;
416 0 $self->throw_not_implemented;
417 }
418  
419 =head2 _request_parameters
420  
421 Title : _request_parameters
422 Usage : ($method,$base,@params = $db->_request_parameters
423 Function: return information needed to construct the request
424 Returns : list of method, url base and key=>value pairs
425 Args : none
426  
427 NOTE: This method must be implemented by subclass.
428  
429 =cut
430  
431 0 ▶sub _request_parameters {
Subroutine _request_parameters: not called CC=1 SCAR=6.9
432 0 my $self = shift;
433 0 $self->throw_not_implemented;
434 }
435  
436 =head2 _generate_id_string
437  
438 Title : _generate_id_string
439 Usage : $string = $db->_generate_id_string
440 Function: joins IDs together in string (implementation-dependent)
441 Returns : string of concatenated IDs
442 Args : array ref of ids (normally passed into the constructor)
443  
444 NOTE: This method must be implemented by subclass.
445  
446 =cut
447  
448 0 ▶sub _generate_id_string {
Subroutine _generate_id_string: not called CC=1 SCAR=6.9
449 0 my $self = shift;
450 0 $self->throw_not_implemented;
451 }
452  
453 1 1;
« Bio/Tools/Sim4/Results.pm ↑ Summary Bio/DB/Taxonomy/list.pm »