]> git.donarmstrong.com Git - function2gene.git/blob - bin/get_harvester_results
7446e088c245d9d58adcafef59dd04f769edb6a8
[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
76 # XXX parse config file
77
78 my %options = (debug    => 0,
79                help     => 0,
80                man      => 0,
81                format   => 'xml',
82                database => 'gene',
83                dir      => '.',
84                name     => '${search}_results_harvester',
85                terms    => '-',
86                orgn     => 'human',
87                harvester_site => 'http://harvester.fzk.de',
88
89               );
90 GetOptions(\%options,'format|f=s','database|b=s','name|n=s',
91            'terms|t=s','dir|D=s','debug|d+','help|h|?','man|m');
92
93 pod2usage() if $options{help};
94 pod2usage({verbose=>2}) if $options{man};
95
96 $DEBUG = $options{debug};
97
98 if (not -d $options{dir}) {
99      die "$options{dir} does not exist or is not a directory";
100 }
101
102
103 $options{harvester_search_url}  = '/cgi-bin/'.$options{orgn}.'/search.cgi?zoom_query=golgi&zoom_per_page=100&zoom_and=1&zoom_sort=0';
104
105 #open search terms file
106 my $terms;
107 if ($options{terms} eq '-') {
108      $terms = \*STDIN;
109 }
110 else {
111      $terms = new IO::File $options{terms}, 'r' or die "Unable to open file $options{terms}: $!";
112 }
113
114 #For every term
115 my @threads;
116 while (<$terms>) {
117      # Get uids to retrieve
118      chomp;
119      my $search = $_;
120      my $uri = URI->new($options{harvester_site}.$options{harvester_search_url});
121      $uri->query_form(zoom_query =>[],
122                      );
123      $uri->query_form(zoom_query => $search,
124                      );
125      my $url = $uri->as_string;
126      my $queue = Thread::Queue->new();
127      my $dir_name = eval qq("$options{name}") or die $@;
128      if (not -d "$options{dir}/$dir_name") {
129         mkdir("$options{dir}/$dir_name") or die "Unable to make directory $options{dir}/$dir_name $!";
130      }
131      my $wget_thread = threads->new(\&get_url,"$options{dir}/$dir_name",$queue);
132      push @threads,$wget_thread;
133
134      my $mech = WWW::Mechanize->new(agent => "DA_get_harvester_results/$REVISION");
135
136      #HTTP::Request->new('GET', $url);
137      $mech->get($url);
138      my $next_link;
139      do {
140           my @links = $mech->links;
141           $next_link = undef;
142           for my $link (@links) {
143                if ($link->text() =~ /Next /) {
144                     $next_link = $link;
145                }
146                elsif ($link->url =~ m#http://harvester.fzk.de/harvester/human/[^\/]+/[^.]+.htm#) {
147                     $queue->enqueue($link->url());
148                }
149           }
150           $mech->follow_link(url=>$next_link->url) if defined $next_link;
151      } while ($next_link);
152      $queue->enqueue(undef);
153 }
154 for my $thread (@threads) {
155      $thread->join;
156 }
157
158 sub get_url{
159      my ($dir,$queue) = @_;
160
161
162      my @current_urls;
163      while (my $url = $queue->dequeue) {
164           push @current_urls,$url;
165           if (@current_urls >= 30) {
166                wget_urls($dir,@current_urls);
167                @current_urls = ();
168           }
169      }
170      wget_urls($dir,@current_urls) if @current_urls;
171 }
172 sub wget_urls{
173      my ($dir,@urls) = @_;
174      return unless @urls;
175      system(q(wget),'-nd','-nH','-w','2','--random-wait','-P',$dir,@urls) == 0 or warn "$!";
176 }
177
178 __END__