Bio/DB/Query/WebQuery.pm

filter: Statementstmt 18.0% 17 / 94 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 27.7% 5 / 18 Podpod 100% 6 / 6 totaltotal 14.3% 28 / 195 CC 29 SCAR 30.4
CC
29
Cov
11%
new
42.0
proxy
42.0
ids
30.0
CRAP 20.8
« 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.6';
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 base qw(Bio::Root::Root Bio::DB::QueryI);
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 =head2 new
92  
93 Title : new
94 Usage : $db = Bio::DB::WebQuery->new(@args)
95 Function: create new query object
96 Returns : new query object
97 Args : -db database (e.g. 'protein')
98 -ids array ref of ids (overrides query)
99 -verbose turn on verbose debugging
100  
101 This method creates a new query object. Typically you will specify a
102 -db and a -query argument. The value of -query is a database-specific
103 string.
104  
105 If you provide an array reference of IDs in -ids, the query will be
106 ignored and the list of IDs will be used when the query is passed to
107 the database.
108  
109 =cut
110  
111 # Borrowed shamelessly from WebDBSeqI. Some of this code should be
112 # refactored.
113 sub new {
114 0 ▶ my $class = shift;
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
115 0 my $self = $class->SUPER::new(@_);
116  
117 0 my ($query,$ids,$verbose) = $self->_rearrange(['QUERY','IDS','VERBOSE'],@_);
118 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
119 unless defined($query) || defined($ids);
120 0 ▶ if ($ids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ids) 0 0 0
121 0 $query = $self->_generate_id_string($ids);
122 }
123 0 $self->query($query);
124 0 ▶ $verbose && $self->verbose($verbose);
Branch: 1 branch
BranchTrueFalseTotal
$verbose and $self->verbose($verbose) 0 0 0
125  
126 0 my $ua = LWP::UserAgent->new(env_proxy => 1);
127 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
128 0 $self->ua($ua);
129 0 $self->{'_authentication'} = [];
130 0 $self;
131 }
132  
133 =head2 ua
134  
135 Title : ua
136 Usage : my $ua = $self->ua or
137 $self->ua($ua)
138 Function: Get/Set a LWP::UserAgent for use
139 Returns : reference to LWP::UserAgent Object
140 Args : $ua - must be a LWP::UserAgent
141  
142 =cut
143  
144 sub ua {
145 0 ▶ my ($self, $ua) = @_;
Subroutine ua: not called CC=3 SCAR=24.8
Pod documented
146 0 my $d = $self->{'_ua'};
147 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
148 0 $self->{'_ua'} = $ua;
149 }
150 0 $d;
151 }
152  
153 =head2 proxy
154  
155 Title : proxy
156 Usage : $httpproxy = $db->proxy('http') or
157 $db->proxy(['http','ftp'], 'http://myproxy' )
158 Function: Get/Set a proxy for use of proxy
159 Returns : a string indicating the proxy
160 Args : $protocol : an array ref of the protocol(s) to set/get
161 $proxyurl : url of the proxy to use for the specified protocol
162 $username : username (if proxy requires authentication)
163 $password : password (if proxy requires authentication)
164  
165 =cut
166  
167 sub proxy {
168 0 ▶ my ($self,$protocol,$proxy,$username,$password) = @_;
Subroutine proxy: not called CC=6 SCAR=37.4
Pod documented
169 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
170 || !defined $proxy );
171 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
172 if ($username && $password);
173 0 return $self->ua->proxy($protocol,$proxy);
174 }
175  
176 =head2 authentication
177  
178 Title : authentication
179 Usage : $db->authentication($user,$pass)
180 Function: Get/Set authentication credentials
181 Returns : Array of user/pass
182 Args : Array or user/pass
183  
184  
185 =cut
186  
187 sub authentication{
188 0 ▶ my ($self,$u,$p) = @_;
Subroutine authentication: not called CC=3 SCAR=24.8
Pod documented
189  
190 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
191 0 $self->{'_authentication'} = [ $u,$p];
192 }
193 0 return @{$self->{'_authentication'}};
194 }
195  
196 =head2 ids
197  
198 Title : ids
199 Usage : @ids = $db->ids([@ids])
200 Function: get/set matching ids
201 Returns : array of sequence ids
202 Args : (optional) array ref with new set of ids
203  
204 =cut
205  
206 sub ids {
207 0 ▶ my $self = shift;
Subroutine ids: not called CC=5 SCAR=34.0
Pod documented
208 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
209 0 my $d = $self->{'_ids'};
210 0 my $arg = shift;
211 0 ▶ $self->{'_ids'} = ref $arg ? $arg : [$arg];
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 0 0 0
212 0 ▶ return $d ? @$d : ();
Branch: 1 branch
BranchTrueFalseTotal
$d ? : 0 0 0
213 } else {
214 0 $self->_fetch_ids;
215 0 ▶ return @{$self->{'_ids'} || []};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_ids"} or [] 0 0 0
216 }
217 }
218  
219 =head2 query
220  
221 Title : query
222 Usage : $query = $db->query([$query])
223 Function: get/set query string
224 Returns : string
225 Args : (optional) new query string
226  
227 =cut
228  
229 sub query {
230 0 ▶ my $self = shift;
Subroutine query: not called CC=2 SCAR=17.9
Pod documented
231 0 my $d = $self->{'_query'};
232 0 ▶ $self->{'_query'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
233 0 $d;
234 }
235  
236 =head2 _fetch_ids
237  
238 Title : _fetch_ids
239 Usage : @ids = $db->_fetch_ids
240 Function: run query, get ids
241 Returns : array of sequence ids
242 Args : none
243  
244 =cut
245  
246 sub _fetch_ids {
247 0 ▶ my $self = shift;
Subroutine _fetch_ids: not called CC=4 SCAR=30.0
248 0 $self->_run_query;
249 0 ▶ $self->_run_query(1) if $self->_truncated;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_truncated 0 0 0
250 0 ▶ $self->throw('Id list has been truncated even after maxids requested')
Branch: 1 branch
BranchTrueFalseTotal
if $self->_truncated 0 0 0
251 if $self->_truncated;
252 0 ▶ return @{$self->{'_ids'}} if $self->{'_ids'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_ids"} 0 0 0
253 }
254  
255 =head2 _run_query
256  
257 Title : _run_query
258 Usage : $success = $db->_run_query
259 Function: run query, parse results
260 Returns : true if successful
261 Args : none
262  
263 =cut
264  
265 sub _run_query {
266 0 ▶ my $self = shift;
Subroutine _run_query: not called CC=4 SCAR=30.0
267 0 my $force = shift;
268  
269 # allow the query to be run one extra time if truncated
270 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
271  
272 0 my $request = $self->_get_request;
273 0 $self->debug("request is ".$request->url."\n");
274 0 my $response = $self->ua->request($request);
275 0 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
276 0 $self->debug("response is ".$response->content."\n");
277 0 $self->_parse_response($response->content);
278 0 1;
279 }
280  
281 =head2 _truncated
282  
283 Title : _truncated
284 Usage : $flag = $db->_truncated([$newflag])
285 Function: get/set truncation flag
286 Returns : boolean
287 Args : new flag
288  
289 Some databases will truncate output unless explicitly asked
290 not to. This flag allows a "two probe" attempt.
291  
292 =cut
293  
294 sub _truncated {
295 0 ▶ my $self = shift;
Subroutine _truncated: not called CC=2 SCAR=17.9
296 0 my $d = $self->{'_truncated'};
297 0 ▶ $self->{'_truncated'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
298 0 $d;
299 }
300  
301 =head2 _get_request
302  
303 Title : _get_request
304 Usage : $http_request = $db->_get_request(@params)
305 Function: create an HTTP::Request with indicated parameters
306 Returns : HTTP::Request object
307 Args : CGI parameter list
308  
309 =cut
310  
311 sub _get_request {
312 0 ▶ my $self = shift;
Subroutine _get_request: not called CC=3 SCAR=24.8
313 0 my ($method,$base,@params) = $self->_request_parameters;
314 0 my $uri = URI->new($base);
315 0 my $request;
316 0 ▶ if ($method eq 'get') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'get') { } 0 0 0
317 0 $uri->query_form(@params);
318 0 $request = GET $uri;
319 } else {
320 0 $request = POST $uri,\@params;
321 }
322  
323 0 ▶ $request->proxy_authorization_basic($self->authentication)
Branch: 1 branch
BranchTrueFalseTotal
if $self->authentication 0 0 0
324 if $self->authentication;
325 0 $request;
326 }
327  
328 =head2 _parse_response
329  
330 Title : _parse_response
331 Usage : $db->_parse_response($content)
332 Function: parse out response
333 Returns : empty
334 Args : none
335 Throws : 'unparseable output exception'
336  
337 NOTE: This method must be implemented by subclass.
338  
339 =cut
340  
341 sub _parse_response {
342 0 ▶ my $self = shift;
Subroutine _parse_response: not called CC=1 SCAR=6.9
343 0 my $content = shift;
344 0 $self->throw_not_implemented;
345 }
346  
347 =head2 _request_parameters
348  
349 Title : _request_parameters
350 Usage : ($method,$base,@params = $db->_request_parameters
351 Function: return information needed to construct the request
352 Returns : list of method, url base and key=>value pairs
353 Args : none
354  
355 NOTE: This method must be implemented by subclass.
356  
357 =cut
358  
359 sub _request_parameters {
360 0 ▶ my $self = shift;
Subroutine _request_parameters: not called CC=1 SCAR=6.9
361 0 $self->throw_not_implemented;
362 }
363  
364 =head2 _generate_id_string
365  
366 Title : _generate_id_string
367 Usage : $string = $db->_generate_id_string
368 Function: joins IDs together in string (implementation-dependent)
369 Returns : string of concatenated IDs
370 Args : array ref of ids (normally passed into the constructor)
371  
372 NOTE: This method must be implemented by subclass.
373  
374 =cut
375  
376 sub _generate_id_string {
377 0 ▶ my $self = shift;
Subroutine _generate_id_string: not called CC=1 SCAR=6.9
378 0 $self->throw_not_implemented;
379 }
380  
381 1 1;
« Bio/Tools/Sim4/Results.pm ↑ Summary Bio/DB/Taxonomy/list.pm »