NCBIHelper.pm

filter: Statementstmt 79.9% 159 / 199 Branchbran 63.4% 80 / 126 Conditioncond 36.2% 25 / 69 MC/DCmcdc 8.1% 3 / 37 Subroutinesub 81.4% 22 / 27 Podpod 100% 18 / 18 totaltotal 64.5% 307 / 476 CC 88 SCAR 37.9
CC
88
Cov
67%
get_request
86.0
get_seq_stream
74.3
strand
14.9
CRAP 44.4
« Taxonomy/entrez.pm ↑ Summary Query/GenBank.pm »
1 #
2 # BioPerl module for Bio::DB::NCBIHelper
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Jason Stajich
7 #
8 # Copyright Jason Stajich
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 # Interfaces with new WebDBSeqI interface
15  
16 =head1 NAME
17  
18 Bio::DB::NCBIHelper - A collection of routines useful for queries to
19 NCBI databases.
20  
21 =head1 SYNOPSIS
22  
23 # Do not use this module directly.
24  
25 # get a Bio::DB::NCBIHelper object somehow
26 my $seqio = $db->get_Stream_by_acc(['J00522']);
27 foreach my $seq ( $seqio->next_seq ) {
28 # process seq
29 }
30  
31 =head1 DESCRIPTION
32  
33 Provides a single place to setup some common methods for querying NCBI
34 web databases. This module just centralizes the methods for
35 constructing a URL for querying NCBI GenBank and NCBI GenPept and the
36 common HTML stripping done in L<postprocess_data>().
37  
38 The base NCBI query URL used is:
39 https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi
40  
41 =head1 FEEDBACK
42  
43 =head2 Mailing Lists
44  
45 User feedback is an integral part of the
46 evolution of this and other Bioperl modules. Send
47 your comments and suggestions preferably to one
48 of the Bioperl mailing lists. Your participation
49 is much appreciated.
50  
51 bioperl-l@bioperl.org - General discussion
52 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
53  
54 =head2 Support
55  
56 Please direct usage questions or support issues to the mailing list:
57  
58 I<bioperl-l@bioperl.org>
59  
60 rather than to the module maintainer directly. Many experienced and
61 reponsive experts will be able look at the problem and quickly
62 address it. Please include a thorough description of the problem
63 with code and data examples if at all possible.
64  
65 =head2 Reporting Bugs
66  
67 Report bugs to the Bioperl bug tracking system to
68 help us keep track the bugs and their resolution.
69 Bug reports can be submitted via the web.
70  
71 https://github.com/bioperl/bioperl-live/issues
72  
73 =head1 AUTHOR - Jason Stajich
74  
75 Email jason@bioperl.org
76  
77 =head1 APPENDIX
78  
79 The rest of the documentation details each of the
80 object methods. Internal methods are usually
81 preceded with a _
82  
83 =cut
84  
85 # Let the code begin...
86  
87 package Bio::DB::NCBIHelper;
88 12 $Bio::DB::NCBIHelper::VERSION = '1.7.8';
89 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 12 ▶use Bio::DB::Query::GenBank;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 12 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 12 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 12 ▶use Bio::Root::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 # use Bio::DB::RefSeq;
96 12 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 12 ▶use base qw(Bio::DB::WebDBSeqI Bio::Root::Root);
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 12 our $HOSTBASE = 'https://eutils.ncbi.nlm.nih.gov';
101 12 our $MAX_ENTRIES = 19000;
102 12 our $REQUEST_DELAY = 4;
103 12 our %CGILOCATION = (
104 'batch' => [ 'post' => '/entrez/eutils/epost.fcgi' ],
105 'query' => [ 'get' => '/entrez/eutils/efetch.fcgi' ],
106 'single' => [ 'get' => '/entrez/eutils/efetch.fcgi' ],
107 'version' => [ 'get' => '/entrez/eutils/efetch.fcgi' ],
108 'gi' => [ 'get' => '/entrez/eutils/efetch.fcgi' ],
109 'webenv' => [ 'get' => '/entrez/eutils/efetch.fcgi' ]
110 );
111 12 our %FORMATMAP = (
112 'gb' => 'genbank',
113 'gp' => 'genbank',
114 'fasta' => 'fasta',
115 'asn.1' => 'entrezgene',
116 'gbwithparts' => 'genbank',
117 );
118 12 our $DEFAULTFORMAT = 'gb';
119  
120 =head2 new
121  
122 Title : new
123 Usage :
124 Function: the new way to make modules a little more lightweight
125 Returns :
126 Args :
127  
128 =cut
129  
130 14 ▶sub new {
Subroutine new: called CC=10 SCAR=23.7
Pod documented
131 14 my ( $class, @args ) = @_;
132 14 my $self = $class->SUPER::new(@args);
133 14 my ($seq_start, $seq_stop, $no_redirect,
134 $redirect, $complexity, $strand, $email
135 )
136 = $self->_rearrange(
137 [ qw(SEQ_START SEQ_STOP NO_REDIRECT REDIRECT_REFSEQ COMPLEXITY STRAND EMAIL) ],
138 @args
139 );
140 14 ▶ $seq_start && $self->seq_start($seq_start);
Branch: 1 branch
BranchTrueFalseTotal
$seq_start and $self->seq_start($seq_start) 1 13 14
141 14 ▶ $seq_stop && $self->seq_stop($seq_stop);
Branch: 1 branch
BranchTrueFalseTotal
$seq_stop and $self->seq_stop($seq_stop) 1 13 14
142 14 ▶ $no_redirect && $self->no_redirect($no_redirect);
Branch: 1 branch
BranchTrueFalseTotal
$no_redirect and $self->no_redirect($no_redirect) 0 14 14
143 14 ▶ $redirect && $self->redirect_refseq($redirect);
Branch: 1 branch
BranchTrueFalseTotal
$redirect and $self->redirect_refseq($redirect) 0 14 14
144 14 ▶ $strand && $self->strand($strand);
Branch: 1 branch
BranchTrueFalseTotal
$strand and $self->strand($strand) 0 14 14
145 14 ▶ $email && $self->email($email);
Branch: 1 branch
BranchTrueFalseTotal
$email and $self->email($email) 0 14 14
146  
147 # adjust statement to accept zero value
148 14 ▶ if (defined $complexity && $complexity >= 0 && $complexity <= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $complexity and $complexity >= 0 and $complexity <= 4) 1 13 14
Condition66%4/6
defined $complexity and $complexity >= 0 and $complexity <= 4
!ll&&!rl&&r
1301
defined $complexity and $complexity >= 0
!ll&&!rl&&r
1301
MC/DC33%1/3
defined $complexity and $complexity >= 0 and $complexity <= 4
defined $complexity $complexity >= 0 $complexity <= 4
Truth table50%2/4
A and B and C
A= defined $complexityB= $complexity >= 0C= $complexity <= 4
ABCresult
0XX0
10X0
1100
1111
149 1 $self->complexity($complexity)
150 }
151 14 return $self;
152 }
153  
154  
155 =head2 get_params
156  
157 Title : get_params
158 Usage : my %params = $self->get_params($mode)
159 Function: returns key,value pairs to be passed to NCBI database
160 for either 'batch' or 'single' sequence retrieval method
161 Returns : a key,value pair hash
162 Args : 'single' or 'batch' mode for retrieval
163  
164 =cut
165  
166 0 ▶sub get_params {
Subroutine get_params: not called CC=1 SCAR=6.9
Pod documented
167 0 my ($self, $mode) = @_;
168 0 $self->throw("subclass did not implement get_params");
169 }
170  
171 =head2 default_format
172  
173 Title : default_format
174 Usage : my $format = $self->default_format
175 Function: returns default sequence format for this module
176 Returns : string
177 Args : none
178  
179 =cut
180  
181 0 ▶sub default_format {
Subroutine default_format: not called CC=1 SCAR=6.9
Pod documented
182 0 return $DEFAULTFORMAT;
183 }
184  
185 =head2 get_request
186  
187 Title : get_request
188 Usage : my $url = $self->get_request
189 Function: HTTP::Request
190 Returns :
191 Args : %qualifiers = a hash of qualifiers (ids, format, etc)
192  
193 =cut
194  
195 28 ▶sub get_request {
Subroutine get_request: called CC=33 SCAR=44.5
Pod documented
196 28 my ( $self, @qualifiers ) = @_;
197 28 my ( $mode, $uids, $format, $query, $seq_start, $seq_stop, $strand,
198 $complexity, $email)
199 = $self->_rearrange(
200 [qw(MODE UIDS FORMAT QUERY SEQ_START SEQ_STOP STRAND COMPLEXITY EMAIL)],
201 @qualifiers );
202 28 $mode = lc $mode;
203 28 ▶ ($format) = $self->request_format() unless ( defined $format );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 28 28
204 0 ▶ if ( !defined $mode || $mode eq '' ) { $mode = 'single'; }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $mode or $mode eq "") 0 28 28
Condition33%1/3
not defined $mode or $mode eq ""
l!l&&r!l&&!r
0028
MC/DC0%0/2
not defined $mode or $mode eq ""
not defined $mode $mode eq ""
Truth table33%1/3
not(A) or B
A= not defined $modeB= $mode eq ""
ABresult
1X1
011
000
205 28 my %params = $self->get_params($mode);
206 28 ▶ if ( !%params ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%params) 0 28 28
207 0 $self->throw(
208 "must specify a valid retrieval mode 'single' or 'batch' not '$mode'"
209 );
210 }
211 28 my $url = URI->new( $HOSTBASE . $CGILOCATION{$mode}[1] );
212 28 ▶ unless ( $mode eq 'webenv' || defined $uids || defined $query ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mode eq "webenv" or defined $uids or defined $query) 0 28 28
Condition83%5/6
$mode eq "webenv" or defined $uids or defined $query
l!l&&r!l&&!r
2620
$mode eq "webenv" or defined $uids
l!l&&r!l&&!r
2242
MC/DC0%0/3
$mode eq "webenv" or defined $uids or defined $query
$mode eq "webenv" defined $uids defined $query
Truth table75%3/4
A or B or C
A= $mode eq "webenv"B= defined $uidsC= defined $query
ABCresult
1XX1
01X1
0011
0000
213 0 $self->throw("Must specify a query or list of uids to fetch");
214 }
215 28 ▶ if ( $query && $query->can('cookie') ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($query and $query->can('cookie')) { } 2 26 28
elsif ($query) { } 0 26 26
elsif ($mode eq 'webenv' and $self->can('cookie')) { } 2 24 26
elsif ($uids) { } 24 0 24
Condition66%2/3
$query and $query->can("cookie")
!ll&&!rl&&r
2602
MC/DC50%1/2
$query and $query->can("cookie")
$query $query->can("cookie")
Truth table66%2/3
A and B
A= $queryB= $query->can("cookie")
ABresult
0X0
100
111
216 2 @params{ 'WebEnv', 'query_key' } = $query->cookie;
217 2 $params{'db'} = $query->db;
218 }
219 elsif ($query) {
220 0 $params{'id'} = join ',', $query->ids;
221 }
222  
223 # for batch retrieval, non-query style
224 ▶ elsif ( $mode eq 'webenv' && $self->can('cookie') ) {
Condition66%2/3
$mode eq "webenv" and $self->can("cookie")
!ll&&!rl&&r
2402
MC/DC50%1/2
$mode eq "webenv" and $self->can("cookie")
$mode eq "webenv" $self->can("cookie")
Truth table66%2/3
A and B
A= $mode eq "webenv"B= $self->can("cookie")
ABresult
0X0
100
111
225 2 @params{ 'WebEnv', 'query_key' } = $self->cookie;
226 }
227 elsif ($uids) {
228 24 ▶ if ( ref($uids) =~ /array/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($uids) =~ /array/i) 14 10 24
229 14 $uids = join( ",", @$uids );
230 }
231 24 $params{'id'} = $uids;
232 }
233 28 ▶ $seq_start && ( $params{'seq_start'} = $seq_start );
Branch: 1 branch
BranchTrueFalseTotal
$seq_start and $params{"seq_start"} = $seq_start 1 27 28
234 28 ▶ $seq_stop && ( $params{'seq_stop'} = $seq_stop );
Branch: 1 branch
BranchTrueFalseTotal
$seq_stop and $params{"seq_stop"} = $seq_stop 1 27 28
235 28 ▶ $strand && ( $params{'strand'} = $strand );
Branch: 1 branch
BranchTrueFalseTotal
$strand and $params{"strand"} = $strand 0 28 28
236 28 ▶ $email && ( $params{'email'} = $email );
Branch: 1 branch
BranchTrueFalseTotal
$email and $params{"email"} = $email 0 28 28
237 28 ▶ if ( defined $complexity && ( $seq_start || $seq_stop || $strand ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $complexity and $seq_start || $seq_stop || $strand) 0 28 28
Condition44%4/9
defined $complexity and $seq_start || $seq_stop || $strand
!ll&&!rl&&r
2710
$seq_start or $seq_stop or $strand
l!l&&r!l&&!r
001
$seq_start or $seq_stop
l!l&&r!l&&!r
001
MC/DC0%0/4
defined $complexity and $seq_start || $seq_stop || $strand
defined $complexity $seq_start $seq_stop $strand
Truth table40%2/5
A and B or C or D
A= defined $complexityB= $seq_startC= $seq_stopD= $strand
ABCDresult
0XXX0
10000
11XX1
101X1
10011
238 0 ▶ $self->warn(
Branch: 1 branch
BranchTrueFalseTotal
if $complexity != 1 and $seq_start || $seq_stop 0 0 0
Condition0%0/6
$complexity != 1 and $seq_start || $seq_stop
!ll&&!rl&&r
000
$seq_start or $seq_stop
l!l&&r!l&&!r
000
MC/DC0%0/3
$complexity != 1 and $seq_start || $seq_stop
$complexity != 1 $seq_start $seq_stop
Truth table0%0/4
A and B or C
A= $complexity != 1B= $seq_startC= $seq_stop
ABCresult
0XX0
1000
11X1
1011
239 "Complexity set to $complexity; seq_start and seq_stop may not work!"
240 ) if ( $complexity != 1 && ( $seq_start || $seq_stop ) );
241 0 ▶ $self->warn(
Branch: 1 branch
BranchTrueFalseTotal
if $complexity == 0 and $strand == 2 and $format eq "fasta" 0 0 0
Condition0%0/6
$complexity == 0 and $strand == 2 and $format eq "fasta"
!ll&&!rl&&r
000
$complexity == 0 and $strand == 2
!ll&&!rl&&r
000
MC/DC0%0/3
$complexity == 0 and $strand == 2 and $format eq "fasta"
$complexity == 0 $strand == 2 $format eq "fasta"
Truth table0%0/4
A and B and C
A= $complexity == 0B= $strand == 2C= $format eq "fasta"
ABCresult
0XX0
10X0
1100
1111
242 "Complexity set to 0; expect strange results with strand set to 2"
243 ) if ( $complexity == 0 && $strand == 2 && $format eq 'fasta' );
244 }
245 28 ▶ defined $complexity && ( $params{'complexity'} = $complexity );
Branch: 1 branch
BranchTrueFalseTotal
defined $complexity and $params{"complexity"} = $complexity 1 27 28
246 28 ▶ $params{'rettype'} = $format unless $mode eq 'batch';
Branch: 1 branch
BranchTrueFalseTotal
unless $mode eq "batch" 26 2 28
247  
248 # for now, 'post' is batch retrieval
249 28 ▶ if ( $CGILOCATION{$mode}[0] eq 'post' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Bio::DB::NCBIHelper::CGILOCATION{$mode}[0] eq 'post') { } 2 26 28
250 2 my $response = $self->ua->request( POST $url, [%params] );
251 2 ▶ $response->proxy_authorization_basic( $self->authentication )
Branch: 1 branch
BranchTrueFalseTotal
if $self->authentication 0 2 2
252 if ( $self->authentication );
253 2 $self->_parse_response( $response->content );
254 2 my ( $cookie, $querykey ) = $self->cookie;
255 2 my %qualifiers = (
256 '-mode' => 'webenv',
257 '-seq_start' => $seq_start,
258 '-seq_stop' => $seq_stop,
259 '-strand' => $strand,
260 '-complexity' => $complexity,
261 '-format' => $format,
262 '-email' => $email
263 );
264 2 $self->_sleep();
265 2 return $self->get_request(%qualifiers);
266 }
267 else {
268 26 $url->query_form(%params);
269 26 return GET $url;
270 }
271 }
272  
273 =head2 get_seq_stream
274  
275 Title : get_seq_stream
276 Usage : my $seqio = $self->get_seq_stream(%qualifiers)
277 Function: builds a url and queries a web db
278 Returns : a Bio::SeqIO stream capable of producing sequence
279 Args : %qualifiers = a hash qualifiers that the implementing class
280 will process to make a url suitable for web querying
281  
282 =cut
283  
284 26 ▶sub get_seq_stream {
Subroutine get_seq_stream: called CC=26 SCAR=43.1
Pod documented
285 26 my ($self, %qualifiers) = @_;
286 26 my ($rformat, $ioformat) = $self->request_format();
287 26 my $seen = 0;
288 26 foreach my $key ( keys %qualifiers ) {
289 64 ▶ if( $key =~ /format/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /format/i) 12 52 64
290 12 $rformat = $qualifiers{$key};
291 12 $seen = 1;
292 }
293 }
294 26 ▶ $qualifiers{'-format'} = $rformat if( !$seen);
Branch: 1 branch
BranchTrueFalseTotal
unless $seen 14 12 26
295 26 ($rformat, $ioformat) = $self->request_format($rformat);
296 # These parameters are implemented for Bio::DB::GenBank objects only
297 26 ▶ if($self->isa('Bio::DB::GenBank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("Bio::DB::GenBank")) 19 7 26
298 19 ▶ $self->seq_start() && ($qualifiers{'-seq_start'} = $self->seq_start());
Branch: 1 branch
BranchTrueFalseTotal
$self->seq_start and $qualifiers{"-seq_start"} = $self->seq_start 1 18 19
299 19 ▶ $self->seq_stop() && ($qualifiers{'-seq_stop'} = $self->seq_stop());
Branch: 1 branch
BranchTrueFalseTotal
$self->seq_stop and $qualifiers{"-seq_stop"} = $self->seq_stop 1 18 19
300 19 ▶ $self->strand() && ($qualifiers{'-strand'} = $self->strand());
Branch: 1 branch
BranchTrueFalseTotal
$self->strand and $qualifiers{"-strand"} = $self->strand 0 19 19
301 19 ▶ $self->email() && ($qualifiers{'-email'} = $self->email());
Branch: 1 branch
BranchTrueFalseTotal
$self->email and $qualifiers{"-email"} = $self->email 0 19 19
302 19 ▶ defined $self->complexity() && ($qualifiers{'-complexity'} = $self->complexity());
Branch: 1 branch
BranchTrueFalseTotal
defined $self->complexity and $qualifiers{"-complexity"} = $self->complexity 1 18 19
303 }
304 26 my $request = $self->get_request(%qualifiers);
305 26 ▶ $request->proxy_authorization_basic($self->authentication)
Branch: 1 branch
BranchTrueFalseTotal
if $self->authentication 0 26 26
306 if ( $self->authentication);
307 26 $self->debug("request is ". $request->as_string(). "\n");
308  
309 # workaround for MSWin systems
310 26 ▶ $self->retrieval_type('io_string') if $self->retrieval_type =~ /pipeline/ && $^O =~ /^MSWin/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->retrieval_type =~ /pipeline/ and $^O =~ /^MSWin/ 0 26 26
Condition66%2/3
$self->retrieval_type =~ /pipeline/ and $^O =~ /^MSWin/
!ll&&!rl&&r
5210
MC/DC0%0/2
$self->retrieval_type =~ /pipeline/ and $^O =~ /^MSWin/
$self->retrieval_type =~ /pipeline/ $^O =~ /^MSWin/
Truth table66%2/3
A and B
A= $self->retrieval_type =~ /pipeline/B= $^O =~ /^MSWin/
ABresult
0X0
100
111
311  
312 26 ▶ if ($self->retrieval_type =~ /pipeline/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->retrieval_type =~ /pipeline/) 21 5 26
313 # Try to create a stream using POSIX fork-and-pipe facility.
314 # this is a *big* win when fetching thousands of sequences from
315 # a web database because we can return the first entry while
316 # transmission is still in progress.
317 # Also, no need to keep sequence in memory or in a temporary file.
318 # If this fails (Windows, MacOS 9), we fall back to non-pipelined access.
319  
320 # fork and pipe: _stream_request()=><STREAM>
321 21 my ($result,$stream) = $self->_open_pipe();
322  
323 21 ▶ if (defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) { } 21 0 21
324 21 $DB::fork_TTY = File::Spec->devnull; # prevents complaints from debugger
325 21 ▶ if (!$result) { # in child process
Branch: 1 branch
BranchTrueFalseTotal
if (not $result) { } 0 21 21
326 0 $self->_stream_request($request,$stream);
327 0 POSIX::_exit(0); #prevent END blocks from executing in this forked child
328 }
329 else {
330 21 return Bio::SeqIO->new('-verbose' => $self->verbose,
331 '-format' => $ioformat,
332 '-fh' => $stream);
333 }
334 }
335 else {
336 0 $self->retrieval_type('io_string');
337 }
338 }
339  
340 5 ▶ if ($self->retrieval_type =~ /temp/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->retrieval_type =~ /temp/i) 5 0 5
341 5 my $dir = $self->io->tempdir( CLEANUP => 1);
342 5 my ( $fh, $tmpfile) = $self->io()->tempfile( DIR => $dir );
343 5 close $fh;
344 5 my $resp = $self->_request($request, $tmpfile);
345 5 ▶ if( ! -e $tmpfile || -z $tmpfile || ! $resp->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $tmpfile or -z $tmpfile or not $resp->is_success) 0 5 5
Condition33%2/6
not -e $tmpfile or -z $tmpfile or not $resp->is_success
l!l&&r!l&&!r
005
not -e $tmpfile or -z $tmpfile
l!l&&r!l&&!r
005
MC/DC0%0/3
not -e $tmpfile or -z $tmpfile or not $resp->is_success
not -e $tmpfile -z $tmpfile not $resp->is_success
Truth table25%1/4
not(A) or B or not(C)
A= not -e $tmpfileB= -z $tmpfileC= not $resp->is_success
ABCresult
1XX1
01X1
0011
0000
346 0 $self->throw("WebDBSeqI Error - check query sequences!\n");
347 }
348 5 $self->postprocess_data('type' => 'file',
349 'location' => $tmpfile);
350 # this may get reset when requesting batch mode
351 5 ($rformat,$ioformat) = $self->request_format();
352 5 ▶ if( $self->verbose > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose > 0) 0 5 5
353 0 ▶ open my $ERR, '<', $tmpfile or $self->throw("Could not read file '$tmpfile': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $ERR, "<", $tmpfile or $self->throw("Could not read file '${tmpfile}': $!") 0 0 0
354 0 ▶ while(<$ERR>) { $self->debug($_);}
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $ERR)) 0 0 0
355 0 close $ERR;
356 }
357  
358 5 return Bio::SeqIO->new('-verbose' => $self->verbose,
359 '-format' => $ioformat,
360 '-file' => $tmpfile);
361 }
362  
363 0 ▶ if ($self->retrieval_type =~ /io_string/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->retrieval_type =~ /io_string/i) 0 0 0
364 0 my $resp = $self->_request($request);
365 0 my $content = $resp->content_ref;
366 0 $self->debug( "content is $$content\n");
367 0 ▶ if (!$resp->is_success() || length($$content) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $resp->is_success or length $$content == 0) 0 0 0
Condition0%0/3
not $resp->is_success or length $$content == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not $resp->is_success or length $$content == 0
not $resp->is_success length $$content == 0
Truth table0%0/3
not(A) or B
A= not $resp->is_successB= length $$content == 0
ABresult
1X1
011
000
368 0 $self->throw("WebDBSeqI Error - check query sequences!\n");
369 }
370 0 ($rformat,$ioformat) = $self->request_format();
371 0 $self->postprocess_data('type'=> 'string',
372 'location' => $content);
373 0 $self->debug( "str is $$content\n");
374 0 return Bio::SeqIO->new('-verbose' => $self->verbose,
375 '-format' => $ioformat,
376 '-fh' => new IO::String($$content));
377 }
378  
379 # if we got here, we don't know how to handle the retrieval type
380 0 $self->throw("retrieval type " . $self->retrieval_type .
381 " unsupported\n");
382 }
383  
384 =head2 get_Stream_by_batch
385  
386 Title : get_Stream_by_batch
387 Usage : $seq = $db->get_Stream_by_batch($ref);
388 Function: Retrieves Seq objects from Entrez 'en masse', rather than one
389 at a time. For large numbers of sequences, this is far superior
390 than get_Stream_by_id or get_Stream_by_acc.
391 Example :
392 Returns : a Bio::SeqIO stream object
393 Args : $ref : either an array reference, a filename, or a filehandle
394 from which to get the list of unique ids/accession numbers.
395  
396 NOTE: deprecated API. Use get_Stream_by_id() instead.
397  
398 =cut
399  
400 0 ▶*get_Stream_by_batch = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
401 0 my $self = shift;
402 0 $self->deprecated('get_Stream_by_batch() is deprecated; use get_Stream_by_id() instead');
403 0 $self->get_Stream_by_id(@_)
404 12 };
405  
406 =head2 get_Stream_by_query
407  
408 Title : get_Stream_by_query
409 Usage : $seq = $db->get_Stream_by_query($query);
410 Function: Retrieves Seq objects from Entrez 'en masse', rather than one
411 at a time. For large numbers of sequences, this is far superior
412 to get_Stream_by_id and get_Stream_by_acc.
413 Example :
414 Returns : a Bio::SeqIO stream object
415 Args : An Entrez query string or a Bio::DB::Query::GenBank object.
416 It is suggested that you create a Bio::DB::Query::GenBank object and get
417 the entry count before you fetch a potentially large stream.
418  
419 =cut
420  
421 2 ▶sub get_Stream_by_query {
Subroutine get_Stream_by_query: called CC=3 SCAR=13.3
Pod documented
422 2 my ($self, $query) = @_;
423 2 ▶ unless (ref $query && $query->can('query')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $query and $query->can("query")) 0 2 2
Condition33%1/3
ref $query and $query->can("query")
!ll&&!rl&&r
002
MC/DC0%0/2
ref $query and $query->can("query")
ref $query $query->can("query")
Truth table33%1/3
A and B
A= ref $queryB= $query->can("query")
ABresult
0X0
100
111
424 0 $query = Bio::DB::Query::GenBank->new($query);
425 }
426 2 return $self->get_seq_stream('-query' => $query, '-mode'=>'query');
427 }
428  
429 =head2 postprocess_data
430  
431 Title : postprocess_data
432 Usage : $self->postprocess_data ( 'type' => 'string',
433 'location' => \$datastr );
434 Function: Process downloaded data before loading into a Bio::SeqIO. This
435 works for Genbank and Genpept, other classes should override
436 it with their own method.
437 Returns : void
438 Args : hash with two keys:
439  
440 'type' can be 'string' or 'file'
441 'location' either file location or string reference containing data
442  
443 =cut
444  
445 5 ▶sub postprocess_data {
Subroutine postprocess_data: called CC=1 SCAR=0.0
Pod documented
446 # retain this in case postprocessing is needed at a future date
447 }
448  
449  
450 =head2 request_format
451  
452 Title : request_format
453 Usage : my ($req_format, $ioformat) = $self->request_format;
454 $self->request_format("genbank");
455 $self->request_format("fasta");
456 Function: Get/Set sequence format retrieval. The get-form will normally not
457 be used outside of this and derived modules.
458 Returns : Array of two strings, the first representing the format for
459 retrieval, and the second specifying the corresponding SeqIO format.
460 Args : $format = sequence format
461  
462 =cut
463  
464 87 ▶sub request_format {
Subroutine request_format: called CC=3 SCAR=11.1
Pod documented
465 87 my ( $self, $value ) = @_;
466 87 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 57 30 87
467 57 $value = lc $value;
468 57 ▶ if ( defined $FORMATMAP{$value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Bio::DB::NCBIHelper::FORMATMAP{$value}) { } 57 0 57
469 57 $self->{'_format'} = [ $value, $FORMATMAP{$value} ];
470 }
471 else {
472 # Try to fall back to a default. Alternatively, we could throw
473 # an exception
474 0 $self->{'_format'} = [ $value, $value ];
475 }
476 }
477 87 return @{ $self->{'_format'} };
478 }
479  
480  
481 =head2 redirect_refseq
482  
483 Title : redirect_refseq
484 Usage : $db->redirect_refseq(1)
485 Function: simple getter/setter which redirects RefSeqs to use Bio::DB::RefSeq
486 Returns : Boolean value
487 Args : Boolean value (optional)
488 Throws : 'unparseable output exception'
489 Note : This replaces 'no_redirect' as a more straightforward flag to
490 redirect possible RefSeqs to use Bio::DB::RefSeq (EBI interface)
491 instead of retrieving the NCBI records
492  
493 =cut
494  
495 0 ▶sub redirect_refseq {
Subroutine redirect_refseq: not called CC=1 SCAR=6.9
Pod documented
496 shift->throw(
497 0 "Use of redirect_refseq() is deprecated. Bio::DB::GenBank default is to\n".
498 "always retrieve from NCBI, including RefSeq sequences. Please use\n".
499 "Bio::DB::EMBL to retrieve records from EBI");
500 }
501  
502 =head2 complexity
503  
504 Title : complexity
505 Usage : $db->complexity(3)
506 Function: get/set complexity value
507 Returns : value from 0-4 indicating level of complexity
508 Args : value from 0-4 (optional); if unset server assumes 1
509 Throws : if arg is not an integer or falls outside of noted range above
510 Note : From efetch docs, the complexity regulates the display:
511  
512 0 - get the whole blob
513 1 - get the bioseq for gi of interest (default in Entrez)
514 2 - get the minimal bioseq-set containing the gi of interest
515 3 - get the minimal nuc-prot containing the gi of interest
516 4 - get the minimal pub-set containing the gi of interest
517  
518 =cut
519  
520 37 ▶sub complexity {
Subroutine complexity: called CC=5 SCAR=17.8
Pod documented
521 37 my ( $self, $comp ) = @_;
522 37 ▶ if ( defined $comp ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comp) 1 36 37
523 1 ▶ $self->throw("Complexity value must be integer between 0 and 4")
Branch: 1 branch
BranchTrueFalseTotal
if not $comp =~ /^\d+$/ or $comp < 0 or $comp > 4 0 1 1
Condition33%2/6
not $comp =~ /^\d+$/ or $comp < 0 or $comp > 4
l!l&&r!l&&!r
001
not $comp =~ /^\d+$/ or $comp < 0
l!l&&r!l&&!r
001
MC/DC0%0/3
not $comp =~ /^\d+$/ or $comp < 0 or $comp > 4
not $comp =~ /^\d+$/ $comp < 0 $comp > 4
Truth table25%1/4
not(A) or B or C
A= not $comp =~ /^\d+$/B= $comp < 0C= $comp > 4
ABCresult
1XX1
01X1
0011
0000
524 if $comp !~ /^\d+$/ || $comp < 0 || $comp > 4;
525 1 $self->{'_complexity'} = $comp;
526 }
527 37 return $self->{'_complexity'};
528 }
529  
530 =head2 strand
531  
532 Title : strand
533 Usage : $db->strand(1)
534 Function: get/set strand value
535 Returns : strand value if set
536 Args : value of 1 (plus) or 2 (minus); if unset server assumes 1
537 Throws : if arg is not an integer or is not 1 or 2
538 Note : This differs from BioPerl's use of strand: 1 = plus, -1 = minus 0 = not relevant.
539 We should probably add in some functionality to convert over in the future.
540  
541 =cut
542  
543 19 ▶sub strand {
Subroutine strand: called CC=5 SCAR=27.0
Pod documented
544 19 my ($self, $str) = @_;
545 19 ▶ if ($str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str) 0 19 19
546 0 ▶ $self->throw("strand() must be integer value of 1 (plus strand) or 2 (minus strand) if set") if
Branch: 1 branch
BranchTrueFalseTotal
if not $str =~ /^\d+$/ or $str < 1 or $str > 2 0 0 0
Condition0%0/6
not $str =~ /^\d+$/ or $str < 1 or $str > 2
l!l&&r!l&&!r
000
not $str =~ /^\d+$/ or $str < 1
l!l&&r!l&&!r
000
MC/DC0%0/3
not $str =~ /^\d+$/ or $str < 1 or $str > 2
not $str =~ /^\d+$/ $str < 1 $str > 2
Truth table0%0/4
not(A) or B or C
A= not $str =~ /^\d+$/B= $str < 1C= $str > 2
ABCresult
1XX1
01X1
0011
0000
547 $str !~ /^\d+$/ || $str < 1 || $str > 2;
548 0 $self->{'_strand'} = $str;
549 }
550 19 return $self->{'_strand'};
551 }
552  
553 =head2 seq_start
554  
555 Title : seq_start
556 Usage : $db->seq_start(123)
557 Function: get/set sequence start location
558 Returns : sequence start value if set
559 Args : integer; if unset server assumes 1
560 Throws : if arg is not an integer
561  
562 =cut
563  
564 21 ▶sub seq_start {
Subroutine seq_start: called CC=3 SCAR=11.0
Pod documented
565 21 my ($self, $start) = @_;
566 21 ▶ if ($start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start) 1 20 21
567 1 ▶ $self->throw("seq_start() must be integer value if set") if
Branch: 1 branch
BranchTrueFalseTotal
unless $start =~ /^\d+$/ 0 1 1
568 $start !~ /^\d+$/;
569 1 $self->{'_seq_start'} = $start;
570 }
571 21 return $self->{'_seq_start'};
572 }
573  
574 =head2 seq_stop
575  
576 Title : seq_stop
577 Usage : $db->seq_stop(456)
578 Function: get/set sequence stop (end) location
579 Returns : sequence stop (end) value if set
580 Args : integer; if unset server assumes 1
581 Throws : if arg is not an integer
582  
583 =cut
584  
585 21 ▶sub seq_stop {
Subroutine seq_stop: called CC=3 SCAR=11.0
Pod documented
586 21 my ($self, $stop) = @_;
587 21 ▶ if ($stop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stop) 1 20 21
588 1 ▶ $self->throw("seq_stop() must be integer if set") if
Branch: 1 branch
BranchTrueFalseTotal
unless $stop =~ /^\d+$/ 0 1 1
589 $stop !~ /^\d+$/;
590 1 $self->{'_seq_stop'} = $stop;
591 }
592 21 return $self->{'_seq_stop'};
593 }
594  
595 =head2 email
596  
597 Title : email
598 Usage : $db->email('foo@bar.edu')
599 Function: get/set email value
600 Returns : email (string) or undef
601 Args : string with a valid email address; note we do not vallidate this
602 currently!
603 Throws : if arg is not an integer or falls outside of noted range above
604 Note : This is required if you wish to speed up mulltiple requests faster
605 than 4s per request.
606  
607 =cut
608  
609 19 ▶sub email {
Subroutine email: called CC=2 SCAR=7.6
Pod documented
610 19 my ( $self, $email ) = @_;
611 19 ▶ if ( defined $email ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $email) 0 19 19
612 # TODO: validate email?
613 0 $self->{'_email'} = $email;
614 }
615 19 return $self->{'_email'};
616 }
617  
618 =head2 Bio::DB::WebDBSeqI methods
619  
620 Overriding WebDBSeqI method to help newbies to retrieve sequences
621  
622 =head2 get_Stream_by_acc
623  
624 Title : get_Stream_by_acc
625 Usage : $seq = $db->get_Stream_by_acc([$acc1, $acc2]);
626 Function: gets a series of Seq objects by accession numbers
627 Returns : a Bio::SeqIO stream object
628 Args : $ref : a reference to an array of accession numbers for
629 the desired sequence entries
630 Note : For GenBank, this just calls the same code for get_Stream_by_id()
631  
632 =cut
633  
634 9 ▶sub get_Stream_by_acc {
Subroutine get_Stream_by_acc: called CC=2 SCAR=6.9
Pod documented
635 9 my ( $self, $ids ) = @_;
636 9 ▶ $self->throw("NT_ contigs are whole chromosome files which are not part of regular"
Branch: 1 branch
BranchTrueFalseTotal
if $ids =~ /NT_/ 1 8 9
637 . "database distributions. Go to ftp://ftp.ncbi.nih.gov/genomes/.")
638 if $ids =~ /NT_/;
639 8 return $self->get_seq_stream( '-uids' => $ids, '-mode' => 'single' );
640 }
641  
642 =head2 delay_policy
643  
644 Title : delay_policy
645 Usage : $secs = $self->delay_policy
646 Function: NCBI requests a delay of 4 seconds between requests unless email is
647 provided. This method implements a 4 second delay; use 'delay()' to
648 override, though understand if no email is provided we are not
649 responsible for users being IP-blocked by NCBI
650 Returns : number of seconds to delay
651 Args : none
652  
653 =cut
654  
655 14 ▶sub delay_policy {
Subroutine delay_policy: called CC=1 SCAR=0.0
Pod documented
656 14 my $self = shift;
657 14 return $REQUEST_DELAY;
658 }
659  
660 =head2 cookie
661  
662 Title : cookie
663 Usage : ($cookie,$querynum) = $db->cookie
664 Function: return the NCBI query cookie, this information is used by
665 Bio::DB::GenBank in conjunction with efetch, ripped from
666 Bio::DB::Query::GenBank
667 Returns : list of (cookie,querynum)
668 Args : none
669  
670 =cut
671  
672 6 ▶sub cookie {
Subroutine cookie: called CC=2 SCAR=6.9
Pod documented
673 6 my $self = shift;
674 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 4 6
675 2 $self->{'_cookie'} = shift;
676 2 $self->{'_querynum'} = shift;
677 }
678 else {
679 4 return @{$self}{qw(_cookie _querynum)};
680 }
681 }
682  
683 =head2 _parse_response
684  
685 Title : _parse_response
686 Usage : $db->_parse_response($content)
687 Function: parse out response for cookie, this is a trimmed-down version
688 of _parse_response from Bio::DB::Query::GenBank
689 Returns : empty
690 Args : none
691 Throws : 'unparseable output exception'
692  
693 =cut
694  
695 2 ▶sub _parse_response {
Subroutine _parse_response: called CC=3 SCAR=11.8
696 2 my $self = shift;
697 2 my $content = shift;
698 2 ▶ if ( my ($warning) = $content =~ m!<ErrorList>(.+)</ErrorList>!s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($warning) = $content =~ m[<ErrorList>(.+)</ErrorList>]s) 0 2 2
699 0 $self->warn("Warning(s) from GenBank: $warning\n");
700 }
701 2 ▶ if ( my ($error) = $content =~ /<OutputMessage>([^<]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($error) = $content =~ /<OutputMessage>([^<]+)/) 0 2 2
702 0 $self->throw("Error from Genbank: $error");
703 }
704 2 my ($cookie) = $content =~ m!<WebEnv>(\S+)</WebEnv>!;
705 2 my ($querykey) = $content =~ m!<QueryKey>(\d+)!;
706 2 $self->cookie( uri_unescape($cookie), $querykey );
707 }
708  
709 =head2 no_redirect
710  
711 Title : no_redirect
712 Usage : $db->no_redirect($content)
713 Function: DEPRECATED - Used to indicate that Bio::DB::GenBank instance retrieves
714 possible RefSeqs from EBI instead; default behavior is now to
715 retrieve directly from NCBI
716 Returns : None
717 Args : None
718 Throws : Method is deprecated in favor of positive flag method 'redirect_refseq'
719  
720 =cut
721  
722 0 ▶sub no_redirect {
Subroutine no_redirect: not called CC=1 SCAR=6.9
Pod documented
723 shift->throw(
724 0 "Use of no_redirect() is deprecated. Bio::DB::GenBank new default is to always\n".
725 "retrieve from NCBI. Please use Bio::DB::EMBL to retrieve records\n").
726 "from EBI";
727 }
728  
729 12 1;
730  
731 __END__
« Taxonomy/entrez.pm ↑ Summary Query/GenBank.pm »