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