blib/lib/NCBIx/BigFetch.pm

filter: Statementstmt 53.0% 146 / 275 Branchbran 26.5% 17 / 64 Conditioncond 25.0% 1 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 55.5% 20 / 36 Podpod 66.6% 10 / 15 totaltotal 48.9% 194 / 396 CC 35 SCAR 21.5
CC
35
Cov
48%
_get_file_text
30.0
get_batch
20.0
_add_file_text
20.0
CRAP 8.6
↑ Summary
1 package NCBIx::BigFetch;
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Class::Std::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use YAML qw(DumpFile LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Time::HiRes qw(usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use version; our $VERSION = qv('0.5.6');
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $config_file = 'efetch_N.yml';
14 2 our $esearch_file = 'esearch_N.txt';
15 2 our $data_file = 'sequences_N_M.txt';
16 2 our $sleep_policy = 2_750_000;
17  
18 {
19 # These properties have defaults but can also be initialized by new()
20 2 my %project_id_of :ATTR( :get<project_id> :set<project_id> );
21 2 my %base_url_of :ATTR( :get<base_url> :set<base_url> );
22 2 my %base_dir_of :ATTR( :get<base_dir> :set<base_dir> );
23 2 my %db_of :ATTR( :get<db> :set<db> );
24 2 my %query_of :ATTR( :get<query> :set<query> );
25 2 my %index_of :ATTR( :get<index> :set<index> );
26 2 my %return_max_of :ATTR( :get<return_max> :set<return_max> );
27 2 my %return_type_of :ATTR( :get<return_type> :set<return_type> );
28 2 my %return_mode_of :ATTR( :get<return_mode> :set<return_mode> );
29 2 my %missing_of :ATTR( :get<missing> :set<missing> );
30  
31 # These properties are set by the code
32 2 my %start_date_of :ATTR( :get<start_date> :set<start_date> );
33 2 my %start_time_of :ATTR( :get<start_time> :set<start_time> );
34 2 my %querykey_of :ATTR( :get<querykey> :set<querykey> );
35 2 my %webenv_of :ATTR( :get<webenv> :set<webenv> );
36 2 my %count_of :ATTR( :get<count> :set<count> );
37  
38 0 ▶ sub next_index { my ($self) = @_; my $ident = ident $self; $index_of{$ident} += $return_max_of{$ident}; $self->_save(); }
Subroutine next_index: not called CC=1 SCAR=6.9
Pod documented
39  
40 3 ▶ sub get_config_filename { my ($self) = @_; my $project_id = $self->get_project_id(); $config_file =~ s/N/$project_id/; return $self->get_base_dir() . '/' . $config_file; }
Subroutine get_config_filename: called CC=1 SCAR=0.0
Pod undocumented
41 2 ▶ sub get_esearch_filename { my ($self) = @_; my $project_id = $self->get_project_id(); $esearch_file =~ s/N/$project_id/; return $self->get_base_dir() . '/' . $esearch_file; }
Subroutine get_esearch_filename: called CC=1 SCAR=0.0
Pod undocumented
42 0 ▶ sub get_data_filename { my ($self, $index) = @_; my $project_id = $self->get_project_id(); $index = defined($index) ? $index : $self->get_index(); my $filename = $data_file; $filename =~ s/N/$project_id/g; $filename =~ s/M/$index/g; return $self->get_base_dir() . '/' . $filename; }
Subroutine get_data_filename: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $index ? : 0 0 0
43  
44 1 ▶ sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.2
Pod undocumented
45 1 my ($self, $ident, $arg_ref) = @_;
46  
47 # Set environment
48 1 $self->_init( $arg_ref );
49
50 # Check for existing project
51 1 ▶ if (-e $self->get_config_filename()) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->get_config_filename) { } 0 1 1
52 0 $self->_status("Loading existing project");
53  
54 # Get existing config
55 0 $self->_load();
56 } else {
57 1 $self->_status("Starting new project");
58  
59 # Set start date and time
60 1 $self->_set_date();
61
62 # Submit search and parse results
63 1 $self->_search();
64
65 # Save config
66 1 $self->_save( $arg_ref );
67 }
68  
69 1 return;
70 }
71  
72 1 ▶ sub file_test {
Subroutine file_test: called CC=3 SCAR=11.1
Pod undocumented
73 1 my ( $self ) = @_;
74 1 my $file_written;
75  
76 # Get the file names for the test (/home/username/e* or /root/e*)
77 1 my $config_filename = $self->get_config_filename();
78 1 my $esearch_filename = $self->get_esearch_filename();
79  
80 # Remove exisitng files and count files written
81 1 ▶ if (-e $config_filename ) { $file_written++; `rm $config_filename`; }
Branch: 1 branch
BranchTrueFalseTotal
if (-e $config_filename) 1 0 1
82 1 ▶ if (-e $esearch_filename ) { $file_written++; `rm $esearch_filename`; }
Branch: 1 branch
BranchTrueFalseTotal
if (-e $esearch_filename) 1 0 1
83  
84 1 return $file_written;
85 }
86  
87 0 ▶ sub results_waiting {
Subroutine results_waiting: not called CC=2 SCAR=17.9
Pod documented
88 0 my ( $self ) = @_;
89 0 ▶ if ( $self->get_index() < $self->get_count() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_index < $self->get_count) { } 0 0 0
90 0 return 1;
91 } else {
92 0 $self->_status("Found " . $self->_commify( scalar(@{ $self->get_missing() }) ) . " missing batches." );
93 0 return 0;
94 }
95 }
96  
97 0 ▶ sub missing_batches {
Subroutine missing_batches: not called CC=2 SCAR=17.9
Pod documented
98 0 my ( $self ) = @_;
99 0 ▶ if ( @{ $self->get_missing() } ) { return 1; } else { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->get_missing;}) { } 0 0 0
100 }
101  
102 0 ▶ sub get_next_batch {
Subroutine get_next_batch: not called CC=1 SCAR=6.9
Pod documented
103 0 my ( $self ) = @_;
104 0 my $index = $self->get_index();
105  
106 # Get the batch
107 0 $self->get_batch( $index );
108  
109 # Update the index
110 0 $self->next_index();
111  
112 0 return;
113 }
114  
115 0 ▶ sub get_batch {
Subroutine get_batch: not called CC=4 SCAR=30.0
Pod documented
116 0 my ( $self, $index ) = @_;
117 0 my $return_max = $self->get_return_max();
118 0 my $return_type = $self->get_return_type();
119 0 my $return_mode = $self->get_return_mode();
120  
121 0 $self->_status("Starting with index " . $self->_commify( $index ) );
122
123 # Ethics requires we wait sleep_policy microseconds before retrieving
124 0 $self->_sleep();
125
126 # Define a batch through URL
127 0 my $efetch_url = $self->get_base_url() . 'efetch.fcgi?db=' . $self->get_db();
128 0 $efetch_url .= '&WebEnv=' . $self->get_webenv() . '&query_key=' . $self->get_querykey() . "&rettype=$return_type&retmode=$return_mode";
129 0 $efetch_url .= "&retstart=$index&retmax=$return_max";
130 0 $efetch_url .= '&tool=ncbix_bigfetch&email=roger@iosea.com';
131  
132 # Get the batch using LWP::Simple (get)
133 0 my $results = get($efetch_url);
134
135 # Check results # TODO: capture expired WebEnv and restart query
136 0 ▶ if ( $results =~ m/resource is temporarily unavailable/i ) { $self->note_missing_batch( $index ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($results =~ /resource is temporarily unavailable/i) 0 0 0
137 0 ▶ if ( $results =~ m/NCBI C\+\+ Exception/i ) { $self->note_missing_batch( $index ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($results =~ /NCBI C\+\+ Exception/i) 0 0 0
138 0 ▶ if ( $results eq '' ) { $self->note_missing_batch( $index ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($results eq "") 0 0 0
139  
140 # Save the sequences
141 0 $self->_set_file_text( $self->get_data_filename( $index ), $results );
142  
143 0 return;
144 }
145  
146 0 ▶ sub get_missing_batch {
Subroutine get_missing_batch: not called CC=1 SCAR=6.9
Pod documented
147 0 my ( $self ) = @_;
148  
149 # Get the next missing batch index
150 0 my @missing = @{ $self->get_missing() };
151 0 my $index = shift @missing;
152  
153 # Update the missing batch list
154 0 $self->set_missing( \@missing );
155  
156 # Get the batch
157 0 $self->get_batch( $index );
158  
159 0 return;
160 }
161  
162 0 ▶ sub note_missing_batch {
Subroutine note_missing_batch: not called CC=2 SCAR=17.9
Pod documented
163 0 my ( $self, $index ) = @_;
164 0 my @missing;
165 0 my $missing = $self->get_missing();
166 0 ▶ if ( defined $missing ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $missing) { } 0 0 0
167 0 @missing = @{ $missing };
168 } else {
169 0 @missing = ();
170 }
171 0 push @missing, $index;
172 0 $self->set_missing( \@missing );
173 0 $self->_save();
174 }
175  
176 0 ▶ sub get_sequence {
Subroutine get_sequence: not called CC=1 SCAR=6.9
Pod documented
177 0 my ( $self, $id ) = @_;
178 0 my $return_type = $self->get_return_type();
179 0 my $return_mode = $self->get_return_mode();
180  
181 0 $self->_status("Fetching sequence $id");
182
183 # Ethics requires we wait sleep_policy microseconds before retrieving
184 0 $self->_sleep();
185
186 # Define a batch through URL
187 0 my $efetch_url = $self->get_base_url() . 'efetch.fcgi?db=' . $self->get_db();
188 0 $efetch_url .= '&id=' . $id . "&rettype=$return_type&retmode=$return_mode";
189 0 $efetch_url .= '&tool=ncbix_bigfetch&email=roger@iosea.com';
190  
191 # Get the sequence
192 0 my $results = get($efetch_url);
193
194 # Save the sequences in missing file
195 0 $self->_add_file_text( $self->get_data_filename( 0 ), $results );
196  
197 0 return;
198 }
199  
200 0 ▶ sub unavailable_ids {
Subroutine unavailable_ids: not called CC=3 SCAR=24.8
Pod documented
201 0 my ( $self ) = @_;
202 0 my $count = $self->get_index();
203 0 my $return_max = $self->get_return_max();
204 0 my $index = 1;
205 0 my @unavailables = ();
206  
207 0 ▶ while ( $index < $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $count) 0 0 0
208 0 $self->_status("Checking " . $self->_commify( $index ) . " through " . $self->_commify( $index + $return_max - 1 ) );
209
210 # Get the sequences
211 0 my $text = $self->_get_file_text( $self->get_data_filename( $index ) );
212  
213 0 ▶ while ( $text =~ m/Error:\s(\d+)\sis\snot\savailable\sat\sthis\stime/g ) { push @unavailables, $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /Error:\s(\d+)\sis\snot\savailable\sat\sthis\stime/g) 0 0 0
214  
215 # Update the index
216 0 $index += $return_max;
217 }
218  
219 0 $self->_status("Found " . $self->_commify( scalar(@unavailables) ) . " unavailable ids." );
220  
221 0 return \@unavailables;
222 }
223  
224 1 ▶ sub _init {
Subroutine _init: called CC=11 SCAR=25.4
225 1 my ( $self, $arg_ref ) = @_;
226  
227 1 ▶ my $project_id = $arg_ref->{project_id} ? $arg_ref->{project_id} : "1";
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'project_id'} ? : 0 1 1
228 1 ▶ my $base_url = $arg_ref->{base_url} ? $arg_ref->{base_url} : "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/";
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'base_url'} ? : 0 1 1
229 1 ▶ my $base_dir = $arg_ref->{base_dir} ? $arg_ref->{base_dir} : $self->_get_base_dir();
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'base_dir'} ? : 0 1 1
230 1 ▶ my $db = $arg_ref->{db} ? $arg_ref->{db} : "protein";
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'db'} ? : 0 1 1
231 1 ▶ my $query = $arg_ref->{query} ? $arg_ref->{query} : "apoptosis";
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'query'} ? : 0 1 1
232 1 ▶ my $index = $arg_ref->{index} ? $arg_ref->{index} : "1";
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'index'} ? : 0 1 1
233 1 ▶ my $return_max = $arg_ref->{return_max} ? $arg_ref->{return_max} : "500";
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'return_max'} ? : 0 1 1
234 1 ▶ my $return_type = $arg_ref->{return_type} ? $arg_ref->{return_type} : "fasta";
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'return_type'} ? : 0 1 1
235 1 ▶ my $return_mode = $arg_ref->{return_mode} ? $arg_ref->{return_mode} : "text";
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'return_mode'} ? : 0 1 1
236 1 ▶ my $missing = $arg_ref->{missing} ? $arg_ref->{missing} : [];
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'missing'} ? : 0 1 1
237  
238 1 $self->set_project_id( $project_id );
239 1 $self->set_base_url( $base_url );
240 1 $self->set_base_dir( $base_dir );
241 1 $self->set_db( $db );
242 1 $self->set_query( $query );
243 1 $self->set_index( $index );
244 1 $self->set_return_max( $return_max );
245 1 $self->set_return_type( $return_type );
246 1 $self->set_return_mode( $return_mode );
247 1 $self->set_missing( $missing );
248  
249 1 return;
250 }
251  
252 1 ▶ sub _set_date {
Subroutine _set_date: called CC=1 SCAR=0.0
253 1 my ( $self ) = @_;
254  
255 1 my @time = localtime;
256 1 my $year = 1900 + $time[5];
257 1 my $month = $time[4] + 1; $month =~ s/^(\d)$/0$1/;
258 1 my $day = $time[3]; $day =~ s/^(\d)$/0$1/;
259 1 my $hour = $time[2]; $hour =~ s/^(\d)$/0$1/;
260 1 my $min = $time[1]; $min =~ s/^(\d)$/0$1/;
261 1 my $sec = $time[0]; $sec =~ s/^(\d)$/0$1/;
262
263 1 $self->set_start_date( "$year-$month-$day" );
264 1 $self->set_start_time( "$hour:$min:$sec" );
265  
266 1 return;
267 }
268  
269 1 ▶ sub _search {
Subroutine _search: called CC=1 SCAR=0.0
270 #my ( $self, $arg_ref ) = @_;
271 1 my ( $self, $arg_ref ) = @_;
272  
273 # Get search result ticket
274 1 my $esearch_url = $self->get_base_url() . 'esearch.fcgi?db=' . $self->get_db();
275 1 $esearch_url .= '&term=' . $self->get_query() . '&usehistory=y';
276 1 $esearch_url .= '&tool=ncbix_bigfetch&email=roger@iosea.com';
277 1 my $esearch_result = get($esearch_url);
278  
279 # Save search result
280 1 $self->_set_file_text( $self->get_esearch_filename(), $esearch_result );
281
282 # Parse the relevant keys
283 1 $esearch_result =~ m/<Count>([0-9]*)<\/Count>/g; $self->set_count( $1 );
284 1 $esearch_result =~ m/<QueryKey>([0-9]*)<\/QueryKey>/g; $self->set_querykey( $1 );
285 1 $esearch_result =~ m/<WebEnv>([\.a-zA-Z0-9_@\-]*)<\/WebEnv>/g; $self->set_webenv( $1 );
286  
287 1 return;
288 }
289  
290 0 ▶ sub _load {
Subroutine _load: not called CC=1 SCAR=6.9
291 0 my ( $self ) = @_;
292 0 my %config = %{ LoadFile( $self->get_config_filename() ) };
293 0 $self->set_project_id( $config{project_id} );
294 0 $self->set_base_url( $config{base_url} );
295 0 $self->set_base_dir( $config{base_dir} );
296 0 $self->set_db( $config{db} );
297 0 $self->set_query( $config{query} );
298 0 $self->set_querykey( $config{querykey} );
299 0 $self->set_webenv( $config{webenv} );
300 0 $self->set_count( $config{count} );
301 0 $self->set_index( $config{index} );
302 0 $self->set_start_date( $config{start_date} );
303 0 $self->set_start_time( $config{start_time} );
304 0 $self->set_return_max( $config{return_max} );
305 0 $self->set_return_type( $config{return_type} );
306 0 $self->set_return_mode( $config{return_mode} );
307 0 $self->set_missing( $config{missing} );
308 }
309  
310 1 ▶ sub _save {
Subroutine _save: called CC=2 SCAR=7.1
311 1 my ( $self, $arg_ref ) = @_;
312 1 my $ident = ident $self;
313 1 my $config;
314  
315 1 ▶ if (defined $arg_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg_ref) { } 1 0 1
316 1 $config = $arg_ref;
317 } else {
318 $config = { project_id => $project_id_of{$ident},
319 base_url => $base_url_of{$ident},
320 base_dir => $base_dir_of{$ident},
321 db => $db_of{$ident},
322 query => $query_of{$ident},
323 querykey => $querykey_of{$ident},
324 webenv => $webenv_of{$ident},
325 count => $count_of{$ident},
326 index => $index_of{$ident},
327 start_date => $start_date_of{$ident},
328 start_time => $start_time_of{$ident},
329 return_max => $return_max_of{$ident},
330 return_type => $return_type_of{$ident},
331 return_mode => $return_mode_of{$ident},
332 0 missing => $missing_of{$ident} };
333 }
334 1 DumpFile( $self->get_config_filename(), $config );
335 1 return;
336 }
337  
338 1 ▶ sub _get_base_dir {
Subroutine _get_base_dir: called CC=2 SCAR=7.2
339 1 my ( $self, $base_dir ) = @_;
340 1 chomp( my $id = `id -nu`);
341 0 ▶ if ($id eq 'root') { $base_dir = '/root'; } else { $base_dir = '/home/' . $id; }
Branch: 1 branch
BranchTrueFalseTotal
if ($id eq 'root') { } 0 1 1
342 1 return $base_dir;
343 }
344  
345 1 ▶ sub _status {
Subroutine _status: called CC=1 SCAR=0.0
346 1 my ( $self, $msg ) = @_;
347 1 print STDOUT " STATUS: $msg \n";
348 1 return;
349 }
350  
351 0 ▶ sub _sleep {
Subroutine _sleep: not called CC=1 SCAR=6.9
352 0 my ( $self ) = @_;
353 0 usleep($sleep_policy);
354 0 return;
355 }
356  
357 0 ▶ sub _get_file_text {
Subroutine _get_file_text: not called CC=5 SCAR=34.0
358 0 my ( $self, $path_file_name ) = @_;
359 0 my ($text, $line);
360 0 ▶ if (-e $path_file_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path_file_name) 0 0 0
361 0 ▶ open (my $IN, '<', $path_file_name) || croak( "Cannot open $path_file_name: $!" );
Branch: 1 branch
BranchTrueFalseTotal
open my $IN, "<", $path_file_name or croak("Cannot open ${path_file_name}: $!") 0 0 0
362 0 ▶ while ($line = <$IN>) { $text .= $line; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $IN)) 0 0 0
363 0 ▶ close ($IN) || croak( "Cannot close $path_file_name: $!" );
Branch: 1 branch
BranchTrueFalseTotal
close $IN or croak("Cannot close ${path_file_name}: $!") 0 0 0
364 }
365 0 return $text;
366 }
367
368 1 ▶ sub _set_file_text {
Subroutine _set_file_text: called CC=4 SCAR=14.9
369 1 my ( $self, $path_file_name, $text ) = @_;
370 1 ▶ open (my $OUT, '>', $path_file_name) || croak( "Cannot open $path_file_name: $!" );
Branch: 1 branch
BranchTrueFalseTotal
open my $OUT, ">", $path_file_name or croak("Cannot open ${path_file_name}: $!") 1 0 1
371 1 ▶ print $OUT $text || croak( "Cannot write $path_file_name: $!" );
Condition50%1/2
$text || croak("Cannot write ${path_file_name}: $!")
l!l
10
MC/DC0%0/1
$text || croak("Cannot write ${path_file_name}: $!")
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
372 1 ▶ close ($OUT) || croak( "Cannot close $path_file_name: $!" );
Branch: 1 branch
BranchTrueFalseTotal
close $OUT or croak("Cannot close ${path_file_name}: $!") 1 0 1
373 }
374
375 0 ▶ sub _add_file_text {
Subroutine _add_file_text: not called CC=4 SCAR=30.0
376 0 my ( $self, $path_file_name, $text ) = @_;
377 0 ▶ open (my $OUT, '>>', $path_file_name) || croak( "Cannot open $path_file_name: $!" );
Branch: 1 branch
BranchTrueFalseTotal
open my $OUT, ">>", $path_file_name or croak("Cannot open ${path_file_name}: $!") 0 0 0
378 0 ▶ print $OUT $text || croak( "Cannot write $path_file_name: $!" );
Condition0%0/2
$text || croak("Cannot write ${path_file_name}: $!")
l!l
00
MC/DC0%0/1
$text || croak("Cannot write ${path_file_name}: $!")
$text
Truth table0%0/2
A
A= $text
Aresult
11
00
379 0 ▶ close ($OUT) || croak( "Cannot close $path_file_name: $!" );
Branch: 1 branch
BranchTrueFalseTotal
close $OUT or croak("Cannot close ${path_file_name}: $!") 0 0 0
380 }
381  
382 0 ▶ sub _commify { # Perl Cookbook 2.17
Subroutine _commify: not called CC=1 SCAR=6.9
383 0 my ( $self, $string ) = @_;
384 0 my $text = reverse $string;
385 0 $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
386 0 return scalar reverse $text;
387 }
388
389 0 ▶ sub authors { return 'Roger Hall <roger@iosea.com>, Michael Bauer <mbkodos@gmail.org>, Kamakshi Duvvuru <kduvvuru@gmail.com>. Copyleft (C) 2009'; }
Subroutine authors: not called CC=1 SCAR=6.9
Pod documented
390 }
391  
392 2 1; # Magic true value required at end of module
393 __END__
↑ Summary