]> git.donarmstrong.com Git - function2gene.git/blob - bin/parse_ncbi_results
update svn ignore
[function2gene.git] / bin / parse_ncbi_results
1 #! /usr/bin/perl
2
3 # parse_ncbi_results parses search results retrieved 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 2005,7 by Don Armstrong <don@donarmstrong.com>.
9
10
11
12 use warnings;
13 use strict;
14
15
16 use Getopt::Long;
17 use Pod::Usage;
18
19 =head1 NAME
20
21   parse_ncbi_results [options]
22
23 =head1 SYNOPSIS
24
25
26  Options:
27   --dir, -D directory to stick results into [default .]
28   --name, -n file naming scheme [default ${search}_results.$format]
29   --terms, -t file of search terms [default -]
30   --debug, -d debugging level [default 0]
31   --help, -h display this help
32   --man, -m display manual
33
34 =head1 OPTIONS
35
36 =over
37
38 =item B<--debug, -d>
39
40 Debug verbosity. (Default 0)
41
42 =item B<--help, -h>
43
44 Display brief useage information.
45
46 =item B<--man, -m>
47
48 Display this manual.
49
50 =back
51
52 =head1 EXAMPLES
53
54   parse_ncbi_results -D ./ncbi_results/ -n '${search}_name.html' < search_parameters
55
56 Will pretty much do what you want
57
58 =cut
59
60
61
62 use vars qw($DEBUG $REVISION);
63
64 BEGIN{
65      ($REVISION) = q$LastChangedRevision: 1$ =~ /LastChangedRevision:\s+([^\s]+)/;
66      $DEBUG = 0 unless defined $DEBUG;
67 }
68
69 use XML::Parser::Expat;
70 use IO::File;
71
72 # XXX parse config file
73
74 my %options = (debug    => 0,
75                help     => 0,
76                man      => 0,
77                dir      => '.',
78                keyword  => undef,
79                keywords => 0,
80               );
81
82 GetOptions(\%options,'keyword|k=s','debug|d+','help|h|?','man|m',
83            'keywords'
84           );
85
86
87 pod2usage() if $options{help};
88 pod2usage({verbose=>2}) if $options{man};
89
90 $DEBUG = $options{debug};
91
92 # CSV columns
93 use constant {NAME        => 0,
94               REFSEQ      => 1,
95               LOCATION    => 2,
96               ALIAS       => 3,
97               FUNCTION    => 4,
98               DESCRIPTION => 5,
99               KEYWORD     => 6,
100               DBNAME      => 7,
101               FILENAME    => 8,
102              };
103
104 my $current_gene = undef;
105 my $keyword = undef;
106 my $file_name = undef;
107 my ($within_GO,$mrna_ref_seq) = 0,0;
108
109 sub tag_start{
110      my ($expat, $element, %attr) = @_;
111
112      local $_ = lc $element;
113      if ($_ eq 'entrezgene') {
114           $current_gene = [];
115           $$current_gene[KEYWORD] = $keyword;
116           $$current_gene[DBNAME] = 'ncbi';
117           $$current_gene[FILENAME] = $file_name;
118      }
119 }
120
121 sub tag_content {
122      my ($expat, $string) = @_;
123
124      return unless defined $current_gene;
125
126      local $_ = lc $expat->current_element;
127
128      if ($_ eq 'gene-ref_locus') {
129           $$current_gene[NAME] = $string;
130      }
131      elsif ($_ eq 'gene-ref_maploc') {
132           $$current_gene[LOCATION] = $string;
133      }
134      elsif ($_ eq 'gene-ref_desc') {
135           push @{$$current_gene[ALIAS]}, $string;
136      }
137      elsif ($_ eq 'prot-ref_name_e' or $_ eq 'gene-ref_syn_e') {
138           push @{$$current_gene[ALIAS]}, $string;
139      }
140      elsif ($_ eq 'entrezgene_summary') {
141           $$current_gene[DESCRIPTION] = $string;
142      }
143      elsif ($_ eq 'gene-commentary_heading') {
144           $within_GO = 0;
145           $mrna_ref_seq = 0;
146           $within_GO = 1 if $string =~ /GeneOntology/;
147           $mrna_ref_seq = 1 if $string =~ /mRNA Sequence/i;
148      }
149      elsif ($_ eq 'other-source_anchor') {
150           return unless $within_GO;
151           push @{$$current_gene[FUNCTION]}, $string;
152      }
153      elsif ($_ eq 'gene-commentary_accession') {
154           return unless $expat->within_element('Gene-commentary_products');
155           $$current_gene[REFSEQ] ||= $string;
156      }
157 }
158
159 sub tag_stop {
160      my ($expat, $element) = @_;
161
162      local $_ = lc $element;
163      if ($_ eq 'entrezgene') {
164           # If current_gene is defined, output the current gene information
165           if (defined $current_gene and @$current_gene) {
166                $$current_gene[NAME] ||= ${$$current_gene[ALIAS]}[1] if defined $$current_gene[ALIAS];
167                for (qw(NAME REFSEQ LOCATION ALIAS
168                        FUNCTION DESCRIPTION KEYWORD DBNAME
169                        FILENAME)) {
170                     $$current_gene[eval "$_"] ||= "NO $_";
171                }
172                print STDOUT join(',', map {$_ = join('; ', @$_) if ref $_; qq("$_");} @$current_gene),qq(\n);
173                undef $current_gene;
174           }
175      }
176 }
177
178 my $parser = new XML::Parser::Expat;
179 $parser->setHandlers('Start' => \&tag_start,
180                      'End'   => \&tag_stop,
181                      'Char'  => \&tag_content
182                     );
183
184 print STDOUT join(",", map {qq("$_");} qw(Name RefSeq Location Alias Function Description Keyword DBName Filename)),qq(\n);
185 for (@ARGV) {
186      $file_name = $_;
187      if ($options{keywords}) {
188           $keyword = $_;
189           $file_name = "ncbi_${keyword}_results.xml";
190      }
191      else {
192           ($keyword) = $options{keyword} || $file_name =~ m#(?:^|/)([^\/]+?)[\s-]+AND[\s\-].+_results.xml$#;
193      }
194      my $file = new IO::File $file_name, 'r' or die "Unable to open file $file_name $!";
195
196      $parser->parse($file);
197
198      undef $file;
199 }
200
201
202
203
204
205
206 __END__