#!/usr/bin/perl -w

# usage : perl plot1.pl et stat1_V2.pl
# Sarah Maman - 1 aout 2012 - Mis en place pour le traitement des mi-RNAs
# Copyright (C) 2012 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 Config::IniFiles;
my $input_fq     = $ARGV[0];
my $input_log    = $ARGV[1];
my $output_html  = $ARGV[2];
my $cmd          = '';

#pour connaître le path vers les scripts wrappes
my $cfg = Config::IniFiles->new( -file => "/usr/local/bioinfo/src/galaxy-dev/galaxy-dist/PATH.ini" );
my $PLOT1 = $cfg->val( 'toolsPath', 'PLOT1' );
my $STAT1 = $cfg->val( 'toolsPath', 'STAT1' );

my $input_dir=dirname($input_fq);
my $input_basename=basename($input_fq,'.html');
my $input_plot1="$input_dir/${input_basename}-report";

#Production du rapport txt
$cmd = "($STAT1 $input_fq $input_log > $input_fq.txt) >& ./stat.log 2>&1";
system $cmd;
#Production du rapport html a partir du rapport txt
$cmd = "($PLOT1 $input_fq.txt > ${input_plot1}.html) >& ./plot.log 2>&1";
system $cmd;

#Recuperation du fichier de sortie par Galaxy
if (! -e "${input_plot1}.html"){print STDERR "Echec lors de la generation du rapport html\n";}
else{$cmd = "(cp -a ${input_plot1}.html $output_html) >& ./cp_plot1.log 2>&1";system $cmd;}
