+#! /usr/bin/perl
+
+# parse_harvester_results retreives files of search results from ncbi,
+# and is released under the terms of the GPL version 2, or any later
+# version, at your option. See the file README and COPYING for more
+# information.
+
+# Copyright 2004 by Don Armstrong <don@donarmstrong.com>.
+
+# $Id: ss,v 1.1 2004/06/29 05:26:35 don Exp $
+
+
+use warnings;
+use strict;
+
+
+use Getopt::Long;
+use Pod::Usage;
+
+=head1 NAME
+
+ parse_harvester_results [options]
+
+=head1 SYNOPSIS
+
+
+ Options:
+ --dir, -D directory to stick results into [default .]
+ --name, -n file naming scheme [default ${search}_results.$format]
+ --terms, -t file of search terms [default -]
+ --debug, -d debugging level [default 0]
+ --help, -h display this help
+ --man, -m display manual
+
+=head1 OPTIONS
+
+=over
+
+=item B<--debug, -d>
+
+Debug verbosity. (Default 0)
+
+=item B<--help, -h>
+
+Display brief useage information.
+
+=item B<--man, -m>
+
+Display this manual.
+
+=back
+
+=head1 EXAMPLES
+
+ parse_harvester_results -D ./harvester_results/ -n '${search}_name.html' < search_parameters
+
+Will pretty much do what you want
+
+=cut
+
+
+
+use vars qw($DEBUG $REVISION);
+
+BEGIN{
+ ($REVISION) = q$LastChangedRevision: 1$ =~ /LastChangedRevision:\s+([^\s]+)/;
+ $DEBUG = 0 unless defined $DEBUG;
+}
+
+use IO::File;
+use IO::Dir;
+
+# XXX parse config file
+
+my %options = (debug => 0,
+ help => 0,
+ man => 0,
+ dir => '.',
+ keyword => undef,
+ );
+
+GetOptions(\%options,'keyword|k=s','dir|D=s','debug|d+','help|h|?','man|m');
+
+
+pod2usage() if $options{help};
+pod2usage({verbose=>2}) if $options{man};
+
+$DEBUG = $options{debug};
+
+# CSV columns
+use constant {NAME => 0,
+ REFSEQ => 1,
+ LOCATION => 2,
+ ALIAS => 3,
+ FUNCTION => 4,
+ DESCRIPTION => 5,
+ KEYWORD => 6,
+ DBNAME => 7,
+ FILENAME => 8,
+ };
+
+if (not -d $options{dir}) {
+ die "$options{dir} does not exist or is not a directory";
+}
+
+my $dir = new IO::Dir $options{dir} or die "Unable to open dir $options{dir}: $!";
+
+print join(",", map {qq("$_");} qw(Name RefSeq Location Alias Function Description Keyword DBName Filename)),qq(\n);
+
+my ($keyword) = $options{keyword} || $options{dir} =~ m#(?:^|/)([^\/]+)_results_harvester#;
+
+while ($_ = $dir->read) {
+ my $file_name = $_;
+ next if $file_name =~ /^\./;
+ next unless -f "$options{dir}/$file_name" and -r "$options{dir}/$file_name";
+
+ my $file = new IO::File "$options{dir}/$file_name", 'r' or die "Unable to open file $file_name";
+
+ local $/;
+ my $result = <$file>;
+
+ my @results;
+
+ # Find gene name
+ ($results[NAME]) = $result =~ m&<img\s+src=\"http://genome-www5.stanford.edu/images/SOURCE/hsgr.gif\"\s*/>
+ </td><td\s+bgcolor="tomato"\s+colspan="1"><center><font\s+size="\+5"
+ \s+color="white">([^<]+)</font></center>&xis;
+
+ if (not defined $results[NAME]) {
+ ($results[NAME]) = $result =~ m&<TR>\s*<TD\s*BGCOLOR="\#FEFE99"\s*VALIGN="top"\s*NOWRAP>Entry\s*name</TD>\s*
+ <TD\s*VALIGN="top"\s*COLSPAN="5">\s*<b>\s*([^<]+?)\s*</b></TD>\s*</TR>&xis;
+ }
+
+ $results[NAME] ||= 'NO NAME';
+
+ # Find REF SEQ number
+ ($results[REFSEQ]) = $result =~ m&<a\s+href="http://www.ncbi.nlm.nih.gov/entrez/
+ query.fcgi\?db=Nucleotide\&cmd=Search\&term=([^\&]+)\&doptcmdl=GenBank">&xis;
+
+ $results[REFSEQ] ||= 'NO REFSEQ';
+
+ # Find Chromosomal Location
+ ($results[LOCATION]) = $result =~ m&Chromosomal\s+Location</font></td></tr>\s+<tr><td\s+colspan="1"\s+bgcolor="beige"><dl>
+ <dd><b>Chromosome/Cytoband</b></td><td\s+width="525"><center>\s*([^\<]+?)\s*</center>&xis;
+
+ $results[LOCATION] ||= 'NO LOCATION';
+ # Find gene aliases
+ # SOURCE ALIASES
+ my ($alias_table) = $result =~ m|Aliases</font></td></tr>\s*<tr><td\s+bgcolor=\"beige\"\s+colspan=\"2\">
+ <font\s+size=\"-1\"\s*/>\s+<ul>(.+?)</ul>|xis;
+ $alias_table ||='';
+
+ my @gene_aliases = $alias_table =~ m&<li>\s*([^\(\<]{0,30}?)\s*(?:\<|\()&gis;
+
+ # UNIPROT ALIASES
+ push @gene_aliases, $result =~ m&<TR>\s*<TD\s+BGCOLOR=\"\#CECFCE\"\s+VALIGN=\"top\"\s+NOWRAP>\s*Synonym\(s\)\s*</TD>\s*
+ <TD\s+VALIGN=\"top\"\s+COLSPAN=\"\d\">\s*([^<]+?)\s*</TD>\s*</TR>&xis;
+ push @gene_aliases, $result =~ m&<TD\s+BGCOLOR=\"\#CECFCE\"\s+VALIGN=\"top\"\s*>\s*Description\s*</TD>\s*
+ <TD\s+VALIGN=\"top\"\s+COLSPAN=\"\d\">\s*([^<]+?)\s*</TD>\s*</TR>&xis;
+
+ $results[ALIAS] = join('; ', @gene_aliases);
+ $results[ALIAS] ||= 'NO ALIASES';
+
+ # Find gene function(s)
+
+ # Stanford GO functions
+ my ($gene_ontology) = $result =~ m&<table\s+width="100%"\s+cellspacing="0"\s+border="1"><tr>\s*
+ <th\s+valign="middle"\s+align="left"\s+bgcolor="\#CCCCCC">Ontology</th>\s*(.+?)</table>&xis;
+
+ my @functions;
+ push @functions, map {s#\s*\"\>\s*# #g; $_;} $gene_ontology =~ m&<a\s+href="http://godatabase.org/cgi-bin/go.cgi\?view=details
+ \&depth=1\&query=([^\"]+\">[^\<]+)</a>&gxis
+ if defined $gene_ontology;
+
+ # UNIPROT GO Functions
+ push @functions, map {s#\s*</a>\;?\s*# #g; $_;} m&<TD\s+VALIGN="top"\s+COLSPAN="5">
+ <a\s+href="http://www.ebi.ac.uk/ego/GSearch\?query=[^\&+]\&mode=id">
+ (GO\:\d+</a>\;\s+[^\<]+?)\s*</TD>&xgis;
+
+ $results[FUNCTION] = join('; ', map {(defined $_)?($_):()} @functions);
+ $results[FUNCTION] ||= 'NO FUNCTION';
+
+ # Figure out the keyword used
+ $results[KEYWORD] = $keyword;
+
+ $results[KEYWORD] ||= 'NO KEYWORD';
+
+ # Figure out what the description is
+ ($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;
+ if (not defined $results[DESCRIPTION]) {
+ ($results[DESCRIPTION]) = map{s#\n# #g; $_;} $result =~ m&<TD\s+BGCOLOR="\#CECFCE"\s+VALIGN="center"\s+COLSPAN="2">
+ <FONT\s+face="verdana,helvetica,arial,sans-serif"><B>FUNCTION</B></TD>\s*
+ <TD\s+VALIGN="top"\s+COLSPAN="4">([^\<]+)</TD>\s*</TR>&xis;
+ }
+ $results[DESCRIPTION] ||= '';
+
+ # Database searched
+ $results[DBNAME] = 'harvester';
+ $results[FILENAME] = $file_name;
+
+ print join(',',map {qq("$_")} @results),qq(\n);
+}
+
+
+
+
+
+
+__END__