#! /usr/bin/perl # get_genecard_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_genecard_results [options] =head1 SYNOPSIS Options: --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_harvester_results -D ./harvester_results/ -n '${search}_name.html' < 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; use Time::HiRes qw(usleep); # XXX parse config file my %options = (debug => 0, help => 0, man => 0, format => 'xml', database => 'gene', dir => '.', name => '${search}_results_genecard', terms => '-', genecard_site => 'http://www.genecards.org/cgi-bin/', genecard_search_url => 'cardsearch.pl?search_type=kwd&mini=no&speed=fast&matches=999999', ); 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}: $!"; } #For every term while (<$terms>) { # Get uids to retrieve chomp; my $search = $_; my $uri = URI->new($options{genecard_site}.$options{genecard_search_url}); $uri->query_form($uri->query_form(), search => $search, ); my $url = $uri->as_string; my $mech = WWW::Mechanize->new(agent=>"DA_get_harvester_results/$REVISION"); $mech->get($url); my $response = $mech->content(); my @result_urls = $response =~ m##sg; my $dir_name = eval qq("$options{name}") or die $@; if (not -d "$options{dir}/$dir_name") { mkdir("$options{dir}/$dir_name") or die "Unable to make directory $options{dir}/$dir_name $!"; } # Get XML file my @current_urls; while (@current_urls = map{$options{genecard_site}.$_} splice(@result_urls,0,30)) { for my $url (@current_urls) { # sleep for around 2 seconds usleep((0.5+rand)*2*1000000); $mech->get($url); my $cleaned_url = $url; $cleaned_url =~ s{http://}{}g; $cleaned_url =~ s/[^\w]//g; eval { $mech->save_content($options{dir}.'/'.$dir_name.'/'.$cleaned_url); print "retreived $url\n"; }; if ($@) { warn $@; } } #system(q(wget),'-nd','-nH','-w','2','--random-wait','-P',qq($options{dir}/$dir_name),@current_urls) == 0 or warn "$!"; } } __END__