#!/usr/bin/perl -w

# usage : perl sm_mothur_preprocess_1.pl <input FASTA> <input config txt>
# 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_config   = $ARGV[1];
my $bF             = $ARGV[2];
my $bR             = $ARGV[3];
my $PF             = $ARGV[4];
my $PR             = $ARGV[5];
my $minlenght      = $ARGV[6];#taille minimum de lecture après trimming. Par default : 200
my $NB_BARCODE      = $ARGV[7];
my $output_fasta   = $ARGV[8];
my $output_all_trim_count_table  = $ARGV[9]; #all.trim.count_table
my $output_html    = $ARGV[10];





my $path= dirname($input_fasta);
my ($nb) = ($output_html=~/galaxy_dataset_(\d+)\.\S+$/);
#my $nb = "TATA";

my $MOTHUR1 = '/home/sigenae/bin/Galaxy/r454_diversity_Galaxy/1_preprocess_mothur1.1.sh'; 
my $cmd = '';
#Repertoire de sortie cree par le script, verif des droits d'ecriture sur ce repertoire de sortie
`cd $path; mkdir $nb/; chmod 777 $nb/`;
my $dirresults= $path."/".$nb;

#print STDOUT "nb : $nb \n path : $path  \n dirresults : $dirresults \n";

#modification de l extension des inputs
`cp $input_fasta $dirresults/input.fasta; cp $input_config $dirresults/config.txt`;



#TESTS commande : perl sm_mothur_preprocess_1.pl /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/batch1_16S_extract.fasta /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/454_Thailande_2013_16S_correct.txt 0 0 4 1 200 outgz outfasta outgroups outhtmlgalaxy_dataset_123 outnames
#mkdir out/; chmod 777 out/; cd out/; sh /home/sigenae/bin/Galaxy/r454_diversity_Galaxy/1_preprocess_mothur1.1.sh /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/batch1_16S_extract.fasta  /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/out/ /usr/local/bioinfo/src/galaxy/galaxy-dist/tools/sm_mothur/tests2/454_Thailande_2013_16S_correct.txt 0 0 4 1 200

#$cmd = "(cd $dirresults; sh $MOTHUR1 $input_fasta $nb $input_config $bF $bR $PF $PR) >& ./mothur1.log 2>&1";
$cmd = "(cd $dirresults; sh $MOTHUR1 $dirresults/input.fasta $dirresults $dirresults/config.txt $NB_BARCODE $bF $bR $PF $PR $minlenght) >& ./mothur1.log 2>&1";
system $cmd;
#GALAXY_BIN/r454_diversity_Galaxy/1_preprocess_454_mothur1.2.sh input.fasta /path/to/work_dir /path/to/config_file NB_BARCODE BFM BRM PFM PRM MIN_LEN



#Info pour les biologistes
print STDOUT "Step 1 - Pre-process Mothur : \n\n $cmd \n\n ";


#Recuperation des fichiers par Galaxy
if (! -e "$dirresults/all.trim.count_table"){print STDERR "all.trim.count_table file not found. \n";}else{`cp -a $dirresults/all.trim.count_table $output_all_trim_count_table`;}
if (! -e "$dirresults/all.trim.unique.fasta"){print STDERR "all.trim.unique.fasta file not found. \n";}else{`cp -a $dirresults/all.trim.unique.fasta $output_fasta`;}
#if (! -e "$dirresults/individuals_fasta.tgz"){print STDERR "individuals_fasta.tgz not found. \n";}else{`cp -a $dirresults/individuals_fasta.tgz $output_gz`;}
if (! -e "$dirresults/bilan.html"){print STDERR "bilan.html file not found. \n";}else{`cp -a $dirresults/bilan.html $output_html`;}
