#! /usr/bin/perl # get_ncbi_results retreives files of search results from ncbi, and is # released under the terms of the GPL version 2, or any later version, # at your option. See the file README and COPYING for more # information. # Copyright 2004 by Don Armstrong . # $Id: ss,v 1.1 2004/06/29 05:26:35 don Exp $ use warnings; use strict; use Getopt::Long; use Pod::Usage; =head1 NAME get_ncbi_results [options] get_ncbi_results - Retrieve search results from NCBI using parameters passed on stdin. =head1 SYNOPSIS Options: --format, -f format of search results to return [default xml] --database, -b database to search for results [default gene] --dir, -D directory to stick results into [default .] --name, -n file naming scheme [default ${search}_results.$format] --terms, -t file of search terms [default -] --debug, -d debugging level [default 0] --help, -h display this help --man, -m display manual =head1 OPTIONS =over =item B<--debug, -d> Debug verbosity. (Default 0) =item B<--help, -h> Display brief useage information. =item B<--man, -m> Display this manual. =back =head1 EXAMPLES get_ncbi_results -f xml -b gene -D ./results/ -n '${search}_name.xml' < search_parameters Will pretty much do what you want =cut use vars qw($DEBUG $REVISION); BEGIN{ ($REVISION) = q$LastChangedRevision: 1$ =~ /LastChangedRevision:\s+([^\s]+)/; $DEBUG = 0 unless defined $DEBUG; } use IO::File; use URI::ParamMunge; use LWP::UserAgent; # XXX parse config file my %options = (debug => 0, help => 0, man => 0, format => 'xml', database => 'gene', dir => '.', name => 'ncbi_${search}_results.$format', terms => '-', pubmed_site => 'http://www.ncbi.nlm.nih.gov', pubmed_search_url => '/entrez/query.fcgi?db=gene&cmd=search&term=12q24*+AND+homo[Orgn]&doptcmdl=Brief&dispmax=1000', pubmed_get_url => '/entrez/query.fcgi?db=gene&cmd=Text&dopt=XML', ); GetOptions(\%options,'format|f=s','database|b=s','name|n=s', 'terms|t=s','dir|D=s','debug|d+','help|h|?','man|m'); pod2usage() if $options{help}; pod2usage({verbose=>2}) if $options{man}; $DEBUG = $options{debug}; if (not -d $options{dir}) { die "$options{dir} does not exist or is not a directory"; } #open search terms file my $terms; if ($options{terms} eq '-') { $terms = \*STDIN; } else { $terms = new IO::File $options{terms}, 'r' or die "Unable to open file $options{terms}: $!"; } my $ua = new LWP::UserAgent(agent=>"DA_get_ncbi_results/$REVISION"); #For every term while (<$terms>) { # Get uids to retrieve chomp; my $search = $_; my $format = $options{format}; my $url = uri_param_munge($options{pubmed_site}.$options{pubmed_search_url}, {term => $search, db => $options{database}, }, ); my $request = HTTP::Request->new('GET', $url); my $response = $ua->request($request); $response = $response->content; my @gene_ids = $response =~ m/\[GeneID\:\s+(\d+)\]/g; my $file_name = eval qq("$options{name}") or die $@; my $xml_file = new IO::File "$options{dir}/$file_name", 'w' or die "Unable to open $options{dir}/$file_name: $!"; # Get XML file my @current_ids; while (@current_ids = splice(@gene_ids,0,20)) { $url = uri_param_munge($options{pubmed_site}.$options{pubmed_get_url}, {dopt => uc($options{format}), db => $options{database}, }, ) .'&' . join('&',map {qq(uid=$_)} @current_ids); $request = HTTP::Request->new('GET', $url); $response = $ua->request($request); $response = $response->content; # For some dumb reason, they send us xml with html # entities. Ditch them. #$response = decode_entities($response); $response =~ s/\>/>/gso; $response =~ s/\</ and suffix a ditch them. $response =~ s/^\s*
//gso;
	  $response =~ s#
\s*$##gso; $response =~ s#<\?xml[^>]+>##gso; $response =~ s#]+>##gso; print {$xml_file} $response; sleep 10; } undef $xml_file; } __END__