#! /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; use WWW::Mechanize; # XXX parse config file my %options = (debug => 0, help => 0, man => 0, format => 'xml', database => 'gene', dir => '.', name => 'ncbi_${search}_results.$format', terms => '-', orgn => 'homo', pubmed_site => 'http://www.ncbi.nlm.nih.gov', pubmed_search_url => '/entrez/query.fcgi?cmd=search&doptcmdl=Brief&dispmax=1000', pubmed_get_url => '/entrez/query.fcgi?cmd=Text', ); 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 $uri = URI->new($options{pubmed_site}.$options{pubmed_search_url}); $uri->query_form($uri->query_form(), term => $search.' AND '.$options{orgn}.'[Orgn]', db => $options{database}, ); my $url = $uri->as_string; my $mech = WWW::Mechanize->new(agent => "DA_get_ncbi_results/$REVISION"); $mech->get($url); my @gene_ids; { do { my $response = $mech->content(); push @gene_ids , $response =~ m{\[GeneID\:\s+(\d+)\s*\]\s*}mg; last unless $mech->find_link(text => 'Next'); $mech->follow_link(text => 'Next'); } while (1); } 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; print {$xml_file} "\n"; while (@current_ids = splice(@gene_ids,0,5)) { $uri = URI->new($options{pubmed_site}.$options{pubmed_get_url}); $uri->query_form($uri->query_form(), dopt => uc($options{format}), db => $options{database}, map {('uid',$_)} @current_ids, ); $url = $uri->as_string; print STDERR "url: $url\n"; $mech->get($url); my $response = $mech->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#<\?xml[^>]+>##gso; $response =~ s#]+>##gso; $response =~ s/^\s*
//gso;
	  $response =~ s#
\s*$##gso; print {$xml_file} $response; sleep 10; } print {$xml_file} "
\n"; undef $xml_file; } __END__