#!/usr/bin/perl -w

# usage : perl sm_mothur_align_2.pl <output TXT file> <BAM file 1> <BAM file 2> 
# 02/08/2013 - Wrapper dans le cadre du projet 16S (pour Maria B.)

use strict;
use File::Basename;

my $input_fasta             = $ARGV[0];
my $input_names             = $ARGV[1];
my $input_groups            = $ARGV[2];
my $input_ref               = $ARGV[3];
my $output_html             = $ARGV[4];
my $output_all_final_fasta  = $ARGV[5];
my $output_all_final_names  = $ARGV[6];
my $output_all_final_groups = $ARGV[7];

my $MOTHUR2 = '/home/sigenae/bin/Galaxy/r454_diversity_Galaxy/2_align_mothur1.0.sh';
my $cmd = '';
my $cmd1 = '';
my $cmd2 = '';


my $path= dirname($input_fasta);
my ($nb) = ($output_html=~/galaxy_dataset_(\d+)\.\S+$/);
#Repertoire de sortie cree par le script, verif des droits d'ecriture sur ce repertoire de sortie
`mkdir $path/$nb/; chmod 777 $path/$nb/`;
my $dirresults= $path."/".$nb;
print STDOUT "Votre repertoire de travail : $dirresults \n";


#Modification de l extension des inputs
$cmd1 = "ln -s $input_fasta $dirresults/all.trim.unique.fasta; ln -s $input_names $dirresults/all.trim.names; ln -s $input_groups $dirresults/all.groups; ln -s $input_ref $dirresults/ref.fasta;";
system $cmd1;
print STDOUT "Step 1 - Recuperation des fichiers d entree : \n\n $cmd1 \n\n ";

#mkdir /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/MOTHUR2; chmod 777 /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/MOTHUR2; cd /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/MOTHUR2; /home/sigenae/bin/Galaxy/r454_diversity_Galaxy/2_align_mothur1.0.sh /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/TATA/TATA/all.trim.unique.fasta /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/TATA/TATA/all.trim.names /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/TATA/TATA/all.groups /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/MOTHUR2 /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/silva.bacteria/silva.bacteria.fasta 
#2_align_mothur1.0.sh path_INPUT_FASTA path_INPUT_NAMES path_INPUT_GROUPS path_to_OUTDIR path_DB_REF MIN_LEN
#TESTS commande : perl sm_mothur_align_2.pl /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/TATA/TATA/all.trim.unique.fasta /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/TATA/TATA/all.trim.names /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/TATA/TATA/all.groups /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/silva.bacteria/silva.bacteria.fasta out_galaxy_dataset_1234.dathtml outfasta outnames outgroups
#attention, rechercher les resultats dans /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/TATA/TATA/1234
#perl sm_mothur_align_2.pl /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/TATA/TATA/all.trim.unique.fasta /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/TATA/TATA/all.trim.names /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/TATA/TATA/all.groups /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/silva.bacteria/silva.bacteria.fasta out_galaxy_dataset_123456789.dathtml outfasta outnames outgroups


#$cmd = "(cd $dirresults; $MOTHUR2 ${input_fasta}.fasta $input_names $input_groups $dirresults ${input_ref}.fasta) >& ./mothur2.log 2>&1";
$cmd = "(cd $dirresults; $MOTHUR2 $dirresults/all.trim.unique.fasta $dirresults/all.trim.names $dirresults/all.groups $dirresults $dirresults/ref.fasta) >& ./mothur2.log 2>&1";
#pour les tests sur la vm car maria fait des liens symboliques et il ne faut pas changer les noms
#cette ligne de ccde est OK mais les fichiers galaxy etant renommes et extension aussi, ca ne marchera pas via l interface
#$cmd = "(cd $dirresults; $MOTHUR2 $input_fasta $input_names $input_groups $dirresults $input_ref) >& ./mothur2.log 2>&1";
system $cmd;

#Info pour les biologistes
print STDOUT "Step 2 - Alignement Mothur : \n\n $cmd \n\n ";

#Suppression des fichiers inutiles
#$cmd2 = "rm $dirresults/*mothur*; rm $dirresults/all.trim.*; rm $dirresults/all.filter; rm $dirresults/all.good.groups*; rm $dirresults/*.pdf; rm $dirresults/*ref*;rm $dirresults/all.groups ; ";
#system $cmd2;


#Recuperation des fichiers par Galaxy
if (! -e "$dirresults/all.final.fasta"){print STDERR "all.final.fasta file not found. \n";}else{`cp -a $dirresults/all.final.fasta $output_all_final_fasta`;}
if (! -e "$dirresults/all.final.names"){print STDERR "all.final.names file not found. \n";}else{`cp -a $dirresults/all.final.names $output_all_final_names`;}
if (! -e "$dirresults/all.final.groups"){print STDERR "all.final.groups file not found. \n";}else{`cp -a $dirresults/all.final.groups $output_all_final_groups`;}
if (! -e "$dirresults/bilan.html"){print STDERR "bilan html file not found. \n";}else{`cp -a $dirresults/bilan.html $output_html`;}
