2 # BioPerl module for Bio::DB::WebQuery.pm
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Lincoln Stein <lstein@cshl.org>
8 # Copyright Lincoln Stein
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
17 Bio::DB::Query::WebQuery - Helper class for web-based sequence queryies
21 # Do not use this class directly. See Bio::DB::QueryI and one of
22 # the implementor classes (such as Bio::DB::GenBankQuery) for
25 See L<Bio::DB::QueryI>, L<Bio::DB::GenBankQuery>
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.
33 Those writing subclasses must define _get_params() and
34 _parse_response(), and possibly override _request_method().
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
46 bioperl-l@bioperl.org - General discussion
47 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
51 Please direct usage questions or support issues to the mailing list:
53 I<bioperl-l@bioperl.org>
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.
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
66 https://github.com/bioperl/bioperl-live/issues
68 =head1 AUTHOR - Lincoln Stein
74 The rest of the documentation details each of the
75 object methods. Internal methods are usually
80 # Let the code begin...
82 package Bio
::DB
::Query
::WebQuery
;
86 use HTTP
::Request
::Common
;
88 use base
qw(Bio::Root::Root Bio::DB::QueryI);
93 Usage : $db = Bio::DB::WebQuery->new(@args)
94 Function: create new query object
95 Returns : new query object
96 Args : -db database (e.g. 'protein')
97 -ids array ref of ids (overrides query)
98 -verbose turn on verbose debugging
100 This method creates a new query object. Typically you will specify a
101 -db and a -query argument. The value of -query is a database-specific
104 If you provide an array reference of IDs in -ids, the query will be
105 ignored and the list of IDs will be used when the query is passed to
110 # Borrowed shamelessly from WebDBSeqI. Some of this code should be
114 my $self = $class->SUPER::new
(@_);
116 my ($query,$ids,$verbose) = $self->_rearrange(['QUERY','IDS','VERBOSE'],@_);
117 $self->throw('must provide one of the the -query or -ids arguments')
118 unless defined($query) || defined($ids);
120 $query = $self->_generate_id_string($ids);
122 $self->query($query);
123 $verbose && $self->verbose($verbose);
125 my $ua = LWP
::UserAgent
->new(env_proxy
=> 1);
126 $ua->agent(ref($self) ."/".($Bio::DB
::Query
::WebQuery
::VERSION
|| '0.1'));
128 $self->{'_authentication'} = [];
135 Usage : my $ua = $self->ua or
137 Function: Get/Set a LWP::UserAgent for use
138 Returns : reference to LWP::UserAgent Object
139 Args : $ua - must be a LWP::UserAgent
144 my ($self, $ua) = @_;
145 my $d = $self->{'_ua'};
146 if( defined $ua && $ua->isa("LWP::UserAgent") ) {
147 $self->{'_ua'} = $ua;
155 Usage : $httpproxy = $db->proxy('http') or
156 $db->proxy(['http','ftp'], 'http://myproxy' )
157 Function: Get/Set a proxy for use of proxy
158 Returns : a string indicating the proxy
159 Args : $protocol : an array ref of the protocol(s) to set/get
160 $proxyurl : url of the proxy to use for the specified protocol
161 $username : username (if proxy requires authentication)
162 $password : password (if proxy requires authentication)
167 my ($self,$protocol,$proxy,$username,$password) = @_;
168 return undef if ( !defined $self->ua || !defined $protocol
169 || !defined $proxy );
170 $self->authentication($username, $password)
171 if ($username && $password);
172 return $self->ua->proxy($protocol,$proxy);
175 =head2 authentication
177 Title : authentication
178 Usage : $db->authentication($user,$pass)
179 Function: Get/Set authentication credentials
180 Returns : Array of user/pass
181 Args : Array or user/pass
187 my ($self,$u,$p) = @_;
189 if( defined $u && defined $p ) {
190 $self->{'_authentication'} = [ $u,$p];
192 return @
{$self->{'_authentication'}};
198 Usage : @ids = $db->ids([@ids])
199 Function: get/set matching ids
200 Returns : array of sequence ids
201 Args : (optional) array ref with new set of ids
208 my $d = $self->{'_ids'};
210 $self->{'_ids'} = ref $arg ?
$arg : [$arg];
211 return $d ? @
$d : ();
214 return @
{$self->{'_ids'} || []};
221 Usage : $query = $db->query([$query])
222 Function: get/set query string
224 Args : (optional) new query string
230 my $d = $self->{'_query'};
231 $self->{'_query'} = shift if @_;
238 Usage : @ids = $db->_fetch_ids
239 Function: run query, get ids
240 Returns : array of sequence ids
248 $self->_run_query(1) if $self->_truncated;
249 $self->throw('Id list has been truncated even after maxids requested')
250 if $self->_truncated;
251 return @
{$self->{'_ids'}} if $self->{'_ids'};
257 Usage : $success = $db->_run_query
258 Function: run query, parse results
259 Returns : true if successful
268 # allow the query to be run one extra time if truncated
269 return $self->{'_ran_query'} if $self->{'_ran_query'}++ && !$force;
271 my $request = $self->_get_request;
272 $self->debug("request is ".$request->url."\n");
273 my $response = $self->ua->request($request);
274 return unless $response->is_success;
275 $self->debug("response is ".$response->content."\n");
276 $self->_parse_response($response->content);
283 Usage : $flag = $db->_truncated([$newflag])
284 Function: get/set truncation flag
288 Some databases will truncate output unless explicitly asked
289 not to. This flag allows a "two probe" attempt.
295 my $d = $self->{'_truncated'};
296 $self->{'_truncated'} = shift if @_;
303 Usage : $http_request = $db->_get_request(@params)
304 Function: create an HTTP::Request with indicated parameters
305 Returns : HTTP::Request object
306 Args : CGI parameter list
312 my ($method,$base,@params) = $self->_request_parameters;
313 my $uri = URI
->new($base);
315 if ($method eq 'get') {
316 $uri->query_form(@params);
319 $request = POST
$uri,\
@params;
322 $request->proxy_authorization_basic($self->authentication)
323 if $self->authentication;
327 =head2 _parse_response
329 Title : _parse_response
330 Usage : $db->_parse_response($content)
331 Function: parse out response
334 Throws : 'unparseable output exception'
336 NOTE: This method must be implemented by subclass.
340 sub _parse_response
{
343 $self->throw_not_implemented;
346 =head2 _request_parameters
348 Title : _request_parameters
349 Usage : ($method,$base,@params = $db->_request_parameters
350 Function: return information needed to construct the request
351 Returns : list of method, url base and key=>value pairs
354 NOTE: This method must be implemented by subclass.
358 sub _request_parameters
{
360 $self->throw_not_implemented;
363 =head2 _generate_id_string
365 Title : _generate_id_string
366 Usage : $string = $db->_generate_id_string
367 Function: joins IDs together in string (implementation-dependent)
368 Returns : string of concatenated IDs
369 Args : array ref of ids (normally passed into the constructor)
371 NOTE: This method must be implemented by subclass.
375 sub _generate_id_string
{
377 $self->throw_not_implemented;