-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathncbi_search.pl
executable file
·293 lines (243 loc) · 7.5 KB
/
ncbi_search.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env perl
#FILE: ncbi_search.pl
#AUTH: Paul Stothard ([email protected])
#DATE: April 18, 2020
#VERS: 1.2
use warnings;
use strict;
use Getopt::Long;
use URI::Escape;
use LWP::Protocol::https;
use LWP::UserAgent;
use HTTP::Request::Common;
my %param = (
query => undef,
output_file => undef,
database => undef,
return_type => '',
max_records => undef,
format => undef,
verbose => undef,
separate => undef,
url => 'https://www.ncbi.nlm.nih.gov/entrez/eutils',
retries => 0,
max_retries => 5,
help => undef
);
Getopt::Long::Configure('bundling');
GetOptions(
'q|query=s' => \$param{query},
'o|output_file=s' => \$param{output_file},
'd|database=s' => \$param{database},
'r|return_type=s' => \$param{return_type},
'm|max_records=i' => \$param{max_records},
's|separate' => \$param{separate},
'verbose|v' => \$param{verbose},
'h|help' => \$param{help}
);
if ( defined( $param{help} ) ) {
print_usage();
exit(0);
}
if ( !( defined( $param{query} ) )
or !( defined( $param{output_file} ) )
or !( defined( $param{database} ) ) )
{
print_usage();
exit(1);
}
$param{return_type} = lc( $param{return_type} );
$param{query} = uri_escape( $param{query} );
# Set up for the -split option
if ( $param{separate} ) {
if ( ( $param{return_type} eq 'gb' )
|| ( $param{return_type} eq 'gbwithparts' ) )
{
if ( !( -d $param{output_file} ) ) {
mkdir( $param{output_file}, 0775 )
or
die( "Could not create directory " . $param{output_file} . "." );
}
}
else {
$param{separate} = 0;
print
"-r is not 'gb' or 'gbwithparts', so the -s option will be ignored.\n";
}
}
search(%param);
sub search {
my %param = @_;
my $esearch = "$param{url}/esearch.fcgi?db=$param{database}"
. "&retmax=1&usehistory=y&term=$param{query}";
my $ua = LWP::UserAgent->new(
ssl_opts => { verify_hostname => 0 },
protocols_allowed => ['https'],
);
my $esearch_response = $ua->get($esearch);
my $esearch_result = $esearch_response->decoded_content;
while (
( !defined($esearch_result) )
|| (
!(
$esearch_result =~
m/<Count>(\d+)<\/Count>.*<QueryKey>(\d+)<\/QueryKey>.*<WebEnv>(\S+)<\/WebEnv>/s
)
)
)
{
if ( $esearch_result =~ m/<ERROR>(.*)<\/ERROR>/is ) {
die("ESearch returned an error: $1");
}
message( $param{verbose},
"ESearch results could not be parsed. Resubmitting query.\n" );
sleep(10);
if ( $param{retries} >= $param{max_retries} ) {
die("Too many failures--giving up search.");
}
$esearch_response = $ua->get($esearch);
$esearch_result = $esearch_response->decoded_content;
$param{retries}++;
}
$param{retries} = 0;
$esearch_result =~
m/<Count>(\d+)<\/Count>.*<QueryKey>(\d+)<\/QueryKey>.*<WebEnv>(\S+)<\/WebEnv>/s;
my $count = $1;
my $query_key = $2;
my $web_env = $3;
if ( defined( $param{max_records} ) ) {
if ( $count > $param{max_records} ) {
message( $param{verbose},
"Retrieving $param{max_records} records out of $count available records.\n"
);
$count = $param{max_records};
}
else {
message( $param{verbose},
"Retrieving $count records out of $count available records.\n"
);
}
}
else {
message( $param{verbose},
"Retrieving $count records out of $count available records.\n" );
}
my $retmax = 500;
if ( $param{separate} ) {
$retmax = 1;
}
if ( $retmax > $count ) {
$retmax = $count;
}
my $OUTFILE;
if ( !$param{separate} ) {
open( $OUTFILE, ">" . $param{output_file} )
or die("Error: Cannot open $param{output_file} : $!");
}
for (
my $retstart = 0 ;
$retstart < $count ;
$retstart = $retstart + $retmax
)
{
if ( $retmax == 1 ) {
message( $param{verbose},
"Downloading record " . ( $retstart + 1 ) . "\n" );
}
else {
message( $param{verbose},
"Downloading records "
. ( $retstart + 1 ) . " to "
. ( $retstart + $retmax )
. "\n" );
}
my $efetch =
"$param{url}/efetch.fcgi?rettype=$param{return_type}&retmode=text&retstart=$retstart&retmax=$retmax&db=$param{database}&query_key=$query_key&WebEnv=$web_env";
my $efetch_response = $ua->get($efetch);
my $efetch_result = $efetch_response->decoded_content;
while ( !defined($efetch_result) ) {
message( $param{verbose},
"EFetch results could not be parsed. Resubmitting query.\n" );
sleep(10);
if ( $param{retries} >= $param{max_retries} ) {
die("Too many failures--giving up search.");
}
$efetch_response = $ua->get($efetch);
$efetch_result = $efetch_response->decoded_content;
$param{retries}++;
}
$efetch_result =~ s/[^[:ascii:]]+//g;
if ( $param{separate} ) {
my $record_num = $retstart + 1;
write_separate_record( $efetch_result, $record_num );
}
else {
print( $OUTFILE $efetch_result );
}
unless (
( defined( $param{max_records} ) && ( $param{max_records} == 1 ) ) )
{
sleep(1);
}
}
if ( !$param{separate} ) {
close($OUTFILE)
or die("Error: Cannot close $param{output_file} file: $!");
}
}
sub write_separate_record {
my $record = shift;
my $record_num = shift;
if ( $record =~ /ACCESSION\s+(\S+)/ ) {
my $accession = $1;
my $filename = $param{output_file} . "/$accession.gbk";
my $count = 0;
while ( -e $filename ) {
$filename =
$param{output_file} . "/$accession" . '_' . "$count.gbk";
$count++;
}
open( my $RECORD_FILE, ">" . $filename );
print( $RECORD_FILE $record );
close($RECORD_FILE);
}
else {
print("Could not find accession line in record '$record_num'.\n");
}
}
sub message {
my $verbose = shift;
my $message = shift;
if ($verbose) {
print $message;
}
}
sub print_usage {
print <<BLOCK;
ncbi_search.pl - search NCBI databases.
DISPLAY HELP AND EXIT:
usage:
perl ncbi_search.pl -help
PERFORM NCBI SEARCH:
usage:
perl ncbi_search.pl -q <string> -o <file> -d <string> [Options]
required arguments:
-q - Entrez query text.
-o - Output file to create. If the -s option is used this is the output
directory to create.
-d - Name of the NCBI database to search, such as 'nuccore', 'protein', or
'gene'.
optional arguments:
-r - Type of information to download. For sequences, 'fasta' is typically
specified. The accepted formats depend on the database being queried. The
default is to specify no format.
-m - The maximum number of records to download. Default is to download all
records.
-s - Save each record as a separate file. This option is only supported for -r
values of 'gb' and 'gbwithparts'.
-v - Provide progress messages.
example usage:
perl ncbi_search.pl -q 'NC_045512[Accession]' -o NC_045512.gbk -d nuccore \\
-r gbwithparts
BLOCK
}