#!/usr/bin/perl -w

# usage : perl formatSNPeffect-protseq.pl
# Sarah Maman - 09/10/2012 - Sabrina Rodriguez 17/09/15
# 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 URL::Encode qw(url_encode url_decode); 
use Data::Dumper;
use Config::IniFiles;

# SETTING GLOBAL VARIABLES ################################################################################
my $cfg = Config::IniFiles->new( -file => "/usr/local/bioinfo/src/galaxy-dev/galaxy-dist/PATH.ini" );
my $file_path = $cfg->val( 'workPath', 'FILEPATH_DEV' );
my $registry = $cfg->val( 'toolsPath', 'registry_PATH' );
my $SCRIPT_CONSERVATION_MAISON = $cfg->val( 'toolsPath', 'SCRIPT_CONSERVATION_MAISON' );

#pour connaître le path du script perl
my $PROTSEQ = $SCRIPT_CONSERVATION_MAISON."/formatSNPeffect-protseq.pl" ;
###########################################################################################################

my $config = {} ;
Getopt::Long::Configure( 'no_ignorecase', 'bundling' );
GetOptions(
  $config,
  'vep_in|v=s',
  'specie|s=s',
  'delta|t=s',
  'vep_out=s',
  'fasta_out=s',
  'fasta_encoded_out=s',
  'encoded_name_out=s',
  #~ 'dirname|d=s',
  'genome_fasta_file=s',
  'annot_file=s'
) or die "ERROR: Failed to parse command line options\n";


print STDOUT Dumper $config;

##################################################################################
# UNIQ WORKING DIR
##################################################################################
my ($nb) = ($config->{vep_out}=~/dataset_(\d+)\.\S+$/);
my $dirresults= $file_path.$nb."/";
`cd $file_path; mkdir $nb/; chmod 777 $nb/; cd $nb/;`;
##################################################################################

my $vep_in  = $dirresults."input.vep";
my ($genome_fasta_file , $annot_file );

my $vep_out = $dirresults."output.vep";
my $fasta_out = $dirresults."protseq_sequences.fasta";
my $fasta_encoded_out = $dirresults."protseq_sequences_encoded.fasta";
my $encoded_name_out = $dirresults."encoded_names.txt";

my $h=$config->{genome_fasta_file};
my $e=$config->{annot_file};


# COPY INPUT FILES TO WORKING DIR
# reference genome in ENsembl
if($config->{specie} ne "no"){
	
	`cp "$config->{vep_in}" $vep_in; `;
	$genome_fasta_file = "no"; # reference genome not in Ensembl
	$annot_file = "no"; # reference genome not in Ensembl
	
	print "FILEIN: $vep_in \n";
}
# reference genome not in Ensembl
else{
	
	`cp "$config->{vep_in}" $vep_in; cp "$config->{genome_fasta_file}" $genome_fasta_file; cp "$config->{annot_file}" $annot_file; `;
	$genome_fasta_file = $dirresults."genome.fasta"; # reference genome not in Ensembl
	$annot_file = $dirresults."annot.gff"; # reference genome not in Ensembl
}

print STDOUT "$dirresults\n";

if(exists($ENV{PERL5LIB})&&$ENV{PERL5LIB} ne ''){
    $ENV{PERL5LIB}="/usr/local/bioinfo/src/ensembl-api/current/ensembl/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-variation/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-compara/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-functgenomics/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-variation/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-functgenomics/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-compara/modules:/tools/perl5/modules/lib/perl5/x86_64-linux-thread-multi:/tools/perl5/modules/lib/perl5:/usr/local/bioinfo/src/ensembl-api/current/ensembl/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-variation/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-functgenomics/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-compara/modules:/usr/local/bioinfo/src/ensembl-api/current/ensemblgenomes-api/modules:/tools/perl5/modules/lib/perl5/x86_64-linux-thread-multi:/tools/perl5/modules/lib/perl5:/usr/local/bioinfo/src/Eval/latest:/usr/local/bioinfo/src/tRNAscan-SE/latest/bin:/usr/local/bioinfo/src/CEGMA/current/lib:$ENV{PERL5LIB}";
}
else
{
    $ENV{PERL5LIB}='/usr/local/bioinfo/src/ensembl-api/current/ensembl/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-variation/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-compara/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-functgenomics/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-variation/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-functgenomics/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-compara/modules:/tools/perl5/modules/lib/perl5/x86_64-linux-thread-multi:/tools/perl5/modules/lib/perl5:/usr/local/bioinfo/src/ensembl-api/current/ensembl/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-variation/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-functgenomics/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-compara/modules:/usr/local/bioinfo/src/ensembl-api/current/ensemblgenomes-api/modules:/tools/perl5/modules/lib/perl5/x86_64-linux-thread-multi:/tools/perl5/modules/lib/perl5:/usr/local/bioinfo/src/Eval/latest:/usr/local/bioinfo/src/tRNAscan-SE/latest/bin:/usr/local/bioinfo/src/CEGMA/current/lib';
}


#Autres variables
my $cmd = '';
my $basename = '"_seq"';
my $Nseq ='3000';
my $sizeseqmin ='4000';
my $Nseq_protlenmin ='50';
 

#Avant de lancer la commander, il faut créer les répertoires résultats:
$cmd = "mkdir -p ".$dirresults."/protNseq ".$dirresults."/protNseqSizemin ".$dirresults."/VEP_NSC ".$dirresults."/VEP_STOP";
system $cmd;

$cmd = $PROTSEQ." --annot_file ".$annot_file." --genome_fasta_file ".$genome_fasta_file." --infile ".$vep_in." --outfileprot ".$fasta_out." --outfileprot_codedName ".$fasta_encoded_out." --species ".$config->{specie}." --registry_file ".$registry." --delta ".$config->{delta}." --out_fastas_single_dir ".$dirresults."/protNseq --outencodingtable ".$encoded_name_out." --out_fastas_Nseq_dir ".$dirresults."/protNseq --Nseq ".$Nseq." --base_name ".$basename." --sizeseqmin ".$sizeseqmin." --out_fastas_Nseq_dir_protlenmin ".$dirresults."/protNseqSizemin --Nseq_protlenmin ".$Nseq_protlenmin." --out_vep_NSC_dir ".$dirresults."/VEP_NSC --out_vep_STOP_dir ".$dirresults."/VEP_STOP --outvep_res ".$dirresults."/vep_results.txt --outvep_title ".$dirresults."/vep_title.txt 2>&1 ";


#Fournir la ligne de commande au biologiste via le stdout
print STDOUT "La ligne de commande utilisee est : \n\n $cmd \n\n";
system $cmd;

#recuperer les deux fichiers .vep pour le script extract conservation
if (! -e "".$dirresults."/VEP_NSC/NSC_file.vep" || ! -e $dirresults."/vep_title.txt"){
	print "vep_title.txt or NSC_file.vep NOT FOUND\n";
}else{
	`cat "$dirresults/vep_title.txt" "$dirresults/VEP_NSC/NSC_file.vep" > $config->{vep_out} 2>&1`;
} 

if (! -e $fasta_out){
	print STDERR "$fasta_out fasta FILE NOT FOUND\n";
}
else{
	`(cp -a $fasta_out $config->{fasta_out}) >& ./protseq.log 2>&1`;
}

if (! -e $fasta_encoded_out){
	print STDERR "$fasta_encoded_out fasta FILE NOT FOUND\n";
}
else{
	`(cp -a $fasta_encoded_out $config->{fasta_encoded_out}) >& ./protseq.log 2>&1`;
}

if (! -e $encoded_name_out){
	print STDERR "$encoded_name_out fasta FILE NOT FOUND\n";
}
else{
	`(cp -a $encoded_name_out $config->{encoded_name_out}) >& ./protseq.log 2>&1`;
}
