]> git.donarmstrong.com Git - function2gene.git/blob - bin/get_harvester_results
add bin files for search routines
[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 warnings;
14 use strict;
15
16
17 use Getopt::Long;
18 use Pod::Usage;
19
20 =head1 NAME
21
22   get_harvester_results [options]
23
24 =head1 SYNOPSIS
25
26
27  Options:
28   --dir, -D directory to stick results into [default .]
29   --name, -n file naming scheme [default ${search}_results.$format]
30   --terms, -t file of search terms [default -]
31   --debug, -d debugging level [default 0]
32   --help, -h display this help
33   --man, -m display manual
34
35 =head1 OPTIONS
36
37 =over
38
39 =item B<--debug, -d>
40
41 Debug verbosity. (Default 0)
42
43 =item B<--help, -h>
44
45 Display brief useage information.
46
47 =item B<--man, -m>
48
49 Display this manual.
50
51 =back
52
53 =head1 EXAMPLES
54
55   get_harvester_results -D ./harvester_results/ -n '${search}_name.html' < search_parameters
56
57 Will pretty much do what you want
58
59 =cut
60
61
62
63 use vars qw($DEBUG $REVISION);
64
65 BEGIN{
66      ($REVISION) = q$LastChangedRevision: 1$ =~ /LastChangedRevision:\s+([^\s]+)/;
67      $DEBUG = 0 unless defined $DEBUG;
68 }
69
70 use IO::File;
71 use URI::ParamMunge;
72 use LWP::UserAgent;
73
74 # XXX parse config file
75
76 my %options = (debug    => 0,
77                help     => 0,
78                man      => 0,
79                format   => 'xml',
80                database => 'gene',
81                dir      => '.',
82                name     => '${search}_results_harvester',
83                terms    => '-',
84                harvester_site => 'http://www-db.embl.de',
85                harvester_search_url  => '/jss/servlet/de.embl.bk.htmlfind.HarvesterPageSearchOutput?search=GOLGI&chipsetID=-1&maxHits=10000&submit=search',
86               );
87
88 GetOptions(\%options,'format|f=s','database|b=s','name|n=s',
89            'terms|t=s','dir|D=s','debug|d+','help|h|?','man|m');
90
91 pod2usage() if $options{help};
92 pod2usage({verbose=>2}) if $options{man};
93
94 $DEBUG = $options{debug};
95
96 if (not -d $options{dir}) {
97      die "$options{dir} does not exist or is not a directory";
98 }
99
100 #open search terms file
101 my $terms;
102 if ($options{terms} eq '-') {
103      $terms = \*STDIN;
104 }
105 else {
106      $terms = new IO::File $options{terms}, 'r' or die "Unable to open file $options{terms}: $!";
107 }
108
109 my $ua = new LWP::UserAgent(agent=>"DA_get_harvester_results/$REVISION");
110
111 #For every term
112 while (<$terms>) {
113      # Get uids to retrieve
114      chomp;
115      my $search = $_;
116      my $url = uri_param_munge($options{harvester_site}.$options{harvester_search_url},
117                                {search => $search,
118                                },
119                               );
120      my $request = HTTP::Request->new('GET', $url);
121      my $response = $ua->request($request);
122      $response = $response->content;
123      my @result_urls = $response =~ m#<a\s+href=(http://harvester.embl.de/harvester/[^\/]+/[^.]+.htm)\s*>#g;
124
125      my $dir_name = eval qq("$options{name}") or die $@;
126      mkdir("$options{dir}/$dir_name") or die "Unable to make directory $options{dir}/$dir_name $!";
127      # Get XML file
128      my @current_urls;
129      while (@current_urls = splice(@result_urls,0,30)) {
130           system(q(wget),'-nd','-nH','-w','2','--random-wait','-P',qq($options{dir}/$dir_name),@current_urls) == 0 or warn "$!";
131      }
132 }
133
134
135
136
137
138
139 __END__