#!/usr/local/bin/perl
#
# Copyright 2005-2009 Genes to Cognition Programme (G2C) and 
# Genome Research Limited (GRL)
#
# Contact: webmaster@genes2cognition.org
# See    : www.genes2cognition.org/software/southern_blot
#
# You may distribute this file/module under the terms of the Perl artistic
# licence: http://www.perlfoundation.org/artistic_license_2_0

=pod

=head1 NAME - delete_job_results

=head1 COMMAND LINE PARAMETERS

Required parameters
  --config_file         Otherwise the default is used (see below)
  --job_id         

Optional parameters
  --debug               Switch on debugging
  --help|h              Output documentation

=head1 CONFIGURATION FILE

See delete_job_results.ini for more configuration details.

=head1 DESCRIPTION

Utility script to delete (the potentially incomplete) results stored
from a failed job.

See delete_job_results.ini for more configuration details.

Deletes from these tables:

  sequence_hit
  job_error
  
Resets each job state to 'created', ready for re-submission
by submit_probe_search 

=head1 EXAMPLE RUN

./delete_job_results --config_file=delete_job_results.ini --job_id=1
GeneTargeting analysis database --
Connected to host: host, as user: user
Database       : southern_blot_design

Fetched job id=1,  state='success'
  - fetched 20 sequence ids
  - job state reset to 'created'


deleted_job_errors         : 0
deleted_sequence_hit_links : 33
fetched_sequences          : 20
jobs_processed             : 1

=head1 CONTACT

G2C B<email> webmaster@genes2cognition.org

=cut


use strict;
use warnings;
use Carp;
use GeneTargetingDB;
use GeneTargeting::Utils qw(show_perldoc print_hash_ref);
use GeneTargeting::Utils::Config;
use GeneTargeting::Utils::Counts;
use Getopt::Long;

#Globals
my ($debug, $counts);

{
    my ($config_file, @job_ids);

    GetOptions(
        "config_file=s"       => \$config_file,
        "job_id=i"            => \@job_ids,
        "debug"               => \$debug,
        "help|h"              => \&show_perldoc
    ) or show_perldoc();

    show_perldoc("Must set --job_id(s)\n") unless @job_ids;
    
    #Make the database connections
    my $cfg = GeneTargeting::Utils::Config->new($config_file, $debug);
    print "GeneTargeting analysis database --\n";
    my ($dba) = $cfg->make_analysis_DBAdaptor_from_config(1);

    initialise_counts();    
    foreach my $job_id (@job_ids) {
        delete_job_results($dba, $job_id);
    }
    

END {
        $dba->DESTROY if $dba;
        my $stderr = \*STDERR;
        $counts->display_counts($stderr) if $counts;
    }    
}

sub delete_job_results {
    my ( $dba, $job_id ) = @_;

    my ($job_aptr, $sequence_aptr, $sequence_hit_aptr, $conf_aptr)
        = get_GeneTargeting_adaptors($dba);
    
    my $job = $job_aptr->fetch_by_db_id($job_id)
        or confess "Could not fetch Job by id: $job_id";
    print "Fetched job id=", "$job_id,  state='", $job->state, "'\n";

    my $conf = $conf_aptr->fetch_by_Job_id($job->id)
        or confess "Could not fetch Conf by Job id: $job_id";
    unless ($conf->isa('GeneTargeting::DBEntry::Conf::Exonerate')) {
        confess "Do not know how to delete jobs of class" . ref($conf);
    }

    my $seq_ids = $job_aptr->get_Sequence_ids($job)
        or confess "Could not get Sequence ids for Job id: ", $job->id;
    print "  - fetched ", scalar(@$seq_ids), " sequence ids\n";

    #Delete the rows in sequence_hit
    foreach my $seq_id (@$seq_ids) {

        my $sequence = $sequence_aptr->fetch_by_db_id($seq_id)
            or confess "Could not fetch Sequence by id '$seq_id'";        
        $counts->increment_fetched_sequences;

        $counts->deleted_sequence_hit_links($counts->deleted_sequence_hit_links
            + $sequence_hit_aptr->delete_SequenceHit_links($sequence));
    }
    
    #Delete the rows in job_error
    $counts->deleted_job_errors($counts->deleted_job_errors
        + $job_aptr->delete_errors($job));
    
    #Reset the status of the job
    $job->state('created');
    $job_aptr->update($job);
    print "  - job state reset to 'created'\n\n";
    $counts->increment_jobs_processed;
}

sub get_GeneTargeting_adaptors {
    my ( $dba ) = @_;

    my $job_aptr  = $dba->get_JobAdaptor()
        or confess "Could not get JobAdaptor";
    my $sequence_aptr = $dba->get_SequenceAdaptor()
        or confess "Could not get SequenceAdaptor";
    my $sequence_hit_aptr = $dba->get_SequenceHitAdaptor()
        or confess "Could not get SequenceHitAdaptor";
    my $conf_aptr = $dba->get_ConfAdaptor()
        or confess "Could not get ConfAdaptor";
            
    return ($job_aptr, $sequence_aptr, $sequence_hit_aptr
        , $conf_aptr);
}

sub initialise_counts {

    my @counts = qw(
        jobs_processed
        deleted_job_errors
        deleted_sequence_hit_links
        fetched_sequences);
    
    $counts = GeneTargeting::Utils::Counts->new(@counts);
}

###  configure_parameters
#
# Specifies the mandatory and optional parameters that can (or must)
# be specified in the configuration .ini file for the programme

sub configure_parameters {

    my $mandatory = {};
    my $optional  = {};

    $mandatory->{'analysis_database'} = ['db', 'host', 'user'];
     $optional->{'analysis_database'} = ['pass', 'port'];
    
    return ($mandatory, $optional);
}
