#!/usr/bin/perl -w

# usage : perl variant-effect_predictor.pl
# Sarah Maman - 04/10/2012 - Maria Bernard 14/08/2015
# version : 1.1.0 : adding the possibility to use a non Ensembl referenced genome
# 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;
use Data::Dumper;
use Config::IniFiles;

my $config = {} ;

GetOptions(
  $config,
  'specie|s=s',
  'is_in_ensembl',
  'fasta|r=s',
  'annot|a|gtf|gff|g=s',
  'variant|v=s',
  'format|f=s',
  'vep_out=s',
  'summary_out=s',
  'dirname|d=s'
) or die "ERROR: Failed to parse command line options\n";

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

print "## Variant Effect Predictor Galaxy Wrapper options : \n";
print Dumper $config;

my $command;

my $cfg = Config::IniFiles->new( -file => "/path/to/PATH.ini" );


if($config->{is_in_ensembl}){
    my $VARIANT = $cfg->val( 'toolsPath', 'VARIANT_PATH' );
	my $registry = $cfg->val( 'toolsPath', 'registry_PATH' );

	if ($config->{specie} eq "human"){
		$command = $VARIANT." --database --species ".$config->{specie}." -i ".$config->{variant}." -o ".$config->{dirname}."/".$config->{specie}.".vep --stats_file ".$config->{dirname}."/".$config->{specie}."_summary.html --format ".$config->{format}." --registry ".$registry." --check_existing --sift b --polyphen b  --verbose 2>&1";
		#Ligne de commande fournie aux biologistes dans le stdout
		print STDOUT "## Annotation fonctionnelle des variants:\n\nEspece : ".$config->{specie}." \n Option supplementaire : polyphen b \n La ligne de commande utilisee est : \n\n \t ".$command." \n\n";
		system $command;
		
	}else{
		$command = $VARIANT." --database --species ".$config->{specie}." -i ".$config->{variant}." -o ".$config->{dirname}."/".$config->{specie}.".vep --stats_file ".$config->{dirname}."/".$config->{specie}."_summary.html --format ".$config->{format}." --registry ".$registry." --check_existing --sift b --verbose 2>&1";
		#Ligne de commande fournie aux biologistes dans le stdout
		print STDOUT "## Annotation fonctionnelle des variants:\n\nEspece : ".$config->{specie}." \nLa ligne de commande utilisee est : \n\n \t ".$command." \n\n";
		system $command;
	}
	
	$command = "mv ".$config->{dirname}."/".$config->{specie}.".vep ".$config->{vep_out} ;
	system $command;
	$command = "mv ".$config->{dirname}."/".$config->{specie}."_summary.html ".$config->{summary_out} ;
	system $command;
	
}else{
	
	my $GTF2VEP = $cfg->val( 'toolsPath', 'GTF2VEP_PATH' );
	my $VARIANT = $cfg->val( 'toolsPath', 'VARIANT_PATH' );
	
	$command = " ln -s ".$config->{fasta}." ".$config->{dirname}."/." ;
	$config->{fasta}=$config->{dirname}."/". basename $config->{fasta};
	system $command;

	$command = $GTF2VEP." -i ".$config->{annot}." -f ".$config->{fasta}." -s ".$config->{specie}." --dir ".$config->{dirname}."/GTF2VEP_out/";
	print STDOUT "## Generation de la base d'annotation personnelle:\n\nLa ligne de commande utilisee est :\n\n  ";
	print STDOUT "\t".$command."\n\n";
	system $command;
	
	my $GTF2VEP_dir = $config->{dirname}."/GTF2VEP_out/*";
	print $GTF2VEP_dir."\n";
	my @files = glob( $GTF2VEP_dir );
	foreach my $f1 (@files){
		print "\t".$f1."\n";
		my @files2 = glob($f1."/*" );
		foreach my $f2 (@files2) {
			print "\t\t".$f2."\n";
			my @files3 = glob($f2."/*" );
			foreach my $f3 (@files3) {
				print "\t\t\t".$f3."\n";
			}
		}
	}
	
	$command = $VARIANT." --database --species ".$config->{specie}." -i ".$config->{variant}." --format ".$config->{format}." -o ".$config->{dirname}."/".$config->{specie}.".vep --stats_file ".$config->{dirname}."/".$config->{specie}."_summary.html --offline --dir_cache ".$config->{dirname}."/GTF2VEP_out --verbose 2>&1";
	print STDOUT "\n\n## Annotation fonctionnelle des variants:\n\nEspece : $config->{specie} \nLa ligne de commande utilisee est : \n\n \t $command \n\n";
	system $command;
	
	$command = "mv ".$config->{dirname}."/".$config->{specie}.".vep ".$config->{vep_out} ;
	system $command;
	$command = "mv ".$config->{dirname}."/".$config->{specie}."_summary.html ".$config->{summary_out} ;
	system $command;
	
	print STDOUT "\n\n## Nettoyage : suppression de la banque personnelle : ".$config->{dirname}."/GTF2VEP_out\n";
	$command = "rm -r ".$config->{dirname}."/GTF2VEP_out";
	system $command;
}
