3 # get_ncbi_results retreives files of search results from ncbi, and is
4 # released under the terms of the GPL version 2, or any later version,
5 # at your option. See the file README and COPYING for more
8 # Copyright 2004 by Don Armstrong <don@donarmstrong.com>.
10 # $Id: ss,v 1.1 2004/06/29 05:26:35 don Exp $
22 get_ncbi_results [options]
24 get_ncbi_results - Retrieve search results from NCBI using parameters
31 --format, -f format of search results to return [default xml]
32 --database, -b database to search for results [default gene]
33 --dir, -D directory to stick results into [default .]
34 --name, -n file naming scheme [default ${search}_results.$format]
35 --terms, -t file of search terms [default -]
36 --debug, -d debugging level [default 0]
37 --help, -h display this help
38 --man, -m display manual
46 Debug verbosity. (Default 0)
50 Display brief useage information.
60 get_ncbi_results -f xml -b gene -D ./results/ -n '${search}_name.xml' < search_parameters
62 Will pretty much do what you want
68 use vars qw($DEBUG $REVISION);
71 ($REVISION) = q$LastChangedRevision: 1$ =~ /LastChangedRevision:\s+([^\s]+)/;
72 $DEBUG = 0 unless defined $DEBUG;
79 # XXX parse config file
81 my %options = (debug => 0,
87 name => 'ncbi_${search}_results.$format',
90 ncbi_site => 'http://www.ncbi.nlm.nih.gov',
91 ncbi_search_url => '/entrez/query.fcgi?cmd=search&doptcmdl=Brief&dispmax=1000',
92 ncbi_get_url => '/entrez/query.fcgi?cmd=Text',
95 GetOptions(\%options,'format|f=s','database|b=s','name|n=s',
96 'terms|t=s','dir|D=s','debug|d+','help|h|?','man|m');
98 pod2usage() if $options{help};
99 pod2usage({verbose=>2}) if $options{man};
101 $DEBUG = $options{debug};
103 if (not -d $options{dir}) {
104 die "$options{dir} does not exist or is not a directory";
107 #open search terms file
109 if ($options{terms} eq '-') {
113 $terms = new IO::File $options{terms}, 'r' or die "Unable to open file $options{terms}: $!";
116 my $ua = new LWP::UserAgent(agent=>"DA_get_ncbi_results/$REVISION");
120 # Get uids to retrieve
123 my $format = $options{format};
124 my $uri = URI->new($options{ncbi_site}.$options{ncbi_search_url});
125 $uri->query_form($uri->query_form(),
126 term => $search.' AND '.$options{orgn}.'[Orgn]',
127 db => $options{database},
129 my $url = $uri->as_string;
130 my $mech = WWW::Mechanize->new(agent => "DA_get_ncbi_results/$REVISION");
135 my $response = $mech->content();
136 push @gene_ids , $response =~ m{\[GeneID\:\s+(\d+)\s*\]\s*</td>}mg;
137 last unless $mech->find_link(text => 'Next');
138 $mech->follow_link(text => 'Next');
141 my $file_name = eval qq("$options{name}") or die $@;
142 my $xml_file = new IO::File "$options{dir}/$file_name", 'w' or die "Unable to open $options{dir}/$file_name: $!";
146 print {$xml_file} "<opt>\n";
147 while (@current_ids = splice(@gene_ids,0,5)) {
148 $uri = URI->new($options{ncbi_site}.$options{ncbi_get_url});
149 $uri->query_form($uri->query_form(),
150 dopt => uc($options{format}),
151 db => $options{database},
152 map {('uid',$_)} @current_ids,
154 $url = $uri->as_string;
155 print STDERR "url: $url\n";
157 my $response = $mech->content;
159 while ($response =~ /Error reading from remote server/ and $retry_count > 0) {
161 $response = $mech->content;
164 if ($retry_count <= 0) {
165 die 'Unable to retreive ids ['.join(',',@current_ids).'] because of a remote server error';
167 # For some dumb reason, they send us xml with html
168 # entities. Ditch them.
169 #$response = decode_entities($response);
170 $response =~ s/\>/>/gso;
171 $response =~ s/\</</gso;
172 $response =~ s/"/"/gso;
174 # They also affix a <pre> and suffix a </pre> ditch them.
175 $response =~ s#<\?xml[^>]+>##gso;
176 $response =~ s#<!DOCTYPE[^>]+>##gso;
177 $response =~ s/^\s*<pre>//gso;
178 $response =~ s#</pre>\s*$##gso;
180 print {$xml_file} $response;
183 print {$xml_file} "</opt>\n";