]> git.donarmstrong.com Git - function2gene.git/blob - bin/get_uniprot_results
skip unreviewed and fragment records
[function2gene.git] / bin / get_uniprot_results
1 #! /usr/bin/perl
2
3 # get_uniprot_results retreives files of search results from uniprot,
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_uniprot_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_uniprot_results -D ./uniprot_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_uniprot',
80                terms    => '-',
81                uniprot_site => 'http://ca.expasy.org',
82                uniprot_search_url  => '/cgi-bin/sprot-search-ful?S=on&T=on',
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{uniprot_site}.$options{uniprot_search_url});
117      $uri->query_form($uri->query_form(),
118                       SEARCH => $search,
119                      );
120      my $url = $uri->as_string;
121      my $mech = WWW::Mechanize->new(agent=>"DA_get_uniprot_results/$REVISION");
122      print "retreived $url\n";
123      $mech->get($url);
124      my @links = $mech->find_all_links(text_regex=>qr/_HUMAN/);
125      for my $gene_link (@links) {
126           my $gene_url = $gene_link->url_abs->as_string;
127           print STDERR "saving $gene_url\n";
128           $mech->get($gene_url);
129           $mech->follow_link(text_regex => qr/View\s*entry\s*in\s*raw\s*text\s*format/);
130           my $cleaned_url = $gene_url;
131           $cleaned_url =~ s{http://}{}g;
132           $cleaned_url =~ s/[^\w]//g;
133           eval {
134                $mech->save_content($options{dir}.'/'.$dir_name.'/'.$cleaned_url);
135           };
136           if ($@) {
137                warn $@;
138           }
139      }
140 }
141
142
143
144
145
146
147 __END__