#!/usr/bin/perl -w

# usage : perl mapper.pl
# Olivier Rué - Sarah Maman - 13/09/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 Getopt::Long;

my $clip ='';
my $discard='';
my $mismatch='';
my $pos='';
my $outputarf ='';
my $outputfa ='';
my $collapse='';
my $inputrefgenome ='';
my $inputfasta='';
my @inputotherfasta =[];


Getopt::Long::Configure( 'no_ignorecase', 'bundling' );
GetOptions (
            'clip =s' => \$clip,
            'discard=i' => \$discard,
            'mismatch=s' => \$mismatch,
            'pos=i' => \$pos,
            'outputarf=s' => \$outputarf,
            'outputfa=s' => \$outputfa,
            'collapse=s' => \$collapse,
            'inputrefgenome=s' => \$inputrefgenome,
            'inputfasta=s' => \$inputfasta,
            'inputotherfasta=s' => \@inputotherfasta
) or die "Usage: Error in command line arguments\n";  

#my  $clip      = $ARGV[0];
#my  $discard   = $ARGV[1];
#my  $mismatch  = $ARGV[2]; 
#my  $pos       = $ARGV[3];
#my $output1    = $ARGV[4];
#my $output2    = $ARGV[5];
##pour les options "$collapse"
#my $collapse   = $ARGV[6];
##genome de reference
#my $input1     = $ARGV[7];
#my $input2     = @ARGV[8..$#ARGV];
#my $nbFastaFiles = @ARGV; 

print STDOUT "
Paramètres : \n
clip 0 : $clip   \n
discard 1 :   $discard   \n
mismatch 2 :   $mismatch \n
pos 3 :  $pos      \n
ouput1 - 4:  $outputarf    \n
outputfa - 5:  $outputfa    \n
collapse -6 :  $collapse   \n
inputrefgenome - 7:  $inputrefgenome     \n
input  $inputfasta \n
 \n";


#print ("var input1 : $input1 \n");
#print ("var collapse : $collapse");
#print ("var input2 : $input2 \n");
#print ("$nbFastaFiles = @ARGV \n");
#print ("ARGV[4..$#ARGV] : @ARGV[4..$#ARGV] \n");

#pour connaître le path vers le script mapper.pl
my $MAPPER = '/usr/local/bioinfo/src/galaxy-dev/galaxy-dist/tools/sm_mirdeep2/mapper.pl';
my $cmd = '';
my $collapse_code = '';
my $collapse_code2 = '';
#Pour recuperer la date et l'heure
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); 
my $ALL =  $mday."-".($mon+1)."_".$hour."h".$min."mn".$sec."sec"; 

print STDOUT "repertoire sortant : ${ALL}_mapper\  \n";
#travailler dans un repertoire specifique
`cd /galaxydata/database/files/workspace/; mkdir ${ALL}_mapper/; chmod 777 ${ALL}_mapper/; ln -s $inputrefgenome /galaxydata/database/files/workspace/${ALL}_mapper/inputrefgenome.fasta`;



my $collapsefasta = "/galaxydata/database/files/workspace/${ALL}_mapper/collapse.fasta"; 
$collapse ="YES";
if ($collapse eq "YES"){
#$collapse_code = "-m -s @ARGV[4..$#ARGV]_collapse.fa";
$collapse_code = "-m -s $collapsefasta";
$collapse_code2 = "-m -s $collapsefasta";      
}
else {
$collapse_code = "";
$collapse_code2 = "";
}

my  $mismatch2  = "";
if ($mismatch eq "YES"){$mismatch2 = " -q ";}else {$mismatch2 ="";}



my  $clip2      = "";
if ($clip eq "None"){$clip2 ="";} else {$clip2=" -k $clip ";}
my  $discard2   = "";
if ($discard eq ""){$discard2 ="";} else {$discard2=" -l $discard ";}
my  $pos2       = "";
if ($pos eq ""){$pos2 ="";} else {$pos2=" -r $pos ";}

print STDOUT "$clip2 \n $discard2 \n $mismatch2 \n$pos2" ;


#Pour tester le code via l'interface Galaxy avec un seul fichier Fasta
#print "\n ** nombre de fichiers entrants : $nbFastaFiles \n";
#print "\n ** variable collapse : $collapse \n";
#print "\n ** variable collapse : $collapse_code \n";
#print "\n ** variable genome : $input1 \n";  
#print "\n ** variable input 2 [4 : @ARGV[4..$#ARGV] \n";        
  


#if ($nbFastaFiles > 9){
if (scalar(@inputotherfasta)>0){
	#Commande mapper POUR PLUSIEURS FICHIERS FASTA
	#perl mapper.pl TRUITE/config.txt -d -c -j -k ATCTCGTATGCCGTCTTCTGCTTG -l 18 -m -p TRUITE/Oncorhynchus_mykiss_V1.0.fa -s TESTS/reads_used.fa -t TESTS/mapped.arf -v
	#creer le fichier config.txt
#commande shell pour recuperer tous les fichiers fasta et ajouter un identifiant a coté (nombre max de 3 caracteres pas géré)
#find . -name \*.fa | perl -ne 'chomp;($n) = $_ =~ /.\/(\S+)/;print $n,"\t","s".$i,"\n";$i++;' > config.txt

	my $config = "/galaxydata/database/files/workspace/${ALL}_mapper/"."config_".$ALL.".txt";
	open FILE,">$config";

	my $cpt=1;

	foreach my $file (@inputotherfasta){
		my $id="s";
		if(length($cpt)<2){
			$id = $id."0".$cpt;
		}
		else{
			$id = $id."$cpt";
		}
		print FILE $file,"\t",$id,"\n"; 
		$cpt++;
	}
     
	#lancer la commande (sans nettoyer les adaptateurs car cela est déjà fait avec le cutadapt
	$cmd = "(cd /galaxydata/database/files/workspace/${ALL}_mapper/; $MAPPER /galaxydata/database/files/workspace/${ALL}_mapper/config $clip2 $discard2 $mismatch2 $pos2 -d -c -j $collapse_code2 -p /galaxydata/database/files/workspace/${ALL}_mapper/input1.fasta -t /galaxydata/database/files/workspace/${ALL}_mapper/mapped.arf -v) >& ./mapperPlrsFasta.log 2>&1";
	system $cmd;
print STDOUT "\n *Commande - plusieurs fastas: $cmd \n";
        my $arf = "/galaxydata/database/files/workspace/${ALL}_mapper/mapped.arf";
		if (! -e $arf){print STDERR "Echec a la generation du fichier mapped.arf\n";}
		else {$cmd = "(cp -a $arf $outputarf) >& ./cp_mapper_outputarf.log 2>&1";
		system $cmd;}
 
        
		if (! -e $collapsefasta ){print STDERR "Echec a la generation du fichier genome.fa \n";}
		else {$cmd = "(cp -a $collapsefasta $outputfa) >& ./cp_mapper_outputfa.log 2>&1";
		system $cmd;} 

close FILE;
                
}
else{
	#commande mapper POUR UN SEUL FASTA
	#perl mapper.pl TRUITE/s_1_uT21.fa -c -j -k TCGTATGCCGTCTTCTGCTTGT -l 18 -m -p TRUITE/Oncorhynchus_mykiss_V1.0.fa -s TESTS/ reads_collapsed.fa -t TESTS/reads_collapsed_vs_genome.arf -v
	#$cmd = "($MAPPER $input2 -c -j $collapse_code -p $input1 -t ${input2}_reads_collapsed_vs_genome.arf -v) >& ./mapper1fasta.log 2>&1";
 	#$cmd = "($MAPPER @ARGV[4..$#ARGV] -c -j $collapse_code -p $input1 -t @ARGV[4..$#ARGV]_reads_collapsed_vs_genome.arf -v) >& ./mapper1fasta.log 2>&1";
        $cmd = "(cd /galaxydata/database/files/workspace/${ALL}_mapper/; $MAPPER @ARGV[8..$#ARGV] $clip2 $discard2 $mismatch2 $pos2 -c -j $collapse_code -p /galaxydata/database/files/workspace/${ALL}_mapper/input1.fasta -t /galaxydata/database/files/workspace/${ALL}_mapper/reads_collapsed_vs_genome.arf -v) >& ./mapper1fasta.log 2>&1";
	system $cmd; 
print STDOUT "\n ** Voici la commande utilisée avec un fasta en entree: $cmd \n";

        my $arfgen = "/galaxydata/database/files/workspace/${ALL}_mapper/reads_collapsed_vs_genome.arf";
		if (! -e $arfgen){print STDERR "Echec a la generation du fichier collapse.arf $arfgen\n";}
		else {$cmd = "(cp -a $arfgen $outputarf) >& ./cp_mapper_outputarf.log 2>&1";
		system $cmd;}


		if (! -e $collapsefasta){print STDERR "Echec a la generation du fichier genome.fa \n";}
		else {$cmd = "(cp -a $collapsefasta $outputfa) >& ./cp_mapper_outputfa.log 2>&1";
		system $cmd;} 

}
