]> git.donarmstrong.com Git - function2gene.git/blob - bin/get_ncbi_xml_results
update search program with options for do_it_all; implement calls to subsideary scripts
[function2gene.git] / bin / get_ncbi_xml_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::ParamMunge;
77 use LWP::UserAgent;
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                pubmed_site => 'http://www.ncbi.nlm.nih.gov',
90                pubmed_search_url  => '/entrez/query.fcgi?db=gene&cmd=search&term=12q24*+AND+homo[Orgn]&doptcmdl=Brief&dispmax=1000',
91                pubmed_get_url     => '/entrez/query.fcgi?db=gene&cmd=Text&dopt=XML',
92               );
93
94 GetOptions(\%options,'format|f=s','database|b=s','name|n=s',
95            'terms|t=s','dir|D=s','debug|d+','help|h|?','man|m');
96
97 pod2usage() if $options{help};
98 pod2usage({verbose=>2}) if $options{man};
99
100 $DEBUG = $options{debug};
101
102 if (not -d $options{dir}) {
103      die "$options{dir} does not exist or is not a directory";
104 }
105
106 #open search terms file
107 my $terms;
108 if ($options{terms} eq '-') {
109      $terms = \*STDIN;
110 }
111 else {
112      $terms = new IO::File $options{terms}, 'r' or die "Unable to open file $options{terms}: $!";
113 }
114
115 my $ua = new LWP::UserAgent(agent=>"DA_get_ncbi_results/$REVISION");
116
117 #For every term
118 while (<$terms>) {
119      # Get uids to retrieve
120      chomp;
121      my $search = $_;
122      my $format = $options{format};
123      my $url = uri_param_munge($options{pubmed_site}.$options{pubmed_search_url},
124                                {term => $search,
125                                 db   => $options{database},
126                                },
127                               );
128      my $request = HTTP::Request->new('GET', $url);
129      my $response = $ua->request($request);
130      $response = $response->content;
131      my @gene_ids = $response =~ m/\[GeneID\:\s+(\d+)\]/g;
132
133      my $file_name = eval qq("$options{name}") or die $@;
134      my $xml_file = new IO::File "$options{dir}/$file_name", 'w' or die "Unable to open $options{dir}/$file_name: $!";
135
136      # Get XML file
137      my @current_ids;
138      while (@current_ids = splice(@gene_ids,0,20)) {
139           $url = uri_param_munge($options{pubmed_site}.$options{pubmed_get_url},
140                                  {dopt => uc($options{format}),
141                                   db   => $options{database},
142                                  },
143                                 ) .'&' . join('&',map {qq(uid=$_)} @current_ids);
144           $request = HTTP::Request->new('GET', $url);
145           $response = $ua->request($request);
146           $response = $response->content;
147           # For some dumb reason, they send us xml with html
148           # entities. Ditch them.
149           #$response = decode_entities($response);
150           $response =~ s/\&gt;/>/gso;
151           $response =~ s/\&lt;/</gso;
152           $response =~ s/&quot;/"/gso;
153
154           # They also affix a <pre> and suffix a </pre> ditch them.
155           $response =~ s/^\s*<pre>//gso;
156           $response =~ s#</pre>\s*$##gso;
157
158           $response =~ s#<\?xml[^>]+>##gso;
159           $response =~ s#<!DOCTYPE[^>]+>##gso;
160
161           print {$xml_file} $response;
162           sleep 10;
163      }
164      undef $xml_file;
165 }
166
167
168
169
170
171
172 __END__