]> git.donarmstrong.com Git - function2gene.git/blob - bin/get_ncbi_results
3e1a6259ce1d2e717727ad7a9ed4a1d1a3bef118
[function2gene.git] / bin / get_ncbi_results
1 #! /usr/bin/perl
2
3 # get_ncbi_results retreives files of search results from ncbi, and is
4 # released under the terms of the GPL version 2, or any later version,
5 # 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_ncbi_results [options]
23
24 get_ncbi_results - Retrieve search results from NCBI using parameters
25 passed on stdin.
26
27 =head1 SYNOPSIS
28
29
30  Options:
31   --format, -f format of search results to return [default xml]
32   --database, -b database to search for results [default gene]
33   --dir, -D directory to stick results into [default .]
34   --name, -n file naming scheme [default ${search}_results.$format]
35   --terms, -t file of search terms [default -]
36   --debug, -d debugging level [default 0]
37   --help, -h display this help
38   --man, -m display manual
39
40 =head1 OPTIONS
41
42 =over
43
44 =item B<--debug, -d>
45
46 Debug verbosity. (Default 0)
47
48 =item B<--help, -h>
49
50 Display brief useage information.
51
52 =item B<--man, -m>
53
54 Display this manual.
55
56 =back
57
58 =head1 EXAMPLES
59
60   get_ncbi_results -f xml -b gene -D ./results/ -n '${search}_name.xml' < search_parameters
61
62 Will pretty much do what you want
63
64 =cut
65
66
67
68 use vars qw($DEBUG $REVISION);
69
70 BEGIN{
71      ($REVISION) = q$LastChangedRevision: 1$ =~ /LastChangedRevision:\s+([^\s]+)/;
72      $DEBUG = 0 unless defined $DEBUG;
73 }
74
75 use IO::File;
76 use URI;
77 use WWW::Mechanize;
78
79 # XXX parse config file
80
81 my %options = (debug    => 0,
82                help     => 0,
83                man      => 0,
84                format   => 'xml',
85                database => 'gene',
86                dir      => '.',
87                name     => 'ncbi_${search}_results.$format',
88                terms    => '-',
89                orgn     => 'homo',
90                pubmed_site => 'http://www.ncbi.nlm.nih.gov',
91                pubmed_search_url  => '/entrez/query.fcgi?cmd=search&doptcmdl=Brief&dispmax=1000',
92                pubmed_get_url     => '/entrez/query.fcgi?cmd=Text',
93               );
94
95 GetOptions(\%options,'format|f=s','database|b=s','name|n=s',
96            'terms|t=s','dir|D=s','debug|d+','help|h|?','man|m');
97
98 pod2usage() if $options{help};
99 pod2usage({verbose=>2}) if $options{man};
100
101 $DEBUG = $options{debug};
102
103 if (not -d $options{dir}) {
104      die "$options{dir} does not exist or is not a directory";
105 }
106
107 #open search terms file
108 my $terms;
109 if ($options{terms} eq '-') {
110      $terms = \*STDIN;
111 }
112 else {
113      $terms = new IO::File $options{terms}, 'r' or die "Unable to open file $options{terms}: $!";
114 }
115
116 my $ua = new LWP::UserAgent(agent=>"DA_get_ncbi_results/$REVISION");
117
118 #For every term
119 while (<$terms>) {
120      # Get uids to retrieve
121      chomp;
122      my $search = $_;
123      my $format = $options{format};
124      my $uri = URI->new($options{pubmed_site}.$options{pubmed_search_url});
125      $uri->query_form($uri->query_form(),
126                       term => $search.' AND '.$options{orgn}.'[Orgn]',
127                       db   => $options{database},
128                      );
129      my $url = $uri->as_string;
130      my $mech = WWW::Mechanize->new(agent => "DA_get_ncbi_results/$REVISION");
131      $mech->get($url);
132      my @gene_ids;
133      {
134           do {
135                my $response = $mech->content();
136                push @gene_ids , $response =~ m{\[GeneID\:\s+(\d+)\s*\]\s*</td>}mg;
137                last unless $mech->find_link(text => 'Next');
138                $mech->follow_link(text => 'Next');
139           } while (1);
140      }
141      my $file_name = eval qq("$options{name}") or die $@;
142      my $xml_file = new IO::File "$options{dir}/$file_name", 'w' or die "Unable to open $options{dir}/$file_name: $!";
143
144      # Get XML file
145      my @current_ids;
146      print {$xml_file} "<opt>\n";
147      while (@current_ids = splice(@gene_ids,0,5)) {
148           $uri = URI->new($options{pubmed_site}.$options{pubmed_get_url});
149           $uri->query_form($uri->query_form(),
150                            dopt => uc($options{format}),
151                            db   => $options{database},
152                            map {('uid',$_)} @current_ids,
153                           );
154           $url = $uri->as_string;
155           print STDERR "url: $url\n";
156           $mech->get($url);
157           my $response = $mech->content;
158           # For some dumb reason, they send us xml with html
159           # entities. Ditch them.
160           #$response = decode_entities($response);
161           $response =~ s/\&gt;/>/gso;
162           $response =~ s/\&lt;/</gso;
163           $response =~ s/&quot;/"/gso;
164
165           # They also affix a <pre> and suffix a </pre> ditch them.
166           $response =~ s#<\?xml[^>]+>##gso;
167           $response =~ s#<!DOCTYPE[^>]+>##gso;
168           $response =~ s/^\s*<pre>//gso;
169           $response =~ s#</pre>\s*$##gso;
170
171           print {$xml_file} $response;
172           sleep 10;
173      }
174      print {$xml_file} "</opt>\n";
175      undef $xml_file;
176 }
177
178
179
180
181
182
183 __END__