]> git.donarmstrong.com Git - function2gene.git/blob - bin/get_harvester_results
* Finish addition of ensembl support
[function2gene.git] / bin / get_harvester_results
1 #! /usr/bin/perl
2
3 # get_harvester_results retreives files of search results from ncbi,
4 # and is released under the terms of the GPL version 2, or any later
5 # version, at your option. See the file README and COPYING for more
6 # information.
7
8 # Copyright 2004 by Don Armstrong <don@donarmstrong.com>.
9
10 # $Id: ss,v 1.1 2004/06/29 05:26:35 don Exp $
11
12
13 use threads;
14 use warnings;
15 use strict;
16
17
18 use Getopt::Long;
19 use Pod::Usage;
20
21 =head1 NAME
22
23   get_harvester_results [options]
24
25 =head1 SYNOPSIS
26
27
28  Options:
29   --dir, -D directory to stick results into [default .]
30   --name, -n file naming scheme [default ${search}_results.$format]
31   --terms, -t file of search terms [default -]
32   --debug, -d debugging level [default 0]
33   --help, -h display this help
34   --man, -m display manual
35
36 =head1 OPTIONS
37
38 =over
39
40 =item B<--debug, -d>
41
42 Debug verbosity. (Default 0)
43
44 =item B<--help, -h>
45
46 Display brief useage information.
47
48 =item B<--man, -m>
49
50 Display this manual.
51
52 =back
53
54 =head1 EXAMPLES
55
56   get_harvester_results -D ./harvester_results/ -n '${search}_name.html' < search_parameters
57
58 Will pretty much do what you want
59
60 =cut
61
62
63
64 use vars qw($DEBUG $REVISION);
65
66 BEGIN{
67      ($REVISION) = q$LastChangedRevision: 1$ =~ /LastChangedRevision:\s+([^\s]+)/;
68      $DEBUG = 0 unless defined $DEBUG;
69 }
70
71 use IO::File;
72 use URI;
73 use WWW::Mechanize;
74 use Thread::Queue;
75 use Time::HiRes qw(usleep);
76
77 # XXX parse config file
78
79 my %options = (debug    => 0,
80                help     => 0,
81                man      => 0,
82                format   => 'xml',
83                database => 'gene',
84                dir      => '.',
85                name     => '${search}_results_harvester',
86                terms    => '-',
87                orgn     => 'human',
88                harvester_site => 'http://harvester.fzk.de',
89
90               );
91 GetOptions(\%options,'format|f=s','database|b=s','name|n=s',
92            'terms|t=s','dir|D=s','debug|d+','help|h|?','man|m');
93
94 pod2usage() if $options{help};
95 pod2usage({verbose=>2}) if $options{man};
96
97 $DEBUG = $options{debug};
98
99 if (not -d $options{dir}) {
100      die "$options{dir} does not exist or is not a directory";
101 }
102
103
104 $options{harvester_search_url}  = '/cgi-bin/'.$options{orgn}.'/search.cgi?zoom_query=golgi&zoom_per_page=100&zoom_and=1&zoom_sort=0';
105
106 #open search terms file
107 my $terms;
108 if ($options{terms} eq '-') {
109      $terms = \*STDIN;
110 }
111 else {
112      $terms = new IO::File $options{terms}, 'r' or die "Unable to open file $options{terms}: $!";
113 }
114
115 #For every term
116 my @threads;
117 while (<$terms>) {
118      # Get uids to retrieve
119      chomp;
120      my $search = $_;
121      my $uri = URI->new($options{harvester_site}.$options{harvester_search_url});
122      $uri->query_form(zoom_query =>[],
123                      );
124      $uri->query_form(zoom_query => $search,
125                      );
126      my $url = $uri->as_string;
127      my $queue = Thread::Queue->new();
128      my $dir_name = eval qq("$options{name}") or die $@;
129      if (not -d "$options{dir}/$dir_name") {
130         mkdir("$options{dir}/$dir_name") or die "Unable to make directory $options{dir}/$dir_name $!";
131      }
132      my $wget_thread = threads->new(\&get_url,"$options{dir}/$dir_name",$queue);
133      push @threads,$wget_thread;
134
135      my $mech = WWW::Mechanize->new(agent => "DA_get_harvester_results/$REVISION");
136
137      #HTTP::Request->new('GET', $url);
138      $mech->get($url);
139      my $next_link;
140      do {
141           my @links = $mech->links;
142           $next_link = undef;
143           for my $link (@links) {
144                if ($link->text() =~ /Next /) {
145                     $next_link = $link;
146                }
147                elsif ($link->url =~ m#http://harvester.fzk.de/harvester/human/[^\/]+/[^.]+.htm#) {
148                     $queue->enqueue($link->url());
149                }
150           }
151           $mech->follow_link(url=>$next_link->url) if defined $next_link;
152      } while ($next_link);
153      $queue->enqueue(undef);
154 }
155 for my $thread (@threads) {
156      $thread->join;
157 }
158
159 sub get_url{
160      my ($dir,$queue) = @_;
161
162
163      my @current_urls;
164      while (my $url = $queue->dequeue) {
165           push @current_urls,$url;
166           if (@current_urls >= 30) {
167                wget_urls($dir,@current_urls);
168                @current_urls = ();
169           }
170      }
171      wget_urls($dir,@current_urls) if @current_urls;
172 }
173 sub wget_urls{
174      my ($dir,@urls) = @_;
175      return unless @urls;
176      # replacing wget with WWW::Mechanize
177      my $mech = WWW::Mechanize->new(agent => "DA_get_harvester_results/$REVISION");
178      for my $url (@urls) {
179           # sleep for around 2 seconds
180           usleep((0.5+rand)*2*1000000);
181           $mech->get($url);
182           my $cleaned_url = $url;
183           $cleaned_url =~ s{http://}{}g;
184           $cleaned_url =~ s/[^\w]//g;
185           eval {
186                $mech->save_content($dir.'/'.$cleaned_url);
187                print "retreived $url\n";
188           };
189           if ($@) {
190                warn $@;
191           }
192      }
193      #system(q(wget),'-nd','-nH','-w','2','--random-wait','-P',$dir,@urls) == 0 or warn "$!";
194 }
195
196 __END__