#!/usr/bin/perl -w

# usage : perl sm_tophat.pl <read1 file> <read2 file>
# Sarah Maman - 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 File::Basename;

my $input_gff                    = $ARGV[0];
my $output_gtf                   = $ARGV[1];

#/usr/local/bioinfo/src/Eval/latest/gff3_to_gtf.pl gff3-file

my $GFFGTF = '/usr/local/bioinfo/src/Eval/latest/gff3_to_gtf.pl';
my $cmd = '';
my $path = "/galaxydata/database/files/workspace/";
my ($nb) = ($output_gtf=~/galaxy_dataset_(\d+)\.\S+$/);

#Creation du repertoire de sortie des resultats
`cd $path/; mkdir $nb/; chmod -R 777 $nb/;`;
`ln -s $input_gff $path/$nb/input_gff.gff;`

$cmd = "($GFFGTF $path/$nb/input_gff.gff) >& ./convert_gff.log 2>&1";
#Info pour les biologistes
print STDOUT "Convert GFF to GTF : \n\n $cmd \n\n ";
system $cmd;


my $gtf =  glob("$path/$nb/*.gtf");
if (! -e $gtf){print STDERR "GTF FILE NOT FOUND\n";} else { `mv $gtf $output_gtf`;}
