#! /usr/bin/perl
-# parse_ncbi_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.
+# combine_results, is part of the gene search suite, 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 $
+# Copyright 2006,2007 by Don Armstrong <don@donarmstrong.com>.
use warnings;
=head1 NAME
- parse_ncbi_results [options]
+ combine_results -- combines parsed result files; outputs to stdout.
=head1 SYNOPSIS
+ combine_results parsed_results_1.txt [parsedresultfiles ...]
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 EXAMPLES
- parse_ncbi_results -D ./ncbi_results/ -n '${search}_name.html' < search_parameters
+ combine_results foo_1.txt
Will pretty much do what you want
-use vars qw($DEBUG $REVISION);
+use vars qw($DEBUG);
BEGIN{
- ($REVISION) = q$LastChangedRevision: 1$ =~ /LastChangedRevision:\s+([^\s]+)/;
$DEBUG = 0 unless defined $DEBUG;
}
my %options = (debug => 0,
help => 0,
man => 0,
- dir => '.',
- keyword => undef,
);
GetOptions(\%options,'keyword|k=s','debug|d+','help|h|?','man|m');
# 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 2007 by Don Armstrong <don@donarmstrong.com>.
-# $Id: perl_script 495 2006-08-10 08:02:01Z don $
use warnings;
=head1 NAME
-do_it_all - Call out to each of the search modules to search for each
-of the terms
+ do_it_all - Call out to each of the search modules to search for
+ each of the terms
=head1 SYNOPSIS
use Cwd qw(abs_path);
use IO::File;
use Storable qw(thaw freeze);
+use File::Basename qw(basename);
my %options = (databases => [],
keywords => [],
pod2usage() if $options{help};
pod2usage({verbose=>2}) if $options{man};
+my $base_dir = basename($0);
+
my $ERRORS='';
$ERRORS.="restart-at must be one of get, parse or combine\n" if
if (exists $options{restart_at} and length $options{restart_at}) {
if (lc($options{restart_at}) eq 'get') {
- delete $state{gotten_keywords};
- delete $state{parsed_keywords};
- delete $state{combined_keywords};
+ delete $state{done_keywords}{get};
+ delete $state{done_keywords}{parse};
+ delete $state{done_keywords}{combine};
}
elsif (lc($options{restart_at}) eq 'parse') {
- delete $state{parsed_keywords};
- delete $state{combined_keywords};
+ delete $state{done_keywords}{parse};
+ delete $state{done_keywords}{combine};
}
elsif (lc($options{restart_at}) eq 'combine') {
- delete $state{combined_keywords};
+ delete $state{done_keywords}{combine};
}
}
}
if (not exists $state{done_keywords}{parse}{$database}{$keyword}) {
push @{$actions{parse}{$database}},$keyword;
- delete $state{done_keywords}{combine}{$database}{$keyword} if
+ delete $state{done_keywords}{combine}{$database}{$keyword} if
exists $state{done_keywords}{combine}{$database}{$keyword};
}
if (not exists $state{done_keywords}{combine}{$database}{$keyword}) {
}
}
+if ($actions{combine}) {
+ save_state(\%state);
+ # deal with combining results
+ my @parsed_results = map { my $db = $_;
+ map {
+ "parsed_results_${db}_${_}.txt"
+ } keys %{$state{done_keywords}{parse}{$db}}
+ } keys %{$state{done_keywords}{parse}};
+
+ write_command_to_file('combined_results.txt',
+ "$base_dir/combine_results",
+ @parsed_results,
+ );
+ for my $result (@parsed_results) {
+ s/^parsed_results_//;
+ s/\.txt$//;
+ my ($db,$keyword) = split /_/, $_, 2;
+ $state{done_keywords}{combined}{$db}{$keyword} = 1;
+ }
+ save_state(\%state);
+ ADVISE("Finished; results in $options{results}/combined_results");
+}
+else {
+ ADVISE('Nothing to do. [Perhaps you wanted --restart-at?]');
+}
+
sub handle_action{
my ($state,$database,$queue) = @_;
my $keyword;
my $failed_keywords = ();
while ($keyword = $queue->dequeue) {
# handle the action, baybee
+ if ($state eq 'get') {
+ my $command_fh;
+ open($command_fh,'|-',
+ "get_${database}_results",
+ );
+ print {$command_fh} "$keyword\n";
+ close($command_fh);
+ if ($? != 0) {
+ WARN("get_${database}_results with keyword $keyword failed with error code ".($?>>8));
+ next;
+ }
+ }
+ elsif ($state eq 'parse') {
+ eval {
+ write_command_to_file("parsed_results_${database}_${keyword}.txt",
+ "parse_${database}_results",
+ '--keywords',
+ $keyword,
+ );
+ };
+ if ($@) {
+ WARN("parse_${database}_results failed with $@");
+ next;
+ }
+ }
+ else {
+ die "I don't know how to handle state $state";
+ }
ADVISE("$state results from '$database' for '$keyword'");
push @{$actioned_keywords},$keyword;
}
close $state_fh or die "Unable to close state file: $!";
}
+sub write_command_to_file{
+ my ($file,@command);
+ my $fh = IO::File->new($file,'w') or
+ die "Unable to open $file for writing: $!";
+ my $command_fh;
+ open($command_fh,'-|',
+ @command,
+ ) or die "Unable to execute $command[0] $!";
+ print {$fh} <$command_fh>;
+ close $fh;
+ close $command_fh or die "$command[0] failed with ".($?>>8);
+}
+
sub ADVISE{
print STDOUT map {($_,qq(\n))} @_;
man => 0,
dir => '.',
keyword => undef,
+ keywords => 0,
);
-GetOptions(\%options,'keyword|k=s','debug|d+','help|h|?','man|m');
+GetOptions(\%options,'keyword|k=s','debug|d+','help|h|?','man|m',
+ 'keywords'
+ );
pod2usage() if $options{help};
'Char' => \&tag_content
);
+print STDOUT join(",", map {qq("$_");} qw(Name RefSeq Location Alias Function Description Keyword DBName Filename)),qq(\n);
for (@ARGV) {
$file_name = $_;
- ($keyword) = $options{keyword} || $file_name =~ m#(?:^|/)([^\/]+?)[\s-]+AND[\s\-].+_results.xml$#;
- print STDOUT join(",", map {qq("$_");} qw(Name RefSeq Location Alias Function Description Keyword DBName Filename)),qq(\n);
+ if ($options{keywords}) {
+ $keyword = $_;
+ $file_name = "ncbi_${keyword}_results.xml";
+ }
+ else {
+ ($keyword) = $options{keyword} || $file_name =~ m#(?:^|/)([^\/]+?)[\s-]+AND[\s\-].+_results.xml$#;
+ }
my $file = new IO::File $file_name, 'r' or die "Unable to open file $file_name $!";
$parser->parse($file);