#!/usr/bin/perl -w

# usage : perl extract_conservation_results.pl
# Sarah Maman - 12/11/2012 - Maria Bernard 19/08/2015
# version : 1.1.0 : removing the use of output directory
# Copyright (C) 2012 INRA
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

use strict;
use File::Basename; 
use Getopt::Long;

my $config = {} ;

GetOptions(
  $config,
  'vep|v=s',
  'specie|s=s',
  'conservation_out=s',
  'dirname|d=s'
) or die "ERROR: Failed to parse command line options\n";

if (! defined $config->{dirname} ){
	$config->{dirname}= dirname $config->{conservation_out} ;
}


#pour connaître le path du tool
use Config::IniFiles;
my $cfg = Config::IniFiles->new( -file => "/path/to/PATH.ini" );
my $registry = $cfg->val( 'toolsPath', 'registry_PATH' );
my $SCRIPT_CONSERVATION_MAISON= $cfg->val( 'toolsPath', 'SCRIPT_CONSERVATION_MAISON' );
my $CONSERVATION = $SCRIPT_CONSERVATION_MAISON."/extract_conservation_results.pl" ;

my $cmd = '';

$cmd = $CONSERVATION." --infile ".$config->{vep}." --outpath ".$config->{dirname}." --registry ".$registry." --species ".$config->{specie}." --compara_user compara 2>&1";
print STDOUT "La ligne de commande utilisee est : \n\n $cmd \n\n";
print STDOUT "\n\n## conservation log\n";
system $cmd;

if (! -e $config->{dirname}."/conservation_title.txt" || ! -e $config->{dirname}."/conservation_results.res"){
	print STDERR "Pas de fichier conservation_title ou conservation_results produit \n";
}else {
	$cmd = "sed 's/\t/\tconservation /g' ".$config->{dirname}."/conservation_title.txt | cat - ".$config->{dirname}."/conservation_results.res > ".$config->{conservation_out};
system $cmd;
}

