#!/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];
#fichiers sortants output3 = fa
#my $output4 = $ARGV[6];
#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-dev/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"; 


#recuperer le path au niveau duquel s execute le script mirdeep2
#my $path = substr ($input_fasta, 0 , 31);
#my $path = dirname($input_fasta);
my $path = "/galaxydata/database/files/workspace/";


#Avant de lancer la commander, il faut créer le répertoire résultat dans /work/galaxy/:
$cmd="cd $path; mkdir ${ALL}_mirdeep2 ; chmod -R 777 ${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

#commande mirdeep2core
#$cmd = "($MIR $input1 $input2 $hairpin $mature -t $input3 2> ${input_log}.log) >& ./mirdeep2core.log 2>&1";
$cmd = "(cd $path/${ALL}_mirdeep2/;$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;


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


#my $jour = $mday;
#my $mois = $mon+1;
#my $annee = $year;
#my $heure = $hour ;
#my $minutes = $min ;
#my $secondes = $sec ;

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



my $html = glob "$path/${ALL}_mirdeep2/result_*.html";
#print ("RECHERCHE FICHIER : $html  *******************  \n");
if (! -e "$html"){print STDERR "Pas de fichier html produit \n";}
else {$cmd = "(cp -a \"$html\" $output1) >& ./cp_mirdeep2core_html.log 2>&1";
system $cmd;
$cmd = "(cp -a $output1 \"$path/${ALL}_mirdeep2/$output1\") >& ./cp_mirdeep2core_html2.log 2>&1";
system $cmd;
}



#copier toutes les images des pdf pour qu'elles soient accessibles depuis le fichier html
#print ("******* $output1 ********** \n");
my ($nb) = ($output1=~/galaxy_dataset_(\d+)\.\S+$/);
#print ("******* $nb ********** \n");
my $file = "dataset_".$nb."_files";
$cmd = "mkdir $path/$file; cp -r $path/${ALL}_mirdeep2/pdf* $path/$file/.";
system $cmd;
#print ("******* $cmd ********** \n");
#cp -r /work/galaxy/database/files/001/19-11_11h21mn55sec_mirdeep2/pdf* /work/galaxy/database/files/001/dataset_1675_files

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


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

#identified_precursors.fa dans /work/galaxy/database/files/001/25-11_18h13mn8sec_mirdeep2/mirdeep_runs/run_25_11_2012_t_18_13_08/
#my $fa = glob "$path/${ALL}_mirdeep2/mirdeep_runs/run_*/identified_precursors.fa"; 
#if (! -e "$fa"){print STDERR "Pas de fichier fa produit \n";}
#else {$cmd = "(cp -a \"$fa\" $output4) >& ./cp_mirdeep2core_fa.log 2>&1";
#system $cmd;}
