]> git.donarmstrong.com Git - function2gene.git/blob - bin/get_ensembl_results
update function2gene to allow uniprot
[function2gene.git] / bin / get_ensembl_results
1 #! /usr/bin/perl
2
3 # get_ensembl_results retreives files of search results from ensembl,
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 2008 by Don Armstrong <don@donarmstrong.com>.
9
10
11 use warnings;
12 use strict;
13
14
15 use Getopt::Long;
16 use Pod::Usage;
17
18 =head1 NAME
19
20   get_ensembl_results [options]
21
22 =head1 SYNOPSIS
23
24
25  Options:
26   --dir, -D directory to stick results into [default .]
27   --name, -n file naming scheme [default ${search}_results.$format]
28   --terms, -t file of search terms [default -]
29   --debug, -d debugging level [default 0]
30   --help, -h display this help
31   --man, -m display manual
32
33 =head1 OPTIONS
34
35 =over
36
37 =item B<--debug, -d>
38
39 Debug verbosity. (Default 0)
40
41 =item B<--help, -h>
42
43 Display brief useage information.
44
45 =item B<--man, -m>
46
47 Display this manual.
48
49 =back
50
51 =head1 EXAMPLES
52
53   get_ensembl_results -D ./ensembl_results/ -n '${search}_name.html' < search_parameters
54
55 Will pretty much do what you want
56
57 =cut
58
59
60
61 use vars qw($DEBUG $REVISION);
62
63 BEGIN{
64      ($REVISION) = q$LastChangedRevision: 1$ =~ /LastChangedRevision:\s+([^\s]+)/;
65      $DEBUG = 0 unless defined $DEBUG;
66 }
67
68 use IO::File;
69 use URI;
70 use WWW::Mechanize;
71 use Time::HiRes qw(usleep);
72
73 # XXX parse config file
74
75 my %options = (debug    => 0,
76                help     => 0,
77                man      => 0,
78                dir      => '.',
79                name     => '${search}_results_ensembl',
80                terms    => '-',
81                ensembl_site => 'http://www.ensembl.org',
82                ensembl_search_url  => '/Homo_sapiens/searchview?species=Homo_sapiens&idx=&',
83               );
84
85 GetOptions(\%options,'name|n=s',
86            'terms|t=s','dir|D=s','debug|d+','help|h|?','man|m');
87
88 pod2usage() if $options{help};
89 pod2usage({verbose=>2}) if $options{man};
90
91 $DEBUG = $options{debug};
92
93 if (not -d $options{dir}) {
94      die "$options{dir} does not exist or is not a directory";
95 }
96
97 #open search terms file
98 my $terms;
99 if ($options{terms} eq '-') {
100      $terms = \*STDIN;
101 }
102 else {
103      $terms = new IO::File $options{terms}, 'r' or die "Unable to open file $options{terms}: $!";
104 }
105
106 #For every term
107 while (<$terms>) {
108      # Get uids to retrieve
109      chomp;
110      s/\r$//g;
111      my $search = $_;
112      my $dir_name = eval qq("$options{name}") or die $@;
113      if (not -d "$options{dir}/$dir_name") {
114           mkdir("$options{dir}/$dir_name") or die "Unable to make directory $options{dir}/$dir_name $!";
115      }
116      my $uri = URI->new($options{ensembl_site}.$options{ensembl_search_url});
117      $uri->query_form($uri->query_form(),
118                       q => $search,
119                      );
120      my $url = $uri->as_string;
121      my $mech = WWW::Mechanize->new(agent=>"DA_get_ensembl_results/$REVISION");
122      #print STDERR $url,qq(\n);
123      $mech->get($url);
124      #print STDERR $mech->content();
125      my @links = $mech->find_link(text_regex=>qr/Gene/);
126      $mech->follow_link(text_regex=>qr/Gene/);
127      #print STDERR $mech->content();
128      # now we need to walk through all of the pages
129      push @links,$mech->find_all_links(url_regex => qr/_s=\d{2,}$/);
130      for my $link (@links) {
131           my $link_url = $link->url_abs->as_string;
132           print STDERR "getting $link_url\n";
133           $mech->get($link_url);
134           my @gene_links = $mech->find_all_links(url_regex => qr/geneview/);
135           for my $gene_link (@gene_links) {
136                my $gene_url = $gene_link->url_abs->as_string;
137                print STDERR "saving $gene_url\n";
138                $mech->get($gene_url);
139                my $cleaned_url = $gene_url;
140                $cleaned_url =~ s{http://}{}g;
141                $cleaned_url =~ s/[^\w]//g;
142                eval {
143                     $mech->save_content($options{dir}.'/'.$dir_name.'/'.$cleaned_url);
144                     print "retreived $url\n";
145                };
146                if ($@) {
147                     warn $@;
148                }
149           }
150      }
151 }
152
153
154
155
156
157
158 __END__