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 => '${search}_results.$format',
89 pubmed_site => 'http://www.ncbi.nlm.nih.gov',
90 pubmed_search_url => '/entrez/query.fcgi?db=gene&cmd=search&term=12q24*+AND+homo[Orgn]&doptcmdl=Brief&dispmax=1000',
91 pubmed_get_url => '/entrez/query.fcgi?db=gene&cmd=Text&dopt=XML',
94 GetOptions(\%options,'format|f=s','database|b=s','name|n=s',
95 'terms|t=s','dir|D=s','debug|d+','help|h|?','man|m');
97 pod2usage() if $options{help};
98 pod2usage({verbose=>2}) if $options{man};
100 $DEBUG = $options{debug};
102 if (not -d $options{dir}) {
103 die "$options{dir} does not exist or is not a directory";
106 #open search terms file
108 if ($options{terms} eq '-') {
112 $terms = new IO::File $options{terms}, 'r' or die "Unable to open file $options{terms}: $!";
115 my $ua = new LWP::UserAgent(agent=>"DA_get_ncbi_results/$REVISION");
119 # Get uids to retrieve
122 my $format = $options{format};
123 my $url = uri_param_munge($options{pubmed_site}.$options{pubmed_search_url},
125 db => $options{database},
128 my $request = HTTP::Request->new('GET', $url);
129 my $response = $ua->request($request);
130 $response = $response->content;
131 my @gene_ids = $response =~ m/\[GeneID\:\s+(\d+)\]/g;
133 my $file_name = eval qq("$options{name}") or die $@;
134 my $xml_file = new IO::File "$options{dir}/$file_name", 'w' or die "Unable to open $options{dir}/$file_name: $!";
138 while (@current_ids = splice(@gene_ids,0,20)) {
139 $url = uri_param_munge($options{pubmed_site}.$options{pubmed_get_url},
140 {dopt => uc($options{format}),
141 db => $options{database},
143 ) .'&' . join('&',map {qq(uid=$_)} @current_ids);
144 $request = HTTP::Request->new('GET', $url);
145 $response = $ua->request($request);
146 $response = $response->content;
147 # For some dumb reason, they send us xml with html
148 # entities. Ditch them.
149 #$response = decode_entities($response);
150 $response =~ s/\>/>/gso;
151 $response =~ s/\</</gso;
152 $response =~ s/"/"/gso;
154 # They also affix a <pre> and suffix a </pre> ditch them.
155 $response =~ s/^\s*<pre>//gso;
156 $response =~ s#</pre>\s*$##gso;
158 $response =~ s#<\?xml[^>]+>##gso;
159 $response =~ s#<!DOCTYPE[^>]+>##gso;
161 print {$xml_file} $response;