]> git.donarmstrong.com Git - function2gene.git/blob - bin/get_genecard_results
add todo
[function2gene.git] / bin / get_genecard_results
1 #! /usr/bin/perl
2
3 # get_genecard_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_genecard_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;
72 use WWW::Mechanize;
73 use Time::HiRes qw(usleep);
74
75 # XXX parse config file
76
77 my %options = (debug    => 0,
78                help     => 0,
79                man      => 0,
80                format   => 'xml',
81                database => 'gene',
82                dir      => '.',
83                name     => '${search}_results_genecard',
84                terms    => '-',
85                genecard_site => 'http://www.genecards.org/cgi-bin/',
86                genecard_search_url  => 'cardsearch.pl?search_type=kwd&mini=no&speed=fast&matches=999999',
87               );
88
89 GetOptions(\%options,'format|f=s','database|b=s','name|n=s',
90            'terms|t=s','dir|D=s','debug|d+','help|h|?','man|m');
91
92 pod2usage() if $options{help};
93 pod2usage({verbose=>2}) if $options{man};
94
95 $DEBUG = $options{debug};
96
97 if (not -d $options{dir}) {
98      die "$options{dir} does not exist or is not a directory";
99 }
100
101 #open search terms file
102 my $terms;
103 if ($options{terms} eq '-') {
104      $terms = \*STDIN;
105 }
106 else {
107      $terms = new IO::File $options{terms}, 'r' or die "Unable to open file $options{terms}: $!";
108 }
109
110 #For every term
111 while (<$terms>) {
112      # Get uids to retrieve
113      chomp;
114      my $search = $_;
115      my $uri = URI->new($options{genecard_site}.$options{genecard_search_url});
116      $uri->query_form($uri->query_form(),
117                       search => $search,
118                      );
119      my $url = $uri->as_string;
120      my $mech = WWW::Mechanize->new(agent=>"DA_get_harvester_results/$REVISION");
121      $mech->get($url);
122      my $response = $mech->content();
123      my @result_urls = $response =~ m#<a\s+target\=\'card\'\s+href=\"(carddisp\.pl\?[^\"]+)\"\s*>#sg;
124      my $dir_name = eval qq("$options{name}") or die $@;
125      if (not -d "$options{dir}/$dir_name") {
126           mkdir("$options{dir}/$dir_name") or die "Unable to make directory $options{dir}/$dir_name $!";
127      }
128      # Get XML file
129      my @current_urls;
130      while (@current_urls = map{$options{genecard_site}.$_} splice(@result_urls,0,30)) {
131           for my $url (@current_urls) {
132                # sleep for around 2 seconds
133                usleep((0.5+rand)*2*1000000);
134                $mech->get($url);
135                my $cleaned_url = $url;
136                $cleaned_url =~ s{http://}{}g;
137                $cleaned_url =~ s/[^\w]//g;
138                eval {
139                     $mech->save_content($options{dir}.'/'.$dir_name.'/'.$cleaned_url);
140                     print "retreived $url\n";
141                };
142                if ($@) {
143                     warn $@;
144                }
145           }
146           #system(q(wget),'-nd','-nH','-w','2','--random-wait','-P',qq($options{dir}/$dir_name),@current_urls) == 0 or warn "$!";
147      }
148 }
149
150
151
152
153
154
155 __END__