#!/usr/bin/perl -w

# usage : perl netphos.pl
# Sarah Maman - 11/10/2012
# 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 $cfg = Config::IniFiles->new( -file => "/path/to/PATH.ini" );

my $input1   = $ARGV[0];
my $output1  = $ARGV[1];
my $output2  = $ARGV[2];

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); 
my $ALL =  $mday."-".($mon+1)."_".$hour."h".$min."mn".$sec."sec"; 

#Récuperer le numero (unique) de l'output 
my ($nboutput) = ($output1=~/galaxy_dataset_(\d+)\.\S+$/);

$ENV{PERL5LIB} = '/usr/local/bioinfo/src/ensembl-api/current/ensembl/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-variation/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-compara/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-functgenomics/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-variation/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-functgenomics/modules:/usr/local/bioinfo/src/ensembl-api/current/ensembl-compara/modules:/tools/perl5/modules/lib/perl5/x86_64-linux-thread-multi:/tools/perl5/modules/lib/perl5';


my $NETPHOS = $cfg->val( 'toolsPath', 'NETPHOS_PATH' );
my $GALAXYDATA = $cfg->val( 'workPath', 'FILEPATH_DEV' ); 
my $LOGS= = $cfg->val( 'toolsPath', 'LOGS_PATH' );
my $cmd = '';

#Creation du repertoire de sortie de l'image
$cmd = "mkdir $GALAXYDATA/${ALL}_images/; chmod -R 777 $GALAXYDATA/${ALL}_images/;";
system $cmd;

#commande netphos
$cmd = "($NETPHOS $input1 -g -d $GALAXYDATA/${ALL}_images/ > out.txt) >& $LOGS/netphos_$nboutput.log 2>&1";
system $cmd;


print STDOUT "La ligne de commande utilisee est : \n\n $$cmd \n\n";

if (! -e "out.txt") 
{print STDERR "Pas de fichier txt produit \n" ; }
else 
{
$cmd = "(mv out.txt $output1) >& ./cp_netphos_txt.log 2>&1";
system $cmd;
}


#Ouverture des images dans un fichier html (ouput2)
#creation d'un fichier html
open (HTML, ">", "results_netphos.html") or die "Cannot create results_netphos.html";

print HTML "Voici l'ensemble des graphiques generes par NETPHOS :";
print HTML ("<br><br>");

my @GIF = glob("$GALAXYDATA/${ALL}_images/netphos-3.1b._seq*.gif");
my $i ='';

foreach $i (@GIF)
{
	my $url="$i";
	$url=~s/^\/galaxydata\/database\/files\/workspace\/${ALL}_images/\/galaxy-dev\/download\/${ALL}_images/;
	print HTML ("<br>");
	print HTML "<img src=\"$url\">";
	print HTML ("<br><br>");
}

if (! -e "results_netphos.html"){print STDERR "Pas de fichier html produit \n";}
else {$cmd = "(mv results_netphos.html $output2) >& ./cp_netphos_html.log 2>&1";system $cmd;}

close( HTML );
