]> git.donarmstrong.com Git - function2gene.git/blobdiff - bin/do_it_all
update search program with options for do_it_all; implement calls to subsideary scripts
[function2gene.git] / bin / do_it_all
index 3d3cfa3effb42640cbe917f9ebb41da3fbcad279..788319a03226fab077cda0ca67543ac5702340ee 100755 (executable)
@@ -3,7 +3,6 @@
 # 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;
@@ -16,8 +15,8 @@ use Storable;
 
 =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
 
@@ -82,6 +81,7 @@ use vars qw($DEBUG);
 use Cwd qw(abs_path);
 use IO::File;
 use Storable qw(thaw freeze);
+use File::Basename qw(basename);
 
 my %options = (databases       => [],
               keywords        => [],
@@ -98,6 +98,8 @@ GetOptions(\%options,'keywords=s@','databases=s@',
 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
@@ -180,16 +182,16 @@ if (@{$options{keywords}}) {
 
 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};
      }
 }
 
@@ -217,7 +219,7 @@ for my $keyword (@{$state{keywords}}) {
          }
          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}) {
@@ -255,6 +257,32 @@ for my $state (qw(get parse)) {
      }
 }
 
+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;
@@ -262,6 +290,34 @@ sub handle_action{
      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;
      }
@@ -276,6 +332,19 @@ sub save_state{
      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))} @_;