]> git.donarmstrong.com Git - function2gene.git/blob - bin/parse_genecard_results
* Fix genecard and harvester parsers
[function2gene.git] / bin / parse_genecard_results
1 #! /usr/bin/perl
2
3 # parse_genecard_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_genecard_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_harvester_results -D ./harvester_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 IO::File;
71 use IO::Dir;
72
73 use HTML::TreeBuilder;
74 use HTML::ElementTable;
75
76 my %options = (debug    => 0,
77                help     => 0,
78                man      => 0,
79                dir      => '.',
80                keyword  => undef,
81                keywords => 0,
82               );
83
84 GetOptions(\%options,'keyword|k=s','dir|D=s','debug|d+','help|h|?','man|m',
85            'keywords',
86           );
87
88
89 pod2usage() if $options{help};
90 pod2usage({verbose=>2}) if $options{man};
91
92 $DEBUG = $options{debug};
93
94 # CSV columns
95 use constant {NAME        => 0,
96               REFSEQ      => 1,
97               LOCATION    => 2,
98               ALIAS       => 3,
99               FUNCTION    => 4,
100               DESCRIPTION => 5,
101               KEYWORD     => 6,
102               DBNAME      => 7,
103               FILENAME    => 8,
104              };
105
106 if ($options{keywords}) {
107      if (@ARGV != 1) {
108           pod2usage("If the --keywords option is used, exactly one argument (the keyword) must be passed");
109      }
110      $options{dir} = "$ARGV[0]_results_genecard";
111 }
112
113 if (not -d $options{dir}) {
114      die "$options{dir} does not exist or is not a directory";
115 }
116
117 my $dir = new IO::Dir $options{dir} or die "Unable to open dir $options{dir}: $!";
118
119 print join(",", map {qq("$_");} qw(Name RefSeq Location Alias Function Description Keyword DBName Filename)),qq(\n);
120
121 while ($_ = $dir->read) {
122      my $file_name = $_;
123      next if $file_name =~ /^\./;
124      next unless -f "$options{dir}/$file_name" and -r "$options{dir}/$file_name";
125
126      my $file = new IO::File "$options{dir}/$file_name", 'r' or die "Unable to open file $file_name";
127
128      local $/;
129      my $result = <$file>;
130
131      my @results;
132
133      # Find gene name
134      ($results[NAME]) = $result =~ m{(?:Lean|Gene)Card\s+for\s+[^<]+<FONT[^>]+>\s*([^<]+)}xis;
135
136      $results[NAME] ||= 'NO NAME';
137      # Find REF SEQ number
138      ($results[REFSEQ]) = $result =~ m{http://www.ncbi.nlm.nih.gov/entrez/query.fcgi\?
139                                        (?:cmd=Search\&db=nucleotide|db=nucleotide\&cmd=search)
140                                        \&doptcmdl=GenBank\&term=([^\"]+)\"}xis;
141
142      $results[REFSEQ] ||= 'NO REFSEQ';
143
144      # Find Gene Location
145      ($results[LOCATION]) = $result =~ m{cytogenetic\s+band:</I><b>\s+
146                                          <a\s+href="[^\"]+"\s+target\s*=\s*"aaa"[^>]*>\s*([^\<]+?)\s*</a>}xis;
147
148      $results[LOCATION] ||= 'NO LOCATION';
149
150      # Find gene aliases
151      my ($alias_table) = $result =~ m{(<table[^>]+><tr><th[^>]+>Aliases.+?</table>)}is;
152      $alias_table ||= '';
153      my @gene_aliases = map {s/\s*$//; $_;} $alias_table =~ m{<td(?: nowrap)?>\s*([^<]+)<}gis;
154
155      $results[ALIAS] = join('; ', @gene_aliases);
156      $results[ALIAS] ||= 'NO ALIASES';
157
158      # Find gene function(s)
159
160      my @functions;
161      # GO Functions
162      push @functions, (map {s/\n//g; $_;}
163                        map {s#\s*</a>(?:</td><td>\s*)?\s*# #g; $_;}
164                        $result =~ m{(GO:\d+\s*</a>(?:</td><td>\s*)?.+?)(?:</font>|</td>|<dd>|<p>)}gis
165                       );
166      $results[FUNCTION] = join('; ', map {(defined $_)?($_):()} @functions);
167      $results[FUNCTION] ||= 'NO FUNCTION';
168
169      # Figure out the keyword used
170      ($results[KEYWORD]) = $file_name =~ /search=([^&]+)/;
171
172      $results[KEYWORD] ||= 'NO KEYWORD';
173
174      # Swiss prot functions
175      my @description = (map {s/<[^>]+>/ /g;
176                            s/\s+/ /g;
177                            $_;
178                       }
179                       $result =~ m{<(?:dd|li)><b>Function(?::</b>|</b>:)\s+
180                                    (.+?)<(?:/dd|li)>}xgis
181                      );
182
183      # Figure out what the description is
184      $results[DESCRIPTION] = join('; ',
185                                   map {(defined $_)?($_):()}
186                                   @description);
187
188      # Database searched
189      $results[DBNAME] = 'genecard';
190      $results[FILENAME] = $file_name;
191
192      print join(',',map {qq("$_")} @results),qq(\n);
193 }
194
195
196
197
198
199
200 __END__