#!/usr/bin/perl -w

# usage : perl run_netphos.pl
# Sarah Maman - 14/11/2012 - Maria Bernard 19/08/2015
# version : 1.1.0 : removing the use of output directory
# more /usr/local/bioinfo/src/ergatisdev/current/bin/AnnotationPipelines/bin/run_netphos.pl
# Copyright (C) 20XX 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 POSIX;

my $config = {} ;

GetOptions(
  $config,
  'fasta|f=s',
  'encoded_name|n=s',
  'delta|t=s',
  'netphos_rawout=s',
  'netphos_out=s',
  'dirname|d=s'
) or die "ERROR: Failed to parse command line options\n";

if (! defined $config->{dirname} ){
	$config->{dirname}= dirname $config->{netphos_out} ;
}

#pour connaître le path du tool
my $cfg = Config::IniFiles->new( -file => "/path/to/PATH.ini" );
my $NETPHOS_BIN = $cfg->val( 'toolsPath', 'NETPHOS_BIN_PATH' );
my $fastasplit = $cfg->val( 'toolsPath', 'fastasplit_PATH' );
my $SCRIPT_CONSERVATION_MAISON= $cfg->val( 'toolsPath', 'SCRIPT_CONSERVATION_MAISON' );
my $NETPHOS_PERL = $SCRIPT_CONSERVATION_MAISON."/run_netphos.pl" ;

my $cmd = '';

# split fasta by 2000 seq
my $nb_seq = `grep -c '^>' $config->{fasta}`;
chomp $nb_seq;
my $nb_files = ceil($nb_seq/2000);
print STDOUT "nb_seq : ".$nb_seq."\nnb_files :".$nb_files."\n";

$cmd = "mkdir ".$config->{dirname}."/SplitFa; chmod -R 777 ".$config->{dirname}."/SplitFa";
system $cmd;
$cmd = $fastasplit." -f ".$config->{fasta}." -o ".$config->{dirname}."/SplitFa -c ".$nb_files ; 
system $cmd;

print STDOUT "Nombre de paquets de séquences analysées : ".$nb_files."\n";
print STDOUT "Le ligne de commande utilisée est :\n".$cmd."\n\n";

#commande netphos
#ATTENTION /usr/local/bioinfo/bin/netphos-3.1 est absente sur Genotoul, Patrice a donc créé un lien symbolique de netphos-3.1 vers netphos.
#Lancer NETPHOS en boucke sur tous les fichiers fasta issus du split
my @FASTAs = glob($config->{dirname}."/SplitFa/*");
my @netphos_results = ();
my $cpt = 1 ;
foreach my $i (@FASTAs)
{
    print STDOUT "\n##\tLaunch netphos on $i\n\n";
    $cmd = $NETPHOS_PERL." --infile ".$i." --outpath ".$config->{dirname}." --execommand ".$NETPHOS_BIN." --delta ".$config->{delta}." --outfile ".$config->{dirname}."/netphos_results_".$cpt.".txt --encodingfile ".$config->{encoded_name}." --base_name '_seq' 2>&1";
    print STDOUT "#La ligne de commande utilisee est : \n".$cmd."\n\n";
    print STDOUT "\n#Netphos log";
    system $cmd;
	$cmd = "cat ".$config->{dirname}."/errors.txt";
	system $cmd;

    push (@netphos_results, $config->{dirname}."/netphos_results_".$cpt.".txt" );
    $cpt += 1;
}

#Merge de l'ensemble des results et title produits
my $cmd2 =  "cat @netphos_results >  ".$config->{dirname}."/netphos_results.txt" ;
system $cmd2;

#Elimination des fichiers temporaires
foreach my $i (@netphos_results){`rm $i`;}

#Recuperation des fichiers resultat par galaxy
if (! -e $config->{dirname}."/netphos_results.txt" || ! -e $config->{dirname}."/netphos_title.txt") {
	print STDERR "Pas de fichier netphos_title.txt ou netphos_results.txt produit \n" ; 
}else {
	$cmd = "sed 's/\t/\tnetphos /g' ".$config->{dirname}."/netphos_title.txt |cat - ".$config->{dirname}."/netphos_results.txt >".$config->{netphos_out};
	system $cmd;
}

if (! -e $config->{dirname}."/netphos_output.txt") 
{
	print STDERR "Pas de fichier netphos_output.txt produit \n" ; 
}else {
	$cmd = "mv ".$config->{dirname}."/netphos_output.txt ".$config->{netphos_rawout};
	system $cmd;
}
