]> git.donarmstrong.com Git - function2gene.git/blob - bin/parse_harvester_results
add bin files for search routines
[function2gene.git] / bin / parse_harvester_results
1 #! /usr/bin/perl
2
3 # parse_harvester_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_harvester_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 # 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','dir|D=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 if (not -d $options{dir}) {
103      die "$options{dir} does not exist or is not a directory";
104 }
105
106 my $dir = new IO::Dir $options{dir} or die "Unable to open dir $options{dir}: $!";
107
108 print join(",", map {qq("$_");} qw(Name RefSeq Location Alias Function Description Keyword DBName Filename)),qq(\n);
109
110 my ($keyword) = $options{keyword} || $options{dir} =~ m#(?:^|/)([^\/]+)_results_harvester#;
111
112 while ($_ = $dir->read) {
113      my $file_name = $_;
114      next if $file_name =~ /^\./;
115      next unless -f "$options{dir}/$file_name" and -r "$options{dir}/$file_name";
116
117      my $file = new IO::File "$options{dir}/$file_name", 'r' or die "Unable to open file $file_name";
118
119      local $/;
120      my $result = <$file>;
121
122      my @results;
123
124      # Find gene name
125      ($results[NAME]) = $result =~ m&<img\s+src=\"http://genome-www5.stanford.edu/images/SOURCE/hsgr.gif\"\s*/>
126                                      </td><td\s+bgcolor="tomato"\s+colspan="1"><center><font\s+size="\+5"
127                                      \s+color="white">([^<]+)</font></center>&xis;
128
129      if (not defined $results[NAME]) {
130           ($results[NAME]) = $result =~ m&<TR>\s*<TD\s*BGCOLOR="\#FEFE99"\s*VALIGN="top"\s*NOWRAP>Entry\s*name</TD>\s*
131                                           <TD\s*VALIGN="top"\s*COLSPAN="5">\s*<b>\s*([^<]+?)\s*</b></TD>\s*</TR>&xis;
132      }
133
134      $results[NAME] ||= 'NO NAME';
135
136      # Find REF SEQ number
137      ($results[REFSEQ]) = $result =~ m&<a\s+href="http://www.ncbi.nlm.nih.gov/entrez/
138                                        query.fcgi\?db=Nucleotide\&amp;cmd=Search\&amp;term=([^\&]+)\&amp;doptcmdl=GenBank">&xis;
139
140      $results[REFSEQ] ||= 'NO REFSEQ';
141
142      # Find Chromosomal Location
143      ($results[LOCATION]) = $result =~ m&Chromosomal\s+Location</font></td></tr>\s+<tr><td\s+colspan="1"\s+bgcolor="beige"><dl>
144                                          <dd><b>Chromosome/Cytoband</b></td><td\s+width="525"><center>\s*([^\<]+?)\s*</center>&xis;
145
146      $results[LOCATION] ||= 'NO LOCATION';
147      # Find gene aliases
148      # SOURCE ALIASES
149      my ($alias_table) = $result =~ m|Aliases</font></td></tr>\s*<tr><td\s+bgcolor=\"beige\"\s+colspan=\"2\">
150                                       <font\s+size=\"-1\"\s*/>\s+<ul>(.+?)</ul>|xis;
151      $alias_table ||='';
152
153      my @gene_aliases = $alias_table =~ m&<li>\s*([^\(\<]{0,30}?)\s*(?:\<|\()&gis;
154
155      # UNIPROT ALIASES
156      push @gene_aliases, $result =~ m&<TR>\s*<TD\s+BGCOLOR=\"\#CECFCE\"\s+VALIGN=\"top\"\s+NOWRAP>\s*Synonym\(s\)\s*</TD>\s*
157                                       <TD\s+VALIGN=\"top\"\s+COLSPAN=\"\d\">\s*([^<]+?)\s*</TD>\s*</TR>&xis;
158      push @gene_aliases, $result =~ m&<TD\s+BGCOLOR=\"\#CECFCE\"\s+VALIGN=\"top\"\s*>\s*Description\s*</TD>\s*
159                                       <TD\s+VALIGN=\"top\"\s+COLSPAN=\"\d\">\s*([^<]+?)\s*</TD>\s*</TR>&xis;
160
161      $results[ALIAS] = join('; ', @gene_aliases);
162      $results[ALIAS] ||= 'NO ALIASES';
163
164      # Find gene function(s)
165
166      # Stanford GO functions
167      my ($gene_ontology) = $result =~ m&<table\s+width="100%"\s+cellspacing="0"\s+border="1"><tr>\s*
168                                         <th\s+valign="middle"\s+align="left"\s+bgcolor="\#CCCCCC">Ontology</th>\s*(.+?)</table>&xis;
169
170      my @functions;
171      push @functions, map {s#\s*\"\>\s*# #g; $_;} $gene_ontology =~ m&<a\s+href="http://godatabase.org/cgi-bin/go.cgi\?view=details
172                                                                       \&amp;depth=1\&amp;query=([^\"]+\">[^\<]+)</a>&gxis
173                                                                            if defined $gene_ontology;
174
175      # UNIPROT GO Functions
176      push @functions, map {s#\s*</a>\;?\s*# #g; $_;} m&<TD\s+VALIGN="top"\s+COLSPAN="5">
177                                                     <a\s+href="http://www.ebi.ac.uk/ego/GSearch\?query=[^\&+]\&mode=id">
178                                                     (GO\:\d+</a>\;\s+[^\<]+?)\s*</TD>&xgis;
179
180      $results[FUNCTION] = join('; ', map {(defined $_)?($_):()} @functions);
181      $results[FUNCTION] ||= 'NO FUNCTION';
182
183      # Figure out the keyword used
184      $results[KEYWORD] = $keyword;
185
186      $results[KEYWORD] ||= 'NO KEYWORD';
187
188      # Figure out what the description is
189      ($results[DESCRIPTION]) = map{s#\n# #g; $_;} $result =~ m&<b>Locus\s+Link\s+Summary</b></td><td\s+width="\d+">(.+?)\s*</td>\s*</tr>&is;
190      if (not defined $results[DESCRIPTION]) {
191           ($results[DESCRIPTION]) = map{s#\n# #g; $_;} $result =~ m&<TD\s+BGCOLOR="\#CECFCE"\s+VALIGN="center"\s+COLSPAN="2">
192                                                                     <FONT\s+face="verdana,helvetica,arial,sans-serif"><B>FUNCTION</B></TD>\s*
193                                                                     <TD\s+VALIGN="top"\s+COLSPAN="4">([^\<]+)</TD>\s*</TR>&xis;
194      }
195      $results[DESCRIPTION] ||= '';
196
197      # Database searched
198      $results[DBNAME] = 'harvester';
199      $results[FILENAME] = $file_name;
200
201      print join(',',map {qq("$_")} @results),qq(\n);
202 }
203
204
205
206
207
208
209 __END__