]> git.donarmstrong.com Git - function2gene.git/blob - bin/parse_ncbi_results
1e2b8d4f4dd72989d809d4e7a546b6eb1b501d66
[function2gene.git] / bin / parse_ncbi_results
1 #! /usr/bin/perl
2
3 # parse_ncbi_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   parse_ncbi_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   parse_ncbi_results -D ./ncbi_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 XML::Parser::Expat;
71 use IO::File;
72
73 # XXX parse config file
74
75 my %options = (debug    => 0,
76                help     => 0,
77                man      => 0,
78                dir      => '.',
79                keyword  => undef,
80                keywords => 0,
81               );
82
83 GetOptions(\%options,'keyword|k=s','debug|d+','help|h|?','man|m',
84            'keywords'
85           );
86
87
88 pod2usage() if $options{help};
89 pod2usage({verbose=>2}) if $options{man};
90
91 $DEBUG = $options{debug};
92
93 # CSV columns
94 use constant {NAME        => 0,
95               REFSEQ      => 1,
96               LOCATION    => 2,
97               ALIAS       => 3,
98               FUNCTION    => 4,
99               DESCRIPTION => 5,
100               KEYWORD     => 6,
101               DBNAME      => 7,
102               FILENAME    => 8,
103              };
104
105 my $current_gene = undef;
106 my $keyword = undef;
107 my $file_name = undef;
108 my ($within_GO,$mrna_ref_seq) = 0,0;
109
110 sub tag_start{
111      my ($expat, $element, %attr) = @_;
112
113      local $_ = lc $element;
114      if ($_ eq 'entrezgene') {
115           $current_gene = [];
116           $$current_gene[KEYWORD] = $keyword;
117           $$current_gene[DBNAME] = 'ncbi';
118           $$current_gene[FILENAME] = $file_name;
119      }
120 }
121
122 sub tag_content {
123      my ($expat, $string) = @_;
124
125      return unless defined $current_gene;
126
127      local $_ = lc $expat->current_element;
128
129      if ($_ eq 'gene-ref_locus') {
130           $$current_gene[NAME] = $string;
131      }
132      elsif ($_ eq 'gene-ref_maploc') {
133           $$current_gene[LOCATION] = $string;
134      }
135      elsif ($_ eq 'gene-ref_desc') {
136           push @{$$current_gene[ALIAS]}, $string;
137      }
138      elsif ($_ eq 'prot-ref_name_e' or $_ eq 'gene-ref_syn_e') {
139           push @{$$current_gene[ALIAS]}, $string;
140      }
141      elsif ($_ eq 'entrezgene_summary') {
142           $$current_gene[DESCRIPTION] = $string;
143      }
144      elsif ($_ eq 'gene-commentary_heading') {
145           $within_GO = 0;
146           $mrna_ref_seq = 0;
147           $within_GO = 1 if $string =~ /GeneOntology/;
148           $mrna_ref_seq = 1 if $string =~ /mRNA Sequence/i;
149      }
150      elsif ($_ eq 'other-source_anchor') {
151           return unless $within_GO;
152           push @{$$current_gene[FUNCTION]}, $string;
153      }
154      elsif ($_ eq 'gene-commentary_accession') {
155           return unless $expat->within_element('Gene-commentary_products');
156           $$current_gene[REFSEQ] ||= $string;
157      }
158 }
159
160 sub tag_stop {
161      my ($expat, $element) = @_;
162
163      local $_ = lc $element;
164      if ($_ eq 'entrezgene') {
165           # If current_gene is defined, output the current gene information
166           if (defined $current_gene and @$current_gene) {
167                $$current_gene[NAME] ||= ${$$current_gene[ALIAS]}[1] if defined $$current_gene[ALIAS];
168                for (qw(NAME REFSEQ LOCATION ALIAS
169                        FUNCTION DESCRIPTION KEYWORD DBNAME
170                        FILENAME)) {
171                     $$current_gene[eval "$_"] ||= "NO $_";
172                }
173                print STDOUT join(',', map {$_ = join('; ', @$_) if ref $_; qq("$_");} @$current_gene),qq(\n);
174                undef $current_gene;
175           }
176      }
177 }
178
179 my $parser = new XML::Parser::Expat;
180 $parser->setHandlers('Start' => \&tag_start,
181                      'End'   => \&tag_stop,
182                      'Char'  => \&tag_content
183                     );
184
185 print STDOUT join(",", map {qq("$_");} qw(Name RefSeq Location Alias Function Description Keyword DBName Filename)),qq(\n);
186 for (@ARGV) {
187      $file_name = $_;
188      if ($options{keywords}) {
189           $keyword = $_;
190           $file_name = "ncbi_${keyword}_results.xml";
191      }
192      else {
193           ($keyword) = $options{keyword} || $file_name =~ m#(?:^|/)([^\/]+?)[\s-]+AND[\s\-].+_results.xml$#;
194      }
195      my $file = new IO::File $file_name, 'r' or die "Unable to open file $file_name $!";
196
197      $parser->parse($file);
198
199      undef $file;
200 }
201
202
203
204
205
206
207 __END__