#!/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_probe_search

=head1 COMMAND LINE PARAMETERS

Required parameters
  --config_file
  --probe_name          

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

=head1 DESCRIPTION

Utility script to delete a probe search.

See delete_probe_search.ini for more configuration details.

Deletes from these tables:

  probe, probe_sequence
  job, job_sequence, job_error
  sequence and sequence_hit 

Does not delete from:

  hit, hit_xref and xref

=head1 EXAMPLE RUN

./delete_probe_search --config_file=delete_probe_search.ini
--probe_name=test

GeneTargeting analysis database --
Connected to host: host, as user: user
Database       : southern_blot_design

Fetched 23 Jobs to be deleted

deleted_job_errors           : 0
deleted_job_sequence_links   : 458
deleted_jobs                 : 23
deleted_probe_sequence_links : 458
deleted_probes               : 1
deleted_sequence_hit_links   : 0
deleted_sequences            : 458

=head1 CONTACT

G2C B<email> webmaster@genes2cognition.org

=cut


use strict;
use warnings;
use Bio::EnsEMBL::DBSQL::DBAdaptor;
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, $probe_name);

    GetOptions(
        "config_file=s"       => \$config_file,
        "probe_name=s"        => \$probe_name,
        "debug"               => \$debug,
        "help|h"              => \&show_perldoc
    ) or show_perldoc();

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

    my ($dna_probe_aptr) = get_GeneTargeting_adaptors($GeneTargeting_dba);

    initialise_counts();    
    my $dna_probe = $dna_probe_aptr->fetch_by_name($probe_name)
        or confess "Could not fetch DNAProbe by name '$probe_name'";
    
    delete_probe_search($GeneTargeting_dba, $dna_probe);

END {
        $counts->display_counts() if $counts;
    }    
}

sub delete_probe_search {
    my ( $dba, $dna_probe ) = @_;

    my ($dna_probe_aptr, $job_aptr, $sequence_aptr, $sequence_hit_aptr)
        = get_GeneTargeting_adaptors($dba);

    my $job_ids = $job_aptr->get_ids_by_DNAProbe_name($dna_probe->name)
        or confess "Fetched no Job ids for DNAProbe " . $dna_probe->name;
    print "Fetched ", scalar(@$job_ids), " Jobs to be deleted\n";
    
    foreach my $job_id (@$job_ids) {
    
        my $job = $job_aptr->fetch_by_db_id($job_id)
            or confess "Could not fetch Job by id: $job_id";
            
        my $seq_ids = $job_aptr->get_Sequence_ids($job)
            or confess "Could not get Sequence ids for Job id: ", $job->id;
        
        #Delete the rows in sequence and 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->deleted_sequence_hit_links($counts->deleted_sequence_hit_links
                + $sequence_hit_aptr->delete_SequenceHit_links($sequence));

            $counts->deleted_sequences($counts->deleted_sequences
                + $sequence_aptr->delete($sequence));
        }
        
        #Delete rows in job_sequence, job_error and job
        $counts->deleted_job_sequence_links($counts->deleted_job_sequence_links
            + $job_aptr->delete_JobSequence_links($job));
            
        $counts->deleted_job_errors($counts->deleted_job_errors
            + $job_aptr->delete_errors($job));
            
        $counts->deleted_jobs($counts->deleted_jobs + $job_aptr->delete($job));    
    }
    
    #Delete from probe_sequence and probe tables
    $counts->deleted_probe_sequence_links($counts->deleted_probe_sequence_links
        + $dna_probe_aptr->delete_ProbeSequence_links($dna_probe));

    $counts->deleted_probes($counts->deleted_probes 
        + $dna_probe_aptr->delete($dna_probe)); 
}

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

    my $dna_probe_aptr = $dba->get_DNAProbeAdaptor()
        or confess "Could not get DNAProbeAdaptor";
    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";
            
    return ($dna_probe_aptr, $job_aptr, $sequence_aptr
        , $sequence_hit_aptr);
}

sub initialise_counts {

    my @counts = qw(
        deleted_sequences
        deleted_jobs
        deleted_sequence_hit_links
        deleted_job_sequence_links
        deleted_job_errors
        deleted_probe_sequence_links
        deleted_probes);
    
    $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
# (see create_probe_search_defaults.ini)

sub configure_parameters {

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

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