#!/usr/bin/perl -w

# usage : perl mirdeepcore.pl
#sarah maman - 04/10/2012

use strict;
use File::Basename; 


#fichier entrant fasta (apres mapper)
my $input_fasta = $ARGV[0];
#reference 
my $input_ref = $ARGV[1];
#fichier entrant arf (apres mapper)
my $input_arf = $ARGV[2];
#fichiers sortants output1 = html
my $output1 = $ARGV[3];
#fichiers sortants output2 = csv
my $output2 = $ARGV[4];
#fichiers sortants output3 = bed
my $output3 = $ARGV[5];
#récupération du fichier généré dans un répertoire
#my $input_dir = dirname($input1); 
#my $input_basename=basename($input1,'.log'); 
#my $input_log="$input_dir/${input_basename}-log"; 


#drwxr-xr-x  2 smaman BIOINFO  16K 12 oct.  15:13 dir_prepare_signature1350047604
#-rw-r--r--  1 smaman BIOINFO  377 12 oct.  15:34 error_12_10_2012_t_15_12_24.log
#-rw-r--r--  1 smaman BIOINFO  20K 12 oct.  15:34 result_12_10_2012_t_15_12_24.csv
#-rw-r--r--  1 smaman BIOINFO 233K 12 oct.  15:34 result_12_10_2012_t_15_12_24.html
#drwxr-xr-x  2 smaman BIOINFO  16K 12 oct.  15:37 pdfs_12_10_2012_t_15_12_24
#drwxr-xr-x  4 smaman BIOINFO  16K 12 oct.  15:37 .
#-rw-r--r--  1 smaman BIOINFO 7,9K 12 oct.  15:37 result_12_10_2012_t_15_12_24.bed


#pour connaître le path du tool
my $MIR = '/usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mirdeep2/miRDeep2.pl';
my $cmd = '';

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

#Avant de lancer la commander, il faut créer le répertoire résultat dans /work/galaxy/:
$cmd="cd /work/galaxy/; mkdir ${ALL}_mirdeep2 ; chmod a+w ${ALL}_mirdeep2;";
system $cmd;
my $inputnone = "none";

$ENV{'PATH'}.=':/usr/local/bioinfo/bin';

#Se positionner dans un repertoire specifique avant de lancer la commande mirdeep2
$cmd="cd /work/galaxy/${ALL}_mirdeep2/;";
system $cmd;

#commande mirdeep2core
#$cmd = "($MIR $input1 $input2 $hairpin $mature -t $input3 2> ${input_log}.log) >& ./mirdeep2core.log 2>&1";
$cmd = "($MIR $input_fasta $input_ref $input_arf $inputnone $inputnone $inputnone 2> report_mirdeep2core.log) >& ./mirdeep2core.log 2>&1";
#print $cmd,"\n";
#perl /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mirdeep2/miRDeep2.pl reads.fa bank/Oncorhynchus_mykiss_V1.0.fa mapped.arf none none none -r TOTO/ 2> report.log
#attention de bien placer l option -r à la fin de la commande
system $cmd;

print ("fichiers resultats ************* ls /work/galaxy/${ALL}_mirdeep2/ \n");


#tests sur le glob 
#my $html = glob '*.txt';
#print "$html\n";
#if (! -e $html){print "NOK \n";}
#else {print "OKKKKKKKKKKKK";}

my $path = substr($input_fasta,0,32);
print ("CHEMIN ************* $path \n");
my $jour = $mday;
my $mois = $mon+1;
my $annee = $year;
my $heure = $hour ;
my $minutes = $min ;
my $secondes = $sec ;

#my $html = glob "$path/result_$jour"_"$mois"_"$annee"_t_"$heure"_"$minutes"_"$secondes.html";
my $html2 = glob "result_".$jour."_".$mois."_".$annee."_t_".$heure."_".$minutes."_".$secondes.".html";
#print ("RECHERCHE FICHIER : $html  *******************  \n");
print ("RECHERCHE FICHIER : $html2  *******************  \n");

#my $html = glob "/work/galaxy/${ALL}_mirdeep2/result_$ALL*.html";


#if (! -e "/work/galaxy/${ALL}_mirdeep2/$html"){print STDERR "Pas de fichier html produit \n";}
#else {$cmd = "(cp -a \"/work/galaxy/${ALL}_mirdeep2/$html\" $output1) >& ./cp_mirdeep2core_html.log 2>&1";
#system $cmd;}


my $csv = glob "/work/galaxy/${ALL}_mirdeep2/result_$ALL*.csv"; 
if (! -e "/work/galaxy/${ALL}_mirdeep2/$csv"){print STDERR "Pas de fichier csv produit \n";}
else {$cmd = "(cp -a \"/work/galaxy/${ALL}_mirdeep2/$csv\" $output2) >& ./cp_mirdeep2core_csv.log 2>&1";
system $cmd;}


my $bed = glob "/work/galaxy/${ALL}_mirdeep2/result_$ALL*.bed"; 
if (! -e "/work/galaxy/${ALL}_mirdeep2/$bed"){print STDERR "Pas de fichier fa produit \n";}
else {$cmd = "(cp -a \"/work/galaxy/${ALL}_mirdeep2/$bed\" $output3) >& ./cp_mirdeep2core_bed.log 2>&1";
system $cmd;}
