#!/usr/bin/perl -w

# usage : sm_gtfgff.pl
# sarah maman - 12 dec 2013 - 
# Copyright (C) 2013 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 warnings;
use File::Basename;
use Config::IniFiles;

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

#Inputs files
my $input_gtf        = $ARGV[0];

#Galaxy outputs files
my $output_gff       = $ARGV[1];
my ($nb) = ($output_gff=~/galaxy_dataset_(\d+)\.\S+$/);
my $cmd; my $cmd2;
my $CONVERT_PATH = $cfg->val( 'toolsPath', 'CONVERT_PATH' );
my $CONVERT = $CONVERT_PATH."/sm_gtf_to_gff.py" ;
my $LOGS= $cfg->val( 'toolsPath', 'LOGS_PATH' );

`cp $input_gtf ${input_gtf}.gft`;

#Lancer la premiere etape de traitement
$cmd = "(python $CONVERT ${input_gtf}.gft > ${input_gtf}.gff) >& $LOGS/sm_convertGTFGFF_$nb.log 2>&1;";
system $cmd;

#Info pour les biologistes
print STDOUT "Convert GTF to GFF : \n\n $cmd \n\n ";

#Recuperation des output Galaxy
my $gff = "${input_gtf}.gff";
if (! -e $gff ){print STDERR "Echec lors de la generation du fichier de sortie $gff \n";} 
else 
{$cmd2 = "(cp -a $gff $output_gff) >& ./cpgtftogff.log 2>&1"; system $cmd2;}
