#!/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; 


#genome de reference
my $input1 = $ARGV[3];
#pour les options "$collapse"
my $collapse = $ARGV[2];
my $output1 = $ARGV[0];
my $output2 = $ARGV[1];
#reads
my $input2 = @ARGV[4..$#ARGV];
my $nbFastaFiles = @ARGV; 

#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"; 

#Ouverture des fichiers entrants
open (IN, "<$input1") or die "Cannot open $input1 !";


#TESTS
$collapse ="YES";
if ($collapse eq "YES"){
#$collapse_code = "-m -s @ARGV[4..$#ARGV]_collapse.fa";
$collapse_code = "-m -s ${ALL}_collapse.fasta";
$collapse_code2 = "-m -s ${ALL}_collapse.fasta";      
}
else {
$collapse_code = "";
$collapse_code2 = "";
}

#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 > 5){
	#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 = "config_".$ALL.".txt";
	open FILE,">$config";

	my $cpt=1;

	#foreach my $file (@ARGV){
        foreach my $file (@ARGV[4..$#ARGV]){
	#foreach my $file (@ARGV[3..$#ARGV]){
		my $id="s";
		if(length($cpt)<2){
			$id = $id."0".$cpt;
		}
		else{
			$id = $id."$cpt";
		}
		print FILE $file,"\t",$id,"\n"; 
		$cpt++;
	}

	#close FILE;



	#lancer la commande (sans nettoyer les adaptateurs car cela est déjà fait avec le cutadapt
	$cmd = "($MAPPER $config -d -c -j $collapse_code2 -p $input1 -t ${ALL}_mapped.arf -v) >& ./mapperPlrsFasta.log 2>&1";
	system $cmd;
#print "\n *Commande - plusieurs fastas: $cmd \n";

		if (! -e "${ALL}_mapped.arf"){print STDERR "Echec a la generation du fichier mapped.arf\n";}
		else {$cmd = "(cp -a \"${ALL}_mapped.arf\" $output1) >& ./cp_mapper_output1.log 2>&1";
		system $cmd;}

		if (! -e "${ALL}_collapse.fasta"){print STDERR "Echec a la generation du fichier genome.fa \n";}
		else {$cmd = "(cp -a \"${ALL}_collapse.fasta\" $output2) >& ./cp_mapper_output2.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 = "($MAPPER @ARGV[4..$#ARGV] -c -j $collapse_code -p $input1 -t ${ALL}_reads_collapsed_vs_genome.arf -v) >& ./mapper1fasta.log 2>&1";
	system $cmd; 
#print "\n ** Voici la commande utilisée avec un fasta en entree: $cmd \n";


		if (! -e "${ALL}_reads_collapsed_vs_genome.arf"){print STDERR "Echec a la generation du fichier collapse.arf\n";}
		else {$cmd = "(cp -a \"${ALL}_reads_collapsed_vs_genome.arf\" $output1) >& ./cp_mapper_output1.log 2>&1";
		system $cmd;}


		if (! -e "${ALL}_collapse.fasta"){print STDERR "Echec a la generation du fichier genome.fa \n";}
		else {$cmd = "(cp -a \"${ALL}_collapse.fasta\" $output2) >& ./cp_mapper_output2.log 2>&1";
		system $cmd;} 

}
close( IN );
