| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict 'vars'; |
| 4 |
use LWP::UserAgent; |
| 5 |
use URI::Escape; |
| 6 |
use Getopt::Long; |
| 7 |
use strict; |
| 8 |
use constant GENBANK_URL=>"http://www.ncbi.nlm.nih.gov/htbin-post/Entrez/query?form=4&html=no"; |
| 9 |
use constant CHUNKSIZE=>10000; |
| 10 |
my ($TERM,$AGE,$MAX,$DB,$VERBOSE,$BUFFER,$COUNT,$GOT,$GOT_TOTAL); |
| 11 |
|
| 12 |
GetOptions('max=i' => \$MAX, |
| 13 |
'db=s' =>\$DB, |
| 14 |
'age=i' => \$AGE, |
| 15 |
'count' => \$COUNT, |
| 16 |
'verbose!'=>\$VERBOSE) || die <<USAGE; |
| 17 |
Usage: $0 [options] <query string> |
| 18 |
Query GenBank for a list of accession numbers. The query |
| 19 |
string should be in the form recognized by NCBI\'s term parser. |
| 20 |
See http://www.ncbi.nlm.nih.gov/Entrez/linking.html |
| 21 |
for examples. |
| 22 |
|
| 23 |
Options: |
| 24 |
-db <database> Database to search (n) |
| 25 |
-max <max> Max entries to return (100) |
| 26 |
-age <days> Only fetch accessions entered <days> ago |
| 27 |
-verbose Show brief description line |
| 28 |
-count Just retrieve the count that would be retrieved |
| 29 |
|
| 30 |
Database specifiers: |
| 31 |
m MEDLINE |
| 32 |
p Protein |
| 33 |
n Nucleotide |
| 34 |
t 3-D structure |
| 35 |
c Genome |
| 36 |
Example search: |
| 37 |
gb_search -verbose -db n 'Oryza sativa[Organism] AND EST[Keyword]' |
| 38 |
Some common field modifiers: |
| 39 |
[All Fields] |
| 40 |
[Accession] |
| 41 |
[Author Name] |
| 42 |
[Feature Key] |
| 43 |
[Gene Name] |
| 44 |
[Keyword] |
| 45 |
[Organism] |
| 46 |
USAGE |
| 47 |
; |
| 48 |
|
| 49 |
$MAX ||= 100; |
| 50 |
$DB ||= 'n'; |
| 51 |
$DB = lc(substr($DB,0,1)); # first character, whatever it is |
| 52 |
$BUFFER = ''; |
| 53 |
my $max = $MAX > CHUNKSIZE ? CHUNKSIZE : $MAX; |
| 54 |
|
| 55 |
$TERM = GENBANK_URL . "&db=$DB&dispmax=$max&term=" . uri_escape("@ARGV"); |
| 56 |
$TERM .= "&relpubdate=$AGE" if $AGE > 0; |
| 57 |
$TERM .= "&Dopt=" . ($COUNT ? 'q' : 'd'); |
| 58 |
$GOT_TOTAL=0; |
| 59 |
my $agent = LWP::UserAgent->new(); |
| 60 |
|
| 61 |
while ($GOT_TOTAL < $MAX) { |
| 62 |
$GOT = 0; |
| 63 |
my $req = HTTP::Request->new('GET' => "$TERM&dispstart=$GOT_TOTAL"); |
| 64 |
my $response = $agent->request($req,\&process_text); |
| 65 |
die "Request failure: ",$response->status_line |
| 66 |
unless $response->is_success; |
| 67 |
last if $GOT < CHUNKSIZE; |
| 68 |
$GOT_TOTAL += $GOT; |
| 69 |
} |
| 70 |
exit 0; |
| 71 |
|
| 72 |
sub process_text ($$$) { |
| 73 |
my ($chunk,$response,$protocol) = @_; |
| 74 |
my ($position,$remainder); |
| 75 |
$BUFFER .= $chunk; |
| 76 |
|
| 77 |
if ($COUNT) { |
| 78 |
print $1,"\n" if $BUFFER=~/^(\d+)\n/; |
| 79 |
$GOT = $MAX; |
| 80 |
return; |
| 81 |
} |
| 82 |
elsif ($DB =~ /^[mt]$/) { |
| 83 |
while ($BUFFER=~/^\s+(\d+) (.+)$/mg) { |
| 84 |
print $VERBOSE ? "$1\t$2" : $1,"\n"; |
| 85 |
$position = pos($BUFFER); |
| 86 |
$GOT++; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
else { |
| 91 |
while ($BUFFER=~/^\s+\d+ (\w+)\n\n(.*)\ngi/mg) { |
| 92 |
print $VERBOSE ? "$1\t$2" : $1,"\n"; |
| 93 |
$position = pos($BUFFER); |
| 94 |
$GOT++; |
| 95 |
} |
| 96 |
} |
| 97 |
substr($BUFFER,0,$position) = ''; |
| 98 |
pos($BUFFER)=0; |
| 99 |
} |
| 100 |
|