#!/usr/bin/perl -w

#usage : perl netoglyc.pl
#sarah maman - 11/10/2012
# sabrina rodriguez - 19/10/15

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

my $config = {} ;

GetOptions(
  $config,
  'fasta|f=s',
  'encoded_name|n=s',
  'delta|t=s',
  'netoglyc_rawout=s',
  'netoglyc_out=s',
  'dirname|d=s'
) or die "ERROR: Failed to parse command line options\n";

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

# create a working dir
my $cfg = Config::IniFiles->new( -file => "/path/to/PATH.ini" );
my $file_path = $cfg->val( 'workPath', 'FILEPATH_DEV' );
my ($nb) = ($config->{netoglyc_out}=~/dataset_(\d+)\.\S+$/);
chdir($file_path);
my $a=`ls | head -n 7`;

`cd $file_path; mkdir $nb/; chmod 777 $nb/; cd $nb/;`;
$config->{dirname}= $file_path.$nb."/";
chdir($config->{dirname});
print "Working directory : $config->{dirname}\n";
`cp $config->{fasta} input.fasta`;


#pour connaître le path du tool
my $NETOGLYC_BIN = $cfg->val( 'toolsPath', 'NETOGLYC_BIN_PATH' );
my $SCRIPT_CONSERVATION_MAISON= $cfg->val( 'toolsPath', 'SCRIPT_CONSERVATION_MAISON' );
my $NETOGLYC_PERL = $SCRIPT_CONSERVATION_MAISON."/extract_netoglyc_results.pl" ;

my $cmd = '';

#commande
print STDOUT "\n##\tLaunch netoglyc\n\n";
#~ $cmd = "$NETOGLYC_BIN ".$config->{fasta}." > ".$config->{netoglyc_rawout}." 2>&1";
$cmd = "$NETOGLYC_BIN "."input.fasta"." > raw_output.txt";
print STDOUT "#Command line used : \n".$cmd."\n\n";
system $cmd;

print STDOUT "\n##\tLaunch extract_netoglyc_results.pl\n\n";
$cmd = "$NETOGLYC_PERL --infile raw_output.txt --delta ".$config->{delta}." --encodingfile ".$config->{encoded_name}." --outputfile ".$config->{dirname}."/netoglyc_results.txt  --base_name \"_seq\" 2>&1";
print STDOUT "#Command line used : \n".$cmd."\n\n";
system $cmd;

#~ if (! -e $config->{dirname}."/netoglyc_results.txt" ){print STDERR "Pas de fichier results produit \n";}
#~ else {$cmd = "cat ".$config->{dirname}."/netoglyc_title.txt ".$config->{dirname}."/netoglyc_results.txt > ".$config->{netoglyc_out};
#~ system $cmd;}

if (! -e "raw_output.txt" ){print STDERR "Pas de fichier results produit \n";}
else {`cp raw_output.txt $config->{netoglyc_rawout}`;}

if (! -e $config->{dirname}."/netoglyc_results.txt" ){print STDERR "Pas de fichier results produit \n";}
else {$cmd = "cat ".$config->{dirname}."/netoglyc_title.txt ".$config->{dirname}."/netoglyc_results.txt > ".$config->{netoglyc_out};
system $cmd;}


