#!/usr/bin/perl -w

#usage : perl netCglyc.pl
#sarah maman - 11/10/2012
# Sabrina Rodriguez - 16/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',
  'netcglyc_rawout=s',
  'netcglyc_out=s',
  'dirname|d=s'
) or die "ERROR: Failed to parse command line options\n";


# 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->{netcglyc_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 $NETCGLYC_BIN = $cfg->val( 'toolsPath', 'NETCGLYC_BIN_PATH' );
my $SCRIPT_CONSERVATION_MAISON= $cfg->val( 'toolsPath', 'SCRIPT_CONSERVATION_MAISON' );
my $NETCGLYC_PERL = $SCRIPT_CONSERVATION_MAISON."/extract_netcglyc_results.pl" ;
my $cmd = '';

#commande
print STDOUT "\n##\tLaunch netcglyc\n\n";
$cmd = "$NETCGLYC_BIN "."input.fasta"." > raw_output.txt";
print STDOUT "#Command line used : \n".$cmd."\n\n";
system $cmd;

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


if (! -e "raw_output.txt" ){print STDERR "Pas de fichier results produit \n";}
else {`cp raw_output.txt $config->{netcglyc_rawout}`;}
if (! -e $config->{dirname}."/netcglyc_results.txt" ){print STDERR "Pas de fichier results produit \n";}
else {$cmd = "cat ".$config->{dirname}."/netcglyc_title.txt ".$config->{dirname}."/netcglyc_results.txt > ".$config->{netcglyc_out};
system $cmd;}


