package Bio::EnsEMBL::Funcgen::Hive::QcChanceJobFactory;

use warnings;
use strict;

use base qw( Bio::EnsEMBL::Funcgen::Hive::BaseDB );

sub run {
  my $self = shift;
  
  my $db = $self->param_required('out_db');
  
  my $result_set_adaptor = $db->get_ResultSetAdaptor;
  $result_set_adaptor->{file_type} = 'BAM';
  
  my $result_set;  
  my $set_type = $self->param_required('set_type');
  if ($set_type eq 'ResultSet') {
    $result_set = $self->fetch_Set_input('ResultSet'); 
  } else {
    my $feature_set = $self->fetch_Set_input('FeatureSet');
    my $analysis    = $feature_set->analysis;
    $result_set     = $self->ResultSet; 
  }

  my $input_id = $self->create_input_id($result_set);

  $self->dataflow_output_id($input_id, 2);
  return;
}

sub create_input_id {

  my $self       = shift;
  my $result_set = shift;
  
  my $control_result_set = $result_set->get_ControlResultSet;
  
  my $db_output_dir = $self->db_output_dir;
  
  my $signal_bam_file  = $db_output_dir . '/' . $result_set->dbfile_path;
  my $control_bam_file = $db_output_dir . '/' . $control_result_set->dbfile_path;
  
  if (! -e $signal_bam_file) {
    die("$signal_bam_file doesn't exist!");
  }
  if (! -e $control_bam_file) {
    die("$control_bam_file doesn't exist!");
  }
  
  my $out_db   = $self->param('out_db');
  my $work_dir = $self->chance_output_dir;
  
  my $epigenome_production_name = $result_set->epigenome->production_name;
  my $epigenome_gender          = $result_set->epigenome->gender;
  
  # epigenome_gender is used as a substitution parameter in another ehive 
  # parameter. If it is undefined, it will lead to an error.
  #
  if (! defined $epigenome_gender) {
    $epigenome_gender = '';
  }
  
  my $chance_bin_file = 'chance_bin_file_for_'.$epigenome_production_name.'_'.$epigenome_gender.'.bed';

  my $result_set_id = $result_set->dbID;
  my $temp_dir = "$work_dir/$epigenome_production_name/$result_set_id";
  
  use File::Basename;
  (my $signal_bam_file_base_name,  my $signal_bam_directory)  = fileparse($signal_bam_file);
  (my $control_bam_file_base_name, my $control_bam_directory) = fileparse($control_bam_file);
  
  if ($signal_bam_directory ne $control_bam_directory) {
    warn(
      "Directories are not identical for signal_bam_file ($signal_bam_file) and control_bam_file ($control_bam_file)!"
    );
  }
  
  my $input_id = {

      # This is for JobFactoryArgenrich to generate the jobs for indexing the
      # pairs of signal and control bam files.
      #
      column_names => [ 'kind', 'file', 'sourcedir', 'tempdir' ],
      inputlist    => [
	[ 'signal',  $signal_bam_file_base_name,   $signal_bam_directory,  "#tempdir#" ],
	[ 'control', $control_bam_file_base_name,  $control_bam_directory, "#tempdir#" ],
      ],

      # Necessary to find the correct chromosome file. In human the set of 
      # chromosomes is different depending on the gender of the epigenome.
      #
      epigenome_gender => $epigenome_gender,

      # Directory into which the bam files will be copied or symlinked.
      tempdir               => $temp_dir,

      # Name of the output file that argenrich creates. This will be in 
      # #tempdir#
      argenrich_outfile     => 'argenrich_outfile.txt',
      
      # The name of the bin file required by CHANCE. Generated by the 
      # CreateChanceBins analysis.
      #
      chance_bin_file       => $chance_bin_file,

      # result_set_id of the signal to which this will be linked
      signal_result_set_id  => $result_set_id,

      # Connection details for the db to which the results will be written
      tracking_db_user   => $out_db->dbc->user,
      tracking_db_pass   => $out_db->dbc->password,
      tracking_db_host   => $out_db->dbc->host,
      tracking_db_name   => $out_db->dbc->dbname,
      tracking_db_port   => $out_db->dbc->port,
  };
  return $input_id;
}

1;


