#!/usr/bin/perl -w

# usage : perl mirdeepcore.pl
#sarah maman - 04/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 File::Temp qw/ tempfile /; 


#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"; 


# HACK OLIVE : modification reference
=pod
my $new_ref = tempfile( CLEANUP => 1);

open(IN , $input_ref) or die "$!";
open(OUT, '>', $new_ref ) or die "$!";

while (my $line =<IN>) {
	chomp $line;
	if ( $line =~ /^>/ ){
		my ($id) =~ /^>(\S+).*/;
		print OUT ">$id\n";
	}
	else {
		print OUT $line ."\n";
	}
}

close (OUT);
close IN;

$input_ref = $new_ref;
=cut

#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 ($nb) = ($output1=~/galaxy_dataset_(\d+)\.\S+$/);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); 
my $ALL =  $mday."-".($mon+1)."_".$hour."h".$min."mn".$sec."sec".$nb."MIRDEEP2"; 


#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 = "(cd $path/${ALL}_mirdeep2/;$MIR $input_fasta $input_ref $input_arf $inputnone $inputnone $inputnone -c 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;



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
my $file = "dataset_".$nb."_files";
$cmd = "mkdir $path/$file; cp -r $path/${ALL}_mirdeep2/pdf* $path/$file/.";
system $cmd;
#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;}

